github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/encrypt-clear.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 encryptClearCmd = cli.Command{
    32  	Name:         "clear",
    33  	Usage:        "clear encryption config",
    34  	Action:       mainEncryptClear,
    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. Remove auto encryption config on bucket "mybucket" for alias "myminio".
    49       {{.Prompt}} {{.HelpName}} myminio/mybucket
    50  `,
    51  }
    52  
    53  // checkEncryptClearSyntax - validate all the passed arguments
    54  func checkEncryptClearSyntax(ctx *cli.Context) {
    55  	if len(ctx.Args()) != 1 {
    56  		showCommandHelpAndExit(ctx, 1) // last argument is exit code
    57  	}
    58  }
    59  
    60  type encryptClearMessage struct {
    61  	Op     string `json:"op"`
    62  	Status string `json:"status"`
    63  	URL    string `json:"url"`
    64  }
    65  
    66  func (v encryptClearMessage) JSON() string {
    67  	v.Status = "success"
    68  	jsonMessageBytes, e := json.MarshalIndent(v, "", " ")
    69  	fatalIf(probe.NewError(e), "Unable to marshal into JSON.")
    70  	return string(jsonMessageBytes)
    71  }
    72  
    73  func (v encryptClearMessage) String() string {
    74  	return console.Colorize("encryptClearMessage", fmt.Sprintf("Auto encryption configuration has been cleared successfully for %s", v.URL))
    75  }
    76  
    77  func mainEncryptClear(cliCtx *cli.Context) error {
    78  	ctx, cancelencryptClear := context.WithCancel(globalContext)
    79  	defer cancelencryptClear()
    80  
    81  	console.SetColor("encryptClearMessage", color.New(color.FgGreen))
    82  
    83  	checkEncryptClearSyntax(cliCtx)
    84  
    85  	// Get the alias parameter from cli
    86  	args := cliCtx.Args()
    87  	aliasedURL := args.Get(0)
    88  	// Create a new Client
    89  	client, err := newClient(aliasedURL)
    90  	fatalIf(err, "Unable to initialize connection.")
    91  	fatalIf(client.DeleteEncryption(ctx), "Unable to clear auto encryption configuration")
    92  	printMsg(encryptClearMessage{
    93  		Op:     cliCtx.Command.Name,
    94  		Status: "success",
    95  		URL:    aliasedURL,
    96  	})
    97  	return nil
    98  }