-
Notifications
You must be signed in to change notification settings - Fork 364
/
Copy pathAuthPolicy.java
281 lines (232 loc) · 10.9 KB
/
AuthPolicy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file 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 io;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* AuthPolicy receives a set of allowed and denied methods and generates a valid
* AWS policy for the API Gateway authorizer. The constructor receives the calling
* user principal, the AWS account ID of the API owner, and an apiOptions object.
* The apiOptions can contain an API Gateway RestApi Id, a region for the RestApi, and a
* stage that calls should be allowed/denied for. For example
*
* new AuthPolicy(principalId, AuthPolicy.PolicyDocument.getDenyAllPolicy(region, awsAccountId, restApiId, stage));
*
* @author Jack Kohn
*/
public class AuthPolicy {
// IAM Policy Constants
public static final String VERSION = "Version";
public static final String STATEMENT = "Statement";
public static final String EFFECT = "Effect";
public static final String ACTION = "Action";
public static final String NOT_ACTION = "NotAction";
public static final String RESOURCE = "Resource";
public static final String NOT_RESOURCE = "NotResource";
public static final String CONDITION = "Condition";
String principalId;
transient AuthPolicy.PolicyDocument policyDocumentObject;
Map<String, Object> policyDocument;
public AuthPolicy(String principalId, AuthPolicy.PolicyDocument policyDocumentObject) {
this.principalId = principalId;
this.policyDocumentObject = policyDocumentObject;
}
public AuthPolicy() {
}
public String getPrincipalId() {
return principalId;
}
public void setPrincipalId(String principalId) {
this.principalId = principalId;
}
/**
* IAM Policies use capitalized field names, but Lambda by default will serialize object members using camel case
*
* This method implements a custom serializer to return the IAM Policy as a well-formed JSON document, with the correct field names
*
* @return IAM Policy as a well-formed JSON document
*/
public Map<String, Object> getPolicyDocument() {
Map<String, Object> serializablePolicy = new HashMap<>();
serializablePolicy.put(VERSION, policyDocumentObject.Version);
Statement[] statements = policyDocumentObject.getStatement();
Map<String, Object>[] serializableStatementArray = new Map[statements.length];
for (int i = 0; i < statements.length; i++) {
Map<String, Object> serializableStatement = new HashMap<>();
AuthPolicy.Statement statement = statements[i];
serializableStatement.put(EFFECT, statement.Effect);
serializableStatement.put(ACTION, statement.Action);
serializableStatement.put(RESOURCE, statement.getResource());
serializableStatement.put(CONDITION, statement.getCondition());
serializableStatementArray[i] = serializableStatement;
}
serializablePolicy.put(STATEMENT, serializableStatementArray);
return serializablePolicy;
}
public void setPolicyDocument(PolicyDocument policyDocumentObject) {
this.policyDocumentObject = policyDocumentObject;
}
/**
* PolicyDocument represents an IAM Policy, specifically for the execute-api:Invoke action
* in the context of a API Gateway Authorizer
*
* Initialize the PolicyDocument with
* the region where the RestApi is configured,
* the AWS Account ID that owns the RestApi,
* the RestApi identifier
* and the Stage on the RestApi that the Policy will apply to
*/
public static class PolicyDocument {
static final String EXECUTE_API_ARN_FORMAT = "arn:aws:execute-api:%s:%s:%s/%s/%s/%s";
String Version = "2012-10-17"; // override if necessary
private Statement allowStatement;
private Statement denyStatement;
private List<Statement> statements;
// context metadata
transient String region;
transient String awsAccountId;
transient String restApiId;
transient String stage;
/**
* Creates a new PolicyDocument with the given context,
* and initializes two base Statement objects for allowing and denying access to API Gateway methods
*
* @param region the region where the RestApi is configured
* @param awsAccountId the AWS Account ID that owns the RestApi
* @param restApiId the RestApi identifier
* @param stage and the Stage on the RestApi that the Policy will apply to
*/
public PolicyDocument(String region, String awsAccountId, String restApiId, String stage) {
this.region = region;
this.awsAccountId = awsAccountId;
this.restApiId = restApiId;
this.stage = stage;
allowStatement = Statement.getEmptyInvokeStatement("Allow");
denyStatement = Statement.getEmptyInvokeStatement("Deny");
this.statements = new ArrayList<>();
statements.add(allowStatement);
statements.add(denyStatement);
}
public String getVersion() {
return Version;
}
public void setVersion(String version) {
Version = version;
}
public AuthPolicy.Statement[] getStatement() {
return statements.toArray(new AuthPolicy.Statement[statements.size()]);
}
public void allowMethod(HttpMethod httpMethod, String resourcePath) {
addResourceToStatement(allowStatement, httpMethod, resourcePath);
}
public void denyMethod(HttpMethod httpMethod, String resourcePath) {
addResourceToStatement(denyStatement, httpMethod, resourcePath);
}
public void addStatement(AuthPolicy.Statement statement) {
statements.add(statement);
}
private void addResourceToStatement(Statement statement, HttpMethod httpMethod, String resourcePath) {
// resourcePath must start with '/'
// to specify the root resource only, resourcePath should be an empty string
if (resourcePath.equals("/")) {
resourcePath = "";
}
String resource = resourcePath.startsWith("/") ? resourcePath.substring(1) : resourcePath;
String method = httpMethod == HttpMethod.ALL ? "*" : httpMethod.toString();
statement.addResource(String.format(EXECUTE_API_ARN_FORMAT, region, awsAccountId, restApiId, stage, method, resource));
}
// Static methods
/**
* Generates a new PolicyDocument with a single statement that allows the requested method/resourcePath
*
* @param region API Gateway region
* @param awsAccountId AWS Account that owns the API Gateway RestApi
* @param restApiId RestApi identifier
* @param stage Stage name
* @param method HttpMethod to allow
* @param resourcePath Resource path to allow
* @return new PolicyDocument that allows the requested method/resourcePath
*/
public static PolicyDocument getAllowOnePolicy(String region, String awsAccountId, String restApiId, String stage, HttpMethod method, String resourcePath) {
AuthPolicy.PolicyDocument policyDocument = new AuthPolicy.PolicyDocument(region, awsAccountId, restApiId, stage);
policyDocument.allowMethod(method, resourcePath);
return policyDocument;
}
/**
* Generates a new PolicyDocument with a single statement that denies the requested method/resourcePath
*
* @param region API Gateway region
* @param awsAccountId AWS Account that owns the API Gateway RestApi
* @param restApiId RestApi identifier
* @param stage Stage name
* @param method HttpMethod to deny
* @param resourcePath Resource path to deny
* @return new PolicyDocument that denies the requested method/resourcePath
*/
public static PolicyDocument getDenyOnePolicy(String region, String awsAccountId, String restApiId, String stage, HttpMethod method, String resourcePath) {
AuthPolicy.PolicyDocument policyDocument = new AuthPolicy.PolicyDocument(region, awsAccountId, restApiId, stage);
policyDocument.denyMethod(method, resourcePath);
return policyDocument;
}
public static AuthPolicy.PolicyDocument getAllowAllPolicy(String region, String awsAccountId, String restApiId, String stage) {
return getAllowOnePolicy(region, awsAccountId, restApiId, stage, HttpMethod.ALL, "*");
}
public static PolicyDocument getDenyAllPolicy(String region, String awsAccountId, String restApiId, String stage) {
return getDenyOnePolicy(region, awsAccountId, restApiId, stage, HttpMethod.ALL, "*");
}
}
public enum HttpMethod {
GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS, ALL
}
static class Statement {
String Effect;
String Action;
Map<String, Map<String, Object>> Condition;
private List<String> resourceList;
public Statement() {
}
public Statement(String effect, String action, List<String> resourceList, Map<String, Map<String, Object>> condition) {
this.Effect = effect;
this.Action = action;
this.resourceList = resourceList;
this.Condition = condition;
}
public static Statement getEmptyInvokeStatement(String effect) {
return new Statement(effect, "execute-api:Invoke", new ArrayList<>(), new HashMap<>());
}
public String getEffect() {
return Effect;
}
public void setEffect(String effect) {
this.Effect = effect;
}
public String getAction() {
return Action;
}
public void setAction(String action) {
this.Action = action;
}
public String[] getResource() {
return resourceList.toArray(new String[resourceList.size()]);
}
public void addResource(String resource) {
resourceList.add(resource);
}
public Map<String, Map<String,Object>> getCondition() {
return Condition;
}
public void addCondition(String operator, String key, Object value) {
Condition.put(operator, Collections.singletonMap(key, value));
}
}
}