Skip to content

Commit

Permalink
only use basic_auth if the repo matches configured docker_repo
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh committed Dec 19, 2022
1 parent 4b3dc4c commit 11b6f3e
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions internal/provider/resource_ko_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func (o *buildOptions) makeBuilder(ctx context.Context) (*build.Caching, error)

kc := authn.DefaultKeychain
if o.auth != nil {
kc = authn.NewMultiKeychain(staticKeychain{o.auth}, kc)
kc = authn.NewMultiKeychain(staticKeychain{o.dockerRepo, o.auth}, kc)
}
desc, err := remote.Get(ref, remote.WithAuthFromKeychain(kc))
if err != nil {
Expand Down Expand Up @@ -191,8 +191,12 @@ func doBuild(ctx context.Context, opts buildOptions) (string, error) {
return "", fmt.Errorf("build: %v", err)
}

p, err := publish.NewDefault(opts.dockerRepo,
publish.WithAuthFromKeychain(authn.DefaultKeychain))
kc := authn.DefaultKeychain
if opts.auth != nil {
kc = authn.NewMultiKeychain(staticKeychain{opts.dockerRepo, opts.auth}, kc)
}

p, err := publish.NewDefault(opts.dockerRepo, publish.WithAuthFromKeychain(kc))
if err != nil {
return "", fmt.Errorf("NewDefault: %v", err)
}
Expand Down Expand Up @@ -272,10 +276,20 @@ func resourceKoBuildDelete(ctx context.Context, d *schema.ResourceData, meta int
return nil
}

type staticKeychain struct{ b *authn.Basic }
type staticKeychain struct {
repo string
b *authn.Basic
}

func (k staticKeychain) Resolve(authn.Resource) (authn.Authenticator, error) {
return staticAuthenticator{k.b}, nil
func (k staticKeychain) Resolve(r authn.Resource) (authn.Authenticator, error) {
ref, err := name.ParseReference(k.repo)
if err != nil {
return nil, err
}
if r.RegistryStr() == ref.Context().RegistryStr() {
return staticAuthenticator{k.b}, nil
}
return authn.Anonymous, nil
}

type staticAuthenticator struct{ b *authn.Basic }
Expand Down

0 comments on commit 11b6f3e

Please sign in to comment.