Skip to content

Commit 21a6cf1

Browse files
authored
Merge pull request #114 from awslabs/spring
Spring security tests
2 parents 805ccbd + 54bcef5 commit 21a6cf1

File tree

6 files changed

+196
-0
lines changed

6 files changed

+196
-0
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsHttpServletRequest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public String getRequestedSessionId() {
107107

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

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

123125

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

129132

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

135139

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

141146

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

147153

148154
@Override
149155
@Deprecated
150156
public boolean isRequestedSessionIdFromUrl() {
157+
log.warn("Trying to access session. Lambda functions are stateless and should not rely on the session");
151158
return false;
152159
}
153160

aws-serverless-java-container-spring/pom.xml

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<properties>
1818
<spring.version>4.3.13.RELEASE</spring.version>
19+
<spring-security.version>4.2.4.RELEASE</spring-security.version>
1920
<jackson.version>2.9.3</jackson.version>
2021
</properties>
2122

@@ -77,6 +78,72 @@
7778
<version>2.2.4</version>
7879
<scope>test</scope>
7980
</dependency>
81+
<dependency>
82+
<groupId>org.springframework.boot</groupId>
83+
<artifactId>spring-boot-autoconfigure</artifactId>
84+
<version>1.5.9.RELEASE</version>
85+
<scope>test</scope>
86+
</dependency>
87+
<dependency>
88+
<groupId>org.springframework.security</groupId>
89+
<artifactId>spring-security-config</artifactId>
90+
<version>${spring-security.version}</version>
91+
<scope>test</scope>
92+
<exclusions>
93+
<exclusion>
94+
<groupId>org.springframework</groupId>
95+
<artifactId>spring-aop</artifactId>
96+
</exclusion>
97+
<exclusion>
98+
<groupId>org.springframework</groupId>
99+
<artifactId>spring-expression</artifactId>
100+
</exclusion>
101+
<exclusion>
102+
<groupId>org.springframework</groupId>
103+
<artifactId>spring-context</artifactId>
104+
</exclusion>
105+
<exclusion>
106+
<groupId>org.springframework</groupId>
107+
<artifactId>spring-beans</artifactId>
108+
</exclusion>
109+
<exclusion>
110+
<groupId>org.springframework</groupId>
111+
<artifactId>spring-core</artifactId>
112+
</exclusion>
113+
</exclusions>
114+
</dependency>
115+
<dependency>
116+
<groupId>org.springframework.security</groupId>
117+
<artifactId>spring-security-web</artifactId>
118+
<version>${spring-security.version}</version>
119+
<scope>test</scope>
120+
<exclusions>
121+
<exclusion>
122+
<groupId>org.springframework</groupId>
123+
<artifactId>spring-aop</artifactId>
124+
</exclusion>
125+
<exclusion>
126+
<groupId>org.springframework</groupId>
127+
<artifactId>spring-expression</artifactId>
128+
</exclusion>
129+
<exclusion>
130+
<groupId>org.springframework</groupId>
131+
<artifactId>spring-context</artifactId>
132+
</exclusion>
133+
<exclusion>
134+
<groupId>org.springframework</groupId>
135+
<artifactId>spring-beans</artifactId>
136+
</exclusion>
137+
<exclusion>
138+
<groupId>org.springframework</groupId>
139+
<artifactId>spring-core</artifactId>
140+
</exclusion>
141+
<exclusion>
142+
<groupId>org.springframework</groupId>
143+
<artifactId>spring-web</artifactId>
144+
</exclusion>
145+
</exclusions>
146+
</dependency>
80147
</dependencies>
81148

82149
<reporting>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.amazonaws.serverless.proxy.spring;
2+
3+
4+
import com.amazonaws.serverless.proxy.internal.testutils.AwsProxyRequestBuilder;
5+
import com.amazonaws.serverless.proxy.internal.testutils.MockLambdaContext;
6+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
7+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
8+
import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel;
9+
import com.amazonaws.serverless.proxy.spring.springbootapp.LambdaHandler;
10+
import com.amazonaws.serverless.proxy.spring.springbootapp.TestController;
11+
12+
import com.fasterxml.jackson.databind.ObjectMapper;
13+
import org.junit.Test;
14+
15+
import java.io.IOException;
16+
17+
import static org.junit.Assert.*;
18+
19+
20+
public class SpringBootAppTest {
21+
private LambdaHandler handler = new LambdaHandler();
22+
private MockLambdaContext context = new MockLambdaContext();
23+
private ObjectMapper mapper = new ObjectMapper();
24+
25+
@Test
26+
public void testMethod_springSecurity_doesNotThrowException() {
27+
AwsProxyRequest req = new AwsProxyRequestBuilder("/test", "GET").build();
28+
AwsProxyResponse resp = handler.handleRequest(req, context);
29+
assertNotNull(resp);
30+
validateSingleValueModel(resp, TestController.TEST_VALUE);
31+
}
32+
33+
private void validateSingleValueModel(AwsProxyResponse output, String value) {
34+
try {
35+
SingleValueModel response = mapper.readValue(output.getBody(), SingleValueModel.class);
36+
assertNotNull(response.getValue());
37+
assertEquals(value, response.getValue());
38+
} catch (IOException e) {
39+
fail("Exception while parsing response body: " + e.getMessage());
40+
e.printStackTrace();
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.amazonaws.serverless.proxy.spring.springbootapp;
2+
3+
4+
import com.amazonaws.serverless.exceptions.ContainerInitializationException;
5+
import com.amazonaws.serverless.proxy.model.AwsProxyRequest;
6+
import com.amazonaws.serverless.proxy.model.AwsProxyResponse;
7+
import com.amazonaws.serverless.proxy.spring.SpringBootLambdaContainerHandler;
8+
import com.amazonaws.serverless.proxy.spring.SpringLambdaContainerHandler;
9+
import com.amazonaws.serverless.proxy.spring.springbootapp.TestApplication;
10+
import com.amazonaws.services.lambda.runtime.Context;
11+
import com.amazonaws.services.lambda.runtime.RequestHandler;
12+
13+
import org.springframework.web.context.support.XmlWebApplicationContext;
14+
15+
16+
public class LambdaHandler
17+
implements RequestHandler<AwsProxyRequest, AwsProxyResponse>
18+
{
19+
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;
20+
boolean isinitialized = false;
21+
22+
public AwsProxyResponse handleRequest(AwsProxyRequest awsProxyRequest, Context context)
23+
{
24+
if (!isinitialized) {
25+
isinitialized = true;
26+
try {
27+
handler = SpringBootLambdaContainerHandler.getAwsProxyHandler(TestApplication.class);
28+
} catch (ContainerInitializationException e) {
29+
e.printStackTrace();
30+
return null;
31+
}
32+
}
33+
AwsProxyResponse res = handler.proxy(awsProxyRequest, context);
34+
return res;
35+
}
36+
}
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.amazonaws.serverless.proxy.spring.springbootapp;
2+
3+
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.web.support.SpringBootServletInitializer;
6+
import org.springframework.context.annotation.ComponentScan;
7+
8+
9+
@SpringBootApplication
10+
@ComponentScan(basePackages = "com.amazonaws.serverless.proxy.spring.springbootapp")
11+
public class TestApplication extends SpringBootServletInitializer {
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.amazonaws.serverless.proxy.spring.springbootapp;
2+
3+
4+
import com.amazonaws.serverless.proxy.spring.echoapp.model.SingleValueModel;
5+
6+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
7+
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
8+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
14+
@RestController
15+
@EnableWebSecurity
16+
public class TestController extends WebSecurityConfigurerAdapter{
17+
public static final String TEST_VALUE = "test";
18+
19+
@RequestMapping(path = "/test", method = { RequestMethod.GET })
20+
public SingleValueModel testGet() {
21+
SingleValueModel value = new SingleValueModel();
22+
value.setValue(TEST_VALUE);
23+
return value;
24+
}
25+
26+
@Override
27+
protected void configure(HttpSecurity http) throws Exception {
28+
http.sessionManagement().disable();
29+
}
30+
}

0 commit comments

Comments
 (0)