GH-434 Added generic FunctionInvoker for AWS

- Added generic FunctionInvoker capable of handling the request generically without requiring user to implemen specific AWS request handler

Resolves #434
This commit is contained in:
Oleg Zhurakousky
2019-12-05 19:28:54 +01:00
parent 0f38ea47b8
commit 52b0fdea50
15 changed files with 641 additions and 234 deletions

View File

@@ -0,0 +1,143 @@
/*
* Copyright 2019-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.adapter.aws;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestStreamHandler;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.cloud.function.utils.FunctionClassUtils;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;
/**
*
* @author Oleg Zhurakousky
* @since 3.1
*
* see
* https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format
*/
public class FunctionInvoker implements RequestStreamHandler {
private static Log logger = LogFactory.getLog(FunctionInvoker.class);
private ObjectMapper mapper;
private boolean started;
private Function<Message<byte[]>, Message<byte[]>> function;
@Override
public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
if (!this.started) {
this.start();
}
Message<byte[]> requestMessage = this.generateMessage(input, context);
Message<byte[]> responseMessage = this.function.apply(requestMessage);
byte[] responseBytes = responseMessage.getPayload();
if (requestMessage.getHeaders().containsKey("httpMethod")) {
Map<String, Object> response = new HashMap<String, Object>();
response.put("isBase64Encoded", false);
response.put("statusCode", 200);
response.put("body", new String(responseMessage.getPayload(), StandardCharsets.UTF_8));
response.put("headers", Collections.singletonMap("foo", "bar"));
responseBytes = mapper.writeValueAsBytes(response);
}
StreamUtils.copy(responseBytes, output);
}
private void start() {
ConfigurableApplicationContext context = SpringApplication.run(FunctionClassUtils.getStartClass());
Environment environment = context.getEnvironment();
String functionName = environment.getProperty("spring.cloud.function.definition");
FunctionCatalog functionCatalog = context.getBean(FunctionCatalog.class);
this.mapper = context.getBean(ObjectMapper.class);
this.configureObjectMapper();
if (logger.isInfoEnabled()) {
logger.info("Locating function: '" + functionName + "'");
}
this.function = functionCatalog.lookup(functionName, "application/json");
Assert.notNull(this.function, "Failed to lookup function " + functionName);
if (!StringUtils.hasText(functionName)) {
FunctionInspector inspector = context.getBean(FunctionInspector.class);
functionName = inspector.getRegistration(this.function).getNames().toString();
}
if (logger.isInfoEnabled()) {
logger.info("Located function: '" + functionName + "'");
}
this.started = true;
}
private void configureObjectMapper() {
SimpleModule module = new SimpleModule();
module.addDeserializer(Date.class, new JsonDeserializer<Date>() {
@Override
public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext)
throws IOException {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(jsonParser.getValueAsLong());
return calendar.getTime();
}
});
mapper.registerModule(module);
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
}
private Message<byte[]> generateMessage(InputStream input, Context context) throws IOException {
byte[] payload = StreamUtils.copyToByteArray(input);
Message<byte[]> message = MessageBuilder.withPayload(payload).setHeader("aws-context", context).build();
return message;
}
}

View File

@@ -0,0 +1,154 @@
/*
* Copyright 2012-2019 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.adapter.aws;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.function.Function;
import com.amazonaws.services.lambda.runtime.events.KinesisEvent;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import static org.assertj.core.api.Assertions.assertThat;
/**
*
* @author Oleg Zhurakousky
*
*/
public class FunctionInvokerTests {
String sampleEvent = "{" +
" \"Records\": [" +
" {" +
" \"kinesis\": {" +
" \"kinesisSchemaVersion\": \"1.0\"," +
" \"partitionKey\": \"1\"," +
" \"sequenceNumber\": \"49590338271490256608559692538361571095921575989136588898\"," +
" \"data\": \"SGVsbG8sIHRoaXMgaXMgYSB0ZXN0Lg==\"," +
" \"approximateArrivalTimestamp\": 1545084650.987" +
" }," +
" \"eventSource\": \"aws:kinesis\"," +
" \"eventVersion\": \"1.0\"," +
" \"eventID\": \"shardId-000000000006:49590338271490256608559692538361571095921575989136588898\"," +
" \"eventName\": \"aws:kinesis:record\"," +
" \"invokeIdentityArn\": \"arn:aws:iam::123456789012:role/lambda-role\"," +
" \"awsRegion\": \"us-east-2\"," +
" \"eventSourceARN\": \"arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream\"" +
" }," +
" {" +
" \"kinesis\": {" +
" \"kinesisSchemaVersion\": \"1.0\"," +
" \"partitionKey\": \"1\"," +
" \"sequenceNumber\": \"49590338271490256608559692540925702759324208523137515618\"," +
" \"data\": \"VGhpcyBpcyBvbmx5IGEgdGVzdC4=\"," +
" \"approximateArrivalTimestamp\": 1545084711.166" +
" }," +
" \"eventSource\": \"aws:kinesis\"," +
" \"eventVersion\": \"1.0\"," +
" \"eventID\": \"shardId-000000000006:49590338271490256608559692540925702759324208523137515618\"," +
" \"eventName\": \"aws:kinesis:record\"," +
" \"invokeIdentityArn\": \"arn:aws:iam::123456789012:role/lambda-role\"," +
" \"awsRegion\": \"us-east-2\"," +
" \"eventSourceARN\": \"arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream\"" +
" }" +
" ]" +
"}";
@Test
public void testKinesisStringMessageEvent() throws Exception {
System.setProperty("MAIN_CLASS", KinesisConfiguration.class.getName());
System.setProperty("spring.cloud.function.definition", "echoStringMessage");
FunctionInvoker invoker = new FunctionInvoker();
InputStream targetStream = new ByteArrayInputStream(this.sampleEvent.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);
String result = new String(output.toByteArray(), StandardCharsets.UTF_8);
assertThat(result).isEqualTo(this.sampleEvent);
}
@Test
public void testKinesisStringEvent() throws Exception {
System.setProperty("MAIN_CLASS", KinesisConfiguration.class.getName());
System.setProperty("spring.cloud.function.definition", "echoStringMessage");
FunctionInvoker invoker = new FunctionInvoker();
InputStream targetStream = new ByteArrayInputStream(this.sampleEvent.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);
String result = new String(output.toByteArray(), StandardCharsets.UTF_8);
System.out.println(result);
assertThat(result).isEqualTo(this.sampleEvent);
}
@Test
public void testKinesisEvent() throws Exception {
System.setProperty("MAIN_CLASS", KinesisConfiguration.class.getName());
System.setProperty("spring.cloud.function.definition", "echoKinesisEvent");
FunctionInvoker invoker = new FunctionInvoker();
InputStream targetStream = new ByteArrayInputStream(this.sampleEvent.getBytes());
ByteArrayOutputStream output = new ByteArrayOutputStream();
invoker.handleRequest(targetStream, output, null);
String result = new String(output.toByteArray(), StandardCharsets.UTF_8);
System.out.println(result);
assertThat(result).contains("\"sequenceNumber\":\"49590338271490256608559692538361571095921575989136588898\"");
}
@EnableAutoConfiguration
@Configuration
public static class KinesisConfiguration {
@Bean
public Function<Message<String>, Message<String>> echoStringMessage() {
return v -> {
System.out.println("Received: " + v);
return v;
};
}
@Bean
public Function<String, String> echoString() {
return v -> {
System.out.println("Received: " + v);
return v;
};
}
@Bean
public Function<KinesisEvent, KinesisEvent> echoKinesisEvent() {
return v -> {
System.out.println("Received: " + v);
return v;
};
}
}
}