use of io.cdap.cdap.proto.audit.payload.access.AccessPayload in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testAuditPublish.
@Test
public void testAuditPublish() throws Exception {
// Clear all audit messages
inMemoryAuditPublisher.popMessages();
List<AuditMessage> expectedMessages = new ArrayList<>();
// Adding modules
DatasetFramework framework = getFramework();
framework.addModule(IN_MEMORY, new InMemoryTableModule());
// Creating instances
framework.addInstance(Table.class.getName(), MY_TABLE, DatasetProperties.EMPTY);
expectedMessages.add(new AuditMessage(0, MY_TABLE, "", AuditType.CREATE, AuditPayload.EMPTY_PAYLOAD));
framework.addInstance(Table.class.getName(), MY_TABLE2, DatasetProperties.EMPTY);
expectedMessages.add(new AuditMessage(0, MY_TABLE2, "", AuditType.CREATE, AuditPayload.EMPTY_PAYLOAD));
// Update instance
framework.updateInstance(MY_TABLE, DatasetProperties.EMPTY);
expectedMessages.add(new AuditMessage(0, MY_TABLE, "", AuditType.UPDATE, AuditPayload.EMPTY_PAYLOAD));
// Access instance
ProgramRunId runId = new ProgramId("ns", "app", ProgramType.WORKER, "worker").run(RunIds.generate().getId());
LineageWriterDatasetFramework lineageFramework = new LineageWriterDatasetFramework(framework, new NoOpLineageWriter(), new NoOpUsageRegistry(), new AuthenticationTestContext(), new NoOpAccessController());
lineageFramework.setContext(new TestProgramContext(runId));
lineageFramework.setAuditPublisher(inMemoryAuditPublisher);
lineageFramework.getDataset(MY_TABLE, ImmutableMap.<String, String>of(), getClass().getClassLoader());
expectedMessages.add(new AuditMessage(0, MY_TABLE, "", AuditType.ACCESS, new AccessPayload(AccessType.UNKNOWN, runId)));
// Truncate instance
framework.truncateInstance(MY_TABLE);
expectedMessages.add(new AuditMessage(0, MY_TABLE, "", AuditType.TRUNCATE, AuditPayload.EMPTY_PAYLOAD));
// Delete instance
framework.deleteInstance(MY_TABLE);
expectedMessages.add(new AuditMessage(0, MY_TABLE, "", AuditType.DELETE, AuditPayload.EMPTY_PAYLOAD));
// Delete all instances in a namespace
framework.deleteAllInstances(MY_TABLE2.getParent());
expectedMessages.add(new AuditMessage(0, MY_TABLE2, "", AuditType.DELETE, AuditPayload.EMPTY_PAYLOAD));
Assert.assertEquals(expectedMessages, inMemoryAuditPublisher.popMessages());
// cleanup
framework.deleteModule(IN_MEMORY);
}
use of io.cdap.cdap.proto.audit.payload.access.AccessPayload in project cdap by caskdata.
the class AuditMessageTest method testAccessMessage.
@Test
public void testAccessMessage() throws Exception {
String workerAccessJson = "{\"version\":2,\"time\":2000,\"metadataEntity\":{\"details\":{\"namespace\":\"ns1\",\"dataset\":\"ds1\"}," + "\"type\":\"dataset\"},\"user\":\"user1\",\"type\":\"ACCESS\",\"payload\":{\"accessType\":\"WRITE\"," + "\"accessor\":{\"namespace\":\"ns1\",\"application\":\"app1\",\"version\":\"v1\",\"type\":\"Worker\"," + "\"program\":\"worker1\",\"run\":\"run1\",\"entity\":\"PROGRAM_RUN\"}}}";
AuditMessage workerAccess = new AuditMessage(2000L, new NamespaceId("ns1").dataset("ds1"), "user1", AuditType.ACCESS, new AccessPayload(AccessType.WRITE, new NamespaceId("ns1").app("app1", "v1").worker("worker1").run("run1")));
Assert.assertEquals(jsonToMap(workerAccessJson), jsonToMap(GSON.toJson(workerAccess)));
Assert.assertEquals(workerAccess, GSON.fromJson(workerAccessJson, AuditMessage.class));
String exploreAccessJson = "{\"version\":2,\"time\":2500,\"metadataEntity\":{\"details\":{\"namespace\":\"ns1\",\"dataset\":\"ds1\"}," + "\"type\":\"dataset\"},\"user\":\"user1\",\"type\":\"ACCESS\",\"payload\":{\"accessType\":\"UNKNOWN\"," + "\"accessor\":{\"service\":\"explore\",\"entity\":\"SYSTEM_SERVICE\"}}}";
AuditMessage exploreAccess = new AuditMessage(2500L, new NamespaceId("ns1").dataset("ds1"), "user1", AuditType.ACCESS, new AccessPayload(AccessType.UNKNOWN, new SystemServiceId("explore")));
Assert.assertEquals(jsonToMap(exploreAccessJson), jsonToMap(GSON.toJson(exploreAccess)));
Assert.assertEquals(exploreAccess, GSON.fromJson(exploreAccessJson, AuditMessage.class));
}
use of io.cdap.cdap.proto.audit.payload.access.AccessPayload in project cdap by caskdata.
the class AuditPublishers method publishAccess.
/**
* Publish access audit information using {@link AuditPublisher}.
*
* @param publisher audit publisher, if null no audit information is published
* @param entityId entity id for which audit information is being published
* @param accessType access type
* @param accessor the entity accessing entityId
*/
public static void publishAccess(@Nullable AuditPublisher publisher, EntityId entityId, AccessType accessType, EntityId accessor) {
if (publisher == null) {
logWarning();
return;
}
AccessAuditInfo accessAuditInfo = new AccessAuditInfo(accessor, entityId, accessType);
synchronized (CACHE_AUDIT_LOGS) {
if (CACHE_AUDIT_LOGS.getIfPresent(accessAuditInfo) != null) {
// this access has already been published recently (since it is present in the cache). hence don't publish again
return;
}
CACHE_AUDIT_LOGS.put(accessAuditInfo, true);
}
switch(accessType) {
case READ:
publisher.publish(entityId, AuditType.ACCESS, new AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessType.READ, accessor));
break;
case WRITE:
publisher.publish(entityId, AuditType.ACCESS, new AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessType.WRITE, accessor));
break;
case READ_WRITE:
publisher.publish(entityId, AuditType.ACCESS, new AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessType.READ, accessor));
publisher.publish(entityId, AuditType.ACCESS, new AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessType.WRITE, accessor));
break;
case UNKNOWN:
publisher.publish(entityId, AuditType.ACCESS, new AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessType.UNKNOWN, accessor));
break;
}
}
Aggregations