roryqi commented on code in PR #14908: URL: https://github.com/apache/gravitino/pull/14908#discussion_r4550672177
########## iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/server/web/filter/UpdateTableAuthzHandler.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.server.web.filter; + +import static org.apache.gravitino.server.authorization.expression.AuthorizationExpressionConstants.CREATE_TABLE_AUTHORIZATION_EXPRESSION; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.apache.gravitino.Entity.EntityType; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.authorization.AuthorizationRequestContext; +import org.apache.gravitino.exceptions.ForbiddenException; +import org.apache.gravitino.iceberg.service.IcebergRESTUtils; +import org.apache.gravitino.server.authorization.expression.AuthorizationExpressionEvaluator; +import org.apache.gravitino.server.web.filter.BaseMetadataAuthorizationMethodInterceptor.AuthorizationHandler; +import org.apache.iceberg.UpdateRequirement; +import org.apache.iceberg.rest.requests.UpdateTableRequest; + +/** Authorizes staged table creation against the schema before Iceberg executes the commit. */ +public class UpdateTableAuthzHandler implements AuthorizationHandler { + private final boolean stagedCreate; + + /** + * Creates a handler using the same staged-create predicate as the table hook dispatcher. + * + * @param request the table commit request + */ + public UpdateTableAuthzHandler(UpdateTableRequest request) { + IcebergRESTUtils.validateRequest(request, "UpdateTableRequest"); + stagedCreate = + request.requirements().stream() + .anyMatch(UpdateRequirement.AssertTableDoesNotExist.class::isInstance); Review Comment: Confirmed: the commit path still validates `AssertTableDoesNotExist` against the current table metadata and rejects the commit if the table exists. Selecting `CREATE_TABLE` here does not remove that requirement. Added `testStagedCreateCannotReplaceExistingTable` in 964e9fd62a. It stages a create, lets another client create the same table, then submits the staged commit carrying owner and location updates through the actual Iceberg `CatalogHandlers` commit path. The commit fails with `CommitFailedException`, and the existing table's metadata file, UUID, properties, and location remain unchanged. This also covers the race that an authorization-time existence check would miss. All 43 related tests passed. ########## iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/server/web/filter/UpdateTableAuthzHandler.java: ########## @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.server.web.filter; + +import static org.apache.gravitino.server.authorization.expression.AuthorizationExpressionConstants.CREATE_TABLE_AUTHORIZATION_EXPRESSION; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import org.apache.gravitino.Entity.EntityType; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.authorization.AuthorizationRequestContext; +import org.apache.gravitino.exceptions.ForbiddenException; +import org.apache.gravitino.iceberg.service.IcebergRESTUtils; +import org.apache.gravitino.server.authorization.expression.AuthorizationExpressionEvaluator; +import org.apache.gravitino.server.web.filter.BaseMetadataAuthorizationMethodInterceptor.AuthorizationHandler; +import org.apache.iceberg.UpdateRequirement; +import org.apache.iceberg.rest.requests.UpdateTableRequest; + +/** Authorizes staged table creation against the schema before Iceberg executes the commit. */ +public class UpdateTableAuthzHandler implements AuthorizationHandler { + private final boolean stagedCreate; + + /** + * Creates a handler using the same staged-create predicate as the table hook dispatcher. + * + * @param request the table commit request + */ + public UpdateTableAuthzHandler(UpdateTableRequest request) { + IcebergRESTUtils.validateRequest(request, "UpdateTableRequest"); + stagedCreate = + request.requirements().stream() + .anyMatch(UpdateRequirement.AssertTableDoesNotExist.class::isInstance); + } + + @Override + public void process(Map<EntityType, NameIdentifier> nameIdentifierMap) { + if (stagedCreate + && !new AuthorizationExpressionEvaluator(CREATE_TABLE_AUTHORIZATION_EXPRESSION) + .evaluate( + nameIdentifierMap, + new HashMap<>(), + new AuthorizationRequestContext(), + Optional.of(EntityType.SCHEMA.name()))) { + throw new ForbiddenException( + "Not authorized to create table in schema '%s'", + nameIdentifierMap.get(EntityType.SCHEMA)); + } + } + + @Override + public boolean authorizationCompleted() { + // Ordinary commits continue through the endpoint's existing MODIFY_TABLE expression. + return stagedCreate; Review Comment: Yes, this is intentional for staged creation. Setting the initial location is part of creating a new table, just as on the create-table endpoint, so it uses the schema's `CREATE_TABLE` permission. A supplied owner is handled separately by the executor's existing `IcebergOwnerPropertyGuard`, which rewrites it to the authenticated user before committing. The commit still enforces `AssertTableDoesNotExist`, so these updates cannot modify an existing table. The new regression in 964e9fd62a verifies rejection with both owner and location updates and checks that the existing table is unchanged. Ordinary commits continue to require `MODIFY_TABLE`; no additional permission checks were introduced. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
