role_command.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(newRoleGrantCommand())
  29. ac.AddCommand(newRoleGetCommand())
  30. ac.AddCommand(newRoleRevokePermissionCommand())
  31. ac.AddCommand(newRoleDeleteCommand())
  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 newRoleGrantCommand() *cobra.Command {
  42. return &cobra.Command{
  43. Use: "grant <role name> <permission type> <key>",
  44. Short: "grant a key to a role",
  45. Run: roleGrantCommandFunc,
  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 newRoleRevokePermissionCommand() *cobra.Command {
  56. return &cobra.Command{
  57. Use: "revoke-permission <role name> <key>",
  58. Short: "revoke a key from a role",
  59. Run: roleRevokePermissionCommandFunc,
  60. }
  61. }
  62. func newRoleDeleteCommand() *cobra.Command {
  63. return &cobra.Command{
  64. Use: "delete <role name>",
  65. Short: "delete a role",
  66. Run: roleDeleteCommandFunc,
  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. // roleGrantCommandFunc executes the "role grant" command.
  81. func roleGrantCommandFunc(cmd *cobra.Command, args []string) {
  82. if len(args) != 3 {
  83. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key as its argument."))
  84. }
  85. perm, err := clientv3.StrToPermissionType(args[1])
  86. if err != nil {
  87. ExitWithError(ExitBadArgs, err)
  88. }
  89. _, err = mustClientFromCmd(cmd).Auth.RoleGrant(context.TODO(), args[0], args[2], perm)
  90. if err != nil {
  91. ExitWithError(ExitError, err)
  92. }
  93. fmt.Printf("Role %s updated\n", args[0])
  94. }
  95. // roleGetCommandFunc executes the "role get" command.
  96. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  97. if len(args) != 1 {
  98. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  99. }
  100. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), args[0])
  101. if err != nil {
  102. ExitWithError(ExitError, err)
  103. }
  104. fmt.Printf("Role %s\n", args[0])
  105. fmt.Println("KV Read:")
  106. for _, perm := range resp.Perm {
  107. if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
  108. fmt.Printf("\t%s\n", string(perm.Key))
  109. }
  110. }
  111. fmt.Println("KV Write:")
  112. for _, perm := range resp.Perm {
  113. if perm.PermType == clientv3.PermWrite || perm.PermType == clientv3.PermReadWrite {
  114. fmt.Printf("\t%s\n", string(perm.Key))
  115. }
  116. }
  117. }
  118. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  119. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  120. if len(args) != 2 {
  121. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key as its argument."))
  122. }
  123. _, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1])
  124. if err != nil {
  125. ExitWithError(ExitError, err)
  126. }
  127. fmt.Printf("Permission of key %s is revoked from role %s\n", args[1], args[0])
  128. }
  129. // roleDeleteCommandFunc executes the "role delete" command.
  130. func roleDeleteCommandFunc(cmd *cobra.Command, args []string) {
  131. if len(args) != 1 {
  132. ExitWithError(ExitBadArgs, fmt.Errorf("role delete command requires role name as its argument."))
  133. }
  134. _, err := mustClientFromCmd(cmd).Auth.RoleDelete(context.TODO(), args[0])
  135. if err != nil {
  136. ExitWithError(ExitError, err)
  137. }
  138. fmt.Printf("Role %s deleted\n", args[0])
  139. }