github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/encrypt-info.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  	"context"
    22  	"fmt"
    23  
    24  	"github.com/fatih/color"
    25  	"github.com/minio/cli"
    26  	json "github.com/minio/colorjson"
    27  	"github.com/minio/mc/pkg/probe"
    28  	"github.com/minio/pkg/v2/console"
    29  )
    30  
    31  var encryptInfoCmd = cli.Command{
    32  	Name:         "info",
    33  	Usage:        "show bucket encryption status",
    34  	Action:       mainEncryptInfo,
    35  	OnUsageError: onUsageError,
    36  	Before:       setGlobalsFromContext,
    37  	Flags:        globalFlags,
    38  	CustomHelpTemplate: `NAME:
    39    {{.HelpName}} - {{.Usage}}
    40  
    41  USAGE:
    42    {{.HelpName}} TARGET
    43  
    44  FLAGS:
    45    {{range .VisibleFlags}}{{.}}
    46    {{end}}
    47  EXAMPLES:
    48    1. Display bucket encryption status for bucket "mybucket".
    49       {{.Prompt}} {{.HelpName}} myminio/mybucket
    50  `,
    51  }
    52  
    53  // checkversionInfoSyntax - validate all the passed arguments
    54  func checkEncryptInfoSyntax(ctx *cli.Context) {
    55  	if len(ctx.Args()) != 1 {
    56  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    57  	}
    58  }
    59  
    60  type encryptInfoMessage struct {
    61  	Op         string `json:"op"`
    62  	Status     string `json:"status"`
    63  	URL        string `json:"url"`
    64  	Encryption struct {
    65  		Algorithm string `json:"algorithm,omitempty"`
    66  		KeyID     string `json:"keyId,omitempty"`
    67  	} `json:"encryption,omitempty"`
    68  }
    69  
    70  func (v encryptInfoMessage) JSON() string {
    71  	v.Status = "success"
    72  	jsonMessageBytes, e := json.MarshalIndent(v, "", " ")
    73  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    74  	return string(jsonMessageBytes)
    75  }
    76  
    77  func (v encryptInfoMessage) String() string {
    78  	msg := ""
    79  	switch v.Encryption.Algorithm {
    80  	case "":
    81  		msg = fmt.Sprintf("Auto encryption is not enabled for %s ", v.URL)
    82  	default:
    83  		msg = "Auto encryption 'sse-s3' is enabled"
    84  	}
    85  	if v.Encryption.KeyID != "" {
    86  		msg = fmt.Sprintf("Auto encrytion 'sse-kms' is enabled with KeyID: %s", v.Encryption.KeyID)
    87  	}
    88  	return console.Colorize("encryptInfoMessage", msg)
    89  }
    90  
    91  func mainEncryptInfo(cliCtx *cli.Context) error {
    92  	ctx, cancelEncryptInfo := context.WithCancel(globalContext)
    93  	defer cancelEncryptInfo()
    94  
    95  	console.SetColor("encryptInfoMessage", color.New(color.FgGreen))
    96  
    97  	checkEncryptInfoSyntax(cliCtx)
    98  
    99  	// Get the alias parameter from cli
   100  	args := cliCtx.Args()
   101  	aliasedURL := args.Get(0)
   102  	// Create a new Client
   103  	client, err := newClient(aliasedURL)
   104  	fatalIf(err, "Unable to initialize connection.")
   105  	algorithm, keyID, e := client.GetEncryption(ctx)
   106  	fatalIf(e, "Unable to get encryption info")
   107  	msg := encryptInfoMessage{
   108  		Op:     cliCtx.Command.Name,
   109  		Status: "success",
   110  		URL:    aliasedURL,
   111  	}
   112  	msg.Encryption.Algorithm = algorithm
   113  	msg.Encryption.KeyID = keyID
   114  	printMsg(msg)
   115  	return nil
   116  }