github.com/minio/mc@v0.0.0-20240507152021-646712d5e5fb/cmd/ilm-tier-edit.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  
    23  	"github.com/fatih/color"
    24  	"github.com/minio/cli"
    25  	"github.com/minio/madmin-go/v3"
    26  	"github.com/minio/mc/pkg/probe"
    27  	"github.com/minio/pkg/v2/console"
    28  )
    29  
    30  var adminTierEditFlags = []cli.Flag{
    31  	cli.StringFlag{
    32  		Name:  "access-key",
    33  		Value: "",
    34  		Usage: "AWS S3 or compatible object storage access-key",
    35  	},
    36  	cli.StringFlag{
    37  		Name:  "secret-key",
    38  		Value: "",
    39  		Usage: "AWS S3 or compatible object storage secret-key",
    40  	},
    41  	cli.BoolFlag{
    42  		Name:  "use-aws-role",
    43  		Usage: "use AWS S3 role",
    44  	},
    45  	cli.StringFlag{
    46  		Name:  "account-key",
    47  		Value: "",
    48  		Usage: "Azure Blob Storage account key",
    49  	},
    50  	cli.StringFlag{
    51  		Name:  "credentials-file",
    52  		Value: "",
    53  		Usage: "path to Google Cloud Storage credentials file",
    54  	},
    55  }
    56  
    57  var adminTierEditCmd = cli.Command{
    58  	Name:         "edit",
    59  	Usage:        "update an existing remote tier configuration",
    60  	Action:       mainAdminTierEdit,
    61  	Hidden:       true,
    62  	OnUsageError: onUsageError,
    63  	Before:       setGlobalsFromContext,
    64  	Flags:        append(globalFlags, adminTierEditFlags...),
    65  	CustomHelpTemplate: `NAME:
    66    {{.HelpName}} - {{.Usage}}
    67  
    68  USAGE:
    69    {{.HelpName}} ALIAS NAME
    70  
    71  NAME:
    72    Name of remote tier. e.g WARM-TIER
    73  
    74  FLAGS:
    75    {{range .VisibleFlags}}{{.}}
    76    {{end}}
    77  EXAMPLES:
    78    1. Update credentials for an existing Azure Blob Storage remote tier:
    79       {{.Prompt}} {{.HelpName}} myminio AZTIER --account-key ACCOUNT-KEY
    80  
    81    2. Update credentials for an existing AWS S3 compatible remote tier:
    82       {{.Prompt}} {{.HelpName}} myminio S3TIER --access-key ACCESS-KEY --secret-key SECRET-KEY
    83  
    84    3. Update credentials for an existing Google Cloud Storage remote tier:
    85       {{.Prompt}} {{.HelpName}} myminio GCSTIER --credentials-file /path/to/credentials.json
    86  `,
    87  }
    88  
    89  // checkAdminTierEditSyntax - validate all the postitional arguments
    90  func checkAdminTierEditSyntax(ctx *cli.Context) {
    91  	argsNr := len(ctx.Args())
    92  	if argsNr < 2 {
    93  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    94  	}
    95  	if argsNr > 2 {
    96  		fatalIf(errInvalidArgument().Trace(ctx.Args().Tail()...),
    97  			"Incorrect number of arguments for tier-edit subcommand.")
    98  	}
    99  }
   100  
   101  func mainAdminTierEdit(ctx *cli.Context) error {
   102  	checkAdminTierEditSyntax(ctx)
   103  
   104  	console.SetColor("TierMessage", color.New(color.FgGreen))
   105  
   106  	args := ctx.Args()
   107  	aliasedURL := args.Get(0)
   108  	tierName := args.Get(1)
   109  
   110  	// Create a new MinIO Admin Client
   111  	client, cerr := newAdminClient(aliasedURL)
   112  	fatalIf(cerr, "Unable to initialize admin connection.")
   113  
   114  	var creds madmin.TierCreds
   115  	accessKey := ctx.String("access-key")
   116  	secretKey := ctx.String("secret-key")
   117  	accountKey := ctx.String("account-key")
   118  	credsPath := ctx.String("credentials-file")
   119  	useAwsRole := ctx.IsSet("use-aws-role")
   120  
   121  	switch {
   122  	case accessKey != "" && secretKey != "" && !useAwsRole: // S3 tier
   123  		creds.AccessKey = accessKey
   124  		creds.SecretKey = secretKey
   125  	case useAwsRole:
   126  		creds.AWSRole = true
   127  	case accountKey != "": // Azure tier
   128  		creds.SecretKey = accountKey
   129  	case credsPath != "": // GCS tier
   130  		credsBytes, e := os.ReadFile(credsPath)
   131  		fatalIf(probe.NewError(e), "Unable to read credentials file at %s", credsPath)
   132  
   133  		creds.CredsJSON = credsBytes
   134  	default:
   135  		fatalIf(errInvalidArgument().Trace(args.Tail()...), "Insufficient credential information supplied to update remote tier target credentials")
   136  	}
   137  
   138  	e := client.EditTier(globalContext, tierName, creds)
   139  	fatalIf(probe.NewError(e).Trace(args...), "Unable to edit remote tier")
   140  
   141  	printMsg(&tierMessage{
   142  		op:       ctx.Command.Name,
   143  		Status:   "success",
   144  		TierName: tierName,
   145  	})
   146  	return nil
   147  }