Skip to content

Commit

Permalink
Added unit test to validate the proposed solution for #111 and warnin…
Browse files Browse the repository at this point in the history
…g log messages in request object when a framework tries to access the session
  • Loading branch information
sapessi committed Jan 31, 2018
1 parent 68658d8 commit 54bcef5
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public String getRequestedSessionId() {

@Override
public HttpSession getSession(boolean b) {
log.warn("Trying to access session. Lambda functions are stateless and should not rely on the session");
if (b && null == this.session) {
ApiGatewayRequestContext requestContext = (ApiGatewayRequestContext) getAttribute(RequestReader.API_GATEWAY_CONTEXT_PROPERTY);
this.session = new AwsHttpSession(requestContext.getRequestId());
Expand All @@ -117,37 +118,43 @@ public HttpSession getSession(boolean b) {

@Override
public HttpSession getSession() {
log.warn("Trying to access session. Lambda functions are stateless and should not rely on the session");
return this.session;
}


@Override
public String changeSessionId() {
log.warn("Trying to access session. Lambda functions are stateless and should not rely on the session");
return null;
}


@Override
public boolean isRequestedSessionIdValid() {
log.warn("Trying to access session. Lambda functions are stateless and should not rely on the session");
return false;
}


@Override
public boolean isRequestedSessionIdFromCookie() {
log.warn("Trying to access session. Lambda functions are stateless and should not rely on the session");
return false;
}


@Override
public boolean isRequestedSessionIdFromURL() {
log.warn("Trying to access session. Lambda functions are stateless and should not rely on the session");
return false;
}


@Override
@Deprecated
public boolean isRequestedSessionIdFromUrl() {
log.warn("Trying to access session. Lambda functions are stateless and should not rely on the session");
return false;
}

Expand Down
67 changes: 67 additions & 0 deletions aws-serverless-java-container-spring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<properties>
<spring.version>4.3.13.RELEASE</spring.version>
<spring-security.version>4.2.4.RELEASE</spring-security.version>
<jackson.version>2.9.3</jackson.version>
</properties>

Expand Down Expand Up @@ -77,6 +78,72 @@
<version>2.2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
<version>1.5.9.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

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


import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel;
import com.amazonaws.serverless.proxy.spring.springbootapp.LambdaHandler;
import com.amazonaws.serverless.proxy.spring.springbootapp.TestController;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;


public class SpringBootAppTest {
private LambdaHandler handler = new LambdaHandler();
private MockLambdaContext context = new MockLambdaContext();
private ObjectMapper mapper = new ObjectMapper();

@Test
public void testMethod_springSecurity_doesNotThrowException() {
AwsProxyRequest req = new AwsProxyRequestBuilder("/test", "GET").build();
AwsProxyResponse resp = handler.handleRequest(req, context);
assertNotNull(resp);
validateSingleValueModel(resp, TestController.TEST_VALUE);
}

private void validateSingleValueModel(AwsProxyResponse output, String value) {
try {
SingleValueModel response = mapper.readValue(output.getBody(), SingleValueModel.class);
assertNotNull(response.getValue());
assertEquals(value, response.getValue());
} catch (IOException e) {
fail("Exception while parsing response body: " + e.getMessage());
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.amazonaws.serverless.proxy.spring.springbootapp;


import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler;
import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler;
import com.amazonaws.serverless.proxy.spring.springbootapp.TestApplication;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

import org.springframework.web.context.support.XmlWebApplicationContext;


public class LambdaHandler
implements RequestHandler<AwsProxyRequest, AwsProxyResponse>
{
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
boolean isinitialized = false;

public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context)
{
if (!isinitialized) {
isinitialized = true;
try {
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(TestApplication.class);
} catch (ContainerInitializationException e) {
e.printStackTrace();
return null;
}
}
AwsProxyResponse res = handler.proxy(awsProxyRequest, context);
return res;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.amazonaws.serverless.proxy.spring.springbootapp;


import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication
@ComponentScan(basePackages = "com.amazonaws.serverless.proxy.spring.springbootapp")
public class TestApplication extends SpringBootServletInitializer {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.amazonaws.serverless.proxy.spring.springbootapp;


import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel;

import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RestController
@EnableWebSecurity
public class TestController extends WebSecurityConfigurerAdapter{
public static final String TEST_VALUE = "test";

@RequestMapping(path = "/test", method = { RequestMethod.GET })
public SingleValueModel testGet() {
SingleValueModel value = new SingleValueModel();
value.setValue(TEST_VALUE);
return value;
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.sessionManagement().disable();
}
}

0 comments on commit 54bcef5

Please sign in to comment.