github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/admin-cluster-iam-import.go (about)

     1  // Copyright (c) 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  	"context"
    22  	"fmt"
    23  	"io"
    24  	"os"
    25  	"path/filepath"
    26  
    27  	"github.com/klauspost/compress/zip"
    28  	"github.com/minio/cli"
    29  	"github.com/minio/mc/pkg/probe"
    30  	"github.com/minio/pkg/v2/console"
    31  )
    32  
    33  var adminClusterIAMImportCmd = cli.Command{
    34  	Name:            "import",
    35  	Usage:           "imports IAM info from zipped file",
    36  	Action:          mainClusterIAMImport,
    37  	OnUsageError:    onUsageError,
    38  	Before:          setGlobalsFromContext,
    39  	Flags:           globalFlags,
    40  	HideHelpCommand: true,
    41  	CustomHelpTemplate: `NAME:
    42    {{.HelpName}} - {{.Usage}}
    43  
    44  USAGE:
    45    {{.HelpName}} [FLAGS] TARGET/BUCKET /path/to/myminio-iam-info.zip
    46  
    47  FLAGS:
    48    {{range .VisibleFlags}}{{.}}
    49    {{end}}
    50  EXAMPLES:
    51    1. Set IAM info from previously exported metadata zip file.
    52       {{.Prompt}} {{.HelpName}} myminio /tmp/myminio-iam-info.zip
    53  
    54  `,
    55  }
    56  
    57  func checkIAMImportSyntax(ctx *cli.Context) {
    58  	if len(ctx.Args()) != 2 {
    59  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    60  	}
    61  }
    62  
    63  // mainClusterIAMImport - iam info import command
    64  func mainClusterIAMImport(ctx *cli.Context) error {
    65  	// Check for command syntax
    66  	checkIAMImportSyntax(ctx)
    67  
    68  	// Get the alias parameter from cli
    69  	args := ctx.Args()
    70  	aliasedURL := filepath.ToSlash(args.Get(0))
    71  	aliasedURL = filepath.Clean(aliasedURL)
    72  
    73  	var r io.Reader
    74  	var sz int64
    75  	f, e := os.Open(args.Get(1))
    76  	if e != nil {
    77  		fatalIf(probe.NewError(e).Trace(args...), "Unable to get IAM info")
    78  	}
    79  	if st, e := f.Stat(); e == nil {
    80  		sz = st.Size()
    81  	}
    82  	defer f.Close()
    83  	r = f
    84  
    85  	_, e = zip.NewReader(r.(io.ReaderAt), sz)
    86  	fatalIf(probe.NewError(e).Trace(args...), fmt.Sprintf("Unable to read zip file %s", args.Get(1)))
    87  
    88  	f, e = os.Open(args.Get(1))
    89  	fatalIf(probe.NewError(e).Trace(args...), "Unable to get IAM info")
    90  
    91  	// Create a new MinIO Admin Client
    92  	client, err := newAdminClient(aliasedURL)
    93  	if err != nil {
    94  		fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.")
    95  		return nil
    96  	}
    97  
    98  	e = client.ImportIAM(context.Background(), f)
    99  	fatalIf(probe.NewError(e).Trace(aliasedURL), "Unable to import IAM info.")
   100  
   101  	if !globalJSON {
   102  		console.Infof("IAM info imported to %s from %s\n", aliasedURL, args.Get(1))
   103  	}
   104  	return nil
   105  }