role_command.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. )
  24. // NewRoleCommand returns the cobra command for "role".
  25. func NewRoleCommand() *cobra.Command {
  26. ac := &cobra.Command{
  27. Use: "role <subcommand>",
  28. Short: "Role related commands",
  29. }
  30. ac.AddCommand(newRoleAddCommand())
  31. ac.AddCommand(newRoleDeleteCommand())
  32. ac.AddCommand(newRoleGetCommand())
  33. ac.AddCommand(newRoleListCommand())
  34. ac.AddCommand(newRoleGrantPermissionCommand())
  35. ac.AddCommand(newRoleRevokePermissionCommand())
  36. return ac
  37. }
  38. func newRoleAddCommand() *cobra.Command {
  39. return &cobra.Command{
  40. Use: "add <role name>",
  41. Short: "Adds a new role",
  42. Run: roleAddCommandFunc,
  43. }
  44. }
  45. func newRoleDeleteCommand() *cobra.Command {
  46. return &cobra.Command{
  47. Use: "delete <role name>",
  48. Short: "Deletes a role",
  49. Run: roleDeleteCommandFunc,
  50. }
  51. }
  52. func newRoleGetCommand() *cobra.Command {
  53. return &cobra.Command{
  54. Use: "get <role name>",
  55. Short: "Gets detailed information of a role",
  56. Run: roleGetCommandFunc,
  57. }
  58. }
  59. func newRoleListCommand() *cobra.Command {
  60. return &cobra.Command{
  61. Use: "list",
  62. Short: "Lists all roles",
  63. Run: roleListCommandFunc,
  64. }
  65. }
  66. func newRoleGrantPermissionCommand() *cobra.Command {
  67. cmd := &cobra.Command{
  68. Use: "grant-permission [options] <role name> <permission type> <key> [endkey]",
  69. Short: "Grants a key to a role",
  70. Run: roleGrantPermissionCommandFunc,
  71. }
  72. cmd.Flags().BoolVar(&grantPermissionPrefix, "prefix", false, "grant a prefix permission")
  73. return cmd
  74. }
  75. func newRoleRevokePermissionCommand() *cobra.Command {
  76. return &cobra.Command{
  77. Use: "revoke-permission <role name> <key> [endkey]",
  78. Short: "Revokes a key from a role",
  79. Run: roleRevokePermissionCommandFunc,
  80. }
  81. }
  82. // roleAddCommandFunc executes the "role add" command.
  83. func roleAddCommandFunc(cmd *cobra.Command, args []string) {
  84. if len(args) != 1 {
  85. ExitWithError(ExitBadArgs, fmt.Errorf("role add command requires role name as its argument."))
  86. }
  87. resp, err := mustClientFromCmd(cmd).Auth.RoleAdd(context.TODO(), args[0])
  88. if err != nil {
  89. ExitWithError(ExitError, err)
  90. }
  91. display.RoleAdd(args[0], *resp)
  92. }
  93. // roleDeleteCommandFunc executes the "role delete" command.
  94. func roleDeleteCommandFunc(cmd *cobra.Command, args []string) {
  95. if len(args) != 1 {
  96. ExitWithError(ExitBadArgs, fmt.Errorf("role delete command requires role name as its argument."))
  97. }
  98. resp, err := mustClientFromCmd(cmd).Auth.RoleDelete(context.TODO(), args[0])
  99. if err != nil {
  100. ExitWithError(ExitError, err)
  101. }
  102. display.RoleDelete(args[0], *resp)
  103. }
  104. // roleGetCommandFunc executes the "role get" command.
  105. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  106. if len(args) != 1 {
  107. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  108. }
  109. name := args[0]
  110. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), name)
  111. if err != nil {
  112. ExitWithError(ExitError, err)
  113. }
  114. display.RoleGet(name, *resp)
  115. }
  116. // roleListCommandFunc executes the "role list" command.
  117. func roleListCommandFunc(cmd *cobra.Command, args []string) {
  118. if len(args) != 0 {
  119. ExitWithError(ExitBadArgs, fmt.Errorf("role list command requires no arguments."))
  120. }
  121. resp, err := mustClientFromCmd(cmd).Auth.RoleList(context.TODO())
  122. if err != nil {
  123. ExitWithError(ExitError, err)
  124. }
  125. display.RoleList(*resp)
  126. }
  127. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  128. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  129. if len(args) < 3 {
  130. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key [endkey] as its argument."))
  131. }
  132. perm, err := clientv3.StrToPermissionType(args[1])
  133. if err != nil {
  134. ExitWithError(ExitBadArgs, err)
  135. }
  136. rangeEnd := ""
  137. if 4 <= len(args) {
  138. if grantPermissionPrefix {
  139. ExitWithError(ExitBadArgs, fmt.Errorf("don't pass both of --prefix option and range end to grant permission command"))
  140. }
  141. rangeEnd = args[3]
  142. } else if grantPermissionPrefix {
  143. rangeEnd = clientv3.GetPrefixRangeEnd(args[2])
  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 := ""
  157. if 3 <= len(args) {
  158. rangeEnd = args[2]
  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. }