*: support getting role in auth v3
This commit implements RoleGet() RPC of etcdserver and adds a new subcommand "role get" to etcdctl v3. It will list up permissions that are granted to a given role. $ ETCDCTL_API=3 bin/etcdctl role get r1 Role r1 KV Read: b d KV Write: a c d
This commit is contained in:

committed by
Hitoshi Mitake

parent
755567cb3d
commit
10ee69b44c
@ -31,6 +31,7 @@ func NewRoleCommand() *cobra.Command {
|
||||
|
||||
ac.AddCommand(newRoleAddCommand())
|
||||
ac.AddCommand(newRoleGrantCommand())
|
||||
ac.AddCommand(newRoleGetCommand())
|
||||
|
||||
return ac
|
||||
}
|
||||
@ -51,6 +52,14 @@ func newRoleGrantCommand() *cobra.Command {
|
||||
}
|
||||
}
|
||||
|
||||
func newRoleGetCommand() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "get <role name>",
|
||||
Short: "get detailed information of a role",
|
||||
Run: roleGetCommandFunc,
|
||||
}
|
||||
}
|
||||
|
||||
// roleAddCommandFunc executes the "role add" command.
|
||||
func roleAddCommandFunc(cmd *cobra.Command, args []string) {
|
||||
if len(args) != 1 {
|
||||
@ -83,3 +92,29 @@ func roleGrantCommandFunc(cmd *cobra.Command, args []string) {
|
||||
|
||||
fmt.Printf("Role %s updated\n", args[0])
|
||||
}
|
||||
|
||||
// roleGetCommandFunc executes the "role get" command.
|
||||
func roleGetCommandFunc(cmd *cobra.Command, args []string) {
|
||||
if len(args) != 1 {
|
||||
ExitWithError(ExitBadArgs, fmt.Errorf("role get command requires role name as its argument."))
|
||||
}
|
||||
|
||||
resp, err := mustClientFromCmd(cmd).Auth.RoleGet(context.TODO(), args[0])
|
||||
if err != nil {
|
||||
ExitWithError(ExitError, err)
|
||||
}
|
||||
|
||||
fmt.Printf("Role %s\n", args[0])
|
||||
fmt.Println("KV Read:")
|
||||
for _, perm := range resp.Perm {
|
||||
if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
|
||||
fmt.Printf("\t%s\n", string(perm.Key))
|
||||
}
|
||||
}
|
||||
fmt.Println("KV Write:")
|
||||
for _, perm := range resp.Perm {
|
||||
if perm.PermType == clientv3.PermWrite || perm.PermType == clientv3.PermReadWrite {
|
||||
fmt.Printf("\t%s\n", string(perm.Key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user