role_command.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. // NewRoleCommand returns the cobra command for "role".
  22. func NewRoleCommand() *cobra.Command {
  23. ac := &cobra.Command{
  24. Use: "role <subcommand>",
  25. Short: "role related command",
  26. }
  27. ac.AddCommand(newRoleAddCommand())
  28. ac.AddCommand(newRoleDeleteCommand())
  29. ac.AddCommand(newRoleGetCommand())
  30. ac.AddCommand(newRoleListCommand())
  31. ac.AddCommand(newRoleRevokePermissionCommand())
  32. ac.AddCommand(newRoleGrantPermissionCommand())
  33. return ac
  34. }
  35. func newRoleAddCommand() *cobra.Command {
  36. return &cobra.Command{
  37. Use: "add <role name>",
  38. Short: "add a new role",
  39. Run: roleAddCommandFunc,
  40. }
  41. }
  42. func newRoleDeleteCommand() *cobra.Command {
  43. return &cobra.Command{
  44. Use: "delete <role name>",
  45. Short: "delete a role",
  46. Run: roleDeleteCommandFunc,
  47. }
  48. }
  49. func newRoleGetCommand() *cobra.Command {
  50. return &cobra.Command{
  51. Use: "get <role name>",
  52. Short: "get detailed information of a role",
  53. Run: roleGetCommandFunc,
  54. }
  55. }
  56. func newRoleListCommand() *cobra.Command {
  57. return &cobra.Command{
  58. Use: "list",
  59. Short: "list up all roles",
  60. Run: roleListCommandFunc,
  61. }
  62. }
  63. func newRoleGrantPermissionCommand() *cobra.Command {
  64. return &cobra.Command{
  65. Use: "grant-permission <role name> <permission type> <key> [endkey]",
  66. Short: "grant a key to a role",
  67. Run: roleGrantPermissionCommandFunc,
  68. }
  69. }
  70. func newRoleRevokePermissionCommand() *cobra.Command {
  71. return &cobra.Command{
  72. Use: "revoke-permission <role name> <key> [endkey]",
  73. Short: "revoke a key from a role",
  74. Run: roleRevokePermissionCommandFunc,
  75. }
  76. }
  77. // roleAddCommandFunc executes the "role add" command.
  78. func roleAddCommandFunc(cmd *cobra.Command, args []string) {
  79. if len(args) != 1 {
  80. ExitWithError(ExitBadArgs, fmt.Errorf("role add command requires role name as its argument."))
  81. }
  82. _, err := mustClientFromCmd(cmd).Auth.RoleAdd(context.TODO(), args[0])
  83. if err != nil {
  84. ExitWithError(ExitError, err)
  85. }
  86. fmt.Printf("Role %s created\n", args[0])
  87. }
  88. // roleDeleteCommandFunc executes the "role delete" command.
  89. func roleDeleteCommandFunc(cmd *cobra.Command, args []string) {
  90. if len(args) != 1 {
  91. ExitWithError(ExitBadArgs, fmt.Errorf("role delete command requires role name as its argument."))
  92. }
  93. _, err := mustClientFromCmd(cmd).Auth.RoleDelete(context.TODO(), args[0])
  94. if err != nil {
  95. ExitWithError(ExitError, err)
  96. }
  97. fmt.Printf("Role %s deleted\n", args[0])
  98. }
  99. // roleGetCommandFunc executes the "role get" command.
  100. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  101. if len(args) != 1 {
  102. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  103. }
  104. name := args[0]
  105. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), name)
  106. if err != nil {
  107. ExitWithError(ExitError, err)
  108. }
  109. fmt.Printf("Role %s\n", name)
  110. fmt.Println("KV Read:")
  111. for _, perm := range resp.Perm {
  112. if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
  113. if len(perm.RangeEnd) == 0 {
  114. fmt.Printf("\t%s\n", string(perm.Key))
  115. } else {
  116. fmt.Printf("\t[%s, %s)\n", string(perm.Key), string(perm.RangeEnd))
  117. }
  118. }
  119. }
  120. fmt.Println("KV Write:")
  121. for _, perm := range resp.Perm {
  122. if perm.PermType == clientv3.PermWrite || perm.PermType == clientv3.PermReadWrite {
  123. if len(perm.RangeEnd) == 0 {
  124. fmt.Printf("\t%s\n", string(perm.Key))
  125. } else {
  126. fmt.Printf("\t[%s, %s)\n", string(perm.Key), string(perm.RangeEnd))
  127. }
  128. }
  129. }
  130. }
  131. // roleListCommandFunc executes the "role list" command.
  132. func roleListCommandFunc(cmd *cobra.Command, args []string) {
  133. if len(args) != 0 {
  134. ExitWithError(ExitBadArgs, fmt.Errorf("role list command requires no arguments."))
  135. }
  136. resp, err := mustClientFromCmd(cmd).Auth.RoleList(context.TODO())
  137. if err != nil {
  138. ExitWithError(ExitError, err)
  139. }
  140. for _, role := range resp.Roles {
  141. fmt.Printf("%s\n", role)
  142. }
  143. }
  144. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  145. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  146. if len(args) < 3 {
  147. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key [endkey] as its argument."))
  148. }
  149. perm, err := clientv3.StrToPermissionType(args[1])
  150. if err != nil {
  151. ExitWithError(ExitBadArgs, err)
  152. }
  153. rangeEnd := ""
  154. if 4 <= len(args) {
  155. rangeEnd = args[3]
  156. }
  157. _, err = mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], args[2], rangeEnd, perm)
  158. if err != nil {
  159. ExitWithError(ExitError, err)
  160. }
  161. fmt.Printf("Role %s updated\n", args[0])
  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. }
  172. _, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1], rangeEnd)
  173. if err != nil {
  174. ExitWithError(ExitError, err)
  175. }
  176. if len(rangeEnd) == 0 {
  177. fmt.Printf("Permission of key %s is revoked from role %s\n", args[1], args[0])
  178. } else {
  179. fmt.Printf("Permission of range [%s, %s) is revoked from role %s\n", args[1], rangeEnd, args[0])
  180. }
  181. }