Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jerqi committed Dec 24, 2024
1 parent 9f410f8 commit 36e9a91
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@ public class JdbcAuthorizationProperties {
public static final String JDBC_DRIVER = CONFIG_PREFIX + "driver";

public static void validate(Map<String, String> 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<String, String> properties, String key, String errorMsg) {
if (!properties.containsKey(key) && properties.get(key) != null) {
throw new IllegalArgumentException(String.format(errorMsg, key));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,7 +54,7 @@ public List<String> names() {

@Override
public Type type() {
return () -> type;
return type;
}

@Override
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class JdbcSecurableObject extends JdbcMetadataObject
private JdbcSecurableObject(
String parent,
String name,
MetadataObject.Type type,
JdbcMetadataObject.Type type,
List<AuthorizationPrivilege> privileges) {
super(parent, name, type);
this.privileges = privileges;
Expand All @@ -48,8 +48,10 @@ static JdbcSecurableObject create(
String schema, String table, List<AuthorizationPrivilege> 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);
}
Expand Down

0 comments on commit 36e9a91

Please sign in to comment.