github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-policy-entities.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  	"github.com/minio/cli"
    22  	"github.com/minio/madmin-go/v3"
    23  	"github.com/minio/mc/pkg/probe"
    24  )
    25  
    26  var adminPolicyEntitiesFlags = []cli.Flag{
    27  	cli.StringSliceFlag{
    28  		Name:  "user, u",
    29  		Usage: "list policies associated with user(s)",
    30  	},
    31  	cli.StringSliceFlag{
    32  		Name:  "group, g",
    33  		Usage: "list policies associated with group(s)",
    34  	},
    35  	cli.StringSliceFlag{
    36  		Name:  "policy, p",
    37  		Usage: "list users or groups associated with policy",
    38  	},
    39  }
    40  
    41  var adminPolicyEntitiesCmd = cli.Command{
    42  	Name:         "entities",
    43  	Usage:        "list policy association entities",
    44  	Action:       mainAdminPolicyEntities,
    45  	Before:       setGlobalsFromContext,
    46  	Flags:        append(adminPolicyEntitiesFlags, globalFlags...),
    47  	OnUsageError: onUsageError,
    48  	CustomHelpTemplate: `NAME:
    49    {{.HelpName}} - {{.Usage}}
    50    
    51  USAGE:
    52    {{.HelpName}} [FLAGS] TARGET
    53  
    54  FLAGS:
    55    {{range .VisibleFlags}}{{.}}
    56    {{end}}
    57  EXAMPLES:
    58    1. List all entities associated with all policies
    59       {{.Prompt}} {{.HelpName}} play/
    60    2. List all entities associated with the policies 'finteam-policy' and 'mlteam-policy'
    61       {{.Prompt}} {{.HelpName}} play/ --policy finteam-policy --policy mlteam-policy
    62    3. List all policies associated with a pair of user entities
    63       {{.Prompt}} {{.HelpName}} play/ --user bob --user james
    64    4. List all policies associated with a pair of group entities
    65       {{.Prompt}} {{.HelpName}} play/ --group auditors --group accounting
    66    5. List all entities associated with a policy, group and user
    67       {{.Prompt}} {{.HelpName}} play/ \
    68                --policy finteam-policy --user bobfisher --group consulting
    69  `,
    70  }
    71  
    72  // mainAdminPolicyEntities is the handler for "mc admin policy entities" command.
    73  func mainAdminPolicyEntities(ctx *cli.Context) error {
    74  	if len(ctx.Args()) != 1 {
    75  		showCommandHelpAndExit(ctx, 1)
    76  	}
    77  
    78  	usersToQuery := ctx.StringSlice("user")
    79  	groupsToQuery := ctx.StringSlice("group")
    80  	policiesToQuery := ctx.StringSlice("policy")
    81  
    82  	args := ctx.Args()
    83  
    84  	aliasedURL := args.Get(0)
    85  
    86  	// Create a new MinIO Admin Client
    87  	client, err := newAdminClient(aliasedURL)
    88  	fatalIf(err, "Unable to initialize admin connection.")
    89  
    90  	res, e := client.GetPolicyEntities(globalContext,
    91  		madmin.PolicyEntitiesQuery{
    92  			Users:  usersToQuery,
    93  			Groups: groupsToQuery,
    94  			Policy: policiesToQuery,
    95  		})
    96  	fatalIf(probe.NewError(e), "Unable to fetch policy entities")
    97  
    98  	printMsg(policyEntitiesFrom(res))
    99  	return nil
   100  }