Skip to content

Commit

Permalink
Support detecting region from FIPS URLs (#1663)
Browse files Browse the repository at this point in the history
Signed-off-by: Balasankar 'Balu' C <balu@dravidam.net>
  • Loading branch information
balasankarc committed Jun 10, 2022
1 parent a1dc6ea commit 47da873
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions pkg/s3utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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$`)

Expand Down Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions pkg/s3utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 47da873

Please sign in to comment.