etcdctl, clientv3: improve printing of role get for prefix permission
This commit improves printing of role get command for prefix permission. If a range permission corresponds to a prefix permission, it is explicitly printed for a user. Below is an example of the new printing: $ ETCDCTL_API=3 bin/etcdctl --user root:p role get r1 Role r1 KV Read: [/dir/, /dir0) (prefix /dir/) [k1, k5) KV Write: [/dir/, /dir0) (prefix /dir/) [k1, k5)
This commit is contained in:
@ -43,6 +43,7 @@ type (
|
|||||||
AuthRoleListResponse pb.AuthRoleListResponse
|
AuthRoleListResponse pb.AuthRoleListResponse
|
||||||
|
|
||||||
PermissionType authpb.Permission_Type
|
PermissionType authpb.Permission_Type
|
||||||
|
Permission authpb.Permission
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -16,6 +16,7 @@ package command
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/coreos/etcd/clientv3"
|
"github.com/coreos/etcd/clientv3"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -126,12 +127,23 @@ func roleDeleteCommandFunc(cmd *cobra.Command, args []string) {
|
|||||||
func printRolePermissions(name string, resp *clientv3.AuthRoleGetResponse) {
|
func printRolePermissions(name string, resp *clientv3.AuthRoleGetResponse) {
|
||||||
fmt.Printf("Role %s\n", name)
|
fmt.Printf("Role %s\n", name)
|
||||||
fmt.Println("KV Read:")
|
fmt.Println("KV Read:")
|
||||||
|
|
||||||
|
printRange := func(perm *clientv3.Permission) {
|
||||||
|
sKey := string(perm.Key)
|
||||||
|
sRangeEnd := string(perm.RangeEnd)
|
||||||
|
fmt.Printf("\t[%s, %s)", sKey, sRangeEnd)
|
||||||
|
if strings.Compare(clientv3.GetPrefixRangeEnd(sKey), sRangeEnd) == 0 {
|
||||||
|
fmt.Printf(" (prefix %s)", sKey)
|
||||||
|
}
|
||||||
|
fmt.Printf("\n")
|
||||||
|
}
|
||||||
|
|
||||||
for _, perm := range resp.Perm {
|
for _, perm := range resp.Perm {
|
||||||
if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
|
if perm.PermType == clientv3.PermRead || perm.PermType == clientv3.PermReadWrite {
|
||||||
if len(perm.RangeEnd) == 0 {
|
if len(perm.RangeEnd) == 0 {
|
||||||
fmt.Printf("\t%s\n", string(perm.Key))
|
fmt.Printf("\t%s\n", string(perm.Key))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\t[%s, %s)\n", string(perm.Key), string(perm.RangeEnd))
|
printRange((*clientv3.Permission)(perm))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -141,7 +153,7 @@ func printRolePermissions(name string, resp *clientv3.AuthRoleGetResponse) {
|
|||||||
if len(perm.RangeEnd) == 0 {
|
if len(perm.RangeEnd) == 0 {
|
||||||
fmt.Printf("\t%s\n", string(perm.Key))
|
fmt.Printf("\t%s\n", string(perm.Key))
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\t[%s, %s)\n", string(perm.Key), string(perm.RangeEnd))
|
printRange((*clientv3.Permission)(perm))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user