Search in sources :

Example 1 with AuditType

use of co.cask.cdap.proto.audit.AuditType in project cdap by caskdata.

the class AuditPublishTest method testPublish.

@Test
public void testPublish() throws Exception {
    String defaultNs = NamespaceId.DEFAULT.getNamespace();
    String appName = WordCountApp.class.getSimpleName();
    // Define expected values
    Set<? extends EntityId> expectedMetadataChangeEntities = ImmutableSet.of(Ids.namespace(defaultNs).artifact(WordCountApp.class.getSimpleName(), "1"), Ids.namespace(defaultNs).app(appName), Ids.namespace(defaultNs).app(appName).flow(WordCountApp.WordCountFlow.class.getSimpleName()), Ids.namespace(defaultNs).app(appName).mr(WordCountApp.VoidMapReduceJob.class.getSimpleName()), Ids.namespace(defaultNs).app(appName).service(WordCountApp.WordFrequencyService.class.getSimpleName()), Ids.namespace(defaultNs).dataset("mydataset"), Ids.namespace(defaultNs).stream("text"));
    Multimap<AuditType, EntityId> expectedAuditEntities = HashMultimap.create();
    expectedAuditEntities.putAll(AuditType.METADATA_CHANGE, expectedMetadataChangeEntities);
    expectedAuditEntities.putAll(AuditType.CREATE, ImmutableSet.of(Ids.namespace(defaultNs).dataset("mydataset"), Ids.namespace(defaultNs).stream("text")));
    // Deploy application
    AppFabricTestHelper.deployApplication(Id.Namespace.DEFAULT, WordCountApp.class, null, cConf);
    // Verify audit messages
    List<AuditMessage> publishedMessages = fetchAuditMessages();
    Multimap<AuditType, EntityId> actualAuditEntities = HashMultimap.create();
    for (AuditMessage message : publishedMessages) {
        EntityId entityId = message.getEntityId();
        if (entityId instanceof NamespacedEntityId) {
            if (((NamespacedEntityId) entityId).getNamespace().equals(NamespaceId.SYSTEM.getNamespace())) {
                // Ignore system audit messages
                continue;
            }
        }
        if (entityId.getEntityType() == EntityType.ARTIFACT && entityId instanceof ArtifactId) {
            ArtifactId artifactId = (ArtifactId) entityId;
            // Version is dynamic for deploys in test cases
            entityId = Ids.namespace(artifactId.getNamespace()).artifact(artifactId.getArtifact(), "1");
        }
        actualAuditEntities.put(message.getType(), entityId);
    }
    Assert.assertEquals(expectedAuditEntities, actualAuditEntities);
}
Also used : NamespacedEntityId(co.cask.cdap.proto.id.NamespacedEntityId) EntityId(co.cask.cdap.proto.id.EntityId) AuditMessage(co.cask.cdap.proto.audit.AuditMessage) NamespacedEntityId(co.cask.cdap.proto.id.NamespacedEntityId) ArtifactId(co.cask.cdap.proto.id.ArtifactId) AuditType(co.cask.cdap.proto.audit.AuditType) WordCountApp(co.cask.cdap.WordCountApp) Test(org.junit.Test)

Example 2 with AuditType

use of co.cask.cdap.proto.audit.AuditType 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();
    EntityId entityId = context.deserialize(jsonObj.getAsJsonObject("entityId"), EntityId.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, entityId, user, auditType, payload);
}
Also used : EntityId(co.cask.cdap.proto.id.EntityId) AccessPayload(co.cask.cdap.proto.audit.payload.access.AccessPayload) AuditMessage(co.cask.cdap.proto.audit.AuditMessage) AuditType(co.cask.cdap.proto.audit.AuditType) JsonObject(com.google.gson.JsonObject) AuditPayload(co.cask.cdap.proto.audit.AuditPayload) MetadataPayload(co.cask.cdap.proto.audit.payload.metadata.MetadataPayload)

Aggregations

AuditMessage (co.cask.cdap.proto.audit.AuditMessage)2 AuditType (co.cask.cdap.proto.audit.AuditType)2 EntityId (co.cask.cdap.proto.id.EntityId)2 WordCountApp (co.cask.cdap.WordCountApp)1 AuditPayload (co.cask.cdap.proto.audit.AuditPayload)1 AccessPayload (co.cask.cdap.proto.audit.payload.access.AccessPayload)1 MetadataPayload (co.cask.cdap.proto.audit.payload.metadata.MetadataPayload)1 ArtifactId (co.cask.cdap.proto.id.ArtifactId)1 NamespacedEntityId (co.cask.cdap.proto.id.NamespacedEntityId)1 JsonObject (com.google.gson.JsonObject)1 Test (org.junit.Test)1