diff --git a/cmd/admin-user-sts-info.go b/cmd/admin-user-sts-info.go new file mode 100644 index 00000000..96ab0fb8 --- /dev/null +++ b/cmd/admin-user-sts-info.go @@ -0,0 +1,128 @@ +// 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 ( + "os" + "strings" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" + iampolicy "github.com/minio/pkg/iam/policy" +) + +var adminUserSTSAcctSubcommands = []cli.Command{ + adminUserSTSAcctInfoCmd, +} + +var adminUserSTSAcctCmd = cli.Command{ + Name: "sts", + Usage: "manage STS accounts", + Action: mainAdminUserSTSAcct, + Before: setGlobalsFromContext, + Flags: globalFlags, + Subcommands: adminUserSTSAcctSubcommands, + HideHelpCommand: true, +} + +// mainAdminUserSTSAcct is the handle for "mc admin user sts" command. +func mainAdminUserSTSAcct(ctx *cli.Context) error { + commandNotFound(ctx, adminUserSTSAcctSubcommands) + return nil +} + +var adminUserSTSAcctInfoFlags = []cli.Flag{ + cli.BoolFlag{ + Name: "policy", + Usage: "print policy in JSON format", + }, +} + +var adminUserSTSAcctInfoCmd = cli.Command{ + Name: "info", + Usage: "display temporary account info", + Action: mainAdminUserSTSAcctInfo, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(adminUserSTSAcctInfoFlags, globalFlags...), + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} ALIAS STS-ACCOUNT + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Display information for the temporary account 'J123C4ZXEQN8RK6ND35I' + {{.Prompt}} {{.HelpName}} myminio/ J123C4ZXEQN8RK6ND35I +`, +} + +// checkAdminUserSTSAcctInfoSyntax - validate all the passed arguments +func checkAdminUserSTSAcctInfoSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 2 { + showCommandHelpAndExit(ctx, 1) + } +} + +// mainAdminUserSTSAcctInfo is the handle for "mc admin user sts info" command. +func mainAdminUserSTSAcctInfo(ctx *cli.Context) error { + checkAdminUserSTSAcctInfoSyntax(ctx) + + console.SetColor("AccountMessage", color.New(color.FgGreen)) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + stsAccount := args.Get(1) + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + fatalIf(err, "Unable to initialize admin connection.") + + stsInfo, e := client.TemporaryAccountInfo(globalContext, stsAccount) + fatalIf(probe.NewError(e).Trace(args...), "Unable to get information of the specified service account") + + if ctx.Bool("policy") { + if stsInfo.Policy == "" { + fatalIf(errDummy().Trace(args...), "No policy found associated to the specified service account. Check the policy of its parent user.") + } + p, e := iampolicy.ParseConfig(strings.NewReader(stsInfo.Policy)) + fatalIf(probe.NewError(e).Trace(args...), "Unable to parse policy.") + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + fatalIf(probe.NewError(enc.Encode(p)).Trace(args...), "Unable to write policy to stdout.") + return nil + } + + printMsg(acctMessage{ + op: svcAccOpInfo, + AccessKey: stsAccount, + AccountStatus: stsInfo.AccountStatus, + ParentUser: stsInfo.ParentUser, + ImpliedPolicy: stsInfo.ImpliedPolicy, + Policy: json.RawMessage(stsInfo.Policy), + }) + + return nil +} diff --git a/cmd/admin-user-svcacct-add.go b/cmd/admin-user-svcacct-add.go index 1a2e72a7..7b67eddc 100644 --- a/cmd/admin-user-svcacct-add.go +++ b/cmd/admin-user-svcacct-add.go @@ -83,9 +83,9 @@ func checkAdminUserSvcAcctAddSyntax(ctx *cli.Context) { } } -// svcAcctMessage container for content message structure -type svcAcctMessage struct { - op svcAcctOp +// acctMessage container for content message structure +type acctMessage struct { + op acctOp Status string `json:"status"` AccessKey string `json:"accessKey,omitempty"` SecretKey string `json:"secretKey,omitempty"` @@ -101,33 +101,35 @@ const ( accessFieldMaxLen = 20 ) -type svcAcctOp int +type acctOp int const ( - svcAccOpAdd = svcAcctOp(iota) + svcAccOpAdd = acctOp(iota) svcAccOpList svcAccOpInfo svcAccOpRemove svcAccOpDisable svcAccOpEnable svcAccOpSet + + stsAccOpInfo ) -func (u svcAcctMessage) String() string { +func (u acctMessage) String() string { switch u.op { case svcAccOpList: // Create a new pretty table with cols configuration return newPrettyTable(" ", Field{"AccessKey", accessFieldMaxLen}, ).buildRow(u.AccessKey) - case svcAccOpInfo: + case stsAccOpInfo, svcAccOpInfo: policyField := "" if u.ImpliedPolicy { policyField = "implied" } else { policyField = "embedded" } - return console.Colorize("SVCMessage", strings.Join( + return console.Colorize("AccMessage", strings.Join( []string{ fmt.Sprintf("AccessKey: %s", u.AccessKey), fmt.Sprintf("ParentUser: %s", u.ParentUser), @@ -136,21 +138,21 @@ func (u svcAcctMessage) String() string { fmt.Sprintf("Policy: %s", policyField), }, "\n")) case svcAccOpRemove: - return console.Colorize("SVCMessage", "Removed service account `"+u.AccessKey+"` successfully.") + return console.Colorize("AccMessage", "Removed service account `"+u.AccessKey+"` successfully.") case svcAccOpDisable: - return console.Colorize("SVCMessage", "Disabled service account `"+u.AccessKey+"` successfully.") + return console.Colorize("AccMessage", "Disabled service account `"+u.AccessKey+"` successfully.") case svcAccOpEnable: - return console.Colorize("SVCMessage", "Enabled service account `"+u.AccessKey+"` successfully.") + return console.Colorize("AccMessage", "Enabled service account `"+u.AccessKey+"` successfully.") case svcAccOpAdd: - return console.Colorize("SVCMessage", + return console.Colorize("AccMessage", fmt.Sprintf("Access Key: %s\nSecret Key: %s", u.AccessKey, u.SecretKey)) case svcAccOpSet: - return console.Colorize("SVCMessage", "Edited service account `"+u.AccessKey+"` successfully.") + return console.Colorize("AccMessage", "Edited service account `"+u.AccessKey+"` successfully.") } return "" } -func (u svcAcctMessage) JSON() string { +func (u acctMessage) JSON() string { u.Status = "success" jsonMessageBytes, e := json.MarshalIndent(u, "", " ") fatalIf(probe.NewError(e), "Unable to marshal into JSON.") @@ -162,7 +164,7 @@ func (u svcAcctMessage) JSON() string { func mainAdminUserSvcAcctAdd(ctx *cli.Context) error { checkAdminUserSvcAcctAddSyntax(ctx) - console.SetColor("SVCMessage", color.New(color.FgGreen)) + console.SetColor("AccMessage", color.New(color.FgGreen)) // Get the alias parameter from cli args := ctx.Args() @@ -202,7 +204,7 @@ func mainAdminUserSvcAcctAdd(ctx *cli.Context) error { creds, e := client.AddServiceAccount(globalContext, opts) fatalIf(probe.NewError(e).Trace(args...), "Unable to add a new service account") - printMsg(svcAcctMessage{ + printMsg(acctMessage{ op: svcAccOpAdd, AccessKey: creds.AccessKey, SecretKey: creds.SecretKey, diff --git a/cmd/admin-user-svcacct-disable.go b/cmd/admin-user-svcacct-disable.go index a279e43a..d10b2171 100644 --- a/cmd/admin-user-svcacct-disable.go +++ b/cmd/admin-user-svcacct-disable.go @@ -58,7 +58,7 @@ func checkAdminUserSvcAcctDisableSyntax(ctx *cli.Context) { func mainAdminUserSvcAcctDisable(ctx *cli.Context) error { checkAdminUserSvcAcctDisableSyntax(ctx) - console.SetColor("SVCMessage", color.New(color.FgGreen)) + console.SetColor("AccMessage", color.New(color.FgGreen)) // Get the alias parameter from cli args := ctx.Args() @@ -76,7 +76,7 @@ func mainAdminUserSvcAcctDisable(ctx *cli.Context) error { e := client.UpdateServiceAccount(globalContext, svcAccount, opts) fatalIf(probe.NewError(e).Trace(args...), "Unable to disable the specified service account") - printMsg(svcAcctMessage{ + printMsg(acctMessage{ op: svcAccOpDisable, AccessKey: svcAccount, }) diff --git a/cmd/admin-user-svcacct-enable.go b/cmd/admin-user-svcacct-enable.go index f4ede10e..3daaab36 100644 --- a/cmd/admin-user-svcacct-enable.go +++ b/cmd/admin-user-svcacct-enable.go @@ -58,7 +58,7 @@ func checkAdminUserSvcAcctEnableSyntax(ctx *cli.Context) { func mainAdminUserSvcAcctEnable(ctx *cli.Context) error { checkAdminUserSvcAcctEnableSyntax(ctx) - console.SetColor("SVCMessage", color.New(color.FgGreen)) + console.SetColor("AccMessage", color.New(color.FgGreen)) // Get the alias parameter from cli args := ctx.Args() @@ -76,7 +76,7 @@ func mainAdminUserSvcAcctEnable(ctx *cli.Context) error { e := client.UpdateServiceAccount(globalContext, svcAccount, opts) fatalIf(probe.NewError(e).Trace(args...), "Unable to enable the specified service account") - printMsg(svcAcctMessage{ + printMsg(acctMessage{ op: svcAccOpEnable, AccessKey: svcAccount, }) diff --git a/cmd/admin-user-svcacct-info.go b/cmd/admin-user-svcacct-info.go index ba3caa7d..14b33981 100644 --- a/cmd/admin-user-svcacct-info.go +++ b/cmd/admin-user-svcacct-info.go @@ -69,7 +69,7 @@ func checkAdminUserSvcAcctInfoSyntax(ctx *cli.Context) { func mainAdminUserSvcAcctInfo(ctx *cli.Context) error { checkAdminUserSvcAcctInfoSyntax(ctx) - console.SetColor("SVCMessage", color.New(color.FgGreen)) + console.SetColor("AccMessage", color.New(color.FgGreen)) // Get the alias parameter from cli args := ctx.Args() @@ -95,7 +95,7 @@ func mainAdminUserSvcAcctInfo(ctx *cli.Context) error { return nil } - printMsg(svcAcctMessage{ + printMsg(acctMessage{ op: svcAccOpInfo, AccessKey: svcAccount, Comment: svcInfo.Comment, diff --git a/cmd/admin-user-svcacct-list.go b/cmd/admin-user-svcacct-list.go index e3827efb..87fcc7b3 100644 --- a/cmd/admin-user-svcacct-list.go +++ b/cmd/admin-user-svcacct-list.go @@ -61,7 +61,7 @@ func checkAdminUserSvcAcctListSyntax(ctx *cli.Context) { func mainAdminUserSvcAcctList(ctx *cli.Context) error { checkAdminUserSvcAcctListSyntax(ctx) - console.SetColor("SVCMessage", color.New(color.FgGreen)) + console.SetColor("AccMessage", color.New(color.FgGreen)) console.SetColor("AccessKey", color.New(color.FgBlue)) // Get the alias parameter from cli @@ -77,7 +77,7 @@ func mainAdminUserSvcAcctList(ctx *cli.Context) error { fatalIf(probe.NewError(e).Trace(args...), "Unable to list service accounts") for _, svc := range svcList.Accounts { - printMsg(svcAcctMessage{ + printMsg(acctMessage{ op: svcAccOpList, AccessKey: svc, }) diff --git a/cmd/admin-user-svcacct-rm.go b/cmd/admin-user-svcacct-rm.go index ad19fd4f..fe02030f 100644 --- a/cmd/admin-user-svcacct-rm.go +++ b/cmd/admin-user-svcacct-rm.go @@ -56,7 +56,7 @@ func checkAdminUserSvcAcctRemoveSyntax(ctx *cli.Context) { // mainAdminUserSvcAcctRemove is the handle for "mc admin user svcacct rm" command. func mainAdminUserSvcAcctRemove(ctx *cli.Context) error { - console.SetColor("SVCMessage", color.New(color.FgGreen)) + console.SetColor("AccMessage", color.New(color.FgGreen)) checkAdminUserSvcAcctRemoveSyntax(ctx) @@ -72,7 +72,7 @@ func mainAdminUserSvcAcctRemove(ctx *cli.Context) error { e := client.DeleteServiceAccount(globalContext, svcAccount) fatalIf(probe.NewError(e).Trace(args...), "Unable to remove the specified service account") - printMsg(svcAcctMessage{ + printMsg(acctMessage{ op: svcAccOpRemove, AccessKey: svcAccount, }) diff --git a/cmd/admin-user-svcacct-set.go b/cmd/admin-user-svcacct-set.go index 8a558d2b..c12084b1 100644 --- a/cmd/admin-user-svcacct-set.go +++ b/cmd/admin-user-svcacct-set.go @@ -103,7 +103,7 @@ func mainAdminUserSvcAcctSet(ctx *cli.Context) error { e := client.UpdateServiceAccount(globalContext, svcAccount, opts) fatalIf(probe.NewError(e).Trace(args...), "Unable to edit the specified service account") - printMsg(svcAcctMessage{ + printMsg(acctMessage{ op: svcAccOpSet, AccessKey: svcAccount, }) diff --git a/cmd/admin-user.go b/cmd/admin-user.go index c89f6b15..02248660 100644 --- a/cmd/admin-user.go +++ b/cmd/admin-user.go @@ -28,6 +28,7 @@ var adminUserSubcommands = []cli.Command{ adminUserInfoCmd, adminUserPolicyCmd, adminUserSvcAcctCmd, + adminUserSTSAcctCmd, } var adminUserCmd = cli.Command{ diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 21b8e0c7..2d1691f3 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -412,6 +412,8 @@ var completeCmds = map[string]complete.Predictor{ "/admin/user/svcacct/enable": aliasCompleter, "/admin/user/svcacct/disable": aliasCompleter, + "/admin/user/sts/info": aliasCompleter, + "/admin/group/add": aliasCompleter, "/admin/group/disable": aliasCompleter, "/admin/group/enable": aliasCompleter,