Skip to content

[RFC-0010] Add aws auth library#907

Merged
matheuscscp merged 1 commit intomainfrom
auth-aws
Apr 30, 2025
Merged

[RFC-0010] Add aws auth library#907
matheuscscp merged 1 commit intomainfrom
auth-aws

Conversation

@matheuscscp
Copy link
Copy Markdown
Member

@matheuscscp matheuscscp commented Apr 15, 2025

This PR introduces the aws library from RFC-0010.

Part of: fluxcd/flux2#5022

PR Stack:

Comment thread auth/aws/options.go
Comment on lines +56 to +66
// 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(`([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,
// otherwise empty strings and `false`.
func ParseRegistry(registry string) (accountId, awsEcrRegion string, ok bool) {
registryParts := registryPartRe.FindAllStringSubmatch(registry, -1)
if len(registryParts) < 1 || len(registryParts[0]) < 3 {
return "", "", false
}
return registryParts[0][1], registryParts[0][2], true
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copied from here:

pkg/oci/auth/aws/auth.go

Lines 40 to 53 in f7fc565

// 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(`([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,
// otherwise empty strings and `false`.
func ParseRegistry(registry string) (accountId, awsEcrRegion string, ok bool) {
registryParts := registryPartRe.FindAllStringSubmatch(registry, -1)
if len(registryParts) < 1 || len(registryParts[0]) < 3 {
return "", "", false
}
return registryParts[0][1], registryParts[0][2], true
}

Comment thread auth/aws/options_test.go
Comment on lines +27 to +115
func TestParseRegistry(t *testing.T) {
tests := []struct {
registry string
wantAccountID string
wantRegion string
wantOK bool
}{
{
registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com/foo:v1",
wantAccountID: "012345678901",
wantRegion: "us-east-1",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com/foo",
wantAccountID: "012345678901",
wantRegion: "us-east-1",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com",
wantAccountID: "012345678901",
wantRegion: "us-east-1",
wantOK: true,
},
{
registry: "https://012345678901.dkr.ecr.us-east-1.amazonaws.com/v2/part/part",
wantAccountID: "012345678901",
wantRegion: "us-east-1",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr.cn-north-1.amazonaws.com.cn/foo",
wantAccountID: "012345678901",
wantRegion: "cn-north-1",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr-fips.us-gov-west-1.amazonaws.com",
wantAccountID: "012345678901",
wantRegion: "us-gov-west-1",
wantOK: true,
},
{
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,
},
// TODO: Fix: this invalid registry is allowed by the regex.
// {
// registry: ".dkr.ecr.error.amazonaws.com",
// wantOK: false,
// },
{
registry: "gcr.io/foo/bar:baz",
wantOK: false,
},
}

for _, tt := range tests {
t.Run(tt.registry, func(t *testing.T) {
g := NewWithT(t)

accId, region, ok := aws.ParseRegistry(tt.registry)
g.Expect(ok).To(Equal(tt.wantOK), "unexpected OK")
g.Expect(accId).To(Equal(tt.wantAccountID), "unexpected account IDs")
g.Expect(region).To(Equal(tt.wantRegion), "unexpected regions")
})
}
}
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copied from here:

func TestParseRegistry(t *testing.T) {
tests := []struct {
registry string
wantAccountID string
wantRegion string
wantOK bool
}{
{
registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com/foo:v1",
wantAccountID: "012345678901",
wantRegion: "us-east-1",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com/foo",
wantAccountID: "012345678901",
wantRegion: "us-east-1",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr.us-east-1.amazonaws.com",
wantAccountID: "012345678901",
wantRegion: "us-east-1",
wantOK: true,
},
{
registry: "https://012345678901.dkr.ecr.us-east-1.amazonaws.com/v2/part/part",
wantAccountID: "012345678901",
wantRegion: "us-east-1",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr.cn-north-1.amazonaws.com.cn/foo",
wantAccountID: "012345678901",
wantRegion: "cn-north-1",
wantOK: true,
},
{
registry: "012345678901.dkr.ecr-fips.us-gov-west-1.amazonaws.com",
wantAccountID: "012345678901",
wantRegion: "us-gov-west-1",
wantOK: true,
},
{
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,
},
// TODO: Fix: this invalid registry is allowed by the regex.
// {
// registry: ".dkr.ecr.error.amazonaws.com",
// wantOK: false,
// },
{
registry: "gcr.io/foo/bar:baz",
wantOK: false,
},
}
for _, tt := range tests {
t.Run(tt.registry, func(t *testing.T) {
g := NewWithT(t)
accId, region, ok := ParseRegistry(tt.registry)
g.Expect(ok).To(Equal(tt.wantOK), "unexpected OK")
g.Expect(accId).To(Equal(tt.wantAccountID), "unexpected account IDs")
g.Expect(region).To(Equal(tt.wantRegion), "unexpected regions")
})
}
}

Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
Copy link
Copy Markdown
Member

@stefanprodan stefanprodan left a comment

Choose a reason for hiding this comment

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

LGTM

Thanks @matheuscscp 🏅

Base automatically changed from auth-core to main April 30, 2025 18:11
@matheuscscp matheuscscp merged commit 9f68942 into main Apr 30, 2025
9 checks passed
@matheuscscp matheuscscp deleted the auth-aws branch April 30, 2025 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants