Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ECR parsing regex to include non-public AWS partitions #835

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions oci/auth/aws/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ import (
"github.com/fluxcd/pkg/oci"
)

var registryPartRe = regexp.MustCompile(`([0-9+]*).dkr.ecr(?:-fips)?\.([^/.]*)\.(amazonaws\.com[.cn]*)`)
// This regex is sourced from the AWS ECR Credential Helper (https://github.com/awslabs/amazon-ecr-credential-helper).
// It covers both public AWS partitions like amazonaws.com, China partitions like amazonaws.com.cn, and non-public partitions.
var registryPartRe = regexp.MustCompile(`(\d{12})\.dkr\.ecr(\-fips)?\.([a-zA-Z0-9][a-zA-Z0-9-_]*)\.(amazonaws\.com(\.cn)?|sc2s\.sgov\.gov|c2s\.ic\.gov|cloud\.adc-e\.uk|csp\.hci\.ic\.gov)`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm afraid of completely replacing the previous expression. It was introduced several years ago in https://github.com/fluxcd/image-reflector-controller/pull/174/files#diff-922fcbfbe2565443329918a2b0d49b2b03588007ec6464a20d3feb824f3f80a2R199 and I see some weirdness with it, compared to the new one. I think it would be safer to only append what's needed here, instead of replacing the whole expression, and also needing to change the registryParts slice index below.

Suggested change
var registryPartRe = regexp.MustCompile(`(\d{12})\.dkr\.ecr(\-fips)?\.([a-zA-Z0-9][a-zA-Z0-9-_]*)\.(amazonaws\.com(\.cn)?|sc2s\.sgov\.gov|c2s\.ic\.gov|cloud\.adc-e\.uk|csp\.hci\.ic\.gov)`)
var registryPartRe = regexp.MustCompile(`([0-9+]*).dkr.ecr(?:-fips)?\.([^/.]*)\.(amazonaws\.com[.cn]*|sc2s\.sgov\.gov|c2s\.ic\.gov|cloud\.adc-e\.uk|csp\.hci\.ic\.gov)`)


// ParseRegistry returns the AWS account ID and region and `true` if
// the image registry/repository is hosted in AWS's Elastic Container Registry,
Expand All @@ -47,7 +49,7 @@ func ParseRegistry(registry string) (accountId, awsEcrRegion string, ok bool) {
if len(registryParts) < 1 || len(registryParts[0]) < 3 {
return "", "", false
}
return registryParts[0][1], registryParts[0][2], true
return registryParts[0][1], registryParts[0][3], true
}

// Client is a AWS ECR client which can log into the registry and return
Expand Down
33 changes: 28 additions & 5 deletions oci/auth/aws/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,34 @@ func TestParseRegistry(t *testing.T) {
wantRegion: "us-gov-west-1",
wantOK: true,
},
// TODO: Fix: this invalid registry is allowed by the regex.
// {
// registry: ".dkr.ecr.error.amazonaws.com",
// wantOK: false,
// },
{
registry: "012345678901.dkr.ecr.us-secret-region.sc2s.sgov.gov",
wantAccountID: "012345678901",
wantRegion: "us-secret-region",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr-fips.us-ts-region.c2s.ic.gov",
wantAccountID: "012345678901",
wantRegion: "us-ts-region",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr.uk-region.cloud.adc-e.uk",
wantAccountID: "012345678901",
wantRegion: "uk-region",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr.us-ts-region.csp.hci.ic.gov",
wantAccountID: "012345678901",
wantRegion: "us-ts-region",
wantOK: true,
},
{
registry: ".dkr.ecr.error.amazonaws.com",
wantOK: false,
},
Comment on lines +104 to +107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case is unrelated. Please revert it with its comment as before.

{
registry: "gcr.io/foo/bar:baz",
wantOK: false,
Expand Down
Loading