jerqi commented on code in PR #5904: URL: https://github.com/apache/gravitino/pull/5904#discussion_r1891851800
########## authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcSecurableObjectMappingProvider.java: ########## @@ -0,0 +1,218 @@ +/* + * 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.jdbc; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import java.util.List; +import java.util.Map; +import java.util.Set; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.MetadataObjects; +import org.apache.gravitino.authorization.AuthorizationMetadataObject; +import org.apache.gravitino.authorization.AuthorizationPrivilege; +import org.apache.gravitino.authorization.AuthorizationPrivilegesMappingProvider; +import org.apache.gravitino.authorization.AuthorizationSecurableObject; +import org.apache.gravitino.authorization.Privilege; +import org.apache.gravitino.authorization.SecurableObject; + +/** + * JdbcSecurableObjectMappingProvider is used for translating securable object to authorization + * securable object. + */ +public class JdbcSecurableObjectMappingProvider implements AuthorizationPrivilegesMappingProvider { + + private final Map<Privilege.Name, Set<AuthorizationPrivilege>> privilegeMapping = + ImmutableMap.of( + Privilege.Name.CREATE_TABLE, + Sets.newHashSet(JdbcPrivilege.valueOf(JdbcPrivilege.Type.CREATE)), + Privilege.Name.CREATE_SCHEMA, + Sets.newHashSet(JdbcPrivilege.valueOf(JdbcPrivilege.Type.CREATE)), + Privilege.Name.SELECT_TABLE, + Sets.newHashSet(JdbcPrivilege.valueOf(JdbcPrivilege.Type.SELECT)), + Privilege.Name.MODIFY_TABLE, + Sets.newHashSet( + JdbcPrivilege.valueOf(JdbcPrivilege.Type.SELECT), + JdbcPrivilege.valueOf(JdbcPrivilege.Type.UPDATE), + JdbcPrivilege.valueOf(JdbcPrivilege.Type.DELETE), + JdbcPrivilege.valueOf(JdbcPrivilege.Type.INSERT), + JdbcPrivilege.valueOf(JdbcPrivilege.Type.ALTER)), + Privilege.Name.USE_SCHEMA, + Sets.newHashSet(JdbcPrivilege.valueOf(JdbcPrivilege.Type.USAGE))); + + private final Map<Privilege.Name, PrivilegeScope> privilegeScopeMapping = + ImmutableMap.of( + Privilege.Name.CREATE_TABLE, PrivilegeScope.TABLE, + Privilege.Name.CREATE_SCHEMA, PrivilegeScope.DATABASE, + Privilege.Name.SELECT_TABLE, PrivilegeScope.TABLE, + Privilege.Name.MODIFY_TABLE, PrivilegeScope.TABLE, + Privilege.Name.USE_SCHEMA, PrivilegeScope.DATABASE); + + private final Set<AuthorizationPrivilege> ownerPrivileges = ImmutableSet.of(); + + private final Set<MetadataObject.Type> allowObjectTypes = + ImmutableSet.of( + MetadataObject.Type.METALAKE, + MetadataObject.Type.CATALOG, + MetadataObject.Type.SCHEMA, + MetadataObject.Type.TABLE); + + @Override + public Map<Privilege.Name, Set<AuthorizationPrivilege>> privilegesMappingRule() { + return privilegeMapping; + } + + @Override + public Set<AuthorizationPrivilege> ownerMappingRule() { + return ownerPrivileges; + } + + @Override + public Set<Privilege.Name> allowPrivilegesRule() { + return privilegeMapping.keySet(); + } + + @Override + public Set<MetadataObject.Type> allowMetadataObjectTypesRule() { + return allowObjectTypes; + } + + @Override + public List<AuthorizationSecurableObject> translatePrivilege(SecurableObject securableObject) { + List<AuthorizationSecurableObject> authObjects = Lists.newArrayList(); + List<AuthorizationPrivilege> databasePrivileges = Lists.newArrayList(); + List<AuthorizationPrivilege> tablePrivileges = Lists.newArrayList(); + JdbcAuthorizationObject databaseObject; + JdbcAuthorizationObject tableObject; + switch (securableObject.type()) { + case METALAKE: + case CATALOG: + convertPluginPrivileges(securableObject, databasePrivileges, tablePrivileges); + + if (!databasePrivileges.isEmpty()) { + databaseObject = + new JdbcAuthorizationObject(JdbcAuthorizationObject.ALL, null, databasePrivileges); + authObjects.add(databaseObject); + } + + if (!tablePrivileges.isEmpty()) { + tableObject = + new JdbcAuthorizationObject( + JdbcAuthorizationObject.ALL, JdbcAuthorizationObject.ALL, tablePrivileges); + authObjects.add(tableObject); + } + break; + + case SCHEMA: + convertPluginPrivileges(securableObject, databasePrivileges, tablePrivileges); + if (!databasePrivileges.isEmpty()) { + databaseObject = + new JdbcAuthorizationObject(securableObject.name(), null, databasePrivileges); + authObjects.add(databaseObject); + } + + if (!tablePrivileges.isEmpty()) { + tableObject = + new JdbcAuthorizationObject( + securableObject.name(), JdbcAuthorizationObject.ALL, tablePrivileges); + authObjects.add(tableObject); + } + break; + + case TABLE: + convertPluginPrivileges(securableObject, databasePrivileges, tablePrivileges); + if (!tablePrivileges.isEmpty()) { + MetadataObject metadataObject = + MetadataObjects.parse(securableObject.parent(), MetadataObject.Type.SCHEMA); + tableObject = + new JdbcAuthorizationObject( + metadataObject.name(), securableObject.name(), tablePrivileges); + authObjects.add(tableObject); + } + break; + + default: + throw new IllegalArgumentException( + String.format("Don't support metadata object type %s", securableObject.type())); + } + + return authObjects; + } + + @Override + public List<AuthorizationSecurableObject> translateOwner(MetadataObject metadataObject) { + List<AuthorizationSecurableObject> objects = Lists.newArrayList(); + JdbcPrivilege allPri = JdbcPrivilege.valueOf(JdbcPrivilege.Type.ALL); + switch (metadataObject.type()) { + case METALAKE: + case CATALOG: + objects.add( + new JdbcAuthorizationObject( + JdbcAuthorizationObject.ALL, null, Lists.newArrayList(allPri))); + objects.add( + new JdbcAuthorizationObject( + JdbcAuthorizationObject.ALL, + JdbcAuthorizationObject.ALL, + Lists.newArrayList(allPri))); + break; + case SCHEMA: + objects.add( + new JdbcAuthorizationObject(metadataObject.name(), null, Lists.newArrayList(allPri))); + objects.add( + new JdbcAuthorizationObject( + metadataObject.name(), JdbcAuthorizationObject.ALL, Lists.newArrayList(allPri))); + break; + case TABLE: + MetadataObject schema = + MetadataObjects.parse(metadataObject.parent(), MetadataObject.Type.SCHEMA); + objects.add( + new JdbcAuthorizationObject( + schema.name(), metadataObject.name(), Lists.newArrayList(allPri))); + break; + default: + throw new IllegalArgumentException(""); + } + return objects; + } + + @Override + public AuthorizationMetadataObject translateMetadataObject(MetadataObject metadataObject) { + throw new UnsupportedOperationException("Not supported"); + } + + private void convertPluginPrivileges( Review Comment: Renamed. -- 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]
