✨ Add CSV export for audit logs

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-03 19:11:51 +02:00
co-authored by Claude Opus 5.5
parent 4b069e66bc
commit 08c0b453cd
4 changed files with 162 additions and 0 deletions
@@ -0,0 +1,48 @@
package dev.dinauer.r9t.core.audit;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@ApplicationScoped
public class AuditLogCsvMapper
{
private static final String HEADER = "timestamp,action,resourceType,bucket,key,principal";
public String map(List<AuditLogEntry> input)
{
return Stream.concat(Stream.of(HEADER), input.stream().map(this::map))
.collect(Collectors.joining("\r\n", "", "\r\n"));
}
private String map(AuditLogEntry input)
{
return String.join(",",
csvField(input.timestamp().toString()),
csvField(input.action().name()),
csvField(input.resourceType().name()),
csvField(input.bucket()),
csvField(input.key()),
csvField(input.principal()));
}
private String csvField(String value)
{
if (value == null)
{
return "";
}
if (needsQuoting(value))
{
return "\"" + value.replace("\"", "\"\"") + "\"";
}
return value;
}
private boolean needsQuoting(String value)
{
return value.contains(",") || value.contains("\"") || value.contains("\n") || value.contains("\r");
}
}
@@ -18,10 +18,23 @@ public class AuditLogResource
@Inject @Inject
AuditLogJsonMapper auditLogJsonMapper; AuditLogJsonMapper auditLogJsonMapper;
@Inject
AuditLogCsvMapper auditLogCsvMapper;
@GET @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
public Response list() public Response list()
{ {
return Response.ok(auditLogJsonMapper.map(auditLogService.list())).build(); return Response.ok(auditLogJsonMapper.map(auditLogService.list())).build();
} }
@GET
@Path("/csv")
@Produces("text/csv")
public Response csv()
{
return Response.ok(auditLogCsvMapper.map(auditLogService.list()))
.header("Content-Disposition", "attachment; filename=\"audit-logs.csv\"")
.build();
}
} }
@@ -0,0 +1,29 @@
package dev.dinauer.r9t.core.audit;
import org.junit.jupiter.api.Test;
import java.time.Instant;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class AuditLogCsvMapperTest
{
private final AuditLogCsvMapper mapper = new AuditLogCsvMapper();
@Test
public void empty()
{
assertEquals("timestamp,action,resourceType,bucket,key,principal\r\n", mapper.map(List.of()));
}
@Test
public void escapesSpecialCharacters()
{
AuditLogEntry entry = new AuditLogEntry(Instant.parse("2026-10-03T12:00:00Z"), AuditAction.CREATE,
AuditResourceType.OBJECT, "bucket", "a,b \"c\"\nd.txt", null);
assertEquals("timestamp,action,resourceType,bucket,key,principal\r\n"
+ "2026-10-03T12:00:00Z,CREATE,OBJECT,bucket,\"a,b \"\"c\"\"\nd.txt\",\r\n", mapper.map(List.of(entry)));
}
}
@@ -0,0 +1,72 @@
package dev.dinauer.r9t.core.audit;
import dev.dinauer.r9t.core.auth.SigV4TestSigner;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@QuarkusTest
public class AuditLogCsvTest
{
private static final String BUCKET = "audit-csv-test-bucket";
private static final String KEY = "audit-csv-test.txt";
private static final String ACCESS_KEY = "testkey";
private static final String SECRET_KEY = "testsecret";
@TestHTTPResource
URI baseUri;
private final HttpClient client = HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build();
@Test
public void csv() throws Exception
{
send("PUT", baseUri.resolve(BUCKET), "".getBytes(StandardCharsets.UTF_8));
send("PUT", baseUri.resolve(BUCKET + "/" + KEY), "content".getBytes(StandardCharsets.UTF_8));
HttpResponse<String> response = send("GET", baseUri.resolve("_audit-logs/csv"), new byte[0]);
assertEquals(200, response.statusCode());
assertTrue(response.headers().firstValue("Content-Type").orElse("").startsWith("text/csv"));
assertEquals("attachment; filename=\"audit-logs.csv\"",
response.headers().firstValue("Content-Disposition").orElse(""));
String[] lines = response.body().split("\r\n");
assertEquals("timestamp,action,resourceType,bucket,key,principal", lines[0]);
assertTrue(lines[1].endsWith(",CREATE,OBJECT," + BUCKET + "," + KEY + "," + ACCESS_KEY));
assertTrue(lines[2].endsWith(",CREATE,BUCKET," + BUCKET + ",," + ACCESS_KEY));
}
@Test
public void unsignedRequestIsRejected() throws Exception
{
HttpResponse<Void> response = client.send(
HttpRequest.newBuilder(baseUri.resolve("_audit-logs/csv")).GET().build(),
HttpResponse.BodyHandlers.discarding());
assertEquals(401, response.statusCode());
}
@AfterEach
public void cleanup() throws Exception
{
send("DELETE", baseUri.resolve(BUCKET + "/" + KEY), new byte[0]);
send("DELETE", baseUri.resolve(BUCKET), new byte[0]);
}
private HttpResponse<String> send(String method, URI uri, byte[] body) throws Exception
{
HttpRequest.Builder builder = HttpRequest.newBuilder(uri)
.method(method, HttpRequest.BodyPublishers.ofByteArray(body));
SigV4TestSigner.sign(builder, method, uri, ACCESS_KEY, SECRET_KEY, body);
return client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
}
}