Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --dangerous flag for mc rb sitewide removal #2709

Merged
merged 2 commits into from Mar 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 38 additions & 21 deletions cmd/rb-main.go
Expand Up @@ -18,7 +18,9 @@ package cmd

import (
"encoding/json"
"fmt"
"path/filepath"
"strings"

"github.com/fatih/color"
"github.com/minio/cli"
Expand All @@ -32,6 +34,10 @@ var (
Name: "force",
Usage: "allow a recursive remove operation",
},
cli.BoolFlag{
Name: "dangerous",
Usage: "allow site-wide removal of objects",
},
}
)

Expand Down Expand Up @@ -62,7 +68,7 @@ EXAMPLES:
$ {{.HelpName}} --force s3/jazz-songs

4. Remove all buckets and objects recursively from S3 host
$ {{.HelpName}} --force s3
$ {{.HelpName}} --force --dangerous s3
`,
}

Expand All @@ -74,7 +80,7 @@ type removeBucketMessage struct {

// String colorized delete bucket message.
func (s removeBucketMessage) String() string {
return console.Colorize("RemoveBucket", "Bucket removed successfully `"+s.Bucket+"`.")
return console.Colorize("RemoveBucket", fmt.Sprintf("Removed `%s` successfully.", s.Bucket))
}

// JSON jsonified remove bucket message.
Expand All @@ -91,6 +97,19 @@ func checkRbSyntax(ctx *cli.Context) {
exitCode := 1
cli.ShowCommandHelpAndExit(ctx, "rb", exitCode)
}
// Set command flags from context.
isForce := ctx.Bool("force")
isDangerous := ctx.Bool("dangerous")

for _, url := range ctx.Args() {
if isNamespaceRemoval(url) {
if isForce && isDangerous {
continue
}
fatalIf(errDummy().Trace(),
"This operation results in **site-wide** removal of buckets. If you are really sure, retry this command with ‘--force’ and ‘--dangerous’ flags.")
}
}
}

// deletes a bucket and all its contents
Expand Down Expand Up @@ -134,10 +153,21 @@ func deleteBucket(url string) *probe.Error {
return pErr
}
}
// list internally mimics recursive directory listing of object prefixes for s3 similar to FS.
// The rmMessage needs to be printed only for actual buckets being deleted and not objects.
tgt := strings.TrimPrefix(urlString, string(filepath.Separator))
if !strings.Contains(tgt, string(filepath.Separator)) && tgt != targetAlias {
printMsg(removeBucketMessage{
Bucket: targetAlias + urlString, Status: "success",
})
}
}

// Remove the given url since the user will always want to remove it.
contentCh <- &clientContent{URL: *newClientURL(targetURL)}
alias, _ := url2Alias(targetURL)
if alias != "" {
contentCh <- &clientContent{URL: *newClientURL(targetURL)}
}

// Finish removing and print all the remaining errors
close(contentCh)
Expand Down Expand Up @@ -171,9 +201,9 @@ func isNamespaceRemoval(url string) bool {
func mainRemoveBucket(ctx *cli.Context) error {
// check 'rb' cli arguments.
checkRbSyntax(ctx)
// Set command flags from context.
isForce := ctx.Bool("force")
// Additional command speific theme customization.

// Additional command specific theme customization.
console.SetColor("RemoveBucket", color.New(color.FgGreen, color.Bold))

var cErr error
Expand Down Expand Up @@ -202,24 +232,11 @@ func mainRemoveBucket(ctx *cli.Context) error {
break
}
// For all recursive operations make sure to check for 'force' flag.
if !isForce {
if isNamespaceRemoval(targetURL) {
fatalIf(errDummy().Trace(),
"This operation results in **site-wide** removal of buckets. If you are really sure, retry this command with ‘--force’ flags.")
}
if !isEmpty {
fatalIf(errDummy().Trace(), "`"+targetURL+"` is not empty. Retry this command with ‘--force’ flag if you want to remove `"+targetURL+"` and all its contents")
}
if !isForce && !isEmpty {
fatalIf(errDummy().Trace(), "`"+targetURL+"` is not empty. Retry this command with ‘--force’ flag if you want to remove `"+targetURL+"` and all its contents")
}
e := deleteBucket(targetURL)
if e == nil {
// Successfully removed a bucket.
printMsg(removeBucketMessage{Status: "success", Bucket: targetURL})
} else {
errorIf(e.Trace(targetURL), "Failed to remove `"+targetURL+"`.")
cErr = exitStatus(globalErrorExitStatus)
}

fatalIf(e.Trace(targetURL), "Failed to remove `"+targetURL+"`.")
}
return cErr
}