role_command.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "fmt"
  17. "github.com/coreos/etcd/clientv3"
  18. "github.com/spf13/cobra"
  19. "golang.org/x/net/context"
  20. )
  21. var (
  22. rolePermPrefix bool
  23. rolePermFromKey bool
  24. )
  25. // NewRoleCommand returns the cobra command for "role".
  26. func NewRoleCommand() *cobra.Command {
  27. ac := &cobra.Command{
  28. Use: "role <subcommand>",
  29. Short: "Role related commands",
  30. }
  31. ac.AddCommand(newRoleAddCommand())
  32. ac.AddCommand(newRoleDeleteCommand())
  33. ac.AddCommand(newRoleGetCommand())
  34. ac.AddCommand(newRoleListCommand())
  35. ac.AddCommand(newRoleGrantPermissionCommand())
  36. ac.AddCommand(newRoleRevokePermissionCommand())
  37. return ac
  38. }
  39. func newRoleAddCommand() *cobra.Command {
  40. return &cobra.Command{
  41. Use: "add <role name>",
  42. Short: "Adds a new role",
  43. Run: roleAddCommandFunc,
  44. }
  45. }
  46. func newRoleDeleteCommand() *cobra.Command {
  47. return &cobra.Command{
  48. Use: "delete <role name>",
  49. Short: "Deletes a role",
  50. Run: roleDeleteCommandFunc,
  51. }
  52. }
  53. func newRoleGetCommand() *cobra.Command {
  54. return &cobra.Command{
  55. Use: "get <role name>",
  56. Short: "Gets detailed information of a role",
  57. Run: roleGetCommandFunc,
  58. }
  59. }
  60. func newRoleListCommand() *cobra.Command {
  61. return &cobra.Command{
  62. Use: "list",
  63. Short: "Lists all roles",
  64. Run: roleListCommandFunc,
  65. }
  66. }
  67. func newRoleGrantPermissionCommand() *cobra.Command {
  68. cmd := &cobra.Command{
  69. Use: "grant-permission [options] <role name> <permission type> <key> [endkey]",
  70. Short: "Grants a key to a role",
  71. Run: roleGrantPermissionCommandFunc,
  72. }
  73. cmd.Flags().BoolVar(&rolePermPrefix, "prefix", false, "grant a prefix permission")
  74. cmd.Flags().BoolVar(&rolePermFromKey, "from-key", false, "grant a permission of keys that are greater than or equal to the given key using byte compare")
  75. return cmd
  76. }
  77. func newRoleRevokePermissionCommand() *cobra.Command {
  78. cmd := &cobra.Command{
  79. Use: "revoke-permission <role name> <key> [endkey]",
  80. Short: "Revokes a key from a role",
  81. Run: roleRevokePermissionCommandFunc,
  82. }
  83. cmd.Flags().BoolVar(&rolePermPrefix, "prefix", false, "revoke a prefix permission")
  84. cmd.Flags().BoolVar(&rolePermFromKey, "from-key", false, "revoke a permission of keys that are greater than or equal to the given key using byte compare")
  85. return cmd
  86. }
  87. // roleAddCommandFunc executes the "role add" command.
  88. func roleAddCommandFunc(cmd *cobra.Command, args []string) {
  89. if len(args) != 1 {
  90. ExitWithError(ExitBadArgs, fmt.Errorf("role add command requires role name as its argument."))
  91. }
  92. resp, err := mustClientFromCmd(cmd).Auth.RoleAdd(context.TODO(), args[0])
  93. if err != nil {
  94. ExitWithError(ExitError, err)
  95. }
  96. display.RoleAdd(args[0], *resp)
  97. }
  98. // roleDeleteCommandFunc executes the "role delete" command.
  99. func roleDeleteCommandFunc(cmd *cobra.Command, args []string) {
  100. if len(args) != 1 {
  101. ExitWithError(ExitBadArgs, fmt.Errorf("role delete command requires role name as its argument."))
  102. }
  103. resp, err := mustClientFromCmd(cmd).Auth.RoleDelete(context.TODO(), args[0])
  104. if err != nil {
  105. ExitWithError(ExitError, err)
  106. }
  107. display.RoleDelete(args[0], *resp)
  108. }
  109. // roleGetCommandFunc executes the "role get" command.
  110. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  111. if len(args) != 1 {
  112. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  113. }
  114. name := args[0]
  115. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), name)
  116. if err != nil {
  117. ExitWithError(ExitError, err)
  118. }
  119. display.RoleGet(name, *resp)
  120. }
  121. // roleListCommandFunc executes the "role list" command.
  122. func roleListCommandFunc(cmd *cobra.Command, args []string) {
  123. if len(args) != 0 {
  124. ExitWithError(ExitBadArgs, fmt.Errorf("role list command requires no arguments."))
  125. }
  126. resp, err := mustClientFromCmd(cmd).Auth.RoleList(context.TODO())
  127. if err != nil {
  128. ExitWithError(ExitError, err)
  129. }
  130. display.RoleList(*resp)
  131. }
  132. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  133. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  134. if len(args) < 3 {
  135. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key [endkey] as its argument."))
  136. }
  137. perm, err := clientv3.StrToPermissionType(args[1])
  138. if err != nil {
  139. ExitWithError(ExitBadArgs, err)
  140. }
  141. rangeEnd, rerr := rangeEndFromPermFlags(args[2:])
  142. if rerr != nil {
  143. ExitWithError(ExitBadArgs, rerr)
  144. }
  145. resp, err := mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], args[2], rangeEnd, perm)
  146. if err != nil {
  147. ExitWithError(ExitError, err)
  148. }
  149. display.RoleGrantPermission(args[0], *resp)
  150. }
  151. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  152. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  153. if len(args) < 2 {
  154. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key [endkey] as its argument."))
  155. }
  156. rangeEnd, rerr := rangeEndFromPermFlags(args[1:])
  157. if rerr != nil {
  158. ExitWithError(ExitBadArgs, rerr)
  159. }
  160. resp, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1], rangeEnd)
  161. if err != nil {
  162. ExitWithError(ExitError, err)
  163. }
  164. display.RoleRevokePermission(args[0], args[1], rangeEnd, *resp)
  165. }
  166. func rangeEndFromPermFlags(args []string) (string, error) {
  167. if len(args) == 1 {
  168. if rolePermPrefix {
  169. if rolePermFromKey {
  170. return "", fmt.Errorf("--from-key and --prefix flags are mutually exclusive")
  171. }
  172. return clientv3.GetPrefixRangeEnd(args[0]), nil
  173. }
  174. if rolePermFromKey {
  175. return "\x00", nil
  176. }
  177. // single key case
  178. return "", nil
  179. }
  180. if rolePermPrefix {
  181. return "", fmt.Errorf("unexpected endkey argument with --prefix flag")
  182. }
  183. if rolePermFromKey {
  184. return "", fmt.Errorf("unexpected endkey argument with --from-key flag")
  185. }
  186. return args[1], nil
  187. }