Search in sources :

Example 1 with AuditType

use of io.cdap.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();
    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)

Example 2 with AuditType

use of io.cdap.cdap.proto.audit.AuditType in project cdap by caskdata.

the class AuditPublishTest method testPublish.

@Test
public void testPublish() throws Exception {
    String appName = AllProgramsApp.NAME;
    ApplicationId appId = NamespaceId.DEFAULT.app(appName);
    ApplicationSpecification spec = Specifications.from(new AllProgramsApp());
    // Define expected values
    Set<EntityId> expectedMetadataChangeEntities = new HashSet<>();
    // Metadata change on the artifact and app
    expectedMetadataChangeEntities.add(NamespaceId.DEFAULT.artifact(AllProgramsApp.class.getSimpleName(), "1"));
    expectedMetadataChangeEntities.add(appId);
    // All programs would have metadata change
    for (ProgramType programType : ProgramType.values()) {
        for (String programName : spec.getProgramsByType(programType)) {
            io.cdap.cdap.proto.ProgramType internalProgramType = io.cdap.cdap.proto.ProgramType.valueOf(programType.name());
            expectedMetadataChangeEntities.add(appId.program(internalProgramType, programName));
        }
    }
    // All dataset would have metadata change as well as creation
    Set<EntityId> expectedCreateEntities = new HashSet<>();
    for (String dataset : spec.getDatasets().keySet()) {
        expectedCreateEntities.add(NamespaceId.DEFAULT.dataset(dataset));
    }
    expectedMetadataChangeEntities.addAll(expectedCreateEntities);
    // TODO (CDAP-14733): Scheduler doesn't publish CREATE audit events. Once it does, we must expect them here, too.
    for (String schedule : spec.getProgramSchedules().keySet()) {
        expectedMetadataChangeEntities.add(appId.schedule(schedule));
    }
    Multimap<AuditType, EntityId> expectedAuditEntities = HashMultimap.create();
    expectedAuditEntities.putAll(AuditType.METADATA_CHANGE, expectedMetadataChangeEntities);
    expectedAuditEntities.putAll(AuditType.CREATE, expectedCreateEntities);
    // Deploy application
    AppFabricTestHelper.deployApplication(Id.Namespace.DEFAULT, AllProgramsApp.class, null, cConf);
    // Verify audit messages
    Tasks.waitFor(expectedAuditEntities, () -> {
        List<AuditMessage> publishedMessages = fetchAuditMessages();
        Multimap<AuditType, EntityId> actualAuditEntities = HashMultimap.create();
        for (AuditMessage message : publishedMessages) {
            EntityId entityId = EntityId.fromMetadataEntity(message.getEntity());
            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);
        }
        return actualAuditEntities;
    }, 5, TimeUnit.SECONDS);
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) AuditMessage(io.cdap.cdap.proto.audit.AuditMessage) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) AuditType(io.cdap.cdap.proto.audit.AuditType) AllProgramsApp(io.cdap.cdap.AllProgramsApp) NamespacedEntityId(io.cdap.cdap.proto.id.NamespacedEntityId) EntityId(io.cdap.cdap.proto.id.EntityId) NamespacedEntityId(io.cdap.cdap.proto.id.NamespacedEntityId) ProgramType(io.cdap.cdap.api.app.ProgramType) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

AuditMessage (io.cdap.cdap.proto.audit.AuditMessage)2 AuditType (io.cdap.cdap.proto.audit.AuditType)2 EntityId (io.cdap.cdap.proto.id.EntityId)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 MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)1 AuditPayload (io.cdap.cdap.proto.audit.AuditPayload)1 AccessPayload (io.cdap.cdap.proto.audit.payload.access.AccessPayload)1 MetadataPayload (io.cdap.cdap.proto.audit.payload.metadata.MetadataPayload)1 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)1 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)1 NamespacedEntityId (io.cdap.cdap.proto.id.NamespacedEntityId)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1