brucearctor commented on code in PR #14976: URL: https://github.com/apache/gravitino/pull/14976#discussion_r4648798341
########## authorizations/authorization-kafka/src/main/java/org/apache/gravitino/authorization/kafka/KafkaPrivilegeMapper.java: ########## @@ -0,0 +1,284 @@ +/* + * 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.authorization.kafka; + +import com.google.common.base.Preconditions; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.authorization.Privilege; +import org.apache.kafka.common.acl.AccessControlEntry; +import org.apache.kafka.common.acl.AclBinding; +import org.apache.kafka.common.acl.AclOperation; +import org.apache.kafka.common.acl.AclPermissionType; +import org.apache.kafka.common.resource.PatternType; +import org.apache.kafka.common.resource.Resource; +import org.apache.kafka.common.resource.ResourcePattern; +import org.apache.kafka.common.resource.ResourceType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Translates Apache Gravitino {@link MetadataObject} privileges and ownership into native Apache + * Kafka {@link AclBinding} entries. + */ +public final class KafkaPrivilegeMapper { + + private static final Logger LOG = LoggerFactory.getLogger(KafkaPrivilegeMapper.class); + + private static final ResourcePattern WILDCARD_TOPIC_RESOURCE = + new ResourcePattern(ResourceType.TOPIC, "*", PatternType.LITERAL); + + private static final ResourcePattern CLUSTER_RESOURCE = + new ResourcePattern(ResourceType.CLUSTER, Resource.CLUSTER_NAME, PatternType.LITERAL); + + private static final Set<AclOperation> OWNER_TOPIC_OPERATIONS; + + static { + Set<AclOperation> ops = new LinkedHashSet<>(); + ops.add(AclOperation.READ); + ops.add(AclOperation.WRITE); + ops.add(AclOperation.CREATE); + ops.add(AclOperation.DELETE); + ops.add(AclOperation.ALTER); + ops.add(AclOperation.DESCRIBE); + ops.add(AclOperation.ALTER_CONFIGS); + ops.add(AclOperation.DESCRIBE_CONFIGS); + OWNER_TOPIC_OPERATIONS = Collections.unmodifiableSet(ops); + } + + private KafkaPrivilegeMapper() {} + + /** + * Maps a Gravitino privilege on a metadata object for a specific Kafka principal into one or more + * Kafka {@link AclBinding}s. + * + * @param metadataObject Target securable object. + * @param privilege Gravitino privilege (name + ALLOW/DENY condition). + * @param kafkaPrincipal Formatted Kafka principal (e.g. {@code User:alice}). + * @param properties Authorization plugin configuration properties. + * @return Immutable or ordered set of Kafka ACL bindings (empty if non-Kafka privilege or type). + */ + public static Set<AclBinding> toAclBindings( + MetadataObject metadataObject, + Privilege privilege, + String kafkaPrincipal, + KafkaAuthorizationProperties properties) { + Preconditions.checkArgument(metadataObject != null, "MetadataObject must not be null"); + Preconditions.checkArgument(privilege != null, "Privilege must not be null"); + Preconditions.checkArgument(kafkaPrincipal != null, "Kafka principal must not be null"); + Preconditions.checkArgument(properties != null, "Properties must not be null"); + + ResourcePattern topicTarget = resolveTopicResource(metadataObject); + if (topicTarget == null) { + return Collections.emptySet(); + } + + AclPermissionType permissionType = toPermissionType(privilege.condition()); + String host = properties.getAclHost(); + Set<AclBinding> bindings = new LinkedHashSet<>(); + + switch (privilege.name()) { + case PRODUCE_TOPIC: + if (permissionType == AclPermissionType.ALLOW) { + bindings.add( + new AclBinding( + topicTarget, + new AccessControlEntry( + kafkaPrincipal, host, AclOperation.WRITE, AclPermissionType.ALLOW))); + bindings.add( + new AclBinding( + topicTarget, + new AccessControlEntry( + kafkaPrincipal, host, AclOperation.DESCRIBE, AclPermissionType.ALLOW))); + if (properties.isIdempotentWriteAclEnabled()) { + bindings.add( + new AclBinding( + CLUSTER_RESOURCE, + new AccessControlEntry( + kafkaPrincipal, + host, + AclOperation.IDEMPOTENT_WRITE, + AclPermissionType.ALLOW))); + } + } else { + // Only deny the primary WRITE operation so shared DESCRIBE or cluster ops remain usable. + bindings.add( + new AclBinding( + topicTarget, + new AccessControlEntry( + kafkaPrincipal, host, AclOperation.WRITE, AclPermissionType.DENY))); + } + break; + + case CONSUME_TOPIC: + if (permissionType == AclPermissionType.ALLOW) { + bindings.add( + new AclBinding( + topicTarget, + new AccessControlEntry( + kafkaPrincipal, host, AclOperation.READ, AclPermissionType.ALLOW))); + bindings.add( + new AclBinding( + topicTarget, + new AccessControlEntry( + kafkaPrincipal, host, AclOperation.DESCRIBE, AclPermissionType.ALLOW))); + bindings.add( + new AclBinding( + consumerGroupResource(properties), + new AccessControlEntry( + kafkaPrincipal, host, AclOperation.READ, AclPermissionType.ALLOW))); + } else { + // Only deny topic READ so consuming other topics or producing is unaffected. + bindings.add( + new AclBinding( + topicTarget, + new AccessControlEntry( + kafkaPrincipal, host, AclOperation.READ, AclPermissionType.DENY))); + } + break; + + case CREATE_TOPIC: + if (permissionType == AclPermissionType.ALLOW) { + bindings.add( + new AclBinding( + CLUSTER_RESOURCE, + new AccessControlEntry( + kafkaPrincipal, host, AclOperation.CREATE, AclPermissionType.ALLOW))); Review Comment: Addressed in f11736e69: removed CLUSTER:CREATE from both CREATE_TOPIC and ownership mappings in KafkaPrivilegeMapper so creation relies on TOPIC CREATE bindings and respects topic-level DENY rules. Updated docs/kafka-catalog.md and added real-broker DENY precedence coverage in KafkaAuthorizationIT. ########## authorizations/authorization-kafka/src/main/java/org/apache/gravitino/authorization/kafka/KafkaAuthorizationPlugin.java: ########## @@ -0,0 +1,1010 @@ +/* + * 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.authorization.kafka; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.collect.Sets; +import java.io.IOException; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ExecutionException; +import javax.annotation.Nullable; +import org.apache.commons.lang3.StringUtils; +import org.apache.gravitino.Audit; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.authorization.Group; +import org.apache.gravitino.authorization.MetadataObjectChange; +import org.apache.gravitino.authorization.Owner; +import org.apache.gravitino.authorization.Privilege; +import org.apache.gravitino.authorization.Role; +import org.apache.gravitino.authorization.RoleChange; +import org.apache.gravitino.authorization.SecurableObject; +import org.apache.gravitino.authorization.User; +import org.apache.gravitino.connector.authorization.AuthorizationPlugin; +import org.apache.gravitino.exceptions.AuthorizationPluginException; +import org.apache.kafka.clients.admin.AdminClient; +import org.apache.kafka.common.acl.AccessControlEntry; +import org.apache.kafka.common.acl.AccessControlEntryFilter; +import org.apache.kafka.common.acl.AclBinding; +import org.apache.kafka.common.acl.AclBindingFilter; +import org.apache.kafka.common.acl.AclOperation; +import org.apache.kafka.common.acl.AclPermissionType; +import org.apache.kafka.common.resource.PatternType; +import org.apache.kafka.common.resource.Resource; +import org.apache.kafka.common.resource.ResourcePattern; +import org.apache.kafka.common.resource.ResourcePatternFilter; +import org.apache.kafka.common.resource.ResourceType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link AuthorizationPlugin} implementation that pushes Apache Gravitino RBAC grants, revocations, + * ownership changes, and topic metadata updates to Kafka brokers using {@link AdminClient}. + */ +public class KafkaAuthorizationPlugin implements AuthorizationPlugin { + + private static final Logger LOG = LoggerFactory.getLogger(KafkaAuthorizationPlugin.class); + + private final KafkaAuthorizationProperties properties; + private final AdminClient adminClient; + private final KafkaAuthorizationStateProvider stateProvider; + + private volatile String metalake; + private volatile String catalogName; + + /** + * Creates a {@link KafkaAuthorizationPlugin} backed by a new Kafka {@link AdminClient} and the + * default {@link EntityStoreAuthorizationStateProvider}. + * + * @param metalake Metalake name associated with this catalog plugin. + * @param properties Validated Kafka authorization configuration properties. + */ + public KafkaAuthorizationPlugin(String metalake, KafkaAuthorizationProperties properties) { + this( + metalake, + properties, + AdminClient.create(properties.toAdminClientProperties()), + new EntityStoreAuthorizationStateProvider(), + null); + } + + @VisibleForTesting + KafkaAuthorizationPlugin( + String metalake, + KafkaAuthorizationProperties properties, + AdminClient adminClient, + KafkaAuthorizationStateProvider stateProvider, + @Nullable String initialCatalogName) { + Preconditions.checkArgument( + StringUtils.isNotBlank(metalake), "Metalake name must not be blank"); + Preconditions.checkArgument(properties != null, "Properties must not be null"); + Preconditions.checkArgument(adminClient != null, "AdminClient must not be null"); + Preconditions.checkArgument(stateProvider != null, "StateProvider must not be null"); + this.metalake = metalake; + this.properties = properties; + this.adminClient = adminClient; + this.stateProvider = stateProvider; + this.catalogName = initialCatalogName; + } + + @Override + public Boolean onRoleCreated(Role role) throws RuntimeException { + // Roles carry no Kafka user/group principals until granted to a user or group. + return Boolean.TRUE; + } + + @Override + public Boolean onRoleAcquired(Role role) throws RuntimeException { + return Boolean.TRUE; + } + + @Override + public Boolean onRoleDeleted(Role role) throws RuntimeException { + if (role == null) { + return Boolean.TRUE; + } + + Set<String> excludedRoles = Collections.singleton(role.name()); + List<String> boundUsers = stateProvider.listUserNamesByRole(metalake, role.name()); + for (String username : boundUsers) { + String principal = properties.formatUserPrincipal(username); + Set<AclBinding> candidateRemovals = bindingsForRoles(List.of(role), principal); + removeBindingsPreservingActiveGrants( + Owner.Type.USER, + username, + candidateRemovals, + excludedRoles, + Collections.emptyList(), + Collections.emptyMap(), + null); + } + + List<String> boundGroups = stateProvider.listGroupNamesByRole(metalake, role.name()); + for (String groupName : boundGroups) { + String principal = properties.formatGroupPrincipal(groupName); + Set<AclBinding> candidateRemovals = bindingsForRoles(List.of(role), principal); + removeBindingsPreservingActiveGrants( + Owner.Type.GROUP, + groupName, + candidateRemovals, + excludedRoles, + Collections.emptyList(), + Collections.emptyMap(), + null); + } + return Boolean.TRUE; + } + + @Override + public Boolean onRoleUpdated(Role role, RoleChange... changes) throws RuntimeException { + if (role == null || changes == null || changes.length == 0) { + return Boolean.TRUE; + } + + List<String> boundUsers = stateProvider.listUserNamesByRole(metalake, role.name()); + List<String> boundGroups = stateProvider.listGroupNamesByRole(metalake, role.name()); + if (boundUsers.isEmpty() && boundGroups.isEmpty()) { + return Boolean.TRUE; + } + + Role updatedRole = applyRoleChanges(role, changes); + Set<String> replacedRoleNames = Collections.singleton(role.name()); + List<Role> replacementRoles = Collections.singletonList(updatedRole); + + for (String username : boundUsers) { + String principal = properties.formatUserPrincipal(username); + applyRoleUpdateForPrincipal( + Owner.Type.USER, + username, + principal, + role, + updatedRole, + changes, + replacedRoleNames, + replacementRoles); + } + + for (String groupName : boundGroups) { + String principal = properties.formatGroupPrincipal(groupName); + applyRoleUpdateForPrincipal( + Owner.Type.GROUP, + groupName, + principal, + role, + updatedRole, + changes, + replacedRoleNames, + replacementRoles); + } + + return Boolean.TRUE; + } + + @Override + public Boolean onGrantedRolesToUser(List<Role> roles, User user) throws RuntimeException { + if (user == null || roles == null || roles.isEmpty()) { + return Boolean.TRUE; + } + String principal = properties.formatUserPrincipal(user.name()); + createAcls(bindingsForRoles(roles, principal)); + return Boolean.TRUE; + } + + @Override + public Boolean onRevokedRolesFromUser(List<Role> roles, User user) throws RuntimeException { + if (user == null || roles == null || roles.isEmpty()) { + return Boolean.TRUE; + } + String principal = properties.formatUserPrincipal(user.name()); + Set<AclBinding> candidateRemovals = bindingsForRoles(roles, principal); + removeBindingsPreservingActiveGrants( + Owner.Type.USER, + user.name(), + candidateRemovals, + extractRoleNames(roles), + Collections.emptyList(), + Collections.emptyMap(), + null); + return Boolean.TRUE; + } + + @Override + public Boolean onGrantedRolesToGroup(List<Role> roles, Group group) throws RuntimeException { + if (group == null || roles == null || roles.isEmpty()) { + return Boolean.TRUE; + } + String principal = properties.formatGroupPrincipal(group.name()); + createAcls(bindingsForRoles(roles, principal)); Review Comment: Addressed in f11736e69: added authorization.kafka.group-acl.enabled (default false) so Group:<name> ACL push-down is skipped with an explicit warning on stock StandardAuthorizer deployments and only emitted when explicitly enabled for a group-aware Authorizer or KafkaPrincipalBuilder. Documented in docs/kafka-catalog.md and tested in TestKafkaAuthorizationPlugin / KafkaAuthorizationIT. -- 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]
