Skip to content

Instantly share code, notes, and snippets.

@JohannesRudolph
Last active September 23, 2017 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohannesRudolph/c08fe856a31254a9d276eba9daa2ebe6 to your computer and use it in GitHub Desktop.
Save JohannesRudolph/c08fe856a31254a9d276eba9daa2ebe6 to your computer and use it in GitHub Desktop.
Fetching a project-scoped Keystone V3 Token using bash
#!/bin/bash
set -e
if [ -z "$OS_USERNAME" ]; then echo "Error: OS_USERNAME is unset"; exit 1; fi;
if [ -z "$OS_PASSWORD" ]; then echo "Error: OS_PASSWORD is unset"; exit 1; fi;
if [ -z "$OS_USER_DOMAIN_NAME" ]; then echo "Error: OS_USER_DOMAIN_NAME is unset"; exit 1; fi;
if [ -z "$OS_PROJECT_DOMAIN_NAME" ]; then echo "Error: OS_PROJECT_DOMAIN_NAME is unset"; exit 1; fi;
if [ -z "$OS_AUTH_URL" ]; then echo "Error: OS_AUTH_URL is unset"; exit 1; fi;
if [ -z "$OS_PROJECT_NAME" ]; then echo "Error: OS_PROJECT_NAME is unset"; exit 1; fi;
generate_post_data()
{
cat <<EOF
{
"auth": {
"identity": {
"methods": [
"password"
],
"password": {
"user": {
"domain": {
"name": "$OS_USER_DOMAIN_NAME"
},
"name": "$OS_USERNAME",
"password": "$OS_PASSWORD"
}
}
},
"scope": {
"project": {
"name": "$OS_PROJECT_NAME",
"domain": {
"name": "$OS_PROJECT_DOMAIN_NAME"
}
}
}
}
}
EOF
}
response=$(curl -is \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
-X POST --data "$(generate_post_data)" "$OS_AUTH_URL/auth/tokens")
# we have to remove a trailing carriage return here so that we can return a clean token
token=$(echo "$response" | awk '/[Xx]-[Ss]ubject-[Tt]oken/ {print $2}' | tr -d '\r')
[ -z "$token" ] && echo -e "Error fetching token:\n$response" && exit 1;
echo "$token";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment