user_command.go 6.8 KB

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