user_command.go 4.7 KB

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