From 582536f922bd2ce9c91aa6afd2de4f747d32f54e Mon Sep 17 00:00:00 2001 From: David Fridrich <49119790+gauron99@users.noreply.github.com> Date: Mon, 2 Dec 2024 17:22:45 +0100 Subject: [PATCH] add repository to the credentials prompt (#2596) * simplify more Signed-off-by: David Fridrich * fix Signed-off-by: David Fridrich * fix creds test Signed-off-by: David Fridrich * name fix Signed-off-by: David Fridrich * naming Signed-off-by: David Fridrich --------- Signed-off-by: David Fridrich --- cmd/prompt/prompt.go | 9 ++++----- pkg/docker/creds/credentials.go | 9 +++++++-- pkg/docker/creds/credentials_test.go | 4 ++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cmd/prompt/prompt.go b/cmd/prompt/prompt.go index db7f7c9b53..109620afde 100644 --- a/cmd/prompt/prompt.go +++ b/cmd/prompt/prompt.go @@ -15,16 +15,15 @@ import ( "knative.dev/func/pkg/docker/creds" ) -func NewPromptForCredentials(in io.Reader, out, errOut io.Writer) func(registry string) (docker.Credentials, error) { +func NewPromptForCredentials(in io.Reader, out, errOut io.Writer) func(repository string) (docker.Credentials, error) { firstTime := true - return func(registry string) (docker.Credentials, error) { + return func(repository string) (docker.Credentials, error) { var result docker.Credentials - if firstTime { firstTime = false - fmt.Fprintf(out, "Please provide credentials for image registry (%s).\n", registry) + fmt.Fprintf(out, "Please provide credentials for image repository '%s'.\n", repository) } else { - fmt.Fprintln(out, "Incorrect credentials, please try again.") + fmt.Fprintf(out, "Incorrect credentials for repository '%s'. Please make sure the repository is correct and try again.\n", repository) } var qs = []*survey.Question{ diff --git a/pkg/docker/creds/credentials.go b/pkg/docker/creds/credentials.go index 4e6aef24fe..47e15a28e0 100644 --- a/pkg/docker/creds/credentials.go +++ b/pkg/docker/creds/credentials.go @@ -236,7 +236,6 @@ func (c *credentialsProvider) getCredentials(ctx context.Context, image string) } registry := ref.Context().RegistryStr() - for _, load := range c.credentialLoaders { result, err = load(registry) @@ -263,8 +262,14 @@ func (c *credentialsProvider) getCredentials(ctx context.Context, image string) return docker.Credentials{}, ErrCredentialsNotFound } + // this is [registry] / [repository] + // this is index.io / user/imagename + repository := registry + "/" + ref.Context().RepositoryStr() + + // the trying-to-actually-authorize cycle for { - result, err = c.promptForCredentials(registry) + // use repo here to print it out in prompt + result, err = c.promptForCredentials(repository) if err != nil { return docker.Credentials{}, err } diff --git a/pkg/docker/creds/credentials_test.go b/pkg/docker/creds/credentials_test.go index c1227d152e..0a09a4e562 100644 --- a/pkg/docker/creds/credentials_test.go +++ b/pkg/docker/creds/credentials_test.go @@ -870,6 +870,8 @@ func pwdCbkFirstWrongThenCorrect(t *testing.T) func(registry string) (Credential t.Helper() var firstInvocation bool return func(registry string) (Credentials, error) { + // registry is in form of registry/repository, need to extract registry only + registry = strings.Split(registry, "/")[0] if registry != "index.docker.io" && registry != "quay.io" { return Credentials{}, fmt.Errorf("unexpected registry: %s", registry) } @@ -882,6 +884,8 @@ func pwdCbkFirstWrongThenCorrect(t *testing.T) func(registry string) (Credential } func correctPwdCallback(registry string) (Credentials, error) { + // registry is in form of registry/repository, need to extract registry only + registry = strings.Split(registry, "/")[0] if registry == "index.docker.io" { return Credentials{Username: dockerIoUser, Password: dockerIoUserPwd}, nil }