From 7b933438031c4da305edfc5f7087ff8eaf44c58b Mon Sep 17 00:00:00 2001 From: Shireesh Anjal <355479+anjalshireesh@users.noreply.github.com> Date: Mon, 14 Feb 2022 05:08:06 +0530 Subject: [PATCH] Move `admin inspect` to `support inspect` (#3971) --- cmd/admin-inspect.go | 171 ++---------------------------------- cmd/auto-complete.go | 1 + cmd/support-inspect.go | 193 +++++++++++++++++++++++++++++++++++++++++ cmd/support.go | 1 + 4 files changed, 204 insertions(+), 162 deletions(-) create mode 100644 cmd/support-inspect.go diff --git a/cmd/admin-inspect.go b/cmd/admin-inspect.go index 90d72e36..e31d11b5 100644 --- a/cmd/admin-inspect.go +++ b/cmd/admin-inspect.go @@ -18,176 +18,23 @@ package cmd import ( - "context" - "encoding/binary" - "encoding/hex" - "fmt" - "hash/crc32" - "io" - "io/ioutil" - "os" - "path/filepath" - "runtime" - "strings" - "time" - - "github.com/fatih/color" "github.com/minio/cli" - json "github.com/minio/colorjson" - "github.com/minio/madmin-go" - "github.com/minio/mc/pkg/probe" "github.com/minio/pkg/console" - "github.com/secure-io/sio-go" ) -var adminInspectFlags = []cli.Flag{ - cli.BoolFlag{ - Name: "encrypt", - Usage: "Encrypt content with one time key for confidential data", - }, -} - var adminInspectCmd = cli.Command{ - Name: "inspect", - Usage: "inspect files on MinIO server", - Action: mainAdminInspect, - OnUsageError: onUsageError, - Before: setGlobalsFromContext, - Flags: append(adminInspectFlags, globalFlags...), - HideHelpCommand: true, - CustomHelpTemplate: `NAME: - {{.HelpName}} - {{.Usage}} - -USAGE: - {{.HelpName}} [FLAGS] TARGET - -FLAGS: - {{range .VisibleFlags}}{{.}} - {{end}} -EXAMPLES: - 1. Download 'xl.meta' for a specific object from all the drives in a zip file. - {{.Prompt}} {{.HelpName}} myminio/bucket/test*/xl.meta - - 2. Download all constituent parts for a specific object, and optionally encrypt the downloaded zip. - {{.Prompt}} {{.HelpName}} --encrypt myminio/bucket/test*/*/part.* - - 3. Download recursively all objects at a prefix. NOTE: This can be an expensive operation use it with caution. - {{.Prompt}} {{.HelpName}} myminio/bucket/test/** -`, -} - -func checkAdminInspectSyntax(ctx *cli.Context) { - if len(ctx.Args()) != 1 { - cli.ShowCommandHelpAndExit(ctx, "inspect", 1) // last argument is exit code - } + Name: "inspect", + Usage: "inspect files on MinIO server", + Action: mainAdminInspect, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + HideHelpCommand: true, + Hidden: true, + CustomHelpTemplate: "Please use 'mc support inspect'", } // mainAdminHeal - the entry function of heal command func mainAdminInspect(ctx *cli.Context) error { - // Check for command syntax - checkAdminInspectSyntax(ctx) - - // Get the alias parameter from cli - args := ctx.Args() - aliasedURL := args.Get(0) - encrypt := ctx.Bool("encrypt") - - console.SetColor("File", color.New(color.FgWhite, color.Bold)) - console.SetColor("Key", color.New(color.FgHiRed, 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 - } - - // Compute bucket and object from the aliased URL - aliasedURL = filepath.ToSlash(aliasedURL) - splits := splitStr(aliasedURL, "/", 3) - bucket, prefix := splits[1], splits[2] - - shellName, _ := getShellName() - if runtime.GOOS != "windows" && shellName != "bash" && strings.Contains(prefix, "*") { - console.Infoln("Your shell is auto determined as '" + shellName + "', wildcard patterns are only supported with 'bash' SHELL.") - } - - key, r, ierr := client.Inspect(context.Background(), madmin.InspectOptions{ - Volume: bucket, - File: prefix, - }) - fatalIf(probe.NewError(ierr).Trace(aliasedURL), "Unable to inspect file.") - - // Create profile zip file - tmpFile, e := ioutil.TempFile("", "mc-inspect-") - fatalIf(probe.NewError(e), "Unable to download file data.") - - ext := "enc" - if !encrypt { - ext = "zip" - r = decryptInspect(key, r) - } - - // Copy zip content to target download file - _, e = io.Copy(tmpFile, r) - fatalIf(probe.NewError(e), "Unable to download file data.") - - // Close everything - r.Close() - tmpFile.Close() - - // Create an id that is also crc. - var id [4]byte - binary.LittleEndian.PutUint32(id[:], crc32.ChecksumIEEE(key[:])) - - // We use 4 bytes of the 32 bytes to identify they file. - downloadPath := fmt.Sprintf("inspect.%s.%s", hex.EncodeToString(id[:]), 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 download file data.") - hexKey := hex.EncodeToString(id[:]) + hex.EncodeToString(key[:]) - if !globalJSON { - if !encrypt { - console.Infof("File data successfully downloaded as %s\n", console.Colorize("File", downloadPath)) - return nil - } - console.Infof("Encrypted file data successfully downloaded as %s\n", console.Colorize("File", downloadPath)) - console.Infof("Decryption key: %s\n\n", console.Colorize("Key", hexKey)) - - console.Info("The decryption key will ONLY be shown here. It cannot be recovered.\n") - console.Info("The encrypted file can safely be shared without the decryption key.\n") - console.Info("Even with the decryption key, data stored with encryption cannot be accessed.\n") - return nil - } - - v := struct { - File string `json:"file"` - Key string `json:"key,omitempty"` - }{ - File: downloadPath, - Key: hexKey, - } - if !encrypt { - v.Key = "" - } - b, e := json.Marshal(v) - fatalIf(probe.NewError(e), "Unable to serialize data") - console.Println(string(b)) + console.Infoln("Please use 'mc support inspect'") return nil } - -func decryptInspect(key [32]byte, r io.Reader) io.ReadCloser { - stream, err := sio.AES_256_GCM.Stream(key[:]) - fatalIf(probe.NewError(err), "Unable to initiate decryption") - - // Zero nonce, we only use each key once, and 32 bytes is plenty. - nonce := make([]byte, stream.NonceSize()) - return ioutil.NopCloser(stream.DecryptReader(r, nonce, nil)) -} diff --git a/cmd/auto-complete.go b/cmd/auto-complete.go index 686dba02..6f3ccd88 100644 --- a/cmd/auto-complete.go +++ b/cmd/auto-complete.go @@ -403,6 +403,7 @@ var completeCmds = map[string]complete.Predictor{ "/support/callhome/set": aliasCompleter, "/support/register": aliasCompleter, "/support/diag": aliasCompleter, + "/support/inspect": aliasCompleter, "/update": nil, } diff --git a/cmd/support-inspect.go b/cmd/support-inspect.go new file mode 100644 index 00000000..5fbcfa85 --- /dev/null +++ b/cmd/support-inspect.go @@ -0,0 +1,193 @@ +// Copyright (c) 2015-2021 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" + "encoding/binary" + "encoding/hex" + "fmt" + "hash/crc32" + "io" + "io/ioutil" + "os" + "path/filepath" + "runtime" + "strings" + "time" + + "github.com/fatih/color" + "github.com/minio/cli" + json "github.com/minio/colorjson" + "github.com/minio/madmin-go" + "github.com/minio/mc/pkg/probe" + "github.com/minio/pkg/console" + "github.com/secure-io/sio-go" +) + +var supportInspectFlags = []cli.Flag{ + cli.BoolFlag{ + Name: "encrypt", + Usage: "Encrypt content with one time key for confidential data", + }, +} + +var supportInspectCmd = cli.Command{ + Name: "inspect", + Usage: "inspect files on MinIO server", + Action: mainSupportInspect, + OnUsageError: onUsageError, + Before: setGlobalsFromContext, + Flags: append(supportInspectFlags, globalFlags...), + HideHelpCommand: true, + CustomHelpTemplate: `NAME: + {{.HelpName}} - {{.Usage}} + +USAGE: + {{.HelpName}} [FLAGS] TARGET + +FLAGS: + {{range .VisibleFlags}}{{.}} + {{end}} +EXAMPLES: + 1. Download 'xl.meta' for a specific object from all the drives in a zip file. + {{.Prompt}} {{.HelpName}} myminio/bucket/test*/xl.meta + + 2. Download all constituent parts for a specific object, and optionally encrypt the downloaded zip. + {{.Prompt}} {{.HelpName}} --encrypt myminio/bucket/test*/*/part.* + + 3. Download recursively all objects at a prefix. NOTE: This can be an expensive operation use it with caution. + {{.Prompt}} {{.HelpName}} myminio/bucket/test/** +`, +} + +func checkSupportInspectSyntax(ctx *cli.Context) { + if len(ctx.Args()) != 1 { + cli.ShowCommandHelpAndExit(ctx, "inspect", 1) // last argument is exit code + } +} + +// mainSupportInspect - the entry function of inspect command +func mainSupportInspect(ctx *cli.Context) error { + // Check for command syntax + checkSupportInspectSyntax(ctx) + + // Get the alias parameter from cli + args := ctx.Args() + aliasedURL := args.Get(0) + encrypt := ctx.Bool("encrypt") + + console.SetColor("File", color.New(color.FgWhite, color.Bold)) + console.SetColor("Key", color.New(color.FgHiRed, 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 + } + + // Compute bucket and object from the aliased URL + aliasedURL = filepath.ToSlash(aliasedURL) + splits := splitStr(aliasedURL, "/", 3) + bucket, prefix := splits[1], splits[2] + + shellName, _ := getShellName() + if runtime.GOOS != "windows" && shellName != "bash" && strings.Contains(prefix, "*") { + console.Infoln("Your shell is auto determined as '" + shellName + "', wildcard patterns are only supported with 'bash' SHELL.") + } + + key, r, ierr := client.Inspect(context.Background(), madmin.InspectOptions{ + Volume: bucket, + File: prefix, + }) + fatalIf(probe.NewError(ierr).Trace(aliasedURL), "Unable to inspect file.") + + // Create profile zip file + tmpFile, e := ioutil.TempFile("", "mc-inspect-") + fatalIf(probe.NewError(e), "Unable to download file data.") + + ext := "enc" + if !encrypt { + ext = "zip" + r = decryptInspect(key, r) + } + + // Copy zip content to target download file + _, e = io.Copy(tmpFile, r) + fatalIf(probe.NewError(e), "Unable to download file data.") + + // Close everything + r.Close() + tmpFile.Close() + + // Create an id that is also crc. + var id [4]byte + binary.LittleEndian.PutUint32(id[:], crc32.ChecksumIEEE(key[:])) + + // We use 4 bytes of the 32 bytes to identify they file. + downloadPath := fmt.Sprintf("inspect.%s.%s", hex.EncodeToString(id[:]), 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 download file data.") + hexKey := hex.EncodeToString(id[:]) + hex.EncodeToString(key[:]) + if !globalJSON { + if !encrypt { + console.Infof("File data successfully downloaded as %s\n", console.Colorize("File", downloadPath)) + return nil + } + console.Infof("Encrypted file data successfully downloaded as %s\n", console.Colorize("File", downloadPath)) + console.Infof("Decryption key: %s\n\n", console.Colorize("Key", hexKey)) + + console.Info("The decryption key will ONLY be shown here. It cannot be recovered.\n") + console.Info("The encrypted file can safely be shared without the decryption key.\n") + console.Info("Even with the decryption key, data stored with encryption cannot be accessed.\n") + return nil + } + + v := struct { + File string `json:"file"` + Key string `json:"key,omitempty"` + }{ + File: downloadPath, + Key: hexKey, + } + if !encrypt { + v.Key = "" + } + b, e := json.Marshal(v) + fatalIf(probe.NewError(e), "Unable to serialize data") + console.Println(string(b)) + return nil +} + +func decryptInspect(key [32]byte, r io.Reader) io.ReadCloser { + stream, err := sio.AES_256_GCM.Stream(key[:]) + fatalIf(probe.NewError(err), "Unable to initiate decryption") + + // Zero nonce, we only use each key once, and 32 bytes is plenty. + nonce := make([]byte, stream.NonceSize()) + return ioutil.NopCloser(stream.DecryptReader(r, nonce, nil)) +} diff --git a/cmd/support.go b/cmd/support.go index f8258fbe..0a415a53 100644 --- a/cmd/support.go +++ b/cmd/support.go @@ -25,6 +25,7 @@ var supportSubcommands = []cli.Command{ supportRegisterCmd, supportDiagCmd, supportCallhomeCmd, + supportInspectCmd, } var supportCmd = cli.Command{