Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Audit log PUT requests #9628

Merged
merged 1 commit into from May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -109,6 +109,7 @@ public int compareTo(@NotNull Entry that) {
public enum Method {
POST,
PATCH,
PUT,
DELETE
}

Expand Down
Expand Up @@ -60,6 +60,7 @@ private static String asString(AuditLog.Entry.Method method) {
switch (method) {
case POST: return "POST";
case PATCH: return "PATCH";
case PUT: return "PUT";
case DELETE: return "DELETE";
default: throw new IllegalArgumentException("No serialization defined for method " + method);
}
Expand All @@ -69,6 +70,7 @@ private static AuditLog.Entry.Method methodFrom(Inspector field) {
switch (field.asString()) {
case "POST": return AuditLog.Entry.Method.POST;
case "PATCH": return AuditLog.Entry.Method.PATCH;
case "PUT": return AuditLog.Entry.Method.PUT;
case "DELETE": return AuditLog.Entry.Method.DELETE;
default: throw new IllegalArgumentException("Unknown serialized value '" + field.asString() + "'");
}
Expand Down
Expand Up @@ -24,11 +24,10 @@
public class AuditLoggerTest {

private final ControllerTester tester = new ControllerTester();
private final Supplier<AuditLog> log = () -> tester.controller().auditLogger().readLog();

@Test
public void test_logging() {
Supplier<AuditLog> log = () -> tester.controller().auditLogger().readLog();

{ // GET request is ignored
HttpRequest request = testRequest(Method.GET, URI.create("http://localhost:8080/os/v1/"), "");
tester.controller().auditLogger().log(request);
Expand All @@ -40,11 +39,8 @@ public void test_logging() {
String data = "{\"cloud\":\"cloud9\",\"version\":\"42.0\"}";
HttpRequest request = testRequest(Method.PATCH, url, data);
tester.controller().auditLogger().log(request);

assertEquals(instant(), log.get().entries().get(0).at());
assertEntry(Entry.Method.PATCH, 1, "/os/v1/?foo=bar");
assertEquals("user", log.get().entries().get(0).principal());
assertEquals(Entry.Method.PATCH, log.get().entries().get(0).method());
assertEquals("/os/v1/?foo=bar", log.get().entries().get(0).resource());
assertEquals(data, log.get().entries().get(0).data().get());
}

Expand All @@ -53,25 +49,53 @@ public void test_logging() {
HttpRequest request = testRequest(Method.PATCH, URI.create("http://localhost:8080/os/v1/"),
"{\"cloud\":\"cloud9\",\"version\":\"43.0\"}");
tester.controller().auditLogger().log(request);
assertEquals(2, log.get().entries().size());
assertEquals(instant(), log.get().entries().get(0).at());
assertEquals("/os/v1/", log.get().entries().get(0).resource());
assertEntry(Entry.Method.PATCH, 2, "/os/v1/");
}

{ // PUT is logged
tester.clock().advance(Duration.ofDays(1));
HttpRequest request = testRequest(Method.PUT, URI.create("http://localhost:8080/zone/v2/prod/us-north-1/nodes/v2/state/dirty/node1/"),
"");
tester.controller().auditLogger().log(request);
assertEntry(Entry.Method.PUT, 3, "/zone/v2/prod/us-north-1/nodes/v2/state/dirty/node1/");
}

{ // DELETE is logged
tester.clock().advance(Duration.ofDays(1));
HttpRequest request = testRequest(Method.DELETE, URI.create("http://localhost:8080/zone/v2/prod/us-north-1/nodes/v2/node/node1"),
"");
tester.controller().auditLogger().log(request);
assertEntry(Entry.Method.DELETE, 4, "/zone/v2/prod/us-north-1/nodes/v2/node/node1");
}

{ // POST is logged
tester.clock().advance(Duration.ofDays(1));
HttpRequest request = testRequest(Method.POST, URI.create("http://localhost:8080/controller/v1/jobs/upgrader/confidence/6.42"),
"6.42");
tester.controller().auditLogger().log(request);
assertEntry(Entry.Method.POST, 5, "/controller/v1/jobs/upgrader/confidence/6.42");
}

{ // 14 days pass and another PATCH request is logged. Older entries are removed due to expiry
tester.clock().advance(Duration.ofDays(14));
HttpRequest request = testRequest(Method.PATCH, URI.create("http://localhost:8080/os/v1/"),
"{\"cloud\":\"cloud9\",\"version\":\"44.0\"}");
tester.controller().auditLogger().log(request);
assertEquals(1, log.get().entries().size());
assertEquals(instant(), log.get().entries().get(0).at());
assertEntry(Entry.Method.PATCH, 1, "/os/v1/");
}
}

private Instant instant() {
return tester.clock().instant().truncatedTo(MILLIS);
}

private void assertEntry(Entry.Method method, int logSize, String resource) {
assertEquals(logSize, log.get().entries().size());
assertEquals(instant(), log.get().entries().get(0).at());
assertEquals(method, log.get().entries().get(0).method());
assertEquals(resource, log.get().entries().get(0).resource());
}

private static HttpRequest testRequest(Method method, URI url, String data) {
HttpRequest request = HttpRequest.createTestRequest(
url.toString(),
Expand Down