user_command.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. "fmt"
  17. "strings"
  18. "github.com/bgentry/speakeasy"
  19. "github.com/spf13/cobra"
  20. "golang.org/x/net/context"
  21. )
  22. // NewUserCommand returns the cobra command for "user".
  23. func NewUserCommand() *cobra.Command {
  24. ac := &cobra.Command{
  25. Use: "user <subcommand>",
  26. Short: "user related command",
  27. }
  28. ac.AddCommand(newUserAddCommand())
  29. ac.AddCommand(newUserDeleteCommand())
  30. ac.AddCommand(newUserChangePasswordCommand())
  31. ac.AddCommand(newUserGrantCommand())
  32. ac.AddCommand(newUserGetCommand())
  33. ac.AddCommand(newUserRevokeRoleCommand())
  34. return ac
  35. }
  36. var (
  37. passwordInteractive bool
  38. )
  39. func newUserAddCommand() *cobra.Command {
  40. cmd := cobra.Command{
  41. Use: "add <user name>",
  42. Short: "add a new user",
  43. Run: userAddCommandFunc,
  44. }
  45. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "read password from stdin instead of interactive terminal")
  46. return &cmd
  47. }
  48. func newUserDeleteCommand() *cobra.Command {
  49. return &cobra.Command{
  50. Use: "delete <user name>",
  51. Short: "delete a user",
  52. Run: userDeleteCommandFunc,
  53. }
  54. }
  55. func newUserChangePasswordCommand() *cobra.Command {
  56. cmd := cobra.Command{
  57. Use: "passwd <user name>",
  58. Short: "change password of user",
  59. Run: userChangePasswordCommandFunc,
  60. }
  61. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "read password from stdin instead of interactive terminal")
  62. return &cmd
  63. }
  64. func newUserGrantCommand() *cobra.Command {
  65. return &cobra.Command{
  66. Use: "grant <user name> <role name>",
  67. Short: "grant a role to a user",
  68. Run: userGrantCommandFunc,
  69. }
  70. }
  71. func newUserGetCommand() *cobra.Command {
  72. // TODO(mitake): this command should also get detailed information of roles of the user
  73. return &cobra.Command{
  74. Use: "get <user name>",
  75. Short: "get detailed information of a user",
  76. Run: userGetCommandFunc,
  77. }
  78. }
  79. func newUserRevokeRoleCommand() *cobra.Command {
  80. return &cobra.Command{
  81. Use: "revoke-role <user name> <role name>",
  82. Short: "revoke a role from from a user",
  83. Run: userRevokeRoleCommandFunc,
  84. }
  85. }
  86. // userAddCommandFunc executes the "user add" command.
  87. func userAddCommandFunc(cmd *cobra.Command, args []string) {
  88. if len(args) != 1 {
  89. ExitWithError(ExitBadArgs, fmt.Errorf("user add command requires user name as its argument."))
  90. }
  91. var password string
  92. if !passwordInteractive {
  93. fmt.Scanf("%s", &password)
  94. } else {
  95. password = readPasswordInteractive(args[0])
  96. }
  97. _, err := mustClientFromCmd(cmd).Auth.UserAdd(context.TODO(), args[0], password)
  98. if err != nil {
  99. ExitWithError(ExitError, err)
  100. }
  101. fmt.Printf("User %s created\n", args[0])
  102. }
  103. // userDeleteCommandFunc executes the "user delete" command.
  104. func userDeleteCommandFunc(cmd *cobra.Command, args []string) {
  105. if len(args) != 1 {
  106. ExitWithError(ExitBadArgs, fmt.Errorf("user delete command requires user name as its argument."))
  107. }
  108. _, err := mustClientFromCmd(cmd).Auth.UserDelete(context.TODO(), args[0])
  109. if err != nil {
  110. ExitWithError(ExitError, err)
  111. }
  112. fmt.Printf("User %s deleted\n", args[0])
  113. }
  114. // userChangePasswordCommandFunc executes the "user passwd" command.
  115. func userChangePasswordCommandFunc(cmd *cobra.Command, args []string) {
  116. if len(args) != 1 {
  117. ExitWithError(ExitBadArgs, fmt.Errorf("user passwd command requires user name as its argument."))
  118. }
  119. var password string
  120. if !passwordInteractive {
  121. fmt.Scanf("%s", &password)
  122. } else {
  123. password = readPasswordInteractive(args[0])
  124. }
  125. _, err := mustClientFromCmd(cmd).Auth.UserChangePassword(context.TODO(), args[0], password)
  126. if err != nil {
  127. ExitWithError(ExitError, err)
  128. }
  129. fmt.Println("Password updated")
  130. }
  131. // userGrantCommandFunc executes the "user grant" command.
  132. func userGrantCommandFunc(cmd *cobra.Command, args []string) {
  133. if len(args) != 2 {
  134. ExitWithError(ExitBadArgs, fmt.Errorf("user grant command requires user name and role name as its argument."))
  135. }
  136. _, err := mustClientFromCmd(cmd).Auth.UserGrant(context.TODO(), args[0], args[1])
  137. if err != nil {
  138. ExitWithError(ExitError, err)
  139. }
  140. fmt.Printf("Role %s is granted to user %s\n", args[1], args[0])
  141. }
  142. // userGetCommandFunc executes the "user get" command.
  143. func userGetCommandFunc(cmd *cobra.Command, args []string) {
  144. if len(args) != 1 {
  145. ExitWithError(ExitBadArgs, fmt.Errorf("user get command requires user name as its argument."))
  146. }
  147. resp, err := mustClientFromCmd(cmd).Auth.UserGet(context.TODO(), args[0])
  148. if err != nil {
  149. ExitWithError(ExitError, err)
  150. }
  151. fmt.Printf("User: %s\n", args[0])
  152. fmt.Printf("Roles:")
  153. for _, role := range resp.Roles {
  154. fmt.Printf(" %s", role)
  155. }
  156. fmt.Printf("\n")
  157. }
  158. // userRevokeRoleCommandFunc executes the "user revoke-role" command.
  159. func userRevokeRoleCommandFunc(cmd *cobra.Command, args []string) {
  160. if len(args) != 2 {
  161. ExitWithError(ExitBadArgs, fmt.Errorf("user revoke-role requires user name and role name as its argument."))
  162. }
  163. _, err := mustClientFromCmd(cmd).Auth.UserRevokeRole(context.TODO(), args[0], args[1])
  164. if err != nil {
  165. ExitWithError(ExitError, err)
  166. }
  167. fmt.Printf("Role %s is revoked from user %s\n", args[1], args[0])
  168. }
  169. func readPasswordInteractive(name string) string {
  170. prompt1 := fmt.Sprintf("Password of %s: ", name)
  171. password1, err1 := speakeasy.Ask(prompt1)
  172. if err1 != nil {
  173. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err1))
  174. }
  175. if len(password1) == 0 {
  176. ExitWithError(ExitBadArgs, fmt.Errorf("empty password"))
  177. }
  178. prompt2 := fmt.Sprintf("Type password of %s again for confirmation: ", name)
  179. password2, err2 := speakeasy.Ask(prompt2)
  180. if err2 != nil {
  181. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s.", err2))
  182. }
  183. if strings.Compare(password1, password2) != 0 {
  184. ExitWithError(ExitBadArgs, fmt.Errorf("given passwords are different."))
  185. }
  186. return password1
  187. }