user_commands.go 6.2 KB

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