#!/bin/bash
# First argument - named profile which is used for assuming the role. Second argument - otp from google authenticator.
if  [ ! -x "$(command -v jq)" ] && [ -x "$(command -v brew)" ]; then
  echo "Installing JQ on mac"
  brew install jq
fi
set -euo pipefail;
role_arn=$(aws configure get ${1}.role_arn)
mfa_serial=$(aws configure get ${1}.mfa_serial)
creds_json=$(aws sts assume-role --output json --profile gdb-infra-dev --role-arn ${role_arn} --duration-seconds 43200 --role-session-name assumed-session --serial-number ${mfa_serial} --token-code ${2})
aws configure set default.aws_access_key_id $(echo $creds_json | jq -r .Credentials.AccessKeyId)
aws configure set default.aws_secret_access_key $(echo $creds_json | jq -r .Credentials.SecretAccessKey)
aws configure set default.aws_session_token $(echo $creds_json | jq -r .Credentials.SessionToken)
