user_command.go 7.2 KB

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