jerryshao commented on code in PR #5786: URL: https://github.com/apache/gravitino/pull/5786#discussion_r1896556551
########## authorizations/authorization-chain/build.gradle.kts: ########## @@ -0,0 +1,146 @@ +/* + * 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. + */ +description = "authorization-chain" + +plugins { + `maven-publish` + id("java") + id("idea") +} + +val scalaVersion: String = project.properties["scalaVersion"] as? String ?: extra["defaultScalaVersion"].toString() +val sparkVersion: String = libs.versions.spark35.get() +val kyuubiVersion: String = libs.versions.kyuubi4paimon.get() +val sparkMajorVersion: String = sparkVersion.substringBeforeLast(".") + +dependencies { + implementation(project(":api")) { + exclude(group = "*") + } + implementation(project(":core")) { + exclude(group = "*") + } + implementation(project(":common")) { + exclude(group = "*") + } + implementation(project(":authorizations:authorization-common")) { + exclude(group = "*") + } + implementation(libs.bundles.log4j) + implementation(libs.commons.lang3) + implementation(libs.guava) + implementation(libs.javax.jaxb.api) { + exclude("*") + } + implementation(libs.javax.ws.rs.api) + implementation(libs.jettison) + compileOnly(libs.lombok) + implementation(libs.rome) Review Comment: Can you make this alphabetically ordering? ########## authorizations/authorization-chain/src/main/java/org/apache/gravitino/authorization/chain/ChainAuthorizationPlugin.java: ########## @@ -0,0 +1,283 @@ +/* + * 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.chain; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.gravitino.Catalog; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.authorization.AuthorizationProperties; +import org.apache.gravitino.authorization.ChainAuthorizationProperties; +import org.apache.gravitino.authorization.Group; +import org.apache.gravitino.authorization.MetadataObjectChange; +import org.apache.gravitino.authorization.Owner; +import org.apache.gravitino.authorization.Role; +import org.apache.gravitino.authorization.RoleChange; +import org.apache.gravitino.authorization.User; +import org.apache.gravitino.connector.authorization.AuthorizationPlugin; +import org.apache.gravitino.connector.authorization.BaseAuthorization; +import org.apache.gravitino.exceptions.AuthorizationPluginException; +import org.apache.gravitino.utils.IsolatedClassLoader; + +/** Chain authorization operations plugin class. <br> */ +public class ChainAuthorizationPlugin implements AuthorizationPlugin { + private List<AuthorizationPlugin> plugins = Lists.newArrayList(); + private final String metalake; + + public ChainAuthorizationPlugin( + String metalake, String catalogProvider, Map<String, String> config) { + this.metalake = metalake; + initPlugins(catalogProvider, config); + } + + private void initPlugins(String catalogProvider, Map<String, String> properties) { + ChainAuthorizationProperties chainAuthorizationProperties = + new ChainAuthorizationProperties(properties); + chainAuthorizationProperties.validate(); + // Validate the properties for each plugin + chainAuthorizationProperties + .plugins() + .forEach( + pluginName -> { + Map<String, String> pluginProperties = + chainAuthorizationProperties.fetchAuthPluginProperties(pluginName); + String authProvider = chainAuthorizationProperties.getPluginProvider(pluginName); + AuthorizationProperties.validate(authProvider, pluginProperties); + }); + // Create the plugins + chainAuthorizationProperties + .plugins() + .forEach( + pluginName -> { + String authProvider = chainAuthorizationProperties.getPluginProvider(pluginName); + Map<String, String> pluginConfig = + chainAuthorizationProperties.fetchAuthPluginProperties(pluginName); + + ArrayList<String> libAndResourcesPaths = Lists.newArrayList(); + BaseAuthorization.buildAuthorizationPkgPath( + ImmutableMap.of(Catalog.AUTHORIZATION_PROVIDER, authProvider)) + .ifPresent(libAndResourcesPaths::add); + IsolatedClassLoader classLoader = + IsolatedClassLoader.buildClassLoader(libAndResourcesPaths); + try { + BaseAuthorization<?> authorization = + BaseAuthorization.createAuthorization(classLoader, authProvider); + AuthorizationPlugin authorizationPlugin = + authorization.newPlugin(metalake, catalogProvider, pluginConfig); + plugins.add(authorizationPlugin); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + } + + @Override + public void close() throws IOException { + for (AuthorizationPlugin plugin : plugins) { + plugin.close(); + } + } + + @Override + public Boolean onMetadataUpdated(MetadataObjectChange... changes) + throws AuthorizationPluginException { + for (AuthorizationPlugin plugin : plugins) { + Boolean result = plugin.onMetadataUpdated(changes); Review Comment: Will we throw exceptions here, what happens if the exception throws in the for loop? ########## authorizations/authorization-chain/src/main/java/org/apache/gravitino/authorization/chain/ChainAuthorizationPlugin.java: ########## @@ -0,0 +1,283 @@ +/* + * 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.chain; + +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Lists; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import org.apache.gravitino.Catalog; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.authorization.AuthorizationProperties; +import org.apache.gravitino.authorization.ChainAuthorizationProperties; +import org.apache.gravitino.authorization.Group; +import org.apache.gravitino.authorization.MetadataObjectChange; +import org.apache.gravitino.authorization.Owner; +import org.apache.gravitino.authorization.Role; +import org.apache.gravitino.authorization.RoleChange; +import org.apache.gravitino.authorization.User; +import org.apache.gravitino.connector.authorization.AuthorizationPlugin; +import org.apache.gravitino.connector.authorization.BaseAuthorization; +import org.apache.gravitino.exceptions.AuthorizationPluginException; +import org.apache.gravitino.utils.IsolatedClassLoader; + +/** Chain authorization operations plugin class. <br> */ +public class ChainAuthorizationPlugin implements AuthorizationPlugin { + private List<AuthorizationPlugin> plugins = Lists.newArrayList(); + private final String metalake; + + public ChainAuthorizationPlugin( + String metalake, String catalogProvider, Map<String, String> config) { + this.metalake = metalake; + initPlugins(catalogProvider, config); + } + + private void initPlugins(String catalogProvider, Map<String, String> properties) { + ChainAuthorizationProperties chainAuthorizationProperties = + new ChainAuthorizationProperties(properties); + chainAuthorizationProperties.validate(); + // Validate the properties for each plugin + chainAuthorizationProperties + .plugins() + .forEach( + pluginName -> { + Map<String, String> pluginProperties = + chainAuthorizationProperties.fetchAuthPluginProperties(pluginName); + String authProvider = chainAuthorizationProperties.getPluginProvider(pluginName); + AuthorizationProperties.validate(authProvider, pluginProperties); + }); + // Create the plugins + chainAuthorizationProperties + .plugins() + .forEach( + pluginName -> { + String authProvider = chainAuthorizationProperties.getPluginProvider(pluginName); + Map<String, String> pluginConfig = + chainAuthorizationProperties.fetchAuthPluginProperties(pluginName); + + ArrayList<String> libAndResourcesPaths = Lists.newArrayList(); + BaseAuthorization.buildAuthorizationPkgPath( + ImmutableMap.of(Catalog.AUTHORIZATION_PROVIDER, authProvider)) + .ifPresent(libAndResourcesPaths::add); + IsolatedClassLoader classLoader = + IsolatedClassLoader.buildClassLoader(libAndResourcesPaths); Review Comment: Why do we need to build different classloaders for different plugins, can we just use catalog's classloader? -- 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]
