Skip to content

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

[#2236] feat(core): Add the support of Group entity (#2735)

qqqttt1235b4eb40b75f0
diff --git a/api/src/main/java/com/datastrato/gravitino/authorization/Group.java b/api/src/main/java/com/datastrato/gravitino/authorization/Group.java
new file mode 100644
index 0000000000..ff3e5ce7f4
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/authorization/Group.java
@@ -0,0 +1,28 @@
+/*
+ * 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;
+
+/** The interface of a Group. The Group is the entity which contains users. */
+@Evolving
+public interface Group extends Auditable {
+
+ /**
+ * The name of the group.
+ *
+ * @return The name of the group.
+ */
+ String name();
+
+ /**
+ * The roles of the group.
+ *
+ * @return The roles of the group.
+ */
+ List<String> roles();
+}
diff --git a/api/src/main/java/com/datastrato/gravitino/exceptions/GroupAlreadyExistsException.java b/api/src/main/java/com/datastrato/gravitino/exceptions/GroupAlreadyExistsException.java
new file mode 100644
index 0000000000..edeb10b257
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/exceptions/GroupAlreadyExistsException.java
@@ -0,0 +1,36 @@
+/*
+ * 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;
+
+/** An exception thrown when a group already exists. */
+public class GroupAlreadyExistsException 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 GroupAlreadyExistsException(@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 GroupAlreadyExistsException(Throwable cause, String message, Object... args) {
+ super(cause, message, args);
+ }
+}
diff --git a/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchGroupException.java b/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchGroupException.java
new file mode 100644
index 0000000000..a908923300
--- /dev/null
+++ b/api/src/main/java/com/datastrato/gravitino/exceptions/NoSuchGroupException.java
@@ -0,0 +1,35 @@
+/*
+ * 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 group with specified name is not existed. */
+public class NoSuchGroupException 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 NoSuchGroupException(@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 NoSuchGroupException(Throwable cause, String message, Object... args) {
+ super(cause, message, args);
+ }
+}
diff --git a/core/src/main/java/com/datastrato/gravitino/Entity.java b/core/src/main/java/com/datastrato/gravitino/Entity.java
index 95492996ee..754c364122 100644
--- a/core/src/main/java/com/datastrato/gravitino/Entity.java
+++ b/core/src/main/java/com/datastrato/gravitino/Entity.java
@@ -22,6 +22,7 @@ public interface Entity extends Serializable {
FILESET("fi", 5),
TOPIC("to", 6),
USER("us", 7),
+ GROUP("gr", 8),
AUDIT("au", 65534);
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 0581df4ad7..749f8adcb2 100644
--- a/core/src/main/java/com/datastrato/gravitino/authorization/AccessControlManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/authorization/AccessControlManager.java
@@ -5,6 +5,8 @@
package com.datastrato.gravitino.authorization;
import com.datastrato.gravitino.EntityStore;
+import com.datastrato.gravitino.exceptions.GroupAlreadyExistsException;
+import com.datastrato.gravitino.exceptions.NoSuchGroupException;
import com.datastrato.gravitino.exceptions.NoSuchUserException;
import com.datastrato.gravitino.exceptions.UserAlreadyExistsException;
import com.datastrato.gravitino.storage.IdGenerator;
@@ -15,10 +17,10 @@ import com.datastrato.gravitino.storage.IdGenerator;
*/
public class AccessControlManager {
- private final UserManager userManager;
+ private final UserGroupManager userGroupManager;
public AccessControlManager(EntityStore store, IdGenerator idGenerator) {
- this.userManager = new UserManager(store, idGenerator);
+ this.userGroupManager = new UserGroupManager(store, idGenerator);
}
/**
@@ -31,7 +33,7 @@ public class AccessControlManager {
* @throws RuntimeException If adding the User encounters storage issues.
*/
public User addUser(String metalake, String name) throws UserAlreadyExistsException {
- return userManager.addUser(metalake, name);
+ return userGroupManager.addUser(metalake, name);
}
/**
@@ -43,7 +45,7 @@ public class AccessControlManager {
* @throws RuntimeException If removing the User encounters storage issues.
*/
public boolean removeUser(String metalake, String user) {
- return userManager.removeUser(metalake, user);
+ return userGroupManager.removeUser(metalake, user);
}
/**
@@ -56,6 +58,44 @@ public class AccessControlManager {
* @throws RuntimeException If getting the User encounters storage issues.
*/
public User getUser(String metalake, String user) throws NoSuchUserException {
- return userManager.getUser(metalake, user);
+ return userGroupManager.getUser(metalake, user);
+ }
+
+ /**
+ * Adds a new Group.
+ *
+ * @param metalake The Metalake of the Group.
+ * @param group The name of the Group.
+ * @return The Added Group instance.
+ * @throws GroupAlreadyExistsException If a Group with the same identifier already exists.
+ * @throws RuntimeException If adding the Group encounters storage issues.
+ */
+ public Group addGroup(String metalake, String group) throws GroupAlreadyExistsException {
+ return userGroupManager.addGroup(metalake, group);
+ }
+
+ /**
+ * Removes a Group.
+ *
+ * @param metalake The Metalake of the Group.
+ * @param group THe name of the Group.
+ * @return `true` if the Group was successfully removed, `false` otherwise.
+ * @throws RuntimeException If removing the Group encounters storage issues.
+ */
+ public boolean removeGroup(String metalake, String group) {
+ return userGroupManager.removeGroup(metalake, group);
+ }
+
+ /**
+ * Gets a Group.
+ *
+ * @param metalake The Metalake of the Group.
+ * @param group THe name of the Group.
+ * @return The getting Group instance.
+ * @throws NoSuchGroupException If the Group with the given identifier does not exist.
+ * @throws RuntimeException If getting the Group encounters storage issues.
+ */
+ public Group getGroup(String metalake, String group) throws NoSuchGroupException {
+ return userGroupManager.getGroup(metalake, group);
}
}
diff --git a/core/src/main/java/com/datastrato/gravitino/authorization/UserManager.java b/core/src/main/java/com/datastrato/gravitino/authorization/UserGroupManager.java
similarity index 52%
rename from core/src/main/java/com/datastrato/gravitino/authorization/UserManager.java
rename to core/src/main/java/com/datastrato/gravitino/authorization/UserGroupManager.java
index a0fa164b33..4233b4ec99 100644
--- a/core/src/main/java/com/datastrato/gravitino/authorization/UserManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/authorization/UserGroupManager.java
@@ -9,34 +9,42 @@ 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.GroupAlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchEntityException;
+import com.datastrato.gravitino.exceptions.NoSuchGroupException;
import com.datastrato.gravitino.exceptions.NoSuchUserException;
import com.datastrato.gravitino.exceptions.UserAlreadyExistsException;
import com.datastrato.gravitino.meta.AuditInfo;
import com.datastrato.gravitino.meta.CatalogEntity;
+import com.datastrato.gravitino.meta.GroupEntity;
import com.datastrato.gravitino.meta.UserEntity;
import com.datastrato.gravitino.storage.IdGenerator;
import com.datastrato.gravitino.utils.PrincipalUtils;
import com.google.common.collect.Lists;
import java.io.IOException;
import java.time.Instant;
+import java.util.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
- * UserManager is used for add, remove and get users from one metalake. UserManager doesn't manage
- * users, just sets up the relationship between the metalake and the user. Metalake is like a
- * concept of the organization. `AddUser` means that a user enter an organization.
+ * UserGroupManager is used for add, remove and get users or roles from one metalake.
+ * UserGroupManager doesn't manage users or groups, just sets up the relationship between the
+ * 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 UserManager {
+public class UserGroupManager {
- private static final Logger LOG = LoggerFactory.getLogger(UserManager.class);
+ 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";
+ private static final String GROUP_DOES_NOT_EXIST_MSG =
+ "Group %s does not exist in th metalake %s";
+
private final EntityStore store;
private final IdGenerator idGenerator;
- public UserManager(EntityStore store, IdGenerator idGenerator) {
+ public UserGroupManager(EntityStore store, IdGenerator idGenerator) {
this.store = store;
this.idGenerator = idGenerator;
}
@@ -121,8 +129,95 @@ public class UserManager {
}
}
+ /**
+ * Adds a new Group.
+ *
+ * @param metalake The Metalake of the Group.
+ * @param group The name of the Group.
+ * @return The Added Group instance.
+ * @throws GroupAlreadyExistsException If a Group with the same identifier already exists.
+ * @throws RuntimeException If adding the Group encounters storage issues.
+ */
+ public Group addGroup(String metalake, String group) throws GroupAlreadyExistsException {
+ GroupEntity groupEntity =
+ GroupEntity.builder()
+ .withId(idGenerator.nextId())
+ .withName(group)
+ .withNamespace(
+ Namespace.of(
+ metalake,
+ CatalogEntity.SYSTEM_CATALOG_RESERVED_NAME,
+ GroupEntity.GROUP_SCHEMA_NAME))
+ .withRoles(Collections.emptyList())
+ .withAuditInfo(
+ AuditInfo.builder()
+ .withCreator(PrincipalUtils.getCurrentPrincipal().getName())
+ .withCreateTime(Instant.now())
+ .build())
+ .build();
+ try {
+ store.put(groupEntity, false /* overwritten */);
+ return groupEntity;
+ } catch (EntityAlreadyExistsException e) {
+ LOG.warn("Group {} in the metalake {} already exists", group, metalake, e);
+ throw new GroupAlreadyExistsException(
+ "Group %s in the metalake %s already exists", group, metalake);
+ } catch (IOException ioe) {
+ LOG.error(
+ "Adding group {} failed in the metalake {} due to storage issues", group, metalake, ioe);
+ throw new RuntimeException(ioe);
+ }
+ }
+
+ /**
+ * Removes a Group.
+ *
+ * @param metalake The Metalake of the Group.
+ * @param group THe name of the Group.
+ * @return `true` if the Group was successfully removed, `false` otherwise.
+ * @throws RuntimeException If removing the Group encounters storage issues.
+ */
+ public boolean removeGroup(String metalake, String group) {
+ try {
+ return store.delete(ofGroup(metalake, group), Entity.EntityType.GROUP);
+ } catch (IOException ioe) {
+ LOG.error(
+ "Removing group {} in the metalake {} failed due to storage issues",
+ group,
+ metalake,
+ ioe);
+ throw new RuntimeException(ioe);
+ }
+ }
+
+ /**
+ * Gets a Group.
+ *
+ * @param metalake The Metalake of the Group.
+ * @param group THe name of the Group.
+ * @return The getting Group instance.
+ * @throws NoSuchGroupException If the Group with the given identifier does not exist.
+ * @throws RuntimeException If getting the Group encounters storage issues.
+ */
+ public Group getGroup(String metalake, String group) {
+ try {
+ return store.get(ofGroup(metalake, group), Entity.EntityType.GROUP, GroupEntity.class);
+ } catch (NoSuchEntityException e) {
+ LOG.warn("Group {} does not exist in the metalake {}", group, metalake, e);
+ throw new NoSuchGroupException(GROUP_DOES_NOT_EXIST_MSG, group, metalake);
+ } catch (IOException ioe) {
+ LOG.error("Getting group {} failed due to storage issues", group, ioe);
+ throw new RuntimeException(ioe);
+ }
+ }
+
private NameIdentifier ofUser(String metalake, String user) {
return NameIdentifier.of(
metalake, CatalogEntity.SYSTEM_CATALOG_RESERVED_NAME, UserEntity.USER_SCHEMA_NAME, user);
}
+
+ private NameIdentifier ofGroup(String metalake, String group) {
+ return NameIdentifier.of(
+ metalake, CatalogEntity.SYSTEM_CATALOG_RESERVED_NAME, GroupEntity.GROUP_SCHEMA_NAME, group);
+ }
}
diff --git a/core/src/main/java/com/datastrato/gravitino/meta/GroupEntity.java b/core/src/main/java/com/datastrato/gravitino/meta/GroupEntity.java
new file mode 100644
index 0000000000..7d198b7947
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/meta/GroupEntity.java
@@ -0,0 +1,210 @@
+/*
+ * 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.Group;
+import com.google.common.collect.Maps;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+
+public class GroupEntity implements Group, Entity, Auditable, HasIdentifier {
+
+ public static final String GROUP_SCHEMA_NAME = "group";
+
+ public static final Field ID =
+ Field.required("id", Long.class, " The unique id of the group entity.");
+
+ public static final Field NAME =
+ Field.required("name", String.class, "The name of the group entity.");
+
+ public static final Field ROLES =
+ Field.optional("roles", List.class, "The roles of the group entity.");
+
+ public static final Field AUDIT_INFO =
+ Field.required("audit_info", AuditInfo.class, "The audit details of the group entity.");
+
+ private Long id;
+ private String name;
+ private AuditInfo auditInfo;
+ private List<String> roles;
+ private Namespace namespace;
+
+ private GroupEntity() {}
+
+ /**
+ * Returns a map of fields and their corresponding values for this group entity.
+ *
+ * @return An unmodifiable map of the fields and 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(ROLES, roles);
+
+ return Collections.unmodifiableMap(fields);
+ }
+
+ /**
+ * Returns the name of the group.
+ *
+ * @return The name of the group.
+ */
+ @Override
+ public String name() {
+ return name;
+ }
+
+ /**
+ * Returns the unique id of the group.
+ *
+ * @return The unique id of the group.
+ */
+ @Override
+ public Long id() {
+ return id;
+ }
+
+ /**
+ * Returns the type of the entity.
+ *
+ * @return The type of the entity.
+ */
+ @Override
+ public EntityType type() {
+ return EntityType.GROUP;
+ }
+
+ /**
+ * Returns the namespace of the group.
+ *
+ * @return The namespace of the group.
+ */
+ @Override
+ public Namespace namespace() {
+ return namespace;
+ }
+
+ /**
+ * Returns the audit details of the group.
+ *
+ * @return The audit details of the group.
+ */
+ public AuditInfo auditInfo() {
+ return auditInfo;
+ }
+
+ /**
+ * Returns the roles of the group entity.
+ *
+ * @return The roles of the group entity.
+ */
+ public List<String> roles() {
+ return roles;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof GroupEntity)) return false;
+
+ GroupEntity that = (GroupEntity) o;
+ return Objects.equals(id, that.id)
+ && Objects.equals(name, that.name)
+ && Objects.equals(auditInfo, that.auditInfo)
+ && Objects.equals(roles, that.roles);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id, name, auditInfo, roles);
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private final GroupEntity groupEntity;
+
+ private Builder() {
+ this.groupEntity = new GroupEntity();
+ }
+
+ /**
+ * Sets the unique id of the group entity.
+ *
+ * @param id The unique id of the group entity.
+ * @return The builder instance.
+ */
+ public Builder withId(Long id) {
+ groupEntity.id = id;
+ return this;
+ }
+
+ /**
+ * Sets the name of the group entity.
+ *
+ * @param name The name of the group entity.
+ * @return The builder instance.
+ */
+ public Builder withName(String name) {
+ groupEntity.name = name;
+ return this;
+ }
+
+ /**
+ * Sets the audit details of the group entity.
+ *
+ * @param auditInfo The audit details of the group entity.
+ * @return The builder instance.
+ */
+ public Builder withAuditInfo(AuditInfo auditInfo) {
+ groupEntity.auditInfo = auditInfo;
+ return this;
+ }
+
+ /**
+ * Sets the roles of the group entity.
+ *
+ * @param roles The roles of the group entity.
+ * @return The builder instance.
+ */
+ public Builder withRoles(List<String> roles) {
+ groupEntity.roles = roles;
+ return this;
+ }
+
+ /**
+ * Sets the namespace of the group entity.
+ *
+ * @param namespace The namespace of the group entity.
+ * @return The builder instance.
+ */
+ public Builder withNamespace(Namespace namespace) {
+ groupEntity.namespace = namespace;
+ return this;
+ }
+
+ /**
+ * Builds the group entity.
+ *
+ * @return The built group entity.
+ */
+ public GroupEntity build() {
+ groupEntity.validate();
+ return groupEntity;
+ }
+ }
+}
diff --git a/core/src/main/java/com/datastrato/gravitino/proto/GroupEntitySerDe.java b/core/src/main/java/com/datastrato/gravitino/proto/GroupEntitySerDe.java
new file mode 100644
index 0000000000..083b69029d
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/proto/GroupEntitySerDe.java
@@ -0,0 +1,45 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.proto;
+
+import com.datastrato.gravitino.meta.GroupEntity;
+import java.util.Collection;
+
+public class GroupEntitySerDe implements ProtoSerDe<GroupEntity, Group> {
+
+ @Override
+ public Group serialize(GroupEntity groupEntity) {
+ Group.Builder builder =
+ Group.newBuilder()
+ .setId(groupEntity.id())
+ .setName(groupEntity.name())
+ .setAuditInfo(new AuditInfoSerDe().serialize(groupEntity.auditInfo()));
+
+ if (isCollectionNotEmpty(groupEntity.roles())) {
+ builder.addAllRoles(groupEntity.roles());
+ }
+
+ return builder.build();
+ }
+
+ @Override
+ public GroupEntity deserialize(Group group) {
+ GroupEntity.Builder builder =
+ GroupEntity.builder()
+ .withId(group.getId())
+ .withName(group.getName())
+ .withAuditInfo(new AuditInfoSerDe().deserialize(group.getAuditInfo()));
+
+ if (group.getRolesCount() > 0) {
+ builder.withRoles(group.getRolesList());
+ }
+
+ return builder.build();
+ }
+
+ private boolean isCollectionNotEmpty(Collection collection) {
+ return collection != null && !collection.isEmpty();
+ }
+}
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 a346a5cd9d..7da49a85da 100644
--- a/core/src/main/java/com/datastrato/gravitino/proto/ProtoEntitySerDe.java
+++ b/core/src/main/java/com/datastrato/gravitino/proto/ProtoEntitySerDe.java
@@ -43,6 +43,9 @@ public class ProtoEntitySerDe implements EntitySerDe {
.put(
"com.datastrato.gravitino.meta.UserEntity",
"com.datastrato.gravitino.proto.UserEntitySerDe")
+ .put(
+ "com.datastrato.gravitino.meta.GroupEntity",
+ "com.datastrato.gravitino.proto.GroupEntitySerDe")
.build();
private static final Map<String, String> ENTITY_TO_PROTO =
@@ -62,7 +65,9 @@ public class ProtoEntitySerDe implements EntitySerDe {
"com.datastrato.gravitino.meta.TopicEntity",
"com.datastrato.gravitino.proto.Topic",
"com.datastrato.gravitino.meta.UserEntity",
- "com.datastrato.gravitino.proto.User");
+ "com.datastrato.gravitino.proto.User",
+ "com.datastrato.gravitino.meta.GroupEntity",
+ "com.datastrato.gravitino.proto.Group");
private final Map<Class<? extends Entity>, ProtoSerDe<? extends Entity, ? extends Message>>
entityToSerDe;
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 e6096af372..9882928d9b 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
@@ -6,6 +6,7 @@ package com.datastrato.gravitino.storage.kv;
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.SCHEMA;
import static com.datastrato.gravitino.Entity.EntityType.TABLE;
@@ -78,7 +79,9 @@ public class BinaryEntityKeyEncoder implements EntityKeyEncoder<byte[]> {
FILESET,
new String[] {FILESET.getShortName() + "/", "/", "/", "/"},
USER,
- new String[] {USER.getShortName() + "/", "/", "/", "/"});
+ new String[] {USER.getShortName() + "/", "/", "/", "/"},
+ GROUP,
+ new String[] {GROUP.getShortName() + "/", "/", "/", "/"});
@VisibleForTesting final NameMappingService nameMappingService;
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 e79d679269..c241211bde 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.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.SCHEMA;
import static com.datastrato.gravitino.Entity.EntityType.TABLE;
@@ -334,18 +335,23 @@ public class KvEntityStore implements EntityStore {
return prefixes;
}
- void deleteUserEntitiesIfNecessary(NameIdentifier ident, EntityType type) throws IOException {
+ void deleteAuthorizationEntitiesIfNecessary(NameIdentifier ident, EntityType type)
+ throws IOException {
if (type != METALAKE) {
return;
}
byte[] encode = entityKeyEncoder.encode(ident, type, true);
- byte[] prefix = replacePrefixTypeInfo(encode, USER.getShortName());
- transactionalKvBackend.deleteRange(
- new KvRange.KvRangeBuilder()
- .start(prefix)
- .startInclusive(true)
- .end(Bytes.increment(Bytes.wrap(prefix)).get())
- .build());
+
+ String[] entityShortNames = new String[] {USER.getShortName(), GROUP.getShortName()};
+ for (String name : entityShortNames) {
+ byte[] prefix = replacePrefixTypeInfo(encode, name);
+ transactionalKvBackend.deleteRange(
+ new KvRange.KvRangeBuilder()
+ .start(prefix)
+ .startInclusive(true)
+ .end(Bytes.increment(Bytes.wrap(prefix)).get())
+ .build());
+ }
}
private byte[] replacePrefixTypeInfo(byte[] encode, String subTypePrefix) {
@@ -406,7 +412,7 @@ public class KvEntityStore implements EntityStore {
.build());
}
- deleteUserEntitiesIfNecessary(ident, entityType);
+ deleteAuthorizationEntitiesIfNecessary(ident, entityType);
return transactionalKvBackend.delete(dataKey);
});
}
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 eaa0081fcf..2a1e023d6c 100644
--- a/core/src/test/java/com/datastrato/gravitino/authorization/TestAccessControlManager.java
+++ b/core/src/test/java/com/datastrato/gravitino/authorization/TestAccessControlManager.java
@@ -6,6 +6,8 @@ package com.datastrato.gravitino.authorization;
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.NoSuchUserException;
import com.datastrato.gravitino.exceptions.UserAlreadyExistsException;
import com.datastrato.gravitino.meta.AuditInfo;
@@ -99,11 +101,59 @@ public class TestAccessControlManager {
accessControlManager.addUser("metalake", "testRemove");
// Test to remove user
- boolean dropped = accessControlManager.removeUser("metalake", "testRemove");
- Assertions.assertTrue(dropped);
+ boolean removed = accessControlManager.removeUser("metalake", "testRemove");
+ Assertions.assertTrue(removed);
// Test to remove non-existed user
- boolean dropped1 = accessControlManager.removeUser("metalake", "no-exist");
- Assertions.assertFalse(dropped1);
+ boolean removed1 = accessControlManager.removeUser("metalake", "no-exist");
+ Assertions.assertFalse(removed1);
+ }
+
+ @Test
+ public void testAddGroup() {
+
+ Group group = accessControlManager.addGroup("metalake", "testAdd");
+ Assertions.assertEquals("testAdd", group.name());
+ Assertions.assertTrue(group.roles().isEmpty());
+
+ group = accessControlManager.addGroup("metalake", "testAddWithOptionalField");
+
+ Assertions.assertEquals("testAddWithOptionalField", group.name());
+ Assertions.assertTrue(group.roles().isEmpty());
+
+ // Test with GroupAlreadyExistsException
+ Assertions.assertThrows(
+ GroupAlreadyExistsException.class,
+ () -> accessControlManager.addGroup("metalake", "testAdd"));
+ }
+
+ @Test
+ public void testGetGroup() {
+
+ accessControlManager.addGroup("metalake", "testGet");
+
+ Group group = accessControlManager.getGroup("metalake", "testGet");
+ Assertions.assertEquals("testGet", group.name());
+
+ // Test to get non-existed group
+ Throwable exception =
+ Assertions.assertThrows(
+ NoSuchGroupException.class,
+ () -> accessControlManager.getGroup("metalake", "not-exist"));
+ Assertions.assertTrue(exception.getMessage().contains("Group not-exist does not exist"));
+ }
+
+ @Test
+ public void testRemoveGroup() {
+
+ accessControlManager.addGroup("metalake", "testRemove");
+
+ // Test to remove group
+ boolean removed = accessControlManager.removeGroup("metalake", "testRemove");
+ Assertions.assertTrue(removed);
+
+ // Test to remove non-existed group
+ boolean removed1 = accessControlManager.removeUser("metalake", "no-exist");
+ Assertions.assertFalse(removed1);
}
}
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 94c6b7fd96..6158574a57 100644
--- a/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java
+++ b/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java
@@ -52,6 +52,10 @@ public class TestEntity {
private final Long userId = 1L;
private final String userName = "testUser";
+ // Group test data
+ private final Long groupId = 1L;
+ private final String groupName = "testGroup";
+
@Test
public void testMetalake() {
BaseMetalake metalake =
@@ -227,4 +231,25 @@ public class TestEntity {
Assertions.assertNull(testUserEntityWithoutFields.roles());
}
+
+ @Test
+ public void testGroup() {
+ GroupEntity group =
+ GroupEntity.builder()
+ .withId(groupId)
+ .withName(groupName)
+ .withAuditInfo(auditInfo)
+ .withRoles(Lists.newArrayList("role"))
+ .build();
+ Map<Field, Object> fields = group.fields();
+ Assertions.assertEquals(groupId, fields.get(GroupEntity.ID));
+ Assertions.assertEquals(groupName, fields.get(GroupEntity.NAME));
+ Assertions.assertEquals(auditInfo, fields.get(GroupEntity.AUDIT_INFO));
+ Assertions.assertEquals(Lists.newArrayList("role"), fields.get(GroupEntity.ROLES));
+
+ GroupEntity groupWithoutFields =
+ GroupEntity.builder().withId(userId).withName(userName).withAuditInfo(auditInfo).build();
+
+ Assertions.assertNull(groupWithoutFields.roles());
+ }
}
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 8c8a50d454..83e0f41ca4 100644
--- a/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java
+++ b/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java
@@ -6,6 +6,7 @@ package com.datastrato.gravitino.proto;
import com.datastrato.gravitino.EntitySerDe;
import com.datastrato.gravitino.EntitySerDeFactory;
+import com.datastrato.gravitino.meta.GroupEntity;
import com.datastrato.gravitino.meta.SchemaVersion;
import com.datastrato.gravitino.meta.UserEntity;
import com.google.common.collect.ImmutableMap;
@@ -296,5 +297,28 @@ public class TestEntityProtoSerDe {
userBytes = protoEntitySerDe.serialize(userEntityWithoutFields);
userEntityFromBytes = protoEntitySerDe.deserialize(userBytes, UserEntity.class);
Assertions.assertEquals(userEntityWithoutFields, userEntityFromBytes);
+ Assertions.assertNull(userEntityWithoutFields.roles());
+
+ // Test GroupEntity
+ Long groupId = 1L;
+ String groupName = "group";
+
+ GroupEntity group =
+ GroupEntity.builder()
+ .withId(groupId)
+ .withName(groupName)
+ .withAuditInfo(auditInfo)
+ .withRoles(Lists.newArrayList("role"))
+ .build();
+ byte[] groupBytes = protoEntitySerDe.serialize(group);
+ GroupEntity groupFromBytes = protoEntitySerDe.deserialize(groupBytes, GroupEntity.class);
+ Assertions.assertEquals(group, groupFromBytes);
+
+ GroupEntity groupWithoutFields =
+ GroupEntity.builder().withId(groupId).withName(groupName).withAuditInfo(auditInfo).build();
+ groupBytes = protoEntitySerDe.serialize(groupWithoutFields);
+ groupFromBytes = protoEntitySerDe.deserialize(groupBytes, GroupEntity.class);
+ Assertions.assertEquals(groupWithoutFields, groupFromBytes);
+ Assertions.assertNull(groupWithoutFields.roles());
}
}
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 6666cd0453..e311fc4a0e 100644
--- a/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
+++ b/core/src/test/java/com/datastrato/gravitino/storage/TestEntityStorage.java
@@ -35,6 +35,7 @@ import com.datastrato.gravitino.meta.AuditInfo;
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.SchemaEntity;
import com.datastrato.gravitino.meta.SchemaVersion;
import com.datastrato.gravitino.meta.TableEntity;
@@ -382,8 +383,8 @@ public class TestEntityStorage {
@ParameterizedTest
@MethodSource("storageProvider")
- public void testUserEntityDelete(String type) throws IOException {
- // User entity only supports kv store.
+ public void testAuthorizationEntityDelete(String type) throws IOException {
+ // User and Group entity only support kv store.
Assumptions.assumeTrue(Configs.DEFAULT_ENTITY_STORE.equals(type));
Config config = Mockito.mock(Config.class);
init(type, config);
@@ -400,11 +401,19 @@ public class TestEntityStorage {
store.put(oneUser);
UserEntity anotherUser = createUser("metalake", "anotherUser", auditInfo);
store.put(anotherUser);
+ GroupEntity oneGroup = createGroup("metalake", "oneGroup", auditInfo);
+ store.put(oneGroup);
+ GroupEntity anotherGroup = createGroup("metalake", "anotherGroup", auditInfo);
+ store.put(anotherGroup);
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));
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));
}
}
@@ -872,6 +881,20 @@ public class TestEntityStorage {
.build();
}
+ private static GroupEntity createGroup(String metalake, String name, AuditInfo auditInfo) {
+ return GroupEntity.builder()
+ .withId(1L)
+ .withNamespace(
+ Namespace.of(
+ metalake,
+ CatalogEntity.SYSTEM_CATALOG_RESERVED_NAME,
+ GroupEntity.GROUP_SCHEMA_NAME))
+ .withName(name)
+ .withAuditInfo(auditInfo)
+ .withRoles(Lists.newArrayList())
+ .build();
+ }
+
private void validateDeleteFilesetCascade(EntityStore store, FilesetEntity fileset1)
throws IOException {
// Delete the fileset 'metalake.catalog.schema1.fileset1'
diff --git a/meta/src/main/proto/gravitino_meta.proto b/meta/src/main/proto/gravitino_meta.proto
index 5468cfab25..5ced47063e 100644
--- a/meta/src/main/proto/gravitino_meta.proto
+++ b/meta/src/main/proto/gravitino_meta.proto
@@ -108,3 +108,10 @@ message User {
repeated string roles = 3;
AuditInfo audit_info = 4;
}
+
+message Group {
+ uint64 id = 1;
+ string name = 2;
+ repeated string roles = 3;
+ AuditInfo audit_info = 4;
+}
\ No newline at end of file