user_command.go 5.5 KB

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