-
Notifications
You must be signed in to change notification settings - Fork 0
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 #64 from NHSDigital/AMB-0008-Imms-aws-account-inte…
…gration AMB-0008-Imms-AWS-Account-Integration
- Loading branch information
Showing
7 changed files
with
76 additions
and
83 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
parameters: | ||
- name: 'role' | ||
type: string | ||
- name: 'profile' | ||
type: string | ||
default: '' | ||
- name: 'aws_account' | ||
type: string | ||
default: 'ptl' | ||
|
||
steps: | ||
- template: "azure/components/aws-clean-config.yml@common" | ||
|
||
- bash: | | ||
set -e | ||
echo "##vso[task.setvariable variable=ROLE]${{ parameters.role }}" | ||
displayName: get imms role name | ||
- bash: | | ||
set -e | ||
aws_role="$(ROLE)" | ||
echo "assume role: '${aws_role}'" | ||
if [[ "${{ parameters.aws_account }}" =~ ^(prod|dev)$ ]]; then | ||
echo "account is ${{ parameters.aws_account }}" | ||
account_id="$(aws ssm get-parameter --name /imms-account-ids/${{ parameters.aws_account }} --query Parameter.Value --output text)" | ||
aws_role="arn:aws:iam::${account_id}:role/${aws_role}" | ||
fi | ||
if [[ "${aws_role}" != arn:aws:iam:* ]]; then | ||
echo "check if role exists" | ||
# iam synchronisation issues can take a few to make the role appear | ||
for i in {1..15}; do | ||
if aws iam get-role --role-name ${aws_role} > /dev/null; then | ||
echo role exists | ||
sleep 2 | ||
break | ||
fi | ||
echo waiting for role ... | ||
sleep 2 | ||
done | ||
account_id="$(aws sts get-caller-identity --query Account --output text)" | ||
aws_role="arn:aws:iam::${account_id}:role/${aws_role}" | ||
fi | ||
cp ~/.aws/config.default ~/.aws/config | ||
tmp_file="$(Agent.TempDirectory)/.aws.tmp.creds.json" | ||
# add some backoff to allow for eventual consistency of IAM | ||
for i in {2..4}; | ||
do | ||
if aws sts assume-role --role-arn "${aws_role}" --role-session-name build-assume-role > ${tmp_file}; then | ||
echo assumed role | ||
assumed_role="yes" | ||
break | ||
fi | ||
let "sleep_for=$i*10"; | ||
sleep $sleep_for | ||
done | ||
if [[ "${assumed_role}" != "yes" ]]; then | ||
echo "assume role failed" | ||
exit -1 | ||
fi | ||
echo "aws_access_key_id = $(jq -r .Credentials.AccessKeyId ${tmp_file})" >> ~/.aws/config | ||
echo "aws_secret_access_key = $(jq -r .Credentials.SecretAccessKey ${tmp_file})" >> ~/.aws/config | ||
echo "aws_session_token = $(jq -r .Credentials.SessionToken ${tmp_file})" >> ~/.aws/config | ||
expiry=$(jq -r .Credentials.Expiration ${tmp_file}) | ||
echo "##vso[task.setvariable variable=ASSUME_ROLE_EXPIRY;]$expiry" | ||
rm ${tmp_file} | ||
profile="${{ parameters.profile }}" | ||
if [[ ! -z "${profile}" ]]; then | ||
echo as profile ${profile} | ||
sed -i "s#\[default\]#\[profile ${profile}\]#" ~/.aws/config | ||
fi | ||
displayName: assume role | ||
condition: and(succeeded(), ne(variables['ROLE'], '')) |
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 was deleted.
Oops, something went wrong.
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
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