-
Notifications
You must be signed in to change notification settings - Fork 392
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
### What changes were proposed in this pull request? Add ChainAuthorizationProperties class ### Why are the changes needed? Fix: #5790 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? Add test
- Loading branch information
Showing
23 changed files
with
675 additions
and
205 deletions.
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
...src/main/java/org/apache/gravitino/authorization/ranger/ChainAuthorizationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* 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.ranger; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* The properties for Chain authorization plugin. <br> | ||
* <br> | ||
* Configuration Example: <br> | ||
* "authorization.chain.plugins" = "hive1,hdfs1" <br> | ||
* "authorization.chain.hive1.provider" = "ranger"; <br> | ||
* "authorization.chain.hive1.ranger.service.type" = "HadoopSQL"; <br> | ||
* "authorization.chain.hive1.ranger.service.name" = "hiveDev"; <br> | ||
* "authorization.chain.hive1.ranger.auth.type" = "simple"; <br> | ||
* "authorization.chain.hive1.ranger.admin.url" = "http://localhost:6080"; <br> | ||
* "authorization.chain.hive1.ranger.username" = "admin"; <br> | ||
* "authorization.chain.hive1.ranger.password" = "admin"; <br> | ||
* "authorization.chain.hdfs1.provider" = "ranger"; <br> | ||
* "authorization.chain.hdfs1.ranger.service.type" = "HDFS"; <br> | ||
* "authorization.chain.hdfs1.ranger.service.name" = "hdfsDev"; <br> | ||
* "authorization.chain.hdfs1.ranger.auth.type" = "simple"; <br> | ||
* "authorization.chain.hdfs1.ranger.admin.url" = "http://localhost:6080"; <br> | ||
* "authorization.chain.hdfs1.ranger.username" = "admin"; <br> | ||
* "authorization.chain.hdfs1.ranger.password" = "admin"; <br> | ||
*/ | ||
public class ChainAuthorizationProperties { | ||
public static final String PLUGINS_SPLITTER = ","; | ||
/** Chain authorization plugin names */ | ||
public static final String CHAIN_PLUGINS_PROPERTIES_KEY = "authorization.chain.plugins"; | ||
|
||
/** Chain authorization plugin provider */ | ||
public static final String CHAIN_PROVIDER = "authorization.chain.*.provider"; | ||
|
||
static Map<String, String> fetchAuthPluginProperties( | ||
String pluginName, Map<String, String> properties) { | ||
Preconditions.checkArgument( | ||
properties.containsKey(CHAIN_PLUGINS_PROPERTIES_KEY) | ||
&& properties.get(CHAIN_PLUGINS_PROPERTIES_KEY) != null, | ||
String.format("%s is required", CHAIN_PLUGINS_PROPERTIES_KEY)); | ||
|
||
String[] pluginNames = properties.get(CHAIN_PLUGINS_PROPERTIES_KEY).split(PLUGINS_SPLITTER); | ||
Preconditions.checkArgument( | ||
Arrays.asList(pluginNames).contains(pluginName), | ||
String.format("pluginName %s must be one of %s", pluginName, Arrays.toString(pluginNames))); | ||
|
||
String regex = "^authorization\\.chain\\.(" + pluginName + ")\\..*"; | ||
Pattern pattern = Pattern.compile(regex); | ||
|
||
Map<String, String> filteredProperties = new HashMap<>(); | ||
for (Map.Entry<String, String> entry : properties.entrySet()) { | ||
Matcher matcher = pattern.matcher(entry.getKey()); | ||
if (matcher.matches()) { | ||
filteredProperties.put(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
String removeRegex = "^authorization\\.chain\\.(" + pluginName + ")\\."; | ||
Pattern removePattern = Pattern.compile(removeRegex); | ||
|
||
Map<String, String> resultProperties = new HashMap<>(); | ||
for (Map.Entry<String, String> entry : filteredProperties.entrySet()) { | ||
Matcher removeMatcher = removePattern.matcher(entry.getKey()); | ||
if (removeMatcher.find()) { | ||
resultProperties.put(removeMatcher.replaceFirst("authorization."), entry.getValue()); | ||
} | ||
} | ||
|
||
return resultProperties; | ||
} | ||
|
||
public static void validate(Map<String, String> properties) { | ||
Preconditions.checkArgument( | ||
properties.containsKey(CHAIN_PLUGINS_PROPERTIES_KEY), | ||
String.format("%s is required", CHAIN_PLUGINS_PROPERTIES_KEY)); | ||
List<String> pluginNames = | ||
Arrays.stream(properties.get(CHAIN_PLUGINS_PROPERTIES_KEY).split(PLUGINS_SPLITTER)) | ||
.map(String::trim) | ||
.collect(Collectors.toList()); | ||
Preconditions.checkArgument( | ||
!pluginNames.isEmpty(), | ||
String.format("%s must have at least one plugin name", CHAIN_PLUGINS_PROPERTIES_KEY)); | ||
Preconditions.checkArgument( | ||
pluginNames.size() == pluginNames.stream().distinct().count(), | ||
"Duplicate plugin name in %s: %s", | ||
CHAIN_PLUGINS_PROPERTIES_KEY, | ||
pluginNames); | ||
pluginNames.stream() | ||
.filter(v -> v.contains(".")) | ||
.forEach( | ||
v -> { | ||
throw new IllegalArgumentException( | ||
String.format( | ||
"Plugin name cannot be contain `.` character in the `%s = %s`.", | ||
CHAIN_PLUGINS_PROPERTIES_KEY, properties.get(CHAIN_PLUGINS_PROPERTIES_KEY))); | ||
}); | ||
|
||
Pattern pattern = Pattern.compile("^authorization\\.chain\\..*\\..*$"); | ||
Map<String, String> filteredProperties = | ||
properties.entrySet().stream() | ||
.filter(entry -> pattern.matcher(entry.getKey()).matches()) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
|
||
String pluginNamesPattern = String.join("|", pluginNames); | ||
Pattern patternPluginNames = | ||
Pattern.compile("^authorization\\.chain\\.(" + pluginNamesPattern + ")\\..*$"); | ||
for (String key : filteredProperties.keySet()) { | ||
Matcher matcher = patternPluginNames.matcher(key); | ||
Preconditions.checkArgument( | ||
matcher.matches(), | ||
"The key %s does not match the pattern %s", | ||
key, | ||
patternPluginNames.pattern()); | ||
} | ||
|
||
// Generate regex patterns from wildcardProperties | ||
List<String> wildcardProperties = ImmutableList.of(CHAIN_PROVIDER); | ||
for (String pluginName : pluginNames) { | ||
List<Pattern> patterns = | ||
wildcardProperties.stream() | ||
.map(wildcard -> "^" + wildcard.replace("*", pluginName) + "$") | ||
.map(Pattern::compile) | ||
.collect(Collectors.toList()); | ||
// Validate properties keys | ||
for (Pattern pattern1 : patterns) { | ||
boolean matches = | ||
filteredProperties.keySet().stream().anyMatch(key -> pattern1.matcher(key).matches()); | ||
Preconditions.checkArgument( | ||
matches, | ||
"Missing required properties %s for plugin: %s", | ||
filteredProperties, | ||
pattern1.pattern()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...rc/main/java/org/apache/gravitino/authorization/ranger/RangerAuthorizationProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* 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.ranger; | ||
|
||
import com.google.common.base.Preconditions; | ||
import java.util.Map; | ||
|
||
/** The properties for Ranger authorization plugin. */ | ||
public class RangerAuthorizationProperties { | ||
/** Ranger admin web URIs */ | ||
public static final String RANGER_ADMIN_URL = "authorization.ranger.admin.url"; | ||
|
||
/** Ranger service type */ | ||
public static final String RANGER_SERVICE_TYPE = "authorization.ranger.service.type"; | ||
|
||
/** Ranger service name */ | ||
public static final String RANGER_SERVICE_NAME = "authorization.ranger.service.name"; | ||
|
||
/** Ranger authentication type kerberos or simple */ | ||
public static final String RANGER_AUTH_TYPE = "authorization.ranger.auth.type"; | ||
|
||
/** | ||
* Ranger admin web login username(auth_type=simple), or kerberos principal(auth_type=kerberos) | ||
*/ | ||
public static final String RANGER_USERNAME = "authorization.ranger.username"; | ||
|
||
/** | ||
* Ranger admin web login user password(auth_type=simple), or path of the keytab | ||
* file(auth_type=kerberos) | ||
*/ | ||
public static final String RANGER_PASSWORD = "authorization.ranger.password"; | ||
|
||
public static void validate(Map<String, String> properties) { | ||
Preconditions.checkArgument( | ||
properties.containsKey(RANGER_ADMIN_URL), | ||
String.format("%s is required", RANGER_ADMIN_URL)); | ||
Preconditions.checkArgument( | ||
properties.containsKey(RANGER_SERVICE_TYPE), | ||
String.format("%s is required", RANGER_SERVICE_TYPE)); | ||
Preconditions.checkArgument( | ||
properties.containsKey(RANGER_SERVICE_NAME), | ||
String.format("%s is required", RANGER_SERVICE_NAME)); | ||
Preconditions.checkArgument( | ||
properties.containsKey(RANGER_AUTH_TYPE), | ||
String.format("%s is required", RANGER_AUTH_TYPE)); | ||
Preconditions.checkArgument( | ||
properties.containsKey(RANGER_USERNAME), String.format("%s is required", RANGER_USERNAME)); | ||
Preconditions.checkArgument( | ||
properties.containsKey(RANGER_PASSWORD), String.format("%s is required", RANGER_PASSWORD)); | ||
Preconditions.checkArgument( | ||
properties.get(RANGER_ADMIN_URL) != null, | ||
String.format("%s is required", RANGER_ADMIN_URL)); | ||
Preconditions.checkArgument( | ||
properties.get(RANGER_SERVICE_NAME) != null, | ||
String.format("%s is required", RANGER_SERVICE_NAME)); | ||
Preconditions.checkArgument( | ||
properties.get(RANGER_AUTH_TYPE) != null, | ||
String.format("%s is required", RANGER_AUTH_TYPE)); | ||
Preconditions.checkArgument( | ||
properties.get(RANGER_USERNAME) != null, String.format("%s is required", RANGER_USERNAME)); | ||
Preconditions.checkArgument( | ||
properties.get(RANGER_PASSWORD) != null, String.format("%s is required", RANGER_PASSWORD)); | ||
} | ||
} |
Oops, something went wrong.