Skip to content

Commit

Permalink
fix authentication to ECR public
Browse files Browse the repository at this point in the history
Signed-off-by: Achille Roussel <[email protected]>
  • Loading branch information
achille-roussel committed Jan 4, 2025
1 parent 7155ab0 commit bf7372d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 4 deletions.
6 changes: 5 additions & 1 deletion internal/provider/authentication_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ func normalizeECRPasswordForDockerCLIUsage(password string) string {
return password[4:]
}

func isECRPublicRepositoryURL(url string) bool {
return url == "public.ecr.aws"
}

func isECRRepositoryURL(url string) bool {
if url == "public.ecr.aws" {
if isECRPublicRepositoryURL(url) {
return true
}
// Regexp is based on the ecr urls shown in https://docs.aws.amazon.com/AmazonECR/latest/userguide/registry_auth.html
Expand Down
10 changes: 9 additions & 1 deletion internal/provider/authentication_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ import (
"testing"
)

func TestIsECRRepositoryURL(t *testing.T) {
func TestIsECRPublicRepositoryURL(t *testing.T) {
if !isECRPublicRepositoryURL("public.ecr.aws") {
t.Fatalf("Expected true")
}
if isECRPublicRepositoryURL("public.ecr.aws.com") {
t.Fatalf("Expected false")
}
}

func TestIsECRRepositoryURL(t *testing.T) {
if !isECRRepositoryURL("2385929435838.dkr.ecr.eu-central-1.amazonaws.com") {
t.Fatalf("Expected true")
}
Expand Down
5 changes: 4 additions & 1 deletion internal/provider/data_source_docker_registry_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ func getImageDigest(registry string, registryWithProtocol string, image, tag, us
if registry != "ghcr.io" && !isECRRepositoryURL(registry) && !isAzureCRRepositoryURL(registry) && registry != "gcr.io" {
req.SetBasicAuth(username, password)
} else {
if isECRRepositoryURL(registry) {
if isECRPublicRepositoryURL(registry) {
password = normalizeECRPasswordForHTTPUsage(password)
req.Header.Add("Authorization", "Bearer "+password)
} else if isECRRepositoryURL(registry) {
password = normalizeECRPasswordForHTTPUsage(password)
req.Header.Add("Authorization", "Basic "+password)
} else {
Expand Down
5 changes: 4 additions & 1 deletion internal/provider/resource_docker_registry_image_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ func deleteDockerRegistryImage(pushOpts internalPushImageOptions, registryWithPr
if pushOpts.Registry != "ghcr.io" && !isECRRepositoryURL(pushOpts.Registry) && !isAzureCRRepositoryURL(pushOpts.Registry) && pushOpts.Registry != "gcr.io" {
req.SetBasicAuth(username, password)
} else {
if isECRRepositoryURL(pushOpts.Registry) {
if isECRPublicRepositoryURL(pushOpts.Registry) {
password = normalizeECRPasswordForHTTPUsage(password)
req.Header.Add("Authorization", "Bearer "+password)
} else if isECRRepositoryURL(pushOpts.Registry) {
password = normalizeECRPasswordForHTTPUsage(password)
req.Header.Add("Authorization", "Basic "+password)
} else {
Expand Down

0 comments on commit bf7372d

Please sign in to comment.