user_commands.go 5.6 KB

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