From 6c0970bee2d7c5bbe4e28d07c9bd00bf85a67cf8 Mon Sep 17 00:00:00 2001 From: Richard Thomas Date: Mon, 3 May 2021 21:01:54 +0200 Subject: [PATCH] Get Principal from request, allowing it to be faked for testing. --- .../amazon/lambda/http/LambdaHttpHandler.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java b/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java index 1fcc653280fa2..90187615ed3e8 100644 --- a/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java +++ b/extensions/amazon-lambda-http/runtime/src/main/java/io/quarkus/amazon/lambda/http/LambdaHttpHandler.java @@ -7,6 +7,7 @@ import java.nio.channels.Channels; import java.nio.channels.WritableByteChannel; import java.nio.charset.StandardCharsets; +import java.security.Principal; import java.util.Base64; import java.util.HashMap; import java.util.HashSet; @@ -168,6 +169,10 @@ private APIGatewayV2HTTPResponse nettyDispatch(InetSocketAddress clientAddress, quarkusHeaders.setContextObject(Context.class, context); quarkusHeaders.setContextObject(APIGatewayV2HTTPEvent.class, request); quarkusHeaders.setContextObject(APIGatewayV2HTTPEvent.RequestContext.class, request.getRequestContext()); + final Principal principal = getPrincipal(request); + if (principal != null) { + quarkusHeaders.setContextObject(Principal.class, principal); + } DefaultHttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.valueOf(request.getRequestContext().getHttp().getMethod()), ofNullable(request.getRawQueryString()) .filter(q -> !q.isEmpty()).map(q -> request.getRawPath() + '?' + q).orElse(request.getRawPath()), @@ -234,4 +239,45 @@ private boolean isBinary(String contentType) { return false; } + private Principal getPrincipal(APIGatewayV2HTTPEvent request) { + final Map systemEnvironment = System.getenv(); + final boolean isSamLocal = Boolean.parseBoolean(systemEnvironment.get("AWS_SAM_LOCAL")); + if (isSamLocal) { + final String forcedUserName = systemEnvironment.get("QUARKUS_AWS_LAMBDA_FORCE_USER_NAME"); + if (forcedUserName != null && !forcedUserName.isEmpty()) { + log.info("Forcing local user to " + forcedUserName); + return new Principal() { + + @Override + public String getName() { + return forcedUserName; + } + + }; + } + } else { + final APIGatewayV2HTTPEvent.RequestContext requestContext = request.getRequestContext(); + if (requestContext != null) { + final APIGatewayV2HTTPEvent.RequestContext.Authorizer authorizer = requestContext.getAuthorizer(); + if (authorizer != null) { + final APIGatewayV2HTTPEvent.RequestContext.Authorizer.JWT jwt = authorizer.getJwt(); + if (jwt != null) { + final Map claims = jwt.getClaims(); + if (claims != null) { + final String jwtUsername = claims.get("cognito:username"); + if (jwtUsername != null && !jwtUsername.isEmpty()) + return new Principal() { + @Override + public String getName() { + return jwtUsername; + } + }; + } + } + } + } + } + return null; + } + }