diff --git a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/jaxrs/AwsProxySecurityContext.java b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/jaxrs/AwsProxySecurityContext.java index c6698d86c..816cf8af2 100644 --- a/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/jaxrs/AwsProxySecurityContext.java +++ b/aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/jaxrs/AwsProxySecurityContext.java @@ -69,7 +69,12 @@ public Principal getUserPrincipal() { if (getAuthenticationScheme().equals(AUTH_SCHEME_CUSTOM)) { return event.getRequestContext().getAuthorizer().getPrincipalId(); } else if (getAuthenticationScheme().equals(AUTH_SCHEME_AWS_IAM)) { - return event.getRequestContext().getIdentity().getUserArn(); + // if we received credentials from Cognito Federated Identities then we return the identity id + if (event.getRequestContext().getIdentity().getCognitoIdentityId() != null) { + return event.getRequestContext().getIdentity().getCognitoIdentityId(); + } else { // otherwise the user arn from the credentials + return event.getRequestContext().getIdentity().getUserArn(); + } } else if (getAuthenticationScheme().equals(AUTH_SCHEME_COGNITO_POOL)) { return event.getRequestContext().getAuthorizer().getClaims().getSubject(); } diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/EchoJerseyResource.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java similarity index 96% rename from aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/EchoJerseyResource.java rename to aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java index 33d54392d..c22277da9 100644 --- a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/EchoJerseyResource.java +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/EchoJerseyResource.java @@ -10,12 +10,12 @@ * 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.amazonaws.serverless.proxy.test.jersey; +package com.amazonaws.serverless.proxy.jersey; import com.amazonaws.serverless.proxy.internal.RequestReader; import com.amazonaws.serverless.proxy.internal.model.ApiGatewayRequestContext; -import com.amazonaws.serverless.proxy.test.jersey.model.MapResponseModel; -import com.amazonaws.serverless.proxy.test.jersey.model.SingleValueModel; +import com.amazonaws.serverless.proxy.jersey.model.MapResponseModel; +import com.amazonaws.serverless.proxy.jersey.model.SingleValueModel; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/JerseyAwsProxyTest.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java similarity index 96% rename from aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/JerseyAwsProxyTest.java rename to aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java index 781af55d9..1df8e0b24 100644 --- a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/JerseyAwsProxyTest.java +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/JerseyAwsProxyTest.java @@ -10,7 +10,7 @@ * 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.amazonaws.serverless.proxy.test.jersey; +package com.amazonaws.serverless.proxy.jersey; import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest; @@ -18,9 +18,8 @@ import com.amazonaws.serverless.proxy.internal.servlet.AwsServletContext; import com.amazonaws.serverless.proxy.jersey.factory.AwsProxyServletContextFactory; import com.amazonaws.serverless.proxy.jersey.factory.AwsProxyServletRequestFactory; -import com.amazonaws.serverless.proxy.jersey.JerseyLambdaContainerHandler; -import com.amazonaws.serverless.proxy.test.jersey.model.MapResponseModel; -import com.amazonaws.serverless.proxy.test.jersey.model.SingleValueModel; +import com.amazonaws.serverless.proxy.jersey.model.MapResponseModel; +import com.amazonaws.serverless.proxy.jersey.model.SingleValueModel; import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; import com.amazonaws.services.lambda.runtime.Context; @@ -51,7 +50,7 @@ public class JerseyAwsProxyTest { private static ObjectMapper objectMapper = new ObjectMapper(); - private static ResourceConfig app = new ResourceConfig().packages("com.amazonaws.serverless.proxy.test.jersey") + private static ResourceConfig app = new ResourceConfig().packages("com.amazonaws.serverless.proxy.jersey") .register(new AbstractBinder() { @Override protected void configure() { diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/model/MapResponseModel.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/model/MapResponseModel.java similarity index 94% rename from aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/model/MapResponseModel.java rename to aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/model/MapResponseModel.java index 48fb23fe5..8e9c23ab9 100644 --- a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/model/MapResponseModel.java +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/model/MapResponseModel.java @@ -10,7 +10,7 @@ * 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.amazonaws.serverless.proxy.test.jersey.model; +package com.amazonaws.serverless.proxy.jersey.model; import java.util.HashMap; import java.util.Map; diff --git a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/model/SingleValueModel.java b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/model/SingleValueModel.java similarity index 93% rename from aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/model/SingleValueModel.java rename to aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/model/SingleValueModel.java index 5ec0508fb..512fe37cc 100644 --- a/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/test/jersey/model/SingleValueModel.java +++ b/aws-serverless-java-container-jersey/src/test/java/com/amazonaws/serverless/proxy/jersey/model/SingleValueModel.java @@ -10,7 +10,7 @@ * 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.amazonaws.serverless.proxy.test.jersey.model; +package com.amazonaws.serverless.proxy.jersey.model; /** * Request/response model diff --git a/aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandler.java b/aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandler.java index 1dfce86a1..2a49efc35 100644 --- a/aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandler.java +++ b/aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandler.java @@ -64,10 +64,10 @@ public class SparkLambdaContainerHandler extends AwsL //------------------------------------------------------------- - // Variables - Private - Static + // Variables - Private //------------------------------------------------------------- - private static LambdaEmbeddedServer embeddedServer; + private LambdaEmbeddedServer embeddedServer; //------------------------------------------------------------- @@ -145,13 +145,15 @@ protected AwsHttpServletResponse getContainerResponse(CountDownLatch latch) { @Override protected void handleRequest(AwsProxyHttpServletRequest httpServletRequest, AwsHttpServletResponse httpServletResponse, Context lambdaContext) throws Exception { + // this method of the AwsLambdaServletContainerHandler sets the request context + super.handleRequest(httpServletRequest, httpServletResponse, lambdaContext); + if (embeddedServer == null) { embeddedServer = LambdaEmbeddedServerFactory.getServerInstance(); - servletContext = httpServletRequest.getServletContext(); // call the onStartup event if set to give developers a chance to set filters in the context if (startupHandler != null) { - startupHandler.onStartup(this.servletContext); + startupHandler.onStartup(getServletContext()); } } diff --git a/aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/embeddedserver/LambdaEmbeddedServer.java b/aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/embeddedserver/LambdaEmbeddedServer.java index a5dfbbabf..335e19a35 100644 --- a/aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/embeddedserver/LambdaEmbeddedServer.java +++ b/aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/embeddedserver/LambdaEmbeddedServer.java @@ -62,7 +62,9 @@ public int ignite(String host, public void configureWebSockets(Map webSocketHandlers, Optional webSocketIdleTimeoutMillis) { - throw new UnsupportedOperationException(); + // Swallowing this exception to prevent Spark from getting stuck + // throw new UnsupportedOperationException(); + System.out.println("Spark called configureWebSockets. However, web sockets are not supported"); } diff --git a/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/HelloWorldSparkTest.java b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/HelloWorldSparkTest.java new file mode 100644 index 000000000..7c7e1c332 --- /dev/null +++ b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/HelloWorldSparkTest.java @@ -0,0 +1,54 @@ +package com.amazonaws.serverless.proxy.spark; + + +import com.amazonaws.serverless.exceptions.ContainerInitializationException; +import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest; +import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse; +import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; +import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; + +import org.junit.Test; + +import static org.junit.Assert.*; +import static spark.Spark.get; + + +public class HelloWorldSparkTest { + private static final String CUSTOM_HEADER_KEY = "X-Custom-Header"; + private static final String CUSTOM_HEADER_VALUE = "My Header Value"; + private static final String BODY_TEXT_RESPONSE = "Hello World"; + + private static SparkLambdaContainerHandler handler; + + @Test + public void basicServer_initialize() { + try { + handler = SparkLambdaContainerHandler.getAwsProxyHandler(); + + configureRoutes(); + + } catch (RuntimeException | ContainerInitializationException e) { + e.printStackTrace(); + fail(); + } + } + + @Test + public void basicServer_handleRequest_emptyFilters() { + AwsProxyRequest req = new AwsProxyRequestBuilder().method("GET").path("/hello").build(); + AwsProxyResponse response = handler.proxy(req, new MockLambdaContext()); + + assertEquals(200, response.getStatusCode()); + assertTrue(response.getHeaders().containsKey(CUSTOM_HEADER_KEY)); + assertEquals(CUSTOM_HEADER_VALUE, response.getHeaders().get(CUSTOM_HEADER_KEY)); + assertEquals(BODY_TEXT_RESPONSE, response.getBody()); + } + + private void configureRoutes() { + get("/hello", (req, res) -> { + res.status(200); + res.header(CUSTOM_HEADER_KEY, CUSTOM_HEADER_VALUE); + return BODY_TEXT_RESPONSE; + }); + } +} diff --git a/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandlerTest.java b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandlerTest.java new file mode 100644 index 000000000..f5da33968 --- /dev/null +++ b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandlerTest.java @@ -0,0 +1,66 @@ +package com.amazonaws.serverless.proxy.spark; + + +import com.amazonaws.serverless.exceptions.ContainerInitializationException; +import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest; +import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse; +import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; +import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; +import com.amazonaws.serverless.proxy.spark.filter.CustomHeaderFilter; + +import org.junit.Test; + +import javax.servlet.DispatcherType; +import javax.servlet.FilterRegistration; + +import java.util.EnumSet; + +import static org.junit.Assert.*; +import static spark.Spark.get; + + +public class SparkLambdaContainerHandlerTest { + private static final String RESPONSE_BODY_TEXT = "hello"; + + @Test + public void filters_onStartupMethod_executeFilters() { + + SparkLambdaContainerHandler handler = null; + try { + handler = SparkLambdaContainerHandler.getAwsProxyHandler(); + } catch (ContainerInitializationException e) { + e.printStackTrace(); + fail(); + } + + handler.onStartup(c -> { + if (c == null) { + System.out.println("Null servlet context"); + fail(); + } + FilterRegistration.Dynamic registration = c.addFilter("CustomHeaderFilter", CustomHeaderFilter.class); + // update the registration to map to a path + registration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); + // servlet name mappings are disabled and will throw an exception + }); + + configureRoutes(); + + AwsProxyRequest req = new AwsProxyRequestBuilder().method("GET").path("/header-filter").build(); + AwsProxyResponse response = handler.proxy(req, new MockLambdaContext()); + + assertNotNull(response); + assertEquals(200, response.getStatusCode()); + assertTrue(response.getHeaders().containsKey(CustomHeaderFilter.HEADER_NAME)); + assertEquals(CustomHeaderFilter.HEADER_VALUE, response.getHeaders().get(CustomHeaderFilter.HEADER_NAME)); + assertEquals(RESPONSE_BODY_TEXT, response.getBody()); + + } + + private void configureRoutes() { + get("/header-filter", (req, res) -> { + res.status(200); + return RESPONSE_BODY_TEXT; + }); + } +} diff --git a/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/embeddedserver/LambdaEmbeddedServerTest.java b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/embeddedserver/LambdaEmbeddedServerTest.java new file mode 100644 index 000000000..af4dc275c --- /dev/null +++ b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/embeddedserver/LambdaEmbeddedServerTest.java @@ -0,0 +1,23 @@ +package com.amazonaws.serverless.proxy.spark.embeddedserver; + + +import org.junit.Test; + +import java.util.Optional; + +import static org.junit.Assert.*; + + +public class LambdaEmbeddedServerTest { + private static LambdaEmbeddedServer server = new LambdaEmbeddedServer(null, null, false); + + @Test + public void webSocket_configureWebSocket_noException() { + try { + server.configureWebSockets(null, Optional.of(0)); + } catch (Exception e) { + e.printStackTrace(); + fail(); + } + } +} diff --git a/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/filter/CustomHeaderFilter.java b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/filter/CustomHeaderFilter.java new file mode 100644 index 000000000..349994456 --- /dev/null +++ b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/spark/filter/CustomHeaderFilter.java @@ -0,0 +1,38 @@ +package com.amazonaws.serverless.proxy.spark.filter; + + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import java.io.IOException; + + +public class CustomHeaderFilter implements Filter { + public static final String HEADER_NAME = "X-Filter-Header"; + public static final String HEADER_VALUE = "CustomHeaderFilter"; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + System.out.println("Called init on filter"); + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + System.out.println("Called doFilter"); + HttpServletResponse resp = (HttpServletResponse)servletResponse; + resp.addHeader(HEADER_NAME, HEADER_VALUE); + + filterChain.doFilter(servletRequest, servletResponse); + } + + + @Override + public void destroy() { + System.out.println("Called destroy"); + } +} \ No newline at end of file diff --git a/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/test/spark/HelloWorldSparkTest.java b/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/test/spark/HelloWorldSparkTest.java deleted file mode 100644 index 290330bb2..000000000 --- a/aws-serverless-java-container-spark/src/test/java/com/amazonaws/serverless/proxy/test/spark/HelloWorldSparkTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.amazonaws.serverless.proxy.test.spark; - - -import com.amazonaws.serverless.exceptions.ContainerInitializationException; -import com.amazonaws.serverless.proxy.internal.model.AwsProxyRequest; -import com.amazonaws.serverless.proxy.internal.model.AwsProxyResponse; -import com.amazonaws.serverless.proxy.spark.SparkLambdaContainerHandler; -import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder; -import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import junit.framework.TestCase; - -import static spark.Spark.get; - -public class HelloWorldSparkTest extends TestCase { - private ObjectMapper objectMapper = new ObjectMapper(); - - public void testSparkStart() { - try { - SparkLambdaContainerHandler handler - = SparkLambdaContainerHandler.getAwsProxyHandler(); - - get("/hello", (req, res) -> { - res.status(200); - res.body("Hello World"); - res.header("X-Custom-Header", "My Header Value"); - return "Hello World"; - }); - - AwsProxyRequest req = new AwsProxyRequestBuilder().method("GET").path("/hello").build(); - AwsProxyResponse response = handler.proxy(req, new MockLambdaContext()); - System.out.println("Response: " + objectMapper.writeValueAsString(response)); - } catch (JsonProcessingException | ContainerInitializationException e) { - e.printStackTrace(); - } - } -} diff --git a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringServletContextTest.java b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringServletContextTest.java index 075db5bb7..4c5e80a78 100644 --- a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringServletContextTest.java +++ b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/SpringServletContextTest.java @@ -12,12 +12,10 @@ import com.amazonaws.serverless.proxy.spring.echoapp.model.ValidatedUserModel; import org.junit.BeforeClass; import org.junit.Test; -import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import javax.servlet.DispatcherType; import javax.servlet.FilterRegistration; -import javax.ws.rs.core.MediaType; import java.util.EnumSet; diff --git a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/CustomHeaderFilter.java b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/CustomHeaderFilter.java index 50256b1af..0d3eb903e 100644 --- a/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/CustomHeaderFilter.java +++ b/aws-serverless-java-container-spring/src/test/java/com/amazonaws/serverless/proxy/spring/echoapp/CustomHeaderFilter.java @@ -1,7 +1,14 @@ package com.amazonaws.serverless.proxy.spring.echoapp; -import javax.servlet.*; + +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletResponse; + import java.io.IOException; @@ -28,4 +35,4 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo public void destroy() { System.out.println("Called destroy"); } -} +} \ No newline at end of file