role_command.go 4.7 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(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>",
  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>",
  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. fmt.Printf("\t%s\n", string(perm.Key))
  105. }
  106. }
  107. fmt.Println("KV Write:")
  108. for _, perm := range resp.Perm {
  109. if perm.PermType == clientv3.PermWrite || perm.PermType == clientv3.PermReadWrite {
  110. fmt.Printf("\t%s\n", string(perm.Key))
  111. }
  112. }
  113. }
  114. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  115. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  116. if len(args) != 3 {
  117. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key as its argument."))
  118. }
  119. perm, err := clientv3.StrToPermissionType(args[1])
  120. if err != nil {
  121. ExitWithError(ExitBadArgs, err)
  122. }
  123. _, err = mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], args[2], perm)
  124. if err != nil {
  125. ExitWithError(ExitError, err)
  126. }
  127. fmt.Printf("Role %s updated\n", args[0])
  128. }
  129. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  130. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  131. if len(args) != 2 {
  132. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key as its argument."))
  133. }
  134. _, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1])
  135. if err != nil {
  136. ExitWithError(ExitError, err)
  137. }
  138. fmt.Printf("Permission of key %s is revoked from role %s\n", args[1], args[0])
  139. }