REQUEST_HEADERS_TO_KEEP =
Sets.newHashSet(
"content-type",
+ "accept-encoding",
"last-modified",
"etag",
"prefer",
diff --git a/server/src/main/java/com/google/fhir/gateway/interfaces/AccessDecision.java b/server/src/main/java/com/google/fhir/gateway/interfaces/AccessDecision.java
index abc269ee..a9c9e748 100644
--- a/server/src/main/java/com/google/fhir/gateway/interfaces/AccessDecision.java
+++ b/server/src/main/java/com/google/fhir/gateway/interfaces/AccessDecision.java
@@ -17,6 +17,7 @@
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
import java.io.IOException;
+import javax.annotation.Nullable;
import org.apache.http.HttpResponse;
public interface AccessDecision {
@@ -28,6 +29,20 @@ public interface AccessDecision {
void preProcess(ServletRequestDetails servletRequestDetails);
+ /**
+ * Allows the incoming request mutation based on the access decision.
+ *
+ * Response is used to mutate the incoming request before executing the FHIR operation. We
+ * currently only support query parameters update for GET Http method. This is expected to be
+ * called after checking the access using @canAccess method. Mutating the request before checking
+ * access can have side effect of wrong access check.
+ *
+ * @param requestDetailsReader details about the resource and operation requested
+ * @return mutation to be applied on the incoming request or null if no mutation required
+ */
+ @Nullable
+ RequestMutation getRequestMutation(RequestDetailsReader requestDetailsReader);
+
/**
* Depending on the outcome of the FHIR operations, this does any post-processing operations that
* are related to access policies. This is expected to be called only if the actual FHIR operation
diff --git a/server/src/main/java/com/google/fhir/gateway/interfaces/NoOpAccessDecision.java b/server/src/main/java/com/google/fhir/gateway/interfaces/NoOpAccessDecision.java
index b04a07f1..135e1059 100644
--- a/server/src/main/java/com/google/fhir/gateway/interfaces/NoOpAccessDecision.java
+++ b/server/src/main/java/com/google/fhir/gateway/interfaces/NoOpAccessDecision.java
@@ -26,6 +26,11 @@ public NoOpAccessDecision(boolean accessGranted) {
this.accessGranted = accessGranted;
}
+ @Override
+ public RequestMutation getRequestMutation(RequestDetailsReader requestDetailsReader) {
+ return null;
+ }
+
@Override
public boolean canAccess() {
return accessGranted;
diff --git a/server/src/main/java/com/google/fhir/gateway/interfaces/RequestMutation.java b/server/src/main/java/com/google/fhir/gateway/interfaces/RequestMutation.java
new file mode 100644
index 00000000..fdcac3dd
--- /dev/null
+++ b/server/src/main/java/com/google/fhir/gateway/interfaces/RequestMutation.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2021-2023 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.google.fhir.gateway.interfaces;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import lombok.Builder;
+import lombok.Getter;
+
+/** Defines mutations that can be applied to the incoming request by an {@link AccessChecker}. */
+@Builder
+@Getter
+public class RequestMutation {
+
+ // Additional query parameters and list of values for a parameter that should be added to the
+ // outgoing FHIR request.
+ // New values overwrites the old one if there is a conflict for a request param (i.e. a returned
+ // parameter in RequestMutation is already present in the original request).
+ // Old parameter values should be explicitly retained while mutating values for that parameter.
+ @Builder.Default Map> queryParams = new HashMap<>();
+}
diff --git a/server/src/test/java/com/google/fhir/gateway/BearerAuthorizationInterceptorTest.java b/server/src/test/java/com/google/fhir/gateway/BearerAuthorizationInterceptorTest.java
index c481fce3..8327a159 100644
--- a/server/src/test/java/com/google/fhir/gateway/BearerAuthorizationInterceptorTest.java
+++ b/server/src/test/java/com/google/fhir/gateway/BearerAuthorizationInterceptorTest.java
@@ -16,6 +16,7 @@
package com.google.fhir.gateway;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.arrayContainingInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.mockito.ArgumentMatchers.any;
@@ -31,6 +32,7 @@
import ca.uhn.fhir.rest.server.exceptions.AuthenticationException;
import ca.uhn.fhir.rest.server.exceptions.ForbiddenOperationException;
import ca.uhn.fhir.rest.server.servlet.ServletRequestDetails;
+import ca.uhn.fhir.rest.server.servlet.ServletRestfulResponse;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
@@ -42,6 +44,7 @@
import com.google.fhir.gateway.interfaces.AccessDecision;
import com.google.fhir.gateway.interfaces.NoOpAccessDecision;
import com.google.fhir.gateway.interfaces.RequestDetailsReader;
+import com.google.fhir.gateway.interfaces.RequestMutation;
import com.google.gson.Gson;
import java.io.IOException;
import java.io.StringWriter;
@@ -56,8 +59,11 @@
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.Base64;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.hl7.fhir.instance.model.api.IBaseResource;
@@ -71,6 +77,7 @@
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import org.springframework.mock.web.MockHttpServletResponse;
@RunWith(MockitoJUnitRunner.class)
public class BearerAuthorizationInterceptorTest {
@@ -352,4 +359,73 @@ public void deniedRequest() throws IOException {
testInstance.authorizeRequest(requestMock);
}
+
+ @Test
+ public void mutateRequest() {
+ ServletRequestDetails requestDetails = new ServletRequestDetails();
+ requestDetails.addParameter("param1", new String[] {"param1-value1"});
+ requestDetails.addParameter("param2", new String[] {"param2-value1"});
+
+ HashMap> paramMutations = new HashMap<>();
+ paramMutations.put("param1", List.of("param1-value2"));
+ paramMutations.put("param3", List.of("param3-value1", "param3-value2"));
+ AccessDecision mutableAccessDecision =
+ new AccessDecision() {
+ public boolean canAccess() {
+ return true;
+ }
+
+ public void preProcess(ServletRequestDetails servletRequestDetails) {}
+
+ public RequestMutation getRequestMutation(RequestDetailsReader requestDetailsReader) {
+ return RequestMutation.builder().queryParams(paramMutations).build();
+ }
+
+ public String postProcess(HttpResponse response) throws IOException {
+ return null;
+ }
+ };
+
+ testInstance.mutateRequest(requestDetails, mutableAccessDecision);
+
+ assertThat(
+ requestDetails.getParameters().get("param1"), arrayContainingInAnyOrder("param1-value2"));
+ assertThat(
+ requestDetails.getParameters().get("param2"), arrayContainingInAnyOrder("param2-value1"));
+ assertThat(
+ requestDetails.getParameters().get("param3"),
+ arrayContainingInAnyOrder("param3-value2", "param3-value1"));
+ }
+
+ @Test
+ public void shouldSendGzippedResponseWhenRequested() throws IOException {
+ testInstance = createTestInstance(true, null);
+ String responseJson = "{\"resourceType\": \"Bundle\"}";
+ JWTCreator.Builder jwtBuilder = JWT.create().withIssuer(TOKEN_ISSUER);
+ when(requestMock.getHeader("Authorization")).thenReturn("Bearer " + signJwt(jwtBuilder));
+ when(requestMock.getHeader("Accept-Encoding".toLowerCase())).thenReturn("gzip");
+
+ // requestMock.getResponse() {@link ServletRequestDetails#getResponse()} is an abstraction HAPI
+ // provides to access the response object which is of type ServletRestfulResponse {@link
+ // ServletRestfulResponse}. Internally HAPI uses the HttpServletResponse {@link
+ // HttpServletResponse} object to perform any response related operations for this wrapper class
+ // ServletRestfulResponse. We have to perform mocking at two levels: one with
+ // requestMock.getResponse() because this is how we access the wrapper response object and write
+ // to it. We also need to perform a deeper level mock using requestMock.getServletResponse()
+ // {@link ServletRequestDetails#getServletResponse()} for the internal HAPI operations to be
+ // performed successfully. This complication arises from us mocking the request object. Had the
+ // object been not mocked, and set by a server we would not have needed to do this levels of
+ // mocks.
+ when(requestMock.getServer()).thenReturn(serverMock);
+ ServletRestfulResponse proxyResponseMock = new ServletRestfulResponse(requestMock);
+ when(requestMock.getResponse()).thenReturn(proxyResponseMock);
+ HttpServletResponse proxyServletResponseMock = new MockHttpServletResponse();
+ when(requestMock.getServletResponse()).thenReturn(proxyServletResponseMock);
+ TestUtil.setUpFhirResponseMock(fhirResponseMock, responseJson);
+
+ testInstance.authorizeRequest(requestMock);
+
+ assertThat(
+ proxyServletResponseMock.getHeader("Content-Encoding".toLowerCase()), equalTo("gzip"));
+ }
}