From e2a865948c78155fc50775c54ba298fb494d763e Mon Sep 17 00:00:00 2001 From: Eric Dong Date: Mon, 29 Mar 2021 13:51:53 -0700 Subject: [PATCH] Add support to detect 5 parts s3 virtual hosted-style requests --- detect_s3.go | 12 ++++++++++++ detect_s3_test.go | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/detect_s3.go b/detect_s3.go index 8e0f4a03b..89f3c35dc 100644 --- a/detect_s3.go +++ b/detect_s3.go @@ -34,6 +34,8 @@ func (d *S3Detector) detectHTTP(src string) (string, bool, error) { return d.detectPathStyle(hostParts[0], parts[1:]) } else if len(hostParts) == 4 { return d.detectVhostStyle(hostParts[1], hostParts[0], parts[1:]) + } else if len(hostParts) == 5 && hostParts[1] == "s3" { + return d.detectNewVhostStyle(hostParts[2], hostParts[0], parts[1:]) } else { return "", false, fmt.Errorf( "URL is not a valid S3 URL") @@ -59,3 +61,13 @@ func (d *S3Detector) detectVhostStyle(region, bucket string, parts []string) (st return "s3::" + url.String(), true, nil } + +func (d *S3Detector) detectNewVhostStyle(region, bucket string, parts []string) (string, bool, error) { + urlStr := fmt.Sprintf("https://s3.%s.amazonaws.com/%s/%s", region, bucket, strings.Join(parts, "/")) + url, err := url.Parse(urlStr) + if err != nil { + return "", false, fmt.Errorf("error parsing S3 URL: %s", err) + } + + return "s3::" + url.String(), true, nil +} diff --git a/detect_s3_test.go b/detect_s3_test.go index f410c785c..6aceafd8f 100644 --- a/detect_s3_test.go +++ b/detect_s3_test.go @@ -34,6 +34,11 @@ func TestS3Detector(t *testing.T) { "bucket.s3-eu-west-1.amazonaws.com/foo/bar.baz", "s3::https://s3-eu-west-1.amazonaws.com/bucket/foo/bar.baz", }, + // 5 parts Virtual hosted-style + { + "bucket.s3.eu-west-1.amazonaws.com/foo/bar.baz", + "s3::https://s3.eu-west-1.amazonaws.com/bucket/foo/bar.baz", + }, // Path style { "s3.amazonaws.com/bucket/foo",