Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for row filtering and column masking access control #24277

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,19 @@
import com.facebook.presto.sql.tree.SubqueryExpression;
import com.facebook.presto.sql.tree.Table;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multiset;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Deque;
import java.util.HashMap;
Expand All @@ -70,6 +73,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

Expand Down Expand Up @@ -157,6 +161,12 @@ public class Analysis

private final Map<NodeRef<QuerySpecification>, List<GroupingOperation>> groupingOperations = new LinkedHashMap<>();

private final Multiset<RowFilterScopeEntry> rowFilterScopes = HashMultiset.create();
private final Map<NodeRef<Table>, List<Expression>> rowFilters = new LinkedHashMap<>();

private final Multiset<ColumnMaskScopeEntry> columnMaskScopes = HashMultiset.create();
private final Map<NodeRef<Table>, Map<String, Expression>> columnMasks = new LinkedHashMap<>();

// for create table
private Optional<QualifiedObjectName> createTableDestination = Optional.empty();
private Map<String, Expression> createTableProperties = ImmutableMap.of();
Expand Down Expand Up @@ -994,6 +1004,59 @@ public Map<FunctionKind, Set<String>> getInvokedFunctions()
return functionMap.entrySet().stream().collect(toImmutableMap(Map.Entry::getKey, entry -> ImmutableSet.copyOf(entry.getValue())));
}

public boolean hasRowFilter(QualifiedObjectName table, String identity)
{
return rowFilterScopes.contains(new RowFilterScopeEntry(table, identity));
}

public void registerTableForRowFiltering(QualifiedObjectName table, String identity)
{
rowFilterScopes.add(new RowFilterScopeEntry(table, identity));
}

public void unregisterTableForRowFiltering(QualifiedObjectName table, String identity)
{
rowFilterScopes.remove(new RowFilterScopeEntry(table, identity));
}

public void addRowFilter(Table table, Expression filter)
{
rowFilters.computeIfAbsent(NodeRef.of(table), node -> new ArrayList<>())
.add(filter);
}

public List<Expression> getRowFilters(Table node)
{
return rowFilters.getOrDefault(NodeRef.of(node), ImmutableList.of());
}

public boolean hasColumnMask(QualifiedObjectName table, String column, String identity)
{
return columnMaskScopes.contains(new ColumnMaskScopeEntry(table, column, identity));
}

public void registerTableForColumnMasking(QualifiedObjectName table, String column, String identity)
{
columnMaskScopes.add(new ColumnMaskScopeEntry(table, column, identity));
}

public void unregisterTableForColumnMasking(QualifiedObjectName table, String column, String identity)
{
columnMaskScopes.remove(new ColumnMaskScopeEntry(table, column, identity));
}

public void addColumnMask(Table table, String column, Expression mask)
{
Map<String, Expression> masks = columnMasks.computeIfAbsent(NodeRef.of(table), node -> new LinkedHashMap<>());
checkArgument(!masks.containsKey(column), "Mask already exists for column %s", column);
masks.put(column, mask);
}

public Map<String, Expression> getColumnMasks(Table table)
{
return columnMasks.getOrDefault(NodeRef.of(table), ImmutableMap.of());
}

@Immutable
public static final class Insert
{
Expand Down Expand Up @@ -1177,4 +1240,71 @@ public boolean isFromView()
return isFromView;
}
}

private static class RowFilterScopeEntry
{
private final QualifiedObjectName table;
private final String identity;

public RowFilterScopeEntry(QualifiedObjectName table, String identity)
{
this.table = requireNonNull(table, "table is null");
this.identity = requireNonNull(identity, "identity is null");
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
RowFilterScopeEntry that = (RowFilterScopeEntry) o;
return table.equals(that.table) &&
identity.equals(that.identity);
}

@Override
public int hashCode()
{
return Objects.hash(table, identity);
}
}

private static class ColumnMaskScopeEntry
{
private final QualifiedObjectName table;
private final String column;
private final String identity;

public ColumnMaskScopeEntry(QualifiedObjectName table, String column, String identity)
{
this.table = requireNonNull(table, "table is null");
this.column = requireNonNull(column, "column is null");
this.identity = requireNonNull(identity, "identity is null");
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ColumnMaskScopeEntry that = (ColumnMaskScopeEntry) o;
return table.equals(that.table) &&
column.equals(that.column) &&
identity.equals(that.identity);
}

@Override
public int hashCode()
{
return Objects.hash(table, column, identity);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.hive.security;

import com.facebook.presto.common.Subfield;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.hive.HiveColumnConverterProvider;
import com.facebook.presto.hive.HiveTransactionManager;
import com.facebook.presto.hive.TransactionalMetadata;
Expand All @@ -26,6 +27,7 @@
import com.facebook.presto.spi.security.ConnectorIdentity;
import com.facebook.presto.spi.security.PrestoPrincipal;
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.ViewExpression;

import javax.inject.Inject;

Expand Down Expand Up @@ -281,4 +283,16 @@ public void checkCanAddConstraint(ConnectorTransactionHandle transaction, Connec
denyAddConstraint(tableName.toString());
}
}

@Override
public Optional<ViewExpression> getRowFilter(ConnectorTransactionHandle transactionHandle, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName)
{
return Optional.empty();
}

@Override
public Optional<ViewExpression> getColumnMask(ConnectorTransactionHandle transactionHandle, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName, String columnName, Type type)
{
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.hive.security;

import com.facebook.presto.common.Subfield;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.hive.HiveColumnConverterProvider;
import com.facebook.presto.hive.HiveConnectorId;
import com.facebook.presto.hive.HiveTransactionManager;
Expand All @@ -30,6 +31,7 @@
import com.facebook.presto.spi.security.PrestoPrincipal;
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.RoleGrant;
import com.facebook.presto.spi.security.ViewExpression;

import javax.inject.Inject;

Expand Down Expand Up @@ -681,6 +683,18 @@ public void checkCanShowRoleGrants(ConnectorTransactionHandle transactionHandle,
{
}

@Override
public Optional<ViewExpression> getRowFilter(ConnectorTransactionHandle transactionHandle, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName)
{
return Optional.empty();
}

@Override
public Optional<ViewExpression> getColumnMask(ConnectorTransactionHandle transactionHandle, ConnectorIdentity identity, AccessControlContext context, SchemaTableName tableName, String columnName, Type type)
{
return Optional.empty();
}

private boolean isAdmin(ConnectorTransactionHandle transaction, ConnectorIdentity identity, MetastoreContext metastoreContext)
{
SemiTransactionalHiveMetastore metastore = getMetastore(transaction);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.facebook.presto.common.QualifiedObjectName;
import com.facebook.presto.common.Subfield;
import com.facebook.presto.common.transaction.TransactionId;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.spi.CatalogSchemaTableName;
import com.facebook.presto.spi.ConnectorId;
import com.facebook.presto.spi.PrestoException;
Expand All @@ -33,8 +34,10 @@
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.SystemAccessControl;
import com.facebook.presto.spi.security.SystemAccessControlFactory;
import com.facebook.presto.spi.security.ViewExpression;
import com.facebook.presto.transaction.TransactionManager;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.weakref.jmx.Managed;
Expand All @@ -55,6 +58,7 @@
import java.util.concurrent.atomic.AtomicReference;

import static com.facebook.presto.metadata.MetadataUtil.toSchemaTableName;
import static com.facebook.presto.spi.StandardErrorCode.INVALID_COLUMN_MASK;
import static com.facebook.presto.spi.StandardErrorCode.SERVER_STARTING_UP;
import static com.facebook.presto.util.PropertiesUtil.loadProperties;
import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -781,6 +785,53 @@ public void checkCanAddConstraints(TransactionId transactionId, Identity identit
}
}

@Override
public List<ViewExpression> getRowFilters(TransactionId transactionId, Identity identity, AccessControlContext context, QualifiedObjectName tableName)
{
requireNonNull(transactionId, "transactionId is null");
requireNonNull(identity, "identity is null");
requireNonNull(tableName, "catalogName is null");

ImmutableList.Builder<ViewExpression> filters = ImmutableList.builder();
CatalogAccessControlEntry entry = getConnectorAccessControl(transactionId, tableName.getCatalogName());
if (entry != null) {
entry.getAccessControl().getRowFilter(entry.getTransactionHandle(transactionId), identity.toConnectorIdentity(tableName.getCatalogName()), context, toSchemaTableName(tableName))
.ifPresent(filters::add);
}

systemAccessControl.get().getRowFilter(identity, context, toCatalogSchemaTableName(tableName))
.ifPresent(filters::add);

return filters.build();
}

@Override
public Optional<ViewExpression> getColumnMask(TransactionId transactionId, Identity identity, AccessControlContext context, QualifiedObjectName tableName, String columnName, Type type)
{
requireNonNull(transactionId, "transactionId is null");
requireNonNull(identity, "identity is null");
requireNonNull(tableName, "catalogName is null");

ImmutableList.Builder<ViewExpression> masks = ImmutableList.builder();

// connector-provided masks take precedence over global masks
CatalogAccessControlEntry entry = getConnectorAccessControl(transactionId, tableName.getCatalogName());
if (entry != null) {
entry.getAccessControl().getColumnMask(entry.getTransactionHandle(transactionId), identity.toConnectorIdentity(tableName.getCatalogName()), context, toSchemaTableName(tableName), columnName, type)
.ifPresent(masks::add);
}

systemAccessControl.get().getColumnMask(identity, context, toCatalogSchemaTableName(tableName), columnName, type)
.ifPresent(masks::add);

List<ViewExpression> allMasks = masks.build();
if (allMasks.size() > 1) {
throw new PrestoException(INVALID_COLUMN_MASK, format("Column must have a single mask: %s", columnName));
}

return allMasks.stream().findFirst();
}

private CatalogAccessControlEntry getConnectorAccessControl(TransactionId transactionId, String catalogName)
{
return transactionManager.getOptionalCatalogMetadata(transactionId, catalogName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.security;

import com.facebook.presto.common.CatalogSchemaName;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.spi.CatalogSchemaTableName;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.security.AccessControlContext;
Expand All @@ -23,6 +24,7 @@
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.SystemAccessControl;
import com.facebook.presto.spi.security.SystemAccessControlFactory;
import com.facebook.presto.spi.security.ViewExpression;

import java.security.Principal;
import java.security.cert.X509Certificate;
Expand Down Expand Up @@ -226,4 +228,16 @@ public void checkCanDropConstraint(Identity identity, AccessControlContext conte
public void checkCanAddConstraint(Identity identity, AccessControlContext context, CatalogSchemaTableName table)
{
}

@Override
public Optional<ViewExpression> getRowFilter(Identity identity, AccessControlContext context, CatalogSchemaTableName tableName)
{
return Optional.empty();
}

@Override
public Optional<ViewExpression> getColumnMask(Identity identity, AccessControlContext context, CatalogSchemaTableName tableName, String columnName, Type type)
{
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.facebook.airlift.log.Logger;
import com.facebook.presto.common.CatalogSchemaName;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.plugin.base.security.ForwardingSystemAccessControl;
import com.facebook.presto.plugin.base.security.SchemaAccessControlRule;
import com.facebook.presto.security.CatalogAccessControlRule.AccessMode;
Expand All @@ -28,6 +29,7 @@
import com.facebook.presto.spi.security.Privilege;
import com.facebook.presto.spi.security.SystemAccessControl;
import com.facebook.presto.spi.security.SystemAccessControlFactory;
import com.facebook.presto.spi.security.ViewExpression;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.airlift.units.Duration;
Expand Down Expand Up @@ -441,6 +443,18 @@ public void checkCanAddConstraint(Identity identity, AccessControlContext contex
}
}

@Override
public Optional<ViewExpression> getRowFilter(Identity identity, AccessControlContext context, CatalogSchemaTableName tableName)
{
return Optional.empty();
}

@Override
public Optional<ViewExpression> getColumnMask(Identity identity, AccessControlContext context, CatalogSchemaTableName tableName, String columnName, Type type)
{
return Optional.empty();
}

private boolean isSchemaOwner(Identity identity, CatalogSchemaName schema)
{
if (!canAccessCatalog(identity, schema.getCatalogName(), ALL)) {
Expand Down
Loading
Loading