Extend AWS ECR regex for c2s support (#20648)

* Extend regex for c2s support

Signed-off-by: Ethan Howell <ethan.c.howell@hotmail.com>

* Add more test cases

Signed-off-by: Ethan Howell <ethan.c.howell@hotmail.com>

* Update region

Signed-off-by: Ethan Howell <ethan.c.howell@hotmail.com>

* Fix region parsing

Signed-off-by: Ethan Howell <ethan.c.howell@hotmail.com>

* Add length check

Signed-off-by: Ethan Howell <ethan.c.howell@hotmail.com>

* Update check

Signed-off-by: Ethan Howell <ethan.c.howell@hotmail.com>

---------

Signed-off-by: Ethan Howell <ethan.c.howell@hotmail.com>
This commit is contained in:
Ethan Howell 2024-08-08 04:34:57 -04:00 committed by GitHub
parent ec77bd9b12
commit ec5dc094d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 3 deletions

View File

@ -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 (
@ -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
}
type factory struct {

View File

@ -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) {