Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#5661] feat(auth): Add JDBC authorization plugin interface #5904

Merged
merged 10 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
xunliu marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
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
Loading