use of io.cdap.cdap.proto.audit.AuditMessage in project cdap by caskdata.
the class AuditPublishTest method fetchAuditMessages.
private List<AuditMessage> fetchAuditMessages() throws TopicNotFoundException, IOException {
List<AuditMessage> result = new ArrayList<>();
try (CloseableIterator<RawMessage> iterator = messagingService.prepareFetch(auditTopic).fetch()) {
while (iterator.hasNext()) {
RawMessage message = iterator.next();
result.add(GSON.fromJson(new String(message.getPayload(), StandardCharsets.UTF_8), AuditMessage.class));
}
}
return result;
}
use of io.cdap.cdap.proto.audit.AuditMessage 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.AuditMessage in project cdap by caskdata.
the class InMemoryAuditPublisher method publish.
@Override
public void publish(MetadataEntity metadataEntity, AuditType auditType, AuditPayload auditPayload) {
AuditMessage auditMessage = new AuditMessage(0, metadataEntity, "", auditType, auditPayload);
messages.add(auditMessage);
}
use of io.cdap.cdap.proto.audit.AuditMessage in project cdap by caskdata.
the class SystemMetadataAuditPublishTest method addAllSystemMetadata.
private int addAllSystemMetadata(Set<String> allMetadata) {
for (AuditMessage auditMessage : getMetadataUpdateMessages()) {
AuditPayload payload = auditMessage.getPayload();
Assert.assertTrue(payload instanceof MetadataPayload);
MetadataPayload metadataPayload = (MetadataPayload) payload;
Map<MetadataScope, Metadata> additions = metadataPayload.getAdditions();
if (additions.containsKey(MetadataScope.SYSTEM)) {
allMetadata.addAll(additions.get(MetadataScope.SYSTEM).getProperties().keySet());
allMetadata.addAll(additions.get(MetadataScope.SYSTEM).getTags());
}
Map<MetadataScope, Metadata> deletions = metadataPayload.getDeletions();
if (deletions.containsKey(MetadataScope.SYSTEM)) {
allMetadata.addAll(deletions.get(MetadataScope.SYSTEM).getProperties().keySet());
allMetadata.addAll(deletions.get(MetadataScope.SYSTEM).getTags());
}
}
return allMetadata.size();
}
use of io.cdap.cdap.proto.audit.AuditMessage in project cdap by caskdata.
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);
}
Aggregations