diff --git a/pkg/s3utils/utils.go b/pkg/s3utils/utils.go index 2f1a5a653..79c129466 100644 --- a/pkg/s3utils/utils.go +++ b/pkg/s3utils/utils.go @@ -89,6 +89,12 @@ var amazonS3HostHyphen = regexp.MustCompile(`^s3-(.*?).amazonaws.com$`) // amazonS3HostDualStack - regular expression used to determine if an arg is s3 host dualstack. var amazonS3HostDualStack = regexp.MustCompile(`^s3.dualstack.(.*?).amazonaws.com$`) +// amazonS3HostFIPS - regular expression used to determine if an arg is s3 FIPS host. +var amazonS3HostFIPS = regexp.MustCompile(`^s3-fips.(.*?).amazonaws.com$`) + +// amazonS3HostFIPSDualStack - regular expression used to determine if an arg is s3 FIPS host dualstack. +var amazonS3HostFIPSDualStack = regexp.MustCompile(`^s3-fips.dualstack.(.*?).amazonaws.com$`) + // amazonS3HostDot - regular expression used to determine if an arg is s3 host in . style. var amazonS3HostDot = regexp.MustCompile(`^s3.(.*?).amazonaws.com$`) @@ -126,6 +132,18 @@ func GetRegionFromURL(endpointURL url.URL) string { if len(parts) > 1 { return parts[1] } + if IsAmazonFIPSUSEastWestEndpoint(endpointURL) { + // We check for FIPS dualstack matching first to avoid the non-greedy + // regex for FIPS non-dualstack matching a dualstack URL + parts = amazonS3HostFIPSDualStack.FindStringSubmatch(endpointURL.Host) + if len(parts) > 1 { + return parts[1] + } + parts = amazonS3HostFIPS.FindStringSubmatch(endpointURL.Host) + if len(parts) > 1 { + return parts[1] + } + } parts = amazonS3HostHyphen.FindStringSubmatch(endpointURL.Host) if len(parts) > 1 { return parts[1] diff --git a/pkg/s3utils/utils_test.go b/pkg/s3utils/utils_test.go index 66a453b5c..ca50a16fb 100644 --- a/pkg/s3utils/utils_test.go +++ b/pkg/s3utils/utils_test.go @@ -115,6 +115,18 @@ func TestGetRegionFromURL(t *testing.T) { }, expectedRegion: "us-east-1", }, + { + u: url.URL{ + Host: "s3-fips.us-east-1.amazonaws.com", + }, + expectedRegion: "us-east-1", + }, + { + u: url.URL{ + Host: "s3-fips.dualstack.us-west-1.amazonaws.com", + }, + expectedRegion: "us-west-1", + }, } for i, testCase := range testCases {