Skip to content

Commit

Permalink
add registry options to cosign save (#3645)
Browse files Browse the repository at this point in the history
Signed-off-by: JasonPowr <japower@redhat.com>
  • Loading branch information
JasonPowr committed Apr 17, 2024
1 parent e036af8 commit 02b1b26
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions cmd/cosign/cli/options/save.go
Expand Up @@ -22,12 +22,14 @@ import (
// SaveOptions is the top level wrapper for the load command.
type SaveOptions struct {
Directory string
Registry RegistryOptions
}

var _ Interface = (*SaveOptions)(nil)

// AddFlags implements Interface
func (o *SaveOptions) AddFlags(cmd *cobra.Command) {
o.Registry.AddFlags(cmd)
cmd.Flags().StringVar(&o.Directory, "dir", "",
"path to dir where the signed image should be stored on disk")
_ = cmd.Flags().SetAnnotation("dir", cobra.BashCompSubdirsInDir, []string{})
Expand Down
16 changes: 11 additions & 5 deletions cmd/cosign/cli/save.go
Expand Up @@ -47,27 +47,33 @@ func Save() *cobra.Command {
return cmd
}

func SaveCmd(_ context.Context, opts options.SaveOptions, imageRef string) error {
ref, err := name.ParseReference(imageRef)
func SaveCmd(ctx context.Context, opts options.SaveOptions, imageRef string) error {
regOpts := opts.Registry
regClientOpts, err := regOpts.ClientOpts(ctx)
if err != nil {
return fmt.Errorf("constructing client options: %w", err)
}

ref, err := name.ParseReference(imageRef, opts.Registry.NameOptions()...)
if err != nil {
return fmt.Errorf("parsing image name %s: %w", imageRef, err)
}

se, err := ociremote.SignedEntity(ref)
se, err := ociremote.SignedEntity(ref, regClientOpts...)
if err != nil {
return fmt.Errorf("signed entity: %w", err)
}

if _, ok := se.(oci.SignedImage); ok {
si, err := ociremote.SignedImage(ref)
si, err := ociremote.SignedImage(ref, regClientOpts...)
if err != nil {
return fmt.Errorf("getting signed image: %w", err)
}
return layout.WriteSignedImage(opts.Directory, si)
}

if _, ok := se.(oci.SignedImageIndex); ok {
sii, err := ociremote.SignedImageIndex(ref)
sii, err := ociremote.SignedImageIndex(ref, regClientOpts...)
if err != nil {
return fmt.Errorf("getting signed image index: %w", err)
}
Expand Down
11 changes: 9 additions & 2 deletions doc/cosign_save.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 02b1b26

Please sign in to comment.