Skip to content

Commit

Permalink
Merge pull request opensearch-project#321 from gregschohn/RemoveAutho…
Browse files Browse the repository at this point in the history
…rizationHeaderFix

Two bugfixes to facilitate "--remove-auth-headers" (MIGRATIONS-1309).
  • Loading branch information
gregschohn authored Sep 13, 2023
2 parents 218e19a + 8df297b commit 35930f5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public List<String> put(String key, Object value) {
return strictHeadersMap.put(key, strList);
}

@Override
public List<String> remove(Object key) {
return strictHeadersMap.remove(key);
}


/**
* This is just casting the underlying object's entrySet. An old git commit will show this unrolled,
* but this should be significantly more efficient.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ private void handlePayloadNeutralTransformationOrThrow(ChannelHandlerContext ctx

private boolean headerFieldsAreIdentical(HttpRequest request, HttpJsonMessageWithFaultingPayload httpJsonMessage) {
if (!request.uri().equals(httpJsonMessage.path()) ||
!request.method().toString().equals(httpJsonMessage.method())) {
!request.method().toString().equals(httpJsonMessage.method()) ||
request.headers().size() != httpJsonMessage.headers().strictHeadersMap.size()) {
return false;
}
for (var headerName : httpJsonMessage.headers().keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.opensearch.migrations.transform.JsonCompositeTransformer;
import org.opensearch.migrations.transform.JsonJoltTransformer;
import org.opensearch.migrations.transform.IJsonTransformer;
import org.opensearch.migrations.transform.RemovingAuthTransformerFactory;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void testPassThroughSinglePacketWithoutBodyTransformationPost() throws Ex
null, testPacketCapture, "TEST", new UniqueRequestKey("testConnectionId", 0));
byte[] testBytes;
try (var sampleStream = HttpJsonTransformingConsumer.class.getResourceAsStream(
"/requests/raw/post_formUrlEncoded_withFixedLength.txt")) {
"/requests/raw/get_withAuthHeader.txt")) {
testBytes = sampleStream.readAllBytes();
testBytes = new String(testBytes, StandardCharsets.UTF_8)
.replace("foo.example", "test.domain")
Expand All @@ -62,6 +63,34 @@ public void testPassThroughSinglePacketWithoutBodyTransformationPost() throws Ex
Assertions.assertEquals(HttpRequestTransformationStatus.SKIPPED, returnedResponse.transformationStatus);
}

@Test
public void testRemoveAuthHeadersWorks() throws Exception {
final var dummyAggregatedResponse = new AggregatedRawResponse(17, null, null, null);
var testPacketCapture = new TestCapturePacketToHttpHandler(Duration.ofMillis(100), dummyAggregatedResponse);
var transformingHandler =
new HttpJsonTransformingConsumer<AggregatedRawResponse>(
JsonJoltTransformer.newBuilder()
.addHostSwitchOperation("test.domain")
.build(),
RemovingAuthTransformerFactory.instance,
testPacketCapture, "TEST", new UniqueRequestKey("testConnectionId", 0));
byte[] testBytes;
try (var sampleStream = HttpJsonTransformingConsumer.class.getResourceAsStream(
"/requests/raw/get_withAuthHeader.txt")) {
testBytes = sampleStream.readAllBytes();
testBytes = new String(testBytes, StandardCharsets.UTF_8)
.replace("foo.example", "test.domain")
.replace("auTHorization: Basic YWRtaW46YWRtaW4=\n", "")
.getBytes(StandardCharsets.UTF_8);
}
transformingHandler.consumeBytes(testBytes);
var returnedResponse = transformingHandler.finalizeRequest().get();
Assertions.assertEquals(new String(testBytes, StandardCharsets.UTF_8),
testPacketCapture.getCapturedAsString());
Assertions.assertArrayEquals(testBytes, testPacketCapture.getBytesCaptured());
Assertions.assertEquals(HttpRequestTransformationStatus.SKIPPED, returnedResponse.transformationStatus);
}

@Test
public void testPartialBodyThrowsAndIsRedriven() throws Exception {
final var dummyAggregatedResponse = new AggregatedRawResponse(17, null, null, null);
Expand Down Expand Up @@ -100,4 +129,6 @@ private void walkMaps(Object o) {
Assertions.assertInstanceOf(NettyJsonBodyAccumulateHandler.IncompleteJsonBodyException.class,
returnedResponse.error);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GET /test HTTP/1.1
Host: foo.example
auTHorization: Basic YWRtaW46YWRtaW4=
Content-Type: application/json

0 comments on commit 35930f5

Please sign in to comment.