Skip to content

Commit

Permalink
add repository to the credentials prompt (#2596)
Browse files Browse the repository at this point in the history
* simplify more

Signed-off-by: David Fridrich <[email protected]>

* fix

Signed-off-by: David Fridrich <[email protected]>

* fix creds test

Signed-off-by: David Fridrich <[email protected]>

* name fix

Signed-off-by: David Fridrich <[email protected]>

* naming

Signed-off-by: David Fridrich <[email protected]>

---------

Signed-off-by: David Fridrich <[email protected]>
  • Loading branch information
gauron99 authored Dec 2, 2024
1 parent 0e150e0 commit 582536f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
9 changes: 4 additions & 5 deletions cmd/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
9 changes: 7 additions & 2 deletions pkg/docker/creds/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/docker/creds/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand Down

0 comments on commit 582536f

Please sign in to comment.