jerqi commented on code in PR #6100: URL: https://github.com/apache/gravitino/pull/6100#discussion_r1904954025
########## authorizations/authorization-ranger/src/main/java/org/apache/gravitino/authorization/ranger/RangerAuthorizationHDFSPlugin.java: ########## @@ -118,27 +127,281 @@ public List<String> policyResourceDefinesRule() { return ImmutableList.of(RangerDefines.PolicyResource.PATH.getName()); } + String getAuthorizationPath(PathBasedMetadataObject pathBasedMetadataObject) { + return HDFS_PATTERN.matcher(pathBasedMetadataObject.path()).replaceAll(""); + } + + /** + * Find the managed policy for the ranger securable object. + * + * @param authzMetadataObject The ranger securable object to find the managed policy. + * @return The managed policy for the metadata object. + */ + @Override + public RangerPolicy findManagedPolicy(AuthorizationMetadataObject authzMetadataObject) + throws AuthorizationPluginException { + List<String> nsMetadataObj = authzMetadataObject.names(); + PathBasedMetadataObject pathAuthzMetadataObject = (PathBasedMetadataObject) authzMetadataObject; + Map<String, String> preciseFilters = new HashMap<>(); + for (int i = 0; i < nsMetadataObj.size() && i < policyResourceDefinesRule().size(); i++) { + preciseFilters.put( + policyResourceDefinesRule().get(i), getAuthorizationPath(pathAuthzMetadataObject)); + } + return preciseFindPolicy(authzMetadataObject, preciseFilters); + } + + @Override + /** Wildcard search the Ranger policies in the different Ranger service. */ + protected List<RangerPolicy> wildcardSearchPolies( + AuthorizationMetadataObject authzMetadataObject) { + Preconditions.checkArgument(authzMetadataObject instanceof PathBasedMetadataObject); + PathBasedMetadataObject pathBasedMetadataObject = (PathBasedMetadataObject) authzMetadataObject; + List<String> resourceDefines = policyResourceDefinesRule(); + Map<String, String> searchFilters = new HashMap<>(); + searchFilters.put(SearchFilter.SERVICE_NAME, rangerServiceName); + resourceDefines.stream() + .forEach( + resourceDefine -> { + searchFilters.put( + SearchFilter.RESOURCE_PREFIX + resourceDefine, + getAuthorizationPath(pathBasedMetadataObject)); + }); + try { + return rangerClient.findPolicies(searchFilters); + } catch (RangerServiceException e) { + throw new AuthorizationPluginException(e, "Failed to find the policies in the Ranger"); + } + } + + /** + * If rename the SCHEMA, Need to rename these the relevant policies, `{schema}`, `{schema}.*`, + * `{schema}.*.*` <br> + * If rename the TABLE, Need to rename these the relevant policies, `{schema}.*`, `{schema}.*.*` + * <br> + */ + @Override + protected void doRenameMetadataObject( + AuthorizationMetadataObject authzMetadataObject, + AuthorizationMetadataObject newAuthzMetadataObject) { + Preconditions.checkArgument( + authzMetadataObject instanceof PathBasedMetadataObject, + "The metadata object must be a PathBasedMetadataObject"); + Preconditions.checkArgument( + newAuthzMetadataObject instanceof PathBasedMetadataObject, + "The metadata object must be a PathBasedMetadataObject"); + updatePolicyByMetadataObject( + newAuthzMetadataObject.type().metadataObjectType(), + authzMetadataObject, + newAuthzMetadataObject); + } + + @Override + protected void updatePolicyByMetadataObject( + MetadataObject.Type operationType, + AuthorizationMetadataObject oldAuthzMetaObject, + AuthorizationMetadataObject newAuthzMetaObject) { + PathBasedMetadataObject newPathBasedMetadataObject = + (PathBasedMetadataObject) newAuthzMetaObject; + List<RangerPolicy> oldPolicies = wildcardSearchPolies(oldAuthzMetaObject); + List<RangerPolicy> existNewPolicies = wildcardSearchPolies(newAuthzMetaObject); + if (oldPolicies.isEmpty()) { + LOG.warn("Cannot find the Ranger policy for the metadata object({})!", oldAuthzMetaObject); + return; + } + if (!existNewPolicies.isEmpty()) { + LOG.warn("The Ranger policy for the metadata object({}) already exists!", newAuthzMetaObject); + } + oldPolicies.stream() + .forEach( + policy -> { + try { + // Update the policy name is following Gravitino's spec + policy.setName(getAuthorizationPath(newPathBasedMetadataObject)); + // Update the policy resource name to new name + policy + .getResources() + .put( + rangerHelper.policyResourceDefines.get(0), + new RangerPolicy.RangerPolicyResource( + getAuthorizationPath(newPathBasedMetadataObject))); + + boolean alreadyExist = + existNewPolicies.stream() + .anyMatch( + existNewPolicy -> + existNewPolicy.getName().equals(policy.getName()) + || existNewPolicy.getResources().equals(policy.getResources())); + if (alreadyExist) { + LOG.warn( + "The Ranger policy for the metadata object({}) already exists!", + newAuthzMetaObject); + return; + } + + // Update the policy + rangerClient.updatePolicy(policy.getId(), policy); + } catch (RangerServiceException e) { + LOG.error("Failed to rename the policy {}!", policy); + throw new RuntimeException(e); + } + }); + } + + /** + * If remove the SCHEMA, need to remove these the relevant policies, `{schema}`, `{schema}.*`, + * `{schema}.*.*` <br> + * If remove the TABLE, need to remove these the relevant policies, `{schema}.*`, `{schema}.*.*` + * <br> + * If remove the COLUMN, Only need to remove `{schema}.*.*` <br> Review Comment: Is the comment correct? Do we need to process PATH? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org