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: logout and try ECR Public pull again when token is expired #68

Merged
merged 1 commit into from
Jun 29, 2023
Merged
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
33 changes: 29 additions & 4 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/postrender"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/repo"
"helm.sh/helm/v3/pkg/strvals"
Expand Down Expand Up @@ -75,11 +76,29 @@ func (h *Helm) DownloadChart() (*chart.Chart, error) {

// Download chart archive into memory
data, err := g.Get(chartPath)
if err != nil {
if strings.HasPrefix(h.RepositoryURL, "oci://public.ecr.aws") {
msg := "Please review: https://docs.aws.amazon.com/AmazonECR/latest/public/public-troubleshooting.html"
err = fmt.Errorf("%w\n%s", err, msg)

// If ECR Public is returning a 403 Forbidden error, then log out and try again
// https://docs.aws.amazon.com/AmazonECR/latest/public/public-troubleshooting.html
if h.isECRPublicAuthError(err) {
fmt.Println("ECR Public is returning a 403 Forbidden error. Logging out and trying again...")

registryClient, e := registry.NewClient()
if e != nil {
return nil, fmt.Errorf("failed to create registry client: %w", e)
}

e = registryClient.Logout("public.ecr.aws")
if e != nil {
return nil, fmt.Errorf("failed to log out of ECR Public: %w", e)
}

ociGetter, e := getter.NewOCIGetter()
if e != nil {
return nil, e
}
data, err = ociGetter.Get(chartPath)
}
if err != nil {
return nil, err
}

Expand Down Expand Up @@ -144,6 +163,12 @@ func (h *Helm) Install(chart *chart.Chart, kubeContext string) error {
return nil
}

func (h *Helm) isECRPublicAuthError(err error) bool {
return err != nil &&
strings.HasPrefix(h.RepositoryURL, "oci://public.ecr.aws") &&
strings.HasSuffix(err.Error(), "403 Forbidden")
}

func List(kubeContext string) ([]*release.Release, error) {
actionConfig, err := initialize(kubeContext, "")
if err != nil {
Expand Down