forked from apache/atlas
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2937 from atlanhq/data-contract
DQ-22: Data contract PreProcessor
- Loading branch information
Showing
7 changed files
with
742 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
...e/atlas/repository/store/graph/v2/preprocessor/contract/AbstractContractPreProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.apache.atlas.repository.store.graph.v2.preprocessor.contract; | ||
|
||
import org.apache.atlas.RequestContext; | ||
import org.apache.atlas.authorize.AtlasAuthorizationUtils; | ||
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.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.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.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.*; | ||
|
||
public abstract class AbstractContractPreProcessor implements PreProcessor { | ||
private static final Logger LOG = LoggerFactory.getLogger(AbstractContractPreProcessor.class); | ||
|
||
public final AtlasTypeRegistry typeRegistry; | ||
public final EntityGraphRetriever entityRetriever; | ||
public final AtlasGraph graph; | ||
|
||
|
||
AbstractContractPreProcessor(AtlasGraph graph, AtlasTypeRegistry typeRegistry, | ||
EntityGraphRetriever entityRetriever) { | ||
this.graph = graph; | ||
this.typeRegistry = typeRegistry; | ||
this.entityRetriever = entityRetriever; | ||
} | ||
|
||
void authorizeContractCreateOrUpdate(AtlasEntity contractEntity, AtlasEntity.AtlasEntityWithExtInfo associatedAsset) throws AtlasBaseException { | ||
AtlasPerfMetrics.MetricRecorder metricRecorder = RequestContext.get().startMetricRecord("authorizeContractUpdate"); | ||
try { | ||
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); | ||
} | ||
} | ||
|
||
|
||
private void verifyAssetAccess(AtlasEntityHeader asset, AtlasPrivilege assetPrivilege, | ||
AtlasEntity contract, AtlasPrivilege contractPrivilege) throws AtlasBaseException { | ||
verifyAccess(asset, assetPrivilege); | ||
verifyAccess(contract, contractPrivilege); | ||
} | ||
|
||
private void verifyAccess(AtlasEntity entity, AtlasPrivilege privilege) throws AtlasBaseException { | ||
verifyAccess(new AtlasEntityHeader(entity), privilege); | ||
} | ||
|
||
private void verifyAccess(AtlasEntityHeader entityHeader, AtlasPrivilege privilege) throws AtlasBaseException { | ||
String errorMessage = privilege.name() + " entity: " + entityHeader.getTypeName(); | ||
AtlasAuthorizationUtils.verifyAccess(new AtlasEntityAccessRequest(typeRegistry, privilege, entityHeader), errorMessage); | ||
} | ||
|
||
AtlasEntity.AtlasEntityWithExtInfo getAssociatedAsset(String datasetQName, String typeName) throws AtlasBaseException { | ||
|
||
Map<String, Object> uniqAttributes = new HashMap<>(); | ||
uniqAttributes.put(QUALIFIED_NAME, datasetQName); | ||
|
||
AtlasEntityType entityType = ensureEntityType(typeName); | ||
|
||
AtlasVertex entityVertex = AtlasGraphUtilsV2.getVertexByUniqueAttributes(graph, entityType, uniqAttributes); | ||
|
||
return entityRetriever.toAtlasEntityWithExtInfo(entityVertex); | ||
} | ||
|
||
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; | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.