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);
}
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);
}
Aggregations