github.com/minio/mc@v0.0.0-20240503112107-b471de8d1882/cmd/legalhold-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  	"time"
    23  
    24  	"github.com/fatih/color"
    25  	"github.com/minio/cli"
    26  	"github.com/minio/minio-go/v7"
    27  	"github.com/minio/pkg/v2/console"
    28  )
    29  
    30  var lhClearFlags = []cli.Flag{
    31  	cli.BoolFlag{
    32  		Name:  "recursive, r",
    33  		Usage: "clear legal hold recursively",
    34  	},
    35  	cli.StringFlag{
    36  		Name:  "version-id, vid",
    37  		Usage: "clear legal hold of a specific object version",
    38  	},
    39  	cli.StringFlag{
    40  		Name:  "rewind",
    41  		Usage: "clear legal hold on an object version at specified time",
    42  	},
    43  	cli.BoolFlag{
    44  		Name:  "versions",
    45  		Usage: "clear legal hold on multiple versions of object(s)",
    46  	},
    47  }
    48  
    49  var legalHoldClearCmd = cli.Command{
    50  	Name:         "clear",
    51  	Usage:        "clear legal hold for object(s)",
    52  	Action:       mainLegalHoldClear,
    53  	OnUsageError: onUsageError,
    54  	Before:       setGlobalsFromContext,
    55  	Flags:        append(lhClearFlags, globalFlags...),
    56  	CustomHelpTemplate: `NAME:
    57    {{.HelpName}} - {{.Usage}}
    58  
    59  USAGE:
    60    {{.HelpName}} [FLAGS] TARGET
    61  
    62  FLAGS:
    63    {{range .VisibleFlags}}{{.}}
    64    {{end}}
    65  
    66  EXAMPLES:
    67     1. Disable legal hold on a specific object
    68        $ {{.HelpName}} myminio/mybucket/prefix/obj.csv
    69  
    70     2. Disable legal hold on a specific object version
    71        $ {{.HelpName}} myminio/mybucket/prefix/obj.csv --version-id "HiMFUTOowG6ylfNi4LKxD3ieHbgfgrvC"
    72  
    73     3. Disable object legal hold recursively for all objects at a prefix
    74        $ {{.HelpName}} myminio/mybucket/prefix --recursive
    75  
    76     4. Disable object legal hold recursively for all objects versions older than one year
    77        $ {{.HelpName}} myminio/mybucket/prefix --recursive --rewind 365d --versions
    78  `,
    79  }
    80  
    81  // main for legalhold clear command.
    82  func mainLegalHoldClear(cliCtx *cli.Context) error {
    83  	console.SetColor("LegalHoldSuccess", color.New(color.FgGreen, color.Bold))
    84  	console.SetColor("LegalHoldPartialFailure", color.New(color.FgRed, color.Bold))
    85  	console.SetColor("LegalHoldMessageFailure", color.New(color.FgYellow))
    86  
    87  	targetURL, versionID, timeRef, recursive, withVersions := parseLegalHoldArgs(cliCtx)
    88  	if timeRef.IsZero() && withVersions {
    89  		timeRef = time.Now().UTC()
    90  	}
    91  
    92  	ctx, cancelCopy := context.WithCancel(globalContext)
    93  	defer cancelCopy()
    94  
    95  	enabled, err := isBucketLockEnabled(ctx, targetURL)
    96  	if err != nil {
    97  		fatalIf(err, "Unable to clear legalhold of `%s`", targetURL)
    98  	}
    99  	if !enabled {
   100  		fatalIf(errDummy().Trace(), "Bucket locking needs to be enabled in order to use this feature.")
   101  	}
   102  
   103  	return setLegalHold(ctx, targetURL, versionID, timeRef, withVersions, recursive, minio.LegalHoldDisabled)
   104  }