user_command.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. "context"
  17. "fmt"
  18. "strings"
  19. "github.com/bgentry/speakeasy"
  20. "github.com/spf13/cobra"
  21. "go.etcd.io/etcd/clientv3"
  22. )
  23. var (
  24. userShowDetail bool
  25. )
  26. // NewUserCommand returns the cobra command for "user".
  27. func NewUserCommand() *cobra.Command {
  28. ac := &cobra.Command{
  29. Use: "user <subcommand>",
  30. Short: "User related commands",
  31. }
  32. ac.AddCommand(newUserAddCommand())
  33. ac.AddCommand(newUserDeleteCommand())
  34. ac.AddCommand(newUserGetCommand())
  35. ac.AddCommand(newUserListCommand())
  36. ac.AddCommand(newUserChangePasswordCommand())
  37. ac.AddCommand(newUserGrantRoleCommand())
  38. ac.AddCommand(newUserRevokeRoleCommand())
  39. return ac
  40. }
  41. var (
  42. passwordInteractive bool
  43. passwordFromFlag string
  44. noPassword bool
  45. )
  46. func newUserAddCommand() *cobra.Command {
  47. cmd := cobra.Command{
  48. Use: "add <user name or user:password> [options]",
  49. Short: "Adds a new user",
  50. Run: userAddCommandFunc,
  51. }
  52. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "Read password from stdin instead of interactive terminal")
  53. cmd.Flags().StringVar(&passwordFromFlag, "new-user-password", "", "Supply password from the command line flag")
  54. cmd.Flags().BoolVar(&noPassword, "no-password", false, "Create a user without password (CN based auth only)")
  55. return &cmd
  56. }
  57. func newUserDeleteCommand() *cobra.Command {
  58. return &cobra.Command{
  59. Use: "delete <user name>",
  60. Short: "Deletes a user",
  61. Run: userDeleteCommandFunc,
  62. }
  63. }
  64. func newUserGetCommand() *cobra.Command {
  65. cmd := cobra.Command{
  66. Use: "get <user name> [options]",
  67. Short: "Gets detailed information of a user",
  68. Run: userGetCommandFunc,
  69. }
  70. cmd.Flags().BoolVar(&userShowDetail, "detail", false, "Show permissions of roles granted to the user")
  71. return &cmd
  72. }
  73. func newUserListCommand() *cobra.Command {
  74. return &cobra.Command{
  75. Use: "list",
  76. Short: "Lists all users",
  77. Run: userListCommandFunc,
  78. }
  79. }
  80. func newUserChangePasswordCommand() *cobra.Command {
  81. cmd := cobra.Command{
  82. Use: "passwd <user name> [options]",
  83. Short: "Changes password of user",
  84. Run: userChangePasswordCommandFunc,
  85. }
  86. cmd.Flags().BoolVar(&passwordInteractive, "interactive", true, "If true, read password from stdin instead of interactive terminal")
  87. return &cmd
  88. }
  89. func newUserGrantRoleCommand() *cobra.Command {
  90. return &cobra.Command{
  91. Use: "grant-role <user name> <role name>",
  92. Short: "Grants a role to a user",
  93. Run: userGrantRoleCommandFunc,
  94. }
  95. }
  96. func newUserRevokeRoleCommand() *cobra.Command {
  97. return &cobra.Command{
  98. Use: "revoke-role <user name> <role name>",
  99. Short: "Revokes a role from a user",
  100. Run: userRevokeRoleCommandFunc,
  101. }
  102. }
  103. // userAddCommandFunc executes the "user add" command.
  104. func userAddCommandFunc(cmd *cobra.Command, args []string) {
  105. if len(args) != 1 {
  106. ExitWithError(ExitBadArgs, fmt.Errorf("user add command requires user name as its argument"))
  107. }
  108. var password string
  109. var user string
  110. options := &clientv3.UserAddOptions{
  111. NoPassword: false,
  112. }
  113. if !noPassword {
  114. if passwordFromFlag != "" {
  115. user = args[0]
  116. password = passwordFromFlag
  117. } else {
  118. splitted := strings.SplitN(args[0], ":", 2)
  119. if len(splitted) < 2 {
  120. user = args[0]
  121. if !passwordInteractive {
  122. fmt.Scanf("%s", &password)
  123. } else {
  124. password = readPasswordInteractive(args[0])
  125. }
  126. } else {
  127. user = splitted[0]
  128. password = splitted[1]
  129. if len(user) == 0 {
  130. ExitWithError(ExitBadArgs, fmt.Errorf("empty user name is not allowed"))
  131. }
  132. }
  133. }
  134. } else {
  135. user = args[0]
  136. options.NoPassword = true
  137. }
  138. resp, err := mustClientFromCmd(cmd).Auth.UserAddWithOptions(context.TODO(), user, password, options)
  139. if err != nil {
  140. ExitWithError(ExitError, err)
  141. }
  142. display.UserAdd(user, *resp)
  143. }
  144. // userDeleteCommandFunc executes the "user delete" command.
  145. func userDeleteCommandFunc(cmd *cobra.Command, args []string) {
  146. if len(args) != 1 {
  147. ExitWithError(ExitBadArgs, fmt.Errorf("user delete command requires user name as its argument"))
  148. }
  149. resp, err := mustClientFromCmd(cmd).Auth.UserDelete(context.TODO(), args[0])
  150. if err != nil {
  151. ExitWithError(ExitError, err)
  152. }
  153. display.UserDelete(args[0], *resp)
  154. }
  155. // userGetCommandFunc executes the "user get" command.
  156. func userGetCommandFunc(cmd *cobra.Command, args []string) {
  157. if len(args) != 1 {
  158. ExitWithError(ExitBadArgs, fmt.Errorf("user get command requires user name as its argument"))
  159. }
  160. name := args[0]
  161. client := mustClientFromCmd(cmd)
  162. resp, err := client.Auth.UserGet(context.TODO(), name)
  163. if err != nil {
  164. ExitWithError(ExitError, err)
  165. }
  166. if userShowDetail {
  167. fmt.Printf("User: %s\n", name)
  168. for _, role := range resp.Roles {
  169. fmt.Printf("\n")
  170. roleResp, err := client.Auth.RoleGet(context.TODO(), role)
  171. if err != nil {
  172. ExitWithError(ExitError, err)
  173. }
  174. display.RoleGet(role, *roleResp)
  175. }
  176. } else {
  177. display.UserGet(name, *resp)
  178. }
  179. }
  180. // userListCommandFunc executes the "user list" command.
  181. func userListCommandFunc(cmd *cobra.Command, args []string) {
  182. if len(args) != 0 {
  183. ExitWithError(ExitBadArgs, fmt.Errorf("user list command requires no arguments"))
  184. }
  185. resp, err := mustClientFromCmd(cmd).Auth.UserList(context.TODO())
  186. if err != nil {
  187. ExitWithError(ExitError, err)
  188. }
  189. display.UserList(*resp)
  190. }
  191. // userChangePasswordCommandFunc executes the "user passwd" command.
  192. func userChangePasswordCommandFunc(cmd *cobra.Command, args []string) {
  193. if len(args) != 1 {
  194. ExitWithError(ExitBadArgs, fmt.Errorf("user passwd command requires user name as its argument"))
  195. }
  196. var password string
  197. if !passwordInteractive {
  198. fmt.Scanf("%s", &password)
  199. } else {
  200. password = readPasswordInteractive(args[0])
  201. }
  202. resp, err := mustClientFromCmd(cmd).Auth.UserChangePassword(context.TODO(), args[0], password)
  203. if err != nil {
  204. ExitWithError(ExitError, err)
  205. }
  206. display.UserChangePassword(*resp)
  207. }
  208. // userGrantRoleCommandFunc executes the "user grant-role" command.
  209. func userGrantRoleCommandFunc(cmd *cobra.Command, args []string) {
  210. if len(args) != 2 {
  211. ExitWithError(ExitBadArgs, fmt.Errorf("user grant command requires user name and role name as its argument"))
  212. }
  213. resp, err := mustClientFromCmd(cmd).Auth.UserGrantRole(context.TODO(), args[0], args[1])
  214. if err != nil {
  215. ExitWithError(ExitError, err)
  216. }
  217. display.UserGrantRole(args[0], args[1], *resp)
  218. }
  219. // userRevokeRoleCommandFunc executes the "user revoke-role" command.
  220. func userRevokeRoleCommandFunc(cmd *cobra.Command, args []string) {
  221. if len(args) != 2 {
  222. ExitWithError(ExitBadArgs, fmt.Errorf("user revoke-role requires user name and role name as its argument"))
  223. }
  224. resp, err := mustClientFromCmd(cmd).Auth.UserRevokeRole(context.TODO(), args[0], args[1])
  225. if err != nil {
  226. ExitWithError(ExitError, err)
  227. }
  228. display.UserRevokeRole(args[0], args[1], *resp)
  229. }
  230. func readPasswordInteractive(name string) string {
  231. prompt1 := fmt.Sprintf("Password of %s: ", name)
  232. password1, err1 := speakeasy.Ask(prompt1)
  233. if err1 != nil {
  234. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s", err1))
  235. }
  236. if len(password1) == 0 {
  237. ExitWithError(ExitBadArgs, fmt.Errorf("empty password"))
  238. }
  239. prompt2 := fmt.Sprintf("Type password of %s again for confirmation: ", name)
  240. password2, err2 := speakeasy.Ask(prompt2)
  241. if err2 != nil {
  242. ExitWithError(ExitBadArgs, fmt.Errorf("failed to ask password: %s", err2))
  243. }
  244. if password1 != password2 {
  245. ExitWithError(ExitBadArgs, fmt.Errorf("given passwords are different"))
  246. }
  247. return password1
  248. }