-
Notifications
You must be signed in to change notification settings - Fork 82
/
cognito_urls_config.py
175 lines (156 loc) · 6.34 KB
/
cognito_urls_config.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
import json
import os
import random
import string
import boto3
from botocore.exceptions import ClientError
def setup_cognito(
region,
resource_prefix,
envname,
internet_facing='True',
custom_domain='False',
enable_cw_canaries='False',
):
ssm = boto3.client('ssm', region_name=region)
user_pool_id = ssm.get_parameter(Name=f'/dataall/{envname}/cognito/userpool')[
'Parameter'
]['Value']
print(f'Cognito Pool ID: {user_pool_id}')
app_client = ssm.get_parameter(Name=f'/dataall/{envname}/cognito/appclient')[
'Parameter'
]['Value']
if custom_domain == 'False' and internet_facing == 'True':
print('Switching to us-east-1 region...')
ssm = boto3.client('ssm', region_name='us-east-1')
signin_singout_link = ssm.get_parameter(
Name=f'/dataall/{envname}/CloudfrontDistributionDomainName'
)['Parameter']['Value']
user_guide_link = ssm.get_parameter(
Name=f'/dataall/{envname}/cloudfront/docs/user/CloudfrontDistributionDomainName'
)['Parameter']['Value']
else:
signin_singout_link = ssm.get_parameter(
Name=f'/dataall/{envname}/frontend/custom_domain_name'
)['Parameter']['Value']
user_guide_link = ssm.get_parameter(
Name=f'/dataall/{envname}/userguide/custom_domain_name'
)['Parameter']['Value']
print(f'UI: {signin_singout_link}')
print(f'USERGUIDE: {user_guide_link}')
cognito = boto3.client('cognito-idp', region_name=region)
try:
user_pool = cognito.describe_user_pool_client(
UserPoolId=user_pool_id, ClientId=app_client
)
del user_pool['UserPoolClient']['CreationDate']
del user_pool['UserPoolClient']['LastModifiedDate']
config_callbacks = [
f'https://{signin_singout_link}',
f'https://{user_guide_link}/parseauth',
]
existing_callbacks = user_pool['UserPoolClient'].get('CallbackURLs', [])
if 'example.com' in existing_callbacks:
existing_callbacks.remove('example.com')
updated_callbacks = existing_callbacks + list(
set(config_callbacks) - set(existing_callbacks)
)
print(f'Updated CallBackUrls: {updated_callbacks}')
config_logout_urls = [f'https://{signin_singout_link}']
existing_logout_urls = user_pool['UserPoolClient'].get('LogoutURLs', [])
updated_logout_urls = existing_logout_urls + list(
set(config_logout_urls) - set(existing_logout_urls)
)
print(f'Updated LogOutUrls: {updated_logout_urls}')
user_pool['UserPoolClient']['CallbackURLs'] = updated_callbacks
user_pool['UserPoolClient']['LogoutURLs'] = updated_logout_urls
response = cognito.update_user_pool_client(
**user_pool['UserPoolClient'],
)
print(f'CallbackUrls and LogOutUrls updated successfully: {response}')
try:
response = cognito.create_group(
GroupName='DAAdministrators',
UserPoolId=user_pool_id,
Description='administrators group',
)
print(f'Administrators group created Successfully...: {response}')
except ClientError as e:
if 'GroupExistsException' in str(e):
print('Group already exists')
else:
raise e
if enable_cw_canaries == 'True':
sm = boto3.client('secretsmanager', region_name=region)
secret = sm.get_secret_value(
SecretId=f'{resource_prefix}-{envname}-cognito-canary-user'
)
creds = json.loads(secret['SecretString'])
username = creds['username']
print(f'Creating Canaries user...')
try:
response = cognito.admin_create_user(
UserPoolId=user_pool_id,
Username=username,
UserAttributes=[
{'Name': 'email', 'Value': f'{username}@amazonaws.com'}
],
TemporaryPassword='da@'
+ ''.join(
random.SystemRandom().choice(
string.ascii_uppercase + string.digits
)
for _ in range(13)
),
MessageAction='SUPPRESS',
)
print(f'User Created Successfully...: {response}')
except ClientError as e:
if 'UsernameExistsException' in str(e):
print('User already exists')
else:
raise e
print(f'Updating Canaries user password...')
response = cognito.admin_set_user_password(
UserPoolId=user_pool_id,
Username=username,
Password=creds['password'],
Permanent=True,
)
print(f'User password updated Successfully...: {response}')
try:
response = cognito.create_group(
GroupName='CWCanaries',
UserPoolId=user_pool_id,
Description='CW Canary group',
)
print(f'Canaries group created Successfully...: {response}')
except ClientError as e:
if 'GroupExistsException' in str(e):
print('Group already exists')
else:
raise e
response = cognito.admin_add_user_to_group(
GroupName='CWCanaries', UserPoolId=user_pool_id, Username=username
)
print(f'User added to group Successfully...: {response}')
except ClientError as e:
print(f'Failed to setup cognito due to: {e}')
raise e
if __name__ == '__main__':
print(f'Starting Cognito Configuration...')
envname = os.environ.get('envname')
region = os.environ.get('deployment_region')
internet_facing = os.environ.get('internet_facing')
custom_domain = os.environ.get('custom_domain')
enable_cw_canaries = os.environ.get('enable_cw_canaries')
resource_prefix = os.environ.get('resource_prefix')
setup_cognito(
region,
resource_prefix,
envname,
internet_facing,
custom_domain,
enable_cw_canaries,
)
print(f'Cognito Configuration Finished Successfully')