-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-assume-role-simplesamlphp.sh
executable file
·41 lines (36 loc) · 1.33 KB
/
aws-assume-role-simplesamlphp.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
#!/bin/bash
#
# Get AWS API Key by using `aws sts assume-role-with-saml` and get-saml-assertion-from-simplesamlphp.sh
# Export commands will echo to stdout. Use eval to export to environment.
#
# Example:
# eval `aws-assume-role-simplesamlphp.sh ACCOUNT_ALIAS USER`
#
# Based on https://gist.github.com/borgand/6714584
account=${1:-default}
user=${2:-`whoami`}
role=$3
# Edit the values below based on simpleSAMLphp and AWS setup
case $account in
alias2)
ssourl='https://mysso2.example.com/saml/saml2/idp/SSOService.php?spentityid=aws'
arn=arn:aws:iam::123456789012
idp=saml-provider/myOtheridp
role=role/${3:-powerUser}
;;
*)
ssourl='https://mysso.example.com/saml/saml2/idp/SSOService.php?spentityid=aws'
arn=arn:aws:iam::XXXXXXXXXXXX
idp=saml-provider/myDefaultidp
role=role/${3:-User}
;;
esac
DIR="$(dirname "$0")"
SAMLRESPONSE=`$DIR/get-saml-assertion-from-simplesamlphp.sh "$ssourl" "$user"`
CREDS=( $(aws sts assume-role-with-saml --role-arn $arn:$role --principal-arn $arn:$idp \
--saml-assertion "$SAMLRESPONSE" --output text --query 'Credentials.{id:AccessKeyId,secret:SecretAccessKey}') ) ||
echo "Problem getting credentials from AWS AssumeRoleWithSaml API call" 1>&2
if [ "$CREDS" ]; then
echo export AWS_ACCESS_KEY_ID="${CREDS[0]}"
echo export AWS_SECRET_ACCESS_KEY="${CREDS[1]}"
fi