Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

CI/CD SMTP updates

Rich Turner640198acefe8
diff --git a/.ci_cd/aws/cloudformation-create-ec2.yml b/.ci_cd/aws/cloudformation-create-ec2.yml
index 6bd5062d5..4a948fc33 100644
--- a/.ci_cd/aws/cloudformation-create-ec2.yml
+++ b/.ci_cd/aws/cloudformation-create-ec2.yml
@@ -52,9 +52,6 @@ Parameters:
DNSHostedZoneRoleArn:
Description: Role ARN that can be assumed for updating DNS A record (if Hosted Zone in another account)
Type: String
- SMTPORArn:
- Description: SES Verified identity trust policy ARN for SMTP X-Headers
- Type: String
ElasticIP:
Description: Assign elastic IP address
Type: String
@@ -62,12 +59,21 @@ Parameters:
- true
- false
Default: false
- SMTPKey:
- Type: AWS::SSM::Parameter::Value<String>
- Default: SMTP-User-Key
+ SMTPHost:
+ Type: String
+ SMTPUser:
+ Type: String
+ SMTPPassword:
+ Type: String
SMTPSecret:
- Type: AWS::SSM::Parameter::Value<String>
- Default: SMTP-User-Secret
+ Type: String
+ Description: SMTP CLI user secret (used to generate an SMTP password from)
+ SMTPRegion:
+ Type: String
+ Description: SMTP CLI user region (used to generate an SMTP password from)
+ SMTPORArn:
+ Description: SES Verified identity trust policy ARN for SMTP X-Headers
+ Type: String
Conditions:
CreateDNSRecord: !Equals [!Ref DNSHostedZoneRoleArn, ""]
@@ -495,12 +501,22 @@ Resources:
commands:
01_output_smtp_credentials:
command: !Sub |
- PASS=$(python3 /home/ec2-user/smtp_credentials_generate.py ${SMTPSecret} ${AWS::Region})
- echo "OR_EMAIL_HOST=email-smtp.${AWS::Region}.amazonaws.com" > /etc/environment
- echo "OR_EMAIL_USER=${SMTPKey}" >> /etc/environment
- echo "OR_EMAIL_PASSWORD=$PASS" >> /etc/environment
- echo 'OR_EMAIL_X_HEADERS="X-SES-SOURCE-ARN: ${SMTPORArn}"' >> /etc/environment
- echo "AWS_REGION=${AWS::Region}" >> /etc/environment
+ echo "AWS_REGION=${AWS::Region}" > /etc/environment
+ if [ -n "${SMTPHost}" ]; then
+ echo "OR_EMAIL_HOST=${SMTPHost}" >> /etc/environment
+ fi
+ if [ -n "${SMTPUser}" ]; then
+ echo "OR_EMAIL_USER=${SMTPUser}" >> /etc/environment
+ fi
+ if [ -n "${SMTPPassword}" ]; then
+ echo "OR_EMAIL_PASSWORD=${SMTPPassword}" >> /etc/environment
+ elif [ -n "${SMTPSecret}" ] && [ -n "${SMTPRegion}" ]; then
+ PASS=$(python3 /home/ec2-user/smtp_credentials_generate.py ${SMTPSecret} ${SMTPRegion})
+ echo "OR_EMAIL_PASSWORD=$PASS" >> /etc/environment
+ fi
+ if [ -n "${SMTPORArn}" ]; then
+ echo 'OR_EMAIL_X_HEADERS="X-SES-SOURCE-ARN: ${SMTPORArn}"' >> /etc/environment
+ fi
docker_setup:
packages:
diff --git a/.ci_cd/aws/cloudformation-create-smtp-user.yml b/.ci_cd/aws/cloudformation-create-smtp-user.yml
new file mode 100644
index 000000000..4bd650840
--- /dev/null
+++ b/.ci_cd/aws/cloudformation-create-smtp-user.yml
@@ -0,0 +1,43 @@
+# ---------------------------------------------------------------------------------------------
+#
+# Creates CLI user with permission to send emails.
+#
+# ---------------------------------------------------------------------------------------------
+AWSTemplateFormatVersion: '2010-09-09'
+Description: 'IAM User SMTP'
+Parameters:
+ UserName:
+ Description: User name for smtp user
+ Type: String
+ #AllowedPattern: "^\S{4,}$"
+
+Resources:
+
+ SMTPUser:
+ Type: AWS::IAM::User
+ Properties:
+ UserName: !Ref UserName
+ Policies:
+ - PolicyName: SESSendEmailAccess
+ PolicyDocument:
+ Version: '2012-10-17'
+ Statement:
+ - Effect: Allow
+ Action:
+ - ses:SendEmail
+ - ses:SendRawEmail
+ Resource:
+ - '*'
+
+ SMTPCredentials:
+ Type: AWS::IAM::AccessKey
+ Properties:
+ UserName: !Ref SMTPUser
+
+Outputs:
+ SMTPUserKey:
+ Description: SMTP server username
+ Value: !Ref SMTPCredentials
+ SMTPUserSecret:
+ Description: SMTP server password
+ Value: !GetAtt SMTPCredentials.SecretAccessKey
diff --git a/.ci_cd/aws/provision_host.sh b/.ci_cd/aws/provision_host.sh
index e328affe8..ced64aeab 100644
--- a/.ci_cd/aws/provision_host.sh
+++ b/.ci_cd/aws/provision_host.sh
@@ -54,6 +54,58 @@ if [ -n "$AWS_ACCOUNT_NAME" ]; then
fi
STACK_NAME=$(tr '.' '-' <<< "$HOST")
+SMTP_STACK_NAME="$STACK_NAME-smtp"
+
+# Provision SMTP user using cloud formation (if stack doesn't already exist)
+echo "Provisioning SMTP user"
+STATUS=$(aws cloudformation describe-stacks --stack-name $SMTP_STACK_NAME --query "Stacks[0].StackStatus" --output text 2>/dev/null)
+
+if [ -n "$STATUS" ] && [ "$STATUS" != 'DELETE_COMPLETE' ]; then
+ echo "Stack already exists for this host's SMTP user '$HOST' current status is '$STATUS'"
+ STACK_ID=$(aws cloudformation describe-stacks --stack-name $SMTP_STACK_NAME --query "Stacks[0].StackId" --output text 2>/dev/null)
+else
+
+ if [ -f "${awsDir}cloudformation-create-smtp-user.yml" ]; then
+ SMTP_TEMPLATE_PATH="${awsDir}cloudformation-create-smtp-user.yml"
+ elif [ -f ".ci_cd/aws/cloudformation-create-smtp-user.yml" ]; then
+ SMTP_TEMPLATE_PATH=".ci_cd/aws/cloudformation-create-smtp-user.yml"
+ elif [ -f "openremote/.ci_cd/aws/cloudformation-create-smtp-user.yml" ]; then
+ SMTP_TEMPLATE_PATH="openremote/.ci_cd/aws/cloudformation-create-smtp-user.yml"
+ else
+ echo "Cannot determine location of cloudformation-create-smtp-user.yml"
+ exit 1
+ fi
+
+ #Configure parameters
+ PARAMS="ParameterKey=UserName,ParameterValue='$SMTP_STACK_NAME'"
+
+ # Create standard stack resources in specified account
+ STACK_ID=$(aws cloudformation create-stack --capabilities CAPABILITY_NAMED_IAM --stack-name $SMTP_STACK_NAME --template-body file://$SMTP_TEMPLATE_PATH --parameters $PARAMS --output text)
+
+ if [ $? -ne 0 ]; then
+ echo "Create stack failed"
+ exit 1
+ fi
+
+ if [ "$WAIT_FOR_STACK" != 'false' ]; then
+ # Wait for cloud formation stack status to be CREATE_*
+ echo "Waiting for stack to be created"
+ STATUS=$(aws cloudformation describe-stacks --stack-name $SMTP_STACK_NAME --query "Stacks[?StackId=='$STACK_ID'].StackStatus" --output text 2>/dev/null)
+
+ while [[ "$STATUS" == 'CREATE_IN_PROGRESS' ]]; do
+ echo "Stack creation is still in progress .. Sleeping 30 seconds"
+ sleep 30
+ STATUS=$(aws cloudformation describe-stacks --stack-name $SMTP_STACK_NAME --query "Stacks[?StackId=='$STACK_ID'].StackStatus" --output text 2>/dev/null)
+ done
+
+ if [ "$STATUS" != 'CREATE_COMPLETE' ]; then
+ echo "Stack creation has failed status is '$STATUS'"
+ exit 1
+ else
+ echo "Stack creation is complete"
+ fi
+ fi
+fi
# Check stack doesn't already exist
STATUS=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --query "Stacks[0].StackStatus" --output text $ACCOUNT_PROFILE 2>/dev/null)
@@ -62,15 +114,27 @@ if [ -n "$STATUS" ] && [ "$STATUS" != 'DELETE_COMPLETE' ]; then
echo "Stack already exists for this host '$HOST' current status is '$STATUS'"
STACK_ID=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --query "Stacks[0].StackId" --output text $ACCOUNT_PROFILE 2>/dev/null)
else
- #Configure parameters
- CALLER_AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
- SMTP_ARN="arn:aws:ses:$AWS_REGION:$CALLER_AWS_ACCOUNT_ID:identity/openremote.io"
- PARAMS="ParameterKey=Host,ParameterValue=$HOST ParameterKey=SMTPORArn,ParameterValue=$SMTP_ARN"
+ # Configure parameters
+ PARAMS="ParameterKey=Host,ParameterValue=$HOST"
if [ -n "$INSTANCE_TYPE" ]; then
PARAMS="$PARAMS ParameterKey=InstanceType,ParameterValue=$INSTANCE_TYPE"
fi
+ # Get SMTP credentials
+ SMTP_HOST="email-smtp.$AWS_REGION.amazonaws.com"
+ SMTP_USER=$(aws cloudformation describe-stacks --stack-name $SMTP_STACK_NAME --query "Stacks[0].Outputs[?OutputKey=='SMTPUserKey'].OutputValue" --output text 2>/dev/null)
+ SMTP_SECRET=$(aws cloudformation describe-stacks --stack-name $SMTP_STACK_NAME --query "Stacks[0].Outputs[?OutputKey=='SMTPUserSecret'].OutputValue" --output text 2>/dev/null)
+
+ PARAMS="$PARAMS ParameterKey=SMTPHost,ParameterValue=$SMTP_HOST"
+
+ if [ -n "$SMTP_USER" ]; then
+ PARAMS="$PARAMS ParameterKey=SMTPUser,ParameterValue=$SMTP_USER"
+ fi
+ if [ -n "$SMTP_SECRET" ]; then
+ PARAMS="$PARAMS ParameterKey=SMTPSecret,ParameterValue=$SMTP_USER ParameterKey=SMTPRegion,ParameterValue=$AWS_REGION"
+ fi
+
# Determine DNSHostedZoneName and DNSHostedZoneRoleArn (must be set if hosted zone is not in the same account as where the host is being created)
echo "Determining DNS parameters"
TLD_NAME=$(awk -F. '{print $(NF-1)"."$(NF)}' <<< "$HOST")
@@ -151,7 +215,7 @@ if [ "$WAIT_FOR_STACK" != 'false' ]; then
STATUS=$(aws cloudformation describe-stacks --stack-name $STACK_NAME --query "Stacks[?StackId=='$STACK_ID'].StackStatus" --output text $ACCOUNT_PROFILE 2>/dev/null)
done
- if [ "$STATUS" != 'CREATE_COMPLETE' ]; then
+ if [ "$STATUS" != 'CREATE_COMPLETE' ] && [ "$STATUS" != 'UPDATE_COMPLETE' ]; then
echo "Stack creation has failed status is '$STATUS'"
exit 1
else
@@ -159,7 +223,7 @@ if [ "$WAIT_FOR_STACK" != 'false' ]; then
fi
fi
-# Do this outside of cloudformation template so bucket is not deleted if the stack is removed
+# Provision S3 bucket
if [ "$PROVISION_S3_BUCKET" != 'false' ]; then
echo "Provisioning S3 bucket for host '$HOST'"