1// Copyright (c) 2015-2021 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
18package cmd
19
20import (
21	"github.com/fatih/color"
22	"github.com/minio/cli"
23	"github.com/minio/mc/pkg/probe"
24	"github.com/minio/pkg/console"
25)
26
27var adminPolicyInfoCmd = cli.Command{
28	Name:         "info",
29	Usage:        "show info on a policy",
30	Action:       mainAdminPolicyInfo,
31	OnUsageError: onUsageError,
32	Before:       setGlobalsFromContext,
33	Flags:        globalFlags,
34	CustomHelpTemplate: `NAME:
35  {{.HelpName}} - {{.Usage}}
36
37USAGE:
38  {{.HelpName}} TARGET POLICYNAME
39
40POLICYNAME:
41  Name of the policy on the MinIO server.
42
43FLAGS:
44  {{range .VisibleFlags}}{{.}}
45  {{end}}
46EXAMPLES:
47  1. Show information on a given policy.
48     {{.Prompt}} {{.HelpName}} myminio writeonly
49`,
50}
51
52// checkAdminPolicyInfoSyntax - validate all the passed arguments
53func checkAdminPolicyInfoSyntax(ctx *cli.Context) {
54	if len(ctx.Args()) != 2 {
55		cli.ShowCommandHelpAndExit(ctx, "info", 1) // last argument is exit code
56	}
57}
58
59// mainAdminPolicyInfo is the handler for "mc admin policy info" command.
60func mainAdminPolicyInfo(ctx *cli.Context) error {
61	checkAdminPolicyInfoSyntax(ctx)
62
63	console.SetColor("PolicyMessage", color.New(color.FgGreen))
64	console.SetColor("Policy", color.New(color.FgBlue))
65
66	// Get the alias parameter from cli
67	args := ctx.Args()
68	aliasedURL := args.Get(0)
69	policyName := args.Get(1)
70
71	// Create a new MinIO Admin Client
72	client, err := newAdminClient(aliasedURL)
73	fatalIf(err, "Unable to initialize admin connection")
74
75	buf, e := client.InfoCannedPolicy(globalContext, policyName)
76	fatalIf(probe.NewError(e).Trace(args...), "Unable to fetch policy")
77
78	printMsg(userPolicyMessage{
79		op:         "info",
80		Policy:     policyName,
81		PolicyJSON: buf,
82	})
83
84	return nil
85}
86