Skip to content

Commit

Permalink
Modify parseRegionFromECRPrivateHost to expect 5 capture groups from …
Browse files Browse the repository at this point in the history
…Regex and update testing for possible scenarios of regex parsing to ensure region parsing works in all endpoint combinations
  • Loading branch information
BDworak committed Dec 7, 2024
1 parent 2e1da62 commit 10340d5
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/ecr-credential-provider/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
const ecrPublicRegion string = "us-east-1"
const ecrPublicHost string = "public.ecr.aws"

var ecrPrivateHostPattern = regexp.MustCompile(`^(\d{12})\.dkr[\.\-]ecr(\-fips)?\.([a-zA-Z0-9][a-zA-Z0-9-_]*)\.(amazonaws\.com(\.cn)?|on\.(aws|amazonwebservices\.com\.cn)|sc2s\.sgov\.gov|c2s\.ic\.gov|cloud\.adc-e\.uk|csp\.hci\.ic\.gov)$`)
var ecrPrivateHostPattern = regexp.MustCompile(`^(\d{12})\.dkr[\.\-]ecr(\-fips)?\.([a-zA-Z0-9][a-zA-Z0-9-_]*)\.(amazonaws\.com(?:\.cn)?|on\.(?:aws|amazonwebservices\.com\.cn)|sc2s\.sgov\.gov|c2s\.ic\.gov|cloud\.adc-e\.uk|csp\.hci\.ic\.gov)$`)

// ECR abstracts the calls we make to aws-sdk for testing purposes
type ECR interface {
Expand Down Expand Up @@ -234,7 +234,7 @@ func parseHostFromImageReference(image string) (string, error) {

func parseRegionFromECRPrivateHost(host string) string {
splitHost := ecrPrivateHostPattern.FindStringSubmatch(host)
if len(splitHost) != 6 {
if len(splitHost) != 5 {
return ""
}
return splitHost[3]
Expand Down
50 changes: 50 additions & 0 deletions cmd/ecr-credential-provider/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,61 @@ func Test_parseRegionFromECRPrivateHost(t *testing.T) {
host string
region string
}{
// us-west-2
{
name: "success",
host: "123456789123.dkr.ecr.us-west-2.amazonaws.com",
region: "us-west-2",
},
// CN region
{
name: "success",
host: "123456789123.dkr.ecr.cn-north-1.amazonaws.com.cn",
region: "cn-north-1",
},
// GovCloud
{
name: "success",
host: "123456789123.dkr.ecr.us-gov-east-1.amazonaws.com",
region: "us-gov-east-1",
},
// ISO
{
name: "success",
host: "123456789123.dkr.ecr.us-iso-east-1.c2s.ic.gov",
region: "us-iso-east-1",
},
// Dual-Stack
{
name: "success",
host: "123456789123.dkr-ecr.us-west-2.on.aws",
region: "us-west-2",
},
// Dual-Stack FIPS
{
name: "success",
host: "123456789123.dkr-ecr-fips.us-west-2.on.aws",
region: "us-west-2",
},
// IPv6 CN
{
name: "success",
host: "123456789123.dkr-ecr.cn-north-1.on.amazonwebservices.com.cn",
region: "cn-north-1",
},
// IPv6 GovCloud
{
name: "success",
host: "123456789123.dkr-ecr.us-gov-east-1.on.aws",
region: "us-gov-east-1",
},
// IPv6 GovCloud FIPS
{
name: "success",
host: "123456789123.dkr-ecr-fips.us-gov-east-1.on.aws",
region: "us-gov-east-1",
},
// Invalid name
{
name: "invalid registry",
host: "foobar",
Expand Down

0 comments on commit 10340d5

Please sign in to comment.