From 003e1d5ade784413470dbd0f80e6b1e36eb01cad Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 23 Mar 2022 12:54:15 +0100 Subject: [PATCH] GH-833 Add initial support for AWS APIGatewayCustomAuthorizerEvent Resolves #833 --- pom.xml | 12 -------- .../function/adapter/aws/AWSLambdaUtils.java | 3 +- .../adapter/aws/FunctionInvokerTests.java | 29 +++++++++++++++++++ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/pom.xml b/pom.xml index ba2b87556..71fc8db10 100644 --- a/pom.xml +++ b/pom.xml @@ -137,18 +137,6 @@ - - - - - - - - - - - - core diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java index 53098a0f2..c0cb9100e 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/main/java/org/springframework/cloud/function/adapter/aws/AWSLambdaUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2021-2021 the original author or authors. + * Copyright 2021-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -67,6 +67,7 @@ final class AWSLambdaUtils { || typeName.equals("com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent") || typeName.equals("com.amazonaws.services.lambda.runtime.events.SNSEvent") || typeName.equals("com.amazonaws.services.lambda.runtime.events.SQSEvent") + || typeName.equals("com.amazonaws.services.lambda.runtime.events.APIGatewayCustomAuthorizerEvent") || typeName.equals("com.amazonaws.services.lambda.runtime.events.KinesisEvent"); } diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/FunctionInvokerTests.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/FunctionInvokerTests.java index f3ebec121..5539c49f2 100644 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/FunctionInvokerTests.java +++ b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws/src/test/java/org/springframework/cloud/function/adapter/aws/FunctionInvokerTests.java @@ -27,6 +27,7 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; +import com.amazonaws.services.lambda.runtime.events.APIGatewayCustomAuthorizerEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent; @@ -447,6 +448,12 @@ public class FunctionInvokerTests { " \"isBase64Encoded\": false\n" + "}"; + String gwAuthorizerEvent = "{\n" + + " \"type\":\"TOKEN\",\n" + + " \"authorizationToken\":\"allow\",\n" + + " \"methodArn\":\"arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/*/GET/\"\n" + + "}"; + @BeforeEach public void before() throws Exception { System.clearProperty("MAIN_CLASS"); @@ -455,6 +462,19 @@ public class FunctionInvokerTests { this.getEnvironment().clear(); } + @Test + public void testAPIGatewayCustomAuthorizerEvent() throws Exception { + System.setProperty("MAIN_CLASS", AuthorizerConfiguration.class.getName()); + System.setProperty("spring.cloud.function.definition", "acceptAuthorizerEvent"); + FunctionInvoker invoker = new FunctionInvoker(); + + InputStream targetStream = new ByteArrayInputStream(this.gwAuthorizerEvent.getBytes()); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + invoker.handleRequest(targetStream, output, null); + String result = new String(output.toByteArray(), StandardCharsets.UTF_8); + assertThat(result).contains("APIGatewayCustomAuthorizerEvent(version=null, type=TOKEN"); + } + @Test public void testCollection() throws Exception { System.setProperty("MAIN_CLASS", SampleConfiguration.class.getName()); @@ -1043,6 +1063,15 @@ public class FunctionInvokerTests { return (Map) field.get(env); } + @EnableAutoConfiguration + @Configuration + public static class AuthorizerConfiguration { + @Bean + public Function acceptAuthorizerEvent() { + return v -> v.toString(); + } + } + @EnableAutoConfiguration @Configuration public static class SampleConfiguration {