Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to java events #657

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions aws-serverless-java-container-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@
<version>6.1.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>4.0.0</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.amazonaws.serverless.proxy;

import com.amazonaws.serverless.exceptions.InvalidRequestEventException;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.model.ErrorModel;
import com.amazonaws.services.lambda.runtime.events.AwsProxyResponseEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.ws.rs.InternalServerErrorException;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AwsAlbExceptionHandler implements ExceptionHandler<AwsProxyResponseEvent>{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need that class? Can't we use AwsProxyExceptionHandler? Unittests ran fine without it.


private Logger log = LoggerFactory.getLogger(AwsAlbExceptionHandler.class);

//-------------------------------------------------------------
// Constants
//-------------------------------------------------------------

static final String INTERNAL_SERVER_ERROR = "Internal Server Error";
static final String GATEWAY_TIMEOUT_ERROR = "Gateway timeout";


//-------------------------------------------------------------
// Variables - Private - Static
//-------------------------------------------------------------

private static final Map<String, List<String>> headers = new HashMap<>();

//-------------------------------------------------------------
// Constructors
//-------------------------------------------------------------

static {
List<String> values = new ArrayList<>();
values.add(MediaType.APPLICATION_JSON);
headers.put(HttpHeaders.CONTENT_TYPE, values);
}
@Override
public AwsProxyResponseEvent handle(Throwable ex) {
log.error("Called exception handler for:", ex);

// adding a print stack trace in case we have no appender or we are running inside SAM local, where need the
// output to go to the stderr.
ex.printStackTrace();
AwsProxyResponseEvent responseEvent = new AwsProxyResponseEvent();

responseEvent.setMultiValueHeaders(headers);
if (ex instanceof InvalidRequestEventException || ex instanceof InternalServerErrorException) {
//return new APIGatewayProxyResponseEvent(500, headers, getErrorJson(INTERNAL_SERVER_ERROR));
responseEvent.setBody(getErrorJson(INTERNAL_SERVER_ERROR));
responseEvent.setStatusCode(500);
return responseEvent;
} else {
responseEvent.setBody(getErrorJson(GATEWAY_TIMEOUT_ERROR));
responseEvent.setStatusCode(502);
return responseEvent;
}
}

@Override
public void handle(Throwable ex, OutputStream stream) throws IOException {
AwsProxyResponseEvent response = handle(ex);

LambdaContainerHandler.getObjectMapper().writeValue(stream, response);
}

//-------------------------------------------------------------
// Methods - Protected
//-------------------------------------------------------------

String getErrorJson(String message) {

try {
return LambdaContainerHandler.getObjectMapper().writeValueAsString(new ErrorModel(message));
} catch (JsonProcessingException e) {
log.error("Could not produce error JSON", e);
return "{ \"message\": \"" + message + "\" }";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
package com.amazonaws.serverless.proxy;

import com.amazonaws.serverless.proxy.internal.jaxrs.AwsHttpApiV2SecurityContext;
import com.amazonaws.serverless.proxy.model.HttpApiV2ProxyRequest;
import com.amazonaws.services.lambda.runtime.Context;

import com.amazonaws.services.lambda.runtime.events.apigateway.APIGatewayV2HTTPEvent;
import jakarta.ws.rs.core.SecurityContext;

public class AwsHttpApiV2SecurityContextWriter implements SecurityContextWriter<HttpApiV2ProxyRequest> {
public class AwsHttpApiV2SecurityContextWriter implements SecurityContextWriter<APIGatewayV2HTTPEvent> {
@Override
public SecurityContext writeSecurityContext(HttpApiV2ProxyRequest event, Context lambdaContext) {
public SecurityContext writeSecurityContext(APIGatewayV2HTTPEvent event, Context lambdaContext) {
return new AwsHttpApiV2SecurityContext(lambdaContext, event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@

import com.amazonaws.serverless.exceptions.InvalidRequestEventException;
import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.model.ErrorModel;
import com.amazonaws.serverless.proxy.model.Headers;

import com.amazonaws.services.lambda.runtime.events.AwsProxyResponseEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,7 +39,7 @@
* @see ExceptionHandler
*/
public class AwsProxyExceptionHandler
implements ExceptionHandler<AwsProxyResponse> {
implements ExceptionHandler<AwsProxyResponseEvent> {

private Logger log = LoggerFactory.getLogger(AwsProxyExceptionHandler.class);

Expand Down Expand Up @@ -72,23 +72,29 @@ public class AwsProxyExceptionHandler


@Override
public AwsProxyResponse handle(Throwable ex) {
public AwsProxyResponseEvent handle(Throwable ex) {
log.error("Called exception handler for:", ex);

// adding a print stack trace in case we have no appender or we are running inside SAM local, where need the
// output to go to the stderr.
ex.printStackTrace();
AwsProxyResponseEvent responseEvent = new AwsProxyResponseEvent();
responseEvent.setMultiValueHeaders(headers);
if (ex instanceof InvalidRequestEventException || ex instanceof InternalServerErrorException) {
return new AwsProxyResponse(500, headers, getErrorJson(INTERNAL_SERVER_ERROR));
responseEvent.setBody(getErrorJson(INTERNAL_SERVER_ERROR));
responseEvent.setStatusCode(500);
return responseEvent;
} else {
return new AwsProxyResponse(502, headers, getErrorJson(GATEWAY_TIMEOUT_ERROR));
responseEvent.setBody(getErrorJson(GATEWAY_TIMEOUT_ERROR));
responseEvent.setStatusCode(502);
return responseEvent;
}
}


@Override
public void handle(Throwable ex, OutputStream stream) throws IOException {
AwsProxyResponse response = handle(ex);
AwsProxyResponseEvent response = handle(ex);

LambdaContainerHandler.getObjectMapper().writeValue(stream, response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
package com.amazonaws.serverless.proxy;

import com.amazonaws.serverless.proxy.internal.jaxrs.AwsProxySecurityContext;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.services.lambda.runtime.Context;

import com.amazonaws.services.lambda.runtime.events.apigateway.APIGatewayProxyRequestEvent;
import jakarta.ws.rs.core.SecurityContext;

/**
* Default implementation of <code>SecurityContextWriter</code>. Creates a SecurityContext object based on an API Gateway
* event and the Lambda context. This returns the default <code>AwsProxySecurityContext</code> instance.
*/
public class AwsProxySecurityContextWriter implements SecurityContextWriter<AwsProxyRequest> {
public class AwsProxySecurityContextWriter implements SecurityContextWriter<APIGatewayProxyRequestEvent> {

//-------------------------------------------------------------
// Variables - Private - Static
Expand All @@ -36,7 +36,7 @@ public class AwsProxySecurityContextWriter implements SecurityContextWriter<AwsP
//-------------------------------------------------------------

@Override
public SecurityContext writeSecurityContext(AwsProxyRequest event, Context lambdaContext) {
public SecurityContext writeSecurityContext(APIGatewayProxyRequestEvent event, Context lambdaContext) {
currentContext = new AwsProxySecurityContext(lambdaContext, event);

return currentContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public abstract class RequestReader<RequestType, ContainerRequestType> {
*/
public static final String ALB_CONTEXT_PROPERTY = "com.amazonaws.alb.request.context";

/**
* The key to store the entire Application Load Balancer event
*/
public static final String ALB_EVENT_PROPERTY = "com.amazonaws.alb.request";

/**
* The key to store the entire API Gateway event
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
package com.amazonaws.serverless.proxy;

import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.services.lambda.runtime.Context;

import jakarta.ws.rs.core.SecurityContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import com.amazonaws.serverless.proxy.internal.LambdaContainerHandler;
import com.amazonaws.serverless.proxy.internal.SecurityUtils;
import com.amazonaws.serverless.proxy.model.HttpApiV2ProxyRequest;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.events.apigateway.APIGatewayV2HTTPEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
Expand All @@ -33,9 +33,9 @@ public class AwsHttpApiV2SecurityContext implements SecurityContext {
private static Logger log = LoggerFactory.getLogger(AwsHttpApiV2SecurityContext.class);

private Context lambdaContext;
private HttpApiV2ProxyRequest event;
private APIGatewayV2HTTPEvent event;

public AwsHttpApiV2SecurityContext(final Context lambdaCtx, final HttpApiV2ProxyRequest request) {
public AwsHttpApiV2SecurityContext(final Context lambdaCtx, final APIGatewayV2HTTPEvent request) {
lambdaContext = lambdaCtx;
event = request;
}
Expand Down Expand Up @@ -79,8 +79,8 @@ public boolean isUserInRole(String s) {
return false;
}

return event.getRequestContext().getAuthorizer().getJwtAuthorizer().getScopes().contains(s) ||
event.getRequestContext().getAuthorizer().getJwtAuthorizer().getClaims().containsKey(s);
return event.getRequestContext().getAuthorizer().getJwt().getScopes().contains(s) ||
event.getRequestContext().getAuthorizer().getJwt().getClaims().containsKey(s);

}

Expand All @@ -94,7 +94,7 @@ public String getAuthenticationScheme() {
if (event.getRequestContext().getAuthorizer() == null) {
return null;
}
if (event.getRequestContext().getAuthorizer().isJwt()) {
if (event.getRequestContext().getAuthorizer().getJwt() != null) {
return AUTH_SCHEME_JWT;
}
return null;
Expand Down