Skip to content

Commit

Permalink
Merge pull request #143 from awslabs/core
Browse files Browse the repository at this point in the history
Preparing for 1.1 release
  • Loading branch information
sapessi authored Apr 6, 2018
2 parents 5f706f2 + 0a81cde commit e6a514b
Show file tree
Hide file tree
Showing 39 changed files with 619 additions and 223 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-jersey</artifactId>
<version>1.0.1</version>
<version>1.1</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<jackson.version>2.9.4</jackson.version>
<jackson.version>2.9.5</jackson.version>
<spark.version>2.7.1</spark.version>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-spark</artifactId>
<version>1.0.1</version>
<version>1.1</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</fileSet>
<fileSet filtered="true">
<directory></directory>
<includes>
Expand Down
61 changes: 56 additions & 5 deletions archetypes/spring/src/main/resources/archetype-resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.3.RELEASE</spring.version>
<junit.version>4.12</junit.version>
<log4j.version>2.8.2</log4j.version>
</properties>

<dependencies>
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-spring</artifactId>
<version>1.0.1</version>
<version>1.1</version>
</dependency>

<dependency>
Expand All @@ -37,10 +39,47 @@
<version>${spring.version}</version>
</dependency>

<!--
the Spring Context Indexer run an annotation processor at compile time and generates
a META-INF/spring.components file that Spring can use to speed up component scanning at boot time.
For small applications, this doesn't make a big difference. However, for large applications with
complex dependencies this may improve your cold start time significantly.
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<version>${spring.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>

<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-log4j2</artifactId>
<version>1.1.0</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

Expand All @@ -52,17 +91,29 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<transformers>
<transformer
implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
</transformer>
</transformers>
</configuration>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>com.github.edwgiz</groupId>
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration packages="com.amazonaws.services.lambda.runtime.log4j2.LambdaAppender">
<Appenders>
<Lambda name="Lambda">
<PatternLayout>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %X{AWSRequestId} %-5p %c{1}:%L - %m%n</pattern>
</PatternLayout>
</Lambda>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Lambda" />
</Root>
</Loggers>
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
<include>**/*.java</include>
</includes>
</fileSet>
<fileSet>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</fileSet>
<fileSet filtered="true" packaged="true">
<directory>src/test/java</directory>
<includes>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<dependency>
<groupId>com.amazonaws.serverless</groupId>
<artifactId>aws-serverless-java-container-spring</artifactId>
<version>1.0.1</version>
<version>1.1</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
@ComponentScan(basePackages = "${groupId}.controller")
public class Application extends SpringBootServletInitializer {

// silence console logging
@Value("${${logging}}")
String message = "";

/*
* Create required HandlerMapping, to avoid several default HandlerMapping instances being created
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Reduce logging level to make sure the application works with SAM local
# https://github.com/awslabs/aws-serverless-java-container/issues/134
logging.level.root=WARN
2 changes: 1 addition & 1 deletion aws-serverless-java-container-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</parent>

<properties>
<jackson.version>2.9.4</jackson.version>
<jackson.version>2.9.5</jackson.version>
<jaxrs.version>2.1</jaxrs.version>
<servlet.version>3.1.0</servlet.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package com.amazonaws.serverless.proxy.internal;


import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.LogFormatter;
import com.amazonaws.serverless.proxy.internal.servlet.ApacheCombinedServletLogFormatter;
import com.amazonaws.serverless.proxy.model.ContainerConfig;
Expand Down Expand Up @@ -100,6 +101,8 @@ protected LambdaContainerHandler(Class<RequestType> requestClass,
this.responseWriter = responseWriter;
this.securityContextWriter = securityContextWriter;
this.exceptionHandler = exceptionHandler;
objectReader = getObjectMapper().readerFor(requestTypeClass);
objectWriter = getObjectMapper().writerFor(responseTypeClass);
}


Expand All @@ -113,6 +116,8 @@ protected LambdaContainerHandler(Class<RequestType> requestClass,
protected abstract void handleRequest(ContainerRequestType containerRequest, ContainerResponseType containerResponse, Context lambdaContext)
throws Exception;

public abstract void initialize()
throws ContainerInitializationException;

//-------------------------------------------------------------
// Methods - Public
Expand Down Expand Up @@ -191,16 +196,9 @@ public void proxyStream(InputStream input, OutputStream output, Context context)
throws IOException {

try {
if (objectReader == null) {
objectReader = getObjectMapper().readerFor(requestTypeClass);
}
RequestType request = objectReader.readValue(input);
ResponseType resp = proxy(request, context);

if (objectWriter == null) {
objectWriter = getObjectMapper().writerFor(responseTypeClass);
}

objectWriter.writeValue(output, resp);
} catch (JsonParseException e) {
log.error("Error while parsing request object stream", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;


/**
Expand All @@ -18,6 +21,51 @@
public final class SecurityUtils {
private static Logger log = LoggerFactory.getLogger(SecurityUtils.class);

private static Set<String> SCHEMES = new HashSet<String>() {{
add("http");
add("https");
add("HTTP");
add("HTTPS");
}};

private static Set<Integer> PORTS = new HashSet<Integer>() {{
add(443);
add(80);
add(3000); // we allow port 3000 for SAM local
}};

public static boolean isValidPort(String port) {
if (port == null) {
return false;
}
try {
int intPort = Integer.parseInt(port);
return PORTS.contains(intPort);
} catch (NumberFormatException e) {
log.error("Invalid port parameter: " + crlf(port));
return false;
}
}

public static boolean isValidScheme(String scheme) {
return SCHEMES.contains(scheme);
}

public static boolean isValidHost(String host, String apiId, String region) {
if (host == null) {
return false;
}
if (host.endsWith(".amazonaws.com")) {
String defaultHost = new StringBuilder().append(apiId)
.append(".execute-api.")
.append(region)
.append(".amazonaws.com").toString();
return host.equals(defaultHost);
} else {
return LambdaContainerHandler.getContainerConfig().getCustomDomainNames().contains(host);
}
}

/**
* Replaces CRLF characters in a string with empty string ("").
* @param s The string to be cleaned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public abstract class AwsHttpServletRequest implements HttpServletRequest {
// We need this to pickup the protocol from the CloudFront header since Lambda doesn't receive this
// information from anywhere else
static final String CF_PROTOCOL_HEADER_NAME = "CloudFront-Forwarded-Proto";
static final String PROTOCOL_HEADER_NAME = "X-Forwarded-Proto";
static final String HOST_HEADER_NAME = "Host";
static final String PORT_HEADER_NAME = "X-Forwarded-Port";


//-------------------------------------------------------------
Expand Down Expand Up @@ -296,23 +299,7 @@ protected String generateQueryString(Map<String, String> parameters) {
}

queryString = parameters.keySet().stream()
.map(key -> {
String newKey = key;
String newValue = parameters.get(key);
try {
if (!URLEncoder.encode(newKey, StandardCharsets.UTF_8.name()).equals(newKey)) {
newKey = URLEncoder.encode(key, StandardCharsets.UTF_8.name());
}

if (!URLEncoder.encode(newValue, StandardCharsets.UTF_8.name()).equals(newValue)) {
newValue = URLEncoder.encode(newValue, StandardCharsets.UTF_8.name());
}
} catch (UnsupportedEncodingException e) {
log.error(SecurityUtils.crlf("Could not URLEncode: " + newKey), e);

}
return newKey + "=" + newValue;
})
.map(key -> key + "=" + parameters.get(key))
.collect(Collectors.joining("&"));
return queryString;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public abstract class AwsLambdaServletContainerHandler<RequestType, ResponseType
//-------------------------------------------------------------
// Variables - Private
//-------------------------------------------------------------

protected ServletContext servletContext;
private Logger log = LoggerFactory.getLogger(AwsLambdaServletContainerHandler.class);
private FilterChainManager<AwsServletContext> filterChainManager;
private boolean startupExecuted;

//-------------------------------------------------------------
// Variables - Protected
//-------------------------------------------------------------
protected StartupHandler startupHandler;
private FilterChainManager<AwsServletContext> filterChainManager;
protected ServletContext servletContext;


//-------------------------------------------------------------
Expand All @@ -78,6 +78,8 @@ protected AwsLambdaServletContainerHandler(Class<RequestType> requestTypeClass,
super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, exceptionHandler);
// set the default log formatter for servlet implementations
setLogFormatter(new ApacheCombinedServletLogFormatter<>());
setServletContext(new AwsServletContext(this));
startupExecuted = false;
}

//-------------------------------------------------------------
Expand Down Expand Up @@ -154,17 +156,7 @@ private HttpServletResponse getServletResponse(ContainerResponseType resp) {
*/
public void onStartup(final StartupHandler h) {
startupHandler = h;
}

@Override
protected void handleRequest(ContainerRequestType containerRequest, ContainerResponseType containerResponse, Context lambdaContext)
throws Exception {
// The servlet context should not be linked to a specific request object, only to the Lambda
// context so we only set it once.
// TODO: In the future, if we decide to support multiple servlets/contexts in an instance we only need to modify this method
if (getServletContext() == null) {
setServletContext(new AwsServletContext(this));
}
startupHandler.onStartup(getServletContext());
}


Expand Down
Loading

0 comments on commit e6a514b

Please sign in to comment.