Created
May 4, 2018 12:50
-
-
Save hexfusion/ff278e3d900a1869059e14490823bc52 to your computer and use it in GitHub Desktop.
etcd v3 gRPC gateway authentication and add new user example in bash.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
newuser=$1 | |
read -s -p "Enter password for $newuser" newpass | |
user=root | |
pass=toor | |
host=127.0.0.1 | |
port=2379 | |
cacert="path/to/ca.pem" | |
key="path/to/client-key.pem" | |
cert="path/to/client.pem" | |
tokengen() { | |
json=$(printf '{"name": "%s", "password": "%s"}' \ | |
"$(escape "$1")" \ | |
"$(escape "$2")" | |
) | |
curl -s --cacert $cacert \ | |
--key $key \ | |
--cert $cert \ | |
-X POST \ | |
-d "$json" \ | |
https://${host}:${port}/v3beta/auth/authenticate \ | |
| jq -r '.token' | |
} | |
add_user() { | |
json=$(printf '{"name": "%s", "password": "%s"}' \ | |
"$(escape "$1")" \ | |
"$(escape "$2")" | |
) | |
curl -s --cacert $cacert \ | |
--key $key \ | |
--cert $cert \ | |
-H "Authorization: $3" \ | |
-X POST \ | |
-d "$json" \ | |
https://${host}:${port}/v3beta/auth/user/add | |
} | |
escape() { | |
echo "${1//\"/\\\"}" | |
} | |
token=$(tokengen $user $pass) | |
add_user_response=$(add_user $newuser $newpass $token) | |
echo -e "\n$add_user_response" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment