From fe7009abfbf93f8742f5e3873e81c1354fbf8a98 Mon Sep 17 00:00:00 2001 From: ektavarma10 Date: Thu, 30 Nov 2023 02:41:34 +0530 Subject: [PATCH 1/4] Revert "add parllelisation for checkAccessAndScrub method" This reverts commit 1e226a9e --- .../authorizer/RangerAtlasAuthorizer.java | 67 +++---------------- 1 file changed, 10 insertions(+), 57 deletions(-) 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 0eb249d6aeb..dd645b473b6 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,14 +61,6 @@ import java.util.ListIterator; import java.util.Map; import java.util.Set; -import java.util.Objects; -import java.util.ArrayList; -import java.util.Optional; - -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.stream.Collectors; import static org.apache.atlas.authorization.atlas.authorizer.RangerAtlasAuthorizerUtil.*; import static org.apache.atlas.authorize.AtlasAuthorizationUtils.getCurrentUserGroups; @@ -89,8 +81,6 @@ public class RangerAtlasAuthorizer implements AtlasAuthorizer { add(AtlasPrivilege.ENTITY_UPDATE_CLASSIFICATION); }}; - static final ExecutorService entityAccessThreadpool = Executors.newFixedThreadPool(10); - @Override public void init() { if (LOG.isDebugEnabled()) { @@ -599,27 +589,22 @@ public void scrubSearchResults(AtlasSearchResultScrubRequest request, boolean is if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG)) perf = RangerPerfTracer.getPerfTracer(PERF_LOG, "RangerAtlasAuthorizer.scrubSearchResults(" + request + ")"); AtlasSearchResult result = request.getSearchResult(); - List entitiesToCheck = new ArrayList<>(); - if (CollectionUtils.isNotEmpty(result.getEntities())) { - entitiesToCheck.addAll(result.getEntities()); + for (AtlasEntityHeader entity : result.getEntities()) { + checkAccessAndScrub(entity, request, isScrubAuditEnabled); + } } - if (CollectionUtils.isNotEmpty(result.getFullTextResult())) { - entitiesToCheck.addAll( - result.getFullTextResult() - .stream() - .filter(Objects::nonNull) - .map(res -> res.getEntity()) - .collect(Collectors.toList()) - ); + for (AtlasSearchResult.AtlasFullTextResult fullTextResult : result.getFullTextResult()) { + if (fullTextResult != null) + checkAccessAndScrub(fullTextResult.getEntity(), request, isScrubAuditEnabled); + } } - if (MapUtils.isNotEmpty(result.getReferredEntities())) { - entitiesToCheck.addAll(result.getReferredEntities().values()); + for (AtlasEntityHeader entity : result.getReferredEntities().values()) { + checkAccessAndScrub(entity, request, isScrubAuditEnabled); + } } - - checkAccessAndScrubAsync(entitiesToCheck, request, isScrubAuditEnabled); } finally { RangerPerfTracer.log(perf); } @@ -641,38 +626,6 @@ public void filterTypesDef(AtlasTypesDefFilterRequest request) throws AtlasAutho } - private void checkAccessAndScrubAsync(List entitiesToCheck, AtlasSearchResultScrubRequest request, boolean isScrubAuditEnabled) throws AtlasAuthorizationException { - LOG.info("Creating futures to check access and scrub " + entitiesToCheck.size() + " entities"); - long startTime = System.currentTimeMillis(); - LOG.info("Started checkAccessAndScrubAsync: " + System.currentTimeMillis()); - List> completableFutures = entitiesToCheck - .stream() - .map(entity -> CompletableFuture.supplyAsync(() -> { - try { - checkAccessAndScrub(entity, request, isScrubAuditEnabled); - return null; - } catch (AtlasAuthorizationException e) { - return e; - } - }, entityAccessThreadpool)) - .collect(Collectors.toList()); - LOG.info("Completed async submission " + (System.currentTimeMillis()-startTime)); - // wait for all threads to complete their execution - CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).join(); - LOG.info("Completed waiting for completion of threads: " + (System.currentTimeMillis()-startTime)); - // get the first exception from any checkAccessAndScrub calls - Optional maybeAuthException = completableFutures - .stream() - .map(CompletableFuture::join) - .filter(Objects::nonNull) - .findFirst(); - - LOG.info("Async check access and scrub is complete: "+ (System.currentTimeMillis()-startTime)); - if (maybeAuthException.isPresent()) { - throw maybeAuthException.get(); - } - } - private void filterTypes(AtlasAccessRequest request, List typeDefs)throws AtlasAuthorizationException { if (typeDefs != null) { for (ListIterator iter = typeDefs.listIterator(); iter.hasNext();) { From 7a9f11b45996886ea0a828bfb6a8e5ed72be8c73 Mon Sep 17 00:00:00 2001 From: ektavarma10 Date: Mon, 27 Nov 2023 11:02:23 +0530 Subject: [PATCH 2/4] Add parallelisation for classification level access --- .../authorizer/RangerAtlasAuthorizer.java | 136 ++++++++++++------ .../authorizer/RangerAtlasAuthorizerUtil.java | 2 + 2 files changed, 96 insertions(+), 42 deletions(-) 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 dd645b473b6..63375f2d18e 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,6 +61,10 @@ 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; @@ -81,6 +85,8 @@ public class RangerAtlasAuthorizer implements AtlasAuthorizer { add(AtlasPrivilege.ENTITY_UPDATE_CLASSIFICATION); }}; + private static final ExecutorService classification_access_threadpool = Executors.newFixedThreadPool(NUM_THREADS); + @Override public void init() { if (LOG.isDebugEnabled()) { @@ -657,61 +663,41 @@ 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); - if (contextOjb == null) { - Map contextOjb1 = new HashMap(); - contextOjb1.put("CLASSIFICATIONS", rangerTagForEval); - rangerRequest.setContext(contextOjb1); - } else { - contextOjb.put("CLASSIFICATIONS", rangerTagForEval); - rangerRequest.setContext(contextOjb); - } + List> completableFutures = new ArrayList<>(); // check authorization for each classification for (AtlasClassification classificationToAuthorize : request.getEntityClassifications()) { - rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, request.getClassificationTypeAndAllSuperTypes(classificationToAuthorize.getTypeName())); - ret = checkAccess(rangerRequest, auditHandler); + RangerAccessRequestImpl rangerRequest = createRangerAccessRequest(request, classificationToAuthorize, rangerTagForEval); - if (!ret) { - break; - } + completableFutures.add(CompletableFuture.supplyAsync(()->checkAccess(rangerRequest, auditHandler), classification_access_threadpool)); } + + // 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 { + + RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); + RangerAccessResourceImpl rangerResource = new RangerAccessResourceImpl(); + + initRangerRequest(rangerRequest, request); + initRangerResource(rangerResource, request); + + rangerRequest.setResource(rangerResource); + rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, ENTITY_NOT_CLASSIFIED ); ret = checkAccess(rangerRequest, auditHandler); @@ -730,6 +716,72 @@ private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAud return ret; } + private RangerAccessRequestImpl createRangerAccessRequest(AtlasEntityAccessRequest request, + AtlasClassification classificationToAuthorize, + Set rangerTagForEval) { + + RangerAccessRequestImpl rangerRequest = new RangerAccessRequestImpl(); + RangerAccessResourceImpl rangerResource = new RangerAccessResourceImpl(); + + initRangerRequest(rangerRequest, request); + initRangerResource(rangerResource, request); + + rangerResource.setValue(RESOURCE_ENTITY_CLASSIFICATION, request.getClassificationTypeAndAllSuperTypes(classificationToAuthorize.getTypeName())); + + rangerRequest.setResource(rangerResource); + + setClassificationContextForRanger(rangerTagForEval, rangerRequest); + + 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 initRangerRequest(RangerAccessRequestImpl rangerRequest, AtlasEntityAccessRequest request) { + 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()); + } + + private void initRangerResource(RangerAccessResourceImpl rangerResource, AtlasEntityAccessRequest request) { + 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)); + } + + } + private void setClassificationsToRequestContext(Set entityClassifications, RangerAccessRequestImpl rangerRequest) { Map contextOjb = rangerRequest.getContext(); 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 c78e986581a..1a81bb180ec 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,6 +43,8 @@ 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; From e0bc2b33098255d05ae81244bca5063116ed7419 Mon Sep 17 00:00:00 2001 From: ektavarma10 Date: Thu, 30 Nov 2023 02:43:16 +0530 Subject: [PATCH 3/4] Minor refactorinh --- .../authorization/atlas/authorizer/RangerAtlasAuthorizer.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 63375f2d18e..a0a34738e4e 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 @@ -85,7 +85,7 @@ public class RangerAtlasAuthorizer implements AtlasAuthorizer { add(AtlasPrivilege.ENTITY_UPDATE_CLASSIFICATION); }}; - private static final ExecutorService classification_access_threadpool = Executors.newFixedThreadPool(NUM_THREADS); + private static final ExecutorService classificationAccessThreadpool = Executors.newFixedThreadPool(NUM_THREADS); @Override public void init() { @@ -676,7 +676,7 @@ private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAud RangerAccessRequestImpl rangerRequest = createRangerAccessRequest(request, classificationToAuthorize, rangerTagForEval); - completableFutures.add(CompletableFuture.supplyAsync(()->checkAccess(rangerRequest, auditHandler), classification_access_threadpool)); + completableFutures.add(CompletableFuture.supplyAsync(()->checkAccess(rangerRequest, auditHandler), classificationAccessThreadpool)); } // wait for all threads to complete their execution From e855ebff883bdfc68f4dc2a158ae67d9ecc8fcbb Mon Sep 17 00:00:00 2001 From: ektavarma10 Date: Thu, 30 Nov 2023 02:49:47 +0530 Subject: [PATCH 4/4] Add log lines --- .../atlas/authorizer/RangerAtlasAuthorizer.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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 a0a34738e4e..0fbc9494280 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 @@ -53,6 +53,7 @@ import org.apache.atlas.plugin.service.RangerBasePlugin; import org.apache.atlas.plugin.util.RangerPerfTracer; +import java.awt.*; import java.util.Collection; import java.util.Collections; import java.util.HashMap; @@ -660,6 +661,8 @@ private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAud if (LOG.isDebugEnabled()) { LOG.debug("==> isAccessAllowed(" + request + ")"); } + long startTime = System.currentTimeMillis(); + LOG.info("start isAccessAllowed : " + startTime); boolean ret = false; try { @@ -672,6 +675,7 @@ private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAud List> completableFutures = new ArrayList<>(); // check authorization for each classification + LOG.info("start check authrization for each classification: " + (System.currentTimeMillis() - startTime)); for (AtlasClassification classificationToAuthorize : request.getEntityClassifications()) { RangerAccessRequestImpl rangerRequest = createRangerAccessRequest(request, classificationToAuthorize, rangerTagForEval); @@ -681,6 +685,8 @@ private boolean isAccessAllowed(AtlasEntityAccessRequest request, RangerAtlasAud // wait for all threads to complete their execution CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).join(); + LOG.info("end check authorization for each classification: " + (System.currentTimeMillis() - startTime)); + // if all checkAccess calls return true, then ret is true, else it is false ret = completableFutures @@ -917,11 +923,13 @@ private void checkAccessAndScrub(AtlasEntityHeader entity, AtlasSearchResultScru entityAccessRequest.setClientIPAddress(request.getClientIPAddress()); entityAccessRequest.setForwardedAddresses(request.getForwardedAddresses()); entityAccessRequest.setRemoteIPAddress(request.getRemoteIPAddress()); - + long startTime = System.currentTimeMillis(); boolean isEntityAccessAllowed = isScrubAuditEnabled ? isAccessAllowed(entityAccessRequest) : isAccessAllowed(entityAccessRequest, null); + LOG.info("isEntityAccessAllowed: " + (System.currentTimeMillis() - startTime)); if (!isEntityAccessAllowed) { scrubEntityHeader(entity, request.getTypeRegistry()); } + LOG.info("scrubEntityHeader completed" + (System.currentTimeMillis() - startTime)); } }