-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from bleggett/cognito-terraform
Use Terraform to provision Cognito resources, rather than shell script
- Loading branch information
Showing
9 changed files
with
112 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,59 @@ | ||
# | ||
# Cognito resources | ||
# | ||
resource "null_resource" "cognito_user_pool" { | ||
|
||
provisioner "local-exec" { | ||
command = "${var.cognito_cmd} ${var.envPrefix} ${var.envPrefix}-api-onboarding ${var.cognito_pool_username} ${var.cognito_pool_password} ${var.jenkinsjsonpropsfile}" | ||
} | ||
provisioner "local-exec" { | ||
when = "destroy" | ||
command = "${var.cognitoDelete_cmd} ${var.envPrefix}" | ||
resource "aws_cognito_user_pool" "pool"{ | ||
name = "${var.envPrefix}" | ||
username_attributes = ["email"] | ||
schema = [ | ||
{ | ||
name = "email" | ||
attribute_data_type = "String" | ||
required = true | ||
}, | ||
{ | ||
name = "reg-code" | ||
attribute_data_type = "String" | ||
string_attribute_constraints = { | ||
min_length = 1 | ||
}, | ||
} | ||
] | ||
tags = { | ||
Application = "Jazz" | ||
JazzInstance = "${var.envPrefix}" | ||
} | ||
auto_verified_attributes = ["email"] | ||
verification_message_template = { | ||
email_subject_by_link = "Jazz Notification - Account Verification" | ||
email_message_by_link = "Hello,\n<br><br>\nThanks for signing up!\n<br><br>\nPlease click the link to verify your email address: {##VERIFY EMAIL##}\n<br><br>\nTo know more about Jazz, please refer to link https://github.com/tmobile/jazz-core/wiki\n<br><br>\nBest,<br>\nJazz Team" | ||
default_email_option = "CONFIRM_WITH_LINK" | ||
} | ||
password_policy = { | ||
minimum_length = 8 | ||
require_lowercase = true | ||
require_numbers = false | ||
require_symbols = false | ||
require_uppercase = false | ||
} | ||
} | ||
|
||
resource "aws_cognito_user_pool_client" "client" { | ||
name = "${var.envPrefix}-api-onboarding" | ||
generate_secret = false | ||
user_pool_id = "${aws_cognito_user_pool.pool.id}" | ||
|
||
provisioner "local-exec" { | ||
command = "${var.cognito_cmd} ${var.envPrefix} ${aws_cognito_user_pool.pool.id} ${aws_cognito_user_pool_client.client.id} ${var.cognito_pool_username} ${var.cognito_pool_password}" | ||
} | ||
provisioner "local-exec" { | ||
command = "${var.modifyPropertyFile_cmd} USER_POOL_ID ${aws_cognito_user_pool.pool.id} ${var.jenkinsjsonpropsfile}" | ||
} | ||
provisioner "local-exec" { | ||
command = "${var.modifyPropertyFile_cmd} CLIENT_ID ${aws_cognito_user_pool_client.client.id} ${var.jenkinsjsonpropsfile}" | ||
} | ||
} | ||
|
||
resource "aws_cognito_user_pool_domain" "domain" { | ||
domain = "${var.envPrefix}" | ||
user_pool_id = "${aws_cognito_user_pool.pool.id}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
provider "aws" { | ||
version = "~> 1.14" | ||
} | ||
|
||
provider "null" { | ||
version = "~> 1.0" | ||
} |
36 changes: 6 additions & 30 deletions
36
installscripts/jazz-terraform-unix-noinstances/scripts/cognito.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,15 @@ | ||
#!/bin/bash | ||
|
||
POOL_NAME=$1 | ||
CLIENT_NAME=$2 | ||
POOL_USER_NAME=$3 | ||
POOL_USER_PASSWORD=$4 | ||
jenkinsjsonpropsfile=$5 | ||
|
||
|
||
|
||
#Create the userpool | ||
aws cognito-idp create-user-pool --pool-name $POOL_NAME --username-attributes email --schema '{"Name": "email", "AttributeDataType": "String", "Required": true}' > /tmp/$POOL_NAME-user-pool | ||
USER_POOL_ID=$(grep -E '"Id":' /tmp/$POOL_NAME-user-pool | awk -F'"' '{print $4}') | ||
echo "Created User Pool: " $USER_POOL_ID | ||
|
||
#Update password policy and email verification information | ||
aws cognito-idp update-user-pool --user-pool-id $USER_POOL_ID --user-pool-tags Application="Jazz",JazzInstance="$POOL_NAME" --auto-verified-attributes email --verification-message-template '{"EmailSubjectByLink": "Jazz Notification - Account Verification", "EmailMessageByLink": "Hello,\n<br><br>\nThanks for signing up!\n<br><br>\nPlease click the link to verify your email address: {##Verify Email##}\n<br><br>\nTo know more about Jazz, please refer to link https://github.com/tmobile/jazz-core/wiki\n<br><br>\nBest,<br>\nJazz Team" , "DefaultEmailOption": "CONFIRM_WITH_LINK"}' --policies '{"PasswordPolicy": {"MinimumLength": 6,"RequireUppercase": false,"RequireLowercase": false, "RequireNumbers": false, "RequireSymbols": false}}' > /tmp/$POOL_NAME-user-pool | ||
|
||
#Update the custom attributes | ||
aws cognito-idp add-custom-attributes --user-pool-id $USER_POOL_ID --custom-attributes '{"Name": "reg-code", "AttributeDataType": "String", "StringAttributeConstraints":{"MinLength": "1"}}' | ||
|
||
#Create a userpool client | ||
aws cognito-idp create-user-pool-client --user-pool-id $USER_POOL_ID --no-generate-secret --client-name $CLIENT_NAME > /tmp/$POOL_NAME-app-client | ||
CLIENT_ID=$(grep -E '"ClientId":' /tmp/$POOL_NAME-app-client | awk -F'"' '{print $4}') | ||
echo "Created App Client: " $CLIENT_ID | ||
|
||
#Create a domain for the userpool | ||
aws cognito-idp create-user-pool-domain --domain $POOL_NAME --user-pool-id $USER_POOL_ID | ||
USER_POOL_ID=$2 | ||
CLIENT_ID=$3 | ||
POOL_USER_NAME=$4 | ||
POOL_USER_PASSWORD=$5 | ||
|
||
#Create a user | ||
aws cognito-idp sign-up --client-id $CLIENT_ID --username $POOL_USER_NAME --password $POOL_USER_PASSWORD > /tmp/$POOL_NAME-signup | ||
aws cognito-idp sign-up --client-id $CLIENT_ID --username $POOL_USER_NAME --password $POOL_USER_PASSWORD > $POOL_NAME-signup | ||
|
||
username_rand=`cat /tmp/$POOL_NAME-signup | grep -i usersub | awk '{print $2}' |tr -d '",'` | ||
username_rand=`cat $POOL_NAME-signup | grep -i usersub | awk '{print $2}' |tr -d '",'` | ||
|
||
#Auto verify the user | ||
aws cognito-idp admin-confirm-sign-up --user-pool-id $USER_POOL_ID --username $username_rand | ||
|
||
sed -i "s/USER_POOL_ID\".*.$/USER_POOL_ID\": \"$USER_POOL_ID\",/g" $jenkinsjsonpropsfile | ||
sed -i "s/CLIENT_ID\".*.$/CLIENT_ID\": \"$CLIENT_ID\"/g" $jenkinsjsonpropsfile |
15 changes: 0 additions & 15 deletions
15
installscripts/jazz-terraform-unix-noinstances/scripts/cognitoDelete.sh
This file was deleted.
Oops, something went wrong.
57 changes: 39 additions & 18 deletions
57
installscripts/jazz-terraform-unix-noinstances/scripts/create.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,41 @@ | ||
#!/bin/bash | ||
#!/bin/sh | ||
|
||
print_error() | ||
{ | ||
printf "\r${RED}$1${NC}\n" 1>&3 2>&4 | ||
} | ||
|
||
rm -f ./settings.txt | ||
date | ||
terraform apply \ | ||
-var "aws_access_key=${AWS_ACCESS_KEY_ID}" \ | ||
-var "aws_secret_key=${AWS_SECRET_ACCESS_KEY}" \ | ||
-var "region=${AWS_DEFAULT_REGION}" | ||
date | ||
echo " =======================================================" | ||
echo " The following stack has been created in AWS" | ||
echo " ________________________________________________" | ||
terraform state list | ||
echo " =======================================================" | ||
echo " Please use the following values for checking out Jazz" | ||
echo " ________________________________________________" | ||
cat ./settings.txt | ||
echo " =======================================================" | ||
echo " Installation complete! To cleanup Jazz stack and its resources execute ./destroy.sh in this directory." | ||
realpath ../../ | ||
echo " =======================================================" | ||
terraform init && terraform apply \ | ||
--auto-approve \ | ||
-var "aws_access_key=${AWS_ACCESS_KEY_ID}" \ | ||
-var "aws_secret_key=${AWS_SECRET_ACCESS_KEY}" \ | ||
-var "region=${AWS_DEFAULT_REGION}" | ||
if [ $? -gt 0 ] | ||
then | ||
date | ||
print_error "$message....Failed" | ||
print_error "Destroying created AWS resources because of failure" | ||
terraform destroy --auto-approve | ||
echo " =======================================================" | ||
echo " To cleanup Jazz stack and its resources execute ./destroy.sh in this directory." | ||
realpath ../../ | ||
echo " =======================================================" | ||
exit | ||
else | ||
date | ||
echo " =======================================================" | ||
echo " The following stack has been created in AWS" | ||
echo " ________________________________________________" | ||
terraform state list | ||
echo " =======================================================" | ||
echo " Please use the following values for checking out Jazz" | ||
echo " ________________________________________________" | ||
cat ./settings.txt | ||
echo " =======================================================" | ||
echo " Installation complete! To cleanup Jazz stack and its resources execute ./destroy.sh in this directory." | ||
realpath ../../ | ||
echo " =======================================================" | ||
exit | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters