Skip to content

Commit

Permalink
Update audit log tests
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Oct 2, 2023
1 parent 3b2925b commit 0829c94
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,52 +1,20 @@
package org.opensearch.security.http;

import org.opensearch.common.Nullable;
import org.opensearch.common.io.stream.BytesStreamOutput;
import org.opensearch.core.xcontent.MediaType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.rest.RestChannel;
import org.opensearch.rest.AbstractRestChannel;
import org.opensearch.rest.RestRequest;
import org.opensearch.rest.RestResponse;

import java.io.IOException;

public class InterceptingRestChannel implements RestChannel {
public class InterceptingRestChannel extends AbstractRestChannel {
private RestResponse interceptedResponse;

public InterceptingRestChannel() {}
public InterceptingRestChannel(RestRequest request, boolean detailedErrorsEnabled) {
super(request, detailedErrorsEnabled);
}

public RestResponse getInterceptedResponse() {
return this.interceptedResponse;
}

public XContentBuilder newBuilder() throws IOException {
throw new UnsupportedOperationException("Operation not supported");
}

public XContentBuilder newErrorBuilder() throws IOException {
throw new UnsupportedOperationException("Operation not supported");
}

public XContentBuilder newBuilder(@Nullable MediaType mediaType, boolean useFiltering) throws IOException {
throw new UnsupportedOperationException("Operation not supported");
}

public XContentBuilder newBuilder(MediaType mediaType, MediaType responseContentType, boolean useFiltering) throws IOException {
throw new UnsupportedOperationException("Operation not supported");
}

public BytesStreamOutput bytesOutput() {
throw new UnsupportedOperationException("Operation not supported");
}

public RestRequest request() {
throw new UnsupportedOperationException("Operation not supported");
}

public boolean detailedErrorsEnabled() {
throw new UnsupportedOperationException("Operation not supported");
}

public void sendResponse(RestResponse response) {
this.interceptedResponse = response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest msg) thro
final Netty4DefaultHttpRequest httpRequest = new Netty4DefaultHttpRequest(msg);
RestRequest restRequest = AbstractHttpServerTransport.createRestRequest(xContentRegistry, httpRequest, httpChannel);

InterceptingRestChannel interceptingRestChannel = new InterceptingRestChannel();
InterceptingRestChannel interceptingRestChannel = new InterceptingRestChannel(restRequest, false);
ThreadContext threadContext = threadPool.getThreadContext();
try (ThreadContext.StoredContext ignore = threadPool.getThreadContext().stashContext()) {
boolean isUnauthenticated = restFilter.checkAndAuthenticateRequest(restRequest, interceptingRestChannel, threadContext);
Expand All @@ -69,7 +69,8 @@ public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest msg) thro
} else {
ctx.channel().attr(SHOULD_DECOMPRESS).set(Boolean.TRUE);
}
} finally {
ctx.fireChannelRead(msg);
}
ctx.fireChannelRead(msg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -510,12 +510,15 @@ public void testUpdateSettings() throws Exception {
+ "}"
+ "}";

String expectedRequestBodyLog =
"{\\\"persistent_settings\\\":{\\\"indices\\\":{\\\"recovery\\\":{\\\"*\\\":null}}},\\\"transient_settings\\\":{\\\"indices\\\":{\\\"recovery\\\":{\\\"*\\\":null}}}}";

HttpResponse response = rh.executePutRequest("_cluster/settings", json, encodeBasicHeader("admin", "admin"));
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusCode());
String auditLogImpl = TestAuditlogImpl.sb.toString();
Assert.assertTrue(auditLogImpl.contains("AUTHENTICATED"));
Assert.assertTrue(auditLogImpl.contains("cluster:admin/settings/update"));
Assert.assertTrue(auditLogImpl.contains("indices.recovery.*"));
Assert.assertTrue(auditLogImpl.contains(expectedRequestBodyLog));
// may vary because we log may hit cluster manager directly or not
Assert.assertTrue(TestAuditlogImpl.messages.size() > 1);
Assert.assertTrue(validateMsgs(TestAuditlogImpl.messages));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

import java.nio.file.Path;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
Expand Down

0 comments on commit 0829c94

Please sign in to comment.