|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.execution; |
| 15 | + |
| 16 | +import com.google.common.util.concurrent.ListenableFuture; |
| 17 | +import com.google.inject.Inject; |
| 18 | +import io.trino.Session; |
| 19 | +import io.trino.execution.warnings.WarningCollector; |
| 20 | +import io.trino.metadata.Metadata; |
| 21 | +import io.trino.metadata.QualifiedObjectName; |
| 22 | +import io.trino.metadata.RedirectionAwareTableHandle; |
| 23 | +import io.trino.security.AccessControl; |
| 24 | +import io.trino.spi.connector.CatalogSchemaName; |
| 25 | +import io.trino.spi.connector.EntityKindAndName; |
| 26 | +import io.trino.spi.security.TrinoPrincipal; |
| 27 | +import io.trino.sql.tree.Expression; |
| 28 | +import io.trino.sql.tree.SetAuthorizationStatement; |
| 29 | + |
| 30 | +import java.util.List; |
| 31 | +import java.util.Optional; |
| 32 | + |
| 33 | +import static com.google.common.util.concurrent.Futures.immediateVoidFuture; |
| 34 | +import static io.trino.metadata.MetadataUtil.checkRoleExists; |
| 35 | +import static io.trino.metadata.MetadataUtil.createCatalogSchemaName; |
| 36 | +import static io.trino.metadata.MetadataUtil.createPrincipal; |
| 37 | +import static io.trino.metadata.MetadataUtil.fillInNameParts; |
| 38 | +import static io.trino.metadata.MetadataUtil.getRequiredCatalogHandle; |
| 39 | +import static io.trino.spi.StandardErrorCode.NOT_SUPPORTED; |
| 40 | +import static io.trino.spi.StandardErrorCode.SCHEMA_NOT_FOUND; |
| 41 | +import static io.trino.spi.StandardErrorCode.TABLE_NOT_FOUND; |
| 42 | +import static io.trino.sql.analyzer.SemanticExceptions.semanticException; |
| 43 | +import static java.util.Objects.requireNonNull; |
| 44 | + |
| 45 | +public class SetAuthorizationTask |
| 46 | + implements DataDefinitionTask<SetAuthorizationStatement> |
| 47 | +{ |
| 48 | + private final Metadata metadata; |
| 49 | + private final AccessControl accessControl; |
| 50 | + |
| 51 | + @Inject |
| 52 | + public SetAuthorizationTask(Metadata metadata, AccessControl accessControl) |
| 53 | + { |
| 54 | + this.metadata = requireNonNull(metadata, "metadata is null"); |
| 55 | + this.accessControl = requireNonNull(accessControl, "accessControl is null"); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getName() |
| 60 | + { |
| 61 | + return "SET AUTHORIZATION"; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public ListenableFuture<Void> execute( |
| 66 | + SetAuthorizationStatement statement, |
| 67 | + QueryStateMachine stateMachine, |
| 68 | + List<Expression> parameters, |
| 69 | + WarningCollector warningCollector) |
| 70 | + { |
| 71 | + Session session = stateMachine.getSession(); |
| 72 | + setEntityAuthorization(session, statement); |
| 73 | + |
| 74 | + return immediateVoidFuture(); |
| 75 | + } |
| 76 | + |
| 77 | + private void setEntityAuthorization(Session session, SetAuthorizationStatement statement) |
| 78 | + { |
| 79 | + List<String> name = fillInNameParts(session, statement, statement.getOwnedEntityKind(), statement.getSource().getParts()); |
| 80 | + |
| 81 | + // Preprocess SCHEMA, TABLE and VIEW to generate error messages in the order compatible with existing tests |
| 82 | + switch (statement.getOwnedEntityKind()) { |
| 83 | + case "SCHEMA" -> { |
| 84 | + CatalogSchemaName source = createCatalogSchemaName(session, statement, Optional.of(statement.getSource())); |
| 85 | + if (!metadata.schemaExists(session, source)) { |
| 86 | + throw semanticException(SCHEMA_NOT_FOUND, statement, "Schema '%s' does not exist", source); |
| 87 | + } |
| 88 | + } |
| 89 | + case "TABLE" -> { |
| 90 | + QualifiedObjectName tableName = new QualifiedObjectName(name.get(0), name.get(1), name.get(2)); |
| 91 | + getRequiredCatalogHandle(metadata, session, statement, name.get(0)); |
| 92 | + RedirectionAwareTableHandle redirection = metadata.getRedirectionAwareTableHandle(session, tableName); |
| 93 | + if (redirection.tableHandle().isEmpty()) { |
| 94 | + throw semanticException(TABLE_NOT_FOUND, statement, "Table '%s' does not exist", tableName); |
| 95 | + } |
| 96 | + if (redirection.redirectedTableName().isPresent()) { |
| 97 | + throw semanticException(NOT_SUPPORTED, statement, "Table %s is redirected to %s and SET TABLE AUTHORIZATION is not supported with table redirections", tableName, redirection.redirectedTableName().get()); |
| 98 | + } |
| 99 | + } |
| 100 | + case "VIEW" -> { |
| 101 | + QualifiedObjectName viewName = new QualifiedObjectName(name.get(0), name.get(1), name.get(2)); |
| 102 | + getRequiredCatalogHandle(metadata, session, statement, viewName.catalogName()); |
| 103 | + if (!metadata.isView(session, viewName)) { |
| 104 | + throw semanticException(TABLE_NOT_FOUND, statement, "View '%s' does not exist", viewName); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + TrinoPrincipal principal = createPrincipal(statement.getPrincipal()); |
| 110 | + Optional<String> maybeCatalogName = name.size() > 1 ? Optional.of(name.get(0)) : Optional.empty(); |
| 111 | + checkRoleExists(session, statement, metadata, principal, maybeCatalogName.filter(catalog -> metadata.isCatalogManagedSecurity(session, catalog))); |
| 112 | + EntityKindAndName entityKindAndName = new EntityKindAndName(statement.getOwnedEntityKind(), name); |
| 113 | + accessControl.checkCanSetEntityAuthorization(session.toSecurityContext(), entityKindAndName, principal); |
| 114 | + metadata.setEntityAuthorization(session, entityKindAndName, principal); |
| 115 | + } |
| 116 | +} |
0 commit comments