Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2020 from ibuildthecloud/fields
Browse files Browse the repository at this point in the history
Don't send index.docker.io auth for local image references
  • Loading branch information
ibuildthecloud authored Aug 4, 2023
2 parents ff81ce7 + 45966e6 commit 0727f59
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 92 deletions.
35 changes: 35 additions & 0 deletions pkg/cli/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cli

import (
"context"

apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
"github.com/acorn-io/runtime/pkg/imagesource"
"github.com/acorn-io/runtime/pkg/tags"
"github.com/google/go-containerregistry/pkg/name"
)

func getAuthForImage(ctx context.Context, clientFactory ClientFactory, image string) (*apiv1.RegistryAuth, error) {
if tags.IsLocalReference(image) {
return nil, nil
}

c, err := clientFactory.CreateDefault()
if err != nil {
return nil, err
}

ref, err := name.ParseReference(image)
if err != nil {
// not failing on malformed image names
return nil, nil
}

creds, err := imagesource.GetCreds(c)
if err != nil {
return nil, err
}

auth, _, err := creds(ctx, ref.Context().RegistryStr())
return auth, err
}
10 changes: 1 addition & 9 deletions pkg/cli/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,7 @@ func (s *Build) Run(cmd *cobra.Command, args []string) error {

if s.Push {
for _, tag := range s.Tag {
parsedTag, err := name.NewTag(tag)
if err != nil {
return err
}
creds, err := imagesource.GetCreds(c)
if err != nil {
return err
}
auth, _, err := creds(cmd.Context(), parsedTag.RegistryStr())
auth, err := getAuthForImage(cmd.Context(), s.client, tag)
if err != nil {
return err
}
Expand Down
26 changes: 7 additions & 19 deletions pkg/cli/images_detail.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package cli

import (
apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/cli/builder/table"
"github.com/acorn-io/runtime/pkg/client"
"github.com/acorn-io/runtime/pkg/credentials"
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -33,27 +32,16 @@ func (a *ImageDetails) Run(cmd *cobra.Command, args []string) error {
return err
}

var nested string
var (
nested string
auth *apiv1.RegistryAuth
)

if len(args) > 1 {
nested = args[1]
}

ref, err := name.ParseReference(args[0])
if err != nil {
return err
}

cfg, err := a.client.Options().CLIConfig()
if err != nil {
return err
}

creds, err := credentials.NewStore(cfg, c)
if err != nil {
return err
}

auth, _, err := creds.Get(cmd.Context(), ref.Context().RegistryStr())
auth, err = getAuthForImage(cmd.Context(), a.client, args[0])
if err != nil {
return err
}
Expand Down
20 changes: 6 additions & 14 deletions pkg/cli/images_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (
"os"
"strings"

apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/client"
acornsign "github.com/acorn-io/runtime/pkg/cosign"
"github.com/acorn-io/runtime/pkg/imagesource"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pterm/pterm"
"github.com/sigstore/cosign/v2/pkg/cosign"
Expand Down Expand Up @@ -53,20 +51,14 @@ func (a *ImageSign) Run(cmd *cobra.Command, args []string) error {
return err
}

var auth *apiv1.RegistryAuth
ref, err := name.ParseReference(imageName)
if err == nil { // not failing here, since it could be a local image
creds, err := imagesource.GetCreds(c)
if err != nil {
return err
}

auth, _, err = creds(cmd.Context(), ref.Context().RegistryStr())
if err != nil {
return err
}
auth, err := getAuthForImage(cmd.Context(), a.client, imageName)
if err != nil {
return err
}

// not failing here, since it could be a local image
ref, _ := name.ParseReference(imageName)

details, err := c.ImageDetails(cmd.Context(), args[0], &client.ImageDetailsOptions{
Auth: auth,
})
Expand Down
20 changes: 6 additions & 14 deletions pkg/cli/images_verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"fmt"
"os"

apiv1 "github.com/acorn-io/runtime/pkg/apis/api.acorn.io/v1"
cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/client"
acornsign "github.com/acorn-io/runtime/pkg/cosign"
"github.com/acorn-io/runtime/pkg/imagesource"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,20 +51,14 @@ func (a *ImageVerify) Run(cmd *cobra.Command, args []string) error {
return err
}

var auth *apiv1.RegistryAuth
ref, err := name.ParseReference(imageName)
if err == nil { // not failing here, since it could be a local image
creds, err := imagesource.GetCreds(c)
if err != nil {
return err
}

auth, _, err = creds(cmd.Context(), ref.Context().RegistryStr())
if err != nil {
return err
}
auth, err := getAuthForImage(cmd.Context(), a.client, imageName)
if err != nil {
return err
}

// not failing here, since it could be a local image
ref, _ := name.ParseReference(imageName)

details, err := c.ImageDetails(cmd.Context(), args[0], &client.ImageDetailsOptions{
Auth: auth,
})
Expand Down
19 changes: 1 addition & 18 deletions pkg/cli/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (

cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/client"
"github.com/acorn-io/runtime/pkg/credentials"
"github.com/acorn-io/runtime/pkg/progressbar"
"github.com/google/go-containerregistry/pkg/name"
"github.com/spf13/cobra"
)

Expand All @@ -33,22 +31,7 @@ func (s *Pull) Run(cmd *cobra.Command, args []string) error {
return err
}

ref, err := name.ParseReference(args[0])
if err != nil {
return err
}

cfg, err := s.client.Options().CLIConfig()
if err != nil {
return err
}

creds, err := credentials.NewStore(cfg, c)
if err != nil {
return err
}

auth, _, err := creds.Get(cmd.Context(), ref.Context().RegistryStr())
auth, err := getAuthForImage(cmd.Context(), s.client, args[0])
if err != nil {
return err
}
Expand Down
19 changes: 1 addition & 18 deletions pkg/cli/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package cli
import (
cli "github.com/acorn-io/runtime/pkg/cli/builder"
"github.com/acorn-io/runtime/pkg/client"
"github.com/acorn-io/runtime/pkg/credentials"
"github.com/acorn-io/runtime/pkg/progressbar"
"github.com/google/go-containerregistry/pkg/name"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -33,22 +31,7 @@ func (s *Push) Run(cmd *cobra.Command, args []string) error {
return err
}

cfg, err := s.client.Options().CLIConfig()
if err != nil {
return err
}

creds, err := credentials.NewStore(cfg, c)
if err != nil {
return err
}

tag, err := name.NewTag(args[0])
if err != nil {
return err
}

auth, _, err := creds.Get(cmd.Context(), tag.RegistryStr())
auth, err := getAuthForImage(cmd.Context(), s.client, args[0])
if err != nil {
return err
}
Expand Down

0 comments on commit 0727f59

Please sign in to comment.