From b1d8ca83f20ee0d560ca7cdd7ef662f6598d6a54 Mon Sep 17 00:00:00 2001 From: Poorna Date: Sun, 26 Jun 2022 11:36:48 -0700 Subject: [PATCH] admin: add admin cluster iam import/export cmd (#4102) --- cmd/admin-cluster-iam-export.go | 125 ++++++++++++++++++++++++++++++++ cmd/admin-cluster-iam-import.go | 101 ++++++++++++++++++++++++++ cmd/admin-cluster-iam.go | 42 +++++++++++ cmd/admin-cluster.go | 1 + cmd/auto-complete.go | 2 + 5 files changed, 271 insertions(+) create mode 100644 cmd/admin-cluster-iam-export.go create mode 100644 cmd/admin-cluster-iam-import.go create mode 100644 cmd/admin-cluster-iam.go diff --git a/cmd/admin-cluster-iam-export.go b/cmd/admin-cluster-iam-export.go new file mode 100644 index 00000000..91f56eb9 --- /dev/null +++ b/cmd/admin-cluster-iam-export.go @@ -0,0 +1,125 @@ +// Copyright (c) 2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "fmt" + "io" + "io/ioutil" + "os" + "time" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var adminClusterIAMExportCmd = cli.Command{ + Name: "export", + Usage: "exports IAM info to zipped file", + Action: mainClusterIAMExport, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Download all IAM metadata for cluster into zip file. + {{.Prompt}} {{.HelpName}} myminio +`, +} + +func checkIAMExportSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "export", 1) // last argument is exit code + } +} + +// mainClusterIAMExport - metadata export command +func mainClusterIAMExport(ctx *cli.Context) error { + // Check for command syntax + checkIAMExportSyntax(ctx) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + console.SetColor("File", color.New(color.FgWhite, color.Bold)) + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + if err != nil { + fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.") + return nil + } + + r, ierr := client.ExportIAM(context.Background()) + fatalIf(probe.NewError(ierr).Trace(aliasedURL), "Unable to export IAM info.") + + // Create iam info zip file + tmpFile, e := ioutil.TempFile("", fmt.Sprintf("%s-iam-info", aliasedURL)) + fatalIf(probe.NewError(e), "Unable to download file data.") + + ext := "zip" + // Copy zip content to target download file + _, e = io.Copy(tmpFile, r) + fatalIf(probe.NewError(e), "Unable to download IAM info.") + + // Close everything + r.Close() + tmpFile.Close() + + downloadPath := fmt.Sprintf("%s-iam-info.%s", aliasedURL, ext) + fi, e := os.Stat(downloadPath) + if e == nil && !fi.IsDir() { + e = moveFile(downloadPath, downloadPath+"."+time.Now().Format(dateTimeFormatFilename)) + fatalIf(probe.NewError(e), "Unable to create a backup of "+downloadPath) + } else { + if !os.IsNotExist(e) { + fatal(probe.NewError(e), "Unable to download file data") + } + } + + fatalIf(probe.NewError(moveFile(tmpFile.Name(), downloadPath)), "Unable to rename downloaded data, file exists at %s", tmpFile.Name()) + + if !globalJSON { + console.Infof("IAM info successfully downloaded as %s\n", downloadPath) + return nil + } + + v := struct { + File string `json:"file"` + Key string `json:"key,omitempty"` + }{ + File: downloadPath, + } + b, e := json.Marshal(v) + fatalIf(probe.NewError(e), "Unable to serialize data") + console.Println(string(b)) + return nil +} diff --git a/cmd/admin-cluster-iam-import.go b/cmd/admin-cluster-iam-import.go new file mode 100644 index 00000000..20487b61 --- /dev/null +++ b/cmd/admin-cluster-iam-import.go @@ -0,0 +1,101 @@ +// Copyright (c) 2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import ( + "context" + "fmt" + "io" + "os" + + "github.com/klauspost/compress/zip" + "github.com/minio/cli" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" +) + +var adminClusterIAMImportCmd = cli.Command{ + Name: "import", + Usage: "imports IAM info from zipped file", + Action: mainClusterIAMImport, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: globalFlags, + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET/BUCKET /path/to/myminio-iam-info.zip + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Set IAM info from previously exported metadata zip file. + {{.Prompt}} {{.HelpName}} myminio /tmp/myminio-iam-info.zip + +`, +} + +func checkIAMImportSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 2 { + cli.ShowCommandHelpAndExit(ctx, "import", 1) // last argument is exit code + } +} + +// mainClusterIAMImport - iam info import command +func mainClusterIAMImport(ctx *cli.Context) error { + // Check for command syntax + checkIAMImportSyntax(ctx) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + var r io.Reader + var sz int64 + f, e := os.Open(args.Get(1)) + if e != nil { + fatalIf(probe.NewError(e).Trace(args...), "Unable to get IAM info") + } + if st, e := f.Stat(); e == nil { + sz = st.Size() + } + defer f.Close() + r = f + + _, e = zip.NewReader(r.(io.ReaderAt), sz) + fatalIf(probe.NewError(e).Trace(args...), fmt.Sprintf("Unable to read zip file %s", args.Get(1))) + + f, e = os.Open(args.Get(1)) + fatalIf(probe.NewError(e).Trace(args...), "Unable to get IAM info") + + // Create a new MinIO Admin Client + client, err := newAdminClient(aliasedURL) + if err != nil { + fatalIf(err.Trace(aliasedURL), "Unable to initialize admin client.") + return nil + } + + ierr := client.ImportIAM(context.Background(), f) + fatalIf(probe.NewError(ierr).Trace(aliasedURL), "Unable to import IAM info.") + if !globalJSON { + console.Infof("IAM info imported to %s from %s\n", aliasedURL, args.Get(1)) + } + return nil +} diff --git a/cmd/admin-cluster-iam.go b/cmd/admin-cluster-iam.go new file mode 100644 index 00000000..45b12be0 --- /dev/null +++ b/cmd/admin-cluster-iam.go @@ -0,0 +1,42 @@ +// Copyright (c) 2022 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package cmd + +import "github.com/minio/cli" + +var adminClusterIAMSubcommands = []cli.Command{ + adminClusterIAMImportCmd, + adminClusterIAMExportCmd, +} + +var adminClusterIAMCmd = cli.Command{ + Name: "iam", + Usage: "manage IAM info on MinIO cluster", + Action: mainadminClusterIAM, + Before: setGlobalsFromContext, + Flags: globalFlags, + Subcommands: adminClusterIAMSubcommands, + HideHelpCommand: true, +} + +// mainadminClusterIAM is the handle for "mc admin cluster bucket" command. +func mainadminClusterIAM(ctx *cli.Context) error { + commandNotFound(ctx, adminClusterIAMSubcommands) + return nil + // Sub-commands like "export", "import" have their own main. +} diff --git a/cmd/admin-cluster.go b/cmd/admin-cluster.go index 71c781f9..89ee5cc6 100644 --- a/cmd/admin-cluster.go +++ b/cmd/admin-cluster.go @@ -21,6 +21,7 @@ import "github.com/minio/cli" var adminClusterSubcommands = []cli.Command{ adminClusterBucketCmd, + adminClusterIAMCmd, } var adminClusterCmd = cli.Command{ diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 89ac9a52..06b03083 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -403,6 +403,8 @@ var completeCmds = map[string]complete.Predictor{ "/admin/cluster/bucket/export": aliasCompleter, "/admin/cluster/bucket/import": aliasCompleter, + "/admin/cluster/iam/export": aliasCompleter, + "/admin/cluster/iam/import": aliasCompleter, "/alias/set": nil, "/alias/list": aliasCompleter,