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

Extend AWS ECR regex for c2s support #20648

Merged
merged 14 commits into from
Aug 8, 2024
6 changes: 3 additions & 3 deletions src/pkg/reg/adapter/awsecr/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

const (
ecrPattern = "https://(?:api|(\\d+)\\.dkr)\\.ecr\\.([\\w\\-]+)\\.amazonaws\\.com"
ecrPattern = "https://(?:api|(\\d+)\\.dkr)\\.ecr(\\-fips)?\\.([\\w\\-]+)\\.(amazonaws\\.com(\\.cn)?|sc2s\\.sgov\\.gov|c2s\\.ic\\.gov)"
)

var (
Expand Down Expand Up @@ -64,10 +64,10 @@ func newAdapter(registry *model.Registry) (*adapter, error) {

func parseAccountRegion(url string) (string, string, error) {
rs := ecrRegexp.FindStringSubmatch(url)
if rs == nil {
if rs == nil || len(rs) < 4 {
return "", "", errors.New("bad aws url")
}
return rs[1], rs[2], nil
return rs[1], rs[3], nil
Copy link
Member

Choose a reason for hiding this comment

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

Please add length check for rs before this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to add it, but shouldn't that always evaluate to true based on the docs for FindStringSubmatch? In the event of no match it would just be nil.

}

type factory struct {
Expand Down
32 changes: 32 additions & 0 deletions src/pkg/reg/adapter/awsecr/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,38 @@ func TestAdapter_NewAdapter(t *testing.T) {
assert.Nil(t, adapter)
assert.NotNil(t, err)

adapter, err = newAdapter(&model.Registry{
Type: model.RegistryTypeAwsEcr,
Credential: &model.Credential{
AccessKey: "xxx",
AccessSecret: "ppp",
},
URL: "https://123456.dkr.ecr-fips.test-region.amazonaws.com",
})
assert.Nil(t, err)
assert.NotNil(t, adapter)

adapter, err = newAdapter(&model.Registry{
Type: model.RegistryTypeAwsEcr,
Credential: &model.Credential{
AccessKey: "xxx",
AccessSecret: "ppp",
},
URL: "https://123456.dkr.ecr.us-isob-east-1.sc2s.sgov.gov",
})
assert.Nil(t, err)
assert.NotNil(t, adapter)

adapter, err = newAdapter(&model.Registry{
Type: model.RegistryTypeAwsEcr,
Credential: &model.Credential{
AccessKey: "xxx",
AccessSecret: "ppp",
},
URL: "https://123456.dkr.ecr.us-iso-east-1.c2s.ic.gov",
})
assert.Nil(t, err)
assert.NotNil(t, adapter)
}

func getMockAdapter(t *testing.T, hasCred, health bool) (*adapter, *httptest.Server) {
Expand Down
Loading