Search in sources :

Example 1 with AuditMessage

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;
}
Also used : AuditMessage(io.cdap.cdap.proto.audit.AuditMessage) ArrayList(java.util.ArrayList) RawMessage(io.cdap.cdap.messaging.data.RawMessage)

Example 2 with AuditMessage

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);
}
Also used : AuditMessage(io.cdap.cdap.proto.audit.AuditMessage) Table(io.cdap.cdap.api.dataset.table.Table) ArrayList(java.util.ArrayList) AuthenticationTestContext(io.cdap.cdap.security.auth.context.AuthenticationTestContext) NoOpUsageRegistry(io.cdap.cdap.data2.registry.NoOpUsageRegistry) ProgramId(io.cdap.cdap.proto.id.ProgramId) LineageWriterDatasetFramework(io.cdap.cdap.data2.metadata.writer.LineageWriterDatasetFramework) LineageWriterDatasetFramework(io.cdap.cdap.data2.metadata.writer.LineageWriterDatasetFramework) InMemoryTableModule(io.cdap.cdap.data2.dataset2.module.lib.inmemory.InMemoryTableModule) AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessPayload) NoOpLineageWriter(io.cdap.cdap.data2.metadata.writer.NoOpLineageWriter) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) NoOpAccessController(io.cdap.cdap.security.spi.authorization.NoOpAccessController) Test(org.junit.Test)

Example 3 with AuditMessage

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);
}
Also used : AuditMessage(io.cdap.cdap.proto.audit.AuditMessage)

Example 4 with 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();
}
Also used : AuditMessage(io.cdap.cdap.proto.audit.AuditMessage) Metadata(io.cdap.cdap.api.metadata.Metadata) AuditPayload(io.cdap.cdap.proto.audit.AuditPayload) MetadataPayload(io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope)

Example 5 with AuditMessage

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);
}
Also used : EntityId(io.cdap.cdap.proto.id.EntityId) MetadataEntity(io.cdap.cdap.api.metadata.MetadataEntity) AccessPayload(io.cdap.cdap.proto.audit.payload.access.AccessPayload) AuditMessage(io.cdap.cdap.proto.audit.AuditMessage) AuditType(io.cdap.cdap.proto.audit.AuditType) JsonObject(com.google.gson.JsonObject) AuditPayload(io.cdap.cdap.proto.audit.AuditPayload) MetadataPayload(io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload)

Aggregations

AuditMessage (io.cdap.cdap.proto.audit.AuditMessage)9 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 AuditPayload (io.cdap.cdap.proto.audit.AuditPayload)2 AuditType (io.cdap.cdap.proto.audit.AuditType)2 AccessPayload (io.cdap.cdap.proto.audit.payload.access.AccessPayload)2 MetadataPayload (io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload)2 EntityId (io.cdap.cdap.proto.id.EntityId)2 ProgramId (io.cdap.cdap.proto.id.ProgramId)2 JsonObject (com.google.gson.JsonObject)1 AllProgramsApp (io.cdap.cdap.AllProgramsApp)1 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)1 ProgramType (io.cdap.cdap.api.app.ProgramType)1 Table (io.cdap.cdap.api.dataset.table.Table)1 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)1 Metadata (io.cdap.cdap.api.metadata.Metadata)1 MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)1 MetadataScope (io.cdap.cdap.api.metadata.MetadataScope)1 InMemoryTableModule (io.cdap.cdap.data2.dataset2.module.lib.inmemory.InMemoryTableModule)1 LineageWriterDatasetFramework (io.cdap.cdap.data2.metadata.writer.LineageWriterDatasetFramework)1