jerryshao commented on code in PR #13778: URL: https://github.com/apache/gravitino/pull/13778#discussion_r4375391393
########## iceberg/iceberg-rest-server-experiment/src/main/java/org/apache/gravitino/iceberg/service/restriction/ExperimentalIcebergReadRestrictionLoader.java: ########## @@ -0,0 +1,310 @@ +/* + * 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.iceberg.service.restriction; + +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.gravitino.Entity; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.UserGroup; +import org.apache.gravitino.UserPrincipal; +import org.apache.gravitino.authorization.AccessControlDispatcher; +import org.apache.gravitino.exceptions.NoSuchGroupException; +import org.apache.gravitino.iceberg.common.utils.IcebergIdentifierUtils; +import org.apache.gravitino.iceberg.service.authorization.IcebergRESTServerContext; +import org.apache.gravitino.listener.api.event.IcebergRequestContext; +import org.apache.gravitino.meta.PolicyEntity; +import org.apache.gravitino.policy.ColumnMaskContent; +import org.apache.gravitino.policy.Policy; +import org.apache.gravitino.policy.PolicyDispatcher; +import org.apache.gravitino.policy.RowFilterContent; +import org.apache.gravitino.policy.rego.RestrictedRegoExpressionParserFacade; +import org.apache.gravitino.policy.rego.RestrictedRegoProgram.ColumnMask; +import org.apache.gravitino.policy.rego.RestrictedRegoProgram.RowFilter; +import org.apache.gravitino.utils.HierarchicalSchemaUtil; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.PrincipalUtils; +import org.apache.iceberg.Schema; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.rest.responses.LoadTableResponse; +import org.apache.iceberg.rest.restrictions.ReadRestrictions; +import org.apache.iceberg.rest.restrictions.ReadRestrictionsParser; +import org.apache.iceberg.types.Types; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Loads tag-selected Gravitino policies into experimental Iceberg read restrictions. */ +public final class ExperimentalIcebergReadRestrictionLoader + implements IcebergReadRestrictionLoader { + private static final Logger LOG = + LoggerFactory.getLogger(ExperimentalIcebergReadRestrictionLoader.class); + + /** Creates the service-loaded read-restriction loader. */ + public ExperimentalIcebergReadRestrictionLoader() {} + + @Override + public boolean enabled() { + return GravitinoEnv.getInstance().internalPolicyDispatcher() != null; + } + + @Override + public Result load( + IcebergRequestContext context, TableIdentifier tableIdentifier, LoadTableResponse response) { + GravitinoEnv environment = GravitinoEnv.getInstance(); + PolicyDispatcher policyDispatcher = environment.internalPolicyDispatcher(); + AccessControlDispatcher accessControlDispatcher = environment.internalAccessControlDispatcher(); + if (policyDispatcher == null) { + return Result.of(response, Optional.empty()); + } + if (accessControlDispatcher == null) { + throw new IllegalStateException( + "Iceberg read restrictions require authorization; set gravitino.authorization.enable=true"); + } + + String metalake = IcebergRESTServerContext.getInstance().metalakeName(); + NameIdentifier tableName = + IcebergIdentifierUtils.toGravitinoTableIdentifier( + metalake, + context.catalogName(), + tableIdentifier, + HierarchicalSchemaUtil.schemaSeparator()); + NameIdentifierUtil.checkMetadataObjectName( + NameIdentifierUtil.ofMetalake(metalake), Entity.EntityType.METALAKE); + NameIdentifierUtil.checkMetadataObjectName( + NameIdentifierUtil.ofCatalog(metalake, context.catalogName()), Entity.EntityType.CATALOG); + NameIdentifierUtil.checkMetadataObjectName( + NameIdentifierUtil.ofSchema( + metalake, context.catalogName(), tableName.namespace().level(2)), + Entity.EntityType.SCHEMA); + Schema schema = response.tableMetadata().schema(); + PolicyEntity[] rowPolicies = + policyDispatcher.listPolicyInfosForMetadataObject( + metalake, NameIdentifierUtil.toMetadataObject(tableName, Entity.EntityType.TABLE)); + Map<Integer, PolicyEntity[]> maskPolicies = new LinkedHashMap<>(); + for (Types.NestedField field : schema.columns()) { + NameIdentifier columnName = + NameIdentifierUtil.ofColumn( + metalake, + context.catalogName(), + tableName.namespace().level(2), + tableName.name(), + field.name()); + MetadataObject column = + NameIdentifierUtil.toMetadataObject(columnName, Entity.EntityType.COLUMN); + maskPolicies.put( + field.fieldId(), policyDispatcher.listPolicyInfosForMetadataObject(metalake, column)); + } + + IdentityBindingContext bindingContext = + new IdentityBindingContext(context.userName(), metalake, accessControlDispatcher); + ReadRestrictions restrictions = + resolveForLoad(rowPolicies, maskPolicies, schema, bindingContext); + if (restrictions.isEmpty()) { + return Result.of(response, Optional.empty()); + } + + LoadTableResponse.Builder builder = + copyResponseBuilder(response, response.tableMetadata()).withReadRestrictions(restrictions); + String signature = ReadRestrictionsParser.toJson(restrictions); + return Result.of(builder.build(), Optional.of(signature)); + } + + @Override + public LoadTableResponse preserveSnapshotFilteredFields( + LoadTableResponse original, LoadTableResponse filtered) { + if (original == filtered) { + return filtered; + } + return copyResponseBuilder(original, filtered.tableMetadata()).build(); + } + + static ReadRestrictions resolve( + PolicyEntity[] tablePolicies, + Map<Integer, PolicyEntity[]> columnPolicies, + Schema schema, + IcebergReadRestrictionConverter.BindingContext bindingContext) { + Optional<RowFilter> rowFilter = resolveRowFilter(tablePolicies, schema, bindingContext); + Map<Integer, ColumnMask> columnMasks = + resolveColumnMasks(columnPolicies, schema, bindingContext); + return IcebergReadRestrictionConverter.convert(rowFilter, columnMasks, schema, bindingContext); + } + + static ReadRestrictions resolveForLoad( + PolicyEntity[] tablePolicies, + Map<Integer, PolicyEntity[]> columnPolicies, + Schema schema, + IcebergReadRestrictionConverter.BindingContext bindingContext) { + try { + return resolve(tablePolicies, columnPolicies, schema, bindingContext); + } catch (RuntimeException exception) { + LOG.error("Failed to resolve Iceberg read restrictions", exception); + throw new IllegalStateException( + "Unable to resolve read restrictions for the requested table"); + } + } + + private static Optional<RowFilter> resolveRowFilter( + PolicyEntity[] policies, + Schema schema, + IcebergReadRestrictionConverter.BindingContext bindingContext) { + Map<String, RowFilter> distinct = new LinkedHashMap<>(); + for (PolicyEntity policy : policies) { + if (policy.policyType() != Policy.BuiltInType.ROW_FILTER) { + continue; + } + RowFilter program = + RestrictedRegoExpressionParserFacade.parseRowFilter( + ((RowFilterContent) policy.content()).expression()); + ReadRestrictions resolved = + IcebergReadRestrictionConverter.convert( + Optional.of(program), Collections.emptyMap(), schema, bindingContext); + if (resolved.isEmpty()) { + continue; + } + distinct.putIfAbsent(ReadRestrictionsParser.toJson(resolved), program); + } + if (distinct.size() > 1) { + throw new IllegalArgumentException( + "Conflicting row-filter policies apply to the requested table; only one distinct effective row-filter is supported"); + } + return distinct.values().stream().findFirst(); + } + + private static Map<Integer, ColumnMask> resolveColumnMasks( + Map<Integer, PolicyEntity[]> policiesByField, + Schema schema, + IcebergReadRestrictionConverter.BindingContext bindingContext) { + Map<Integer, ColumnMask> resolvedMasks = new HashMap<>(); + List<Integer> fieldIds = new ArrayList<>(policiesByField.keySet()); + Collections.sort(fieldIds); + for (Integer fieldId : fieldIds) { + Map<String, ColumnMask> distinct = new LinkedHashMap<>(); + for (PolicyEntity policy : policiesByField.get(fieldId)) { + if (policy.policyType() != Policy.BuiltInType.COLUMN_MASK) { + continue; + } + ColumnMask program = + RestrictedRegoExpressionParserFacade.parseColumnMask( + ((ColumnMaskContent) policy.content()).expression()); + ReadRestrictions resolved = + IcebergReadRestrictionConverter.convert( + Optional.empty(), + Collections.singletonMap(fieldId, program), + schema, + bindingContext); + distinct.putIfAbsent(ReadRestrictionsParser.toJson(resolved), program); + } + if (distinct.size() > 1) { + throw new IllegalArgumentException( + "Conflicting column-mask policies apply to a requested table field"); + } + distinct.values().stream().findFirst().ifPresent(mask -> resolvedMasks.put(fieldId, mask)); + } + return resolvedMasks; + } + + private static LoadTableResponse.Builder copyResponseBuilder( + LoadTableResponse response, TableMetadata metadata) { + LoadTableResponse.Builder builder = LoadTableResponse.builder().withTableMetadata(metadata); + if (response.config() != null) { + builder.addAllConfig(response.config()); + } + if (response.credentials() != null) { + builder.addAllCredentials(response.credentials()); + } + if (response.remoteSigningConfig() != null) { + builder.withRemoteSigningConfig(response.remoteSigningConfig()); + } + if (response.labels() != null) { + builder.withLabels(response.labels()); + } + return builder; + } + + static final class IdentityBindingContext + implements IcebergReadRestrictionConverter.BindingContext { + private final String userName; + private final String metalake; + private final AccessControlDispatcher accessControlDispatcher; + private final Optional<Set<String>> groups; + private final Map<String, Optional<Boolean>> memberships = new HashMap<>(); + + IdentityBindingContext( + String userName, String metalake, AccessControlDispatcher accessControlDispatcher) { + this.userName = userName; + this.metalake = metalake; + this.accessControlDispatcher = accessControlDispatcher; + this.groups = snapshotGroups(userName); + } + + @Override + public String sessionUser() { + return userName; + } + + @Override + public Optional<Boolean> groupMembership(String group) { + return memberships.computeIfAbsent(group, this::resolveGroupMembership); + } + + private Optional<Boolean> resolveGroupMembership(String group) { + if (!groups.isPresent() || accessControlDispatcher == null) { + return Optional.empty(); + } + try { + if (accessControlDispatcher.getGroup(metalake, group) == null) { + return Optional.empty(); + } + return Optional.of(groups.get().contains(group)); Review Comment: [Important] `gravitino.authorization.enable=true` is not what makes `is_group_member` resolve, so this message and the matching doc paragraph point operators at the wrong knob - and under the default authenticator every user is silently a confirmed non-member. Membership does not come from the access-control dispatcher: `snapshotGroups` (lines 298-307) takes it from the authenticated `UserPrincipal`'s group claims, and the dispatcher is used only to check the group exists as a Gravitino entity (line 282). Gravitino's `Group` has no user list (`api/.../authorization/Group.java:27-48`), so those claims are the only source. `gravitino.authenticators` defaults to `simple` (`core/src/main/java/org/apache/gravitino/Configs.java:309-322`) and `SimpleAuthenticator` builds `new UserPrincipal(user, authData)` with no groups (`server-common/.../SimpleAuthenticator.java:66`; only OAuth2, JWKS, StaticSignKey, Kerberos and the basic IdP pass groups). So with authorization enabled and simple auth, `groups` is present-but-empty and line 285 returns `Optional.of(false)` - a *confirmed* non-member, not an unresolvable one. Your own `testInconsistentIdentityDoesNotBecomeNonMembership` (test line 468) asserts exactly this for `new UserPrincipal("alice")`. That silently selects the non-member branch of every group-conditional program. For the docs example at `docs/policies.md:95-98` that is fail-closed (a real auditor falls through to `col("owner") == session_user()`), but the opposite shape - restrict one group, leave everyone else alone - selects the permissive branch, and `convert` drops a filter resolving to TRUE (`IcebergReadRestrictionConverter.java:118-120`), so the table is served unrestricted. For a feature fail-closed everywhere else, that asymmetry should be explicit. Suggestion: name the real prerequisite in the message and in `docs/policies.md:116-119` - an authenticator that supplies group claims - and state that `is_group_member` reads as non-member for every user without them. If it should fail closed instead, detect at startup that no configured authenticator can supply groups. Verified by: reading `snapshotGroups` and `resolveGroupMembership` here; `IcebergRequestContext:79` takes `userName` from `PrincipalUtils.getCurrentUserName()`, so the identity check at line 300 passes and the empty set is used; the `Configs.AUTHENTICATORS` default, `SimpleAuthenticator.authenticateToken`, and grep for `new UserPrincipal(` over main sources. No server run. ########## iceberg/iceberg-rest-server-experiment/src/main/java/org/apache/gravitino/iceberg/service/restriction/ExperimentalIcebergReadRestrictionLoader.java: ########## @@ -0,0 +1,310 @@ +/* + * 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.iceberg.service.restriction; + +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import org.apache.gravitino.Entity; +import org.apache.gravitino.GravitinoEnv; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.UserGroup; +import org.apache.gravitino.UserPrincipal; +import org.apache.gravitino.authorization.AccessControlDispatcher; +import org.apache.gravitino.exceptions.NoSuchGroupException; +import org.apache.gravitino.iceberg.common.utils.IcebergIdentifierUtils; +import org.apache.gravitino.iceberg.service.authorization.IcebergRESTServerContext; +import org.apache.gravitino.listener.api.event.IcebergRequestContext; +import org.apache.gravitino.meta.PolicyEntity; +import org.apache.gravitino.policy.ColumnMaskContent; +import org.apache.gravitino.policy.Policy; +import org.apache.gravitino.policy.PolicyDispatcher; +import org.apache.gravitino.policy.RowFilterContent; +import org.apache.gravitino.policy.rego.RestrictedRegoExpressionParserFacade; +import org.apache.gravitino.policy.rego.RestrictedRegoProgram.ColumnMask; +import org.apache.gravitino.policy.rego.RestrictedRegoProgram.RowFilter; +import org.apache.gravitino.utils.HierarchicalSchemaUtil; +import org.apache.gravitino.utils.NameIdentifierUtil; +import org.apache.gravitino.utils.PrincipalUtils; +import org.apache.iceberg.Schema; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.iceberg.rest.responses.LoadTableResponse; +import org.apache.iceberg.rest.restrictions.ReadRestrictions; +import org.apache.iceberg.rest.restrictions.ReadRestrictionsParser; +import org.apache.iceberg.types.Types; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** Loads tag-selected Gravitino policies into experimental Iceberg read restrictions. */ +public final class ExperimentalIcebergReadRestrictionLoader + implements IcebergReadRestrictionLoader { + private static final Logger LOG = + LoggerFactory.getLogger(ExperimentalIcebergReadRestrictionLoader.class); + + /** Creates the service-loaded read-restriction loader. */ + public ExperimentalIcebergReadRestrictionLoader() {} + + @Override + public boolean enabled() { + return GravitinoEnv.getInstance().internalPolicyDispatcher() != null; + } + + @Override + public Result load( + IcebergRequestContext context, TableIdentifier tableIdentifier, LoadTableResponse response) { + GravitinoEnv environment = GravitinoEnv.getInstance(); + PolicyDispatcher policyDispatcher = environment.internalPolicyDispatcher(); + AccessControlDispatcher accessControlDispatcher = environment.internalAccessControlDispatcher(); + if (policyDispatcher == null) { + return Result.of(response, Optional.empty()); + } + if (accessControlDispatcher == null) { + throw new IllegalStateException( + "Iceberg read restrictions require authorization; set gravitino.authorization.enable=true"); Review Comment: [Important] This precondition is a static deployment property, but it is enforced once per request, so on the default configuration every load-table now returns HTTP 500 - including tables with no policies at all. `internalPolicyDispatcher` is assigned unconditionally (`core/src/main/java/org/apache/gravitino/GravitinoEnv.java:936`), while `internalAccessControlDispatcher` is null whenever `gravitino.authorization.enable` is false (`GravitinoEnv.java:1030-1038`), and that flag defaults to false (`Configs.java:390-395`). So in auxiliary mode with this module on the classpath and the default config, this throw fires before any policy lookup for every single load-table request - a table with zero tags included. `IcebergExceptionMapper.getErrorCode` keys on the exact class and does not map `IllegalStateException` (`IcebergExceptionMapper.java:60-110`), so the wire result is HTTP 500 (the message does reach the client through `IcebergRESTUtils.errorResponse`, line 462-467). It is also a behaviour change for deployments that had this working: at f87dbff a row filter or mask with no `is_group_member` predicate resolved fine without an access-control dispatcher, because `sessionUser()` needs only `context.userName()`. Those deployments now get a 500 on every table. Suggestion: validate at startup, not per request. `RESTService.initServer` already reads the flag (`enableAuth`, `RESTService.java:132`) 65 lines above the discovery call (`:197`) and the binding (`:213`), and this PR already moved the multi-provider misconfiguration from per-request to startup for the same reason. Failing to start - or logging once and binding the no-op loader - turns a config mistake into one boot-time error instead of an unmapped 500 on every table read. If it has to stay a request-time failure, please map it to a status that says "server misconfiguration" rather than falling through to the generic 500. Verified by: reading the new throw and the two `GravitinoEnv` init sites in this checkout, the default in `Configs.ENABLE_AUTHORIZATION`, the exact-class lookup in `IcebergExceptionMapper`, `errorResponse` building the body from `ex.getMessage()`, and the order of `enableAuth` / `IcebergReadRestrictionLoader.load()` / `bind(readRestrictionLoader)` in `RESTService.initServer`. No server run. -- 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]
