diff --git a/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcAuthorizationProperties.java b/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcAuthorizationProperties.java index 4d325c6812e..b13504fd2fd 100644 --- a/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcAuthorizationProperties.java +++ b/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcAuthorizationProperties.java @@ -29,17 +29,16 @@ public class JdbcAuthorizationProperties { public static final String JDBC_DRIVER = CONFIG_PREFIX + "driver"; public static void validate(Map properties) { - if (!properties.containsKey(JDBC_URL)) { - throw new IllegalArgumentException("JDBC URL is required"); - } - if (!properties.containsKey(JDBC_USERNAME)) { - throw new IllegalArgumentException("JDBC username is required"); - } - if (!properties.containsKey(JDBC_PASSWORD)) { - throw new IllegalArgumentException("JDBC password is required"); - } - if (!properties.containsKey(JDBC_DRIVER)) { - throw new IllegalArgumentException("JDBC driver is required"); + String errorMsg = "%s is required"; + check(properties, JDBC_URL, errorMsg); + check(properties, JDBC_USERNAME, errorMsg); + check(properties, JDBC_PASSWORD, errorMsg); + check(properties, JDBC_DRIVER, errorMsg); + } + + private static void check(Map properties, String key, String errorMsg) { + if (!properties.containsKey(key) && properties.get(key) != null) { + throw new IllegalArgumentException(String.format(errorMsg, key)); } } } diff --git a/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcMetadataObject.java b/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcMetadataObject.java index b813ba53772..f7a577aeaee 100644 --- a/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcMetadataObject.java +++ b/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcMetadataObject.java @@ -28,9 +28,9 @@ public class JdbcMetadataObject implements AuthorizationMetadataObject { private final String parent; private final String name; - private final MetadataObject.Type type; + private final Type type; - public JdbcMetadataObject(String parent, String name, MetadataObject.Type type) { + public JdbcMetadataObject(String parent, String name, Type type) { this.parent = parent; this.name = name; this.type = type; @@ -54,7 +54,7 @@ public List names() { @Override public Type type() { - return () -> type; + return type; } @Override @@ -66,13 +66,38 @@ public void validateAuthorizationMetadataObject() throws IllegalArgumentExceptio names.size() <= 2, "The name of the object is not in the format of 'database.table'."); Preconditions.checkArgument(type != null, "The type of the object is null."); Preconditions.checkArgument( - names.size() == 1 || type == MetadataObject.Type.SCHEMA, + names.size() == 1 || type.metadataObjectType() == MetadataObject.Type.SCHEMA, "The type of the object is not SCHEMA."); Preconditions.checkArgument( - names.size() == 2 || type == MetadataObject.Type.TABLE, + names.size() == 2 || type.metadataObjectType() == MetadataObject.Type.TABLE, "The type of the object is not TABLE."); for (String name : names) { Preconditions.checkArgument(name != null, "Cannot create a metadata object with null name"); } } + + public enum Type implements AuthorizationMetadataObject.Type { + SCHEMA(MetadataObject.Type.SCHEMA), + TABLE(MetadataObject.Type.TABLE); + + private final MetadataObject.Type metadataType; + + Type(MetadataObject.Type type) { + this.metadataType = type; + } + + public MetadataObject.Type metadataObjectType() { + return metadataType; + } + + public static Type fromMetadataType(MetadataObject.Type metadataType) { + for (Type type : Type.values()) { + if (type.metadataObjectType() == metadataType) { + return type; + } + } + throw new IllegalArgumentException( + "No matching RangerMetadataObject.Type for " + metadataType); + } + } } diff --git a/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcSecurableObject.java b/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcSecurableObject.java index 70ce0e246ee..ab07fd2c2e1 100644 --- a/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcSecurableObject.java +++ b/authorizations/authorization-jdbc/src/main/java/org/apache/gravitino/authorization/jdbc/JdbcSecurableObject.java @@ -38,7 +38,7 @@ public class JdbcSecurableObject extends JdbcMetadataObject private JdbcSecurableObject( String parent, String name, - MetadataObject.Type type, + JdbcMetadataObject.Type type, List privileges) { super(parent, name, type); this.privileges = privileges; @@ -48,8 +48,10 @@ static JdbcSecurableObject create( String schema, String table, List privileges) { String parent = table == null ? null : schema; String name = table == null ? schema : table; - MetadataObject.Type type = - table == null ? MetadataObject.Type.SCHEMA : MetadataObject.Type.TABLE; + JdbcMetadataObject.Type type = + table == null + ? JdbcMetadataObject.Type.fromMetadataType(MetadataObject.Type.SCHEMA) + : JdbcMetadataObject.Type.fromMetadataType(MetadataObject.Type.TABLE); return new JdbcSecurableObject(parent, name, type, privileges); }