Fix the issue that api gateway handler of AWS does not honor the encrypted body data.
Resolves #382
This commit is contained in:
committed by
Oleg Zhurakousky
parent
1cf42f95f6
commit
1de8526b7a
@@ -25,6 +25,7 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
|
||||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.function.context.catalog.FunctionInspector;
|
||||
@@ -58,7 +59,7 @@ public class SpringBootApiGatewayRequestHandler extends
|
||||
|
||||
@Override
|
||||
protected Object convertEvent(APIGatewayProxyRequestEvent event) {
|
||||
Object deserializedBody = event.getBody() != null ? deserializeBody(event.getBody()) : Optional.empty();
|
||||
Object deserializedBody = event.getBody() != null ? deserializeBody(event) : Optional.empty();
|
||||
return functionAcceptsMessage()
|
||||
? new GenericMessage<>(deserializedBody, getHeaders(event))
|
||||
: deserializedBody;
|
||||
@@ -68,9 +69,12 @@ public class SpringBootApiGatewayRequestHandler extends
|
||||
return this.inspector.isMessage(function());
|
||||
}
|
||||
|
||||
private Object deserializeBody(String json) {
|
||||
private Object deserializeBody(APIGatewayProxyRequestEvent event) {
|
||||
try {
|
||||
return this.mapper.readValue(json, getInputType());
|
||||
return this.mapper.readValue(
|
||||
(event.getIsBase64Encoded() != null && event.getIsBase64Encoded())
|
||||
? new String(Base64.decodeBase64(event.getBody())) : event.getBody(),
|
||||
getInputType());
|
||||
}
|
||||
catch (Exception e) {
|
||||
throw new IllegalStateException("Cannot convert event", e);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.cloud.function.adapter.aws;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -72,6 +73,18 @@ public class SpringBootApiGatewayRequestHandlerTests {
|
||||
.isEqualTo(200);
|
||||
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
|
||||
.isEqualTo("{\"value\":\"FOO\"}");
|
||||
|
||||
APIGatewayProxyRequestEvent bodyEncryptedRequest = new APIGatewayProxyRequestEvent();
|
||||
bodyEncryptedRequest.setBody(
|
||||
Base64.getEncoder().encodeToString("{\"value\":\"foo\"}".getBytes()));
|
||||
bodyEncryptedRequest.setIsBase64Encoded(true);
|
||||
|
||||
output = this.handler.handleRequest(bodyEncryptedRequest, null);
|
||||
assertThat(output).isInstanceOf(APIGatewayProxyResponseEvent.class);
|
||||
assertThat(((APIGatewayProxyResponseEvent) output).getStatusCode())
|
||||
.isEqualTo(200);
|
||||
assertThat(((APIGatewayProxyResponseEvent) output).getBody())
|
||||
.isEqualTo("{\"value\":\"FOO\"}");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user