use of io.trino.spi.security.Identity in project trino by trinodb.
the class FileBasedSystemAccessControl method checkCanCreateViewWithSelectFromColumns.
@Override
public void checkCanCreateViewWithSelectFromColumns(SystemSecurityContext context, CatalogSchemaTableName table, Set<String> columns) {
if (!canAccessCatalog(context, table.getCatalogName(), ALL)) {
denySelectTable(table.toString());
}
if (INFORMATION_SCHEMA_NAME.equals(table.getSchemaTableName().getSchemaName())) {
return;
}
Identity identity = context.getIdentity();
CatalogTableAccessControlRule rule = tableRules.stream().filter(tableRule -> tableRule.matches(identity.getUser(), identity.getEnabledRoles(), identity.getGroups(), table)).findFirst().orElse(null);
if (rule == null || !rule.canSelectColumns(columns)) {
denySelectTable(table.toString());
}
if (!rule.getPrivileges().contains(GRANT_SELECT)) {
denyCreateViewWithSelect(table.toString(), context.getIdentity());
}
}
use of io.trino.spi.security.Identity in project trino by trinodb.
the class FileBasedSystemAccessControl method filterColumns.
@Override
public Set<String> filterColumns(SystemSecurityContext context, CatalogSchemaTableName tableName, Set<String> columns) {
if (!checkAnyTablePermission(context, tableName)) {
return ImmutableSet.of();
}
if (INFORMATION_SCHEMA_NAME.equals(tableName.getSchemaTableName().getSchemaName())) {
return columns;
}
Identity identity = context.getIdentity();
CatalogTableAccessControlRule rule = tableRules.stream().filter(tableRule -> tableRule.matches(identity.getUser(), identity.getEnabledRoles(), identity.getGroups(), tableName)).findFirst().orElse(null);
if (rule == null || rule.getPrivileges().isEmpty()) {
return ImmutableSet.of();
}
// if user has privileges other than select, show all columns
if (rule.getPrivileges().stream().anyMatch(privilege -> SELECT != privilege && GRANT_SELECT != privilege)) {
return columns;
}
Set<String> restrictedColumns = rule.getRestrictedColumns();
return columns.stream().filter(column -> !restrictedColumns.contains(column)).collect(toImmutableSet());
}
use of io.trino.spi.security.Identity in project trino by trinodb.
the class FileBasedSystemAccessControl method checkCanSetSystemSessionProperty.
@Override
public void checkCanSetSystemSessionProperty(SystemSecurityContext context, String propertyName) {
Identity identity = context.getIdentity();
boolean allowed = sessionPropertyRules.stream().map(rule -> rule.match(identity.getUser(), identity.getEnabledRoles(), identity.getGroups(), propertyName)).flatMap(Optional::stream).findFirst().orElse(false);
if (!allowed) {
denySetSystemSessionProperty(propertyName);
}
}
use of io.trino.spi.security.Identity in project trino by trinodb.
the class TestFileBasedSystemAccessControl method testQueryDocsExample.
@Test
public void testQueryDocsExample() {
String rulesFile = new File("../../docs/src/main/sphinx/security/query-access.json").getAbsolutePath();
SystemAccessControl accessControlManager = newFileBasedSystemAccessControl(ImmutableMap.of("security.config-file", rulesFile));
accessControlManager.checkCanExecuteQuery(new SystemSecurityContext(admin, queryId));
accessControlManager.checkCanViewQueryOwnedBy(new SystemSecurityContext(admin, queryId), any);
assertEquals(accessControlManager.filterViewQueryOwnedBy(new SystemSecurityContext(admin, queryId), ImmutableSet.of("a", "b")), ImmutableSet.of("a", "b"));
accessControlManager.checkCanKillQueryOwnedBy(new SystemSecurityContext(admin, queryId), any);
accessControlManager.checkCanExecuteQuery(new SystemSecurityContext(alice, queryId));
assertThatThrownBy(() -> accessControlManager.checkCanViewQueryOwnedBy(new SystemSecurityContext(alice, queryId), any)).isInstanceOf(AccessDeniedException.class).hasMessage("Access Denied: Cannot view query");
assertEquals(accessControlManager.filterViewQueryOwnedBy(new SystemSecurityContext(alice, queryId), ImmutableSet.of("a", "b")), ImmutableSet.of());
accessControlManager.checkCanKillQueryOwnedBy(new SystemSecurityContext(alice, queryId), any);
accessControlManager.checkCanExecuteQuery(new SystemSecurityContext(bob, queryId));
assertThatThrownBy(() -> accessControlManager.checkCanViewQueryOwnedBy(new SystemSecurityContext(bob, queryId), any)).isInstanceOf(AccessDeniedException.class).hasMessage("Access Denied: Cannot view query");
assertEquals(accessControlManager.filterViewQueryOwnedBy(new SystemSecurityContext(bob, queryId), ImmutableSet.of("a", "b")), ImmutableSet.of());
assertThatThrownBy(() -> accessControlManager.checkCanKillQueryOwnedBy(new SystemSecurityContext(bob, queryId), any)).isInstanceOf(AccessDeniedException.class).hasMessage("Access Denied: Cannot view query");
accessControlManager.checkCanExecuteQuery(new SystemSecurityContext(dave, queryId));
accessControlManager.checkCanViewQueryOwnedBy(new SystemSecurityContext(dave, queryId), alice);
accessControlManager.checkCanViewQueryOwnedBy(new SystemSecurityContext(dave, queryId), dave);
assertEquals(accessControlManager.filterViewQueryOwnedBy(new SystemSecurityContext(dave, queryId), ImmutableSet.of("alice", "bob", "dave", "admin")), ImmutableSet.of("alice", "dave"));
assertThatThrownBy(() -> accessControlManager.checkCanKillQueryOwnedBy(new SystemSecurityContext(dave, queryId), alice)).isInstanceOf(AccessDeniedException.class).hasMessage("Access Denied: Cannot view query");
assertThatThrownBy(() -> accessControlManager.checkCanKillQueryOwnedBy(new SystemSecurityContext(dave, queryId), bob)).isInstanceOf(AccessDeniedException.class).hasMessage("Access Denied: Cannot view query");
assertThatThrownBy(() -> accessControlManager.checkCanViewQueryOwnedBy(new SystemSecurityContext(dave, queryId), bob)).isInstanceOf(AccessDeniedException.class).hasMessage("Access Denied: Cannot view query");
assertThatThrownBy(() -> accessControlManager.checkCanViewQueryOwnedBy(new SystemSecurityContext(dave, queryId), admin)).isInstanceOf(AccessDeniedException.class).hasMessage("Access Denied: Cannot view query");
Identity contractor = Identity.forUser("some-other-contractor").withGroups(ImmutableSet.of("contractors")).build();
accessControlManager.checkCanExecuteQuery(new SystemSecurityContext(contractor, queryId));
accessControlManager.checkCanViewQueryOwnedBy(new SystemSecurityContext(contractor, queryId), dave);
assertThatThrownBy(() -> accessControlManager.checkCanKillQueryOwnedBy(new SystemSecurityContext(contractor, queryId), dave)).isInstanceOf(AccessDeniedException.class).hasMessage("Access Denied: Cannot view query");
}
use of io.trino.spi.security.Identity in project trino by trinodb.
the class MetadataManager method getView.
@Override
public Optional<ViewDefinition> getView(Session session, QualifiedObjectName viewName) {
Optional<ConnectorViewDefinition> connectorView = getViewInternal(session, viewName);
if (connectorView.isEmpty() || connectorView.get().isRunAsInvoker() || isCatalogManagedSecurity(session, viewName.getCatalogName())) {
return connectorView.map(view -> new ViewDefinition(viewName, view));
}
Identity runAsIdentity = systemSecurityMetadata.getViewRunAsIdentity(session, viewName.asCatalogSchemaTableName()).or(() -> connectorView.get().getOwner().map(Identity::ofUser)).orElseThrow(() -> new TrinoException(NOT_SUPPORTED, "Catalog does not support run-as DEFINER views: " + viewName));
return Optional.of(new ViewDefinition(viewName, connectorView.get(), runAsIdentity));
}
Aggregations