Skip to content
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

DG-1854 : Optimize tag fetching using denormalized attributes during authz check #3605

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasStructType.AtlasAttribute;
import org.apache.atlas.type.AtlasTypeRegistry;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -33,6 +34,9 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.stream.Collectors;

public class AtlasAccessRequest {
private static Logger LOG = LoggerFactory.getLogger(AtlasAccessRequest.class);
Expand Down Expand Up @@ -176,20 +180,20 @@ public String getEntityId(AtlasEntityHeader entity, AtlasTypeRegistry typeRegist
return ret == null ? "" : ret.toString();
}

public Set<AtlasClassification> getClassificationNames(AtlasEntityHeader entity) {
final Set<AtlasClassification> ret;

if (entity == null || entity.getClassifications() == null) {
ret = Collections.emptySet();
} else {
ret = new HashSet<>();

for (AtlasClassification classification : entity.getClassifications()) {
ret.add(classification);
}
public Set<AtlasClassification> getClassifications(AtlasEntityHeader entity) {
if (CollectionUtils.isNotEmpty(entity.getClassifications())) {
return new HashSet<>(entity.getClassifications());
}

return ret;
List<String> classificationNames = entity.getClassificationNames();
if (CollectionUtils.isEmpty(classificationNames)) {
return new HashSet<>();
}
// Create new classification objects with name using stream filter
return classificationNames.stream()
.distinct()
.map(AtlasClassification::new)
.collect(Collectors.toSet());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public AtlasEntityAccessRequest(AtlasTypeRegistry typeRegistry, AtlasPrivilege a
this.businessMetadata = businessMetadata;
this.attributeName = attributeName;
this.typeRegistry = typeRegistry;
this.entityClassifications = super.getClassificationNames(entity);
this.entityClassifications = super.getClassifications(entity);
this.auditEnabled = auditEnabled;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Set<String> getEnd1EntityTypeAndAllSuperTypes() {
}

public Set<AtlasClassification> getEnd1EntityClassifications() {
return super.getClassificationNames(end1Entity);
return super.getClassifications(end1Entity);
}

public String getEnd1EntityId() {
Expand All @@ -74,7 +74,7 @@ public Set<String> getEnd2EntityTypeAndAllSuperTypes() {
}

public Set<AtlasClassification> getEnd2EntityClassifications() {
return super.getClassificationNames(end2Entity);
return super.getClassifications(end2Entity);
}

public String getEnd2EntityId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,8 @@ public void authorizeRemoveRelation(AtlasEdge edge) throws AtlasBaseException {
return;
}

end1Entity = entityRetriever.toAtlasEntityHeaderWithClassifications(edge.getOutVertex());
end2Entity = entityRetriever.toAtlasEntityHeaderWithClassifications(edge.getInVertex());
end1Entity = entityRetriever.toAtlasEntityHeader(edge.getOutVertex());
end2Entity = entityRetriever.toAtlasEntityHeader(edge.getInVertex());

AtlasAuthorizationUtils.verifyAccess(new AtlasRelationshipAccessRequest(typeRegistry, AtlasPrivilege.RELATIONSHIP_REMOVE, relationShipType, end1Entity, end2Entity ));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public AtlasEntityWithExtInfo getByUniqueAttributes(AtlasEntityType entityType,
@Override
@GraphTransaction
public AtlasEntityHeader getAtlasEntityHeaderWithoutAuthorization(String guid, String qualifiedName, String typeName) throws AtlasBaseException {
return extractEntityHeader( guid, qualifiedName, typeName);
return extractEntityHeader( guid, qualifiedName, typeName, false);
}

@Override
Expand Down Expand Up @@ -2253,8 +2253,8 @@ public List<AtlasAccessorResponse> getAccessors(List<AtlasAccessorRequest> atlas
case RELATIONSHIP_ADD:
case RELATIONSHIP_UPDATE:
case RELATIONSHIP_REMOVE:
AtlasEntityHeader end1EntityHeader = extractEntityHeader(accessorRequest.getEntityGuidEnd1(), accessorRequest.getEntityQualifiedNameEnd1(), accessorRequest.getEntityTypeEnd1());
AtlasEntityHeader end2EntityHeader = extractEntityHeader(accessorRequest.getEntityGuidEnd2(), accessorRequest.getEntityQualifiedNameEnd2(), accessorRequest.getEntityTypeEnd2());
AtlasEntityHeader end1EntityHeader = extractEntityHeader(accessorRequest.getEntityGuidEnd1(), accessorRequest.getEntityQualifiedNameEnd1(), accessorRequest.getEntityTypeEnd1(), true);
AtlasEntityHeader end2EntityHeader = extractEntityHeader(accessorRequest.getEntityGuidEnd2(), accessorRequest.getEntityQualifiedNameEnd2(), accessorRequest.getEntityTypeEnd2(), true);

AtlasRelationshipAccessRequest relAccessRequest = new AtlasRelationshipAccessRequest(typeRegistry,
action, accessorRequest.getRelationshipTypeName(), end1EntityHeader, end2EntityHeader);
Expand Down Expand Up @@ -2294,16 +2294,18 @@ public List<AtlasAccessorResponse> getAccessors(List<AtlasAccessorRequest> atlas
}

private AtlasEntityAccessRequestBuilder getEntityAccessRequest(AtlasAccessorRequest element, AtlasPrivilege action) throws AtlasBaseException {
AtlasEntityHeader entityHeader = extractEntityHeader(element.getGuid(), element.getQualifiedName(), element.getTypeName());
AtlasEntityHeader entityHeader = extractEntityHeader(element.getGuid(), element.getQualifiedName(), element.getTypeName(), false);

return new AtlasEntityAccessRequestBuilder(typeRegistry, action, entityHeader);
}

private AtlasEntityHeader extractEntityHeader(String guid, String qualifiedName, String typeName) throws AtlasBaseException {
private AtlasEntityHeader extractEntityHeader(String guid, String qualifiedName, String typeName, boolean useClassificationsNames) throws AtlasBaseException {
AtlasEntityHeader entityHeader = null;

if (StringUtils.isNotEmpty(guid)) {
entityHeader = entityRetriever.toAtlasEntityHeaderWithClassifications(guid);
entityHeader = useClassificationsNames ?
entityRetriever.toAtlasEntityHeader(guid) :
entityRetriever.toAtlasEntityHeaderWithClassifications(guid);

} else {
AtlasEntityType entityType = typeRegistry.getEntityTypeByName(typeName);
Expand All @@ -2313,7 +2315,9 @@ private AtlasEntityHeader extractEntityHeader(String guid, String qualifiedName,
uniqueAttrs.put(QUALIFIED_NAME, qualifiedName);

AtlasVertex vertex = AtlasGraphUtilsV2.getVertexByUniqueAttributes(this.graph, entityType, uniqueAttrs);
entityHeader = entityRetriever.toAtlasEntityHeaderWithClassifications(vertex);
entityHeader = useClassificationsNames ?
entityRetriever.toAtlasEntityHeader(vertex) :
entityRetriever.toAtlasEntityHeaderWithClassifications(vertex);

} catch (AtlasBaseException abe) {
if (abe.getAtlasErrorCode() != AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ private AtlasEdge createRelationship(AtlasVertex end1Vertex, AtlasVertex end2Ver
AtlasRelationshipType relationType = typeRegistry.getRelationshipTypeByName(relationship.getTypeName());


AtlasEntityHeader end1Entity = entityRetriever.toAtlasEntityHeaderWithClassifications(end1Vertex);
AtlasEntityHeader end2Entity = entityRetriever.toAtlasEntityHeaderWithClassifications(end2Vertex);
AtlasEntityHeader end1Entity = entityRetriever.toAtlasEntityHeader(end1Vertex);
AtlasEntityHeader end2Entity = entityRetriever.toAtlasEntityHeader(end2Vertex);

AtlasAuthorizationUtils.verifyAccess(new AtlasRelationshipAccessRequest(typeRegistry, AtlasPrivilege.RELATIONSHIP_ADD,
relationship.getTypeName(), end1Entity, end2Entity));
Expand Down Expand Up @@ -532,8 +532,8 @@ private AtlasRelationship updateRelationship(AtlasEdge relationshipEdge, AtlasRe
AtlasRelationshipType relationType = typeRegistry.getRelationshipTypeByName(relationship.getTypeName());
AtlasVertex end1Vertex = relationshipEdge.getOutVertex();
AtlasVertex end2Vertex = relationshipEdge.getInVertex();
AtlasEntityHeader end1Entity = entityRetriever.toAtlasEntityHeaderWithClassifications(end1Vertex);
AtlasEntityHeader end2Entity = entityRetriever.toAtlasEntityHeaderWithClassifications(end2Vertex);
AtlasEntityHeader end1Entity = entityRetriever.toAtlasEntityHeader(end1Vertex);
AtlasEntityHeader end2Entity = entityRetriever.toAtlasEntityHeader(end2Vertex);

AtlasAuthorizationUtils.verifyAccess(new AtlasRelationshipAccessRequest(typeRegistry, AtlasPrivilege.RELATIONSHIP_UPDATE, relationship.getTypeName(), end1Entity, end2Entity));

Expand Down
Loading