From 210d072da948209d2590b2e08532ffaa892e69bf Mon Sep 17 00:00:00 2001 From: Justin Menga Date: Thu, 24 Mar 2022 12:59:32 -0700 Subject: [PATCH] Correctly detect GovCloud regions --- util/pkg/vfs/s3context.go | 7 ++++--- util/pkg/vfs/s3context_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/util/pkg/vfs/s3context.go b/util/pkg/vfs/s3context.go index 809272a16d..df02ccb887 100644 --- a/util/pkg/vfs/s3context.go +++ b/util/pkg/vfs/s3context.go @@ -36,11 +36,12 @@ import ( "k8s.io/klog/v2" ) -// matches all regional naming conventions of S3: -// https://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region +// matches regional naming conventions of S3: +// https://docs.aws.amazon.com/general/latest/gr/s3.html +// TODO: match fips and S3 access point naming conventions // TODO: perhaps make region regex more specific, i.e. (us|eu|ap|cn|ca|sa), to prevent matching bucket names that match region format? // but that will mean updating this list when AWS introduces new regions -var s3UrlRegexp = regexp.MustCompile(`(s3([-.](?P\w{2}-\w+-\d{1})|[-.](?P[\w.\-\_]+)|)?|(?P[\w.\-\_]+)[.]s3([.](?P\w{2}-\w+-\d{1}))?)[.]amazonaws[.]com([.]cn)?(?P.*)?`) +var s3UrlRegexp = regexp.MustCompile(`(s3([-.](?P\w{2}(-gov)?-\w+-\d{1})|[-.](?P[\w.\-\_]+)|)?|(?P[\w.\-\_]+)[.]s3([.-](?P\w{2}(-gov)?-\w+-\d{1}))?)[.]amazonaws[.]com([.]cn)?(?P.*)?`) type S3BucketDetails struct { // context is the S3Context we are associated with diff --git a/util/pkg/vfs/s3context_test.go b/util/pkg/vfs/s3context_test.go index 4dd74c78a3..89fc954381 100644 --- a/util/pkg/vfs/s3context_test.go +++ b/util/pkg/vfs/s3context_test.go @@ -84,6 +84,16 @@ func Test_VFSPath(t *testing.T) { ExpectedResult: "s3://bucket/path", ExpectError: false, }, + { + Input: "https://s3.us-gov-west-1.amazonaws.com/bucket", + ExpectedResult: "s3://bucket", + ExpectError: false, + }, + { + Input: "https://s3.us-gov-west-1.amazonaws.com/bucket/path", + ExpectedResult: "s3://bucket/path", + ExpectError: false, + }, { Input: "https://s3.amazonaws.com/bucket", ExpectedResult: "s3://bucket", @@ -124,6 +134,16 @@ func Test_VFSPath(t *testing.T) { ExpectedResult: "s3://bucket-name/path", ExpectError: false, }, + { + Input: "https://bucket-name.s3-us-gov-west-1.amazonaws.com", + ExpectedResult: "s3://bucket-name", + ExpectError: false, + }, + { + Input: "https://bucket-name.s3-us-gov-west-1.amazonaws.com/path", + ExpectedResult: "s3://bucket-name/path", + ExpectError: false, + }, { Input: "example.com/bucket", ExpectedResult: "",