update GET range referrer header logic to V2

This commit is contained in:
Ahmar Suhail 2023-01-18 13:21:17 +00:00
parent 644a32f9d2
commit ce02671de4
3 changed files with 63 additions and 32 deletions

View File

@ -24,6 +24,7 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import software.amazon.awssdk.awscore.AwsExecutionAttribute;
import software.amazon.awssdk.core.SdkRequest; import software.amazon.awssdk.core.SdkRequest;
import software.amazon.awssdk.core.interceptor.Context; import software.amazon.awssdk.core.interceptor.Context;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
@ -261,21 +262,18 @@ public class LoggingAuditor
* Attach Range of data for GetObject Request. * Attach Range of data for GetObject Request.
* @param request given get object request * @param request given get object request
*/ */
// private void attachRangeFromRequest(AmazonWebServiceRequest request) { private void attachRangeFromRequest(SdkHttpRequest request,
// if (request instanceof GetObjectRequest) { ExecutionAttributes executionAttributes) {
// long[] rangeValue = ((GetObjectRequest) request).getRange();
// if (rangeValue == null || rangeValue.length == 0) { if (executionAttributes.getAttribute(AwsExecutionAttribute.OPERATION_NAME).equals("GetObject")) {
// return; if (request.headers() != null
// } && request.headers().get("Range") != null) {
// if (rangeValue.length != 2) { String rangeValue = request.headers().get("Range").get(0);
// WARN_INCORRECT_RANGE.warn("Expected range to contain 0 or 2 elements." String rangeHeader = rangeValue.split("=")[1];
// + " Got {} elements. Ignoring.", rangeValue.length); referrer.set(AuditConstants.PARAM_RANGE, rangeHeader);
// return; }
// } }
// String combinedRangeValue = String.format("%d-%d", rangeValue[0], rangeValue[1]); }
// referrer.set(AuditConstants.PARAM_RANGE, combinedRangeValue);
// }
// }
private final String description; private final String description;

View File

@ -22,10 +22,14 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.net.URI; import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import software.amazon.awssdk.awscore.AwsExecutionAttribute;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes; import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.InterceptorContext; import software.amazon.awssdk.core.interceptor.InterceptorContext;
import software.amazon.awssdk.http.SdkHttpMethod; import software.amazon.awssdk.http.SdkHttpMethod;
@ -162,14 +166,37 @@ public abstract class AbstractAuditingTest extends AbstractHadoopTestBase {
} }
/** /**
* Create a GetObject request and modify it before passing it through auditor. * Create a get request and pass it through the manager's beforeExecution()
* @param modifyRequest Consumer Interface for changing the request before passing to the auditor * callback.
* @return the request *
* @return a processed request.
*/ */
protected GetObjectRequest get(Consumer<GetObjectRequest> modifyRequest) { protected SdkHttpRequest get(String range) {
GetObjectRequest req = requestFactory.newGetObjectRequest("/"); GetObjectRequest.Builder getObjectRequestBuilder =
modifyRequest.accept(req); requestFactory.newGetObjectRequestBuilder("/");
return manager.beforeExecution(req);
SdkHttpRequest.Builder httpRequestBuilder =
SdkHttpRequest.builder().uri(URI.create("https://test")).method(SdkHttpMethod.GET);
if (!range.isEmpty()) {
getObjectRequestBuilder.range(range);
List<String> rangeHeader = new ArrayList<>();
rangeHeader.add(range);
Map<String, List<String>> headers = new HashMap<>();
headers.put("Range", rangeHeader);
httpRequestBuilder.headers(headers);
}
manager.requestCreated(getObjectRequestBuilder);
GetObjectRequest getObjectRequest = getObjectRequestBuilder.build();
ExecutionAttributes executionAttributes = ExecutionAttributes.builder().build().putAttribute(
AwsExecutionAttribute.OPERATION_NAME, "GetObject");
InterceptorContext context = InterceptorContext.builder()
.request(getObjectRequest)
.httpRequest(httpRequestBuilder.build())
.build();
manager.beforeExecution(context, executionAttributes);
return manager.modifyHttpRequest(context, executionAttributes);
} }
/** /**

View File

@ -308,13 +308,16 @@ public class TestHttpReferrerAuditHeader extends AbstractAuditingTest {
@Test @Test
public void testGetObjectRange() throws Throwable { public void testGetObjectRange() throws Throwable {
AuditSpan span = span(); AuditSpan span = span();
GetObjectRequest request = get(getObjectRequest -> getObjectRequest.setRange(100, 200)); SdkHttpRequest request = get("bytes=100-200");
Map<String, String> headers Map<String, List<String>> headers = request.headers();
= request.getCustomRequestHeaders();
assertThat(headers) assertThat(headers)
.describedAs("Custom headers") .describedAs("Custom headers")
.containsKey(HEADER_REFERRER); .containsKey(HEADER_REFERRER);
String header = headers.get(HEADER_REFERRER); List<String> headerValues = headers.get(HEADER_REFERRER);
assertThat(headerValues)
.describedAs("Multiple referrer headers")
.hasSize(1);
String header = headerValues.get(0);
LOG.info("Header is {}", header); LOG.info("Header is {}", header);
Map<String, String> params Map<String, String> params
= HttpReferrerAuditHeader.extractQueryParameters(header); = HttpReferrerAuditHeader.extractQueryParameters(header);
@ -327,13 +330,16 @@ public class TestHttpReferrerAuditHeader extends AbstractAuditingTest {
@Test @Test
public void testGetObjectWithoutRange() throws Throwable { public void testGetObjectWithoutRange() throws Throwable {
AuditSpan span = span(); AuditSpan span = span();
GetObjectRequest request = get(getObjectRequest -> {}); SdkHttpRequest request = get("");
Map<String, String> headers Map<String, List<String>> headers = request.headers();
= request.getCustomRequestHeaders();
assertThat(headers) assertThat(headers)
.describedAs("Custom headers") .describedAs("Custom headers")
.containsKey(HEADER_REFERRER); .containsKey(HEADER_REFERRER);
String header = headers.get(HEADER_REFERRER); List<String> headerValues = headers.get(HEADER_REFERRER);
assertThat(headerValues)
.describedAs("Multiple referrer headers")
.hasSize(1);
String header = headerValues.get(0);
LOG.info("Header is {}", header); LOG.info("Header is {}", header);
Map<String, String> params Map<String, String> params
= HttpReferrerAuditHeader.extractQueryParameters(header); = HttpReferrerAuditHeader.extractQueryParameters(header);