Search in sources :

Example 6 with AuditEntry

use of org.apache.druid.audit.AuditEntry in project druid by druid-io.

the class SQLAuditManagerTest method testAuditEntrySerde.

@Test(timeout = 60_000L)
public void testAuditEntrySerde() throws IOException {
    AuditEntry entry = new AuditEntry("testKey", "testType", new AuditInfo("testAuthor", "testComment", "127.0.0.1"), "testPayload", DateTimes.of("2013-01-01T00:00:00Z"));
    ObjectMapper mapper = new DefaultObjectMapper();
    AuditEntry serde = mapper.readValue(mapper.writeValueAsString(entry), AuditEntry.class);
    Assert.assertEquals(entry, serde);
}
Also used : AuditInfo(org.apache.druid.audit.AuditInfo) AuditEntry(org.apache.druid.audit.AuditEntry) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultObjectMapper(org.apache.druid.jackson.DefaultObjectMapper) Test(org.junit.Test)

Example 7 with AuditEntry

use of org.apache.druid.audit.AuditEntry in project druid by druid-io.

the class SQLAuditManagerTest method testAuditMetricEventBuilderConfig.

@Test
public void testAuditMetricEventBuilderConfig() {
    AuditEntry entry = new AuditEntry("testKey", "testType", new AuditInfo("testAuthor", "testComment", "127.0.0.1"), "testPayload", DateTimes.of("2013-01-01T00:00:00Z"));
    SQLAuditManager auditManagerWithPayloadAsDimension = new SQLAuditManager(connector, derbyConnectorRule.metadataTablesConfigSupplier(), new NoopServiceEmitter(), mapper, new SQLAuditManagerConfig() {

        @Override
        public boolean getIncludePayloadAsDimensionInMetric() {
            return true;
        }
    });
    ServiceMetricEvent.Builder auditEntryBuilder = ((SQLAuditManager) auditManager).getAuditMetricEventBuilder(entry);
    Assert.assertEquals(null, auditEntryBuilder.getDimension(PAYLOAD_DIMENSION_KEY));
    ServiceMetricEvent.Builder auditEntryBuilderWithPayload = auditManagerWithPayloadAsDimension.getAuditMetricEventBuilder(entry);
    Assert.assertEquals("testPayload", auditEntryBuilderWithPayload.getDimension(PAYLOAD_DIMENSION_KEY));
}
Also used : AuditInfo(org.apache.druid.audit.AuditInfo) AuditEntry(org.apache.druid.audit.AuditEntry) NoopServiceEmitter(org.apache.druid.server.metrics.NoopServiceEmitter) ServiceMetricEvent(org.apache.druid.java.util.emitter.service.ServiceMetricEvent) Test(org.junit.Test)

Example 8 with AuditEntry

use of org.apache.druid.audit.AuditEntry in project druid by druid-io.

the class SQLAuditManagerTest method testRemoveAuditLogsOlderThanWithEntryOlderThanTime.

@Test(timeout = 60_000L)
public void testRemoveAuditLogsOlderThanWithEntryOlderThanTime() throws IOException {
    String entry1Key = "testKey";
    String entry1Type = "testType";
    AuditInfo entry1AuditInfo = new AuditInfo("testAuthor", "testComment", "127.0.0.1");
    String entry1Payload = "testPayload";
    auditManager.doAudit(entry1Key, entry1Type, entry1AuditInfo, entry1Payload, stringConfigSerde);
    byte[] payload = connector.lookup(derbyConnectorRule.metadataTablesConfigSupplier().get().getAuditTable(), "audit_key", "payload", "testKey");
    AuditEntry dbEntry = mapper.readValue(payload, AuditEntry.class);
    Assert.assertEquals(entry1Key, dbEntry.getKey());
    Assert.assertEquals(entry1Payload, dbEntry.getPayload());
    Assert.assertEquals(entry1Type, dbEntry.getType());
    Assert.assertEquals(entry1AuditInfo, dbEntry.getAuditInfo());
    // Do delete
    auditManager.removeAuditLogsOlderThan(System.currentTimeMillis());
    // Verify the delete
    payload = connector.lookup(derbyConnectorRule.metadataTablesConfigSupplier().get().getAuditTable(), "audit_key", "payload", "testKey");
    Assert.assertNull(payload);
}
Also used : AuditInfo(org.apache.druid.audit.AuditInfo) AuditEntry(org.apache.druid.audit.AuditEntry) Test(org.junit.Test)

Example 9 with AuditEntry

use of org.apache.druid.audit.AuditEntry in project druid by druid-io.

the class SQLAuditManager method doAudit.

@Override
public void doAudit(AuditEntry auditEntry, Handle handle) throws IOException {
    emitter.emit(getAuditMetricEventBuilder(auditEntry).build("config/audit", 1));
    AuditEntry auditEntryToStore = auditEntry;
    if (config.getMaxPayloadSizeBytes() >= 0) {
        int payloadSize = jsonMapper.writeValueAsBytes(auditEntry.getPayload()).length;
        if (payloadSize > config.getMaxPayloadSizeBytes()) {
            auditEntryToStore = AuditEntry.builder().key(auditEntry.getKey()).type(auditEntry.getType()).auditInfo(auditEntry.getAuditInfo()).payload(StringUtils.format(PAYLOAD_SKIP_MSG_FORMAT, config.getMaxPayloadSizeBytes())).auditTime(auditEntry.getAuditTime()).build();
        }
    }
    handle.createStatement(StringUtils.format("INSERT INTO %s ( audit_key, type, author, comment, created_date, payload) VALUES (:audit_key, :type, :author, :comment, :created_date, :payload)", getAuditTable())).bind("audit_key", auditEntry.getKey()).bind("type", auditEntry.getType()).bind("author", auditEntry.getAuditInfo().getAuthor()).bind("comment", auditEntry.getAuditInfo().getComment()).bind("created_date", auditEntry.getAuditTime().toString()).bind("payload", jsonMapper.writeValueAsBytes(auditEntryToStore)).execute();
}
Also used : AuditEntry(org.apache.druid.audit.AuditEntry)

Example 10 with AuditEntry

use of org.apache.druid.audit.AuditEntry in project druid by druid-io.

the class SQLAuditManager method doAudit.

@Override
public <T> void doAudit(String key, String type, AuditInfo auditInfo, T payload, ConfigSerde<T> configSerde) {
    AuditEntry auditEntry = AuditEntry.builder().key(key).type(type).auditInfo(auditInfo).payload(configSerde.serializeToString(payload, config.isSkipNullField())).build();
    dbi.withHandle(new HandleCallback<Void>() {

        @Override
        public Void withHandle(Handle handle) throws Exception {
            doAudit(auditEntry, handle);
            return null;
        }
    });
}
Also used : AuditEntry(org.apache.druid.audit.AuditEntry) IOException(java.io.IOException) Handle(org.skife.jdbi.v2.Handle)

Aggregations

AuditEntry (org.apache.druid.audit.AuditEntry)19 AuditInfo (org.apache.druid.audit.AuditInfo)16 Test (org.junit.Test)16 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 Response (javax.ws.rs.core.Response)4 NoopServiceEmitter (org.apache.druid.server.metrics.NoopServiceEmitter)3 Interval (org.joda.time.Interval)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 IntervalLoadRule (org.apache.druid.server.coordinator.rules.IntervalLoadRule)2 Rule (org.apache.druid.server.coordinator.rules.Rule)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ResourceFilters (com.sun.jersey.spi.container.ResourceFilters)1 IOException (java.io.IOException)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)1 ServiceMetricEvent (org.apache.druid.java.util.emitter.service.ServiceMetricEvent)1 Handle (org.skife.jdbi.v2.Handle)1