Skip to content

Commit d95c51b

Browse files
committed
Simplified aws assume role
1 parent aa3c704 commit d95c51b

File tree

2 files changed

+77
-4
lines changed

2 files changed

+77
-4
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
parameters:
2+
- name: 'role'
3+
type: string
4+
- name: 'profile'
5+
type: string
6+
default: ''
7+
- name: 'aws_account_id'
8+
type: string
9+
10+
steps:
11+
- template: "azure/components/aws-clean-config.yml@common"
12+
13+
- bash: |
14+
set -e
15+
echo "##vso[task.setvariable variable=ROLE]${{ parameters.role }}"
16+
echo "##vso[task.setvariable variable=AWS_ACCOUNT_ID]${{ parameters.aws_account_id }}"
17+
displayName: get imms role name
18+
- bash: |
19+
set -e
20+
aws_role="$(ROLE)"
21+
echo "assume role: '${aws_role}'"
22+
echo "account_id: $(AWS_ACCOUNT_ID)"
23+
24+
aws_role="arn:aws:iam::${account_id}:role/${aws_role}"
25+
echo "AWS role: $aws_role"
26+
27+
echo "Check if role exists"
28+
# iam synchronisation issues can take a few to make the role appear
29+
for i in {1..15}; do
30+
if aws iam get-role --role-name ${aws_role} > /dev/null; then
31+
echo role exists
32+
sleep 2
33+
break
34+
fi
35+
echo waiting for role ...
36+
sleep 2
37+
done
38+
account_id="$(aws sts get-caller-identity --query Account --output text)"
39+
aws_role="arn:aws:iam::${account_id}:role/${aws_role}"
40+
41+
cp ~/.aws/config.default ~/.aws/config
42+
tmp_file="$(Agent.TempDirectory)/.aws.tmp.creds.json"
43+
# add some backoff to allow for eventual consistency of IAM
44+
for i in {2..4};
45+
do
46+
if aws sts assume-role --role-arn "${aws_role}" --role-session-name build-assume-role > ${tmp_file}; then
47+
echo assumed role
48+
assumed_role="yes"
49+
break
50+
fi
51+
let "sleep_for=$i*10";
52+
sleep $sleep_for
53+
done
54+
if [[ "${assumed_role}" != "yes" ]]; then
55+
echo "assume role failed"
56+
exit -1
57+
fi
58+
echo "aws_access_key_id = $(jq -r .Credentials.AccessKeyId ${tmp_file})" >> ~/.aws/config
59+
echo "aws_secret_access_key = $(jq -r .Credentials.SecretAccessKey ${tmp_file})" >> ~/.aws/config
60+
echo "aws_session_token = $(jq -r .Credentials.SessionToken ${tmp_file})" >> ~/.aws/config
61+
expiry=$(jq -r .Credentials.Expiration ${tmp_file})
62+
echo "##vso[task.setvariable variable=ASSUME_ROLE_EXPIRY;]$expiry"
63+
rm ${tmp_file}
64+
profile="${{ parameters.profile }}"
65+
if [[ ! -z "${profile}" ]]; then
66+
echo as profile ${profile}
67+
sed -i "s#\[default\]#\[profile ${profile}\]#" ~/.aws/config
68+
fi
69+
displayName: assume role
70+
condition: and(succeeded(), ne(variables['ROLE'], ''))

azure/new_pipelines/deploy-int-job.yml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,16 @@ steps:
3737
displayName: Setup pytests
3838
condition: always()
3939
40-
- template: ./aws-assume-role.yml
40+
- template: ./aws-assume-role-new-int.yml
4141
parameters:
4242
role: "auto-ops"
43-
profile: "apim-dev"
44-
aws_account: ${{ parameters.aws_account_type }}
43+
profile: "apim-dev" # centralise
44+
aws_account_id: "084828561157"
4545

4646
- bash: |
4747
set -e
4848
if ! [[ $APIGEE_ENVIRONMENT =~ .*-*sandbox ]]; then
49-
export AWS_PROFILE=apim-dev
49+
export AWS_PROFILE=apim-dev
5050
aws_account_no="$(aws sts get-caller-identity --query Account --output text)"
5151
5252
service_name=$(FULLY_QUALIFIED_SERVICE_NAME)
@@ -121,13 +121,15 @@ steps:
121121
fi
122122
displayName: Wait for API to be available
123123
workingDirectory: "$(Pipeline.Workspace)/s/$(SERVICE_NAME)/$(SERVICE_ARTIFACT_NAME)"
124+
condition: eq(1, 2)
124125
125126
- bash: |
126127
pyenv install -s 3.10.8
127128
pyenv install -s 3.11.11
128129
pyenv global 3.10.8
129130
python --version
130131
displayName: Install python 3.10 and 3.11
132+
condition: eq(1, 2)
131133
132134
- bash: |
133135
set -e
@@ -224,3 +226,4 @@ steps:
224226
inputs:
225227
testResultsFiles: '$(Pipeline.Workspace)/s/$(SERVICE_NAME)/$(SERVICE_ARTIFACT_NAME)/tests/test-report.xml'
226228
failTaskOnFailedTests: true
229+
condition: eq(1, 2)

0 commit comments

Comments
 (0)