user_commands.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. 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, user := mustUserAPIAndName(c)
  103. ctx, cancel := contextWithTotalTimeout(c)
  104. defer cancel()
  105. currentUser, err := api.GetUser(ctx, user)
  106. if currentUser != nil {
  107. fmt.Fprintf(os.Stderr, "User %s already exists\n", user)
  108. os.Exit(1)
  109. }
  110. pass, err := speakeasy.Ask("New password: ")
  111. if err != nil {
  112. fmt.Fprintln(os.Stderr, "Error reading password:", err)
  113. os.Exit(1)
  114. }
  115. err = api.AddUser(ctx, user, pass)
  116. if err != nil {
  117. fmt.Fprintln(os.Stderr, err.Error())
  118. os.Exit(1)
  119. }
  120. fmt.Printf("User %s created\n", user)
  121. }
  122. func actionUserRemove(c *cli.Context) {
  123. api, user := mustUserAPIAndName(c)
  124. ctx, cancel := contextWithTotalTimeout(c)
  125. err := api.RemoveUser(ctx, user)
  126. cancel()
  127. if err != nil {
  128. fmt.Fprintln(os.Stderr, err.Error())
  129. os.Exit(1)
  130. }
  131. fmt.Printf("User %s removed\n", user)
  132. }
  133. func actionUserPasswd(c *cli.Context) {
  134. api, user := mustUserAPIAndName(c)
  135. ctx, cancel := contextWithTotalTimeout(c)
  136. defer cancel()
  137. currentUser, err := api.GetUser(ctx, user)
  138. if currentUser == nil {
  139. fmt.Fprintln(os.Stderr, err.Error())
  140. os.Exit(1)
  141. }
  142. pass, err := speakeasy.Ask("New password: ")
  143. if err != nil {
  144. fmt.Fprintln(os.Stderr, "Error reading password:", err)
  145. os.Exit(1)
  146. }
  147. _, err = api.ChangePassword(ctx, user, pass)
  148. if err != nil {
  149. fmt.Fprintln(os.Stderr, err.Error())
  150. os.Exit(1)
  151. }
  152. fmt.Printf("Password updated\n")
  153. }
  154. func actionUserGrant(c *cli.Context) {
  155. userGrantRevoke(c, true)
  156. }
  157. func actionUserRevoke(c *cli.Context) {
  158. userGrantRevoke(c, false)
  159. }
  160. func userGrantRevoke(c *cli.Context, grant bool) {
  161. roles := c.StringSlice("roles")
  162. if len(roles) == 0 {
  163. fmt.Fprintln(os.Stderr, "No roles specified; please use `-roles`")
  164. os.Exit(1)
  165. }
  166. ctx, cancel := contextWithTotalTimeout(c)
  167. defer cancel()
  168. api, user := mustUserAPIAndName(c)
  169. currentUser, err := api.GetUser(ctx, user)
  170. if currentUser == nil {
  171. fmt.Fprintln(os.Stderr, err.Error())
  172. os.Exit(1)
  173. }
  174. var newUser *client.User
  175. if grant {
  176. newUser, err = api.GrantUser(ctx, user, roles)
  177. } else {
  178. newUser, err = api.RevokeUser(ctx, user, roles)
  179. }
  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 := contextWithTotalTimeout(c)
  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. }