Skip to content

Commit

Permalink
Refactore contract code and release
Browse files Browse the repository at this point in the history
  • Loading branch information
bichitra95 committed Apr 3, 2024
1 parent 27285ad commit 5c53ca1
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 211 deletions.
1 change: 1 addition & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ on:
- development
- master
- lineageondemand
- data-contract

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ public enum SupportedFileExtensions { XLSX, XLS, CSV }
public static final String ATTR_STARRED_DETAILS_LIST = "starredDetailsList";
public static final String ATTR_ASSET_STARRED_BY = "assetStarredBy";
public static final String ATTR_ASSET_STARRED_AT = "assetStarredAt";
public static final String ATTR_CERTIFICATE_STATUS = "certificateStatus";

public static final String STRUCT_STARRED_DETAILS = "StarredDetails";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1838,7 +1838,7 @@ public PreProcessor getPreProcessor(String typeName) {
break;

case CONTRACT_ENTITY_TYPE:
preProcessor = new ContractPreProcessor(graph, typeRegistry, entityRetriever, this);
preProcessor = new ContractPreProcessor(graph, typeRegistry, entityRetriever, this, entityGraphMapper);
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@
import org.apache.atlas.authorize.AtlasEntityAccessRequest;
import org.apache.atlas.authorize.AtlasPrivilege;
import org.apache.atlas.exception.AtlasBaseException;
import org.apache.atlas.model.TypeCategory;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.instance.AtlasEntityHeader;
import org.apache.atlas.model.instance.AtlasObjectId;
import org.apache.atlas.repository.graphdb.AtlasEdgeDirection;
import org.apache.atlas.repository.graphdb.AtlasGraph;
import org.apache.atlas.repository.graphdb.AtlasVertex;
import org.apache.atlas.repository.store.graph.v2.AtlasGraphUtilsV2;
import org.apache.atlas.repository.store.graph.v2.EntityGraphRetriever;
import org.apache.atlas.repository.store.graph.v2.preprocessor.PreProcessor;
import org.apache.atlas.repository.store.graph.v2.preprocessor.resource.AbstractResourcePreProcessor;
import org.apache.atlas.type.AtlasEntityType;
import org.apache.atlas.type.AtlasTypeRegistry;
import org.apache.atlas.utils.AtlasPerfMetrics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;

import static org.apache.atlas.AtlasErrorCode.INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND;
import static org.apache.atlas.AtlasErrorCode.TYPE_NAME_INVALID;
import static org.apache.atlas.repository.Constants.*;
import static org.apache.atlas.repository.Constants.ASSET_RELATION_ATTR;

public abstract class AbstractContractPreProcessor implements PreProcessor {
private static final Logger LOG = LoggerFactory.getLogger(AbstractResourcePreProcessor.class);
private static final Logger LOG = LoggerFactory.getLogger(AbstractContractPreProcessor.class);

public final AtlasTypeRegistry typeRegistry;
public final EntityGraphRetriever entityRetriever;
Expand All @@ -39,81 +41,40 @@ public abstract class AbstractContractPreProcessor implements PreProcessor {
this.entityRetriever = entityRetriever;
}

void authorizeResourceUpdate(AtlasEntity resourceEntity, AtlasVertex ResourceVertex, String edgeLabel) throws AtlasBaseException {
AtlasPerfMetrics.MetricRecorder metricRecorder = RequestContext.get().startMetricRecord("authorizeResourceUpdate");

void authorizeContractCreateOrUpdate(AtlasEntity contractEntity, AtlasEntity.AtlasEntityWithExtInfo associatedAsset) throws AtlasBaseException {
AtlasPerfMetrics.MetricRecorder metricRecorder = RequestContext.get().startMetricRecord("authorizeContractUpdate");
try {
AtlasEntityHeader assetEntity = null;

AtlasObjectId asset = getAssetRelationAttr(resourceEntity);
if (asset != null) {
//Found linked asset in payload
AtlasVertex assetVertex = entityRetriever.getEntityVertex(asset);
assetEntity = entityRetriever.toAtlasEntityHeaderWithClassifications(assetVertex);

} else {
//Check for linked asset in store
Iterator atlasVertexIterator = ResourceVertex.query()
.direction(AtlasEdgeDirection.IN)
.label(edgeLabel)
.has(STATE_PROPERTY_KEY, ACTIVE_STATE_VALUE)
.vertices()
.iterator();

if (atlasVertexIterator.hasNext()) {
//Found linked asset in store
AtlasVertex assetVertex = (AtlasVertex) atlasVertexIterator.next();
assetEntity = entityRetriever.toAtlasEntityHeaderWithClassifications(assetVertex);
}
}

if (assetEntity != null) {
//First authorize entity update access
verifyAssetAccess(assetEntity, AtlasPrivilege.ENTITY_UPDATE, resourceEntity, AtlasPrivilege.ENTITY_UPDATE);
} else {
//No linked asset to the Resource, check for resource update permission
verifyAccess(resourceEntity, AtlasPrivilege.ENTITY_UPDATE);
}
AtlasEntityHeader entityHeader = new AtlasEntityHeader(associatedAsset.getEntity());

//First authorize entity update access
verifyAssetAccess(entityHeader, AtlasPrivilege.ENTITY_UPDATE, contractEntity, AtlasPrivilege.ENTITY_UPDATE);

} finally {
RequestContext.get().endMetricRecord(metricRecorder);
}
}

void authorizeResourceDelete(AtlasVertex resourceVertex) throws AtlasBaseException {
AtlasPerfMetrics.MetricRecorder recorder = RequestContext.get().startMetricRecord("authorizeResourceDelete");

void authorizeContractDelete(AtlasVertex contractVertex, String typeName) throws AtlasBaseException {
AtlasPerfMetrics.MetricRecorder recorder = RequestContext.get().startMetricRecord("authorizeContractDelete");

try {
AtlasEntity resourceEntity = entityRetriever.toAtlasEntity(resourceVertex);

AtlasObjectId asset = getAssetRelationAttr(resourceEntity);
if (asset != null) {
AtlasEntityHeader assetEntity = entityRetriever.toAtlasEntityHeaderWithClassifications(asset.getGuid());
verifyAssetAccess(assetEntity, AtlasPrivilege.ENTITY_UPDATE, resourceEntity, AtlasPrivilege.ENTITY_DELETE);
} else {
//No linked asset to the Resource, check for resource delete permission
verifyAccess(resourceEntity, AtlasPrivilege.ENTITY_DELETE);
}
AtlasEntity contractEntity = entityRetriever.toAtlasEntity(contractVertex);
String contractQName = contractEntity.getAttribute(QUALIFIED_NAME).toString();
AtlasEntity.AtlasEntityWithExtInfo assetEntity = getAssociatedAsset(contractQName, typeName);
AtlasEntityHeader entityHeader = new AtlasEntityHeader(assetEntity.getEntity());

verifyAssetAccess(entityHeader, AtlasPrivilege.ENTITY_UPDATE, contractEntity, AtlasPrivilege.ENTITY_DELETE);
} finally {
RequestContext.get().endMetricRecord(recorder);
}
}

private AtlasObjectId getAssetRelationAttr(AtlasEntity entity) {
AtlasObjectId ret = null;

if (entity.hasRelationshipAttribute(ASSET_RELATION_ATTR) &&
entity.getRelationshipAttribute(ASSET_RELATION_ATTR) != null) {
ret = (AtlasObjectId) entity.getRelationshipAttribute(ASSET_RELATION_ATTR);
}

return ret;
}

private void verifyAssetAccess(AtlasEntityHeader asset, AtlasPrivilege assetPrivilege,
AtlasEntity resource, AtlasPrivilege resourcePrivilege) throws AtlasBaseException {
AtlasEntity contract, AtlasPrivilege contractPrivilege) throws AtlasBaseException {
verifyAccess(asset, assetPrivilege);
verifyAccess(resource, resourcePrivilege);
verifyAccess(contract, contractPrivilege);
}

private void verifyAccess(AtlasEntity entity, AtlasPrivilege privilege) throws AtlasBaseException {
Expand All @@ -125,5 +86,36 @@ private void verifyAccess(AtlasEntityHeader entityHeader, AtlasPrivilege privile
AtlasAuthorizationUtils.verifyAccess(new AtlasEntityAccessRequest(typeRegistry, privilege, entityHeader), errorMessage);
}

AtlasEntity.AtlasEntityWithExtInfo getAssociatedAsset(String contractQName, String typeName) throws AtlasBaseException {
String datasetQName = contractQName.substring(0, contractQName.lastIndexOf('/'));

Map<String, Object> uniqAttributes = new HashMap<>();
uniqAttributes.put(QUALIFIED_NAME, datasetQName);

AtlasEntityType entityType = ensureEntityType(typeName);

AtlasVertex entityVertex = AtlasGraphUtilsV2.getVertexByUniqueAttributes(graph, entityType, uniqAttributes);

EntityGraphRetriever entityRetriever = new EntityGraphRetriever(graph, typeRegistry, true);

AtlasEntity.AtlasEntityWithExtInfo ret = entityRetriever.toAtlasEntityWithExtInfo(entityVertex);

if (ret == null) {
throw new AtlasBaseException(INSTANCE_BY_UNIQUE_ATTRIBUTE_NOT_FOUND, entityType.getTypeName(),
uniqAttributes.toString());
}
return ret;
}

AtlasEntityType ensureEntityType(String typeName) throws AtlasBaseException {
AtlasEntityType ret = typeRegistry.getEntityTypeByName(typeName);

if (ret == null) {
throw new AtlasBaseException(TYPE_NAME_INVALID, TypeCategory.ENTITY.name(), typeName);
}

return ret;
}


}
Loading

0 comments on commit 5c53ca1

Please sign in to comment.