role_command.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 commands",
  26. }
  27. ac.AddCommand(newRoleAddCommand())
  28. ac.AddCommand(newRoleDeleteCommand())
  29. ac.AddCommand(newRoleGetCommand())
  30. ac.AddCommand(newRoleListCommand())
  31. ac.AddCommand(newRoleGrantPermissionCommand())
  32. ac.AddCommand(newRoleRevokePermissionCommand())
  33. return ac
  34. }
  35. func newRoleAddCommand() *cobra.Command {
  36. return &cobra.Command{
  37. Use: "add <role name>",
  38. Short: "Adds a new role",
  39. Run: roleAddCommandFunc,
  40. }
  41. }
  42. func newRoleDeleteCommand() *cobra.Command {
  43. return &cobra.Command{
  44. Use: "delete <role name>",
  45. Short: "Deletes a role",
  46. Run: roleDeleteCommandFunc,
  47. }
  48. }
  49. func newRoleGetCommand() *cobra.Command {
  50. return &cobra.Command{
  51. Use: "get <role name>",
  52. Short: "Gets detailed information of a role",
  53. Run: roleGetCommandFunc,
  54. }
  55. }
  56. func newRoleListCommand() *cobra.Command {
  57. return &cobra.Command{
  58. Use: "list",
  59. Short: "Lists 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: "Grants 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: "Revokes 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. func printRolePermissions(name string, resp *clientv3.AuthRoleGetResponse) {
  100. fmt.Printf("Role %s\n", name)
  101. fmt.Println("KV Read:")
  102. for _, perm := range resp.Perm {
  103. if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
  104. if len(perm.RangeEnd) == 0 {
  105. fmt.Printf("\t%s\n", string(perm.Key))
  106. } else {
  107. fmt.Printf("\t[%s, %s)\n", string(perm.Key), string(perm.RangeEnd))
  108. }
  109. }
  110. }
  111. fmt.Println("KV Write:")
  112. for _, perm := range resp.Perm {
  113. if perm.PermType == clientv3.PermWrite || perm.PermType == clientv3.PermReadWrite {
  114. if len(perm.RangeEnd) == 0 {
  115. fmt.Printf("\t%s\n", string(perm.Key))
  116. } else {
  117. fmt.Printf("\t[%s, %s)\n", string(perm.Key), string(perm.RangeEnd))
  118. }
  119. }
  120. }
  121. }
  122. // roleGetCommandFunc executes the "role get" command.
  123. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  124. if len(args) != 1 {
  125. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  126. }
  127. name := args[0]
  128. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), name)
  129. if err != nil {
  130. ExitWithError(ExitError, err)
  131. }
  132. printRolePermissions(name, resp)
  133. }
  134. // roleListCommandFunc executes the "role list" command.
  135. func roleListCommandFunc(cmd *cobra.Command, args []string) {
  136. if len(args) != 0 {
  137. ExitWithError(ExitBadArgs, fmt.Errorf("role list command requires no arguments."))
  138. }
  139. resp, err := mustClientFromCmd(cmd).Auth.RoleList(context.TODO())
  140. if err != nil {
  141. ExitWithError(ExitError, err)
  142. }
  143. for _, role := range resp.Roles {
  144. fmt.Printf("%s\n", role)
  145. }
  146. }
  147. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  148. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  149. if len(args) < 3 {
  150. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key [endkey] as its argument."))
  151. }
  152. perm, err := clientv3.StrToPermissionType(args[1])
  153. if err != nil {
  154. ExitWithError(ExitBadArgs, err)
  155. }
  156. rangeEnd := ""
  157. if 4 <= len(args) {
  158. rangeEnd = args[3]
  159. }
  160. _, err = mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], args[2], rangeEnd, perm)
  161. if err != nil {
  162. ExitWithError(ExitError, err)
  163. }
  164. fmt.Printf("Role %s updated\n", args[0])
  165. }
  166. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  167. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  168. if len(args) < 2 {
  169. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key [endkey] as its argument."))
  170. }
  171. rangeEnd := ""
  172. if 3 <= len(args) {
  173. rangeEnd = args[2]
  174. }
  175. _, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1], rangeEnd)
  176. if err != nil {
  177. ExitWithError(ExitError, err)
  178. }
  179. if len(rangeEnd) == 0 {
  180. fmt.Printf("Permission of key %s is revoked from role %s\n", args[1], args[0])
  181. } else {
  182. fmt.Printf("Permission of range [%s, %s) is revoked from role %s\n", args[1], rangeEnd, args[0])
  183. }
  184. }