Skip to content

Instantly share code, notes, and snippets.

@hexfusion
Created May 4, 2018 12:50
Show Gist options
  • Save hexfusion/ff278e3d900a1869059e14490823bc52 to your computer and use it in GitHub Desktop.
Save hexfusion/ff278e3d900a1869059e14490823bc52 to your computer and use it in GitHub Desktop.
etcd v3 gRPC gateway authentication and add new user example in bash.
#!/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