role_command.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <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. _, err := mustClientFromCmd(cmd).Auth.RoleAdd(context.TODO(), args[0])
  88. if err != nil {
  89. ExitWithError(ExitError, err)
  90. }
  91. fmt.Printf("Role %s created\n", args[0])
  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. _, err := mustClientFromCmd(cmd).Auth.RoleDelete(context.TODO(), args[0])
  99. if err != nil {
  100. ExitWithError(ExitError, err)
  101. }
  102. fmt.Printf("Role %s deleted\n", args[0])
  103. }
  104. func printRolePermissions(name string, resp *clientv3.AuthRoleGetResponse) {
  105. fmt.Printf("Role %s\n", name)
  106. fmt.Println("KV Read:")
  107. for _, perm := range resp.Perm {
  108. if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
  109. if len(perm.RangeEnd) == 0 {
  110. fmt.Printf("\t%s\n", string(perm.Key))
  111. } else {
  112. fmt.Printf("\t[%s, %s)\n", string(perm.Key), string(perm.RangeEnd))
  113. }
  114. }
  115. }
  116. fmt.Println("KV Write:")
  117. for _, perm := range resp.Perm {
  118. if perm.PermType == clientv3.PermWrite || perm.PermType == clientv3.PermReadWrite {
  119. if len(perm.RangeEnd) == 0 {
  120. fmt.Printf("\t%s\n", string(perm.Key))
  121. } else {
  122. fmt.Printf("\t[%s, %s)\n", string(perm.Key), string(perm.RangeEnd))
  123. }
  124. }
  125. }
  126. }
  127. // roleGetCommandFunc executes the "role get" command.
  128. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  129. if len(args) != 1 {
  130. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  131. }
  132. name := args[0]
  133. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), name)
  134. if err != nil {
  135. ExitWithError(ExitError, err)
  136. }
  137. printRolePermissions(name, resp)
  138. }
  139. // roleListCommandFunc executes the "role list" command.
  140. func roleListCommandFunc(cmd *cobra.Command, args []string) {
  141. if len(args) != 0 {
  142. ExitWithError(ExitBadArgs, fmt.Errorf("role list command requires no arguments."))
  143. }
  144. resp, err := mustClientFromCmd(cmd).Auth.RoleList(context.TODO())
  145. if err != nil {
  146. ExitWithError(ExitError, err)
  147. }
  148. for _, role := range resp.Roles {
  149. fmt.Printf("%s\n", role)
  150. }
  151. }
  152. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  153. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  154. if len(args) < 3 {
  155. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key [endkey] as its argument."))
  156. }
  157. perm, err := clientv3.StrToPermissionType(args[1])
  158. if err != nil {
  159. ExitWithError(ExitBadArgs, err)
  160. }
  161. rangeEnd := ""
  162. if 4 <= len(args) {
  163. if grantPermissionPrefix {
  164. ExitWithError(ExitBadArgs, fmt.Errorf("don't pass both of --prefix option and range end to grant permission command"))
  165. }
  166. rangeEnd = args[3]
  167. } else if grantPermissionPrefix {
  168. rangeEnd = clientv3.GetPrefixRangeEnd(args[2])
  169. }
  170. _, err = mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], args[2], rangeEnd, perm)
  171. if err != nil {
  172. ExitWithError(ExitError, err)
  173. }
  174. fmt.Printf("Role %s updated\n", args[0])
  175. }
  176. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  177. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  178. if len(args) < 2 {
  179. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key [endkey] as its argument."))
  180. }
  181. rangeEnd := ""
  182. if 3 <= len(args) {
  183. rangeEnd = args[2]
  184. }
  185. _, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1], rangeEnd)
  186. if err != nil {
  187. ExitWithError(ExitError, err)
  188. }
  189. if len(rangeEnd) == 0 {
  190. fmt.Printf("Permission of key %s is revoked from role %s\n", args[1], args[0])
  191. } else {
  192. fmt.Printf("Permission of range [%s, %s) is revoked from role %s\n", args[1], rangeEnd, args[0])
  193. }
  194. }