Search in sources :

Example 26 with AuditInfo

use of org.apache.druid.audit.AuditInfo 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 27 with AuditInfo

use of org.apache.druid.audit.AuditInfo 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 28 with AuditInfo

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

the class KillCompactionConfig method run.

@Override
public DruidCoordinatorRuntimeParams run(DruidCoordinatorRuntimeParams params) {
    long currentTimeMillis = System.currentTimeMillis();
    if ((lastKillTime + period) < currentTimeMillis) {
        lastKillTime = currentTimeMillis;
        try {
            RetryUtils.retry(() -> {
                final byte[] currentBytes = CoordinatorCompactionConfig.getConfigInByteFromDb(connector, connectorConfig);
                final CoordinatorCompactionConfig current = CoordinatorCompactionConfig.convertByteToConfig(jacksonConfigManager, currentBytes);
                // If current compaction config is empty then there is nothing to do
                if (CoordinatorCompactionConfig.empty().equals(current)) {
                    log.info("Finished running KillCompactionConfig duty. Nothing to do as compaction config is already empty.");
                    emitMetric(params.getEmitter(), 0);
                    return ConfigManager.SetResult.ok();
                }
                // Get all active datasources
                // Note that we get all active datasources after getting compaction config to prevent race condition if new
                // datasource and config are added.
                Set<String> activeDatasources = sqlSegmentsMetadataManager.retrieveAllDataSourceNames();
                final Map<String, DataSourceCompactionConfig> updated = current.getCompactionConfigs().stream().filter(dataSourceCompactionConfig -> activeDatasources.contains(dataSourceCompactionConfig.getDataSource())).collect(Collectors.toMap(DataSourceCompactionConfig::getDataSource, Function.identity()));
                // Calculate number of compaction configs to remove for logging
                int compactionConfigRemoved = current.getCompactionConfigs().size() - updated.size();
                ConfigManager.SetResult result = jacksonConfigManager.set(CoordinatorCompactionConfig.CONFIG_KEY, currentBytes, CoordinatorCompactionConfig.from(current, ImmutableList.copyOf(updated.values())), new AuditInfo("KillCompactionConfig", "CoordinatorDuty for automatic deletion of compaction config", ""));
                if (result.isOk()) {
                    log.info("Finished running KillCompactionConfig duty. Removed %,d compaction configs", compactionConfigRemoved);
                    emitMetric(params.getEmitter(), compactionConfigRemoved);
                } else if (result.isRetryable()) {
                    // Failed but is retryable
                    log.debug("Retrying KillCompactionConfig duty");
                    throw new RetryableException(result.getException());
                } else {
                    // Failed and not retryable
                    log.error(result.getException(), "Failed to kill compaction configurations");
                    emitMetric(params.getEmitter(), 0);
                }
                return result;
            }, e -> e instanceof RetryableException, UPDATE_NUM_RETRY);
        } catch (Exception e) {
            log.error(e, "Failed to kill compaction configurations");
            emitMetric(params.getEmitter(), 0);
        }
    }
    return params;
}
Also used : Logger(org.apache.druid.java.util.common.logger.Logger) AuditInfo(org.apache.druid.audit.AuditInfo) MetadataStorageTablesConfig(org.apache.druid.metadata.MetadataStorageTablesConfig) ServiceMetricEvent(org.apache.druid.java.util.emitter.service.ServiceMetricEvent) Inject(com.google.inject.Inject) CoordinatorCompactionConfig(org.apache.druid.server.coordinator.CoordinatorCompactionConfig) DruidCoordinatorRuntimeParams(org.apache.druid.server.coordinator.DruidCoordinatorRuntimeParams) Set(java.util.Set) JacksonConfigManager(org.apache.druid.common.config.JacksonConfigManager) RetryableException(org.apache.druid.java.util.RetryableException) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) DataSourceCompactionConfig(org.apache.druid.server.coordinator.DataSourceCompactionConfig) MetadataStorageConnector(org.apache.druid.metadata.MetadataStorageConnector) ImmutableList(com.google.common.collect.ImmutableList) ConfigManager(org.apache.druid.common.config.ConfigManager) ServiceEmitter(org.apache.druid.java.util.emitter.service.ServiceEmitter) DruidCoordinatorConfig(org.apache.druid.server.coordinator.DruidCoordinatorConfig) Map(java.util.Map) Preconditions(com.google.common.base.Preconditions) SqlSegmentsMetadataManager(org.apache.druid.metadata.SqlSegmentsMetadataManager) RetryUtils(org.apache.druid.java.util.common.RetryUtils) AuditInfo(org.apache.druid.audit.AuditInfo) CoordinatorCompactionConfig(org.apache.druid.server.coordinator.CoordinatorCompactionConfig) JacksonConfigManager(org.apache.druid.common.config.JacksonConfigManager) ConfigManager(org.apache.druid.common.config.ConfigManager) RetryableException(org.apache.druid.java.util.RetryableException) RetryableException(org.apache.druid.java.util.RetryableException) DataSourceCompactionConfig(org.apache.druid.server.coordinator.DataSourceCompactionConfig)

Example 29 with AuditInfo

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

the class JacksonConfigManagerTest method testSet.

@Test
public void testSet() {
    String key = "key";
    TestConfig val = new TestConfig("version", "string", 3);
    AuditInfo auditInfo = new AuditInfo("testAuthor", "testComment", "127.0.0.1");
    jacksonConfigManager.set(key, val, auditInfo);
    ArgumentCaptor<ConfigSerde> configSerdeCapture = ArgumentCaptor.forClass(ConfigSerde.class);
    Mockito.verify(mockAuditManager).doAudit(ArgumentMatchers.eq(key), ArgumentMatchers.eq(key), ArgumentMatchers.eq(auditInfo), ArgumentMatchers.eq(val), configSerdeCapture.capture());
    Assert.assertNotNull(configSerdeCapture.getValue());
}
Also used : AuditInfo(org.apache.druid.audit.AuditInfo) Test(org.junit.Test)

Example 30 with AuditInfo

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

the class CoordinatorDynamicConfigsResource method setDynamicConfigs.

// default value is used for backwards compatibility
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response setDynamicConfigs(final CoordinatorDynamicConfig.Builder dynamicConfigBuilder, @HeaderParam(AuditManager.X_DRUID_AUTHOR) @DefaultValue("") final String author, @HeaderParam(AuditManager.X_DRUID_COMMENT) @DefaultValue("") final String comment, @Context HttpServletRequest req) {
    try {
        CoordinatorDynamicConfig current = CoordinatorDynamicConfig.current(manager);
        final SetResult setResult = manager.set(CoordinatorDynamicConfig.CONFIG_KEY, dynamicConfigBuilder.build(current), new AuditInfo(author, comment, req.getRemoteAddr()));
        if (setResult.isOk()) {
            return Response.ok().build();
        } else {
            return Response.status(Response.Status.BAD_REQUEST).entity(ServletResourceUtils.sanitizeException(setResult.getException())).build();
        }
    } catch (IllegalArgumentException e) {
        return Response.status(Response.Status.BAD_REQUEST).entity(ServletResourceUtils.sanitizeException(e)).build();
    }
}
Also used : AuditInfo(org.apache.druid.audit.AuditInfo) CoordinatorDynamicConfig(org.apache.druid.server.coordinator.CoordinatorDynamicConfig) SetResult(org.apache.druid.common.config.ConfigManager.SetResult) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Aggregations

AuditInfo (org.apache.druid.audit.AuditInfo)50 Test (org.junit.Test)45 Map (java.util.Map)17 AuditEntry (org.apache.druid.audit.AuditEntry)16 ImmutableMap (com.google.common.collect.ImmutableMap)14 Response (javax.ws.rs.core.Response)14 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 LookupCoordinatorManager (org.apache.druid.server.lookup.cache.LookupCoordinatorManager)10 ImmutableList (com.google.common.collect.ImmutableList)9 List (java.util.List)8 IntervalLoadRule (org.apache.druid.server.coordinator.rules.IntervalLoadRule)6 Rule (org.apache.druid.server.coordinator.rules.Rule)6 TypeReference (com.fasterxml.jackson.core.type.TypeReference)4 NoopServiceEmitter (org.apache.druid.server.metrics.NoopServiceEmitter)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HashMap (java.util.HashMap)3 POST (javax.ws.rs.POST)3 IOException (java.io.IOException)2 Consumes (javax.ws.rs.Consumes)2 Path (javax.ws.rs.Path)2