Skip to content

Commit

Permalink
Remove AuditRecord, use AuditEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
kfaraz committed Dec 5, 2023
1 parent ce41276 commit b466857
Show file tree
Hide file tree
Showing 19 changed files with 265 additions and 418 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.fasterxml.jackson.jaxrs.smile.SmileMediaTypes;
import com.google.inject.Inject;
import com.sun.jersey.spi.container.ResourceFilters;
import org.apache.druid.audit.AuditEvent;
import org.apache.druid.audit.AuditEntry;
import org.apache.druid.audit.AuditInfo;
import org.apache.druid.audit.AuditManager;
import org.apache.druid.guice.LazySingleton;
Expand Down Expand Up @@ -288,7 +288,7 @@ private boolean isSuccess(Response response)
private void performAudit(String authenticatorName, String action, Object payload, AuditInfo auditInfo)
{
auditManager.doAudit(
AuditEvent.builder()
AuditEntry.builder()
.key(authenticatorName)
.type(action)
.auditInfo(auditInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.google.common.collect.Maps;
import com.google.inject.Inject;
import com.sun.jersey.spi.container.ResourceFilters;
import org.apache.druid.audit.AuditEvent;
import org.apache.druid.audit.AuditEntry;
import org.apache.druid.audit.AuditInfo;
import org.apache.druid.audit.AuditManager;
import org.apache.druid.client.indexing.ClientTaskQuery;
Expand Down Expand Up @@ -230,7 +230,7 @@ public Response taskPost(
// Do an audit only if this API was called by a user and not by internal services
if (author != null && !author.isEmpty()) {
auditManager.doAudit(
AuditEvent.builder()
AuditEntry.builder()
.key(task.getDataSource())
.type("ingestion.batch")
.payload(new TaskIdentifier(task.getId(), task.getGroupId(), task.getType()))
Expand Down Expand Up @@ -586,7 +586,7 @@ public Response getWorkerConfigHistory(
Interval theInterval = interval == null ? null : Intervals.of(interval);
if (theInterval == null && count != null) {
try {
List<AuditEvent> workerEntryList = auditManager.fetchAuditHistory(
List<AuditEntry> workerEntryList = auditManager.fetchAuditHistory(
WorkerBehaviorConfig.CONFIG_KEY,
WorkerBehaviorConfig.CONFIG_KEY,
count
Expand All @@ -599,7 +599,7 @@ public Response getWorkerConfigHistory(
.build();
}
}
List<AuditEvent> workerEntryList = auditManager.fetchAuditHistory(
List<AuditEntry> workerEntryList = auditManager.fetchAuditHistory(
WorkerBehaviorConfig.CONFIG_KEY,
WorkerBehaviorConfig.CONFIG_KEY,
theInterval
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,34 @@

package org.apache.druid.audit;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.base.Preconditions;
import org.apache.druid.java.util.common.DateTimes;
import org.joda.time.DateTime;

import java.util.Objects;

/**
* Represents a single audit event with serialized payload.
* Serializable record of an audit event that can be persisted, logged or sent
* over REST APIs.
*/
public class AuditEvent
public class AuditEntry
{
private final String key;
private final String type;
private final AuditInfo auditInfo;
private final Payload payload;
private final DateTime auditTime;

private final String serializedPayload;
private final Object payload;

private AuditEvent(
String key,
String type,
AuditInfo authorInfo,
DateTime auditTime,
Object payload
)
{
this(key, type, authorInfo, payload, null, auditTime);
}

public AuditEvent(
String key,
String type,
AuditInfo authorInfo,
String serializedPayload,
DateTime auditTime
)
{
this(key, type, authorInfo, null, serializedPayload, auditTime);
}

public AuditEvent(
String key,
String type,
AuditInfo authorInfo,
Object payload,
String serializedPayload,
DateTime auditTime
@JsonCreator
public AuditEntry(
@JsonProperty("key") String key,
@JsonProperty("type") String type,
@JsonProperty("auditInfo") AuditInfo authorInfo,
@JsonProperty("payload") Payload payload,
@JsonProperty("auditTime") DateTime auditTime
)
{
Preconditions.checkNotNull(key, "key cannot be null");
Expand All @@ -76,45 +56,45 @@ public AuditEvent(
this.type = type;
this.auditInfo = authorInfo;
this.auditTime = auditTime == null ? DateTimes.nowUtc() : auditTime;
this.serializedPayload = serializedPayload;
this.payload = payload;
this.payload = payload == null ? Payload.fromString("") : payload;
}

@JsonProperty
public String getKey()
{
return key;
}

@JsonProperty
public String getType()
{
return type;
}

@JsonProperty
public AuditInfo getAuditInfo()
{
return auditInfo;
}

public Object getPayload()
/**
* Non-null payload of the audit event.
*/
@JsonProperty
public Payload getPayload()
{
return payload;
}

public String getPayloadAsString()
{
return serializedPayload;
}

/**
* @return audit time as DateTime
*/
@JsonProperty
public DateTime getAuditTime()
{
return auditTime;
}

public static Builder builder()
{
return new Builder();
}

@Override
public boolean equals(Object o)
{
Expand All @@ -125,19 +105,23 @@ public boolean equals(Object o)
return false;
}

AuditEvent that = (AuditEvent) o;
AuditEntry that = (AuditEntry) o;
return Objects.equals(this.auditTime, that.auditTime)
&& Objects.equals(this.key, that.key)
&& Objects.equals(this.type, that.type)
&& Objects.equals(this.auditInfo, that.auditInfo)
&& Objects.equals(this.payload, that.payload)
&& Objects.equals(this.serializedPayload, that.serializedPayload);
&& Objects.equals(this.payload, that.payload);
}

@Override
public int hashCode()
{
return Objects.hash(key, type, auditInfo, payload, serializedPayload, auditTime);
return Objects.hash(key, type, auditInfo, payload, auditTime);
}

public static Builder builder()
{
return new Builder();
}

public static class Builder
Expand Down Expand Up @@ -177,7 +161,7 @@ public Builder auditInfo(AuditInfo auditInfo)
return this;
}

public Builder payloadAsString(String serializedPayload)
public Builder serializedPayload(String serializedPayload)
{
this.serializedPayload = serializedPayload;
return this;
Expand All @@ -195,13 +179,65 @@ public Builder auditTime(DateTime auditTime)
return this;
}

public AuditEvent build()
public AuditEntry build()
{
return new AuditEntry(key, type, auditInfo, new Payload(serializedPayload, payload), auditTime);
}
}

public static class Payload
{
private final String serialized;
private final Object raw;

@JsonCreator
public static Payload fromString(String serialized)
{
return new Payload(serialized, null);
}

@JsonValue
@Override
public String toString()
{
return serialized;
}

private Payload(String serialized, Object raw)
{
this.serialized = serialized;
this.raw = raw;
}

public String asString()
{
return serialized;
}

public Object raw()
{
return raw;
}

@Override
public boolean equals(Object o)
{
if (payload != null) {
return new AuditEvent(key, type, auditInfo, auditTime, payload);
} else {
return new AuditEvent(key, type, auditInfo, serializedPayload, auditTime);
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

Payload that = (Payload) o;
return Objects.equals(this.serialized, that.serialized)
&& Objects.equals(this.raw, that.raw);
}

@Override
public int hashCode()
{
return Objects.hash(serialized, raw);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface AuditManager

String X_DRUID_COMMENT = "X-Druid-Comment";

void doAudit(AuditEvent event);
void doAudit(AuditEntry event);

/**
* Inserts an audit entry in audit table using the provided JDBI handle.
Expand All @@ -44,7 +44,7 @@ public interface AuditManager
* @param event Event to audit
* @param handle JDBI Handle representing connection to the database
*/
default void doAudit(AuditEvent event, Handle handle) throws IOException
default void doAudit(AuditEntry event, Handle handle) throws IOException
{
doAudit(event);
}
Expand All @@ -55,7 +55,7 @@ default void doAudit(AuditEvent event, Handle handle) throws IOException
*
* @return List of recorded audit events satisfying the passed parameters.
*/
List<AuditEvent> fetchAuditHistory(String key, String type, Interval interval);
List<AuditEntry> fetchAuditHistory(String key, String type, Interval interval);

/**
* Fetches audit entries of a type whose audit time lies in the given interval.
Expand All @@ -64,21 +64,21 @@ default void doAudit(AuditEvent event, Handle handle) throws IOException
* @param interval Eligible interval for audit time
* @return List of recorded audit events satisfying the passed parameters.
*/
List<AuditEvent> fetchAuditHistory(String type, Interval interval);
List<AuditEntry> fetchAuditHistory(String type, Interval interval);

/**
* Provides last N entries of audit history for given key, type
*
* @return list of recorded audit events satisfying the passed parameters
*/
List<AuditEvent> fetchAuditHistory(String key, String type, int limit);
List<AuditEntry> fetchAuditHistory(String key, String type, int limit);

/**
* Provides last N entries of audit history for given type
*
* @return List of recorded audit events satisfying the passed parameters.
*/
List<AuditEvent> fetchAuditHistory(String type, int limit);
List<AuditEntry> fetchAuditHistory(String type, int limit);

/**
* Remove audit logs created older than the given timestamp.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import org.apache.druid.audit.AuditEvent;
import org.apache.druid.audit.AuditEntry;
import org.apache.druid.audit.AuditInfo;
import org.apache.druid.audit.AuditManager;
import org.apache.druid.common.config.ConfigManager.SetResult;
Expand Down Expand Up @@ -117,7 +117,7 @@ public <T> SetResult set(
// Audit and actual config change are done in separate transactions
// there can be phantom audits and reOrdering in audit changes as well.
auditManager.doAudit(
AuditEvent.builder()
AuditEntry.builder()
.key(key)
.type(key)
.auditInfo(auditInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,28 @@ public void testAuditInfoEquality()
}

@Test(timeout = 60_000L)
public void testAuditEventEquality()
public void testAuditEntryEquality()
{
final AuditEvent event1 = new AuditEvent(
final AuditEntry event1 = new AuditEntry(
"testKey",
"testType",
new AuditInfo(
"testAuthor",
"testComment",
"127.0.0.1"
),
"testPayload",
AuditEntry.Payload.fromString("testPayload"),
DateTimes.of("2013-01-01T00:00:00Z")
);
final AuditEvent event2 = new AuditEvent(
final AuditEntry event2 = new AuditEntry(
"testKey",
"testType",
new AuditInfo(
"testAuthor",
"testComment",
"127.0.0.1"
),
"testPayload",
AuditEntry.Payload.fromString("testPayload"),
DateTimes.of("2013-01-01T00:00:00Z")
);
Assert.assertEquals(event1, event2);
Expand Down
Loading

0 comments on commit b466857

Please sign in to comment.