role_command.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. "context"
  17. "fmt"
  18. "github.com/spf13/cobra"
  19. "go.etcd.io/etcd/clientv3"
  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. key, rangeEnd := permRange(args[2:])
  142. resp, err := mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], key, rangeEnd, perm)
  143. if err != nil {
  144. ExitWithError(ExitError, err)
  145. }
  146. display.RoleGrantPermission(args[0], *resp)
  147. }
  148. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  149. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  150. if len(args) < 2 {
  151. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key [endkey] as its argument"))
  152. }
  153. key, rangeEnd := permRange(args[1:])
  154. resp, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], key, rangeEnd)
  155. if err != nil {
  156. ExitWithError(ExitError, err)
  157. }
  158. display.RoleRevokePermission(args[0], args[1], rangeEnd, *resp)
  159. }
  160. func permRange(args []string) (string, string) {
  161. key := args[0]
  162. var rangeEnd string
  163. if len(key) == 0 {
  164. if rolePermPrefix && rolePermFromKey {
  165. ExitWithError(ExitBadArgs, fmt.Errorf("--from-key and --prefix flags are mutually exclusive"))
  166. }
  167. // Range permission is expressed as adt.BytesAffineInterval,
  168. // so the empty prefix which should be matched with every key must be like this ["\x00", <end>).
  169. key = "\x00"
  170. if rolePermPrefix || rolePermFromKey {
  171. // For the both cases of prefix and from-key, a permission with an empty key
  172. // should allow access to the entire key space.
  173. // 0x00 will be treated as open ended in server side.
  174. rangeEnd = "\x00"
  175. }
  176. } else {
  177. var err error
  178. rangeEnd, err = rangeEndFromPermFlags(args[0:])
  179. if err != nil {
  180. ExitWithError(ExitBadArgs, err)
  181. }
  182. }
  183. return key, rangeEnd
  184. }
  185. func rangeEndFromPermFlags(args []string) (string, error) {
  186. if len(args) == 1 {
  187. if rolePermPrefix {
  188. if rolePermFromKey {
  189. return "", fmt.Errorf("--from-key and --prefix flags are mutually exclusive")
  190. }
  191. return clientv3.GetPrefixRangeEnd(args[0]), nil
  192. }
  193. if rolePermFromKey {
  194. return "\x00", nil
  195. }
  196. // single key case
  197. return "", nil
  198. }
  199. if rolePermPrefix {
  200. return "", fmt.Errorf("unexpected endkey argument with --prefix flag")
  201. }
  202. if rolePermFromKey {
  203. return "", fmt.Errorf("unexpected endkey argument with --from-key flag")
  204. }
  205. return args[1], nil
  206. }