role_command.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. grantPermissionPrefix bool
  23. permFromKey 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(&grantPermissionPrefix, "prefix", false, "grant a prefix permission")
  74. cmd.Flags().BoolVar(&permFromKey, "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(&permFromKey, "from-key", false, "grant a permission of keys that are greater than or equal to the given key using byte compare")
  84. return cmd
  85. }
  86. // roleAddCommandFunc executes the "role add" command.
  87. func roleAddCommandFunc(cmd *cobra.Command, args []string) {
  88. if len(args) != 1 {
  89. ExitWithError(ExitBadArgs, fmt.Errorf("role add command requires role name as its argument."))
  90. }
  91. resp, err := mustClientFromCmd(cmd).Auth.RoleAdd(context.TODO(), args[0])
  92. if err != nil {
  93. ExitWithError(ExitError, err)
  94. }
  95. display.RoleAdd(args[0], *resp)
  96. }
  97. // roleDeleteCommandFunc executes the "role delete" command.
  98. func roleDeleteCommandFunc(cmd *cobra.Command, args []string) {
  99. if len(args) != 1 {
  100. ExitWithError(ExitBadArgs, fmt.Errorf("role delete command requires role name as its argument."))
  101. }
  102. resp, err := mustClientFromCmd(cmd).Auth.RoleDelete(context.TODO(), args[0])
  103. if err != nil {
  104. ExitWithError(ExitError, err)
  105. }
  106. display.RoleDelete(args[0], *resp)
  107. }
  108. // roleGetCommandFunc executes the "role get" command.
  109. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  110. if len(args) != 1 {
  111. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  112. }
  113. name := args[0]
  114. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), name)
  115. if err != nil {
  116. ExitWithError(ExitError, err)
  117. }
  118. display.RoleGet(name, *resp)
  119. }
  120. // roleListCommandFunc executes the "role list" command.
  121. func roleListCommandFunc(cmd *cobra.Command, args []string) {
  122. if len(args) != 0 {
  123. ExitWithError(ExitBadArgs, fmt.Errorf("role list command requires no arguments."))
  124. }
  125. resp, err := mustClientFromCmd(cmd).Auth.RoleList(context.TODO())
  126. if err != nil {
  127. ExitWithError(ExitError, err)
  128. }
  129. display.RoleList(*resp)
  130. }
  131. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  132. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  133. if len(args) < 3 {
  134. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key [endkey] as its argument."))
  135. }
  136. perm, err := clientv3.StrToPermissionType(args[1])
  137. if err != nil {
  138. ExitWithError(ExitBadArgs, err)
  139. }
  140. rangeEnd := ""
  141. if 4 <= len(args) {
  142. if grantPermissionPrefix {
  143. ExitWithError(ExitBadArgs, fmt.Errorf("don't pass both of --prefix option and range end to grant permission command"))
  144. }
  145. if permFromKey {
  146. ExitWithError(ExitBadArgs, fmt.Errorf("don't pass both of --from-key option and range end to grant permission command"))
  147. }
  148. rangeEnd = args[3]
  149. } else if grantPermissionPrefix {
  150. if permFromKey {
  151. ExitWithError(ExitBadArgs, fmt.Errorf("don't pass both of --from-key option and --prefix option to grant permission command"))
  152. }
  153. rangeEnd = clientv3.GetPrefixRangeEnd(args[2])
  154. } else if permFromKey {
  155. rangeEnd = "\x00"
  156. }
  157. resp, err := mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], args[2], rangeEnd, perm)
  158. if err != nil {
  159. ExitWithError(ExitError, err)
  160. }
  161. display.RoleGrantPermission(args[0], *resp)
  162. }
  163. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  164. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  165. if len(args) < 2 {
  166. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key [endkey] as its argument."))
  167. }
  168. rangeEnd := ""
  169. if 3 <= len(args) {
  170. rangeEnd = args[2]
  171. } else if permFromKey {
  172. rangeEnd = "\x00"
  173. }
  174. resp, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1], rangeEnd)
  175. if err != nil {
  176. ExitWithError(ExitError, err)
  177. }
  178. display.RoleRevokePermission(args[0], args[1], rangeEnd, *resp)
  179. }