-
Notifications
You must be signed in to change notification settings - Fork 68
/
main.py
464 lines (412 loc) · 17.6 KB
/
main.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
"""Lambda to manage IoT PubSub permissions for OCR progress push notifications
Invokable as both a CloudFormation custom resource (to configure initial access) and a Cognito
PostAuthentication trigger - to reconcile all identity pool permissions on *every* user login, because of:
https://forums.aws.amazon.com/thread.jspa?messageID=924345
TODO: PostAuthentication trigger not yet tested able to add new user access
TODO: Implement 'Update' action for CloudFormation (currently just Create and Delete supported)
"""
# Python Built-Ins:
import json
import os
import time
import traceback
# External Dependencies:
import boto3
# Local Dependencies:
import cfnresponse # AWS CloudFormation response util
cognito_identity = boto3.client("cognito-identity")
cognito_idp = boto3.client("cognito-idp")
iam = boto3.client("iam")
iot = boto3.client("iot")
lambdaclient = boto3.client("lambda")
# We pass this in as *both* a CF resource param and a Lambda env var because the Lambda function may need the
# information for other invokations besides CF, but for edge-case CF calls (e.g. update stack) the resource
# prop should be authoritative:
env_iot_policy_name = os.environ["IOT_ACCESS_POLICY_NAME"]
env_identity_pool_id = os.environ["COGNITO_IDENTITY_POOL_ID"]
IOT_TARGET_BATCH_SIZE = 250 # Max permitted per the API doc
IDENTITY_BATCH_SIZE = 60 # Max permitted per the API doc
def handler(event, context):
# Determine if it's a CloudFormation resource request (set up or tear-down) or a Cognito request
# (configure new user)
# CF request object reference here:
# https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-requests.html
if "RequestType" in event and "StackId" in event:
# It's a CloudFormation request
try:
# Check required resource properties:
if "ResourceProperties" not in event or "CognitoUserPoolId" not in event["ResourceProperties"]:
return cfnresponse.send(
event,
context,
"FAILED",
{ "Error": "Missing required resource property 'CognitoUserPoolId'" },
)
if event["RequestType"] == "Create":
return setup_stack_handler(event, context)
elif event["RequestType"] == "Update":
return update_stack_handler(event, context)
elif event["RequestType"] == "Delete":
return delete_stack_handler(event, context)
else:
return cfnresponse.send(
event,
context,
"FAILED",
{
"Error": f"Got unexpected RequestType '{event['RequestType']}' from CloudFormation",
},
)
except Exception as e:
traceback.print_exc()
return cfnresponse.send(
event,
context,
"FAILED",
{ "Error": f"Uncaught exception {traceback.format_exc()}" },
)
elif "triggerSource" in event and "userName" in event:
# (These params are common across Cognito trigger types)
# It's a Cognito Lambda trigger request
return post_login_handler(event, context)
else:
if event and "Delay" in event:
print(f"Delaying invokation by {event['Delay']}s")
time.sleep(event["Delay"])
# Just interpret it as an instruction to refresh everybody's perms:
attach_iot_policy_to_all_identities(env_identity_pool_id, env_iot_policy_name)
return {
"statusCode": 200,
"headers": {
"Access-Control-Allow-Credentials": True,
"Access-Control-Allow-Origin": "*",
},
"body": json.dumps({
"Message": f"Updated IoT permissions for Cognito Identity pool {env_identity_pool_id}"
}),
}
def setup_stack_handler(event, context):
identity_pool_id = event["ResourceProperties"]["CognitoIdentityPoolId"]
user_pool_id = event["ResourceProperties"].get("CognitoUserPoolId")
print(f"Granting IoT access for existing Cognito identities")
try:
attach_iot_policy_to_all_identities(
identity_pool_id,
event["ResourceProperties"].get("IotAccessPolicyName", env_iot_policy_name)
)
except Exception as e:
traceback.print_exc()
return cfnresponse.send(
event,
context,
"FAILED",
{
"Error": "Couldn't attach IoT access policy to existing identities. {}: {}".format(
type(e).__name__,
str(e),
),
},
)
if not user_pool_id:
print(f"Skipping user pool Lambda trigger setup - no user pool ID provided")
else:
try:
current_fn_arn = context.invoked_function_arn
#current_fn_version = context.function_version
update_lambda_triggers(
{ "PostAuthentication": current_fn_arn },
new_user_pool_id=user_pool_id,
)
except Exception as e:
traceback.print_exc()
return cfnresponse.send(
event,
context,
"FAILED",
{
"Error": "Failed to set up Lambda triggers on Cognito user pool ID {}\n{}: {}".format(
user_pool_id,
type(e).__name__,
str(e),
),
},
)
return cfnresponse.send(
event,
context,
"SUCCESS",
{ "Message": "Setup complete" },
)
def update_stack_handler(event, context):
identity_pool_id = event["ResourceProperties"]["CognitoIdentityPoolId"]
user_pool_id = event["ResourceProperties"].get("CognitoUserPoolId", "")
iot_policy_name = event["ResourceProperties"].get("IotAccessPolicyName", env_iot_policy_name)
old_props = event["OldResourceProperties"]
old_identity_pool_id = old_props["CognitoIdentityPoolId"]
old_user_pool_id = old_props.get("CognitoUserPoolId", "")
old_iot_policy_name = event["ResourceProperties"].get("IotAccessPolicyName", env_iot_policy_name)
# Refresh IoT policy attachments on the new pool even if there's been 'no change'
if (identity_pool_id):
try:
attach_iot_policy_to_all_identities(identity_pool_id, iot_policy_name)
except Exception as e:
traceback.print_exc()
# This is a failing error iff the update includes a change to the relevant components:
if (identity_pool_id != old_identity_pool_id or iot_policy_name != old_iot_policy_name):
return cfnresponse.send(
event,
context,
"FAILED",
{
"Error": "Couldn't attach IoT access policy to existing identities. {}: {}".format(
type(e).__name__,
str(e),
),
},
)
else:
print("Ignoring IoT policy refresh failure as cfn Update didn't edit pool ID or policy")
if (
identity_pool_id == old_identity_pool_id
and user_pool_id == old_user_pool_id
and iot_policy_name == old_iot_policy_name
):
print("Null update - nothing to change")
return cfnresponse.send(
event,
context,
"SUCCESS",
{ "Message": "Nothing to update" },
)
if (
old_identity_pool_id
and (old_identity_pool_id != identity_pool_id or old_iot_policy_name != iot_policy_name)
):
try:
detach_iot_policy_from_all_identities(old_identity_pool_id, old_iot_policy_name)
except Exception as e:
traceback.print_exc()
return cfnresponse.send(
event,
context,
"FAILED",
{
"Error": "Couldn't revoke IoT access policy from old identity pool. {}: {}".format(
type(e).__name__,
str(e),
),
},
)
if (user_pool_id != old_user_pool_id):
try:
trigger_dict = { "PostAuthentication": context.invoked_function_arn }
update_lambda_triggers(
trigger_dict,
new_user_pool_id=user_pool_id,
old_user_pool_id=old_user_pool_id,
# TODO: Handle changes in Lambda ARN by taking it as a parameter?
#old_trigger_dict=None,
)
except Exception as e:
traceback.print_exc()
return cfnresponse.send(
event,
context,
"FAILED",
{
"Error": "Couldn't update user pool Lambda trigger configuration. {}: {}".format(
type(e).__name__,
str(e),
),
},
)
return cfnresponse.send(
event,
context,
"SUCCESS",
{ "Message": "Update complete" },
)
def delete_stack_handler(event, context):
identity_pool_id = event["ResourceProperties"]["CognitoIdentityPoolId"]
user_pool_id = event["ResourceProperties"].get("CognitoUserPoolId")
print(f"Revoking IoT access for existing Cognito identities")
try:
detach_iot_policy_from_all_identities(
identity_pool_id,
event["ResourceProperties"].get("IotAccessPolicyName", env_iot_policy_name)
)
except Exception as e:
traceback.print_exc()
return cfnresponse.send(
event,
context,
"FAILED",
{
"Error": "Couldn't detach IoT access policy from existing identities. {}: {}".format(
type(e).__name__,
str(e),
),
},
)
if not user_pool_id:
print(f"Skipping user pool Lambda trigger cleanup - no user pool ID provided")
else:
try:
current_fn_arn = context.invoked_function_arn
#current_fn_arn = context.function_version
update_lambda_triggers(
{ "PostAuthentication": current_fn_arn },
old_user_pool_id=user_pool_id,
)
except Exception as e:
traceback.print_exc()
return cfnresponse.send(
event,
context,
"FAILED",
{
"Error": "Unable to clear Lambda triggers from Cognito user pool ID {}\n{}: {}".format(
user_pool_id,
type(e).__name__,
str(e),
),
},
)
return cfnresponse.send(
event,
context,
"SUCCESS",
{ "Message": "Tear-down complete" },
)
def post_login_handler(event, context):
"""On every user login, check the whole identity pool's setup
Cognito User Pool triggers listen to the user pool, not the identity pool, so they don't see the
IdentityId associated with the current UserId.
Since our demo app will have a small pool of identities, it's more practical/easy to do this than set up
a custom API for logged-in users to request their permissions be set up.
Asynchronously invokes this same Lambda without a payload, to do the updates in the background
"""
# Because PostLogin is called and must return in the authentication flow before a new user's Cognito
# Identity ID is created - so we'll trigger the reconciliation with a small delay to ensure ID exists:
lambdaclient.invoke(
FunctionName=context.invoked_function_arn,
InvocationType="Event",
Payload=json.dumps({ "Delay": 1.2 }),
)
return event
def attach_iot_policy_to_all_identities(identity_pool_id: str, iot_policy_name: str):
"""Lists all targets attached to the policy, and all identities in the pool, then reconciles"""
print(f"Querying which identities are already attached to IoT policy {iot_policy_name}")
already_attached_identities = set()
iot_target_list = iot.list_targets_for_policy(
policyName=iot_policy_name,
pageSize=IOT_TARGET_BATCH_SIZE,
)
while len(iot_target_list["targets"]):
already_attached_identities.update(iot_target_list["targets"])
next_token = iot_target_list.get("nextMarker")
if next_token:
if len(iot_target_list["targets"]) < IOT_TARGET_BATCH_SIZE:
print("WARNING: Got a continuation token but fewer than requested IoT targets???")
iot_target_list = iot.list_targets_for_policy(
policyName=iot_policy_name,
pageSize=IOT_TARGET_BATCH_SIZE,
marker=next_token,
)
else:
break
print(f"Ensuring all identities in {identity_pool_id} attached to IoT policy {iot_policy_name}")
identity_list = cognito_identity.list_identities(
IdentityPoolId=identity_pool_id,
MaxResults=IDENTITY_BATCH_SIZE,
)
while len(identity_list["Identities"]):
for identity in identity_list["Identities"]:
if identity["IdentityId"] not in already_attached_identities:
iot.attach_policy(policyName=iot_policy_name, target=identity["IdentityId"])
next_token = identity_list.get("NextToken")
if next_token:
if len(identity_list["Identities"]) < IDENTITY_BATCH_SIZE:
print("WARNING: Got a continuation token but fewer than requested identities???")
identity_list = cognito_identity.list_identities(
IdentityPoolId=identity_pool_id,
MaxResults=IDENTITY_BATCH_SIZE,
NextToken=next_token,
)
else:
break
def detach_iot_policy_from_all_identities(identity_pool_id: str, iot_policy_name: str):
print(f"Revoking IoT access for existing Cognito identities")
identity_list = cognito_identity.list_identities(
IdentityPoolId=identity_pool_id,
MaxResults=IDENTITY_BATCH_SIZE,
)
while len(identity_list["Identities"]):
for identity in identity_list["Identities"]:
# This doesn't seem to raise an exception if already detached:
iot.detach_policy(policyName=iot_policy_name, target=identity["IdentityId"])
next_token = identity_list.get("NextToken")
if next_token:
if len(identity_list["Identities"]) < IDENTITY_BATCH_SIZE:
print("WARNING: Got a continuation token but fewer than requested identities???")
identity_list = cognito_identity.list_identities(
IdentityPoolId=identity_pool_id,
MaxResults=IDENTITY_BATCH_SIZE,
NextToken=next_token,
)
else:
break
def update_lambda_triggers(
trigger_dict,
new_user_pool_id=None,
old_user_pool_id=None,
old_trigger_dict=None,
):
"""Install triggers from new_user_pool_id and/or clean from old_user_pool_id
trigger_dict is a dict from event to Lambda function ARN, as per:
https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_UpdateUserPool.html
If old_trigger_dict is not specified, the same spec will be used between setup and cleaning
TODO: Tolerate previous versions of the same function, or aliases, or etc
"""
if old_trigger_dict is None:
old_trigger_dict = trigger_dict
# First install on the new pool, if present:
if new_user_pool_id:
print(f"Setting Lambda triggers on new Cognito user pool {new_user_pool_id}")
new_pool_desc = cognito_idp.describe_user_pool(UserPoolId=new_user_pool_id)
existing_triggers = new_pool_desc["UserPool"].get("LambdaConfig", {})
updated_triggers = existing_triggers.copy() # This will be our update request
any_changes = False # Tracking whether an update is needed at all
for key in trigger_dict:
prev_trigger = existing_triggers.get(key)
if trigger_dict[key]:
updated_triggers[key] = trigger_dict[key]
elif key in updated_triggers:
del updated_triggers[key] # a ""/None entry in trigger_dict should explicitly delete.
if trigger_dict[key] != prev_trigger:
any_changes = True
if prev_trigger:
raise ValueError(
f"Trigger '{key}' already configured to different function {prev_trigger}"
)
if any_changes:
cognito_idp.update_user_pool(
UserPoolId=new_user_pool_id,
LambdaConfig=updated_triggers,
)
# Then remove from old pool, if present:
if old_user_pool_id:
print(f"Clearing Lambda triggers from old Cognito user pool {old_user_pool_id}")
new_pool_desc = cognito_idp.describe_user_pool(UserPoolId=old_user_pool_id)
existing_triggers = new_pool_desc["UserPool"].get("LambdaConfig", {})
updated_triggers = existing_triggers.copy() # This will be our update request
any_changes = False # Tracking whether an update is needed at all
for key in old_trigger_dict:
if old_trigger_dict[key] and old_trigger_dict[key] == existing_triggers.get(key):
any_changes = True
del updated_triggers[key]
if any_changes:
cognito_idp.update_user_pool(
UserPoolId=old_user_pool_id,
LambdaConfig=updated_triggers,
)