diff --git a/auth-plugin-atlas/src/main/java/org/apache/atlas/authorization/atlas/authorizer/RangerAtlasAuthorizer.java b/auth-plugin-atlas/src/main/java/org/apache/atlas/authorization/atlas/authorizer/RangerAtlasAuthorizer.java index 6ad01e932b..dd645b473b 100644 --- a/auth-plugin-atlas/src/main/java/org/apache/atlas/authorization/atlas/authorizer/RangerAtlasAuthorizer.java +++ b/auth-plugin-atlas/src/main/java/org/apache/atlas/authorization/atlas/authorizer/RangerAtlasAuthorizer.java @@ -61,10 +61,6 @@ import java.util.ListIterator; import java.util.Map; import java.util.Set; -import java.util.ArrayList; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; import static org.apache.atlas.authorization.atlas.authorizer.RangerAtlasAuthorizerUtil.*; import static org.apache.atlas.authorize.AtlasAuthorizationUtils.getCurrentUserGroups; @@ -85,8 +81,6 @@ public class RangerAtlasAuthorizer implements AtlasAuthorizer { add(AtlasPrivilege.ENTITY_UPDATE_CLASSIFICATION); }}; - private static final ExecutorService classificationAccessThreadpool = Executors.newFixedThreadPool(NUM_THREADS); - @Override public void init() { if (LOG.isDebugEnabled()) { @@ -663,43 +657,61 @@ private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAud boolean ret = false; try { + final String action = request.getAction() != null ? request.getAction().getType() : null; + final Set entityTypes = request.getEntityTypeAndAllSuperTypes(); + final String entityId = request.getEntityId(); + final String classification = request.getClassification() != null ? request.getClassification().getTypeName() : null; + final RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); + final RangerAccessResourceImpl rangerResource = new RangerAccessResourceImpl(); + final String ownerUser = request.getEntity() != null ? (String) request.getEntity().getAttribute(RESOURCE_ENTITY_OWNER) : null; + + rangerResource.setValue(RESOURCE_ENTITY_TYPE, entityTypes); + rangerResource.setValue(RESOURCE_ENTITY_ID, entityId); + rangerResource.setOwnerUser(ownerUser); + rangerRequest.setAccessType(action); + rangerRequest.setAction(action); + rangerRequest.setUser(request.getUser()); + rangerRequest.setUserGroups(request.getUserGroups()); + rangerRequest.setClientIPAddress(request.getClientIPAddress()); + rangerRequest.setAccessTime(request.getAccessTime()); + rangerRequest.setResource(rangerResource); + rangerRequest.setForwardedAddresses(request.getForwardedAddresses()); + rangerRequest.setRemoteIPAddress(request.getRemoteIPAddress()); + + if (AtlasPrivilege.ENTITY_ADD_LABEL.equals(request.getAction()) || AtlasPrivilege.ENTITY_REMOVE_LABEL.equals(request.getAction())) { + rangerResource.setValue(RESOURCE_ENTITY_LABEL, request.getLabel()); + } else if (AtlasPrivilege.ENTITY_UPDATE_BUSINESS_METADATA.equals(request.getAction())) { + rangerResource.setValue(RESOURCE_ENTITY_BUSINESS_METADATA, request.getBusinessMetadata()); + } else if (StringUtils.isNotEmpty(classification) && CLASSIFICATION_PRIVILEGES.contains(request.getAction())) { + rangerResource.setValue(RESOURCE_CLASSIFICATION, request.getClassificationTypeAndAllSuperTypes(classification)); + } if (CollectionUtils.isNotEmpty(request.getEntityClassifications())) { Set entityClassifications = request.getEntityClassifications(); + Map contextOjb = rangerRequest.getContext(); Set rangerTagForEval = getRangerServiceTag(entityClassifications); - List> completableFutures = new ArrayList<>(); + if (contextOjb == null) { + Map contextOjb1 = new HashMap(); + contextOjb1.put("CLASSIFICATIONS", rangerTagForEval); + rangerRequest.setContext(contextOjb1); + } else { + contextOjb.put("CLASSIFICATIONS", rangerTagForEval); + rangerRequest.setContext(contextOjb); + } // check authorization for each classification for (AtlasClassification classificationToAuthorize : request.getEntityClassifications()) { + rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, request.getClassificationTypeAndAllSuperTypes(classificationToAuthorize.getTypeName())); - RangerAccessResourceImpl rangerResource = createRangerResourceRequest(request); - RangerAccessRequestImpl rangerRequest = createRangerAccessRequest(request, rangerResource); - - rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, - request.getClassificationTypeAndAllSuperTypes(classificationToAuthorize.getTypeName())); - setClassificationContextForRanger(rangerTagForEval, rangerRequest); + ret = checkAccess(rangerRequest, auditHandler); - completableFutures.add(CompletableFuture.supplyAsync( - () -> checkAccess(rangerRequest, auditHandler), classificationAccessThreadpool)); + if (!ret) { + break; + } } - - // wait for all threads to complete their execution - CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).join(); - - // if all checkAccess calls return true, then ret is true, else it is false - ret = completableFutures - .stream() - .map(CompletableFuture::join) - .allMatch(result -> result == true); - } else { - - RangerAccessResourceImpl rangerResource = createRangerResourceRequest(request); - - RangerAccessRequestImpl rangerRequest = createRangerAccessRequest(request, rangerResource); - rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, ENTITY_NOT_CLASSIFIED ); ret = checkAccess(rangerRequest, auditHandler); @@ -718,66 +730,6 @@ private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAud return ret; } - private RangerAccessResourceImpl createRangerResourceRequest(AtlasEntityAccessRequest request) { - RangerAccessResourceImpl rangerResource = new RangerAccessResourceImpl(); - - final Set entityTypes = request.getEntityTypeAndAllSuperTypes(); - final String entityId = request.getEntityId(); - final String ownerUser = request.getEntity() != null ? - (String) request.getEntity().getAttribute(RESOURCE_ENTITY_OWNER) : null; - final String classification = request.getClassification() != null ? - request.getClassification().getTypeName() : null; - - rangerResource.setValue(RESOURCE_ENTITY_TYPE, entityTypes); - rangerResource.setValue(RESOURCE_ENTITY_ID, entityId); - rangerResource.setOwnerUser(ownerUser); - - if (AtlasPrivilege.ENTITY_ADD_LABEL.equals(request.getAction()) || - AtlasPrivilege.ENTITY_REMOVE_LABEL.equals(request.getAction())) { - rangerResource.setValue(RESOURCE_ENTITY_LABEL, request.getLabel()); - } else if (AtlasPrivilege.ENTITY_UPDATE_BUSINESS_METADATA.equals(request.getAction())) { - rangerResource.setValue(RESOURCE_ENTITY_BUSINESS_METADATA, request.getBusinessMetadata()); - } else if (StringUtils.isNotEmpty(classification) && CLASSIFICATION_PRIVILEGES.contains(request.getAction())) { - rangerResource.setValue(RESOURCE_CLASSIFICATION, - request.getClassificationTypeAndAllSuperTypes(classification)); - } - - return rangerResource; - } - - private RangerAccessRequestImpl createRangerAccessRequest(AtlasEntityAccessRequest request, - RangerAccessResourceImpl rangerResource) { - - RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); - - final String action = request.getAction() != null ? request.getAction().getType() : null; - - rangerRequest.setAccessType(action); - rangerRequest.setAction(action); - rangerRequest.setUser(request.getUser()); - rangerRequest.setUserGroups(request.getUserGroups()); - rangerRequest.setClientIPAddress(request.getClientIPAddress()); - rangerRequest.setAccessTime(request.getAccessTime()); - rangerRequest.setForwardedAddresses(request.getForwardedAddresses()); - rangerRequest.setRemoteIPAddress(request.getRemoteIPAddress()); - rangerRequest.setResource(rangerResource); - - return rangerRequest; - - } - - private static void setClassificationContextForRanger(Set rangerTagForEval, RangerAccessRequestImpl rangerRequest) { - Map contextOjb = rangerRequest.getContext(); - - if (contextOjb == null) { - Map contextOjb1 = new HashMap(); - contextOjb1.put("CLASSIFICATIONS", rangerTagForEval); - rangerRequest.setContext(contextOjb1); - } else { - contextOjb.put("CLASSIFICATIONS", rangerTagForEval); - rangerRequest.setContext(contextOjb); - } - } private void setClassificationsToRequestContext(Set entityClassifications, RangerAccessRequestImpl rangerRequest) { Map contextOjb = rangerRequest.getContext(); @@ -951,12 +903,10 @@ public RangerAtlasAuditHandler(AtlasEntityAccessRequest request, RangerServiceDe rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, strClassifications); rangerResource.setValue(RESOURCE_ENTITY_ID, request.getEntityId()); - if (AtlasPrivilege.ENTITY_ADD_LABEL.equals(request.getAction()) || - AtlasPrivilege.ENTITY_REMOVE_LABEL.equals(request.getAction())) { + if (AtlasPrivilege.ENTITY_ADD_LABEL.equals(request.getAction()) || AtlasPrivilege.ENTITY_REMOVE_LABEL.equals(request.getAction())) { rangerResource.setValue(RESOURCE_ENTITY_LABEL, "label=" + request.getLabel()); } else if (AtlasPrivilege.ENTITY_UPDATE_BUSINESS_METADATA.equals(request.getAction())) { - rangerResource.setValue(RESOURCE_ENTITY_BUSINESS_METADATA, - "business-metadata=" + request.getBusinessMetadata()); + rangerResource.setValue(RESOURCE_ENTITY_BUSINESS_METADATA, "business-metadata=" + request.getBusinessMetadata()); } auditEvents = new HashMap<>(); diff --git a/auth-plugin-atlas/src/main/java/org/apache/atlas/authorization/atlas/authorizer/RangerAtlasAuthorizerUtil.java b/auth-plugin-atlas/src/main/java/org/apache/atlas/authorization/atlas/authorizer/RangerAtlasAuthorizerUtil.java index 1a81bb180e..c78e986581 100644 --- a/auth-plugin-atlas/src/main/java/org/apache/atlas/authorization/atlas/authorizer/RangerAtlasAuthorizerUtil.java +++ b/auth-plugin-atlas/src/main/java/org/apache/atlas/authorization/atlas/authorizer/RangerAtlasAuthorizerUtil.java @@ -43,8 +43,6 @@ public class RangerAtlasAuthorizerUtil { - public static Integer NUM_THREADS = 5; - static void toRangerRequest(AtlasEntityAccessRequest request, RangerAccessRequestImpl rangerRequest, RangerAccessResourceImpl rangerResource){ final String action = request.getAction() != null ? request.getAction().getType() : null;