role_command.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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(newRoleRevokePermissionCommand())
  31. ac.AddCommand(newRoleGrantPermissionCommand())
  32. return ac
  33. }
  34. func newRoleAddCommand() *cobra.Command {
  35. return &cobra.Command{
  36. Use: "add <role name>",
  37. Short: "add a new role",
  38. Run: roleAddCommandFunc,
  39. }
  40. }
  41. func newRoleDeleteCommand() *cobra.Command {
  42. return &cobra.Command{
  43. Use: "delete <role name>",
  44. Short: "delete a role",
  45. Run: roleDeleteCommandFunc,
  46. }
  47. }
  48. func newRoleGetCommand() *cobra.Command {
  49. return &cobra.Command{
  50. Use: "get <role name>",
  51. Short: "get detailed information of a role",
  52. Run: roleGetCommandFunc,
  53. }
  54. }
  55. func newRoleGrantPermissionCommand() *cobra.Command {
  56. return &cobra.Command{
  57. Use: "grant-permission <role name> <permission type> <key> [endkey]",
  58. Short: "grant a key to a role",
  59. Run: roleGrantPermissionCommandFunc,
  60. }
  61. }
  62. func newRoleRevokePermissionCommand() *cobra.Command {
  63. return &cobra.Command{
  64. Use: "revoke-permission <role name> <key> [endkey]",
  65. Short: "revoke a key from a role",
  66. Run: roleRevokePermissionCommandFunc,
  67. }
  68. }
  69. // roleAddCommandFunc executes the "role add" command.
  70. func roleAddCommandFunc(cmd *cobra.Command, args []string) {
  71. if len(args) != 1 {
  72. ExitWithError(ExitBadArgs, fmt.Errorf("role add command requires role name as its argument."))
  73. }
  74. _, err := mustClientFromCmd(cmd).Auth.RoleAdd(context.TODO(), args[0])
  75. if err != nil {
  76. ExitWithError(ExitError, err)
  77. }
  78. fmt.Printf("Role %s created\n", args[0])
  79. }
  80. // roleDeleteCommandFunc executes the "role delete" command.
  81. func roleDeleteCommandFunc(cmd *cobra.Command, args []string) {
  82. if len(args) != 1 {
  83. ExitWithError(ExitBadArgs, fmt.Errorf("role delete command requires role name as its argument."))
  84. }
  85. _, err := mustClientFromCmd(cmd).Auth.RoleDelete(context.TODO(), args[0])
  86. if err != nil {
  87. ExitWithError(ExitError, err)
  88. }
  89. fmt.Printf("Role %s deleted\n", args[0])
  90. }
  91. // roleGetCommandFunc executes the "role get" command.
  92. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  93. if len(args) != 1 {
  94. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  95. }
  96. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), args[0])
  97. if err != nil {
  98. ExitWithError(ExitError, err)
  99. }
  100. fmt.Printf("Role %s\n", args[0])
  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. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  123. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  124. if len(args) < 3 {
  125. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key [endkey] as its argument."))
  126. }
  127. perm, err := clientv3.StrToPermissionType(args[1])
  128. if err != nil {
  129. ExitWithError(ExitBadArgs, err)
  130. }
  131. rangeEnd := ""
  132. if 4 <= len(args) {
  133. rangeEnd = args[3]
  134. }
  135. _, err = mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], args[2], rangeEnd, perm)
  136. if err != nil {
  137. ExitWithError(ExitError, err)
  138. }
  139. fmt.Printf("Role %s updated\n", args[0])
  140. }
  141. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  142. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  143. if len(args) < 2 {
  144. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key [endkey] as its argument."))
  145. }
  146. rangeEnd := ""
  147. if 3 <= len(args) {
  148. rangeEnd = args[2]
  149. }
  150. _, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1], rangeEnd)
  151. if err != nil {
  152. ExitWithError(ExitError, err)
  153. }
  154. fmt.Printf("Permission of key %s is revoked from role %s\n", args[1], args[0])
  155. }