From 1a4b57a6235be0396b50a0d9c0843334dae80266 Mon Sep 17 00:00:00 2001 From: Ben Date: Fri, 23 Sep 2022 18:23:07 +0000 Subject: [PATCH] Add regex to support ECR as OCI Helm registry This change adds a new regex to parse AWS ECR OCI URLs when to allow URLs that point to the root of an AWS account's ECR registry, instead of forcing all repositories to contain a "/" character. Signed-off-by: Ben --- oci/auth/aws/auth.go | 10 +++++++--- oci/auth/aws/auth_test.go | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/oci/auth/aws/auth.go b/oci/auth/aws/auth.go index 0442772c3..d150def8b 100644 --- a/oci/auth/aws/auth.go +++ b/oci/auth/aws/auth.go @@ -33,15 +33,19 @@ import ( "github.com/fluxcd/pkg/oci" ) -var registryPartRe = regexp.MustCompile(`([0-9+]*).dkr.ecr.([^/.]*)\.(amazonaws\.com[.cn]*)/([^:]+):?(.*)`) +var fullImagePathRe = regexp.MustCompile(`([0-9+]*).dkr.ecr.([^/.]*)\.(amazonaws\.com[.cn]*)/([^:]+):?(.*)`) +var registryPartRe = regexp.MustCompile(`([0-9+]*).dkr.ecr.([^/.]*)\.(amazonaws\.com[.cn]*)`) // ParseImage returns the AWS account ID and region and `true` if // the image repository is hosted in AWS's Elastic Container Registry, // otherwise empty strings and `false`. func ParseImage(image string) (accountId, awsEcrRegion string, ok bool) { - registryParts := registryPartRe.FindAllStringSubmatch(image, -1) + registryParts := fullImagePathRe.FindAllStringSubmatch(image, -1) if len(registryParts) < 1 || len(registryParts[0]) < 3 { - return "", "", false + registryParts = registryPartRe.FindAllStringSubmatch(image, -1) + if len(registryParts) < 1 || len(registryParts[0]) < 3 { + return "", "", false + } } return registryParts[0][1], registryParts[0][2], true } diff --git a/oci/auth/aws/auth_test.go b/oci/auth/aws/auth_test.go index 3ced59fa7..578843c92 100644 --- a/oci/auth/aws/auth_test.go +++ b/oci/auth/aws/auth_test.go @@ -51,8 +51,10 @@ func TestParseImage(t *testing.T) { wantOK: true, }, { - image: "012345678901.dkr.ecr.us-east-1.amazonaws.com", - wantOK: false, + image: "012345678901.dkr.ecr.us-east-1.amazonaws.com", + wantAccountID: "012345678901", + wantRegion: "us-east-1", + wantOK: true, }, { image: "gcr.io/foo/bar:baz",