github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-user-svcacct-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 adminUserSvcAcctInfoFlags = []cli.Flag{
    33  	cli.BoolFlag{
    34  		Name:  "policy",
    35  		Usage: "print policy in JSON format",
    36  	},
    37  }
    38  
    39  var adminUserSvcAcctInfoCmd = cli.Command{
    40  	Name:         "info",
    41  	Usage:        "display service account info",
    42  	Action:       mainAdminUserSvcAcctInfo,
    43  	OnUsageError: onUsageError,
    44  	Before:       setGlobalsFromContext,
    45  	Flags:        append(adminUserSvcAcctInfoFlags, globalFlags...),
    46  	CustomHelpTemplate: `NAME:
    47    {{.HelpName}} - {{.Usage}}
    48  
    49  USAGE:
    50    {{.HelpName}} ALIAS SERVICE-ACCOUNT
    51  
    52  FLAGS:
    53    {{range .VisibleFlags}}{{.}}
    54    {{end}}
    55  EXAMPLES:
    56    1. Display information for service account 'J123C4ZXEQN8RK6ND35I'
    57       {{.Prompt}} {{.HelpName}} myminio/ J123C4ZXEQN8RK6ND35I
    58  `,
    59  }
    60  
    61  // checkAdminUserSvcAcctInfoSyntax - validate all the passed arguments
    62  func checkAdminUserSvcAcctInfoSyntax(ctx *cli.Context) {
    63  	if len(ctx.Args()) != 2 {
    64  		showCommandHelpAndExit(ctx, 1)
    65  	}
    66  }
    67  
    68  // mainAdminUserSvcAcctInfo is the handle for "mc admin user svcacct info" command.
    69  func mainAdminUserSvcAcctInfo(ctx *cli.Context) error {
    70  	checkAdminUserSvcAcctInfoSyntax(ctx)
    71  
    72  	console.SetColor("AccMessage", color.New(color.FgGreen))
    73  
    74  	// Get the alias parameter from cli
    75  	args := ctx.Args()
    76  	aliasedURL := args.Get(0)
    77  	svcAccount := args.Get(1)
    78  
    79  	// Create a new MinIO Admin Client
    80  	client, err := newAdminClient(aliasedURL)
    81  	fatalIf(err, "Unable to initialize admin connection.")
    82  
    83  	svcInfo, e := client.InfoServiceAccount(globalContext, svcAccount)
    84  	fatalIf(probe.NewError(e).Trace(args...), "Unable to get information of the specified service account")
    85  
    86  	if ctx.Bool("policy") {
    87  		if svcInfo.Policy == "" {
    88  			fatalIf(errDummy().Trace(args...), "No policy found associated to the specified service account. Check the policy of its parent user.")
    89  		}
    90  		p, e := policy.ParseConfig(strings.NewReader(svcInfo.Policy))
    91  		fatalIf(probe.NewError(e).Trace(args...), "Unable to parse policy.")
    92  		enc := json.NewEncoder(os.Stdout)
    93  		enc.SetIndent("", " ")
    94  		fatalIf(probe.NewError(enc.Encode(p)).Trace(args...), "Unable to write policy to stdout.")
    95  		return nil
    96  	}
    97  
    98  	printMsg(acctMessage{
    99  		op:            svcAccOpInfo,
   100  		AccessKey:     svcAccount,
   101  		Name:          svcInfo.Name,
   102  		Description:   svcInfo.Description,
   103  		AccountStatus: svcInfo.AccountStatus,
   104  		ParentUser:    svcInfo.ParentUser,
   105  		ImpliedPolicy: svcInfo.ImpliedPolicy,
   106  		Policy:        json.RawMessage(svcInfo.Policy),
   107  		Expiration:    svcInfo.Expiration,
   108  	})
   109  
   110  	return nil
   111  }