-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadTestUsers.py
executable file
·106 lines (95 loc) · 3.09 KB
/
loadTestUsers.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
#!/usr/bin/env python3
import argparse
import json
import os
import subprocess
import sys
def get_user_pool_id(stage):
"""
Retrieve the ID of this stage's Cognito User Pool.
"""
return subprocess.run(
f"./services/output.sh services/ui-auth UserPoolId {stage}",
check=True,
capture_output=True,
shell=True,
).stdout.strip()
def seed_cognito(test_users, user_pool_id, password):
"""
Populate test users into Cognito and set their passwords.
"""
for user in test_users:
# checking errors so we do not set the password when the first call
# fails, but the failure itself is not a big problem - in most cases
# the user already exists
try:
subprocess.run(
[
"aws",
"cognito-idp",
"admin-create-user",
"--user-pool-id",
user_pool_id,
"--message-action",
"SUPPRESS",
"--username",
user["email"],
"--user-attributes",
f'Name=given_name,Value={user["firstName"]}',
f'Name=family_name,Value={user["lastName"]}',
f'Name=custom:cms_roles,Value="{user["cms_roles"]}"',
f'Name=custom:ismemberof,Value="{user["ismemberof"]}"',
],
check=True,
)
# aws cognito-idp admin-set-user-password --user-pool-id {user_pool_id} --username {user["email"]} --password {password} --permanent
subprocess.run(
[
"aws",
"cognito-idp",
"admin-set-user-password",
"--user-pool-id",
user_pool_id,
"--username",
user["email"],
"--password",
password,
"--permanent",
],
check=True,
)
except:
print(f'Received error:{sys.exc_info()}')
pass
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Load test users into Cognito and Dynamo."
)
parser.add_argument("stage", help="stage (Git branch) being deployed")
parser.add_argument(
"-n",
"--dry-run",
action="store_const",
const=True,
help="print seed data without contacting AWS",
)
args = parser.parse_args()
f = open("test-users.json")
seed_users = json.load(f)
if args.dry_run:
print("Seed users:")
print(seed_users)
else:
user_pool_id = get_user_pool_id(args.stage)
if not user_pool_id:
print(
"ERROR: There was an error obtaining AWS resource information to create users.",
file=sys.stderr,
)
sys.exit(101)
seed_cognito(
seed_users,
user_pool_id,
os.environ.get("COGNITO_TEST_USERS_PASSWORD"),
)
f.close()