/
LambdaAuthorizer.java
230 lines (196 loc) · 10.8 KB
/
LambdaAuthorizer.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
package org.example.api;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayV2HTTPResponse;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.google.gson.Gson;
import org.apache.commons.lang3.exception.ExceptionUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.tuple.Pair;
import org.example.authorizerpolicies.AuthPolicy;
import org.example.config.EntityTypesConstants;
import org.example.config.HttpPathToCedarActionMap;
import java.io.IOException;
import java.net.URI;
import java.util.*;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.fasterxml.jackson.core.type.TypeReference;
import org.example.entityBuilders.ResourceEntityBuilderFactory;
import org.example.entityBuilders.UserEntityBuilder;
import org.example.util.CognitoJwtVerifier;
import org.json.simple.JSONObject;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.verifiedpermissions.VerifiedPermissionsClient;
import software.amazon.awssdk.services.verifiedpermissions.VerifiedPermissionsClientBuilder;
import software.amazon.awssdk.services.verifiedpermissions.model.*;
import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Subsegment;
public class LambdaAuthorizer {
// All orders have this entity as the parent. It helps identity entities of type order and give permissions to all orders in the policy head
// this will no longer be required when Cedar support the "is" operator
private static final String AUTHORIZATION_HEADER_NAME = "authorization";
private static final String POLICY_STORE_ID = System.getenv("policyStoreId");
private final ResourceEntityBuilderFactory resourceEntityBuilderFactory = new ResourceEntityBuilderFactory();
private final UserEntityBuilder userEntityBuilder = new UserEntityBuilder();
private final VerifiedPermissionsClient avpClient = VerifiedPermissionsClient.builder().build();
private final CognitoJwtVerifier cognitoJwtVerifier = new CognitoJwtVerifier();
public AuthPolicy authorizeRequest(APIGatewayV2HTTPEvent event, Context context) {
System.out.println("request" + event);
DecodedJWT token = cognitoJwtVerifier.verifyToken(event.getHeaders().get(AUTHORIZATION_HEADER_NAME));
List<String> authorizedActions =
this.getAuthorizedActionsFromAvp(event);
List<String> allActions = HttpPathToCedarActionMap.getRelatedCedarActions(event.getRouteKey());
String sub = token.getSubject();
AuthPolicy authPolicy = buildAuthorizerResponse(sub, event.getRequestContext(),
authorizedActions, allActions, event.getPathParameters());
System.out.println(authPolicy);
return authPolicy;
}
public APIGatewayV2HTTPResponse getAuthorizedActions(APIGatewayV2HTTPEvent event, Context context) {
List<String> authorizedActions = getAuthorizedActionsFromAvp(event);
return ok(new Gson().toJson(authorizedActions));
}
private List<String> getAuthorizedActionsFromAvp(APIGatewayV2HTTPEvent event) {
try {
System.out.println("request" + event);
List<EntityItem> entities = new ArrayList<>();
EntityIdentifier resourceEntity =
resourceEntityBuilderFactory.getResourceEntityBuilder(
event.getRouteKey()).getEntities(event, entities).getLeft();
List<ActionIdentifier> actionEntities =
HttpPathToCedarActionMap.
getRelatedCedarActions(event.getRouteKey()).
stream().map(action ->
ActionIdentifier.
builder().
actionId(action).
actionType(EntityTypesConstants.ACTION_ENTITY_TYPE).build())
.collect(Collectors.toList());
EntityIdentifier userEntity = userEntityBuilder.getEntities(event, entities).getLeft();
EntitiesDefinition entityDefinition = EntitiesDefinition.builder().
entityList(entities).
build();
List<String> authorizedActions = new ArrayList<>();
boolean optimized = Boolean.parseBoolean(event.getQueryStringParameters().getOrDefault("optimized", "false"));
if (optimized) {
Subsegment subsegment = AWSXRay.beginSubsegment("IsAuthorizedBatch");
try {
Collection<BatchIsAuthorizedInputItem> batchIsAuthorizedInputItem = actionEntities.stream()
.map(actionEntity -> BatchIsAuthorizedInputItem.builder().principal(userEntity)
.action(actionEntity)
.resource(resourceEntity)
.build())
.collect(Collectors.toList());
BatchIsAuthorizedResponse batchIsAuthorizedResponse = avpClient.batchIsAuthorized(BatchIsAuthorizedRequest.builder()
.policyStoreId(POLICY_STORE_ID)
.entities(entityDefinition)
.requests(batchIsAuthorizedInputItem).build());
for (int i=0;i<batchIsAuthorizedResponse.results().size();i++) {
// BatchIsAuthorized maintains the order of the response
if(batchIsAuthorizedResponse.results().get(i).decision().equals(Decision.ALLOW)) {
authorizedActions.add(batchIsAuthorizedResponse.results().get(i).request().action().actionId());
}
}
System.out.println("Authorized actions: " + authorizedActions);
} catch (Exception e) {
subsegment.addException(e);
throw e;
} finally {
AWSXRay.endSubsegment();
}
} else {
for (ActionIdentifier actionEntity : actionEntities) {
//System.out.println("Calling verified permissions for action : " + actionEntity.actionId());
Subsegment subsegment = AWSXRay.beginSubsegment("IsAuthorized");
try {
subsegment.putAnnotation("Lambda Function", "LambdaAuthorizer");
IsAuthorizedResponse authorizationResponse =
avpClient.isAuthorized(IsAuthorizedRequest.builder()
.policyStoreId(POLICY_STORE_ID)
.principal(userEntity)
.action(actionEntity)
.resource(resourceEntity)
.entities(entityDefinition)
.build());
if (authorizationResponse.decision().equals(Decision.ALLOW)) {
authorizedActions.add(actionEntity.actionId());
}
} catch (Exception e) {
subsegment.addException(e);
throw e;
} finally {
AWSXRay.endSubsegment();
}
}
}
return authorizedActions;
} catch (ValidationException e) {
System.out.println("Validation exception from AVP while processing request");
System.out.println(e.fieldList());
throw e;
} catch (Exception e) {
System.out.println("Error in processing request");
System.out.println(e.getMessage() + " " + e.getCause());
System.out.println(ExceptionUtils.getStackTrace(e));
throw e;
}
}
private APIGatewayV2HTTPResponse ok(String body) {
return APIGatewayV2HTTPResponse.builder()
.withStatusCode(200)
.withBody(body)
.withIsBase64Encoded(false).build();
}
private APIGatewayV2HTTPResponse error(JSONObject response, Exception exc) {
String exceptionString = String.format("error: %s: %s", exc.getMessage(), Arrays.toString(exc.getStackTrace()));
response.put("Exception", exceptionString);
return APIGatewayV2HTTPResponse.builder()
.withStatusCode(500)
.withBody(response.toJSONString())
.withIsBase64Encoded(false).build();
}
/*
* This function is responsible for generating an IAM policy that is used by Amazon API-Gateway to cache the authorizer response
*
* The return is an IAM policy with the below details
* 1. Principal which in my case is the user sub from the JWT token
* 2. AccountId: AWS account id
* 3. ApiId: API id : This is the key for the specific API
* 4. Stage
* 5. Statements
*
* Statements contains a list of allowed <HttpMethod, HttpPath> and denied <HttpMethod, HttpPath>.
*
* The Tuple<HttpMethod, HttpPath> is a pair of HttpMethod and HttpPath.
* For example, if the request is for the following path
* https://<api-id>.execute-api.<region>.amazonaws.com/<stage>/store/store-1/order/order-1
*
* The Tuple<HttpMethod, HttpPath> will be
* Tuple<HttpMethod, HttpPath> - (GET, /store/store-1/order/order-1)
*
* API gateway caches the IAM policy and authorizes future requests accordingly
*/
private AuthPolicy buildAuthorizerResponse(String sub, APIGatewayV2HTTPEvent.RequestContext
requestContext, List<String> allowedActions, List<String> allActions, Map<String, String> pathParameters) {
String pathWithoutStage = pathParameters.get("proxy");
String accountId = requestContext.getAccountId();
AuthPolicy.PolicyDocument policyDocument = new AuthPolicy.PolicyDocument(
System.getenv("AWS_REGION"),
accountId,
requestContext.getApiId(),
requestContext.getStage()
);
for (String action : allActions) {
Pair<String, AuthPolicy.HttpMethod> httpPathForAction =
HttpPathToCedarActionMap.getHttpPathForAction(action, pathParameters);
if (allowedActions.contains(action)) {
policyDocument.allowMethod(httpPathForAction.getRight(), httpPathForAction.getLeft());
} else {
policyDocument.denyMethod(httpPathForAction.getRight(), httpPathForAction.getLeft());
}
}
return new AuthPolicy(sub, policyDocument);
}
}