Merged
Conversation
matheuscscp
commented
Apr 15, 2025
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 | ||
| } |
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") | ||
| }) | ||
| } | ||
| } |
4b574f2 to
534d1d6
Compare
1d511b0 to
f3fbba5
Compare
This was referenced Apr 18, 2025
3dfbd1f to
1c55ceb
Compare
Signed-off-by: Matheus Pimenta <matheuscscp@gmail.com>
stefanprodan
approved these changes
Apr 30, 2025
Member
stefanprodan
left a comment
There was a problem hiding this comment.
LGTM
Thanks @matheuscscp 🏅
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces the
awslibrary from RFC-0010.Part of: fluxcd/flux2#5022
PR Stack:
azuregcpaws<---core