use of co.cask.cdap.proto.audit.AuditMessage in project cdap by caskdata.
the class DefaultAuditPublisher method publish.
@Override
public void publish(EntityId entityId, AuditType auditType, AuditPayload auditPayload) {
String userId = Objects.firstNonNull(SecurityRequestContext.getUserId(), "");
AuditMessage auditMessage = new AuditMessage(System.currentTimeMillis(), entityId, userId, auditType, auditPayload);
LOG.trace("Publishing audit message {}", auditMessage);
try {
MessagingServices.publishWithRetry(messagingService, auditTopic, retryStrategy, GSON.toJson(auditMessage).getBytes(StandardCharsets.UTF_8));
} catch (TopicNotFoundException e) {
LOG.error("Missing topic for audit publish: {}", auditTopic);
} catch (Exception e) {
LOG.error("Got exception publishing audit message {}. Exception:", auditMessage, e);
}
}
use of co.cask.cdap.proto.audit.AuditMessage in project cdap by caskdata.
the class InMemoryAuditPublisher method publish.
@Override
public void publish(EntityId entityId, AuditType auditType, AuditPayload auditPayload) {
AuditMessage auditMessage = new AuditMessage(0, entityId, "", auditType, auditPayload);
messages.add(auditMessage);
}
use of co.cask.cdap.proto.audit.AuditMessage in project cdap by caskdata.
the class MetadataStoreTest method testPublishing.
@Test
public void testPublishing() throws InterruptedException {
generateMetadataUpdates();
// Audit messages for metadata changes
List<AuditMessage> actualAuditMessages = new ArrayList<>();
for (AuditMessage auditMessage : auditPublisher.popMessages()) {
// Ignore system audit messages
if (auditMessage.getEntityId() instanceof NamespacedEntityId) {
String systemNs = NamespaceId.SYSTEM.getNamespace();
if (!((NamespacedEntityId) auditMessage.getEntityId()).getNamespace().equals(systemNs)) {
actualAuditMessages.add(auditMessage);
}
}
}
Assert.assertEquals(expectedAuditMessages, actualAuditMessages);
}
use of co.cask.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 co.cask.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();
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);
}
Aggregations