Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#2235][part-1] feat(api,core): Add the role entity (#2772)

qqqttt12364bf3394834f
diff --git a/api/src/main/java/com/datastrato/gravitino/authorization/Privilege.java b/api/src/main/java/com/datastrato/gravitino/authorization/Privilege.java
new file mode 100644
index 0000000000..d27bde422e
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/authorization/Privilege.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.authorization;
+
+import com.datastrato.gravitino.annotation.Evolving;
+
+/**
+ * The interface of a privilege. The privilege represents the ability to execute kinds of operations
+ * for kinds of entities
+ */
+@Evolving
+public interface Privilege {
+
+ /** @return The generic name of the privilege. */
+ Name name();
+
+ /** @return A readable string representation for the privilege. */
+ String simpleString();
+
+ /** The name of this privilege. */
+ enum Name {
+ /** The privilege of load a catalog. */
+ LOAD_CATALOG
+ }
+}
diff --git a/api/src/main/java/com/datastrato/gravitino/authorization/Privileges.java b/api/src/main/java/com/datastrato/gravitino/authorization/Privileges.java
new file mode 100644
index 0000000000..9eb4f3f9ef
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/authorization/Privileges.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.authorization;
+
+/** The helper class for {@link Privilege}. */
+public class Privileges {
+
+ /**
+ * Returns the Privilege from the string representation.
+ *
+ * @param privilege The string representation of the privilege.
+ * @return The Privilege.
+ */
+ public static Privilege fromString(String privilege) {
+ Privilege.Name name = Privilege.Name.valueOf(privilege);
+ return fromName(name);
+ }
+
+ /**
+ * Returns the Privilege from the `Privilege.Name`.
+ *
+ * @param name The `Privilege.Name` of the privilege.
+ * @return The Privilege.
+ */
+ public static Privilege fromName(Privilege.Name name) {
+ switch (name) {
+ case LOAD_CATALOG:
+ return LoadCatalog.get();
+ default:
+ throw new IllegalArgumentException("Don't support the privilege: " + name);
+ }
+ }
+
+ /** The privilege of load a catalog. */
+ public static class LoadCatalog implements Privilege {
+ private static final LoadCatalog INSTANCE = new LoadCatalog();
+
+ /** @return The instance of the privilege. */
+ public static LoadCatalog get() {
+ return INSTANCE;
+ }
+
+ private LoadCatalog() {}
+
+ /** @return The generic name of the privilege. */
+ @Override
+ public Name name() {
+ return Name.LOAD_CATALOG;
+ }
+
+ /** @return A readable string representation for the privilege. */
+ @Override
+ public String simpleString() {
+ return "load catalog";
+ }
+ }
+}
diff --git a/api/src/main/java/com/datastrato/gravitino/authorization/Role.java b/api/src/main/java/com/datastrato/gravitino/authorization/Role.java
new file mode 100644
index 0000000000..914fb7159c
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/authorization/Role.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.authorization;
+
+import com.datastrato.gravitino.Auditable;
+import com.datastrato.gravitino.annotation.Evolving;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * The interface of a role. The role is the entity which has kinds of privileges. One role can have
+ * multiple privileges of one securable object. Gravitino chooses to bind one securable object to
+ * one role to avoid granting too many privileges to one role.
+ */
+@Evolving
+public interface Role extends Auditable {
+
+ /**
+ * The name of the role.
+ *
+ * @return The name of the role.
+ */
+ String name();
+
+ /**
+ * The properties of the role. Note, this method will return null if the properties are not set.
+ *
+ * @return The properties of the role.
+ */
+ Map<String, String> properties();
+
+ /**
+ * The privileges of the role. All privileges belong to one securable object. For example: If the
+ * securable object is a table, the privileges could be `READ TABLE`, `WRITE TABLE`, etc. If a
+ * schema has the privilege of `LOAD TABLE`. It means the role can all tables of the schema.
+ *
+ * @return The privileges of the role.
+ */
+ List<Privilege> privileges();
+
+ /**
+ * The securable object represents a special kind of entity with a unique identifier. All
+ * securable objects are organized by tree structure. For example: If the securable object is a
+ * table, the identifier may be `catalog1.schema1.table1`.
+ *
+ * @return The securable object of the role.
+ */
+ SecurableObject securableObject();
+}
diff --git a/api/src/main/java/com/datastrato/gravitino/authorization/SecurableObject.java b/api/src/main/java/com/datastrato/gravitino/authorization/SecurableObject.java
new file mode 100644
index 0000000000..b3d8aa5ce2
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/authorization/SecurableObject.java
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.authorization;
+
+import com.datastrato.gravitino.annotation.Evolving;
+import javax.annotation.Nullable;
+
+/**
+ * The securable object is the entity which access can be granted. Unless allowed by a grant, access
+ * is denied. Gravitino organizes the securable objects using tree structure. The securable object
+ * may be a catalog, a table or a schema, etc. For example, `catalog1.schema1.table1` represents a
+ * table named `table1`. It's in the schema named `schema1`. The schema is in the catalog named
+ * `catalog1`. Similarly, `catalog1.schema1.topic1` can represent a topic.
+ * `catalog1.schema1.fileset1` can represent a fileset. `*` represents all the catalogs. If you want
+ * to use other securable objects which represents all entities," you can use their parent entity,
+ * For example if you want to have read table privileges of all tables of `catalog1.schema1`, " you
+ * can use add `read table` privilege for `catalog1.schema1` directly
+ */
+@Evolving
+public interface SecurableObject {
+
+ /**
+ * The parent securable object. If the securable object doesn't have parent, this method will
+ * return null.
+ *
+ * @return The parent securable object.
+ */
+ @Nullable
+ SecurableObject parent();
+
+ /**
+ * The name of th securable object.
+ *
+ * @return The name of the securable object.
+ */
+ String name();
+}
diff --git a/api/src/main/java/com/datastrato/gravitino/authorization/SecurableObjects.java b/api/src/main/java/com/datastrato/gravitino/authorization/SecurableObjects.java
new file mode 100644
index 0000000000..48b05f6ef6
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/authorization/SecurableObjects.java
@@ -0,0 +1,189 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.authorization;
+
+import java.util.Objects;
+
+/** The helper class for {@link SecurableObject}. */
+public class SecurableObjects {
+
+ /**
+ * Create the {@link SecurableObject} with the given names.
+ *
+ * @param names The names of the securable object.
+ * @return The created {@link SecurableObject}
+ */
+ public static SecurableObject of(String... names) {
+ if (names == null) {
+ throw new IllegalArgumentException("Cannot create a securable object with null names");
+ }
+
+ if (names.length == 0) {
+ throw new IllegalArgumentException("Cannot create a securable object with no names");
+ }
+
+ SecurableObject parent = null;
+ for (String name : names) {
+ if (name == null) {
+ throw new IllegalArgumentException("Cannot create a securable object with null name");
+ }
+
+ if (name.equals("*")) {
+ throw new IllegalArgumentException(
+ "Cannot create a securable object with `*` name. If you want to use a securable object which represents all catalogs,"
+ + " you use the method `ofAllCatalogs`."
+ + " If you want to create an another securable object which represents all entities,"
+ + " you can use its parent entity, For example,"
+ + " if you want to have read table privileges of all tables of `catalog1.schema1`,"
+ + " you can use add `read table` privilege for `catalog1.schema1` directly");
+ }
+
+ parent = new SecurableObjectImpl(parent, name);
+ }
+
+ return parent;
+ }
+
+ /**
+ * Create the catalog {@link SecurableObject} with the given catalog name.
+ *
+ * @param catalog The catalog name
+ * @return The created catalog {@link SecurableObject}
+ */
+ public static SecurableObject ofCatalog(String catalog) {
+ return of(catalog);
+ }
+
+ /**
+ * Create the schema {@link SecurableObject} with the given securable catalog object and schema
+ * name.
+ *
+ * @param catalog The securable catalog object
+ * @param schema The schema name
+ * @return The created schema {@link SecurableObject}
+ */
+ public static SecurableObject ofSchema(SecurableObject catalog, String schema) {
+ checkCatalog(catalog);
+
+ return of(catalog.name(), schema);
+ }
+
+ /**
+ * Create the table {@link SecurableObject} with the given securable schema object and table name.
+ *
+ * @param schema The securable schema object
+ * @param table The table name
+ * @return The created table {@link SecurableObject}
+ */
+ public static SecurableObject ofTable(SecurableObject schema, String table) {
+ checkSchema(schema);
+
+ return of(schema.parent().name(), schema.name(), table);
+ }
+
+ /**
+ * Create the topic {@link SecurableObject} with the given securable schema object and topic name.
+ *
+ * @param schema The securable schema object
+ * @param topic The topic name
+ * @return The created topic {@link SecurableObject}
+ */
+ public static SecurableObject ofTopic(SecurableObject schema, String topic) {
+ checkSchema(schema);
+
+ return of(schema.parent().name(), schema.name(), topic);
+ }
+
+ /**
+ * Create the table {@link SecurableObject} with the given securable schema object and fileset
+ * name.
+ *
+ * @param schema The securable schema object
+ * @param fileset The fileset name
+ * @return The created fileset {@link SecurableObject}
+ */
+ public static SecurableObject ofFileset(SecurableObject schema, String fileset) {
+ checkSchema(schema);
+
+ return of(schema.parent().name(), schema.name(), fileset);
+ }
+
+ /**
+ * All catalogs is a special securable object .You can give the securable object the privileges
+ * `LOAD CATALOG`, `CREATE CATALOG`, etc. It means that you can load any catalog and create any
+ * which doesn't exist.
+ *
+ * @return The created {@link SecurableObject}
+ */
+ public static SecurableObject ofAllCatalogs() {
+ return ALL_CATALOGS;
+ }
+
+ private static void checkSchema(SecurableObject schema) {
+ if (schema == null) {
+ throw new IllegalArgumentException("Securable schema object can't be null");
+ }
+ checkCatalog(schema.parent());
+ }
+
+ private static void checkCatalog(SecurableObject catalog) {
+ if (catalog == null) {
+ throw new IllegalArgumentException("Securable catalog object can't be null");
+ }
+
+ if (catalog.parent() != null) {
+ throw new IllegalArgumentException(
+ String.format("The parent of securable catalog object %s must be null", catalog.name()));
+ }
+ }
+
+ private static final SecurableObject ALL_CATALOGS = new SecurableObjectImpl(null, "*");
+
+ private static class SecurableObjectImpl implements SecurableObject {
+
+ private final SecurableObject parent;
+ private final String name;
+
+ SecurableObjectImpl(SecurableObject parent, String name) {
+ this.parent = parent;
+ this.name = name;
+ }
+
+ @Override
+ public SecurableObject parent() {
+ return parent;
+ }
+
+ @Override
+ public String name() {
+ return name;
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(parent, name);
+ }
+
+ @Override
+ public String toString() {
+ if (parent != null) {
+ return parent.toString() + "." + name;
+ } else {
+ return name;
+ }
+ }
+
+ @Override
+ public boolean equals(Object other) {
+ if (!(other instanceof SecurableObject)) {
+ return false;
+ }
+
+ SecurableObject otherSecurableObject = (SecurableObject) other;
+ return Objects.equals(parent, otherSecurableObject.parent())
+ && Objects.equals(name, otherSecurableObject.name());
+ }
+ }
+}
diff --git a/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchRoleException.java b/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchRoleException.java
new file mode 100644
index 0000000000..0ca9decfb6
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchRoleException.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.exceptions;
+
+import com.google.errorprone.annotations.FormatMethod;
+import com.google.errorprone.annotations.FormatString;
+
+/** Exception thrown when a role with specified name is not existed. */
+public class NoSuchRoleException extends NotFoundException {
+ /**
+ * Constructs a new exception with the specified detail message.
+ *
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public NoSuchRoleException(@FormatString String message, Object... args) {
+ super(message, args);
+ }
+
+ /**
+ * Constructs a new exception with the specified detail message and cause.
+ *
+ * @param cause the cause.
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public NoSuchRoleException(Throwable cause, String message, Object... args) {
+ super(cause, message, args);
+ }
+}
diff --git a/api/src/main/java/com/datastrato/gravitino/exceptions/RoleAlreadyExistsException.java b/api/src/main/java/com/datastrato/gravitino/exceptions/RoleAlreadyExistsException.java
new file mode 100644
index 0000000000..d64dc7ff3c
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/exceptions/RoleAlreadyExistsException.java
@@ -0,0 +1,34 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.exceptions;
+
+import com.google.errorprone.annotations.FormatMethod;
+import com.google.errorprone.annotations.FormatString;
+
+/** Exception thrown when a role with specified name already exists. */
+public class RoleAlreadyExistsException extends AlreadyExistsException {
+ /**
+ * Constructs a new exception with the specified detail message.
+ *
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public RoleAlreadyExistsException(@FormatString String message, Object... args) {
+ super(message, args);
+ }
+
+ /**
+ * Constructs a new exception with the specified detail message and cause.
+ *
+ * @param cause the cause.
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public RoleAlreadyExistsException(Throwable cause, @FormatString String message, Object... args) {
+ super(cause, message, args);
+ }
+}
diff --git a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveTableOperations.java b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveTableOperations.java
index 2cdc815555..8f2388753f 100644
--- a/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveTableOperations.java
+++ b/catalogs/catalog-hive/src/main/java/com/datastrato/gravitino/catalog/hive/HiveTableOperations.java
@@ -4,6 +4,7 @@
*/
package com.datastrato.gravitino.catalog.hive;
+import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.connector.TableOperations;
import com.datastrato.gravitino.exceptions.NoSuchPartitionException;
import com.datastrato.gravitino.exceptions.NoSuchTableException;
@@ -132,6 +133,10 @@ public class HiveTableOperations implements TableOperations, SupportsPartitions
@Override
public Partition addPartition(Partition partition) throws PartitionAlreadyExistsException {
+ if (Entity.SECURABLE_ENTITY_RESERVED_NAME.equals(partition.name())) {
+ throw new IllegalArgumentException("Can't create a catalog with with reserved partition `*`");
+ }
+
Preconditions.checkArgument(
partition instanceof IdentityPartition, "Hive only supports identity partition");
IdentityPartition identityPartition = (IdentityPartition) partition;
diff --git a/core/src/main/java/com/datastrato/gravitino/Entity.java b/core/src/main/java/com/datastrato/gravitino/Entity.java
index af2c187200..d8d6ab03cd 100644
--- a/core/src/main/java/com/datastrato/gravitino/Entity.java
+++ b/core/src/main/java/com/datastrato/gravitino/Entity.java
@@ -23,6 +23,9 @@ public interface Entity extends Serializable {
/** The system reserved catalog name. */
String SYSTEM_CATALOG_RESERVED_NAME = "system";
+ /** The securable object reserved entity name. */
+ String SECURABLE_ENTITY_RESERVED_NAME = "*";
+
/** The authorization catalog name in the system metalake. */
String AUTHORIZATION_CATALOG_NAME = "authorization";
@@ -31,6 +34,8 @@ public interface Entity extends Serializable {
/** The group schema name in the system catalog. */
String GROUP_SCHEMA_NAME = "group";
+ /** The role schema name in the system catalog. */
+ String ROLE_SCHEMA_NAME = "role";
/** The admin schema name in the authorization catalog of the system metalake. */
String ADMIN_SCHEMA_NAME = "admin";
@@ -47,6 +52,7 @@ public interface Entity extends Serializable {
TOPIC("to", 6),
USER("us", 7),
GROUP("gr", 8),
+ ROLE("ro", 9),
AUDIT("au", 65534);
@@ -89,6 +95,7 @@ public interface Entity extends Serializable {
case TOPIC:
case USER:
case GROUP:
+ case ROLE:
return ImmutableList.of(METALAKE, CATALOG, SCHEMA);
case COLUMN:
return ImmutableList.of(METALAKE, CATALOG, SCHEMA, TABLE);
diff --git a/core/src/main/java/com/datastrato/gravitino/authorization/AccessControlManager.java b/core/src/main/java/com/datastrato/gravitino/authorization/AccessControlManager.java
index 44882f3ce8..fcfc911d88 100644
--- a/core/src/main/java/com/datastrato/gravitino/authorization/AccessControlManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/authorization/AccessControlManager.java
@@ -8,10 +8,14 @@ import com.datastrato.gravitino.Config;
import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.exceptions.GroupAlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchGroupException;
+import com.datastrato.gravitino.exceptions.NoSuchRoleException;
import com.datastrato.gravitino.exceptions.NoSuchUserException;
+import com.datastrato.gravitino.exceptions.RoleAlreadyExistsException;
import com.datastrato.gravitino.exceptions.UserAlreadyExistsException;
import com.datastrato.gravitino.storage.IdGenerator;
import com.datastrato.gravitino.utils.Executable;
+import java.util.List;
+import java.util.Map;
/**
* AccessControlManager is used for manage users, roles, admin, grant information, this class is an
@@ -25,12 +29,14 @@ public class AccessControlManager {
private final UserGroupManager userGroupManager;
private final AdminManager adminManager;
+ private final RoleManager roleManager;
private final Object adminOperationLock = new Object();
private final Object nonAdminOperationLock = new Object();
public AccessControlManager(EntityStore store, IdGenerator idGenerator, Config config) {
this.userGroupManager = new UserGroupManager(store, idGenerator);
this.adminManager = new AdminManager(store, idGenerator, config);
+ this.roleManager = new RoleManager(store, idGenerator);
}
/**
@@ -50,7 +56,7 @@ public class AccessControlManager {
* Removes a User.
*
* @param metalake The Metalake of the User.
- * @param user THe name of the User.
+ * @param user The name of the User.
* @return `true` if the User was successfully removed, `false` otherwise.
* @throws RuntimeException If removing the User encounters storage issues.
*/
@@ -152,6 +158,54 @@ public class AccessControlManager {
return doWithAdminLock(() -> adminManager.isMetalakeAdmin(user));
}
+ /**
+ * Creates a new Role.
+ *
+ * @param metalake The Metalake of the Role.
+ * @param role The name of the Role.
+ * @param properties The properties of the Role.
+ * @param securableObject The securable object of the Role.
+ * @param privileges The privileges of the Role.
+ * @return The created Role instance.
+ * @throws RoleAlreadyExistsException If a Role with the same identifier already exists.
+ * @throws RuntimeException If creating the Role encounters storage issues.
+ */
+ public Role createRole(
+ String metalake,
+ String role,
+ Map<String, String> properties,
+ SecurableObject securableObject,
+ List<Privilege> privileges)
+ throws RoleAlreadyExistsException {
+ return doWithNonAdminLock(
+ () -> roleManager.createRole(metalake, role, properties, securableObject, privileges));
+ }
+
+ /**
+ * Loads a Role.
+ *
+ * @param metalake The Metalake of the Role.
+ * @param role The name of the Role.
+ * @return The loading Role instance.
+ * @throws NoSuchRoleException If the Role with the given identifier does not exist.
+ * @throws RuntimeException If loading the Role encounters storage issues.
+ */
+ public Role loadRole(String metalake, String role) throws NoSuchRoleException {
+ return doWithNonAdminLock(() -> roleManager.loadRole(metalake, role));
+ }
+
+ /**
+ * Drops a Role.
+ *
+ * @param metalake The Metalake of the Role.
+ * @param role The name of the Role.
+ * @return `true` if the Role was successfully dropped, `false` otherwise.
+ * @throws RuntimeException If dropping the User encounters storage issues.
+ */
+ public boolean dropRole(String metalake, String role) {
+ return doWithNonAdminLock(() -> roleManager.dropRole(metalake, role));
+ }
+
private <R, E extends Exception> R doWithNonAdminLock(Executable<R, E> executable) throws E {
synchronized (nonAdminOperationLock) {
return executable.execute();
diff --git a/core/src/main/java/com/datastrato/gravitino/authorization/AdminManager.java b/core/src/main/java/com/datastrato/gravitino/authorization/AdminManager.java
index 55a3940da5..edbe18cc2c 100644
--- a/core/src/main/java/com/datastrato/gravitino/authorization/AdminManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/authorization/AdminManager.java
@@ -30,7 +30,7 @@ import org.slf4j.LoggerFactory;
* metalake or drops its metalake. The metalake admin will be responsible for managing the access
* control. AdminManager operates underlying store using the lock because kv storage needs the lock.
*/
-public class AdminManager {
+class AdminManager {
private static final Logger LOG = LoggerFactory.getLogger(AdminManager.class);
diff --git a/core/src/main/java/com/datastrato/gravitino/authorization/RoleManager.java b/core/src/main/java/com/datastrato/gravitino/authorization/RoleManager.java
new file mode 100644
index 0000000000..5813744af1
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/authorization/RoleManager.java
@@ -0,0 +1,139 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.authorization;
+
+import com.datastrato.gravitino.Entity;
+import com.datastrato.gravitino.EntityAlreadyExistsException;
+import com.datastrato.gravitino.EntityStore;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.exceptions.NoSuchEntityException;
+import com.datastrato.gravitino.exceptions.NoSuchRoleException;
+import com.datastrato.gravitino.exceptions.RoleAlreadyExistsException;
+import com.datastrato.gravitino.meta.AuditInfo;
+import com.datastrato.gravitino.meta.RoleEntity;
+import com.datastrato.gravitino.storage.IdGenerator;
+import com.datastrato.gravitino.utils.PrincipalUtils;
+import java.io.IOException;
+import java.time.Instant;
+import java.util.List;
+import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * RoleManager is responsible for managing the roles. Role contains the privileges of one privilege
+ * entity. If one Role is created and the privilege entity is an external system, the role will be
+ * created in the underlying entity, too.
+ */
+class RoleManager {
+
+ private static final Logger LOG = LoggerFactory.getLogger(RoleManager.class);
+ private static final String ROLE_DOES_NOT_EXIST_MSG = "Role %s does not exist in th metalake %s";
+ private final EntityStore store;
+ private final IdGenerator idGenerator;
+
+ public RoleManager(EntityStore store, IdGenerator idGenerator) {
+ this.store = store;
+ this.idGenerator = idGenerator;
+ }
+
+ /**
+ * Creates a new Role.
+ *
+ * @param metalake The Metalake of the Role.
+ * @param role The name of the Role.
+ * @param properties The properties of the Role.
+ * @param securableObject The securable object of the Role.
+ * @param privileges The privileges of the Role.
+ * @return The created Role instance.
+ * @throws RoleAlreadyExistsException If a Role with the same identifier already exists.
+ * @throws RuntimeException If creating the Role encounters storage issues.
+ */
+ public Role createRole(
+ String metalake,
+ String role,
+ Map<String, String> properties,
+ SecurableObject securableObject,
+ List<Privilege> privileges)
+ throws RoleAlreadyExistsException {
+ AuthorizationUtils.checkMetalakeExists(store, metalake);
+ RoleEntity roleEntity =
+ RoleEntity.builder()
+ .withId(idGenerator.nextId())
+ .withName(role)
+ .withProperties(properties)
+ .securableObject(securableObject)
+ .withPrivileges(privileges)
+ .withNamespace(
+ Namespace.of(
+ metalake, Entity.SYSTEM_CATALOG_RESERVED_NAME, Entity.ROLE_SCHEMA_NAME))
+ .withAuditInfo(
+ AuditInfo.builder()
+ .withCreator(PrincipalUtils.getCurrentPrincipal().getName())
+ .withCreateTime(Instant.now())
+ .build())
+ .build();
+ try {
+ store.put(roleEntity, false /* overwritten */);
+ return roleEntity;
+ } catch (EntityAlreadyExistsException e) {
+ LOG.warn("Role {} in the metalake {} already exists", role, metalake, e);
+ throw new RoleAlreadyExistsException(
+ "Role %s in the metalake %s already exists", role, metalake);
+ } catch (IOException ioe) {
+ LOG.error(
+ "Creating role {} failed in the metalake {} due to storage issues", role, metalake, ioe);
+ throw new RuntimeException(ioe);
+ }
+ }
+
+ /**
+ * Loads a Role.
+ *
+ * @param metalake The Metalake of the Role.
+ * @param role The name of the Role.
+ * @return The loading Role instance.
+ * @throws NoSuchRoleException If the Role with the given identifier does not exist.
+ * @throws RuntimeException If loading the Role encounters storage issues.
+ */
+ public Role loadRole(String metalake, String role) throws NoSuchRoleException {
+ try {
+ AuthorizationUtils.checkMetalakeExists(store, metalake);
+ return store.get(ofRole(metalake, role), Entity.EntityType.ROLE, RoleEntity.class);
+ } catch (NoSuchEntityException e) {
+ LOG.warn("Role {} does not exist in the metalake {}", role, metalake, e);
+ throw new NoSuchRoleException(ROLE_DOES_NOT_EXIST_MSG, role, metalake);
+ } catch (IOException ioe) {
+ LOG.error("Loading role {} failed due to storage issues", role, ioe);
+ throw new RuntimeException(ioe);
+ }
+ }
+
+ /**
+ * Drops a Role.
+ *
+ * @param metalake The Metalake of the Role.
+ * @param role The name of the Role.
+ * @return `true` if the Role was successfully dropped, `false` otherwise.
+ * @throws RuntimeException If dropping the User encounters storage issues.
+ */
+ public boolean dropRole(String metalake, String role) {
+ try {
+ AuthorizationUtils.checkMetalakeExists(store, metalake);
+ return store.delete(ofRole(metalake, role), Entity.EntityType.ROLE);
+ } catch (IOException ioe) {
+ LOG.error(
+ "Deleting role {} in the metalake {} failed due to storage issues", role, metalake, ioe);
+ throw new RuntimeException(ioe);
+ }
+ }
+
+ private NameIdentifier ofRole(String metalake, String role) {
+ return NameIdentifier.of(
+ metalake, Entity.SYSTEM_CATALOG_RESERVED_NAME, Entity.ROLE_SCHEMA_NAME, role);
+ }
+}
diff --git a/core/src/main/java/com/datastrato/gravitino/authorization/UserGroupManager.java b/core/src/main/java/com/datastrato/gravitino/authorization/UserGroupManager.java
index 967c55a969..411c640a84 100644
--- a/core/src/main/java/com/datastrato/gravitino/authorization/UserGroupManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/authorization/UserGroupManager.java
@@ -32,7 +32,7 @@ import org.slf4j.LoggerFactory;
* metalake and the user or group. Metalake is like a concept of the organization. `AddUser` or
* `AddGroup` means that a role or user enters an organization.
*/
-public class UserGroupManager {
+class UserGroupManager {
private static final Logger LOG = LoggerFactory.getLogger(UserGroupManager.class);
private static final String USER_DOES_NOT_EXIST_MSG = "User %s does not exist in th metalake %s";
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
index f374ce6e20..9c4419206e 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
@@ -293,6 +293,10 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
throw new IllegalArgumentException("Can't create a catalog with with reserved name `system`");
}
+ if (Entity.SECURABLE_ENTITY_RESERVED_NAME.equals(ident.name())) {
+ throw new IllegalArgumentException("Can't create a catalog with with reserved name `*`");
+ }
+
// load catalog-related configuration from catalog-specific configuration file
Map<String, String> catalogSpecificConfig = loadCatalogSpecificConfig(properties, provider);
Map<String, String> mergedConfig = mergeConf(properties, catalogSpecificConfig);
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/FilesetOperationDispatcher.java b/core/src/main/java/com/datastrato/gravitino/catalog/FilesetOperationDispatcher.java
index 4d9166c810..892b444e1f 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/FilesetOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/FilesetOperationDispatcher.java
@@ -6,6 +6,7 @@ package com.datastrato.gravitino.catalog;
import static com.datastrato.gravitino.catalog.PropertiesMetadataHelpers.validatePropertyForCreate;
+import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
@@ -100,6 +101,10 @@ public class FilesetOperationDispatcher extends OperationDispatcher implements F
Map<String, String> properties)
throws NoSuchSchemaException, FilesetAlreadyExistsException {
NameIdentifier catalogIdent = getCatalogIdentifier(ident);
+ if (Entity.SECURABLE_ENTITY_RESERVED_NAME.equals(ident.name())) {
+ throw new IllegalArgumentException("Can't create a fileset with with reserved name `*`");
+ }
+
doWithCatalog(
catalogIdent,
c ->
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/SchemaOperationDispatcher.java b/core/src/main/java/com/datastrato/gravitino/catalog/SchemaOperationDispatcher.java
index ed750ce976..20a952f0ae 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/SchemaOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/SchemaOperationDispatcher.java
@@ -7,6 +7,7 @@ package com.datastrato.gravitino.catalog;
import static com.datastrato.gravitino.Entity.EntityType.SCHEMA;
import static com.datastrato.gravitino.catalog.PropertiesMetadataHelpers.validatePropertyForCreate;
+import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
@@ -74,6 +75,10 @@ public class SchemaOperationDispatcher extends OperationDispatcher implements Su
@Override
public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> properties)
throws NoSuchCatalogException, SchemaAlreadyExistsException {
+ if (Entity.SECURABLE_ENTITY_RESERVED_NAME.equals(ident.name())) {
+ throw new IllegalArgumentException("Can't create a schema with with reserved name `*`");
+ }
+
NameIdentifier catalogIdent = getCatalogIdentifier(ident);
doWithCatalog(
catalogIdent,
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/TableOperationDispatcher.java b/core/src/main/java/com/datastrato/gravitino/catalog/TableOperationDispatcher.java
index 1d58fb0d9a..4939ec801c 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/TableOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/TableOperationDispatcher.java
@@ -8,6 +8,7 @@ import static com.datastrato.gravitino.Entity.EntityType.TABLE;
import static com.datastrato.gravitino.catalog.PropertiesMetadataHelpers.validatePropertyForCreate;
import static com.datastrato.gravitino.rel.expressions.transforms.Transforms.EMPTY_TRANSFORM;
+import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
@@ -133,6 +134,10 @@ public class TableOperationDispatcher extends OperationDispatcher implements Tab
SortOrder[] sortOrders,
Index[] indexes)
throws NoSuchSchemaException, TableAlreadyExistsException {
+ if (Entity.SECURABLE_ENTITY_RESERVED_NAME.equals(ident.name())) {
+ throw new IllegalArgumentException("Can't create a table with with reserved name `*`");
+ }
+
NameIdentifier catalogIdent = getCatalogIdentifier(ident);
doWithCatalog(
catalogIdent,
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/TopicOperationDispatcher.java b/core/src/main/java/com/datastrato/gravitino/catalog/TopicOperationDispatcher.java
index 172c11b86c..26c5962719 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/TopicOperationDispatcher.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/TopicOperationDispatcher.java
@@ -8,6 +8,7 @@ import static com.datastrato.gravitino.Entity.EntityType.TOPIC;
import static com.datastrato.gravitino.StringIdentifier.fromProperties;
import static com.datastrato.gravitino.catalog.PropertiesMetadataHelpers.validatePropertyForCreate;
+import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
@@ -118,6 +119,10 @@ public class TopicOperationDispatcher extends OperationDispatcher implements Top
public Topic createTopic(
NameIdentifier ident, String comment, DataLayout dataLayout, Map<String, String> properties)
throws NoSuchSchemaException, TopicAlreadyExistsException {
+ if (Entity.SECURABLE_ENTITY_RESERVED_NAME.equals(ident.name())) {
+ throw new IllegalArgumentException("Can't create a topic with with reserved name `*`");
+ }
+
NameIdentifier catalogIdent = getCatalogIdentifier(ident);
doWithCatalog(
catalogIdent,
diff --git a/core/src/main/java/com/datastrato/gravitino/meta/RoleEntity.java b/core/src/main/java/com/datastrato/gravitino/meta/RoleEntity.java
new file mode 100644
index 0000000000..5e4948fa0b
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/meta/RoleEntity.java
@@ -0,0 +1,274 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.meta;
+
+import com.datastrato.gravitino.Auditable;
+import com.datastrato.gravitino.Entity;
+import com.datastrato.gravitino.Field;
+import com.datastrato.gravitino.HasIdentifier;
+import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.authorization.Privilege;
+import com.datastrato.gravitino.authorization.Role;
+import com.datastrato.gravitino.authorization.SecurableObject;
+import com.google.common.collect.Maps;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class RoleEntity implements Role, Entity, Auditable, HasIdentifier {
+
+ public static final Field ID =
+ Field.required("id", Long.class, " The unique id of the role entity.");
+
+ public static final Field NAME =
+ Field.required("name", String.class, "The name of the role entity.");
+
+ public static final Field PROPERTIES =
+ Field.optional("properties", Map.class, "The properties of the role entity.");
+
+ public static final Field AUDIT_INFO =
+ Field.required("audit_info", AuditInfo.class, "The audit details of the role entity.");
+
+ public static final Field SECURABLE_OBJECT =
+ Field.required(
+ "securable_object", SecurableObject.class, "The securable object of the role entity.");
+
+ public static final Field PRIVILEGES =
+ Field.required("privileges", List.class, "The privileges of the role entity.");
+
+ private Long id;
+ private String name;
+ private Map<String, String> properties;
+ private AuditInfo auditInfo;
+ private List<Privilege> privileges;
+ private Namespace namespace;
+ private SecurableObject securableObject;
+
+ /**
+ * The name of the role.
+ *
+ * @return The name of the role.
+ */
+ @Override
+ public String name() {
+ return name;
+ }
+
+ /** @return The audit information of the entity. */
+ @Override
+ public AuditInfo auditInfo() {
+ return auditInfo;
+ }
+
+ /**
+ * The properties of the role. Note, this method will return null if the properties are not set.
+ *
+ * @return The properties of the role.
+ */
+ @Override
+ public Map<String, String> properties() {
+ return properties;
+ }
+
+ /**
+ * The securable object represents a special kind of entity with a unique identifier. All
+ * securable objects are organized by tree structure. For example: If the securable object is a
+ * table, the identifier may be `catalog1.schema1.table1`.
+ *
+ * @return The securable object of the role.
+ */
+ @Override
+ public SecurableObject securableObject() {
+ // The securable object is a special kind of entities. Some entity types aren't the securable
+ // object, such as
+ // User, Role, etc.
+ // The securable object identifier must be unique.
+ // Gravitino assumes that the identifiers of the entities may be the same if they have different
+ // types.
+ // So one type of them can't be the securable object at least if there are the two same
+ // identifier
+ // entities .
+ return securableObject;
+ }
+
+ /**
+ * The privileges of the role. All privileges belong to one securable object. For example: If the
+ * securable object is a table, the privileges could be `READ TABLE`, `WRITE TABLE`, etc. If a
+ * schema has the privilege of `LOAD TABLE`. It means the role can all tables of the schema.
+ *
+ * @return The privileges of the role.
+ */
+ @Override
+ public List<Privilege> privileges() {
+ return privileges;
+ }
+
+ /**
+ * Retrieves the fields and their associated values of the entity.
+ *
+ * @return A map of Field to Object representing the entity's schema with values.
+ */
+ @Override
+ public Map<Field, Object> fields() {
+ Map<Field, Object> fields = Maps.newHashMap();
+ fields.put(ID, id);
+ fields.put(NAME, name);
+ fields.put(AUDIT_INFO, auditInfo);
+ fields.put(PROPERTIES, properties);
+ fields.put(SECURABLE_OBJECT, securableObject);
+ fields.put(PRIVILEGES, privileges);
+
+ return Collections.unmodifiableMap(fields);
+ }
+
+ /**
+ * Retrieves the type of the entity.
+ *
+ * @return The type of the entity as defined by {@link EntityType}.
+ */
+ @Override
+ public EntityType type() {
+ return EntityType.ROLE;
+ }
+
+ /**
+ * Get the unique id of the entity.
+ *
+ * @return The unique id of the entity.
+ */
+ @Override
+ public Long id() {
+ return id;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof RoleEntity)) return false;
+
+ RoleEntity that = (RoleEntity) o;
+ return Objects.equals(id, that.id)
+ && Objects.equals(name, that.name)
+ && Objects.equals(auditInfo, that.auditInfo)
+ && Objects.equals(properties, that.properties)
+ && Objects.equals(securableObject, that.securableObject)
+ && Objects.equals(privileges, that.privileges);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id, name, properties, auditInfo, securableObject, privileges);
+ }
+
+ /**
+ * Get the namespace of the entity.
+ *
+ * @return The namespace of the entity.
+ */
+ @Override
+ public Namespace namespace() {
+ return namespace;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private final RoleEntity roleEntity;
+
+ private Builder() {
+ roleEntity = new RoleEntity();
+ }
+
+ /**
+ * Sets the unique id of the role entity.
+ *
+ * @param id The unique id of the role entity.
+ * @return The builder instance.
+ */
+ public Builder withId(Long id) {
+ roleEntity.id = id;
+ return this;
+ }
+
+ /**
+ * Sets the name of the role entity.
+ *
+ * @param name The name of the role entity.
+ * @return The builder instance.
+ */
+ public Builder withName(String name) {
+ roleEntity.name = name;
+ return this;
+ }
+
+ /**
+ * Sets the properties of the role entity.
+ *
+ * @param properties The properties of the role entity.
+ * @return The builder instance.
+ */
+ public Builder withProperties(Map<String, String> properties) {
+ roleEntity.properties = properties;
+ return this;
+ }
+
+ /**
+ * Sets the audit details of the role entity.
+ *
+ * @param auditInfo The audit details of the role entity.
+ * @return The builder instance.
+ */
+ public Builder withAuditInfo(AuditInfo auditInfo) {
+ roleEntity.auditInfo = auditInfo;
+ return this;
+ }
+
+ /**
+ * Sets the securable object of the role entity.
+ *
+ * @param securableObject The securable object of the role entity.
+ * @return The builder instance.
+ */
+ public Builder securableObject(SecurableObject securableObject) {
+ roleEntity.securableObject = securableObject;
+ return this;
+ }
+
+ /**
+ * Sets the privileges of the role entity.
+ *
+ * @param privileges The privileges of the role entity.
+ * @return The builder instance.
+ */
+ public Builder withPrivileges(List<Privilege> privileges) {
+ roleEntity.privileges = privileges;
+ return this;
+ }
+
+ /**
+ * Sets the namespace of the role entity.
+ *
+ * @param namespace The namespace of the role entity.
+ * @return The builder instance.
+ */
+ public Builder withNamespace(Namespace namespace) {
+ roleEntity.namespace = namespace;
+ return this;
+ }
+
+ /**
+ * Builds the role entity.
+ *
+ * @return The built role entity.
+ */
+ public RoleEntity build() {
+ roleEntity.validate();
+ return roleEntity;
+ }
+ }
+}
diff --git a/core/src/main/java/com/datastrato/gravitino/proto/ProtoEntitySerDe.java b/core/src/main/java/com/datastrato/gravitino/proto/ProtoEntitySerDe.java
index 7da49a85da..b826b4cedb 100644
--- a/core/src/main/java/com/datastrato/gravitino/proto/ProtoEntitySerDe.java
+++ b/core/src/main/java/com/datastrato/gravitino/proto/ProtoEntitySerDe.java
@@ -46,6 +46,9 @@ public class ProtoEntitySerDe implements EntitySerDe {
.put(
"com.datastrato.gravitino.meta.GroupEntity",
"com.datastrato.gravitino.proto.GroupEntitySerDe")
+ .put(
+ "com.datastrato.gravitino.meta.RoleEntity",
+ "com.datastrato.gravitino.proto.RoleEntitySerDe")
.build();
private static final Map<String, String> ENTITY_TO_PROTO =
@@ -67,7 +70,9 @@ public class ProtoEntitySerDe implements EntitySerDe {
"com.datastrato.gravitino.meta.UserEntity",
"com.datastrato.gravitino.proto.User",
"com.datastrato.gravitino.meta.GroupEntity",
- "com.datastrato.gravitino.proto.Group");
+ "com.datastrato.gravitino.proto.Group",
+ "com.datastrato.gravitino.meta.RoleEntity",
+ "com.datastrato.gravitino.proto.Role");
private final Map<Class<? extends Entity>, ProtoSerDe<? extends Entity, ? extends Message>>
entityToSerDe;
diff --git a/core/src/main/java/com/datastrato/gravitino/proto/RoleEntitySerDe.java b/core/src/main/java/com/datastrato/gravitino/proto/RoleEntitySerDe.java
new file mode 100644
index 0000000000..ae65c3a117
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/proto/RoleEntitySerDe.java
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.proto;
+
+import com.datastrato.gravitino.authorization.Privileges;
+import com.datastrato.gravitino.authorization.SecurableObject;
+import com.datastrato.gravitino.authorization.SecurableObjects;
+import com.datastrato.gravitino.meta.RoleEntity;
+import com.google.common.base.Splitter;
+import com.google.common.collect.Iterables;
+import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
+
+public class RoleEntitySerDe implements ProtoSerDe<RoleEntity, Role> {
+
+ private static final Splitter DOT = Splitter.on('.');
+
+ /**
+ * Serializes the provided entity into its corresponding Protocol Buffer message representation.
+ *
+ * @param roleEntity The entity to be serialized.
+ * @return The Protocol Buffer message representing the serialized entity.
+ */
+ @Override
+ public Role serialize(RoleEntity roleEntity) {
+ Role.Builder builder =
+ Role.newBuilder()
+ .setId(roleEntity.id())
+ .setName(roleEntity.name())
+ .setAuditInfo(new AuditInfoSerDe().serialize(roleEntity.auditInfo()))
+ .addAllPrivileges(
+ roleEntity.privileges().stream()
+ .map(privilege -> privilege.name().toString())
+ .collect(Collectors.toList()))
+ .setSecurableObject(roleEntity.securableObject().toString());
+
+ if (roleEntity.properties() != null && !roleEntity.properties().isEmpty()) {
+ builder.putAllProperties(roleEntity.properties());
+ }
+
+ return builder.build();
+ }
+
+ /**
+ * Deserializes the provided Protocol Buffer message into its corresponding entity representation.
+ *
+ * @param role The Protocol Buffer message to be deserialized.
+ * @return The entity representing the deserialized Protocol Buffer message.
+ */
+ @Override
+ public RoleEntity deserialize(Role role) {
+ RoleEntity.Builder builder =
+ RoleEntity.builder()
+ .withId(role.getId())
+ .withName(role.getName())
+ .withPrivileges(
+ role.getPrivilegesList().stream()
+ .map(Privileges::fromString)
+ .collect(Collectors.toList()))
+ .securableObject(parseSecurableObject(role.getSecurableObject()))
+ .withAuditInfo(new AuditInfoSerDe().deserialize(role.getAuditInfo()));
+
+ if (!role.getPropertiesMap().isEmpty()) {
+ builder.withProperties(role.getPropertiesMap());
+ }
+
+ return builder.build();
+ }
+
+ private static SecurableObject parseSecurableObject(String securableObjectIdentifier) {
+ if ("*".equals(securableObjectIdentifier)) {
+ return SecurableObjects.ofAllCatalogs();
+ }
+
+ if (StringUtils.isBlank(securableObjectIdentifier)) {
+ throw new IllegalArgumentException("securable object identifier can't be blank");
+ }
+
+ Iterable<String> parts = DOT.split(securableObjectIdentifier);
+ return SecurableObjects.of(Iterables.toArray(parts, String.class));
+ }
+}
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/BinaryEntityKeyEncoder.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/BinaryEntityKeyEncoder.java
index 773b376948..49fea351a6 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/BinaryEntityKeyEncoder.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/BinaryEntityKeyEncoder.java
@@ -8,6 +8,7 @@ import static com.datastrato.gravitino.Entity.EntityType.CATALOG;
import static com.datastrato.gravitino.Entity.EntityType.FILESET;
import static com.datastrato.gravitino.Entity.EntityType.GROUP;
import static com.datastrato.gravitino.Entity.EntityType.METALAKE;
+import static com.datastrato.gravitino.Entity.EntityType.ROLE;
import static com.datastrato.gravitino.Entity.EntityType.SCHEMA;
import static com.datastrato.gravitino.Entity.EntityType.TABLE;
import static com.datastrato.gravitino.Entity.EntityType.TOPIC;
@@ -82,6 +83,8 @@ public class BinaryEntityKeyEncoder implements EntityKeyEncoder<byte[]> {
new String[] {USER.getShortName() + "/", "/", "/", "/"},
GROUP,
new String[] {GROUP.getShortName() + "/", "/", "/", "/"},
+ ROLE,
+ new String[] {ROLE.getShortName() + "/", "/", "/", "/"},
TOPIC,
new String[] {TOPIC.getShortName() + "/", "/", "/", "/"});
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
index 6ede995414..6222c9d60c 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvEntityStore.java
@@ -8,6 +8,7 @@ package com.datastrato.gravitino.storage.kv;
import static com.datastrato.gravitino.Configs.ENTITY_KV_STORE;
import static com.datastrato.gravitino.Entity.EntityType.GROUP;
import static com.datastrato.gravitino.Entity.EntityType.METALAKE;
+import static com.datastrato.gravitino.Entity.EntityType.ROLE;
import static com.datastrato.gravitino.Entity.EntityType.USER;
import static com.datastrato.gravitino.storage.kv.BinaryEntityEncoderUtil.generateKeyForMapping;
import static com.datastrato.gravitino.storage.kv.BinaryEntityEncoderUtil.getSubEntitiesPrefix;
@@ -228,7 +229,8 @@ public class KvEntityStore implements EntityStore {
}
byte[] encode = entityKeyEncoder.encode(ident, type, true);
- String[] entityShortNames = new String[] {USER.getShortName(), GROUP.getShortName()};
+ String[] entityShortNames =
+ new String[] {USER.getShortName(), GROUP.getShortName(), ROLE.getShortName()};
for (String name : entityShortNames) {
byte[] prefix = replacePrefixTypeInfo(encode, name);
transactionalKvBackend.deleteRange(
diff --git a/core/src/test/java/com/datastrato/gravitino/authorization/TestAccessControlManager.java b/core/src/test/java/com/datastrato/gravitino/authorization/TestAccessControlManager.java
index 5c708c9909..c53ada3a35 100644
--- a/core/src/test/java/com/datastrato/gravitino/authorization/TestAccessControlManager.java
+++ b/core/src/test/java/com/datastrato/gravitino/authorization/TestAccessControlManager.java
@@ -8,19 +8,24 @@ import static com.datastrato.gravitino.Configs.SERVICE_ADMINS;
import com.datastrato.gravitino.Config;
import com.datastrato.gravitino.EntityStore;
+import com.datastrato.gravitino.StringIdentifier;
import com.datastrato.gravitino.exceptions.GroupAlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchGroupException;
import com.datastrato.gravitino.exceptions.NoSuchMetalakeException;
+import com.datastrato.gravitino.exceptions.NoSuchRoleException;
import com.datastrato.gravitino.exceptions.NoSuchUserException;
+import com.datastrato.gravitino.exceptions.RoleAlreadyExistsException;
import com.datastrato.gravitino.exceptions.UserAlreadyExistsException;
import com.datastrato.gravitino.meta.AuditInfo;
import com.datastrato.gravitino.meta.BaseMetalake;
import com.datastrato.gravitino.meta.SchemaVersion;
import com.datastrato.gravitino.storage.RandomIdGenerator;
import com.datastrato.gravitino.storage.memory.TestMemoryEntityStore;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.time.Instant;
+import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
@@ -207,4 +212,69 @@ public class TestAccessControlManager {
Assertions.assertTrue(accessControlManager.isServiceAdmin("admin2"));
Assertions.assertFalse(accessControlManager.isServiceAdmin("admin3"));
}
+
+ @Test
+ public void testCreateRole() {
+ Map<String, String> props = ImmutableMap.of("key1", "value1");
+
+ Role role =
+ accessControlManager.createRole(
+ "metalake", "create", props, SecurableObjects.ofAllCatalogs(), Lists.newArrayList());
+ Assertions.assertEquals("create", role.name());
+ testProperties(props, role.properties());
+
+ // Test with RoleAlreadyExistsException
+ Assertions.assertThrows(
+ RoleAlreadyExistsException.class,
+ () ->
+ accessControlManager.createRole(
+ "metalake",
+ "create",
+ props,
+ SecurableObjects.ofAllCatalogs(),
+ Lists.newArrayList()));
+ }
+
+ @Test
+ public void testLoadRole() {
+ Map<String, String> props = ImmutableMap.of("k1", "v1");
+
+ accessControlManager.createRole(
+ "metalake", "loadRole", props, SecurableObjects.ofAllCatalogs(), Lists.newArrayList());
+ Role role = accessControlManager.loadRole("metalake", "loadRole");
+ Assertions.assertEquals("loadRole", role.name());
+ testProperties(props, role.properties());
+
+ // Test load non-existed group
+ Throwable exception =
+ Assertions.assertThrows(
+ NoSuchRoleException.class,
+ () -> accessControlManager.loadRole("metalake", "not-exist"));
+ Assertions.assertTrue(exception.getMessage().contains("Role not-exist does not exist"));
+ }
+
+ @Test
+ public void testDropRole() {
+ Map<String, String> props = ImmutableMap.of("k1", "v1");
+
+ accessControlManager.createRole(
+ "metalake", "testDrop", props, SecurableObjects.ofAllCatalogs(), Lists.newArrayList());
+
+ // Test drop role
+ boolean dropped = accessControlManager.dropRole("metalake", "testDrop");
+ Assertions.assertTrue(dropped);
+
+ // Test drop non-existed role
+ boolean dropped1 = accessControlManager.dropRole("metalake", "no-exist");
+ Assertions.assertFalse(dropped1);
+ }
+
+ private void testProperties(Map<String, String> expectedProps, Map<String, String> testProps) {
+ expectedProps.forEach(
+ (k, v) -> {
+ Assertions.assertEquals(v, testProps.get(k));
+ });
+
+ Assertions.assertFalse(testProps.containsKey(StringIdentifier.ID_KEY));
+ }
}
diff --git a/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java b/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java
index 6158574a57..5d1f689a6a 100644
--- a/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java
+++ b/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java
@@ -6,6 +6,8 @@ package com.datastrato.gravitino.meta;
import com.datastrato.gravitino.Catalog;
import com.datastrato.gravitino.Field;
+import com.datastrato.gravitino.authorization.Privileges;
+import com.datastrato.gravitino.authorization.SecurableObjects;
import com.datastrato.gravitino.file.Fileset;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
@@ -56,6 +58,10 @@ public class TestEntity {
private final Long groupId = 1L;
private final String groupName = "testGroup";
+ // Role test data
+ private final Long roleId = 1L;
+ private final String roleName = "testRole";
+
@Test
public void testMetalake() {
BaseMetalake metalake =
@@ -252,4 +258,37 @@ public class TestEntity {
Assertions.assertNull(groupWithoutFields.roles());
}
+
+ @Test
+ public void testRole() {
+ RoleEntity role =
+ RoleEntity.builder()
+ .withId(1L)
+ .withName(roleName)
+ .withAuditInfo(auditInfo)
+ .securableObject(SecurableObjects.of(catalogName))
+ .withPrivileges(Lists.newArrayList(Privileges.LoadCatalog.get()))
+ .withProperties(map)
+ .build();
+
+ Map<Field, Object> fields = role.fields();
+ Assertions.assertEquals(roleId, fields.get(RoleEntity.ID));
+ Assertions.assertEquals(roleName, fields.get(RoleEntity.NAME));
+ Assertions.assertEquals(auditInfo, fields.get(RoleEntity.AUDIT_INFO));
+ Assertions.assertEquals(map, fields.get(RoleEntity.PROPERTIES));
+ Assertions.assertEquals(
+ Lists.newArrayList(Privileges.LoadCatalog.get()), fields.get(RoleEntity.PRIVILEGES));
+ Assertions.assertEquals(
+ SecurableObjects.of(catalogName), fields.get(RoleEntity.SECURABLE_OBJECT));
+
+ RoleEntity roleWithoutFields =
+ RoleEntity.builder()
+ .withId(1L)
+ .withName(roleName)
+ .withAuditInfo(auditInfo)
+ .securableObject(SecurableObjects.of(catalogName))
+ .withPrivileges(Lists.newArrayList(Privileges.LoadCatalog.get()))
+ .build();
+ Assertions.assertNull(roleWithoutFields.properties());
+ }
}
diff --git a/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java b/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java
index 83e0f41ca4..9b63d32167 100644
--- a/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java
+++ b/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java
@@ -6,7 +6,10 @@ package com.datastrato.gravitino.proto;
import com.datastrato.gravitino.EntitySerDe;
import com.datastrato.gravitino.EntitySerDeFactory;
+import com.datastrato.gravitino.authorization.Privileges;
+import com.datastrato.gravitino.authorization.SecurableObjects;
import com.datastrato.gravitino.meta.GroupEntity;
+import com.datastrato.gravitino.meta.RoleEntity;
import com.datastrato.gravitino.meta.SchemaVersion;
import com.datastrato.gravitino.meta.UserEntity;
import com.google.common.collect.ImmutableMap;
@@ -320,5 +323,33 @@ public class TestEntityProtoSerDe {
groupFromBytes = protoEntitySerDe.deserialize(groupBytes, GroupEntity.class);
Assertions.assertEquals(groupWithoutFields, groupFromBytes);
Assertions.assertNull(groupWithoutFields.roles());
+
+ // Test RoleEntity
+ Long roleId = 1L;
+ String roleName = "testRole";
+ RoleEntity roleEntity =
+ RoleEntity.builder()
+ .withId(roleId)
+ .withName(roleName)
+ .withAuditInfo(auditInfo)
+ .securableObject(SecurableObjects.of(catalogName))
+ .withPrivileges(Lists.newArrayList(Privileges.LoadCatalog.get()))
+ .withProperties(props)
+ .build();
+ byte[] roleBytes = protoEntitySerDe.serialize(roleEntity);
+ RoleEntity roleFromBytes = protoEntitySerDe.deserialize(roleBytes, RoleEntity.class);
+ Assertions.assertEquals(roleEntity, roleFromBytes);
+
+ RoleEntity roleWithoutFields =
+ RoleEntity.builder()
+ .withId(1L)
+ .withName(roleName)
+ .withAuditInfo(auditInfo)
+ .securableObject(SecurableObjects.of(catalogName))
+ .withPrivileges(Lists.newArrayList(Privileges.LoadCatalog.get()))
+ .build();
+ roleBytes = protoEntitySerDe.serialize(roleWithoutFields);
+ roleFromBytes = protoEntitySerDe.deserialize(roleBytes, RoleEntity.class);
+ Assertions.assertEquals(roleWithoutFields, roleFromBytes);
}
}
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
index 914013f176..b7272c413a 100644
--- a/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
+++ b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
@@ -27,6 +27,8 @@ import com.datastrato.gravitino.EntityStore;
import com.datastrato.gravitino.EntityStoreFactory;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
+import com.datastrato.gravitino.authorization.Privileges;
+import com.datastrato.gravitino.authorization.SecurableObjects;
import com.datastrato.gravitino.exceptions.AlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchEntityException;
import com.datastrato.gravitino.exceptions.NonEmptyEntityException;
@@ -36,6 +38,7 @@ import com.datastrato.gravitino.meta.BaseMetalake;
import com.datastrato.gravitino.meta.CatalogEntity;
import com.datastrato.gravitino.meta.FilesetEntity;
import com.datastrato.gravitino.meta.GroupEntity;
+import com.datastrato.gravitino.meta.RoleEntity;
import com.datastrato.gravitino.meta.SchemaEntity;
import com.datastrato.gravitino.meta.SchemaVersion;
import com.datastrato.gravitino.meta.TableEntity;
@@ -55,6 +58,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
@@ -421,7 +425,7 @@ public class TestEntityStorage {
@ParameterizedTest
@MethodSource("storageProvider")
public void testAuthorizationEntityDelete(String type) throws IOException {
- // User and Group entity only support kv store.
+ // User, Group and Role entity only support kv store.
Assumptions.assumeTrue(Configs.DEFAULT_ENTITY_STORE.equals(type));
Config config = Mockito.mock(Config.class);
init(type, config);
@@ -442,15 +446,23 @@ public class TestEntityStorage {
store.put(oneGroup);
GroupEntity anotherGroup = createGroup("metalake", "anotherGroup", auditInfo);
store.put(anotherGroup);
+ RoleEntity oneRole = createRole("metalake", "oneRole", auditInfo);
+ store.put(oneRole);
+ RoleEntity anotherRole = createRole("metalake", "anotherRole", auditInfo);
+ store.put(anotherRole);
Assertions.assertTrue(store.exists(oneUser.nameIdentifier(), Entity.EntityType.USER));
Assertions.assertTrue(store.exists(anotherUser.nameIdentifier(), Entity.EntityType.USER));
Assertions.assertTrue(store.exists(oneGroup.nameIdentifier(), Entity.EntityType.GROUP));
Assertions.assertTrue(store.exists(anotherGroup.nameIdentifier(), Entity.EntityType.GROUP));
+ Assertions.assertTrue(store.exists(oneRole.nameIdentifier(), Entity.EntityType.ROLE));
+ Assertions.assertTrue(store.exists(anotherRole.nameIdentifier(), Entity.EntityType.ROLE));
store.delete(metalake.nameIdentifier(), Entity.EntityType.METALAKE);
Assertions.assertFalse(store.exists(oneUser.nameIdentifier(), Entity.EntityType.USER));
Assertions.assertFalse(store.exists(anotherUser.nameIdentifier(), Entity.EntityType.USER));
Assertions.assertFalse(store.exists(oneGroup.nameIdentifier(), Entity.EntityType.GROUP));
Assertions.assertFalse(store.exists(anotherGroup.nameIdentifier(), Entity.EntityType.GROUP));
+ Assertions.assertFalse(store.exists(oneRole.nameIdentifier(), Entity.EntityType.ROLE));
+ Assertions.assertFalse(store.exists(anotherRole.nameIdentifier(), Entity.EntityType.ROLE));
}
}
@@ -1162,6 +1174,20 @@ public class TestEntityStorage {
.build();
}
+ private static RoleEntity createRole(String metalake, String name, AuditInfo auditInfo) {
+ return RoleEntity.builder()
+ .withId(1L)
+ .withNamespace(
+ Namespace.of(
+ metalake, CatalogEntity.SYSTEM_CATALOG_RESERVED_NAME, Entity.ROLE_SCHEMA_NAME))
+ .withName(name)
+ .withAuditInfo(auditInfo)
+ .securableObject(SecurableObjects.of("catalog"))
+ .withPrivileges(Lists.newArrayList(Privileges.LoadCatalog.get()))
+ .withProperties(Collections.emptyMap())
+ .build();
+ }
+
private void validateDeleteTopicCascade(EntityStore store, TopicEntity topic1)
throws IOException {
// Delete the topic 'metalake.catalog.schema1.topic1'
diff --git a/meta/src/main/proto/gravitino_meta.proto b/meta/src/main/proto/gravitino_meta.proto
index c040023826..94cccc9185 100644
--- a/meta/src/main/proto/gravitino_meta.proto
+++ b/meta/src/main/proto/gravitino_meta.proto
@@ -114,4 +114,13 @@ message Group {
string name = 2;
repeated string roles = 3;
AuditInfo audit_info = 4;
-}
\ No newline at end of file
+}
+
+message Role {
+ uint64 id = 1;
+ string name = 2;
+ repeated string privileges = 3;
+ string securable_object = 4;
+ map<string, string> properties = 5;
+ AuditInfo audit_info = 6;
+}