Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix copy without any flag set #3409

Merged
merged 1 commit into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/cosign/cli/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func Copy() *cobra.Command {
cosign copy example.com/src:latest example.com/dest:latest

# copy the signatures only
cosign copy --only=sign example.com/src example.com/dest
cosign copy --only=sig example.com/src example.com/dest

# copy the signatures, attestations, sbom only
cosign copy --only=sign,att,sbom example.com/src example.com/dest
cosign copy --only=sig,att,sbom example.com/src example.com/dest

# overwrite destination image and signatures
cosign copy -f example.com/src example.com/dest
Expand Down
17 changes: 11 additions & 6 deletions cmd/cosign/cli/copy/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ func CopyCmd(ctx context.Context, regOpts options.RegistryOptions, srcImg, dstIm
return err
}

onlyFlagSet := false
tags := parseOnlyOpt(copyOnly, sigOnly)
if len(tags) > 0 {
onlyFlagSet = true
} else {
tags = []tagMap{ociremote.SignatureTag, ociremote.AttestationTag, ociremote.SBOMTag}
}
if err := walk.SignedEntity(gctx, root, func(ctx context.Context, se oci.SignedEntity) error {
// Both of the SignedEntity types implement Digest()
h, err := se.Digest()
Expand Down Expand Up @@ -126,7 +132,7 @@ func CopyCmd(ctx context.Context, regOpts options.RegistryOptions, srcImg, dstIm
}

// If we're only copying sig/att/sbom, we have nothing left to do.
if len(tags) > 0 {
if onlyFlagSet {
return nil
}

Expand Down Expand Up @@ -174,17 +180,16 @@ func remoteCopy(ctx context.Context, pusher *remote.Pusher, src, dest name.Refer
return pusher.Push(ctx, dest, got)
}

func parseOnlyOpt(str string, sigOnly bool) []tagMap {
func parseOnlyOpt(onlyFlag string, sigOnly bool) []tagMap {
var tags []tagMap
items := strings.Split(str, ",")
tagSet := sets.New(items...)
tagSet := sets.New(strings.Split(onlyFlag, ",")...)

if sigOnly {
fmt.Fprintf(os.Stderr, "--sig-only is deprecated, use --only=sign instead")
fmt.Fprintf(os.Stderr, "--sig-only is deprecated, use --only=sig instead")
tagSet.Insert("sign")
}

if tagSet.Has("sign") {
if tagSet.Has("sig") {
tags = append(tags, ociremote.SignatureTag)
}
if tagSet.Has("sbom") {
Expand Down
4 changes: 2 additions & 2 deletions doc/cosign_copy.md

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