Skip to content

Commit d0e2752

Browse files
author
Dennis Kieselhorst
authored
Merge pull request #491 from driverpt/add-alb-to-http-v2
Add ALB Context to API GW Payload V2
2 parents 4e85edc + 4bf3876 commit d0e2752

File tree

9 files changed

+51
-16
lines changed

9 files changed

+51
-16
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/jaxrs/AwsProxySecurityContext.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414

1515
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
1616
import com.amazonaws.serverless.proxy.model.CognitoAuthorizerClaims;
17+
import com.amazonaws.serverless.proxy.model.RequestSource;
1718
import com.amazonaws.services.lambda.runtime.Context;
1819

1920
import javax.ws.rs.core.SecurityContext;
2021

2122
import java.security.Principal;
2223

23-
import static com.amazonaws.serverless.proxy.model.AwsProxyRequest.RequestSource.API_GATEWAY;
24-
25-
2624
/**
2725
* default implementation of the <code>SecurityContext</code> object. This class supports 3 API Gateway's authorization methods:
2826
* AWS_IAM, CUSTOM_AUTHORIZER, and COGNITO_USER_POOL (oidc). The Principal returned by the object depends on the authorization

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletRequest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
1919
import com.amazonaws.serverless.proxy.model.ContainerConfig;
2020
import com.amazonaws.serverless.proxy.model.Headers;
21+
import com.amazonaws.serverless.proxy.model.RequestSource;
2122
import com.amazonaws.services.lambda.runtime.Context;
2223

2324
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -49,7 +50,6 @@
4950
import java.util.stream.Collectors;
5051
import java.util.stream.Stream;
5152

52-
5353
/**
5454
* Implementation of the <code>HttpServletRequest</code> interface that supports <code>AwsProxyRequest</code> object.
5555
* This object is initialized with an <code>AwsProxyRequest</code> event and a <code>SecurityContext</code> generated
@@ -204,7 +204,7 @@ public String getQueryString() {
204204
return this.generateQueryString(
205205
request.getMultiValueQueryStringParameters(),
206206
// ALB does not automatically decode parameters, so we don't want to re-encode them
207-
request.getRequestSource() != AwsProxyRequest.RequestSource.ALB,
207+
request.getRequestSource() != RequestSource.ALB,
208208
config.getUriEncoding());
209209
} catch (ServletException e) {
210210
log.error("Could not generate query string", e);
@@ -580,7 +580,7 @@ private List<String> getHeaderValues(String key) {
580580
// special cases for referer and user agent headers
581581
List<String> values = new ArrayList<>();
582582

583-
if (request.getRequestSource() == AwsProxyRequest.RequestSource.API_GATEWAY) {
583+
if (request.getRequestSource() == RequestSource.API_GATEWAY) {
584584
if ("referer".equals(key.toLowerCase(Locale.ENGLISH))) {
585585
values.add(request.getRequestContext().getIdentity().getCaller());
586586
return values;

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsProxyHttpServletResponseWriter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
2121
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
2222
import com.amazonaws.serverless.proxy.model.Headers;
23+
import com.amazonaws.serverless.proxy.model.RequestSource;
2324
import com.amazonaws.services.lambda.runtime.Context;
2425

2526
import javax.ws.rs.core.Response;
@@ -73,7 +74,7 @@ public AwsProxyResponse writeResponse(AwsHttpServletResponse containerResponse,
7374

7475
awsProxyResponse.setStatusCode(containerResponse.getStatus());
7576

76-
if (containerResponse.getAwsProxyRequest() != null && containerResponse.getAwsProxyRequest().getRequestSource() == AwsProxyRequest.RequestSource.ALB) {
77+
if (containerResponse.getAwsProxyRequest() != null && containerResponse.getAwsProxyRequest().getRequestSource() == RequestSource.ALB) {
7778
awsProxyResponse.setStatusDescription(containerResponse.getStatus() + " " + Response.Status.fromStatusCode(containerResponse.getStatus()).getReasonPhrase());
7879
}
7980

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/testutils/AwsProxyRequestBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,11 @@ public AwsProxyRequestBuilder queryString(String key, String value) {
224224
this.request.setMultiValueQueryStringParameters(new MultiValuedTreeMap<>());
225225
}
226226

227-
if (request.getRequestSource() == AwsProxyRequest.RequestSource.API_GATEWAY) {
227+
if (request.getRequestSource() == RequestSource.API_GATEWAY) {
228228
this.request.getMultiValueQueryStringParameters().add(key, value);
229229
}
230230
// ALB does not decode parameters automatically like API Gateway.
231-
if (request.getRequestSource() == AwsProxyRequest.RequestSource.ALB) {
231+
if (request.getRequestSource() == RequestSource.ALB) {
232232
try {
233233
//if (URLDecoder.decode(value, ContainerConfig.DEFAULT_CONTENT_CHARSET).equals(value)) {
234234
// TODO: Assume we are always given an unencoded value, smarter check here to encode
@@ -285,7 +285,7 @@ public AwsProxyRequestBuilder binaryBody(InputStream is)
285285

286286

287287
public AwsProxyRequestBuilder authorizerPrincipal(String principal) {
288-
if (this.request.getRequestSource() == AwsProxyRequest.RequestSource.API_GATEWAY) {
288+
if (this.request.getRequestSource() == RequestSource.API_GATEWAY) {
289289
if (this.request.getRequestContext().getAuthorizer() == null) {
290290
this.request.getRequestContext().setAuthorizer(new ApiGatewayAuthorizerContext());
291291
}
@@ -295,7 +295,7 @@ public AwsProxyRequestBuilder authorizerPrincipal(String principal) {
295295
}
296296
this.request.getRequestContext().getAuthorizer().getClaims().setSubject(principal);
297297
}
298-
if (this.request.getRequestSource() == AwsProxyRequest.RequestSource.ALB) {
298+
if (this.request.getRequestSource() == RequestSource.ALB) {
299299
header("x-amzn-oidc-identity", principal);
300300
try {
301301
header(

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/model/AwsProxyRequest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,4 @@ public boolean isBase64Encoded() {
192192
public void setIsBase64Encoded(boolean base64Encoded) {
193193
isBase64Encoded = base64Encoded;
194194
}
195-
196-
public static enum RequestSource {
197-
ALB,
198-
API_GATEWAY
199-
}
200195
}

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/model/HttpApiV2ProxyRequest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
package com.amazonaws.serverless.proxy.model;
1414

1515
import com.fasterxml.jackson.annotation.JsonProperty;
16+
import com.fasterxml.jackson.annotation.JsonIgnore;
1617

1718
import java.util.List;
1819
import java.util.Map;
20+
import java.util.Objects;
21+
import java.util.Optional;
1922

2023
public class HttpApiV2ProxyRequest {
2124
private String version;
@@ -127,4 +130,12 @@ public HttpApiV2ProxyRequestContext getRequestContext() {
127130
public void setRequestContext(HttpApiV2ProxyRequestContext requestContext) {
128131
this.requestContext = requestContext;
129132
}
133+
134+
@JsonIgnore
135+
public RequestSource getRequestSource() {
136+
return Optional.ofNullable(getRequestContext())
137+
.map(HttpApiV2ProxyRequestContext::getElb)
138+
.map(albContext -> RequestSource.ALB)
139+
.orElse(RequestSource.API_GATEWAY);
140+
}
130141
}

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/model/HttpApiV2ProxyRequestContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class HttpApiV2ProxyRequestContext {
2525
private String stage;
2626
private String time;
2727
private long timeEpoch;
28+
private AlbContext elb;
2829

2930
private HttpApiV2HttpContext http;
3031
private HttpApiV2AuthorizerMap authorizer;
@@ -117,4 +118,13 @@ public void setAuthorizer(HttpApiV2AuthorizerMap authorizer) {
117118
this.authorizer = authorizer;
118119
}
119120

121+
public AlbContext getElb() {
122+
return this.elb;
123+
}
124+
125+
public void setElb(AlbContext context) {
126+
this.elb = context;
127+
}
128+
129+
120130
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
5+
* with the License. A copy of the License is located at
6+
*
7+
* http://aws.amazon.com/apache2.0/
8+
*
9+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
10+
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
package com.amazonaws.serverless.proxy.model;
14+
15+
public enum RequestSource {
16+
ALB,
17+
API_GATEWAY
18+
}

aws-serverless-java-container-core/src/test/java/com/amazonaws/serverless/proxy/model/HttpApiV2ProxyRequestTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ void deserialize_fromJsonString_authorizerPopulatedCorrectly() {
136136
HttpApiV2ProxyRequest req = LambdaContainerHandler.getObjectMapper().readValue(BASE_PROXY_REQUEST, HttpApiV2ProxyRequest.class);
137137
assertTrue(req.getRequestContext().getAuthorizer().getJwtAuthorizer().getClaims().containsKey("claim1"));
138138
assertEquals(2, req.getRequestContext().getAuthorizer().getJwtAuthorizer().getScopes().size());
139+
assertEquals(RequestSource.API_GATEWAY, req.getRequestSource());
139140
} catch (JsonProcessingException e) {
140141
e.printStackTrace();
141142
fail("Exception while parsing request" + e.getMessage());
@@ -177,6 +178,7 @@ void deserialize_fromJsonString_isBase64EncodedPopulates() {
177178
assertFalse(req.isBase64Encoded());
178179
req = LambdaContainerHandler.getObjectMapper().readValue(NO_AUTH_PROXY, HttpApiV2ProxyRequest.class);
179180
assertTrue(req.isBase64Encoded());
181+
assertEquals(RequestSource.API_GATEWAY, req.getRequestSource());
180182
} catch (JsonProcessingException e) {
181183
e.printStackTrace();
182184
fail("Exception while parsing request" + e.getMessage());

0 commit comments

Comments
 (0)