github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-user-sts-info.go (about)

     1  // Copyright (c) 2015-2022 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package cmd
    19  
    20  import (
    21  	"os"
    22  	"strings"
    23  
    24  	"github.com/fatih/color"
    25  	"github.com/minio/cli"
    26  	json "github.com/minio/colorjson"
    27  	"github.com/minio/mc/pkg/probe"
    28  	"github.com/minio/pkg/v2/console"
    29  	"github.com/minio/pkg/v2/policy"
    30  )
    31  
    32  var adminUserSTSAcctSubcommands = []cli.Command{
    33  	adminUserSTSAcctInfoCmd,
    34  }
    35  
    36  var adminUserSTSAcctCmd = cli.Command{
    37  	Name:            "sts",
    38  	Usage:           "manage STS accounts",
    39  	Action:          mainAdminUserSTSAcct,
    40  	Before:          setGlobalsFromContext,
    41  	Flags:           globalFlags,
    42  	Subcommands:     adminUserSTSAcctSubcommands,
    43  	HideHelpCommand: true,
    44  }
    45  
    46  // mainAdminUserSTSAcct is the handle for "mc admin user sts" command.
    47  func mainAdminUserSTSAcct(ctx *cli.Context) error {
    48  	commandNotFound(ctx, adminUserSTSAcctSubcommands)
    49  	return nil
    50  }
    51  
    52  var adminUserSTSAcctInfoFlags = []cli.Flag{
    53  	cli.BoolFlag{
    54  		Name:  "policy",
    55  		Usage: "print policy in JSON format",
    56  	},
    57  }
    58  
    59  var adminUserSTSAcctInfoCmd = cli.Command{
    60  	Name:         "info",
    61  	Usage:        "display temporary account info",
    62  	Action:       mainAdminUserSTSAcctInfo,
    63  	OnUsageError: onUsageError,
    64  	Before:       setGlobalsFromContext,
    65  	Flags:        append(adminUserSTSAcctInfoFlags, globalFlags...),
    66  	CustomHelpTemplate: `NAME:
    67    {{.HelpName}} - {{.Usage}}
    68  
    69  USAGE:
    70    {{.HelpName}} ALIAS STS-ACCOUNT
    71  
    72  FLAGS:
    73    {{range .VisibleFlags}}{{.}}
    74    {{end}}
    75  EXAMPLES:
    76    1. Display information for the temporary account 'J123C4ZXEQN8RK6ND35I'
    77       {{.Prompt}} {{.HelpName}} myminio/ J123C4ZXEQN8RK6ND35I
    78  `,
    79  }
    80  
    81  // checkAdminUserSTSAcctInfoSyntax - validate all the passed arguments
    82  func checkAdminUserSTSAcctInfoSyntax(ctx *cli.Context) {
    83  	if len(ctx.Args()) != 2 {
    84  		showCommandHelpAndExit(ctx, 1)
    85  	}
    86  }
    87  
    88  // mainAdminUserSTSAcctInfo is the handle for "mc admin user sts info" command.
    89  func mainAdminUserSTSAcctInfo(ctx *cli.Context) error {
    90  	checkAdminUserSTSAcctInfoSyntax(ctx)
    91  
    92  	console.SetColor("AccountMessage", color.New(color.FgGreen))
    93  
    94  	// Get the alias parameter from cli
    95  	args := ctx.Args()
    96  	aliasedURL := args.Get(0)
    97  	stsAccount := args.Get(1)
    98  
    99  	// Create a new MinIO Admin Client
   100  	client, err := newAdminClient(aliasedURL)
   101  	fatalIf(err, "Unable to initialize admin connection.")
   102  
   103  	stsInfo, e := client.TemporaryAccountInfo(globalContext, stsAccount)
   104  	fatalIf(probe.NewError(e).Trace(args...), "Unable to get information of the specified service account")
   105  
   106  	if ctx.Bool("policy") {
   107  		if stsInfo.Policy == "" {
   108  			fatalIf(errDummy().Trace(args...), "No policy found associated to the specified service account. Check the policy of its parent user.")
   109  		}
   110  		p, e := policy.ParseConfig(strings.NewReader(stsInfo.Policy))
   111  		fatalIf(probe.NewError(e).Trace(args...), "Unable to parse policy.")
   112  		enc := json.NewEncoder(os.Stdout)
   113  		enc.SetIndent("", " ")
   114  		fatalIf(probe.NewError(enc.Encode(p)).Trace(args...), "Unable to write policy to stdout.")
   115  		return nil
   116  	}
   117  
   118  	printMsg(acctMessage{
   119  		op:            svcAccOpInfo,
   120  		AccessKey:     stsAccount,
   121  		AccountStatus: stsInfo.AccountStatus,
   122  		ParentUser:    stsInfo.ParentUser,
   123  		ImpliedPolicy: stsInfo.ImpliedPolicy,
   124  		Policy:        json.RawMessage(stsInfo.Policy),
   125  		Expiration:    stsInfo.Expiration,
   126  	})
   127  
   128  	return nil
   129  }