mv: Fix regression when checking lock in target bucket (#4015)
A recent commit changed the behavior of client-s3.GetObjectLockConfig() without properly checking the behavior of the callers introducing a bug during mv command where the target is an S3 server. Add some tests for the mc mv command as well.
This commit is contained in:
+2
-8
@@ -395,14 +395,8 @@ func doCopySession(ctx context.Context, cancelCopy context.CancelFunc, cli *cli.
|
||||
sourceURLs := cli.Args()[:len(cli.Args())-1]
|
||||
targetURL := cli.Args()[len(cli.Args())-1] // Last one is target
|
||||
|
||||
tgtClnt, err := newClient(targetURL)
|
||||
fatalIf(err, "Unable to initialize `"+targetURL+"`.")
|
||||
|
||||
// Check if the target bucket has object locking enabled
|
||||
var withLock bool
|
||||
if _, _, _, _, err = tgtClnt.GetObjectLockConfig(ctx); err == nil {
|
||||
withLock = true
|
||||
}
|
||||
// Check if the target path has object locking enabled
|
||||
withLock, _ := isBucketLockEnabled(ctx, targetURL)
|
||||
|
||||
if session != nil {
|
||||
// isCopied returns true if an object has been already copied
|
||||
|
||||
+19
-2
@@ -22,6 +22,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/minio/cli"
|
||||
json "github.com/minio/colorjson"
|
||||
@@ -85,6 +86,10 @@ var (
|
||||
errBucketLockNotSupported = errors.New("bucket lock not supported")
|
||||
)
|
||||
|
||||
// Return true if this an S3 bucket with locking enabled
|
||||
// Return false if this an S3 bucket with no locking enabled
|
||||
// Return false if this is a filesystem URL
|
||||
// Otherwise return unexpected errors
|
||||
func isBucketLockEnabled(ctx context.Context, aliasedURL string) (bool, *probe.Error) {
|
||||
st, err := getBucketLockStatus(ctx, aliasedURL)
|
||||
if err == nil {
|
||||
@@ -103,12 +108,24 @@ func getBucketLockStatus(ctx context.Context, aliasedURL string) (status string,
|
||||
return "", err
|
||||
}
|
||||
|
||||
// Remove the prefix/object from the aliased url and reconstruct the client
|
||||
switch c := clnt.(type) {
|
||||
case *S3Client:
|
||||
_, object := c.url2BucketAndObject()
|
||||
if object != "" {
|
||||
clnt, _ = newClient(strings.TrimSuffix(aliasedURL, object))
|
||||
}
|
||||
default:
|
||||
return "", probe.NewError(errBucketLockNotSupported)
|
||||
}
|
||||
|
||||
status, _, _, _, err = clnt.GetObjectLockConfig(ctx)
|
||||
if err != nil {
|
||||
errResp := minio.ToErrorResponse(err.ToGoError())
|
||||
if errResp.StatusCode == http.StatusNotFound {
|
||||
switch {
|
||||
case errResp.Code == "ObjectLockConfigurationNotFoundError":
|
||||
return "", probe.NewError(errBucketLockConfigNotFound)
|
||||
} else if errResp.StatusCode == http.StatusNotImplemented {
|
||||
case errResp.StatusCode == http.StatusNotImplemented:
|
||||
return "", probe.NewError(errBucketLockNotSupported)
|
||||
}
|
||||
return "", err
|
||||
|
||||
+4
-10
@@ -241,18 +241,12 @@ func mainMove(cliCtx *cli.Context) error {
|
||||
// Check if source URLs does not have object locking enabled
|
||||
// since we cannot move them (remove them from the source)
|
||||
for _, urlStr := range cliCtx.Args()[:cliCtx.NArg()-1] {
|
||||
client, err := newClient(urlStr)
|
||||
enabled, err := isBucketLockEnabled(ctx, urlStr)
|
||||
if err != nil {
|
||||
fatalIf(err.Trace(), "Unable to parse the provided url.")
|
||||
fatalIf(err.Trace(), "Unable to get bucket lock configuration of `%s`", urlStr)
|
||||
}
|
||||
if _, ok := client.(*S3Client); ok {
|
||||
enabled, err := isBucketLockEnabled(ctx, urlStr)
|
||||
if err != nil {
|
||||
fatalIf(err.Trace(), "Unable to get bucket lock configuration of `%s`", urlStr)
|
||||
}
|
||||
if enabled {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Object lock configuration is enabled on the specified bucket in alias %v.", urlStr))
|
||||
}
|
||||
if enabled {
|
||||
fatalIf(errDummy().Trace(), fmt.Sprintf("Object lock configuration is enabled on the specified bucket in alias %v.", urlStr))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -132,7 +132,7 @@ func mainRetentionClear(cliCtx *cli.Context) error {
|
||||
|
||||
target, versionID, rewind, withVersions, recursive, bucketMode := parseClearRetentionArgs(cliCtx)
|
||||
|
||||
checkObjectLockSupport(ctx, target)
|
||||
fatalIfBucketLockNotEnabled(ctx, target)
|
||||
|
||||
if bucketMode {
|
||||
return clearBucketLock(target)
|
||||
|
||||
+4
-26
@@ -21,7 +21,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
json "github.com/minio/colorjson"
|
||||
@@ -171,31 +170,10 @@ func parseRetentionValidity(validityStr string) (uint64, minio.ValidityUnit, *pr
|
||||
return validity, unit, nil
|
||||
}
|
||||
|
||||
// Check if the bucket corresponding to the target url has
|
||||
// object locking enabled, this to show a pretty error message
|
||||
func checkObjectLockSupport(ctx context.Context, aliasedURL string) {
|
||||
clnt, err := newClient(aliasedURL)
|
||||
if err != nil {
|
||||
fatalIf(err.Trace(), "Unable to parse the provided url.")
|
||||
}
|
||||
|
||||
// Remove the prefix/object from the aliased url and reconstruct the client
|
||||
switch c := clnt.(type) {
|
||||
case *S3Client:
|
||||
_, object := c.url2BucketAndObject()
|
||||
if object != "" {
|
||||
clnt, _ = newClient(strings.TrimSuffix(aliasedURL, object))
|
||||
}
|
||||
default:
|
||||
fatalIf(errDummy(), "Bucket locking feature is unsupported with `%s`", aliasedURL)
|
||||
}
|
||||
|
||||
status, _, _, _, err := clnt.GetObjectLockConfig(ctx)
|
||||
if err != nil {
|
||||
fatalIf(err.Trace(), "Unable to get bucket lock configuration from `%s`", aliasedURL)
|
||||
}
|
||||
|
||||
if status != "Enabled" {
|
||||
func fatalIfBucketLockNotEnabled(ctx context.Context, aliasedURL string) {
|
||||
enabled, err := getBucketLockStatus(ctx, aliasedURL)
|
||||
fatalIf(err.Trace(), "Unable to get bucket lock configuration from `%s`", aliasedURL)
|
||||
if enabled != "Enabled" {
|
||||
fatalIf(errDummy().Trace(), "Remote bucket does not support locking `%s`", aliasedURL)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -382,7 +382,7 @@ func mainRetentionInfo(cliCtx *cli.Context) error {
|
||||
|
||||
target, versionID, recursive, rewind, withVersions, bucketMode := parseInfoRetentionArgs(cliCtx)
|
||||
|
||||
checkObjectLockSupport(ctx, target)
|
||||
fatalIfBucketLockNotEnabled(ctx, target)
|
||||
|
||||
if bucketMode {
|
||||
return showBucketLock(target)
|
||||
|
||||
@@ -147,7 +147,7 @@ func mainRetentionSet(cliCtx *cli.Context) error {
|
||||
|
||||
target, versionID, recursive, rewind, withVersions, mode, validity, unit, bypass, bucketMode := parseSetRetentionArgs(cliCtx)
|
||||
|
||||
checkObjectLockSupport(ctx, target)
|
||||
fatalIfBucketLockNotEnabled(ctx, target)
|
||||
|
||||
if bucketMode {
|
||||
return setBucketLock(target, mode, validity, unit)
|
||||
|
||||
@@ -770,6 +770,43 @@ function test_copy_object_preserve_filesystem_attr()
|
||||
log_success "$start_time" "${FUNCNAME[0]}"
|
||||
}
|
||||
|
||||
# Test "mc mv" command
|
||||
function test_mv_object()
|
||||
{
|
||||
show "${FUNCNAME[0]}"
|
||||
|
||||
random_dir="dir-$RANDOM-$RANDOM"
|
||||
tmpdir="$(mktemp -d)"
|
||||
|
||||
# Test mv command locally
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd cp "${FILE_1_MB}" "${tmpdir}/file.tmp"
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd mv "${tmpdir}/file.tmp" "${tmpdir}/file"
|
||||
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd stat "${tmpdir}/file.tmp"
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" check_md5sum "$FILE_1_MB_MD5SUM" "${tmpdir}/file"
|
||||
|
||||
# Test mv command from filesystem to S3
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd mv "${tmpdir}/file" "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/object-1"
|
||||
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd stat "${tmpdir}/file"
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd stat "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/object-1"
|
||||
|
||||
# Test mv command from S3 to S3
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd mv "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/object-1" "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/object-2"
|
||||
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd stat "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/object-1"
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd stat "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/object-2"
|
||||
|
||||
# Test mv command from S3 to filesystem
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd mv "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/object-2" "${tmpdir}/file"
|
||||
assert_failure "$start_time" "${FUNCNAME[0]}" mc_cmd stat "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/object-2"
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd stat "${tmpdir}/file"
|
||||
|
||||
# Cleanup
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" mc_cmd rm -r --force "${SERVER_ALIAS}/${BUCKET_NAME}/${random_dir}/"
|
||||
assert_success "$start_time" "${FUNCNAME[0]}" rm -r "${tmpdir}"
|
||||
|
||||
log_success "$start_time" "${FUNCNAME[0]}"
|
||||
}
|
||||
|
||||
|
||||
function test_copy_object_with_sse_rewrite()
|
||||
{
|
||||
# test server side copy and remove operation - target is unencrypted while source is encrypted
|
||||
@@ -996,6 +1033,7 @@ function run_test()
|
||||
test_put_object_multipart
|
||||
test_get_object
|
||||
test_get_object_multipart
|
||||
test_mv_object
|
||||
test_presigned_post_policy_error
|
||||
test_presigned_put_object
|
||||
test_presigned_get_object
|
||||
|
||||
Reference in New Issue
Block a user