Update admin policy/user commands with new policy info API (#3877)
will support older minio versions as well
This commit is contained in:
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
json "github.com/minio/colorjson"
|
||||
"github.com/minio/madmin-go"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
"github.com/minio/pkg/console"
|
||||
)
|
||||
@@ -66,11 +67,11 @@ func checkAdminPolicyAddSyntax(ctx *cli.Context) {
|
||||
// userPolicyMessage container for content message structure
|
||||
type userPolicyMessage struct {
|
||||
op string
|
||||
Status string `json:"status"`
|
||||
Policy string `json:"policy,omitempty"`
|
||||
PolicyJSON json.RawMessage `json:"policyJSON,omitempty"`
|
||||
UserOrGroup string `json:"userOrGroup,omitempty"`
|
||||
IsGroup bool `json:"isGroup"`
|
||||
Status string `json:"status"`
|
||||
Policy string `json:"policy,omitempty"`
|
||||
PolicyInfo madmin.PolicyInfo `json:"policyInfo,omitempty"`
|
||||
UserOrGroup string `json:"userOrGroup,omitempty"`
|
||||
IsGroup bool `json:"isGroup"`
|
||||
}
|
||||
|
||||
func (u userPolicyMessage) accountType() string {
|
||||
@@ -87,7 +88,9 @@ func (u userPolicyMessage) accountType() string {
|
||||
func (u userPolicyMessage) String() string {
|
||||
switch u.op {
|
||||
case "info":
|
||||
return string(u.PolicyJSON)
|
||||
buf, err := json.MarshalIndent(u.PolicyInfo, "", " ")
|
||||
fatalIf(probe.NewError(err), "Unable to marshal to JSON.")
|
||||
return string(buf)
|
||||
case "list":
|
||||
policyFieldMaxLen := 20
|
||||
// Create a new pretty table with cols configuration
|
||||
|
||||
@@ -20,6 +20,7 @@ package cmd
|
||||
import (
|
||||
"github.com/fatih/color"
|
||||
"github.com/minio/cli"
|
||||
"github.com/minio/madmin-go"
|
||||
"github.com/minio/mc/pkg/probe"
|
||||
"github.com/minio/pkg/console"
|
||||
)
|
||||
@@ -56,6 +57,23 @@ func checkAdminPolicyInfoSyntax(ctx *cli.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func getPolicyInfo(client *madmin.AdminClient, policyName string) (*madmin.PolicyInfo, error) {
|
||||
pinfo, e := client.InfoCannedPolicyV2(globalContext, policyName)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
if pinfo.PolicyName == "" {
|
||||
// Likely server only supports the older version.
|
||||
pinfo.Policy, e = client.InfoCannedPolicy(globalContext, policyName)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
pinfo.PolicyName = policyName
|
||||
}
|
||||
return pinfo, nil
|
||||
}
|
||||
|
||||
// mainAdminPolicyInfo is the handler for "mc admin policy info" command.
|
||||
func mainAdminPolicyInfo(ctx *cli.Context) error {
|
||||
checkAdminPolicyInfoSyntax(ctx)
|
||||
@@ -72,13 +90,13 @@ func mainAdminPolicyInfo(ctx *cli.Context) error {
|
||||
client, err := newAdminClient(aliasedURL)
|
||||
fatalIf(err, "Unable to initialize admin connection")
|
||||
|
||||
buf, e := client.InfoCannedPolicy(globalContext, policyName)
|
||||
pinfo, e := getPolicyInfo(client, policyName)
|
||||
fatalIf(probe.NewError(e).Trace(args...), "Unable to fetch policy")
|
||||
|
||||
printMsg(userPolicyMessage{
|
||||
op: "info",
|
||||
Policy: policyName,
|
||||
PolicyJSON: buf,
|
||||
PolicyInfo: *pinfo,
|
||||
})
|
||||
|
||||
return nil
|
||||
|
||||
@@ -73,10 +73,10 @@ func mainAdminUserPolicy(ctx *cli.Context) error {
|
||||
user, e := client.GetUserInfo(globalContext, args.Get(1))
|
||||
fatalIf(probe.NewError(e).Trace(args...), "Unable to get user info")
|
||||
|
||||
buf, e := client.InfoCannedPolicy(globalContext, user.PolicyName)
|
||||
pinfo, e := getPolicyInfo(client, user.PolicyName)
|
||||
fatalIf(probe.NewError(e).Trace(args...), "Unable to fetch user policy document")
|
||||
|
||||
fmt.Println(string(buf))
|
||||
fmt.Println(string(pinfo.Policy))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ require (
|
||||
github.com/minio/cli v1.22.0
|
||||
github.com/minio/colorjson v1.0.1
|
||||
github.com/minio/filepath v1.0.0
|
||||
github.com/minio/madmin-go v1.1.15
|
||||
github.com/minio/madmin-go v1.1.17
|
||||
github.com/minio/minio-go/v7 v7.0.16-0.20211108161804-a7a36ee131df
|
||||
github.com/minio/pkg v1.1.3
|
||||
github.com/minio/selfupdate v0.3.1
|
||||
|
||||
@@ -276,8 +276,8 @@ github.com/minio/colorjson v1.0.1/go.mod h1:oPM3zQQY8Gz9NGtgvuBEjQ+gPZLKAGc7T+kj
|
||||
github.com/minio/filepath v1.0.0 h1:fvkJu1+6X+ECRA6G3+JJETj4QeAYO9sV43I79H8ubDY=
|
||||
github.com/minio/filepath v1.0.0/go.mod h1:/nRZA2ldl5z6jT9/KQuvZcQlxZIMQoFFQPvEXx9T/Bw=
|
||||
github.com/minio/madmin-go v1.0.12/go.mod h1:BK+z4XRx7Y1v8SFWXsuLNqQqnq5BO/axJ8IDJfgyvfs=
|
||||
github.com/minio/madmin-go v1.1.15 h1:iRfHD3xTMGjJ6EZvLQbcxog2nfXUXZ1yp283xGvuvbM=
|
||||
github.com/minio/madmin-go v1.1.15/go.mod h1:Iu0OnrMWNBYx1lqJTW+BFjBMx0Hi0wjw8VmqhiOs2Jo=
|
||||
github.com/minio/madmin-go v1.1.17 h1:P7my58nSulXawjixhS4S2pT43+3M+Y0P7M43I3YdzGo=
|
||||
github.com/minio/madmin-go v1.1.17/go.mod h1:Iu0OnrMWNBYx1lqJTW+BFjBMx0Hi0wjw8VmqhiOs2Jo=
|
||||
github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
|
||||
Reference in New Issue
Block a user