role_command.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "strings"
  18. "github.com/coreos/etcd/clientv3"
  19. "github.com/spf13/cobra"
  20. "golang.org/x/net/context"
  21. )
  22. var (
  23. grantPermissionPrefix bool
  24. )
  25. // NewRoleCommand returns the cobra command for "role".
  26. func NewRoleCommand() *cobra.Command {
  27. ac := &cobra.Command{
  28. Use: "role <subcommand>",
  29. Short: "Role related commands",
  30. }
  31. ac.AddCommand(newRoleAddCommand())
  32. ac.AddCommand(newRoleDeleteCommand())
  33. ac.AddCommand(newRoleGetCommand())
  34. ac.AddCommand(newRoleListCommand())
  35. ac.AddCommand(newRoleGrantPermissionCommand())
  36. ac.AddCommand(newRoleRevokePermissionCommand())
  37. return ac
  38. }
  39. func newRoleAddCommand() *cobra.Command {
  40. return &cobra.Command{
  41. Use: "add <role name>",
  42. Short: "Adds a new role",
  43. Run: roleAddCommandFunc,
  44. }
  45. }
  46. func newRoleDeleteCommand() *cobra.Command {
  47. return &cobra.Command{
  48. Use: "delete <role name>",
  49. Short: "Deletes a role",
  50. Run: roleDeleteCommandFunc,
  51. }
  52. }
  53. func newRoleGetCommand() *cobra.Command {
  54. return &cobra.Command{
  55. Use: "get <role name>",
  56. Short: "Gets detailed information of a role",
  57. Run: roleGetCommandFunc,
  58. }
  59. }
  60. func newRoleListCommand() *cobra.Command {
  61. return &cobra.Command{
  62. Use: "list",
  63. Short: "Lists all roles",
  64. Run: roleListCommandFunc,
  65. }
  66. }
  67. func newRoleGrantPermissionCommand() *cobra.Command {
  68. cmd := &cobra.Command{
  69. Use: "grant-permission <role name> <permission type> <key> [endkey]",
  70. Short: "Grants a key to a role",
  71. Run: roleGrantPermissionCommandFunc,
  72. }
  73. cmd.Flags().BoolVar(&grantPermissionPrefix, "prefix", false, "grant a prefix permission")
  74. return cmd
  75. }
  76. func newRoleRevokePermissionCommand() *cobra.Command {
  77. return &cobra.Command{
  78. Use: "revoke-permission <role name> <key> [endkey]",
  79. Short: "Revokes a key from a role",
  80. Run: roleRevokePermissionCommandFunc,
  81. }
  82. }
  83. // roleAddCommandFunc executes the "role add" command.
  84. func roleAddCommandFunc(cmd *cobra.Command, args []string) {
  85. if len(args) != 1 {
  86. ExitWithError(ExitBadArgs, fmt.Errorf("role add command requires role name as its argument."))
  87. }
  88. _, err := mustClientFromCmd(cmd).Auth.RoleAdd(context.TODO(), args[0])
  89. if err != nil {
  90. ExitWithError(ExitError, err)
  91. }
  92. fmt.Printf("Role %s created\n", args[0])
  93. }
  94. // roleDeleteCommandFunc executes the "role delete" command.
  95. func roleDeleteCommandFunc(cmd *cobra.Command, args []string) {
  96. if len(args) != 1 {
  97. ExitWithError(ExitBadArgs, fmt.Errorf("role delete command requires role name as its argument."))
  98. }
  99. _, err := mustClientFromCmd(cmd).Auth.RoleDelete(context.TODO(), args[0])
  100. if err != nil {
  101. ExitWithError(ExitError, err)
  102. }
  103. fmt.Printf("Role %s deleted\n", args[0])
  104. }
  105. func printRolePermissions(name string, resp *clientv3.AuthRoleGetResponse) {
  106. fmt.Printf("Role %s\n", name)
  107. fmt.Println("KV Read:")
  108. printRange := func(perm *clientv3.Permission) {
  109. sKey := string(perm.Key)
  110. sRangeEnd := string(perm.RangeEnd)
  111. fmt.Printf("\t[%s, %s)", sKey, sRangeEnd)
  112. if strings.Compare(clientv3.GetPrefixRangeEnd(sKey), sRangeEnd) == 0 {
  113. fmt.Printf(" (prefix %s)", sKey)
  114. }
  115. fmt.Printf("\n")
  116. }
  117. for _, perm := range resp.Perm {
  118. if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
  119. if len(perm.RangeEnd) == 0 {
  120. fmt.Printf("\t%s\n", string(perm.Key))
  121. } else {
  122. printRange((*clientv3.Permission)(perm))
  123. }
  124. }
  125. }
  126. fmt.Println("KV Write:")
  127. for _, perm := range resp.Perm {
  128. if perm.PermType == clientv3.PermWrite || perm.PermType == clientv3.PermReadWrite {
  129. if len(perm.RangeEnd) == 0 {
  130. fmt.Printf("\t%s\n", string(perm.Key))
  131. } else {
  132. printRange((*clientv3.Permission)(perm))
  133. }
  134. }
  135. }
  136. }
  137. // roleGetCommandFunc executes the "role get" command.
  138. func roleGetCommandFunc(cmd *cobra.Command, args []string) {
  139. if len(args) != 1 {
  140. ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
  141. }
  142. name := args[0]
  143. resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), name)
  144. if err != nil {
  145. ExitWithError(ExitError, err)
  146. }
  147. printRolePermissions(name, resp)
  148. }
  149. // roleListCommandFunc executes the "role list" command.
  150. func roleListCommandFunc(cmd *cobra.Command, args []string) {
  151. if len(args) != 0 {
  152. ExitWithError(ExitBadArgs, fmt.Errorf("role list command requires no arguments."))
  153. }
  154. resp, err := mustClientFromCmd(cmd).Auth.RoleList(context.TODO())
  155. if err != nil {
  156. ExitWithError(ExitError, err)
  157. }
  158. for _, role := range resp.Roles {
  159. fmt.Printf("%s\n", role)
  160. }
  161. }
  162. // roleGrantPermissionCommandFunc executes the "role grant-permission" command.
  163. func roleGrantPermissionCommandFunc(cmd *cobra.Command, args []string) {
  164. if len(args) < 3 {
  165. ExitWithError(ExitBadArgs, fmt.Errorf("role grant command requires role name, permission type, and key [endkey] as its argument."))
  166. }
  167. perm, err := clientv3.StrToPermissionType(args[1])
  168. if err != nil {
  169. ExitWithError(ExitBadArgs, err)
  170. }
  171. rangeEnd := ""
  172. if 4 <= len(args) {
  173. if grantPermissionPrefix {
  174. ExitWithError(ExitBadArgs, fmt.Errorf("don't pass both of --prefix option and range end to grant permission command"))
  175. }
  176. rangeEnd = args[3]
  177. } else if grantPermissionPrefix {
  178. rangeEnd = clientv3.GetPrefixRangeEnd(args[2])
  179. }
  180. _, err = mustClientFromCmd(cmd).Auth.RoleGrantPermission(context.TODO(), args[0], args[2], rangeEnd, perm)
  181. if err != nil {
  182. ExitWithError(ExitError, err)
  183. }
  184. fmt.Printf("Role %s updated\n", args[0])
  185. }
  186. // roleRevokePermissionCommandFunc executes the "role revoke-permission" command.
  187. func roleRevokePermissionCommandFunc(cmd *cobra.Command, args []string) {
  188. if len(args) < 2 {
  189. ExitWithError(ExitBadArgs, fmt.Errorf("role revoke-permission command requires role name and key [endkey] as its argument."))
  190. }
  191. rangeEnd := ""
  192. if 3 <= len(args) {
  193. rangeEnd = args[2]
  194. }
  195. _, err := mustClientFromCmd(cmd).Auth.RoleRevokePermission(context.TODO(), args[0], args[1], rangeEnd)
  196. if err != nil {
  197. ExitWithError(ExitError, err)
  198. }
  199. if len(rangeEnd) == 0 {
  200. fmt.Printf("Permission of key %s is revoked from role %s\n", args[1], args[0])
  201. } else {
  202. fmt.Printf("Permission of range [%s, %s) is revoked from role %s\n", args[1], rangeEnd, args[0])
  203. }
  204. }