Skip to content

Commit

Permalink
rename interface name
Browse files Browse the repository at this point in the history
  • Loading branch information
xunliu committed Sep 18, 2024
1 parent 1542d74 commit be618e8
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 112 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ public RangerAuthorizationHivePlugin(Map<String, String> config) {
super(config);
}

/** Initialize the default mapping Gravitino privilege name to the Ranger privileges */
@Override
public void initializePrivilegesMappingConfig() {
/** Set the default mapping Gravitino privilege name to the Ranger rule */
public void privilegesMappingRule() {
privilegesMapping.put(
Privilege.Name.CREATE_SCHEMA, ImmutableSet.of(RangerHivePrivilege.CREATE));
privilegesMapping.put(Privilege.Name.CREATE_TABLE, ImmutableSet.of(RangerHivePrivilege.CREATE));
Expand All @@ -45,19 +44,17 @@ public void initializePrivilegesMappingConfig() {
ImmutableSet.of(RangerHivePrivilege.READ, RangerHivePrivilege.SELECT));
}

/** Initialize the default owner privileges. */
@Override
public void initializeOwnerPrivilegesConfig() {
/** Set the default owner rule. */
public void ownerMappingRule() {
ownerPrivileges.add(RangerHivePrivilege.ALL);
}

/** Initial Ranger policy resource defines. */
@Override
public void initializePolicyResourceDefinesConfig() {
/** Set Ranger policy resource rule. */
public void policyResourceDefinesRule() {
policyResourceDefines =
ImmutableList.of(
PolicyResource.DATABASE.toString(),
PolicyResource.TABLE.toString(),
PolicyResource.COLUMN.toString());
PolicyResource.DATABASE.getName(),
PolicyResource.TABLE.getName(),
PolicyResource.COLUMN.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.io.IOException;
import java.time.Instant;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -62,10 +64,17 @@
* 4. The Ranger policy also supports multiple users and groups, But we only use a user or group to
* implement Gravitino Owner concept. <br>
*/
public abstract class RangerAuthorizationPlugin extends RangerAuthorizationConfig
implements AuthorizationPlugin {
public abstract class RangerAuthorizationPlugin
implements AuthorizationPlugin, RangerPrivilegesMappingProvider {
private static final Logger LOG = LoggerFactory.getLogger(RangerAuthorizationPlugin.class);

/** Mapping Gravitino privilege name to the Ranger privileges configuration. */
protected Map<Privilege.Name, Set<RangerPrivilege>> privilegesMapping = new HashMap<>();
/** The owner privileges, the owner can do anything on the metadata object configuration */
protected Set<RangerPrivilege> ownerPrivileges = new HashSet<>();
/** The Ranger policy resource defines configuration. */
protected List<String> policyResourceDefines;

protected String rangerServiceName;
protected RangerClientExtend rangerClient;
private RangerHelper rangerHelper;
Expand All @@ -86,9 +95,9 @@ public RangerAuthorizationPlugin(Map<String, String> config) {
rangerClient = new RangerClientExtend(rangerUrl, authType, rangerAdminName, password);

// Initialize privilegesMapping and ownerPrivileges
initializeOwnerPrivilegesConfig();
initializePrivilegesMappingConfig();
initializePolicyResourceDefinesConfig();
ownerMappingRule();
privilegesMappingRule();
policyResourceDefinesRule();

rangerHelper =
new RangerHelper(
Expand All @@ -100,6 +109,18 @@ public RangerAuthorizationPlugin(Map<String, String> config) {
policyResourceDefines);
}

public final Map<Privilege.Name, Set<RangerPrivilege>> getPrivilegesMapping() {
return privilegesMapping;
}

public final Set<RangerPrivilege> getOwnerPrivileges() {
return ownerPrivileges;
}

public final List<String> getPolicyResourceDefines() {
return policyResourceDefines;
}

/**
* Translate the privilege name to the corresponding privilege name in the Ranger
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public RangerHelper(
*/
public Set<String> translatePrivilege(Privilege.Name name) {
return privilegesMapping.get(name).stream()
.map(RangerPrivilege::toString)
.map(RangerPrivilege::getName)
.collect(Collectors.toSet());
}

Expand Down Expand Up @@ -362,7 +362,7 @@ protected void updatePolicyOwner(RangerPolicy policy, Owner preOwner, Owner newO
return ownerPrivileges.stream()
.anyMatch(
ownerPrivilege -> {
return ownerPrivilege.equals(policyItemAccess.getType());
return ownerPrivilege.isEquals(policyItemAccess.getType());
});
});
})
Expand Down Expand Up @@ -400,7 +400,7 @@ protected void updatePolicyOwner(RangerPolicy policy, Owner preOwner, Owner newO
return policyItem.getAccesses().stream()
.anyMatch(
policyItemAccess -> {
return ownerPrivilege.equals(policyItemAccess.getType());
return ownerPrivilege.isEquals(policyItemAccess.getType());
});
});
})
Expand All @@ -410,7 +410,7 @@ protected void updatePolicyOwner(RangerPolicy policy, Owner preOwner, Owner newO
RangerPolicy.RangerPolicyItem policyItem = new RangerPolicy.RangerPolicyItem();
policyItem
.getAccesses()
.add(new RangerPolicy.RangerPolicyItemAccess(ownerPrivilege.toString()));
.add(new RangerPolicy.RangerPolicyItemAccess(ownerPrivilege.getName()));
if (newOwner != null) {
if (newOwner.type() == Owner.Type.USER) {
policyItem.getUsers().add(newOwner.name());
Expand Down Expand Up @@ -458,7 +458,7 @@ protected RangerPolicy addOwnerToNewPolicy(MetadataObject metadataObject, Owner
RangerPolicy.RangerPolicyItem policyItem = new RangerPolicy.RangerPolicyItem();
policyItem
.getAccesses()
.add(new RangerPolicy.RangerPolicyItemAccess(ownerPrivilege.toString()));
.add(new RangerPolicy.RangerPolicyItemAccess(ownerPrivilege.getName()));
if (newOwner != null) {
if (newOwner.type() == Owner.Type.USER) {
policyItem.getUsers().add(newOwner.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@

/** RangerPrivilege interface is used to define the Ranger privileges. */
public interface RangerPrivilege {
String toString();

boolean equals(String value);
String getName();
boolean isEquals(String value);

/** Ranger Hive privileges enumeration. */
enum RangerHivePrivilege implements RangerPrivilege {
Expand All @@ -39,20 +38,20 @@ enum RangerHivePrivilege implements RangerPrivilege {
REPLADMIN("repladmin"),
SERVICEADMIN("serviceadmin");

private final String string; // Access a type in the Ranger policy item
private final String name; // Access a type in the Ranger policy item

RangerHivePrivilege(String str) {
this.string = str;
RangerHivePrivilege(String name) {
this.name = name;
}

@Override
public String toString() {
return string;
public String getName() {
return name;
}

@Override
public boolean equals(String value) {
return this.string.equalsIgnoreCase(value);
public boolean isEquals(String value) {
return name.equalsIgnoreCase(value);
}
}

Expand All @@ -62,20 +61,20 @@ enum RangerHdfsPrivilege implements RangerPrivilege {
WRITE("write"),
EXECUTE("execute");

private final String string; // Access a type in the Ranger policy item
private final String name; // Access a type in the Ranger policy item

RangerHdfsPrivilege(String str) {
this.string = str;
RangerHdfsPrivilege(String name) {
this.name = name;
}

@Override
public String toString() {
return string;
public String getName() {
return name;
}

@Override
public boolean equals(String value) {
return this.string.equalsIgnoreCase(value);
public boolean isEquals(String value) {
return name.equalsIgnoreCase(value);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static RangerPrivilege valueOf(String string) {
String strPrivilege = string.trim().toLowerCase();
for (Class<? extends Enum<? extends RangerPrivilege>> enumClass : allRangerPrivileges) {
for (Enum<? extends RangerPrivilege> privilege : enumClass.getEnumConstants()) {
if (((RangerPrivilege) privilege).equals(strPrivilege)) {
if (((RangerPrivilege) privilege).isEquals(strPrivilege)) {
return (RangerPrivilege) privilege;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino.authorization.ranger;

/**
* Ranger authorization use this provider to mapping Gravitino privilege to the Ranger privileges.
* We can use this it to support the different Ranger authorization components, such as Hive, HDFS,
* HBase, etc.
*/
public interface RangerPrivilegesMappingProvider {
/** Set the mapping Gravitino privilege name to the Ranger privileges rule. */
void privilegesMappingRule();

/** Set the owner privileges rule. */
void ownerMappingRule();

/** Set the policy resource defines rule. */
void policyResourceDefinesRule();
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ public enum PolicyResource {
TABLE("table"),
COLUMN("column");

private final String string;
private final String name;

PolicyResource(String str) {
this.string = str;
PolicyResource(String name) {
this.name = name;
}

public String toString() {
return string;
public String getName() {
return name;
}
}
}

0 comments on commit be618e8

Please sign in to comment.