first commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
plugins { id("java-library-conventions") }
|
||||
dependencies {
|
||||
api(project(":gateway-module-audit:audit-contract-subjects"))
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package net.bquarkz.ai.gateway.audit.api;
|
||||
|
||||
import net.bquarkz.ai.gateway.audit.contracts.AuditEntry;
|
||||
import net.bquarkz.ai.gateway.audit.contracts.AuditQuery;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface AuditStore {
|
||||
void record(AuditEntry entry);
|
||||
|
||||
List<AuditEntry> query(AuditQuery query);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
plugins { id("java-library-conventions") }
|
||||
dependencies {
|
||||
api(project(":main-infrastructure"))
|
||||
api(project(":main-contract-subjects"))
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package net.bquarkz.ai.gateway.audit.contracts;
|
||||
|
||||
import net.bquarkz.ai.gateway.contracts.PolicyDecision;
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
import lombok.extern.jackson.Jacksonized;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
@Value
|
||||
@Builder
|
||||
@Jacksonized
|
||||
public class AuditEntry {
|
||||
OffsetDateTime timestamp;
|
||||
Tags tags;
|
||||
Fields fields;
|
||||
|
||||
@Value
|
||||
@Builder
|
||||
@Jacksonized
|
||||
public static class Tags {
|
||||
String agentId;
|
||||
String tool;
|
||||
String action;
|
||||
PolicyDecision decision;
|
||||
String policyRuleId;
|
||||
}
|
||||
|
||||
@Value
|
||||
@Builder
|
||||
@Jacksonized
|
||||
public static class Fields {
|
||||
String requestId;
|
||||
String reason;
|
||||
String arguments;
|
||||
String scopes;
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package net.bquarkz.ai.gateway.audit.contracts;
|
||||
|
||||
import net.bquarkz.ai.gateway.contracts.PolicyDecision;
|
||||
import lombok.Builder;
|
||||
import lombok.Value;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
@Value
|
||||
@Builder
|
||||
public class AuditQuery {
|
||||
@Builder.Default int limit = 20;
|
||||
OffsetDateTime from;
|
||||
OffsetDateTime to;
|
||||
// tag filters
|
||||
String tool;
|
||||
String agentId;
|
||||
PolicyDecision decision;
|
||||
String policyRuleId;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
plugins { id("java-library-conventions") }
|
||||
dependencies {
|
||||
implementation(project(":gateway-module-audit:audit-api"))
|
||||
implementation(Dependencies.jacksonDatabind)
|
||||
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
|
||||
implementation("org.springframework.boot:spring-boot-starter")
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package net.bquarkz.ai.gateway.audit.domain;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
import net.bquarkz.ai.gateway.audit.api.AuditStore;
|
||||
import net.bquarkz.ai.gateway.audit.contracts.AuditEntry;
|
||||
import net.bquarkz.ai.gateway.audit.contracts.AuditQuery;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class NdjsonAuditStore implements AuditStore {
|
||||
private static final Logger log = LoggerFactory.getLogger(NdjsonAuditStore.class);
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
private final Path filePath;
|
||||
|
||||
public NdjsonAuditStore(@Value("${gateway.audit.file-path:audit.ndjson}") String filePath) {
|
||||
this.filePath = Path.of(filePath);
|
||||
this.objectMapper = new ObjectMapper();
|
||||
this.objectMapper.registerModule(new JavaTimeModule());
|
||||
this.objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void record(AuditEntry entry) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(entry);
|
||||
Files.writeString(filePath, json + System.lineSeparator(),
|
||||
StandardOpenOption.CREATE, StandardOpenOption.APPEND);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to write audit entry", e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AuditEntry> query(AuditQuery query) {
|
||||
if (!Files.exists(filePath)) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
List<String> allLines = Files.readAllLines(filePath);
|
||||
LinkedList<AuditEntry> entries = new LinkedList<>();
|
||||
for (int i = allLines.size() - 1; i >= 0 && entries.size() < query.getLimit(); i--) {
|
||||
String line = allLines.get(i).trim();
|
||||
if (line.isEmpty()) continue;
|
||||
|
||||
AuditEntry entry = objectMapper.readValue(line, AuditEntry.class);
|
||||
if (matches(entry, query)) {
|
||||
entries.add(entry);
|
||||
}
|
||||
}
|
||||
return Collections.unmodifiableList(entries);
|
||||
} catch (IOException e) {
|
||||
log.error("Failed to read audit entries", e);
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean matches(AuditEntry entry, AuditQuery query) {
|
||||
if (query.getFrom() != null && entry.getTimestamp().isBefore(query.getFrom())) {
|
||||
return false;
|
||||
}
|
||||
if (query.getTo() != null && entry.getTimestamp().isAfter(query.getTo())) {
|
||||
return false;
|
||||
}
|
||||
AuditEntry.Tags tags = entry.getTags();
|
||||
if (query.getTool() != null && !query.getTool().equals(tags.getTool())) {
|
||||
return false;
|
||||
}
|
||||
if (query.getAgentId() != null && !query.getAgentId().equals(tags.getAgentId())) {
|
||||
return false;
|
||||
}
|
||||
if (query.getDecision() != null && query.getDecision() != tags.getDecision()) {
|
||||
return false;
|
||||
}
|
||||
if (query.getPolicyRuleId() != null && !query.getPolicyRuleId().equals(tags.getPolicyRuleId())) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user