-
Notifications
You must be signed in to change notification settings - Fork 384
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#3132] improvement(core): Use a more elegant validation of User, Gro…
…up and Role NameIdentifier (#3143) ### What changes were proposed in this pull request? add `checkUser`, `checkGroup`, `checkRole` in `NameIdentifier` ### Why are the changes needed? Fix: #3132 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? ut --------- Co-authored-by: yangliwei <[email protected]>
- Loading branch information
Showing
5 changed files
with
194 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
core/src/test/java/com/datastrato/gravitino/authorization/TestAuthorizationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright 2024 Datastrato Pvt Ltd. | ||
* This software is licensed under the Apache License version 2. | ||
*/ | ||
package com.datastrato.gravitino.authorization; | ||
|
||
import com.datastrato.gravitino.NameIdentifier; | ||
import com.datastrato.gravitino.Namespace; | ||
import com.datastrato.gravitino.exceptions.IllegalNameIdentifierException; | ||
import com.datastrato.gravitino.exceptions.IllegalNamespaceException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TestAuthorizationUtils { | ||
|
||
String metalake = "metalake"; | ||
|
||
@Test | ||
void testCreateNameIdentifier() { | ||
NameIdentifier user = AuthorizationUtils.ofUser(metalake, "user"); | ||
NameIdentifier group = AuthorizationUtils.ofGroup(metalake, "group"); | ||
NameIdentifier role = AuthorizationUtils.ofRole(metalake, "role"); | ||
|
||
Assertions.assertEquals(AuthorizationUtils.ofUserNamespace(metalake), user.namespace()); | ||
Assertions.assertEquals("user", user.name()); | ||
Assertions.assertEquals(AuthorizationUtils.ofGroupNamespace(metalake), group.namespace()); | ||
Assertions.assertEquals("group", group.name()); | ||
Assertions.assertEquals(AuthorizationUtils.ofRoleNamespace(metalake), role.namespace()); | ||
Assertions.assertEquals("role", role.name()); | ||
} | ||
|
||
@Test | ||
void testCreateNameIdentifierWithInvalidArgs() { | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.ofUser(metalake, null)); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.ofUser(metalake, "")); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.ofGroup(metalake, null)); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.ofGroup(metalake, "")); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.ofRole(metalake, null)); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.ofRole(metalake, "")); | ||
} | ||
|
||
@Test | ||
void testCreateNamespace() { | ||
Namespace namespace = AuthorizationUtils.ofUserNamespace(metalake); | ||
Assertions.assertEquals(3, namespace.length()); | ||
Assertions.assertEquals(metalake, namespace.level(0)); | ||
Assertions.assertEquals("system", namespace.level(1)); | ||
Assertions.assertEquals("user", namespace.level(2)); | ||
|
||
namespace = AuthorizationUtils.ofGroupNamespace(metalake); | ||
Assertions.assertEquals(3, namespace.length()); | ||
Assertions.assertEquals(metalake, namespace.level(0)); | ||
Assertions.assertEquals("system", namespace.level(1)); | ||
Assertions.assertEquals("group", namespace.level(2)); | ||
|
||
namespace = AuthorizationUtils.ofRoleNamespace(metalake); | ||
Assertions.assertEquals(3, namespace.length()); | ||
Assertions.assertEquals(metalake, namespace.level(0)); | ||
Assertions.assertEquals("system", namespace.level(1)); | ||
Assertions.assertEquals("role", namespace.level(2)); | ||
} | ||
|
||
@Test | ||
void testCreateNamespaceWithInvalidArgs() { | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.ofUserNamespace(null)); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.ofUserNamespace("")); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.ofGroupNamespace(null)); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.ofGroupNamespace("")); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.ofRoleNamespace(null)); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.ofRoleNamespace("")); | ||
} | ||
|
||
@Test | ||
void testCheckNameIdentifier() { | ||
NameIdentifier user = AuthorizationUtils.ofUser(metalake, "user"); | ||
NameIdentifier group = AuthorizationUtils.ofGroup(metalake, "group"); | ||
NameIdentifier role = AuthorizationUtils.ofRole(metalake, "role"); | ||
|
||
Assertions.assertDoesNotThrow(() -> AuthorizationUtils.checkUser(user)); | ||
Assertions.assertDoesNotThrow(() -> AuthorizationUtils.checkGroup(group)); | ||
Assertions.assertDoesNotThrow(() -> AuthorizationUtils.checkRole(role)); | ||
|
||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.checkUser(null)); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.checkGroup(null)); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, () -> AuthorizationUtils.checkRole(null)); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, | ||
() -> AuthorizationUtils.checkUser(NameIdentifier.of(""))); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, | ||
() -> AuthorizationUtils.checkGroup(NameIdentifier.of(""))); | ||
Assertions.assertThrows( | ||
IllegalNameIdentifierException.class, | ||
() -> AuthorizationUtils.checkRole(NameIdentifier.of(""))); | ||
} | ||
|
||
@Test | ||
void testCheckNamespace() { | ||
Namespace userNamespace = AuthorizationUtils.ofUserNamespace(metalake); | ||
Namespace groupNamespace = AuthorizationUtils.ofGroupNamespace(metalake); | ||
Namespace roleNamespace = AuthorizationUtils.ofRoleNamespace(metalake); | ||
|
||
Assertions.assertDoesNotThrow(() -> AuthorizationUtils.checkUserNamespace(userNamespace)); | ||
Assertions.assertDoesNotThrow(() -> AuthorizationUtils.checkGroupNamespace(groupNamespace)); | ||
Assertions.assertDoesNotThrow(() -> AuthorizationUtils.checkRoleNamespace(roleNamespace)); | ||
|
||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.checkUserNamespace(null)); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.checkGroupNamespace(null)); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, () -> AuthorizationUtils.checkRoleNamespace(null)); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, | ||
() -> AuthorizationUtils.checkUserNamespace(Namespace.of("a", "b"))); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, | ||
() -> AuthorizationUtils.checkGroupNamespace(Namespace.of("a"))); | ||
Assertions.assertThrows( | ||
IllegalNamespaceException.class, | ||
() -> AuthorizationUtils.checkRoleNamespace(Namespace.of("a", "b", "c", "d"))); | ||
} | ||
} |