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

fix authentication to ECR public #666

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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