-
Notifications
You must be signed in to change notification settings - Fork 415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#6536] improvement(authz): Create Ranger service if service is absent #6575
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3cdb335
[#6536] improvement: Create Ranger service if service is absent
jerqi 5b018ce
fix ut
jerqi 52444df
address comment
jerqi 9f0743f
Address comment
jerqi 74b88fd
fix ut
jerqi d60cc87
add comment and revert change
jerqi 69e34b2
remove the logic of deleting service
jerqi 69e261f
Add a new assert
jerqi 384e6e4
Merge branch 'main' into ISSUE-6536
jerqi 531ec52
rename method
jerqi 2fb0d96
optimize the usage
jerqi 83a5f42
add ut
jerqi 2fdc026
fix ut
jerqi bef8a8f
fix ut
jerqi 2793f78
Address comment
jerqi 2b07cef
address comments
jerqi bdbca22
address comment
jerqi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Preconditions; | ||
import com.google.common.collect.ImmutableMap; | ||
import com.sun.jersey.api.client.ClientResponse; | ||
import java.io.IOException; | ||
import java.time.Instant; | ||
import java.util.Arrays; | ||
|
@@ -56,6 +57,7 @@ | |
import org.apache.gravitino.utils.PrincipalUtils; | ||
import org.apache.ranger.RangerServiceException; | ||
import org.apache.ranger.plugin.model.RangerPolicy; | ||
import org.apache.ranger.plugin.model.RangerService; | ||
import org.apache.ranger.plugin.util.GrantRevokeRoleRequest; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
@@ -74,6 +76,8 @@ | |
public abstract class RangerAuthorizationPlugin | ||
implements AuthorizationPlugin, AuthorizationPrivilegesMappingProvider { | ||
private static final Logger LOG = LoggerFactory.getLogger(RangerAuthorizationPlugin.class); | ||
protected static final String HDFS_SERVICE_TYPE = "hdfs"; | ||
protected static final String HADOOP_SQL_SERVICE_TYPE = "hive"; | ||
|
||
protected String metalake; | ||
protected final String rangerServiceName; | ||
|
@@ -87,13 +91,22 @@ protected RangerAuthorizationPlugin(String metalake, Map<String, String> config) | |
new RangerAuthorizationProperties(config); | ||
rangerAuthorizationProperties.validate(); | ||
String rangerUrl = config.get(RangerAuthorizationProperties.RANGER_ADMIN_URL); | ||
|
||
String authType = config.get(RangerAuthorizationProperties.RANGER_AUTH_TYPE); | ||
|
||
rangerAdminName = config.get(RangerAuthorizationProperties.RANGER_USERNAME); | ||
|
||
// Apache Ranger Password should be minimum 8 characters with min one alphabet and one numeric. | ||
String password = config.get(RangerAuthorizationProperties.RANGER_PASSWORD); | ||
|
||
rangerServiceName = config.get(RangerAuthorizationProperties.RANGER_SERVICE_NAME); | ||
rangerClient = new RangerClientExtension(rangerUrl, authType, rangerAdminName, password); | ||
|
||
if (Boolean.parseBoolean( | ||
config.get(RangerAuthorizationProperties.RANGER_SERVICE_CREATE_IF_ABSENT))) { | ||
createRangerServiceIfNecessary(config, rangerServiceName); | ||
} | ||
|
||
rangerHelper = | ||
new RangerHelper( | ||
rangerClient, | ||
|
@@ -769,6 +782,34 @@ public Boolean onGroupAcquired(Group group) { | |
return Boolean.TRUE; | ||
} | ||
|
||
private void createRangerServiceIfNecessary(Map<String, String> config, String serviceName) { | ||
try { | ||
rangerClient.getService(serviceName); | ||
} catch (RangerServiceException rse) { | ||
if (rse.getStatus().equals(ClientResponse.Status.NOT_FOUND)) { | ||
try { | ||
RangerService rangerService = new RangerService(); | ||
rangerService.setType(getServiceType()); | ||
rangerService.setName(serviceName); | ||
rangerService.setConfigs(getServiceConfigs(config)); | ||
rangerClient.createService(rangerService); | ||
// We should remove some default policies, they will cause users to get more policies | ||
// than they should do. | ||
List<RangerPolicy> policies = rangerClient.getPoliciesInService(serviceName); | ||
for (RangerPolicy policy : policies) { | ||
rangerClient.deletePolicy(policy.getId()); | ||
} | ||
Comment on lines
+798
to
+801
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a description of why the policy should be deleted There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. I can add the comment. |
||
} catch (RangerServiceException crse) { | ||
throw new AuthorizationPluginException( | ||
"Fail to create ranger service %s, exception: %s", serviceName, crse.getMessage()); | ||
} | ||
} else { | ||
throw new AuthorizationPluginException( | ||
"Fail to get ranger service name %s, exception: %s", serviceName, rse.getMessage()); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Add the securable object's privilege to the Ranger policy. <br> | ||
* 1. Find the policy base the metadata object. <br> | ||
|
@@ -959,6 +1000,22 @@ protected void removePolicyByMetadataObject(AuthorizationMetadataObject authzMet | |
} | ||
} | ||
|
||
protected String getConfValue(Map<String, String> conf, String key, String defaultValue) { | ||
if (conf.containsKey(key)) { | ||
return conf.get(key); | ||
} | ||
return defaultValue; | ||
} | ||
|
||
protected abstract String getServiceType(); | ||
|
||
protected abstract Map<String, String> getServiceConfigs(Map<String, String> config); | ||
|
||
protected int getPrefixLength() { | ||
// We should consider `.`. We need to add 1 | ||
return RangerAuthorizationProperties.RANGER_PREFIX.length() + 1; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException {} | ||
|
||
|
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we be so sure that this property is there?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Boolean.parseBoolean can handle null value. Null value equals false.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please make sure there won't be uncaught exceptions.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see the code