Skip to content

Instantly share code, notes, and snippets.

@alex700
Created April 5, 2024 23:08
Show Gist options
  • Save alex700/39909b2294b53389e237c74d62710e82 to your computer and use it in GitHub Desktop.
Save alex700/39909b2294b53389e237c74d62710e82 to your computer and use it in GitHub Desktop.
Shell script to place AWS credentials issued by Opal to local AWS config
#!/bin/sh
# Usage Instructions
# - Open Opal and login to AWS.
# - Copy content with environment variables and place it to ~/.aws/opal-config.txt
# - Make the script executable: chmod +x update-config.sh
# - The script will place the credentials to ~/.aws/config
config_file="$HOME/.aws/opal-config.txt"
aws_config_file="$HOME/.aws/config"
# Check if the opal-config.txt exists
if [ ! -f "$config_file" ]; then
echo "Error: opal-config.txt not found."
exit 1
fi
# Read the opal-config.txt file
while IFS= read -r line || [[ -n "$line" ]]; do
# Remove leading/trailing whitespaces
line=$(echo "$line" | tr -d '[:space:]')
# Check if the line is empty or starts with '#'
if [ -z "$line" ] || [[ "$line" == "#"* ]]; then
continue
fi
# Extract key and value
key="${line%%=*}"
value="${line#*=}"
# Remove 'export' if present
key="${key#export}"
case "$key" in
AWS_ACCESS_KEY_ID)
aws_access_key_id="$value"
;;
AWS_SECRET_ACCESS_KEY)
aws_secret_access_key="$value"
;;
AWS_SESSION_TOKEN)
aws_session_token="$value"
;;
esac
echo "Key: $key, Value: $value"
done < "$config_file"
echo "$aws_access_key_id / $aws_secret_access_key / $aws_session_token"
# Write to ~/.aws/config
cat <<EOF > "$aws_config_file"
[default]
aws_access_key_id = $aws_access_key_id
aws_secret_access_key = $aws_secret_access_key
aws_session_token = $aws_session_token
EOF
echo "AWS configuration updated."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment