use of io.cdap.cdap.proto.id.EntityId in project cdap by caskdata.
the class AuthorizationTest method deployDummyAppWithImpersonation.
private void deployDummyAppWithImpersonation(NamespaceMeta nsMeta, @Nullable String appOwner) throws Exception {
NamespaceId namespaceId = nsMeta.getNamespaceId();
ApplicationId dummyAppId = namespaceId.app(DummyApp.class.getSimpleName());
ArtifactId artifactId = namespaceId.artifact(DummyApp.class.getSimpleName(), "1.0-SNAPSHOT");
DatasetId datasetId = namespaceId.dataset("whom");
DatasetTypeId datasetTypeId = namespaceId.datasetType(KeyValueTable.class.getName());
String owner = appOwner != null ? appOwner : nsMeta.getConfig().getPrincipal();
KerberosPrincipalId principalId = new KerberosPrincipalId(owner);
Principal principal = new Principal(owner, Principal.PrincipalType.USER);
DatasetId dummyDatasetId = namespaceId.dataset("customDataset");
DatasetTypeId dummyTypeId = namespaceId.datasetType(DummyApp.CustomDummyDataset.class.getName());
DatasetModuleId dummyModuleId = namespaceId.datasetModule((DummyApp.CustomDummyDataset.class.getName()));
// these are the privileges that are needed to deploy the app if no impersonation is involved,
// can check testApps() for more info
Map<EntityId, Set<? extends Permission>> neededPrivileges = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(dummyAppId, EnumSet.of(StandardPermission.GET, StandardPermission.CREATE)).put(artifactId, EnumSet.of(StandardPermission.CREATE)).put(datasetId, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET)).put(datasetTypeId, EnumSet.of(StandardPermission.UPDATE)).put(principalId, EnumSet.of(AccessPermission.SET_OWNER)).put(dummyDatasetId, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET)).put(dummyTypeId, EnumSet.of(StandardPermission.UPDATE)).put(dummyModuleId, EnumSet.of(StandardPermission.UPDATE)).build();
setUpPrivilegeAndRegisterForDeletion(ALICE, neededPrivileges);
// add the artifact
addAppArtifact(artifactId, DummyApp.class);
AppRequest<? extends Config> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion()), null, appOwner);
try {
deployApplication(dummyAppId, appRequest);
Assert.fail();
} catch (Exception e) {
// expected
}
// revoke privileges on datasets from alice, she does not need these privileges to deploy the app
// the owner will need these privileges to deploy
revokeAndAssertSuccess(datasetId);
revokeAndAssertSuccess(datasetTypeId);
revokeAndAssertSuccess(dummyDatasetId);
revokeAndAssertSuccess(dummyTypeId);
revokeAndAssertSuccess(dummyModuleId);
// grant privileges to owner
grantAndAssertSuccess(namespaceId, principal, EnumSet.of(StandardPermission.GET));
grantAndAssertSuccess(datasetId, principal, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET));
grantAndAssertSuccess(datasetTypeId, principal, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET));
grantAndAssertSuccess(dummyDatasetId, principal, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET));
grantAndAssertSuccess(dummyTypeId, principal, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET));
grantAndAssertSuccess(dummyModuleId, principal, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET));
// this time it should be successful
deployApplication(dummyAppId, appRequest);
// clean up the privilege on the owner principal id
revokeAndAssertSuccess(principalId);
}
use of io.cdap.cdap.proto.id.EntityId in project cdap by caskdata.
the class AuthorizationTest method testCrossNSSystemDatasetAccessWithAuthSpark.
private void testCrossNSSystemDatasetAccessWithAuthSpark(SparkManager sparkManager) throws Exception {
addDatasetInstance(NamespaceId.SYSTEM.dataset("table1"), "keyValueTable").create();
addDatasetInstance(NamespaceId.SYSTEM.dataset("table2"), "keyValueTable").create();
NamespaceMeta otherNS = new NamespaceMeta.Builder().setName("otherNS").build();
NamespaceId otherNSId = otherNS.getNamespaceId();
DatasetId otherTableId = otherNSId.dataset("otherTable");
Map<EntityId, Set<? extends Permission>> neededPrivileges = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(otherNSId, EnumSet.of(StandardPermission.GET, StandardPermission.CREATE, StandardPermission.DELETE)).put(otherTableId, EnumSet.of(StandardPermission.GET, StandardPermission.CREATE, StandardPermission.DELETE)).put(otherNSId.datasetType("keyValueTable"), EnumSet.of(StandardPermission.UPDATE)).build();
setUpPrivilegeAndRegisterForDeletion(ALICE, neededPrivileges);
getNamespaceAdmin().create(otherNS);
addDatasetInstance(otherTableId, "keyValueTable").create();
addDummyData(NamespaceId.SYSTEM, "table1");
// give privilege to BOB on all the datasets
grantAndAssertSuccess(NamespaceId.SYSTEM.dataset("table1"), BOB, EnumSet.of(StandardPermission.GET));
grantAndAssertSuccess(NamespaceId.SYSTEM.dataset("table2"), BOB, EnumSet.of(StandardPermission.UPDATE));
grantAndAssertSuccess(otherNS.getNamespaceId().dataset("otherTable"), BOB, ALL_STANDARD_PERMISSIONS);
// Switch to Bob and run the spark program. this will fail because bob is trying to read from a system dataset
SecurityRequestContext.setUserId(BOB.getName());
Map<String, String> args = ImmutableMap.of(TestSparkCrossNSDatasetApp.INPUT_DATASET_NAMESPACE, NamespaceId.SYSTEM.getNamespace(), TestSparkCrossNSDatasetApp.INPUT_DATASET_NAME, "table1", TestSparkCrossNSDatasetApp.OUTPUT_DATASET_NAMESPACE, otherNS.getNamespaceId().getNamespace(), TestSparkCrossNSDatasetApp.OUTPUT_DATASET_NAME, "otherTable");
assertProgramFailure(args, sparkManager);
assertDatasetIsEmpty(otherNS.getNamespaceId(), "otherTable");
// try running spark job with valid input namespace but writing to system namespace this should fail too
args = ImmutableMap.of(TestSparkCrossNSDatasetApp.INPUT_DATASET_NAMESPACE, otherNS.getNamespaceId().getNamespace(), TestSparkCrossNSDatasetApp.INPUT_DATASET_NAME, "otherTable", TestSparkCrossNSDatasetApp.OUTPUT_DATASET_NAMESPACE, NamespaceId.SYSTEM.getNamespace(), TestSparkCrossNSDatasetApp.OUTPUT_DATASET_NAME, "table2");
addDummyData(otherNS.getNamespaceId(), "otherTable");
assertProgramFailure(args, sparkManager);
assertDatasetIsEmpty(NamespaceId.SYSTEM, "table2");
// switch to back to ALICE
SecurityRequestContext.setUserId(ALICE.getName());
// cleanup
deleteDatasetInstance(NamespaceId.SYSTEM.dataset("table1"));
deleteDatasetInstance(NamespaceId.SYSTEM.dataset("table2"));
getNamespaceAdmin().delete(otherNS.getNamespaceId());
}
use of io.cdap.cdap.proto.id.EntityId in project cdap by caskdata.
the class AuthorizationTest method testCrossNSDatasetAccessWithAuthMapReduce.
private void testCrossNSDatasetAccessWithAuthMapReduce(MapReduceManager mrManager) throws Exception {
NamespaceMeta inputDatasetNS = new NamespaceMeta.Builder().setName("inputNS").build();
NamespaceId inputDatasetNSId = inputDatasetNS.getNamespaceId();
NamespaceMeta outputDatasetNS = new NamespaceMeta.Builder().setName("outputNS").build();
NamespaceId outputDatasetNSId = outputDatasetNS.getNamespaceId();
DatasetId table1Id = inputDatasetNSId.dataset("table1");
DatasetId table2Id = outputDatasetNSId.dataset("table2");
Map<EntityId, Set<? extends Permission>> neededPrivileges = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(inputDatasetNSId, EnumSet.allOf(StandardPermission.class)).put(outputDatasetNSId, EnumSet.allOf(StandardPermission.class)).put(table1Id, EnumSet.allOf(StandardPermission.class)).put(table2Id, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET, StandardPermission.DELETE)).put(inputDatasetNSId.datasetType("keyValueTable"), EnumSet.of(StandardPermission.UPDATE)).put(outputDatasetNSId.datasetType("keyValueTable"), EnumSet.of(StandardPermission.UPDATE)).build();
setUpPrivilegeAndRegisterForDeletion(ALICE, neededPrivileges);
getNamespaceAdmin().create(inputDatasetNS);
getNamespaceAdmin().create(outputDatasetNS);
addDatasetInstance(table1Id, "keyValueTable").create();
addDatasetInstance(table2Id, "keyValueTable").create();
addDummyData(inputDatasetNSId, "table1");
Map<String, String> argsForMR = ImmutableMap.of(DatasetCrossNSAccessWithMAPApp.INPUT_DATASET_NS, inputDatasetNS.getNamespaceId().getNamespace(), DatasetCrossNSAccessWithMAPApp.INPUT_DATASET_NAME, "table1", DatasetCrossNSAccessWithMAPApp.OUTPUT_DATASET_NS, outputDatasetNS.getNamespaceId().getNamespace(), DatasetCrossNSAccessWithMAPApp.OUTPUT_DATASET_NAME, "table2");
// Switch to BOB and run the mapreduce job. The job will fail at the runtime since BOB does not have permission
// on the input and output datasets in another namespaces.
SecurityRequestContext.setUserId(BOB.getName());
assertProgramFailure(argsForMR, mrManager);
// Switch back to Alice
SecurityRequestContext.setUserId(ALICE.getName());
// Verify nothing write to the output dataset
assertDatasetIsEmpty(outputDatasetNS.getNamespaceId(), "table2");
// give privilege to BOB on the input dataset
grantAndAssertSuccess(inputDatasetNS.getNamespaceId().dataset("table1"), BOB, EnumSet.of(StandardPermission.GET));
// switch back to bob and try running again. this will still fail since bob does not have access on the output
// dataset
SecurityRequestContext.setUserId(BOB.getName());
assertProgramFailure(argsForMR, mrManager);
// Switch back to Alice
SecurityRequestContext.setUserId(ALICE.getName());
// Verify nothing write to the output dataset
assertDatasetIsEmpty(outputDatasetNS.getNamespaceId(), "table2");
// give privilege to BOB on the output dataset
grantAndAssertSuccess(outputDatasetNS.getNamespaceId().dataset("table2"), BOB, EnumSet.of(StandardPermission.GET, StandardPermission.UPDATE));
// switch back to BOB and run MR again. this should work
SecurityRequestContext.setUserId(BOB.getName());
mrManager.start(argsForMR);
mrManager.waitForRun(ProgramRunStatus.COMPLETED, 60, TimeUnit.SECONDS);
// Verify results as alice
SecurityRequestContext.setUserId(ALICE.getName());
verifyDummyData(outputDatasetNS.getNamespaceId(), "table2");
getNamespaceAdmin().delete(inputDatasetNS.getNamespaceId());
getNamespaceAdmin().delete(outputDatasetNS.getNamespaceId());
}
use of io.cdap.cdap.proto.id.EntityId in project cdap by cdapio.
the class AuditMessageTypeAdapter method deserialize.
@Override
public AuditMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObj = json.getAsJsonObject();
long timeMillis = jsonObj.get("time").getAsLong();
MetadataEntity metadataEntity;
EntityId entityId = context.deserialize(jsonObj.getAsJsonObject("entityId"), EntityId.class);
if (entityId != null) {
metadataEntity = entityId.toMetadataEntity();
} else {
metadataEntity = context.deserialize(jsonObj.getAsJsonObject("metadataEntity"), MetadataEntity.class);
}
String user = jsonObj.get("user").getAsString();
AuditType auditType = context.deserialize(jsonObj.getAsJsonPrimitive("type"), AuditType.class);
AuditPayload payload;
JsonObject jsonPayload = jsonObj.getAsJsonObject("payload");
switch(auditType) {
case METADATA_CHANGE:
payload = context.deserialize(jsonPayload, MetadataPayload.class);
break;
case ACCESS:
payload = context.deserialize(jsonPayload, AccessPayload.class);
break;
default:
payload = AuditPayload.EMPTY_PAYLOAD;
}
return new AuditMessage(timeMillis, metadataEntity, user, auditType, payload);
}
use of io.cdap.cdap.proto.id.EntityId in project cdap by cdapio.
the class EntityIdTypeAdapterTest method testSerializeEntityIdWithNoParent.
@Test
public void testSerializeEntityIdWithNoParent() {
NamespaceId namespaceEntity = new NamespaceId("test_namespace");
String serialized = gson.toJson(namespaceEntity);
EntityId deserializedEntity = gson.fromJson(serialized, EntityId.class);
Assert.assertEquals(namespaceEntity, deserializedEntity);
}
Aggregations