github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/cmd/admin-kms-key-list.go (about)

     1  // Copyright (c) 2015-2023 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  	"fmt"
    22  	"os"
    23  
    24  	"github.com/fatih/color"
    25  	"github.com/jedib0t/go-pretty/v6/table"
    26  	"github.com/jedib0t/go-pretty/v6/text"
    27  	"github.com/minio/cli"
    28  	json "github.com/minio/colorjson"
    29  	"github.com/minio/mc/pkg/probe"
    30  	"github.com/minio/pkg/v2/console"
    31  )
    32  
    33  var adminKMSKeyListCmd = cli.Command{
    34  	Name:         "list",
    35  	Usage:        "request list of KMS master keys",
    36  	Action:       mainAdminKMSKeyList,
    37  	OnUsageError: onUsageError,
    38  	Before:       setGlobalsFromContext,
    39  	Flags:        globalFlags,
    40  	CustomHelpTemplate: `NAME:
    41    {{.HelpName}} - {{.Usage}}
    42  
    43  USAGE:
    44    {{.HelpName}} TARGET
    45  
    46  FLAGS:
    47    {{range .VisibleFlags}}{{.}}
    48    {{end}}
    49  EXAMPLES:
    50    1. Get list of master keys from a MinIO server/cluster.
    51       $ {{.HelpName}} play
    52  `,
    53  }
    54  
    55  // adminKMSKeyCmd is the handle for the "mc admin kms key" command.
    56  func mainAdminKMSKeyList(ctx *cli.Context) error {
    57  	if len(ctx.Args()) == 0 || len(ctx.Args()) > 1 {
    58  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    59  	}
    60  
    61  	console.SetColor("KeyName", color.New(color.FgBlue))
    62  
    63  	// Get the alias parameter from cli
    64  	args := ctx.Args()
    65  	aliasedURL := args.Get(0)
    66  
    67  	// Create a new MinIO Admin Client
    68  	client, err := newAdminClient(aliasedURL)
    69  	fatalIf(err, "Unable to initialize admin connection.")
    70  
    71  	keys, e := client.ListKeys(globalContext, "*")
    72  	fatalIf(probe.NewError(e).Trace(args...), "Unable to list KMS keys")
    73  
    74  	var rows []table.Row
    75  	kmsKeys := []string{}
    76  	for idx, k := range keys {
    77  		rows = append(rows, table.Row{idx + 1, k.Name})
    78  		kmsKeys = append(kmsKeys, k.Name)
    79  	}
    80  
    81  	if globalJSON {
    82  		printMsg(kmsKeysMsg{
    83  			Status: "success",
    84  			Target: aliasedURL,
    85  			Keys:   kmsKeys,
    86  		})
    87  		return nil
    88  	}
    89  
    90  	t := table.NewWriter()
    91  	t.SetOutputMirror(os.Stdout)
    92  	t.SetColumnConfigs([]table.ColumnConfig{{Align: text.AlignCenter}})
    93  	t.SetTitle("KMS Keys")
    94  	t.AppendHeader(table.Row{"S N", "Name"})
    95  	t.AppendRows(rows)
    96  	t.SetStyle(table.StyleLight)
    97  	t.Render()
    98  	return nil
    99  }
   100  
   101  type kmsKeysMsg struct {
   102  	Status string   `json:"status"`
   103  	Target string   `json:"target"`
   104  	Keys   []string `json:"keys"`
   105  }
   106  
   107  func (k kmsKeysMsg) JSON() string {
   108  	kmsBytes, e := json.MarshalIndent(k, "", "    ")
   109  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
   110  
   111  	return string(kmsBytes)
   112  }
   113  
   114  func (k kmsKeysMsg) String() string {
   115  	return fmt.Sprintf("Keys: %s\n", k.Keys)
   116  }