diff --git a/cmd/admin-group-add.go b/cmd/admin-group-add.go index c668a159..87ed2b16 100644 --- a/cmd/admin-group-add.go +++ b/cmd/admin-group-add.go @@ -48,6 +48,10 @@ FLAGS: EXAMPLES: 1. Add users 'fivecent' and 'tencent' to the group 'allcents': {{.Prompt}} {{.HelpName}} myminio allcents fivecent tencent + + 2. Add user "james" to group "staff", then add the "readwrite" policy to the group "staff". + {{.Prompt}} {{.HelpName}} myminio staff james + {{.Prompt}} mc admin policy attach myminio readwrite --group staff `, } diff --git a/cmd/admin-policy-add.go b/cmd/admin-policy-add.go index 7183d1ea..fce7d5e6 100644 --- a/cmd/admin-policy-add.go +++ b/cmd/admin-policy-add.go @@ -19,126 +19,25 @@ package cmd import ( "fmt" - "os" - "github.com/fatih/color" "github.com/minio/cli" - json "github.com/minio/colorjson" - "github.com/minio/madmin-go/v2" "github.com/minio/mc/pkg/probe" - "github.com/minio/pkg/console" ) var adminPolicyAddCmd = cli.Command{ - Name: "add", - Usage: "add new policy", - Action: mainAdminPolicyAdd, - OnUsageError: onUsageError, - Before: setGlobalsFromContext, - Flags: globalFlags, - CustomHelpTemplate: `NAME: - {{.HelpName}} - {{.Usage}} - -USAGE: - {{.HelpName}} TARGET POLICYNAME POLICYFILE - -POLICYNAME: - Name of the canned policy on MinIO server. - -POLICYFILE: - Name of the policy file associated with the policy name. - -FLAGS: - {{range .VisibleFlags}}{{.}} - {{end}} -EXAMPLES: - 1. Add a new canned policy 'writeonly'. - {{.Prompt}} {{.HelpName}} myminio writeonly /tmp/writeonly.json - `, + Name: "add", + Usage: "add an IAM policy", + Action: mainAdminPolicyAdd, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + Hidden: true, + CustomHelpTemplate: `Please use 'mc admin policy create'`, } -// checkAdminPolicyAddSyntax - validate all the passed arguments -func checkAdminPolicyAddSyntax(ctx *cli.Context) { - if len(ctx.Args()) != 3 { - showCommandHelpAndExit(ctx, 1) // last argument is exit code - } -} - -// userPolicyMessage container for content message structure -type userPolicyMessage struct { - op string - 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 { - switch u.op { - case "set", "unset", "update": - if u.IsGroup { - return "group" - } - return "user" - } - return "" -} - -func (u userPolicyMessage) String() string { - switch u.op { - case "info": - buf, e := json.MarshalIndent(u.PolicyInfo, "", " ") - fatalIf(probe.NewError(e), "Unable to marshal to JSON.") - return string(buf) - case "list": - return console.Colorize("PolicyName", u.Policy) - case "remove": - return console.Colorize("PolicyMessage", "Removed policy `"+u.Policy+"` successfully.") - case "add": - return console.Colorize("PolicyMessage", "Added policy `"+u.Policy+"` successfully.") - case "set", "unset": - return console.Colorize("PolicyMessage", - fmt.Sprintf("Policy `%s` is %s on %s `%s`", u.Policy, u.op, u.accountType(), u.UserOrGroup)) - case "update": - return console.Colorize("PolicyMessage", - fmt.Sprintf("Policy `%s` is added to %s `%s`", u.Policy, u.accountType(), u.UserOrGroup)) - } - - return "" -} - -func (u userPolicyMessage) JSON() string { - u.Status = "success" - jsonMessageBytes, e := json.MarshalIndent(u, "", " ") - fatalIf(probe.NewError(e), "Unable to marshal into JSON.") - - return string(jsonMessageBytes) -} - -// mainAdminPolicyAdd is the handle for "mc admin policy add" command. func mainAdminPolicyAdd(ctx *cli.Context) error { - checkAdminPolicyAddSyntax(ctx) - - console.SetColor("PolicyMessage", color.New(color.FgGreen)) - - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) - - policy, e := os.ReadFile(args.Get(2)) - fatalIf(probe.NewError(e).Trace(args...), "Unable to get policy") - - // Create a new MinIO Admin Client - client, err := newAdminClient(aliasedURL) - fatalIf(err, "Unable to initialize admin connection.") - - fatalIf(probe.NewError(client.AddCannedPolicy(globalContext, args.Get(1), policy)).Trace(args...), "Unable to add new policy") - - printMsg(userPolicyMessage{ - op: ctx.Command.Name, - Policy: args.Get(1), - }) - + err := probe.NewError(fmt.Errorf("Please use 'mc admin policy create'")) + fatal(err, "Incorrect command") return nil } diff --git a/cmd/admin-policy-attach.go b/cmd/admin-policy-attach.go new file mode 100644 index 00000000..1453db4c --- /dev/null +++ b/cmd/admin-policy-attach.go @@ -0,0 +1,130 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "github.com/fatih/color" + "github.com/minio/cli" + "github.com/minio/madmin-go/v2" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var adminAttachPolicyFlags = []cli.Flag{ + cli.StringFlag{ + Name: "user, u", + Usage: "attach policy to user", + }, + cli.StringFlag{ + Name: "group, g", + Usage: "attach policy to group", + }, +} + +var adminPolicyAttachCmd = cli.Command{ + Name: "attach", + Usage: "attach an IAM policy to a user or group", + Action: mainAdminPolicyAttach, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(adminAttachPolicyFlags, globalFlags...), + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET POLICY [POLICY...] [--user USER | --group GROUP] + + Exactly one of --user or --group is required. + +POLICY: + Name of the policy on the MinIO server. + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Attach the "readonly" policy to user "james". + {{.Prompt}} {{.HelpName}} myminio readonly --user james + 2. Attach the "audit-policy" and "acct-policy" policies to group "legal". + {{.Prompt}} {{.HelpName}} myminio audit-policy acct-policy --group legal +`, +} + +// mainAdminPolicyAttach is the handler for "mc admin policy attach" command. +func mainAdminPolicyAttach(ctx *cli.Context) error { + return userAttachOrDetachPolicy(ctx, true) +} + +func userAttachOrDetachPolicy(ctx *cli.Context, attach bool) error { + if len(ctx.Args()) < 2 { + showCommandHelpAndExit(ctx, 1) // last argument is exit code + } + + console.SetColor("PolicyMessage", color.New(color.FgGreen)) + console.SetColor("Policy", color.New(color.FgBlue)) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + + // Put args in PolicyAssociationReq, client checks for validity + req := madmin.PolicyAssociationReq{ + User: ctx.String("user"), + Group: ctx.String("group"), + Policies: args.Tail(), + } + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + fatalIf(err, "Unable to initialize admin connection.") + + var e error + if attach { + e = client.AttachPolicy(globalContext, req) + } else { + e = client.DetachPolicy(globalContext, req) + } + + isGroup := false + if req.User == "" { + isGroup = true + } + + userOrGroup := req.User + if isGroup { + userOrGroup = req.Group + } + + if e == nil { + for _, policy := range req.Policies { + printMsg(userPolicyMessage{ + op: ctx.Command.Name, + Policy: policy, + UserOrGroup: userOrGroup, + IsGroup: isGroup, + }) + } + } else { + if attach { + fatalIf(probe.NewError(e), "Unable to attach the policy") + } else { + fatalIf(probe.NewError(e), "Unable to detach the policy") + } + } + return nil +} diff --git a/cmd/admin-policy-create.go b/cmd/admin-policy-create.go new file mode 100644 index 00000000..3e567a0b --- /dev/null +++ b/cmd/admin-policy-create.go @@ -0,0 +1,144 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "fmt" + "os" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/madmin-go/v2" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var adminPolicyCreateCmd = cli.Command{ + Name: "create", + Usage: "create a new IAM policy", + Action: mainAdminPolicyCreate, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} TARGET POLICYNAME POLICYFILE + +POLICYNAME: + Name of the canned policy on MinIO server. + +POLICYFILE: + Name of the policy file associated with the policy name. + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Create a new canned policy 'writeonly'. + {{.Prompt}} {{.HelpName}} myminio writeonly /tmp/writeonly.json + `, +} + +// checkAdminPolicyCreateSyntax - validate all the passed arguments +func checkAdminPolicyCreateSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 3 { + showCommandHelpAndExit(ctx, 1) // last argument is exit code + } +} + +// userPolicyMessage container for content message structure +type userPolicyMessage struct { + op string + 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 { + switch u.op { + case "attach", "detach": + if u.IsGroup { + return "group" + } + return "user" + } + return "" +} + +func (u userPolicyMessage) String() string { + switch u.op { + case "info": + buf, e := json.MarshalIndent(u.PolicyInfo, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal to JSON.") + return string(buf) + case "list": + return console.Colorize("PolicyName", u.Policy) + case "remove": + return console.Colorize("PolicyMessage", "Removed policy `"+u.Policy+"` successfully.") + case "create": + return console.Colorize("PolicyMessage", "Created policy `"+u.Policy+"` successfully.") + case "detach": + return console.Colorize("PolicyMessage", + fmt.Sprintf("Policy `%s` successfully detached from %s `%s`", u.Policy, u.accountType(), u.UserOrGroup)) + case "attach": + return console.Colorize("PolicyMessage", + fmt.Sprintf("Policy `%s` successfully attached to %s `%s`", u.Policy, u.accountType(), u.UserOrGroup)) + } + + return "" +} + +func (u userPolicyMessage) JSON() string { + u.Status = "success" + jsonMessageBytes, e := json.MarshalIndent(u, "", " ") + fatalIf(probe.NewError(e), "Unable to marshal into JSON.") + + return string(jsonMessageBytes) +} + +// mainAdminPolicyCreate is the handle for "mc admin policy create" command. +func mainAdminPolicyCreate(ctx *cli.Context) error { + checkAdminPolicyCreateSyntax(ctx) + + console.SetColor("PolicyMessage", color.New(color.FgGreen)) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + + policy, e := os.ReadFile(args.Get(2)) + fatalIf(probe.NewError(e).Trace(args...), "Unable to get policy") + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + fatalIf(err, "Unable to initialize admin connection.") + + fatalIf(probe.NewError(client.AddCannedPolicy(globalContext, args.Get(1), policy)).Trace(args...), "Unable to create new policy") + + printMsg(userPolicyMessage{ + op: ctx.Command.Name, + Policy: args.Get(1), + }) + + return nil +} diff --git a/cmd/admin-policy-detach.go b/cmd/admin-policy-detach.go new file mode 100644 index 00000000..6a0edb0d --- /dev/null +++ b/cmd/admin-policy-detach.go @@ -0,0 +1,67 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "github.com/minio/cli" +) + +var adminDetachPolicyFlags = []cli.Flag{ + cli.StringFlag{ + Name: "user, u", + Usage: "detach policy from user", + }, + cli.StringFlag{ + Name: "group, g", + Usage: "detach policy from group", + }, +} + +var adminPolicyDetachCmd = cli.Command{ + Name: "detach", + Usage: "detach an IAM policy from a user or group", + Action: mainAdminPolicyDetach, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(adminDetachPolicyFlags, globalFlags...), + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET POLICY [POLICY...] [--user USER | --group GROUP] + + Exactly one of --user or --group is required. + +POLICY: + Name of the policy on the MinIO server. + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Detach the "readonly" policy from user "james". + {{.Prompt}} {{.HelpName}} myminio readonly --user james + 2. Detach the "audit-policy" and "acct-policy" policies from group "legal". + {{.Prompt}} {{.HelpName}} myminio audit-policy acct-policy --group legal +`, +} + +// mainAdmihPolicyDetach is the handler for "mc admin policy detach" command. +func mainAdminPolicyDetach(ctx *cli.Context) error { + return userAttachOrDetachPolicy(ctx, false) +} diff --git a/cmd/admin-policy-entities.go b/cmd/admin-policy-entities.go new file mode 100644 index 00000000..fb51185e --- /dev/null +++ b/cmd/admin-policy-entities.go @@ -0,0 +1,100 @@ +// Copyright (c) 2015-2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "github.com/minio/cli" + "github.com/minio/madmin-go/v2" + "github.com/minio/mc/pkg/probe" +) + +var adminPolicyEntitiesFlags = []cli.Flag{ + cli.StringSliceFlag{ + Name: "user, u", + Usage: "list policies associated with user(s)", + }, + cli.StringSliceFlag{ + Name: "group, g", + Usage: "list policies associated with group(s)", + }, + cli.StringSliceFlag{ + Name: "policy, p", + Usage: "list users or groups associated with policy", + }, +} + +var adminPolicyEntitiesCmd = cli.Command{ + Name: "entities", + Usage: "list policy association entities", + Action: mainAdminPolicyEntities, + Before: setGlobalsFromContext, + Flags: append(adminPolicyEntitiesFlags, globalFlags...), + OnUsageError: onUsageError, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. List all entities associated with all policies + {{.Prompt}} {{.HelpName}} play/ + 2. List all entities associated with the policies 'finteam-policy' and 'mlteam-policy' + {{.Prompt}} {{.HelpName}} play/ --policy finteam-policy --policy mlteam-policy + 3. List all policies associated with a pair of user entities + {{.Prompt}} {{.HelpName}} play/ --user bob --user james + 4. List all policies associated with a pair of group entities + {{.Prompt}} {{.HelpName}} play/ --group auditors --group accounting + 5. List all entities associated with a policy, group and user + {{.Prompt}} {{.HelpName}} play/ \ + --policy finteam-policy --user bobfisher --group consulting +`, +} + +// mainAdminPolicyEntities is the handler for "mc admin policy entities" command. +func mainAdminPolicyEntities(ctx *cli.Context) error { + if len(ctx.Args()) != 1 { + showCommandHelpAndExit(ctx, 1) + } + + usersToQuery := ctx.StringSlice("user") + groupsToQuery := ctx.StringSlice("group") + policiesToQuery := ctx.StringSlice("policy") + + args := ctx.Args() + + aliasedURL := args.Get(0) + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + fatalIf(err, "Unable to initialize admin connection.") + + res, e := client.GetPolicyEntities(globalContext, + madmin.PolicyEntitiesQuery{ + Users: usersToQuery, + Groups: groupsToQuery, + Policy: policiesToQuery, + }) + fatalIf(probe.NewError(e), "Unable to fetch policy entities") + + printMsg(policyEntitiesFrom(res)) + return nil +} diff --git a/cmd/admin-policy-info.go b/cmd/admin-policy-info.go index 1d64960e..99549c46 100644 --- a/cmd/admin-policy-info.go +++ b/cmd/admin-policy-info.go @@ -36,7 +36,7 @@ var policyInfoFlags = []cli.Flag{ var adminPolicyInfoCmd = cli.Command{ Name: "info", - Usage: "show info on a policy", + Usage: "show info on an IAM policy", Action: mainAdminPolicyInfo, OnUsageError: onUsageError, Before: setGlobalsFromContext, diff --git a/cmd/admin-policy-list.go b/cmd/admin-policy-list.go index 64cdd44d..472c7ed9 100644 --- a/cmd/admin-policy-list.go +++ b/cmd/admin-policy-list.go @@ -26,7 +26,7 @@ import ( var adminPolicyListCmd = cli.Command{ Name: "list", - Usage: "list all policies", + Usage: "list all IAM policies", Action: mainAdminPolicyList, OnUsageError: onUsageError, Before: setGlobalsFromContext, diff --git a/cmd/admin-policy-remove.go b/cmd/admin-policy-remove.go index b89c642f..64a3dbac 100644 --- a/cmd/admin-policy-remove.go +++ b/cmd/admin-policy-remove.go @@ -26,7 +26,7 @@ import ( var adminPolicyRemoveCmd = cli.Command{ Name: "remove", - Usage: "remove policy", + Usage: "remove an IAM policy", Action: mainAdminPolicyRemove, OnUsageError: onUsageError, Before: setGlobalsFromContext, diff --git a/cmd/admin-policy-set.go b/cmd/admin-policy-set.go index 9eac74ee..c63f2638 100644 --- a/cmd/admin-policy-set.go +++ b/cmd/admin-policy-set.go @@ -18,102 +18,26 @@ package cmd import ( - "errors" - "strings" + "fmt" - "github.com/fatih/color" "github.com/minio/cli" "github.com/minio/mc/pkg/probe" - "github.com/minio/pkg/console" ) var adminPolicySetCmd = cli.Command{ - Name: "set", - Usage: "set IAM policy on a user or group", - Action: mainAdminPolicySet, - OnUsageError: onUsageError, - Before: setGlobalsFromContext, - Flags: globalFlags, - CustomHelpTemplate: `NAME: - {{.HelpName}} - {{.Usage}} - -USAGE: - {{.HelpName}} TARGET POLICYNAME [ user=username1 | group=groupname1 ] - -POLICYNAME: - Name of the policy on the MinIO server. To set multiple policies, separate names with a comma (,). - -FLAGS: - {{range .VisibleFlags}}{{.}} - {{end}} -EXAMPLES: - 1. Set the "readwrite" policy for user "james". - {{.Prompt}} {{.HelpName}} myminio readwrite user=james - - 2. Set the "readonly" policy for group "auditors". - {{.Prompt}} {{.HelpName}} myminio readonly group=auditors - - 3. Set the "readonly" and the "diagnostics" policies for user "alice" - {{.Prompt}} {{.HelpName}} myminio readonly,diagnostics user=alice -`, + Name: "set", + Usage: "set IAM policy on a user or group", + Action: mainAdminPolicySet, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + Hidden: true, + CustomHelpTemplate: `Please use 'mc admin policy attach'`, } -var errBadUserGroupArg = errors.New("Last argument must be of the form user=xx or group=xx") - -func checkAdminPolicySetSyntax(ctx *cli.Context) { - if len(ctx.Args()) != 3 { - showCommandHelpAndExit(ctx, 1) // last argument is exit code - } -} - -func parseEntityArg(arg string) (userOrGroup string, isGroup bool, err error) { - parts := strings.SplitN(arg, "=", 2) - switch { - case len(parts) != 2 || parts[1] == "": - err = errBadUserGroupArg - case strings.ToLower(parts[0]) == "user": - userOrGroup = parts[1] - isGroup = false - case strings.ToLower(parts[0]) == "group": - userOrGroup = parts[1] - isGroup = true - default: - err = errBadUserGroupArg - - } - return -} - -// mainAdminPolicySet is the handler for "mc admin policy set" command. func mainAdminPolicySet(ctx *cli.Context) error { - checkAdminPolicySetSyntax(ctx) - - console.SetColor("PolicyMessage", color.New(color.FgGreen)) - console.SetColor("Policy", color.New(color.FgBlue)) - - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) - policyName := strings.TrimSpace(args.Get(1)) - entityArg := args.Get(2) - - userOrGroup, isGroup, e1 := parseEntityArg(entityArg) - fatalIf(probe.NewError(e1).Trace(args...), "Bad last argument") - - // Create a new MinIO Admin Client - client, err := newAdminClient(aliasedURL) - fatalIf(err, "Unable to initialize admin connection.") - - e := client.SetPolicy(globalContext, policyName, userOrGroup, isGroup) - if e == nil { - printMsg(userPolicyMessage{ - op: ctx.Command.Name, - Policy: policyName, - UserOrGroup: userOrGroup, - IsGroup: isGroup, - }) - } else { - fatalIf(probe.NewError(e), "Unable to set the policy") - } + err := probe.NewError(fmt.Errorf("Please use 'mc admin policy attach'")) + fatal(err, "Incorrect command") return nil } diff --git a/cmd/admin-policy-unset.go b/cmd/admin-policy-unset.go index 0d1da28e..c97e63ab 100644 --- a/cmd/admin-policy-unset.go +++ b/cmd/admin-policy-unset.go @@ -18,120 +18,26 @@ package cmd import ( - "errors" "fmt" - "strings" - "github.com/fatih/color" "github.com/minio/cli" "github.com/minio/mc/pkg/probe" - "github.com/minio/pkg/console" ) var adminPolicyUnsetCmd = cli.Command{ - Name: "unset", - Usage: "unset an IAM policy for a user or group", - Action: mainAdminPolicyUnset, - OnUsageError: onUsageError, - Before: setGlobalsFromContext, - Flags: globalFlags, - CustomHelpTemplate: `NAME: - {{.HelpName}} - {{.Usage}} - -USAGE: - {{.HelpName}} TARGET POLICYNAME [ user=username1 | group=groupname1 ] - -POLICYNAME: - Name of the policy on the MinIO server. - -FLAGS: - {{range .VisibleFlags}}{{.}} - {{end}} -EXAMPLES: - 1. Unset the "diagnostics" policy for user "james". - {{.Prompt}} {{.HelpName}} myminio diagnostics user=james - - 2. Set the "diagnostics" policy for group "auditors". - {{.Prompt}} {{.HelpName}} myminio diagnostics group=auditors -`, + Name: "unset", + Usage: "unset an IAM policy for a user or group", + Action: mainAdminPolicyUnsetErr, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + Hidden: true, + CustomHelpTemplate: `Please use 'mc admin policy detach'`, } -func checkAdminPolicyUnsetSyntax(ctx *cli.Context) { - if len(ctx.Args()) != 3 { - showCommandHelpAndExit(ctx, 1) // last argument is exit code - } -} - -func removeCannedPolicies(existingPolicies, policiesToRemove string) (string, error) { - policiesToRemove = strings.TrimSpace(policiesToRemove) - if policiesToRemove == "" { - return "", errors.New("empty policy name is not supported") - } - filteredPolicies := strings.Split(existingPolicies, ",") - for _, p1 := range strings.Split(policiesToRemove, ",") { - found := false - for i, p2 := range filteredPolicies { - if p1 == p2 { - found = true - filteredPolicies = append(filteredPolicies[:i], filteredPolicies[i+1:]...) - break - } - } - if !found { - return "", fmt.Errorf("policy `%s` not found", p1) - } - - } - return strings.Join(filteredPolicies, ","), nil -} - -// mainAdminPolicyUnset is the handler for "mc admin policy unset" command. -func mainAdminPolicyUnset(ctx *cli.Context) error { - checkAdminPolicyUnsetSyntax(ctx) - - console.SetColor("PolicyMessage", color.New(color.FgGreen)) - console.SetColor("Policy", color.New(color.FgBlue)) - - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) - policiesToUnset := args.Get(1) - entityArg := args.Get(2) - - userOrGroup, isGroup, e1 := parseEntityArg(entityArg) - fatalIf(probe.NewError(e1).Trace(args...), "Bad last argument") - - // Create a new MinIO Admin Client - client, err := newAdminClient(aliasedURL) - fatalIf(err, "Unable to initialize admin connection.") - - var existingPolicies string - - if !isGroup { - userInfo, e := client.GetUserInfo(globalContext, userOrGroup) - fatalIf(probe.NewError(e).Trace(args...), "Unable to get user policy info") - existingPolicies = userInfo.PolicyName - } else { - groupInfo, e := client.GetGroupDescription(globalContext, userOrGroup) - fatalIf(probe.NewError(e).Trace(args...), "Unable to get group policy info") - existingPolicies = groupInfo.Policy - } - - newPolicies, e := removeCannedPolicies(existingPolicies, policiesToUnset) - if e != nil { - fatalIf(probe.NewError(e).Trace(args...), "Unable to unset the policy") - } - - e = client.SetPolicy(globalContext, newPolicies, userOrGroup, isGroup) - if e == nil { - printMsg(userPolicyMessage{ - op: ctx.Command.Name, - Policy: policiesToUnset, - UserOrGroup: userOrGroup, - IsGroup: isGroup, - }) - } else { - fatalIf(probe.NewError(e), "Unable to unset the policy") - } +func mainAdminPolicyUnsetErr(ctx *cli.Context) error { + err := probe.NewError(fmt.Errorf("Please use 'mc admin policy detach'")) + fatal(err, "Incorrect command") return nil } diff --git a/cmd/admin-policy-update.go b/cmd/admin-policy-update.go index 17ed550e..e2d17b1d 100644 --- a/cmd/admin-policy-update.go +++ b/cmd/admin-policy-update.go @@ -18,125 +18,26 @@ package cmd import ( - "errors" "fmt" - "strings" - "github.com/fatih/color" "github.com/minio/cli" "github.com/minio/mc/pkg/probe" - "github.com/minio/pkg/console" ) var adminPolicyUpdateCmd = cli.Command{ - Name: "update", - Usage: "attach a new IAM policy to user or group", - Action: mainAdminPolicyUpdate, - OnUsageError: onUsageError, - Before: setGlobalsFromContext, - Flags: globalFlags, - CustomHelpTemplate: `NAME: - {{.HelpName}} - {{.Usage}} - -USAGE: - {{.HelpName}} TARGET POLICYNAME [ user=username1 | group=groupname1 ] - -POLICYNAME: - Name of the policy on the MinIO server. - -FLAGS: - {{range .VisibleFlags}}{{.}} - {{end}} -EXAMPLES: - 1. Add the "diagnostics" policy for user "james". - {{.Prompt}} {{.HelpName}} myminio diagnostics user=james - - 2. Add the "diagnostics" policy for group "auditors". - {{.Prompt}} {{.HelpName}} myminio diagnostics group=auditors -`, + Name: "update", + Usage: "attach a new IAM policy to user or group", + Action: mainAdminPolicyUpdateErr, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + Hidden: true, + CustomHelpTemplate: `Please use 'mc admin policy attach'`, } -func checkAdminPolicyUpdateSyntax(ctx *cli.Context) { - if len(ctx.Args()) != 3 { - showCommandHelpAndExit(ctx, 1) // last argument is exit code - } -} - -func updateCannedPolicies(existingPolicies, policiesToAdd string) (string, error) { - policiesToAdd = strings.TrimSpace(policiesToAdd) - if policiesToAdd == "" { - return "", errors.New("empty policy name is unsupported") - } - var updatedPolicies []string - if existingPolicies != "" { - updatedPolicies = strings.Split(existingPolicies, ",") - } - - for _, p1 := range strings.Split(policiesToAdd, ",") { - found := false - p1 = strings.TrimSpace(p1) - for _, p2 := range updatedPolicies { - if p1 == p2 { - found = true - break - } - } - if found { - return "", fmt.Errorf("policy `%s` already exists", p1) - } - updatedPolicies = append(updatedPolicies, p1) - } - - return strings.Join(updatedPolicies, ","), nil -} - -// mainAdminPolicyUpdate is the handler for "mc admin policy update" command. -func mainAdminPolicyUpdate(ctx *cli.Context) error { - checkAdminPolicyUpdateSyntax(ctx) - - console.SetColor("PolicyMessage", color.New(color.FgGreen)) - console.SetColor("Policy", color.New(color.FgBlue)) - - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) - policiesToAdd := args.Get(1) - entityArg := args.Get(2) - - userOrGroup, isGroup, e1 := parseEntityArg(entityArg) - fatalIf(probe.NewError(e1).Trace(args...), "Bad last argument") - - // Create a new MinIO Admin Client - client, err := newAdminClient(aliasedURL) - fatalIf(err, "Unable to initialize admin connection.") - - var existingPolicies string - - if !isGroup { - userInfo, e := client.GetUserInfo(globalContext, userOrGroup) - fatalIf(probe.NewError(e).Trace(args...), "Unable to get user policy info") - existingPolicies = userInfo.PolicyName - } else { - groupInfo, e := client.GetGroupDescription(globalContext, userOrGroup) - fatalIf(probe.NewError(e).Trace(args...), "Unable to get group policy info") - existingPolicies = groupInfo.Policy - } - - updatedPolicies, e := updateCannedPolicies(existingPolicies, policiesToAdd) - if e != nil { - fatalIf(probe.NewError(e).Trace(args...), "Unable to update the policy") - } - - e = client.SetPolicy(globalContext, updatedPolicies, userOrGroup, isGroup) - if e == nil { - printMsg(userPolicyMessage{ - op: ctx.Command.Name, - Policy: policiesToAdd, - UserOrGroup: userOrGroup, - IsGroup: isGroup, - }) - } else { - fatalIf(probe.NewError(e), "Unable to unset the policy") - } +func mainAdminPolicyUpdateErr(ctx *cli.Context) error { + err := probe.NewError(fmt.Errorf("Please use 'mc admin policy attach'")) + fatal(err, "Incorrect command") return nil } diff --git a/cmd/admin-policy.go b/cmd/admin-policy.go index 9e321d67..8f14f9b7 100644 --- a/cmd/admin-policy.go +++ b/cmd/admin-policy.go @@ -20,10 +20,14 @@ package cmd import "github.com/minio/cli" var adminPolicySubcommands = []cli.Command{ - adminPolicyAddCmd, + adminPolicyCreateCmd, adminPolicyRemoveCmd, adminPolicyListCmd, adminPolicyInfoCmd, + adminPolicyAttachCmd, + adminPolicyDetachCmd, + adminPolicyEntitiesCmd, + adminPolicyAddCmd, adminPolicySetCmd, adminPolicyUnsetCmd, adminPolicyUpdateCmd, diff --git a/cmd/admin-user-add.go b/cmd/admin-user-add.go index a0ff1eb2..e1d35447 100644 --- a/cmd/admin-user-add.go +++ b/cmd/admin-user-add.go @@ -68,6 +68,10 @@ EXAMPLES: {{.DisableHistory}} {{.Prompt}} echo -e "foobar\nfoobar12345" | {{.HelpName}} myminio {{.EnableHistory}} + + 4. Add a new user 'foobar' to MinIO server, then attach IAM policy "writeonly". + {{.Prompt}} {{.HelpName}} myminio foobar foo12345 + {{.Prompt}} mc admin policy attach myminio writeonly --user foobar `, } diff --git a/cmd/admin-user-policy.go b/cmd/admin-user-policy.go index 5fd13f5f..da2b734f 100644 --- a/cmd/admin-user-policy.go +++ b/cmd/admin-user-policy.go @@ -35,17 +35,14 @@ var adminUserPolicyCmd = cli.Command{ Flags: globalFlags, CustomHelpTemplate: `NAME: {{.HelpName}} - {{.Usage}} - USAGE: {{.HelpName}} TARGET USERNAME - FLAGS: {{range .VisibleFlags}}{{.}} {{end}} EXAMPLES: 1. Display the policy document of a user "foobar" in JSON format. {{.Prompt}} {{.HelpName}} myminio foobar - `, } diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 2d1691f3..0c67d7c6 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -386,13 +386,15 @@ var completeCmds = map[string]complete.Predictor{ "/admin/idp/ldap/policy/attach": aliasCompleter, "/admin/idp/ldap/policy/detach": aliasCompleter, - "/admin/policy/info": aliasCompleter, - "/admin/policy/set": aliasCompleter, - "/admin/policy/unset": aliasCompleter, - "/admin/policy/update": aliasCompleter, - "/admin/policy/add": aliasCompleter, - "/admin/policy/list": aliasCompleter, - "/admin/policy/remove": aliasCompleter, + "/admin/policy/info": aliasCompleter, + "/admin/policy/update": aliasCompleter, + "/admin/policy/add": aliasCompleter, + "/admin/policy/remove": aliasCompleter, + "/admin/policy/create": aliasCompleter, + "/admin/policy/list": aliasCompleter, + "/admin/policy/attach": aliasCompleter, + "/admin/policy/detach": aliasCompleter, + "/admin/policy/entities": aliasCompleter, "/admin/user/add": aliasCompleter, "/admin/user/disable": aliasCompleter,