Skip to content

Commit 6e1a2b0

Browse files
authored
Merge branch 'master' into fix_headers_insensitive
2 parents 72a0ecf + 477d3b0 commit 6e1a2b0

File tree

10 files changed

+362
-13
lines changed

10 files changed

+362
-13
lines changed

aws-lambda-java-events/src/main/java/com/amazonaws/services/lambda/runtime/events/IamPolicyResponse.java

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
import java.util.List;
1212
import java.util.Map;
1313

14+
/**
15+
* The IAM Policy Response required for API Gateway HTTP APIs
16+
*
17+
* https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-lambda-authorizer.html
18+
*
19+
*/
20+
1421
@Data
1522
@Builder(setterPrefix = "with")
1623
@NoArgsConstructor
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.amazonaws.services.lambda.runtime.events;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
import java.io.Serializable;
9+
import java.util.Collections;
10+
import java.util.HashMap;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
/**
15+
* The IAM Policy Response required for API Gateway REST APIs
16+
*
17+
* https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-lambda-authorizer-output.html
18+
*
19+
*/
20+
21+
@Data
22+
@Builder(setterPrefix = "with")
23+
@NoArgsConstructor
24+
@AllArgsConstructor
25+
public class IamPolicyResponseV1 implements Serializable, Cloneable {
26+
27+
public static final String EXECUTE_API_INVOKE = "execute-api:Invoke";
28+
public static final String VERSION_2012_10_17 = "2012-10-17";
29+
public static final String ALLOW = "Allow";
30+
public static final String DENY = "Deny";
31+
32+
private String principalId;
33+
private PolicyDocument policyDocument;
34+
private Map<String, Object> context;
35+
private String usageIdentifierKey;
36+
37+
public Map<String, Object> getPolicyDocument() {
38+
Map<String, Object> serializablePolicy = new HashMap<>();
39+
serializablePolicy.put("Version", policyDocument.getVersion());
40+
41+
int numberOfStatements = policyDocument.getStatement().size();
42+
Map<String, Object>[] serializableStatementArray = new Map[numberOfStatements];
43+
for (int i = 0; i < numberOfStatements; i++) {
44+
Statement statement = policyDocument.getStatement().get(i);
45+
Map<String, Object> serializableStatement = new HashMap<>();
46+
serializableStatement.put("Effect", statement.getEffect());
47+
serializableStatement.put("Action", statement.getAction());
48+
serializableStatement.put("Resource", statement.getResource().toArray(new String[0]));
49+
serializableStatement.put("Condition", statement.getCondition());
50+
serializableStatementArray[i] = serializableStatement;
51+
}
52+
serializablePolicy.put("Statement", serializableStatementArray);
53+
return serializablePolicy;
54+
}
55+
56+
public static Statement allowStatement(String resource) {
57+
return Statement.builder()
58+
.withEffect(ALLOW)
59+
.withResource(Collections.singletonList(resource))
60+
.withAction(EXECUTE_API_INVOKE)
61+
.build();
62+
}
63+
64+
public static Statement denyStatement(String resource) {
65+
return Statement.builder()
66+
.withEffect(DENY)
67+
.withResource(Collections.singletonList(resource))
68+
.withAction(EXECUTE_API_INVOKE)
69+
.build();
70+
}
71+
72+
@Data
73+
@Builder(setterPrefix = "with")
74+
@NoArgsConstructor
75+
@AllArgsConstructor
76+
public static class PolicyDocument implements Serializable, Cloneable {
77+
78+
private String version;
79+
private List<Statement> statement;
80+
}
81+
82+
@Data
83+
@Builder(setterPrefix = "with")
84+
@NoArgsConstructor
85+
@AllArgsConstructor
86+
public static class Statement implements Serializable, Cloneable {
87+
88+
private String action;
89+
private String effect;
90+
private List<String> resource;
91+
private Map<String, Map<String, Object>> condition;
92+
}
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.amazonaws.services.lambda.runtime.events;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.io.IOException;
8+
import java.nio.charset.StandardCharsets;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import static com.amazonaws.services.lambda.runtime.events.IamPolicyResponseV1.ALLOW;
16+
import static com.amazonaws.services.lambda.runtime.events.IamPolicyResponseV1.EXECUTE_API_INVOKE;
17+
import static com.amazonaws.services.lambda.runtime.events.IamPolicyResponseV1.VERSION_2012_10_17;
18+
import static com.amazonaws.services.lambda.runtime.events.IamPolicyResponseV1.allowStatement;
19+
import static com.amazonaws.services.lambda.runtime.events.IamPolicyResponseV1.denyStatement;
20+
import static java.util.Collections.singletonList;
21+
import static java.util.Collections.singletonMap;
22+
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
23+
24+
public class IamPolicyResponseV1Test {
25+
26+
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
27+
28+
@Test
29+
public void testAllowStatement() throws JsonProcessingException {
30+
IamPolicyResponseV1 iamPolicyResponse = IamPolicyResponseV1.builder()
31+
.withPrincipalId("me")
32+
.withPolicyDocument(IamPolicyResponseV1.PolicyDocument.builder()
33+
.withVersion(VERSION_2012_10_17)
34+
.withStatement(singletonList(allowStatement("arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*")))
35+
.build())
36+
.withUsageIdentifierKey("123ABC")
37+
.build();
38+
39+
String json = OBJECT_MAPPER.writeValueAsString(iamPolicyResponse);
40+
41+
assertThatJson(json).isEqualTo(readResource("iamPolicyV1Responses/allow.json"));
42+
}
43+
44+
@Test
45+
public void testDenyStatement() throws JsonProcessingException {
46+
IamPolicyResponseV1 iamPolicyResponse = IamPolicyResponseV1.builder()
47+
.withPrincipalId("me")
48+
.withPolicyDocument(IamPolicyResponseV1.PolicyDocument.builder()
49+
.withVersion(VERSION_2012_10_17)
50+
.withStatement(singletonList(denyStatement("arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*")))
51+
.build())
52+
.withUsageIdentifierKey("123ABC")
53+
.build();
54+
55+
String json = OBJECT_MAPPER.writeValueAsString(iamPolicyResponse);
56+
57+
assertThatJson(json).isEqualTo(readResource("iamPolicyV1Responses/deny.json"));
58+
}
59+
60+
@Test
61+
public void testStatementWithCondition() throws JsonProcessingException {
62+
Map<String, Map<String, Object>> conditions = new HashMap<>();
63+
conditions.put("DateGreaterThan", singletonMap("aws:TokenIssueTime", "2020-01-01T00:00:01Z"));
64+
65+
IamPolicyResponseV1 iamPolicyResponse = IamPolicyResponseV1.builder()
66+
.withPrincipalId("me")
67+
.withPolicyDocument(IamPolicyResponseV1.PolicyDocument.builder()
68+
.withVersion(VERSION_2012_10_17)
69+
.withStatement(singletonList(IamPolicyResponseV1.Statement.builder()
70+
.withAction(EXECUTE_API_INVOKE)
71+
.withEffect(ALLOW)
72+
.withResource(singletonList("arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*"))
73+
.withCondition(conditions)
74+
.build()))
75+
.build())
76+
.withUsageIdentifierKey("123ABC")
77+
.build();
78+
79+
String json = OBJECT_MAPPER.writeValueAsString(iamPolicyResponse);
80+
81+
assertThatJson(json).isEqualTo(readResource("iamPolicyV1Responses/allow-with-condition.json"));
82+
}
83+
84+
private String readResource(String name) {
85+
Path filePath = Paths.get("src", "test", "resources", name);
86+
byte[] bytes = new byte[0];
87+
try {
88+
bytes = Files.readAllBytes(filePath);
89+
} catch (IOException e) {
90+
e.printStackTrace();
91+
}
92+
return new String(bytes, StandardCharsets.UTF_8);
93+
}
94+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"principalId": "me",
3+
"policyDocument": {
4+
"Version": "2012-10-17",
5+
"Statement": [{
6+
"Action": "execute-api:Invoke",
7+
"Resource": ["arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*"],
8+
"Effect": "Allow",
9+
"Condition": {"DateGreaterThan": {"aws:TokenIssueTime": "2020-01-01T00:00:01Z"}}
10+
}]
11+
},
12+
"context":null,
13+
"usageIdentifierKey": "123ABC"
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"principalId": "me",
3+
"policyDocument": {
4+
"Version": "2012-10-17",
5+
"Statement": [{
6+
"Action": "execute-api:Invoke",
7+
"Resource": ["arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*"],
8+
"Effect": "Allow",
9+
"Condition": null
10+
}]
11+
},
12+
"context":null,
13+
"usageIdentifierKey": "123ABC"
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"principalId": "me",
3+
"policyDocument": {
4+
"Version": "2012-10-17",
5+
"Statement": [{
6+
"Action": "execute-api:Invoke",
7+
"Resource": ["arn:aws:execute-api:eu-west-1:123456789012:1234abc/$deafult/*/*"],
8+
"Effect": "Deny",
9+
"Condition": null
10+
}]
11+
},
12+
"context":null,
13+
"usageIdentifierKey": "123ABC"
14+
}

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/AWSLambda.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ private static void startRuntime(String handler, LambdaLogger lambdaLogger) thro
203203
System.setErr(new PrintStream(new LambdaOutputStream(System.err), false, "UTF-8"));
204204
setupRuntimeLogger(lambdaLogger);
205205

206-
String runtimeApi = getEnvOrExit("AWS_LAMBDA_RUNTIME_API");
206+
String runtimeApi = getEnvOrExit(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_RUNTIME_API);
207207
LambdaRuntimeClient runtimeClient = new LambdaRuntimeClient(runtimeApi);
208208

209209
EnvReader envReader = new EnvReader();

aws-lambda-java-runtime-interface-client/src/main/java/com/amazonaws/services/lambda/runtime/api/client/LambdaEnvironment.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
public class LambdaEnvironment {
1010
public static final EnvReader ENV_READER = new EnvReader();
11-
public static final int MEMORY_LIMIT = parseInt(ENV_READER.getEnvOrDefault("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128"));
12-
public static final String LOG_GROUP_NAME = ENV_READER.getEnv("AWS_LAMBDA_LOG_GROUP_NAME");
13-
public static final String LOG_STREAM_NAME = ENV_READER.getEnv("AWS_LAMBDA_LOG_STREAM_NAME");
14-
public static final String FUNCTION_NAME = ENV_READER.getEnv("AWS_LAMBDA_FUNCTION_NAME");
15-
public static final String FUNCTION_VERSION = ENV_READER.getEnv("AWS_LAMBDA_FUNCTION_VERSION");
11+
public static final int MEMORY_LIMIT = parseInt(ENV_READER.getEnvOrDefault(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_MEMORY_SIZE, "128"));
12+
public static final String LOG_GROUP_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_LOG_GROUP_NAME);
13+
public static final String LOG_STREAM_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_LOG_STREAM_NAME);
14+
public static final String FUNCTION_NAME = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_NAME);
15+
public static final String FUNCTION_VERSION = ENV_READER.getEnv(ReservedRuntimeEnvironmentVariables.AWS_LAMBDA_FUNCTION_VERSION);
1616
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2017-2020 original authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.amazonaws.services.lambda.runtime.api.client;
17+
18+
/**
19+
* Lambda runtimes set several environment variables during initialization.
20+
* Most of the environment variables provide information about the function or runtime.
21+
* The keys for these environment variables are reserved and cannot be set in your function configuration.
22+
* @see <a href="https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime">Using AWS Lambda Environment Variables</a>
23+
*
24+
* NOTICE: This class is forked from io.micronaut.function.aws.runtime.ReservedRuntimeEnvironments found at https://github.com/micronaut-projects/micronaut-aws
25+
*
26+
*/
27+
public interface ReservedRuntimeEnvironmentVariables {
28+
29+
/**
30+
* The handler location configured on the function.
31+
*/
32+
String HANDLER = "_HANDLER";
33+
34+
/**
35+
* The AWS Region where the Lambda function is executed.
36+
*/
37+
String AWS_REGION = "AWS_REGION";
38+
39+
/**
40+
* The runtime identifier, prefixed by AWS_Lambda_—for example, AWS_Lambda_java8.
41+
*/
42+
String AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV";
43+
44+
/**
45+
* The name of the function.
46+
*/
47+
String AWS_LAMBDA_FUNCTION_NAME = "AWS_LAMBDA_FUNCTION_NAME";
48+
49+
/**
50+
* The amount of memory available to the function in MB.
51+
*/
52+
String AWS_LAMBDA_FUNCTION_MEMORY_SIZE = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE";
53+
54+
/**
55+
* The version of the function being executed.
56+
*/
57+
String AWS_LAMBDA_FUNCTION_VERSION = "AWS_LAMBDA_FUNCTION_VERSION";
58+
59+
/**
60+
* The name of the Amazon CloudWatch Logs group for the function.
61+
*/
62+
String AWS_LAMBDA_LOG_GROUP_NAME = "AWS_LAMBDA_LOG_GROUP_NAME";
63+
64+
/**
65+
* The name of the Amazon CloudWatch stream for the function.
66+
*/
67+
String AWS_LAMBDA_LOG_STREAM_NAME = "AWS_LAMBDA_LOG_STREAM_NAME";
68+
69+
/**
70+
* Access key id obtained from the function's execution role.
71+
*/
72+
String AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID";
73+
74+
/**
75+
* secret access key obtained from the function's execution role.
76+
*/
77+
String AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY";
78+
79+
/**
80+
*
81+
* The access keys obtained from the function's execution role.
82+
*/
83+
String AWS_SESSION_TOKEN = "AWS_SESSION_TOKEN";
84+
85+
/**
86+
* (Custom runtime) The host and port of the runtime API.
87+
*/
88+
String AWS_LAMBDA_RUNTIME_API = "AWS_LAMBDA_RUNTIME_API";
89+
90+
/**
91+
* The path to your Lambda function code.
92+
*/
93+
String LAMBDA_TASK_ROOT = "LAMBDA_TASK_ROOT";
94+
95+
/**
96+
* The path to runtime libraries.
97+
*/
98+
String LAMBDA_RUNTIME_DIR = "LAMBDA_RUNTIME_DIR";
99+
100+
/**
101+
* The environment's time zone (UTC). The execution environment uses NTP to synchronize the system clock.
102+
*/
103+
String TZ = "TZ";
104+
}

0 commit comments

Comments
 (0)