Skip to content

Commit b953226

Browse files
authored
[#5665] refactor(auth): Underlying datasource authorization privilege abstraction (#5674)
### What changes were proposed in this pull request? 1. abstract AuthorizationMetadataObject interface 2. abstract AuthorizationPrivilege interface 3. abstract AuthorizationSecurableObject interface 4. abstract AuthorizationPrivilegesMappingProvider interface ### Why are the changes needed? Fix: #5665 ### Does this PR introduce _any_ user-facing change? N/A ### How was this patch tested? CI Passed.
1 parent 93b623b commit b953226

16 files changed

+575
-572
lines changed

authorizations/authorization-ranger/src/main/java/org/apache/gravitino/authorization/ranger/RangerAuthorizationHadoopSQLPlugin.java

+58-82
Large diffs are not rendered by default.

authorizations/authorization-ranger/src/main/java/org/apache/gravitino/authorization/ranger/RangerAuthorizationPlugin.java

+130-99
Large diffs are not rendered by default.

authorizations/authorization-ranger/src/main/java/org/apache/gravitino/authorization/ranger/RangerHelper.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
import java.util.Set;
2727
import java.util.stream.Collectors;
2828
import org.apache.commons.lang.StringUtils;
29+
import org.apache.gravitino.authorization.AuthorizationMetadataObject;
30+
import org.apache.gravitino.authorization.AuthorizationPrivilege;
31+
import org.apache.gravitino.authorization.AuthorizationSecurableObject;
2932
import org.apache.gravitino.authorization.Owner;
3033
import org.apache.gravitino.authorization.Privilege;
3134
import org.apache.gravitino.exceptions.AuthorizationPluginException;
@@ -49,7 +52,7 @@ public class RangerHelper {
4952
/** The `*` gives access to all resources */
5053
public static final String RESOURCE_ALL = "*";
5154
/** The owner privileges, the owner can do anything on the metadata object */
52-
private final Set<RangerPrivilege> ownerPrivileges;
55+
private final Set<AuthorizationPrivilege> ownerPrivileges;
5356
/** The policy search keys */
5457
protected final List<String> policyResourceDefines;
5558

@@ -69,7 +72,7 @@ public RangerHelper(
6972
RangerClient rangerClient,
7073
String rangerAdminName,
7174
String rangerServiceName,
72-
Set<RangerPrivilege> ownerPrivileges,
75+
Set<AuthorizationPrivilege> ownerPrivileges,
7376
List<String> resourceDefines) {
7477
this.rangerClient = rangerClient;
7578
this.rangerAdminName = rangerAdminName;
@@ -102,7 +105,8 @@ void checkPolicyItemAccess(RangerPolicy.RangerPolicyItem policyItem)
102105
* We cannot clean the policy items because one Ranger policy maybe contains multiple Gravitino
103106
* securable objects. <br>
104107
*/
105-
void addPolicyItem(RangerPolicy policy, String roleName, RangerSecurableObject securableObject) {
108+
void addPolicyItem(
109+
RangerPolicy policy, String roleName, AuthorizationSecurableObject securableObject) {
106110
// Add the policy items by the securable object's privileges
107111
securableObject
108112
.privileges()
@@ -191,20 +195,20 @@ public List<RangerPolicy> wildcardSearchPolies(List<String> metadataNames)
191195
/**
192196
* Find the managed policy for the ranger securable object.
193197
*
194-
* @param rangerMetadataObject The ranger securable object to find the managed policy.
198+
* @param AuthorizationMetadataObject The ranger securable object to find the managed policy.
195199
* @return The managed policy for the metadata object.
196200
*/
197-
public RangerPolicy findManagedPolicy(RangerMetadataObject rangerMetadataObject)
201+
public RangerPolicy findManagedPolicy(AuthorizationMetadataObject AuthorizationMetadataObject)
198202
throws AuthorizationPluginException {
199-
List<RangerPolicy> policies = wildcardSearchPolies(rangerMetadataObject.names());
203+
List<RangerPolicy> policies = wildcardSearchPolies(AuthorizationMetadataObject.names());
200204
if (!policies.isEmpty()) {
201205
/**
202206
* Because Ranger doesn't support the precise search, Ranger will return the policy meets the
203207
* wildcard(*,?) conditions, If you use `db.table` condition to search policy, the Ranger will
204208
* match `db1.table1`, `db1.table2`, `db*.table*`, So we need to manually precisely filter
205209
* this research results.
206210
*/
207-
List<String> nsMetadataObj = rangerMetadataObject.names();
211+
List<String> nsMetadataObj = AuthorizationMetadataObject.names();
208212
Map<String, String> preciseFilters = new HashMap<>();
209213
for (int i = 0; i < nsMetadataObj.size(); i++) {
210214
preciseFilters.put(policyResourceDefines.get(i), nsMetadataObj.get(i));
@@ -438,7 +442,7 @@ protected void updatePolicyOwner(RangerPolicy policy, Owner preOwner, Owner newO
438442
});
439443
}
440444

441-
protected RangerPolicy createPolicyAddResources(RangerMetadataObject metadataObject) {
445+
protected RangerPolicy createPolicyAddResources(AuthorizationMetadataObject metadataObject) {
442446
RangerPolicy policy = new RangerPolicy();
443447
policy.setService(rangerServiceName);
444448
policy.setName(metadataObject.fullName());
@@ -451,7 +455,8 @@ protected RangerPolicy createPolicyAddResources(RangerMetadataObject metadataObj
451455
return policy;
452456
}
453457

454-
protected RangerPolicy addOwnerToNewPolicy(RangerMetadataObject metadataObject, Owner newOwner) {
458+
protected RangerPolicy addOwnerToNewPolicy(
459+
AuthorizationMetadataObject metadataObject, Owner newOwner) {
455460
RangerPolicy policy = createPolicyAddResources(metadataObject);
456461

457462
ownerPrivileges.forEach(
@@ -476,7 +481,7 @@ protected RangerPolicy addOwnerToNewPolicy(RangerMetadataObject metadataObject,
476481
}
477482

478483
protected RangerPolicy addOwnerRoleToNewPolicy(
479-
RangerMetadataObject metadataObject, String ownerRoleName) {
484+
AuthorizationMetadataObject metadataObject, String ownerRoleName) {
480485
RangerPolicy policy = createPolicyAddResources(metadataObject);
481486

482487
ownerPrivileges.forEach(

authorizations/authorization-ranger/src/main/java/org/apache/gravitino/authorization/ranger/RangerMetadataObject.java

+91-47
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,20 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
2019
package org.apache.gravitino.authorization.ranger;
2120

21+
import com.google.common.base.Preconditions;
2222
import java.util.List;
23-
import javax.annotation.Nullable;
2423
import org.apache.gravitino.MetadataObject;
25-
import org.apache.gravitino.annotation.Unstable;
24+
import org.apache.gravitino.authorization.AuthorizationMetadataObject;
2625

27-
/**
28-
* The Ranger MetadataObject is the basic unit of the Gravitino system. It represents the Apache
29-
* Ranger metadata object in the Apache Gravitino system. The object can be a catalog, schema,
30-
* table, column, etc.
31-
*/
32-
@Unstable
33-
public interface RangerMetadataObject {
26+
/** The helper class for {@link AuthorizationMetadataObject}. */
27+
public class RangerMetadataObject implements AuthorizationMetadataObject {
3428
/**
3529
* The type of object in the Ranger system. Every type will map one kind of the entity of the
3630
* Gravitino type system.
3731
*/
38-
enum Type {
32+
public enum Type implements AuthorizationMetadataObject.Type {
3933
/** A schema is a sub collection of the catalog. The schema can contain tables, columns, etc. */
4034
SCHEMA(MetadataObject.Type.SCHEMA),
4135
/** A table is mapped the table of relational data sources like Apache Hive, MySQL, etc. */
@@ -49,13 +43,13 @@ enum Type {
4943
this.metadataType = type;
5044
}
5145

52-
public MetadataObject.Type getMetadataType() {
46+
public MetadataObject.Type metadataObjectType() {
5347
return metadataType;
5448
}
5549

5650
public static Type fromMetadataType(MetadataObject.Type metadataType) {
5751
for (Type type : Type.values()) {
58-
if (type.getMetadataType() == metadataType) {
52+
if (type.metadataObjectType() == metadataType) {
5953
return type;
6054
}
6155
}
@@ -64,47 +58,97 @@ public static Type fromMetadataType(MetadataObject.Type metadataType) {
6458
}
6559
}
6660

67-
/**
68-
* The parent full name of the object. If the object doesn't have parent, this method will return
69-
* null.
70-
*
71-
* @return The parent full name of the object.
72-
*/
73-
@Nullable
74-
String parent();
61+
/** The implementation of the {@link MetadataObject}. */
62+
private final String name;
7563

76-
/**
77-
* The name of the object.
78-
*
79-
* @return The name of the object.
80-
*/
81-
String name();
64+
private final String parent;
8265

83-
/**
84-
* The all name list of the object.
85-
*
86-
* @return The name list of the object.
87-
*/
88-
List<String> names();
66+
private final AuthorizationMetadataObject.Type type;
8967

9068
/**
91-
* The full name of the object. Full name will be separated by "." to represent a string
92-
* identifier of the object, like catalog, catalog.table, etc.
69+
* Create the metadata object with the given name, parent and type.
9370
*
94-
* @return The name of the object.
71+
* @param parent The parent of the metadata object
72+
* @param name The name of the metadata object
73+
* @param type The type of the metadata object
9574
*/
96-
default String fullName() {
97-
if (parent() == null) {
98-
return name();
99-
} else {
100-
return parent() + "." + name();
75+
public RangerMetadataObject(String parent, String name, AuthorizationMetadataObject.Type type) {
76+
this.parent = parent;
77+
this.name = name;
78+
this.type = type;
79+
}
80+
81+
@Override
82+
public String name() {
83+
return name;
84+
}
85+
86+
@Override
87+
public List<String> names() {
88+
return DOT_SPLITTER.splitToList(fullName());
89+
}
90+
91+
@Override
92+
public String parent() {
93+
return parent;
94+
}
95+
96+
@Override
97+
public AuthorizationMetadataObject.Type type() {
98+
return type;
99+
}
100+
101+
@Override
102+
public void validateAuthorizationMetadataObject() throws IllegalArgumentException {
103+
List<String> names = names();
104+
Preconditions.checkArgument(
105+
names != null && !names.isEmpty(), "Cannot create a Ranger metadata object with no names");
106+
Preconditions.checkArgument(
107+
names.size() <= 3,
108+
"Cannot create a Ranger metadata object with the name length which is greater than 3");
109+
Preconditions.checkArgument(
110+
type != null, "Cannot create a Ranger metadata object with no type");
111+
112+
Preconditions.checkArgument(
113+
names.size() != 1 || type == RangerMetadataObject.Type.SCHEMA,
114+
"If the length of names is 1, it must be the SCHEMA type");
115+
116+
Preconditions.checkArgument(
117+
names.size() != 2 || type == RangerMetadataObject.Type.TABLE,
118+
"If the length of names is 2, it must be the TABLE type");
119+
120+
Preconditions.checkArgument(
121+
names.size() != 3 || type == RangerMetadataObject.Type.COLUMN,
122+
"If the length of names is 3, it must be COLUMN");
123+
124+
for (String name : names) {
125+
Preconditions.checkArgument(name != null, "Cannot create a metadata object with null name");
101126
}
102127
}
103128

104-
/**
105-
* The type of the object.
106-
*
107-
* @return The type of the object.
108-
*/
109-
Type type();
129+
@Override
130+
public boolean equals(Object o) {
131+
if (this == o) {
132+
return true;
133+
}
134+
135+
if (!(o instanceof RangerMetadataObject)) {
136+
return false;
137+
}
138+
139+
RangerMetadataObject that = (RangerMetadataObject) o;
140+
return java.util.Objects.equals(name, that.name)
141+
&& java.util.Objects.equals(parent, that.parent)
142+
&& type == that.type;
143+
}
144+
145+
@Override
146+
public int hashCode() {
147+
return java.util.Objects.hash(name, parent, type);
148+
}
149+
150+
@Override
151+
public String toString() {
152+
return "MetadataObject: [fullName=" + fullName() + "], [type=" + type + "]";
153+
}
110154
}

authorizations/authorization-ranger/src/main/java/org/apache/gravitino/authorization/ranger/RangerMetadataObjects.java

-125
This file was deleted.

0 commit comments

Comments
 (0)