user_commands.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // Copyright 2015 CoreOS, Inc.
  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. "os"
  18. "reflect"
  19. "sort"
  20. "strings"
  21. "github.com/bgentry/speakeasy"
  22. "github.com/codegangsta/cli"
  23. "github.com/coreos/etcd/client"
  24. )
  25. func NewUserCommands() cli.Command {
  26. return cli.Command{
  27. Name: "user",
  28. Usage: "user add, grant and revoke subcommands",
  29. Subcommands: []cli.Command{
  30. {
  31. Name: "add",
  32. Usage: "add a new user for the etcd cluster",
  33. ArgsUsage: "<user>",
  34. Action: actionUserAdd,
  35. },
  36. {
  37. Name: "get",
  38. Usage: "get details for a user",
  39. ArgsUsage: "<user>",
  40. Action: actionUserGet,
  41. },
  42. {
  43. Name: "list",
  44. Usage: "list all current users",
  45. ArgsUsage: "<user>",
  46. Action: actionUserList,
  47. },
  48. {
  49. Name: "remove",
  50. Usage: "remove a user for the etcd cluster",
  51. ArgsUsage: "<user>",
  52. Action: actionUserRemove,
  53. },
  54. {
  55. Name: "grant",
  56. Usage: "grant roles to an etcd user",
  57. ArgsUsage: "<user>",
  58. Flags: []cli.Flag{cli.StringSliceFlag{Name: "roles", Value: new(cli.StringSlice), Usage: "List of roles to grant or revoke"}},
  59. Action: actionUserGrant,
  60. },
  61. {
  62. Name: "revoke",
  63. Usage: "revoke roles for an etcd user",
  64. ArgsUsage: "<user>",
  65. Flags: []cli.Flag{cli.StringSliceFlag{Name: "roles", Value: new(cli.StringSlice), Usage: "List of roles to grant or revoke"}},
  66. Action: actionUserRevoke,
  67. },
  68. {
  69. Name: "passwd",
  70. Usage: "change password for a user",
  71. ArgsUsage: "<user>",
  72. Action: actionUserPasswd,
  73. },
  74. },
  75. }
  76. }
  77. func mustNewAuthUserAPI(c *cli.Context) client.AuthUserAPI {
  78. hc := mustNewClient(c)
  79. if c.GlobalBool("debug") {
  80. fmt.Fprintf(os.Stderr, "Cluster-Endpoints: %s\n", strings.Join(hc.Endpoints(), ", "))
  81. }
  82. return client.NewAuthUserAPI(hc)
  83. }
  84. func actionUserList(c *cli.Context) {
  85. if len(c.Args()) != 0 {
  86. fmt.Fprintln(os.Stderr, "No arguments accepted")
  87. os.Exit(1)
  88. }
  89. u := mustNewAuthUserAPI(c)
  90. ctx, cancel := contextWithTotalTimeout(c)
  91. users, err := u.ListUsers(ctx)
  92. cancel()
  93. if err != nil {
  94. fmt.Fprintln(os.Stderr, err.Error())
  95. os.Exit(1)
  96. }
  97. for _, user := range users {
  98. fmt.Printf("%s\n", user)
  99. }
  100. }
  101. func actionUserAdd(c *cli.Context) {
  102. api, userarg := mustUserAPIAndName(c)
  103. ctx, cancel := contextWithTotalTimeout(c)
  104. defer cancel()
  105. user, _, _ := getUsernamePassword("", userarg+":")
  106. currentUser, err := api.GetUser(ctx, user)
  107. if currentUser != nil {
  108. fmt.Fprintf(os.Stderr, "User %s already exists\n", user)
  109. os.Exit(1)
  110. }
  111. _, pass, err := getUsernamePassword("New password: ", userarg)
  112. if err != nil {
  113. fmt.Fprintln(os.Stderr, "Error reading password:", err)
  114. os.Exit(1)
  115. }
  116. err = api.AddUser(ctx, user, pass)
  117. if err != nil {
  118. fmt.Fprintln(os.Stderr, err.Error())
  119. os.Exit(1)
  120. }
  121. fmt.Printf("User %s created\n", user)
  122. }
  123. func actionUserRemove(c *cli.Context) {
  124. api, user := mustUserAPIAndName(c)
  125. ctx, cancel := contextWithTotalTimeout(c)
  126. err := api.RemoveUser(ctx, user)
  127. cancel()
  128. if err != nil {
  129. fmt.Fprintln(os.Stderr, err.Error())
  130. os.Exit(1)
  131. }
  132. fmt.Printf("User %s removed\n", user)
  133. }
  134. func actionUserPasswd(c *cli.Context) {
  135. api, user := mustUserAPIAndName(c)
  136. ctx, cancel := contextWithTotalTimeout(c)
  137. defer cancel()
  138. currentUser, err := api.GetUser(ctx, user)
  139. if currentUser == nil {
  140. fmt.Fprintln(os.Stderr, err.Error())
  141. os.Exit(1)
  142. }
  143. pass, err := speakeasy.Ask("New password: ")
  144. if err != nil {
  145. fmt.Fprintln(os.Stderr, "Error reading password:", err)
  146. os.Exit(1)
  147. }
  148. _, err = api.ChangePassword(ctx, user, pass)
  149. if err != nil {
  150. fmt.Fprintln(os.Stderr, err.Error())
  151. os.Exit(1)
  152. }
  153. fmt.Printf("Password updated\n")
  154. }
  155. func actionUserGrant(c *cli.Context) {
  156. userGrantRevoke(c, true)
  157. }
  158. func actionUserRevoke(c *cli.Context) {
  159. userGrantRevoke(c, false)
  160. }
  161. func userGrantRevoke(c *cli.Context, grant bool) {
  162. roles := c.StringSlice("roles")
  163. if len(roles) == 0 {
  164. fmt.Fprintln(os.Stderr, "No roles specified; please use `--roles`")
  165. os.Exit(1)
  166. }
  167. ctx, cancel := contextWithTotalTimeout(c)
  168. defer cancel()
  169. api, user := mustUserAPIAndName(c)
  170. currentUser, err := api.GetUser(ctx, user)
  171. if currentUser == nil {
  172. fmt.Fprintln(os.Stderr, err.Error())
  173. os.Exit(1)
  174. }
  175. var newUser *client.User
  176. if grant {
  177. newUser, err = api.GrantUser(ctx, user, roles)
  178. } else {
  179. newUser, err = api.RevokeUser(ctx, user, roles)
  180. }
  181. sort.Strings(newUser.Roles)
  182. sort.Strings(currentUser.Roles)
  183. if reflect.DeepEqual(newUser.Roles, currentUser.Roles) {
  184. if grant {
  185. fmt.Printf("User unchanged; roles already granted")
  186. } else {
  187. fmt.Printf("User unchanged; roles already revoked")
  188. }
  189. }
  190. if err != nil {
  191. fmt.Fprintln(os.Stderr, err.Error())
  192. os.Exit(1)
  193. }
  194. fmt.Printf("User %s updated\n", user)
  195. }
  196. func actionUserGet(c *cli.Context) {
  197. api, username := mustUserAPIAndName(c)
  198. ctx, cancel := contextWithTotalTimeout(c)
  199. user, err := api.GetUser(ctx, username)
  200. cancel()
  201. if err != nil {
  202. fmt.Fprintln(os.Stderr, err.Error())
  203. os.Exit(1)
  204. }
  205. fmt.Printf("User: %s\n", user.User)
  206. fmt.Printf("Roles: %s\n", strings.Join(user.Roles, " "))
  207. }
  208. func mustUserAPIAndName(c *cli.Context) (client.AuthUserAPI, string) {
  209. args := c.Args()
  210. if len(args) != 1 {
  211. fmt.Fprintln(os.Stderr, "Please provide a username")
  212. os.Exit(1)
  213. }
  214. api := mustNewAuthUserAPI(c)
  215. username := args[0]
  216. return api, username
  217. }