-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.sh
executable file
·459 lines (399 loc) · 16 KB
/
helper.sh
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
#!/usr/bin/env bash
CF_STACK_NAME="avp-stack"
get_account_id() {
ACCOUNT_ID=$(aws sts get-caller-identity \
--query 'Account' --output text)
}
get_stack_region() {
STACK_REGION=$(aws configure get region)
}
get_cognito_users_password() {
local command_output
command_output=$(aws cloudformation describe-stacks \
--stack-name ${CF_STACK_NAME} \
--no-paginate | jq -r '.')
COGNITO_USERS_PASSWORD=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="UserPassword").ParameterValue')
echo "Password: $COGNITO_USERS_PASSWORD"
}
get_cognito_username_and_password() {
local command_output
command_output=$(aws cloudformation describe-stacks \
--stack-name ${CF_STACK_NAME} \
--no-paginate | jq -r '.')
COGNITO_USERS_PASSWORD=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="UserPassword").ParameterValue')
EXTERNAL_USER_NAME1=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="ExternalUser1Name").ParameterValue')
EXTERNAL_USER_NAME2=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="ExternalUser2Name").ParameterValue')
INTERNAL_USER_NAME1=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="InternalUser1Name").ParameterValue')
INTERNAL_USER_NAME2=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="InternalUser2Name").ParameterValue')
}
get_api_url_cognitouser_cognitouserpass_cognitoclientid() {
local command_output
command_output=$(aws cloudformation describe-stacks \
--stack-name ${CF_STACK_NAME} \
--no-paginate | jq -r '.')
COGNITO_USERS_PASSWORD=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="UserPassword").ParameterValue')
EXTERNAL_USER_NAME1=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="ExternalUser1Name").ParameterValue')
EXTERNAL_USER_NAME2=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="ExternalUser2Name").ParameterValue')
INTERNAL_USER_NAME1=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="InternalUser1Name").ParameterValue')
INTERNAL_USER_NAME2=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="InternalUser2Name").ParameterValue')
API_URL=$(echo "$command_output" \
| jq -r '.Stacks[0].Outputs[] | select(.OutputKey=="ApiGatewayDeploymentUrlApiEndpoint").OutputValue')
INTERNAL_COGNITO_CLIENT_ID=$(echo "$command_output" \
| jq -r '.Stacks[0].Outputs[] | select(.OutputKey=="CognitoUserPoolClientInternal").OutputValue')
EXTERNAL_COGNITO_CLIENT_ID=$(echo "$command_output" \
| jq -r '.Stacks[0].Outputs[] | select(.OutputKey=="CognitoUserPoolClientExternal").OutputValue')
}
get_api_url_v2_cognitouser_cognitouserpass_cognitoclientid() {
local command_output
command_output=$(aws cloudformation describe-stacks \
--stack-name ${CF_STACK_NAME} \
--no-paginate | jq -r '.')
COGNITO_USERS_PASSWORD=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="UserPassword").ParameterValue')
EXTERNAL_USER_NAME1=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="ExternalUser1Name").ParameterValue')
EXTERNAL_USER_NAME2=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="ExternalUser2Name").ParameterValue')
INTERNAL_USER_NAME1=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="InternalUser1Name").ParameterValue')
INTERNAL_USER_NAME2=$(echo "$command_output" \
| jq -r '.Stacks[0].Parameters[] | select(.ParameterKey=="InternalUser2Name").ParameterValue')
API_URL=$(echo "$command_output" \
| jq -r '.Stacks[0].Outputs[] | select(.OutputKey=="ApiGatewayDeploymentUrlApiEndpoint").OutputValue')
INTERNAL_COGNITO_CLIENT_ID=$(echo "$command_output" \
| jq -r '.Stacks[0].Outputs[] | select(.OutputKey=="CognitoUserPoolClientInternal").OutputValue')
EXTERNAL_COGNITO_CLIENT_ID=$(echo "$command_output" \
| jq -r '.Stacks[0].Outputs[] | select(.OutputKey=="CognitoUserPoolClientExternal").OutputValue')
}
get_api_url() {
API_URL=$(aws cloudformation describe-stacks \
--stack-name ${CF_STACK_NAME} \
--query 'Stacks[0].Outputs[?OutputKey==`ApiGatewayDeploymentUrlApiEndpoint`].OutputValue' --output text)
}
get_login_payload_data() {
DATA=$(cat<<EOF
{
"AuthParameters" : {
"USERNAME" : "$1",
"PASSWORD" : "$2"
},
"AuthFlow" : "USER_PASSWORD_AUTH",
"ClientId" : "$3"
}
EOF)
}
get_access_token() {
get_stack_region
ACCESS_TOKEN=$(curl -s -X POST --data "${DATA}" \
-H 'X-Amz-Target: AWSCognitoIdentityProviderService.InitiateAuth' \
-H 'Content-Type: application/x-amz-json-1.1' \
https://cognito-idp."${STACK_REGION}".amazonaws.com/ | jq -r '.AuthenticationResult.IdToken')
echo "Access Token: $ACCESS_TOKEN"
}
create_s3_bucket_for_lambdas() {
get_account_id
get_stack_region
S3_BUCKET_NAME="${CF_STACK_NAME}-${ACCOUNT_ID}-${STACK_REGION}-lambdas"
if [[ "${STACK_REGION}" == "us-east-1" ]]
then
aws s3api create-bucket \
--bucket "${S3_BUCKET_NAME}" \
--region "${STACK_REGION}" > /dev/null
else
aws s3api create-bucket \
--bucket "${S3_BUCKET_NAME}" \
--region "${STACK_REGION}" \
--create-bucket-configuration LocationConstraint="${STACK_REGION}" > /dev/null
fi
aws s3 cp ./cf-lambdas/custom-auth.zip s3://"${S3_BUCKET_NAME}"
aws s3 cp ./cf-lambdas/pets-clinic-api.zip s3://"${S3_BUCKET_NAME}"
}
delete_s3_bucket_for_lambdas() {
get_account_id
get_stack_region
S3_BUCKET_NAME="${CF_STACK_NAME}-${ACCOUNT_ID}-${STACK_REGION}-lambdas"
aws s3 rm s3://"${S3_BUCKET_NAME}/custom-auth.zip"
aws s3 rm s3://"${S3_BUCKET_NAME}/pets-clinic-api.zip"
aws s3api delete-bucket \
--bucket "${S3_BUCKET_NAME}" \
--region "${STACK_REGION}" > /dev/null
}
check_for_function_exit_code() {
EXIT_CODE="$1"
MSG="$2"
if [[ "$?" == "${EXIT_CODE}" ]]
then
echo "${MSG}"
else
echo "Error occured. Please verify your configurations and try again."
fi
}
for var in "$@"
do
case "$var" in
cf-create-stack-gen-password)
COGNITO_USER_PASS=Pa%%word-$(date +%F-%H-%M-%S)
echo "Starting..." && echo "Generated password: ${COGNITO_USER_PASS}"
COGNITO_USER_PASS="${COGNITO_USER_PASS}" bash ./helper.sh cf-create-stack
;;
cf-create-stack-openssl-gen-password)
COGNITO_USER_PASS=Pa%%word-$(openssl rand -hex 12)
echo "" && echo "Generated password: ${COGNITO_USER_PASS}"
COGNITO_USER_PASS="${COGNITO_USER_PASS}" bash ./helper.sh cf-create-stack
;;
cf-create-stack)
create_s3_bucket_for_lambdas
echo "Creating CloudFormation Stack in region ${STACK_REGION}."
STACK_ID=$(aws cloudformation create-stack \
--stack-name ${CF_STACK_NAME} \
--template-body file://infrastructure/stack-no-auth.template \
--parameters ParameterKey=UserPassword,ParameterValue=${COGNITO_USER_PASS} \
--capabilities CAPABILITY_NAMED_IAM \
--query 'StackId' --output text)
aws cloudformation wait stack-create-complete \
--stack-name ${STACK_ID}
check_for_function_exit_code "$?" "Successfully created CloudFormation stack."
;;
cf-update-stack)
get_cognito_users_password
STACK_ID=$(aws cloudformation update-stack \
--stack-name ${CF_STACK_NAME} \
--template-body file://infrastructure/stack-with-auth.template \
--parameters ParameterKey=UserPassword,ParameterValue=${COGNITO_USERS_PASSWORD} \
--capabilities CAPABILITY_NAMED_IAM \
--query 'StackId' --output text)
aws cloudformation wait stack-update-complete \
--stack-name ${STACK_ID}
check_for_function_exit_code "$?" "Successfully updated CloudFormation stack."
;;
cf-delete-stack)
delete_s3_bucket_for_lambdas
aws cloudformation delete-stack \
--stack-name ${CF_STACK_NAME} >> /dev/null
echo "Deleting CloudFormation stack. If you want to wait for delete complition please run command below."
echo "bash ./helper.sh cf-delete-stack-completed"
;;
cf-delete-stack-completed)
aws cloudformation wait stack-delete-complete \
--stack-name ${CF_STACK_NAME}
check_for_function_exit_code "$?" "Successfully deleted CloudFormation stack."
;;
open-cognito-internal-domain-ui)
COGNITO_UI_URL=$(aws cloudformation describe-stacks \
--stack-name ${CF_STACK_NAME} \
--query 'Stacks[0].Outputs[?OutputKey==`CognitoHostedUiInternalDomainUrl`].OutputValue' --output text)
get_cognito_username_and_password
echo "Opening Cognito UI..."
echo "URL: ${COGNITO_UI_URL}"
echo ""
echo "Please use following credentials to login and validate for any internal users:"
echo ""
echo "Username: ${INTERNAL_USER_NAME1}"
echo "Password: ${COGNITO_USERS_PASSWORD}"
echo "Username: ${INTERNAL_USER_NAME2}"
echo "Password: ${COGNITO_USERS_PASSWORD}"
# for visual effect for user to recognize msg above
./helper.sh visual 6
open "${COGNITO_UI_URL}"
;;
open-cognito-external-domain-ui)
COGNITO_UI_URL=$(aws cloudformation describe-stacks \
--stack-name ${CF_STACK_NAME} \
--query 'Stacks[0].Outputs[?OutputKey==`CognitoHostedUiExternalDomainUrl`].OutputValue' --output text)
get_cognito_username_and_password
echo "Opening Cognito UI..."
echo "URL: ${COGNITO_UI_URL}"
echo ""
echo "Please use following credentials to login and validate for any external users:"
echo ""
echo "Username: ${EXTERNAL_USER_NAME1}"
echo "Password: ${COGNITO_USERS_PASSWORD}"
echo ""
echo "Username: ${EXTERNAL_USER_NAME2}"
echo "Password: ${COGNITO_USERS_PASSWORD}"
# for visual effect for user to recognize msg above
./helper.sh visual 6
open "${COGNITO_UI_URL}"
;;
curl-api)
get_api_url
APPOITMENT_API=$API_URL"/PI-T123"
echo ""
echo "API to check the appointment details of PI-T123"
echo "URL: $APPOITMENT_API"
echo "Response: "
curl "${APPOITMENT_API}"
echo ""
APPOITMENT_API=$API_URL"/PI-T124"
echo ""
echo "API to check the appointment details of PI-T124"
echo "URL: $APPOITMENT_API"
echo "Response: "
curl "${APPOITMENT_API}"
echo ""
APPOITMENT_API=$API_URL"/PI-T125"
echo ""
echo "API to check the appointment details of PI-T125"
echo "URL: $APPOITMENT_API"
echo "Response: "
curl "${APPOITMENT_API}"
echo ""
;;
curl-api-invalid-token)
get_api_url
APPOITMENT_API=$API_URL"/PI-T123"
echo ""
echo "API to check the appointment details of PI-T123 with invalid token"
curl -s -H "Authorization: Bearer aGVhZGVy.Y2xhaW1z.c2lnbmF0dXJl" "${APPOITMENT_API}"
echo ""
;;
curl-protected-external-user-api)
echo ""
echo "Now calling external userpool users for accessing request"
get_api_url_cognitouser_cognitouserpass_cognitoclientid
APPOITMENT_API=$API_URL"/PI-T123"
echo "User: $EXTERNAL_USER_NAME1"
echo "Password: $COGNITO_USERS_PASSWORD"
echo "Resource: PI-T123"
echo "URL: $APPOITMENT_API"
get_login_payload_data $EXTERNAL_USER_NAME1 $COGNITO_USERS_PASSWORD $EXTERNAL_COGNITO_CLIENT_ID
echo ""
echo "Authenticating to get access_token..."
get_access_token
echo ""
echo "Response: "
curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" -H "ClientType: external" "${APPOITMENT_API}"
echo ""
APPOITMENT_API=$API_URL"/PI-T123"
echo ""
echo "User: $EXTERNAL_USER_NAME2"
echo "Password $COGNITO_USERS_PASSWORD"
echo "Resource: PI-T123"
echo "URL: $APPOITMENT_API"
get_login_payload_data $EXTERNAL_USER_NAME2 $COGNITO_USERS_PASSWORD $EXTERNAL_COGNITO_CLIENT_ID
echo ""
echo "Authenticating to get access_token..."
get_access_token
echo ""
echo "Response: "
curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" -H "ClientType: external" "${APPOITMENT_API}"
echo ""
APPOITMENT_API=$API_URL"/PI-T124"
echo ""
echo "User: $EXTERNAL_USER_NAME2"
echo "Password $COGNITO_USERS_PASSWORD"
echo "Resource: PI-T124"
echo "URL: $APPOITMENT_API"
get_login_payload_data $EXTERNAL_USER_NAME2 $COGNITO_USERS_PASSWORD $EXTERNAL_COGNITO_CLIENT_ID
echo ""
echo "Authenticating to get access_token..."
get_access_token
echo ""
echo "Response: "
curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" -H "ClientType: external" "${APPOITMENT_API}"
echo ""
;;
curl-protected-internal-user-api)
echo ""
echo "Getting API URL, Cognito Usernames, Cognito Users Password and Cognito ClientId..."
get_api_url_cognitouser_cognitouserpass_cognitoclientid
APPOITMENT_API=$API_URL"/PI-T123"
echo "User: $INTERNAL_USER_NAME1"
echo "Password: $COGNITO_USERS_PASSWORD"
echo "Resource: PI-T123"
echo "URL: $APPOITMENT_API"
get_login_payload_data $INTERNAL_USER_NAME1 $COGNITO_USERS_PASSWORD $INTERNAL_COGNITO_CLIENT_ID
echo ""
echo "Authenticating to get access_token..."
get_access_token
echo ""
echo "Response: "
curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" -H "ClientType: internal" "${APPOITMENT_API}"
echo ""
echo ""
APPOITMENT_API=$API_URL"/PI-T123"
echo "User: $INTERNAL_USER_NAME2"
echo "Password: $COGNITO_USERS_PASSWORD"
echo "Resource: PI-T123"
echo "URL: $APPOITMENT_API"
get_login_payload_data $INTERNAL_USER_NAME2 $COGNITO_USERS_PASSWORD $INTERNAL_COGNITO_CLIENT_ID
echo ""
echo "Authenticating to get access_token..."
get_access_token
echo ""
echo "Response: "
curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" -H "ClientType: internal" "${APPOITMENT_API}"
echo ""
echo ""
APPOITMENT_API=$API_URL"/PI-T125"
echo "User: $INTERNAL_USER_NAME2"
echo "Password: $COGNITO_USERS_PASSWORD"
echo "Resource: PI-T125"
echo "URL: $APPOITMENT_API"
get_login_payload_data $INTERNAL_USER_NAME2 $COGNITO_USERS_PASSWORD $INTERNAL_COGNITO_CLIENT_ID
echo ""
echo "Authenticating to get access_token..."
get_access_token
echo ""
echo "Response: "
curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" -H "ClientType: internal" "${APPOITMENT_API}"
echo ""
;;
curl-protected-api-not-allowed-endpoint)
echo "Getting API URL, Cognito Username, Cognito Users Password and Cognito ClientId..."
get_api_url_v2_cognitouser_cognitouserpass_cognitoclientid
get_login_payload_data
echo "Authenticating to get access_token..."
get_access_token
echo "Making api call..."
curl -s -H "Authorization: Bearer ${ACCESS_TOKEN}" "${API_URL_V2}"
echo ""
;;
create-s3-bucket)
create_s3_bucket_for_lambdas
;;
delete-s3-bucket)
delete_s3_bucket_for_lambdas
;;
package-custom-auth)
cd ./custom-auth
pip3 install -r requirements.txt --target ./package/dependencies
cd ./package
zip -r ../custom-auth.zip . > /dev/null
cd .. && zip -g custom-auth.zip lambda.py
mv ./custom-auth.zip ../cf-lambdas
rm -r ./package
echo "Successfully completed packaging custom-auth."
;;
package-pets-clinic-api)
cd ./pets-clinic-api
zip pets-clinic-api.zip lambda.py && mv pets-clinic-api.zip ../cf-lambdas
echo "Successfully completed packaging pets-clinic-api."
;;
package-lambda-functions)
mkdir -p cf-lambdas
bash ./helper.sh package-custom-auth
bash ./helper.sh package-pets-clinic-api
echo "Successfully completed packaging files."
;;
visual)
for ((i=1;i<=${2};i++));
do
sleep 0.5 && echo -n "."
done
;;
*)
;;
esac
done