user_add.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/bin/bash
  2. # Copyright 2018 The etcd Authors
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. usage () {
  16. echo 'Username required: ./user_add.sh $username'
  17. exit
  18. }
  19. if [ "$1" == "" ]; then
  20. usage
  21. fi
  22. newuser=$1
  23. read -r -s -p "Enter password for $newuser" newpass
  24. user=root
  25. pass=toor
  26. host=127.0.0.1
  27. port=2379
  28. api=v3
  29. cacert="path/to/ca.pem"
  30. key="path/to/client-key.pem"
  31. cert="path/to/client.pem"
  32. tokengen() {
  33. json=$(printf '{"name": "%s", "password": "%s"}' \
  34. "$(escape "$1")" \
  35. "$(escape "$2")"
  36. )
  37. curl -s --cacert $cacert \
  38. --key $key \
  39. --cert $cert \
  40. -X POST \
  41. -d "$json" \
  42. https://${host}:${port}/${api}/auth/authenticate \
  43. | jq -r '.token'
  44. }
  45. add_user() {
  46. json=$(printf '{"name": "%s", "password": "%s"}' \
  47. "$(escape "$1")" \
  48. "$(escape "$2")"
  49. )
  50. curl -s --cacert $cacert \
  51. --key $key \
  52. --cert $cert \
  53. -H "Authorization: $3" \
  54. -X POST \
  55. -d "$json" \
  56. https://${host}:${port}/${api}/auth/user/add
  57. }
  58. escape() {
  59. echo "${1//\"/\\\"}"
  60. }
  61. token=$(tokengen $user $pass)
  62. response=$(add_user $newuser $newpass $token)
  63. echo -e "\\n$response"