Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
xunliu committed Dec 11, 2024
1 parent 4cd031e commit 1179f8a
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void testPropertyMeta() {
propertyEntryMap.containsKey(AuthorizationPropertiesMetadata.RANGER_SERVICE_NAME));
Assertions.assertTrue(
propertyEntryMap.containsKey(
AuthorizationPropertiesMetadata.getInstance().wildcardPropertyKey()));
AuthorizationPropertiesMetadata.getInstance().prefixWildcardSegmentName()));
Assertions.assertTrue(
propertyEntryMap.containsKey(AuthorizationPropertiesMetadata.CHAIN_CATALOG_PROVIDER));
Assertions.assertTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ public static synchronized AuthorizationPropertiesMetadata getInstance() {
return instance;
}

public static final String FIRST_SEGMENT_NAME = "authorization";
public static final String SECOND_SEGMENT_NAME = "chain";
static AuthorizationPropertiesMetadata authPropInstance =
AuthorizationPropertiesMetadata.getInstance();

public static final String FIRST_NODE_NAME = "authorization";
public static final String CHAIN_NODE_NAME = "chain";

/** Ranger admin web URIs */
private static final String RANGER_ADMIN_URL_KEY = "ranger.admin.url";
Expand All @@ -50,7 +53,7 @@ public static String getRangerAdminUrlKey() {
}

public static final String RANGER_ADMIN_URL =
String.format("%s.%s", FIRST_SEGMENT_NAME, RANGER_ADMIN_URL_KEY);
String.format("%s.%s", FIRST_NODE_NAME, RANGER_ADMIN_URL_KEY);
/** Ranger authentication type kerberos or simple */
private static final String RANGER_AUTH_TYPE_KEY = "ranger.auth.type";

Expand All @@ -59,7 +62,7 @@ public static String getRangerAuthTypeKey() {
}

public static final String RANGER_AUTH_TYPE =
String.format("%s.%s", FIRST_SEGMENT_NAME, RANGER_AUTH_TYPE_KEY);
String.format("%s.%s", FIRST_NODE_NAME, RANGER_AUTH_TYPE_KEY);
/**
* Ranger admin web login username(auth_type=simple), or kerberos principal(auth_type=kerberos)
*/
Expand All @@ -70,7 +73,7 @@ public static String getRangerUsernameKey() {
}

public static final String RANGER_USERNAME =
String.format("%s.%s", FIRST_SEGMENT_NAME, RANGER_USERNAME_KEY);
String.format("%s.%s", FIRST_NODE_NAME, RANGER_USERNAME_KEY);
/**
* Ranger admin web login user password(auth_type=simple), or path of the keytab
* file(auth_type=kerberos)
Expand All @@ -82,7 +85,7 @@ public static String getRangerPasswordKey() {
}

public static final String RANGER_PASSWORD =
String.format("%s.%s", FIRST_SEGMENT_NAME, RANGER_PASSWORD_KEY);
String.format("%s.%s", FIRST_NODE_NAME, RANGER_PASSWORD_KEY);

/** Ranger service name */
private static final String RANGER_SERVICE_NAME_KEY = "ranger.service.name";
Expand All @@ -92,7 +95,7 @@ public static String getRangerServiceNameKey() {
}

public static final String RANGER_SERVICE_NAME =
String.format("%s.%s", FIRST_SEGMENT_NAME, RANGER_SERVICE_NAME_KEY);
String.format("%s.%s", FIRST_NODE_NAME, RANGER_SERVICE_NAME_KEY);

/** Chain authorization plugin provider */
private static final String CHAIN_CATALOG_PROVIDER_KEY = "catalog-provider";
Expand All @@ -102,8 +105,7 @@ public static String getChainCatalogProviderKey() {
}

public static final String CHAIN_CATALOG_PROVIDER =
AuthorizationPropertiesMetadata.getInstance()
.getPropertyValue(WILDCARD, CHAIN_CATALOG_PROVIDER_KEY);
authPropInstance.getFullPropertyName(WILDCARD, CHAIN_CATALOG_PROVIDER_KEY);

/** Chain authorization plugin provider */
private static final String CHAIN_PROVIDER_KEY = "provider";
Expand All @@ -113,35 +115,32 @@ public static String getChainProviderKey() {
}

public static final String CHAIN_PROVIDER =
AuthorizationPropertiesMetadata.getInstance().getPropertyValue(WILDCARD, CHAIN_PROVIDER_KEY);
authPropInstance.getFullPropertyName(WILDCARD, CHAIN_PROVIDER_KEY);
/** Chain authorization Ranger admin web URIs */
public static final String CHAIN_RANGER_ADMIN_URL =
AuthorizationPropertiesMetadata.getInstance()
.getPropertyValue(WILDCARD, RANGER_ADMIN_URL_KEY);
authPropInstance.getFullPropertyName(WILDCARD, RANGER_ADMIN_URL_KEY);
/** Chain authorization Ranger authentication type kerberos or simple */
public static final String CHAIN_RANGER_AUTH_TYPES =
AuthorizationPropertiesMetadata.getInstance()
.getPropertyValue(WILDCARD, RANGER_AUTH_TYPE_KEY);
authPropInstance.getFullPropertyName(WILDCARD, RANGER_AUTH_TYPE_KEY);
/** Chain authorization Ranger username */
public static final String CHAIN_RANGER_USERNAME =
AuthorizationPropertiesMetadata.getInstance().getPropertyValue(WILDCARD, RANGER_USERNAME_KEY);
authPropInstance.getFullPropertyName(WILDCARD, RANGER_USERNAME_KEY);
/**
* Chain authorization Ranger admin web login user password(auth_type=simple), or path of the
* keytab file(auth_type=kerberos)
*/
public static final String CHAIN_RANGER_PASSWORD =
AuthorizationPropertiesMetadata.getInstance().getPropertyValue(WILDCARD, RANGER_PASSWORD_KEY);
authPropInstance.getFullPropertyName(WILDCARD, RANGER_PASSWORD_KEY);
/** Chain authorization Ranger service name */
public static final String CHAIN_RANGER_SERVICE_NAME =
AuthorizationPropertiesMetadata.getInstance()
.getPropertyValue(WILDCARD, RANGER_SERVICE_NAME_KEY);
authPropInstance.getFullPropertyName(WILDCARD, RANGER_SERVICE_NAME_KEY);

public static final Map<String, PropertyEntry<?>> AUTHORIZATION_PROPERTY_ENTRIES =
ImmutableMap.<String, PropertyEntry<?>>builder()
.put(
AuthorizationPropertiesMetadata.getInstance().wildcardPropertyKey(),
authPropInstance.prefixWildcardSegmentName(),
PropertyEntry.wildcardPropertyEntry(
AuthorizationPropertiesMetadata.getInstance().wildcardPropertyKey(),
authPropInstance.prefixWildcardSegmentName(),
"The Chain authorization plugins",
true,
false))
Expand Down Expand Up @@ -224,7 +223,7 @@ protected Map<String, PropertyEntry<?>> specificPropertyEntries() {

@Override
public String prefixName() {
return String.format("%s.%s", FIRST_SEGMENT_NAME, SECOND_SEGMENT_NAME);
return String.format("%s.%s", FIRST_NODE_NAME, CHAIN_NODE_NAME);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@
* NOTE: Suffix properties-key{N} support multiple segment, separated by dot. for example:
* "x1.y1.z1" <br>
* <br>
* Use define a WildcardPropertiesMetadata object: <br>
* a1.b1.c1.Wildcard = "WildcardValue1,WildcardValue2" <br>
* a1.b1.c1.{WildcardValue1}.x1.y1.z1 = "WildcardValue1 property value" <br>
* a1.b1.c1.{WildcardValue1}.x1.y2.z2 = "WildcardValue1 property value" <br>
* a1.b1.c1.{WildcardValue1}.x1.y2.z3 = "WildcardValue1 property value" <br>
* a1.b1.c1.{WildcardValue2}.x1.y1.z1 = "WildcardValue2 property value" <br>
* a1.b1.c1.{WildcardValue2}.x1.y2.z2 = "WildcardValue2 property value" <br>
* a1.b1.c1.{WildcardValue2}.x1.y2.z3 = "WildcardValue2 property value" <br>
* <br>
* Configuration Example: {@link AuthorizationPropertiesMetadata}, <br>
* The Prefix is "authorization.chain", <br>
* The Wildcard is "plugins" <br>
Expand Down Expand Up @@ -77,14 +68,14 @@ public interface WildcardPropertiesMetadata {
/** The WildcardNode define name */
String wildcardName();

/** The `Prefix.Wildcard` properties key name */
default String wildcardPropertyKey() {
/** The `Prefix.Wildcard` properties segment name */
default String prefixWildcardSegmentName() {
return String.format("%s.%s", prefixName(), wildcardName());
}

/** Get the property value by wildcard value and property key */
default String getPropertyValue(String wildcardValue, String propertyKey) {
return String.format("%s.%s.%s", prefixName(), wildcardValue, propertyKey);
/** Get the full name property name by wildcard value and property key */
default String getFullPropertyName(String wildcardValue, String suffixName) {
return String.format("%s.%s.%s", prefixName(), wildcardValue, suffixName);
}

/**
Expand All @@ -101,75 +92,70 @@ static void validate(PropertiesMetadata propertiesMetadata, Map<String, String>
propertiesMetadata.propertyEntries().keySet().stream()
.filter(propertiesMetadata::isWildcardProperty)
.collect(Collectors.toList());
if (wildcardProperties.size() > 0) {
// Find the wildcard config key from the properties
List<String> wildcardNodePropertyKeys =
wildcardProperties.stream()
.filter(key -> !key.contains(WildcardPropertiesMetadata.WILDCARD))
.collect(Collectors.toList());
Preconditions.checkArgument(
wildcardNodePropertyKeys.size() == 1,
"Only one wildcard config key is allowed, found: %s",
wildcardNodePropertyKeys);
String wildcardNodePropertyKey = wildcardNodePropertyKeys.get(0);
String wildcardValue = properties.get(wildcardNodePropertyKey);
if (wildcardValue == null || wildcardValue.isEmpty()) {
return;
}
if (wildcardProperties.isEmpty()) {
return;
}

// Get the wildcard values from the properties
List<String> wildcardValues =
Arrays.stream(wildcardValue.split(WILDCARD_CONFIG_VALUES_SPLITTER))
.map(String::trim)
.collect(Collectors.toList());
wildcardValues.stream()
.filter(v -> v.contains("."))
.forEach(
v -> {
throw new IllegalArgumentException(
String.format(
"Wildcard property values cannot be set with `.` character in the `%s = %s`.",
wildcardNodePropertyKey, properties.get(wildcardNodePropertyKey)));
});
Preconditions.checkArgument(
wildcardValues.size() == wildcardValues.stream().distinct().count(),
"Duplicate values in wildcard config values: %s",
wildcardValues);
// Find the wildcard config key from the properties
List<String> wildcardNodePropertyKeys =
wildcardProperties.stream()
.filter(key -> !key.contains(WILDCARD))
.collect(Collectors.toList());
Preconditions.checkArgument(
wildcardNodePropertyKeys.size() == 1,
"Only one wildcard config key is allowed, found: %s",
wildcardNodePropertyKeys);
String wildcardNodePropertyKey = wildcardNodePropertyKeys.get(0);
String wildcardValue = properties.get(wildcardNodePropertyKey);
if (wildcardValue == null || wildcardValue.isEmpty()) {
return;
}

// Get all wildcard properties with wildcard values
List<Pattern> patterns =
wildcardProperties.stream()
.filter(k -> k.contains(WILDCARD))
.collect(Collectors.toList())
.stream()
.map(wildcard -> wildcard.replace(".", "\\.").replace(WILDCARD, "([^.]+)"))
.map(Pattern::compile)
.collect(Collectors.toList());
// Get the wildcard values from the properties
List<String> wildcardValues =
Arrays.stream(wildcardValue.split(WILDCARD_CONFIG_VALUES_SPLITTER))
.map(String::trim)
.collect(Collectors.toList());
wildcardValues.stream()
.filter(v -> v.contains("."))
.forEach(
v -> {
throw new IllegalArgumentException(
String.format(
"Wildcard property values cannot be set with `.` character in the `%s = %s`.",
wildcardNodePropertyKey, properties.get(wildcardNodePropertyKey)));
});
Preconditions.checkArgument(
wildcardValues.size() == wildcardValues.stream().distinct().count(),
"Duplicate values in wildcard config values: %s",
wildcardValues);

String secondNodePropertyKey = ((WildcardPropertiesMetadata) propertiesMetadata).prefixName();
for (String key :
properties.keySet().stream()
.filter(
k -> !k.equals(wildcardNodePropertyKey) && k.startsWith(secondNodePropertyKey))
.collect(Collectors.toList())) {
boolean matches =
patterns.stream()
.anyMatch(
pattern -> {
Matcher matcher = pattern.matcher(key);
if (matcher.find()) {
String group = matcher.group(1);
return wildcardValues.contains(group);
} else {
return false;
}
});
Preconditions.checkArgument(
matches,
"Wildcard properties `%s` not a valid wildcard config with values: %s",
key,
wildcardValues);
}
}
// Get all wildcard properties with wildcard values
List<Pattern> patterns =
wildcardProperties.stream()
.filter(k -> k.contains(WILDCARD))
.collect(Collectors.toList())
.stream()
.map(wildcard -> wildcard.replace(".", "\\.").replace(WILDCARD, "([^.]+)"))
.map(Pattern::compile)
.collect(Collectors.toList());

List<String> noMatch =
properties.keySet().stream()
.filter(
key ->
patterns.stream()
.anyMatch(
pattern -> {
Matcher matcher = pattern.matcher(key);
return matcher.matches()
&& !wildcardValues.contains(matcher.group(1));
}))
.collect(Collectors.toList());
Preconditions.checkArgument(
noMatch.isEmpty(),
"Wildcard properties `%s` not a valid wildcard config with values: %s",
noMatch,
wildcardValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino;
package org.apache.gravitino.connector.authorization;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
Expand All @@ -35,14 +35,14 @@ public class TestAuthorizationPropertiesMeta {
private static final Logger LOG = LoggerFactory.getLogger(TestAuthorizationPropertiesMeta.class);

@Test
public void checkChainPropertyDefines() throws IllegalAccessException {
public void checkAuthPropertyDefines() throws IllegalAccessException {
Map<String, String> mapVariable =
getPublicStaticVariableFromClass(AuthorizationPropertiesMetadata.class);
List<String> ignoreChecks =
Arrays.asList(
AuthorizationPropertiesMetadata.CHAIN_CATALOG_PROVIDER,
AuthorizationPropertiesMetadata.CHAIN_PROVIDER,
AuthorizationPropertiesMetadata.getInstance().wildcardPropertyKey());
AuthorizationPropertiesMetadata.getInstance().prefixWildcardSegmentName());
mapVariable.values().stream()
.forEach(
value -> {
Expand All @@ -51,8 +51,8 @@ public void checkChainPropertyDefines() throws IllegalAccessException {
String pluginPropValue =
value.replace(
AuthorizationPropertiesMetadata.getInstance()
.getPropertyValue(WildcardPropertiesMetadata.WILDCARD, ""),
String.format("%s.", AuthorizationPropertiesMetadata.FIRST_SEGMENT_NAME));
.getFullPropertyName(WildcardPropertiesMetadata.WILDCARD, ""),
String.format("%s.", AuthorizationPropertiesMetadata.FIRST_NODE_NAME));
LOG.info("Checking variable: {}, pluginPropValue: {}", value, pluginPropValue);
Assertions.assertTrue(
mapVariable.values().contains(pluginPropValue),
Expand Down
Loading

0 comments on commit 1179f8a

Please sign in to comment.