role_command.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. return ac
  31. }
  32. func newRoleAddCommand() *cobra.Command {
  33. return &cobra.Command{
  34. Use: "add <role name>",
  35. Short: "add a new role",
  36. Run: roleAddCommandFunc,
  37. }
  38. }
  39. func newRoleGrantCommand() *cobra.Command {
  40. return &cobra.Command{
  41. Use: "grant <role name> <permission type> <key>",
  42. Short: "grant a key to a role",
  43. Run: roleGrantCommandFunc,
  44. }
  45. }
  46. func newRoleGetCommand() *cobra.Command {
  47. return &cobra.Command{
  48. Use: "get <role name>",
  49. Short: "get detailed information of a role",
  50. Run: roleGetCommandFunc,
  51. }
  52. }
  53. // roleAddCommandFunc executes the "role add" command.
  54. func roleAddCommandFunc(cmd *cobra.Command, args []string) {
  55. if len(args) != 1 {
  56. ExitWithError(ExitBadArgs, fmt.Errorf("role add command requires role name as its argument."))
  57. }
  58. _, err := mustClientFromCmd(cmd).Auth.RoleAdd(context.TODO(), args[0])
  59. if err != nil {
  60. ExitWithError(ExitError, err)
  61. }
  62. fmt.Printf("Role %s created\n", args[0])
  63. }
  64. // roleGrantCommandFunc executes the "role grant" command.
  65. func roleGrantCommandFunc(cmd *cobra.Command, args []string) {
  66. if len(args) != 3 {
  67. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key as its argument."))
  68. }
  69. perm, err := clientv3.StrToPermissionType(args[1])
  70. if err != nil {
  71. ExitWithError(ExitBadArgs, err)
  72. }
  73. _, err = mustClientFromCmd(cmd).Auth.RoleGrant(context.TODO(), args[0], args[2], perm)
  74. if err != nil {
  75. ExitWithError(ExitError, err)
  76. }
  77. fmt.Printf("Role %s updated\n", args[0])
  78. }
  79. // roleGetCommandFunc executes the "role get" command.
  80. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  81. if len(args) != 1 {
  82. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  83. }
  84. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), args[0])
  85. if err != nil {
  86. ExitWithError(ExitError, err)
  87. }
  88. fmt.Printf("Role %s\n", args[0])
  89. fmt.Println("KV Read:")
  90. for _, perm := range resp.Perm {
  91. if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
  92. fmt.Printf("\t%s\n", string(perm.Key))
  93. }
  94. }
  95. fmt.Println("KV Write:")
  96. for _, perm := range resp.Perm {
  97. if perm.PermType == clientv3.PermWrite || perm.PermType == clientv3.PermReadWrite {
  98. fmt.Printf("\t%s\n", string(perm.Key))
  99. }
  100. }
  101. }