mieczu

aws-connect

Aug 24th, 2023
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.20 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # wymaga sudo apt install jq -y
  4.  
  5. file="$HOME/.aws/credentials"
  6.  
  7. if [[ -a $file ]] ; then
  8.     sed '/autogenerated_config_start/,/autogenerated_config_end/d' $file > tmpfile && mv tmpfile $file
  9. else
  10.     echo "Error: credentials file not found (Run 'aws configure')"
  11. fi
  12.  
  13. rawCredentials=$(aws sts get-session-token --duration-seconds 43200 --serial-number arn:aws:iam::201200975089:mfa/cezarymieczkowski --token-code $1)
  14.  
  15. aws_key=$(echo $rawCredentials | jq -r '. | .Credentials.AccessKeyId')
  16. aws_secret=$(echo $rawCredentials | jq -r '. | .Credentials.SecretAccessKey')
  17. aws_token=$(echo $rawCredentials | jq -r '. | .Credentials.SessionToken')
  18. expiration_time=$(echo $rawCredentials | jq -r '. | .Credentials.Expiration')
  19.  
  20. if [ $aws_token != "" ]; then
  21.     echo "LOGGED IN! Token expiration:$expiration_time"
  22. fi
  23.  
  24. x=`tail -n 1 "$file"`
  25. if [ "$x" != "" ]; then
  26.     echo "" >> $file
  27. fi
  28.  
  29. echo "#autogenerated_config_start" >> $file
  30. echo "[autogenerated_config]" >> $file
  31. echo "aws_access_key_id = $aws_key" >> $file
  32. echo "aws_secret_access_key = $aws_secret" >> $file
  33. echo "aws_session_token = $aws_token" >> $file
  34. echo "#autogenerated_config_end" >> $file
  35.  
  36. echo "Credentials file updated"
Advertisement
Add Comment
Please, Sign In to add comment