user_command.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "github.com/bgentry/speakeasy"
  20. "github.com/spf13/cobra"
  21. )
  22. var (
  23. userShowDetail bool
  24. )
  25. // NewUserCommand returns the cobra command for "user".
  26. func NewUserCommand() *cobra.Command {
  27. ac := &cobra.Command{
  28. Use: "user <subcommand>",
  29. Short: "User related commands",
  30. }
  31. ac.AddCommand(newUserAddCommand())
  32. ac.AddCommand(newUserDeleteCommand())
  33. ac.AddCommand(newUserGetCommand())
  34. ac.AddCommand(newUserListCommand())
  35. ac.AddCommand(newUserChangePasswordCommand())
  36. ac.AddCommand(newUserGrantRoleCommand())
  37. ac.AddCommand(newUserRevokeRoleCommand())
  38. return ac
  39. }
  40. var (
  41. passwordInteractive bool
  42. passwordFromFlag string
  43. )
  44. func newUserAddCommand() *cobra.Command {
  45. cmd := cobra.Command{
  46. Use: "add <user name or user:password> [options]",
  47. Short: "Adds a new user",
  48. Run: userAddCommandFunc,
  49. }
  50. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "Read password from stdin instead of interactive terminal")
  51. cmd.Flags().StringVar(&passwordFromFlag, "new-user-password", "", "Supply password from the command line flag")
  52. return &cmd
  53. }
  54. func newUserDeleteCommand() *cobra.Command {
  55. return &cobra.Command{
  56. Use: "delete <user name>",
  57. Short: "Deletes a user",
  58. Run: userDeleteCommandFunc,
  59. }
  60. }
  61. func newUserGetCommand() *cobra.Command {
  62. cmd := cobra.Command{
  63. Use: "get <user name> [options]",
  64. Short: "Gets detailed information of a user",
  65. Run: userGetCommandFunc,
  66. }
  67. cmd.Flags().BoolVar(&userShowDetail, "detail", false, "Show permissions of roles granted to the user")
  68. return &cmd
  69. }
  70. func newUserListCommand() *cobra.Command {
  71. return &cobra.Command{
  72. Use: "list",
  73. Short: "Lists all users",
  74. Run: userListCommandFunc,
  75. }
  76. }
  77. func newUserChangePasswordCommand() *cobra.Command {
  78. cmd := cobra.Command{
  79. Use: "passwd <user name> [options]",
  80. Short: "Changes password of user",
  81. Run: userChangePasswordCommandFunc,
  82. }
  83. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "If true, read password from stdin instead of interactive terminal")
  84. return &cmd
  85. }
  86. func newUserGrantRoleCommand() *cobra.Command {
  87. return &cobra.Command{
  88. Use: "grant-role <user name> <role name>",
  89. Short: "Grants a role to a user",
  90. Run: userGrantRoleCommandFunc,
  91. }
  92. }
  93. func newUserRevokeRoleCommand() *cobra.Command {
  94. return &cobra.Command{
  95. Use: "revoke-role <user name> <role name>",
  96. Short: "Revokes a role from a user",
  97. Run: userRevokeRoleCommandFunc,
  98. }
  99. }
  100. // userAddCommandFunc executes the "user add" command.
  101. func userAddCommandFunc(cmd *cobra.Command, args []string) {
  102. if len(args) != 1 {
  103. ExitWithError(ExitBadArgs, fmt.Errorf("user add command requires user name as its argument."))
  104. }
  105. var password string
  106. var user string
  107. if passwordFromFlag != "" {
  108. user = args[0]
  109. password = passwordFromFlag
  110. } else {
  111. splitted := strings.SplitN(args[0], ":", 2)
  112. if len(splitted) < 2 {
  113. user = args[0]
  114. if !passwordInteractive {
  115. fmt.Scanf("%s", &password)
  116. } else {
  117. password = readPasswordInteractive(args[0])
  118. }
  119. } else {
  120. user = splitted[0]
  121. password = splitted[1]
  122. if len(user) == 0 {
  123. ExitWithError(ExitBadArgs, fmt.Errorf("empty user name is not allowed."))
  124. }
  125. }
  126. }
  127. resp, err := mustClientFromCmd(cmd).Auth.UserAdd(context.TODO(), user, password)
  128. if err != nil {
  129. ExitWithError(ExitError, err)
  130. }
  131. display.UserAdd(user, *resp)
  132. }
  133. // userDeleteCommandFunc executes the "user delete" command.
  134. func userDeleteCommandFunc(cmd *cobra.Command, args []string) {
  135. if len(args) != 1 {
  136. ExitWithError(ExitBadArgs, fmt.Errorf("user delete command requires user name as its argument."))
  137. }
  138. resp, err := mustClientFromCmd(cmd).Auth.UserDelete(context.TODO(), args[0])
  139. if err != nil {
  140. ExitWithError(ExitError, err)
  141. }
  142. display.UserDelete(args[0], *resp)
  143. }
  144. // userGetCommandFunc executes the "user get" command.
  145. func userGetCommandFunc(cmd *cobra.Command, args []string) {
  146. if len(args) != 1 {
  147. ExitWithError(ExitBadArgs, fmt.Errorf("user get command requires user name as its argument."))
  148. }
  149. name := args[0]
  150. client := mustClientFromCmd(cmd)
  151. resp, err := client.Auth.UserGet(context.TODO(), name)
  152. if err != nil {
  153. ExitWithError(ExitError, err)
  154. }
  155. if userShowDetail {
  156. fmt.Printf("User: %s\n", name)
  157. for _, role := range resp.Roles {
  158. fmt.Printf("\n")
  159. roleResp, err := client.Auth.RoleGet(context.TODO(), role)
  160. if err != nil {
  161. ExitWithError(ExitError, err)
  162. }
  163. display.RoleGet(role, *roleResp)
  164. }
  165. } else {
  166. display.UserGet(name, *resp)
  167. }
  168. }
  169. // userListCommandFunc executes the "user list" command.
  170. func userListCommandFunc(cmd *cobra.Command, args []string) {
  171. if len(args) != 0 {
  172. ExitWithError(ExitBadArgs, fmt.Errorf("user list command requires no arguments."))
  173. }
  174. resp, err := mustClientFromCmd(cmd).Auth.UserList(context.TODO())
  175. if err != nil {
  176. ExitWithError(ExitError, err)
  177. }
  178. display.UserList(*resp)
  179. }
  180. // userChangePasswordCommandFunc executes the "user passwd" command.
  181. func userChangePasswordCommandFunc(cmd *cobra.Command, args []string) {
  182. if len(args) != 1 {
  183. ExitWithError(ExitBadArgs, fmt.Errorf("user passwd command requires user name as its argument."))
  184. }
  185. var password string
  186. if !passwordInteractive {
  187. fmt.Scanf("%s", &password)
  188. } else {
  189. password = readPasswordInteractive(args[0])
  190. }
  191. resp, err := mustClientFromCmd(cmd).Auth.UserChangePassword(context.TODO(), args[0], password)
  192. if err != nil {
  193. ExitWithError(ExitError, err)
  194. }
  195. display.UserChangePassword(*resp)
  196. }
  197. // userGrantRoleCommandFunc executes the "user grant-role" command.
  198. func userGrantRoleCommandFunc(cmd *cobra.Command, args []string) {
  199. if len(args) != 2 {
  200. ExitWithError(ExitBadArgs, fmt.Errorf("user grant command requires user name and role name as its argument."))
  201. }
  202. resp, err := mustClientFromCmd(cmd).Auth.UserGrantRole(context.TODO(), args[0], args[1])
  203. if err != nil {
  204. ExitWithError(ExitError, err)
  205. }
  206. display.UserGrantRole(args[0], args[1], *resp)
  207. }
  208. // userRevokeRoleCommandFunc executes the "user revoke-role" command.
  209. func userRevokeRoleCommandFunc(cmd *cobra.Command, args []string) {
  210. if len(args) != 2 {
  211. ExitWithError(ExitBadArgs, fmt.Errorf("user revoke-role requires user name and role name as its argument."))
  212. }
  213. resp, err := mustClientFromCmd(cmd).Auth.UserRevokeRole(context.TODO(), args[0], args[1])
  214. if err != nil {
  215. ExitWithError(ExitError, err)
  216. }
  217. display.UserRevokeRole(args[0], args[1], *resp)
  218. }
  219. func readPasswordInteractive(name string) string {
  220. prompt1 := fmt.Sprintf("Password of %s: ", name)
  221. password1, err1 := speakeasy.Ask(prompt1)
  222. if err1 != nil {
  223. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err1))
  224. }
  225. if len(password1) == 0 {
  226. ExitWithError(ExitBadArgs, fmt.Errorf("empty password"))
  227. }
  228. prompt2 := fmt.Sprintf("Type password of %s again for confirmation: ", name)
  229. password2, err2 := speakeasy.Ask(prompt2)
  230. if err2 != nil {
  231. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err2))
  232. }
  233. if password1 != password2 {
  234. ExitWithError(ExitBadArgs, fmt.Errorf("given passwords are different."))
  235. }
  236. return password1
  237. }