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

feat: enhance clean cmd capability #1430

Merged
merged 1 commit into from
Feb 9, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions cmd/cosign/cli/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,41 +29,65 @@ import (
)

func Clean() *cobra.Command {
o := &options.RegistryOptions{}
c := &options.CleanOptions{}

cmd := &cobra.Command{
Use: "clean",
Short: "Remove all signatures from an image.",
Example: " cosign clean <IMAGE>",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return CleanCmd(cmd.Context(), *o, args[0])
return CleanCmd(cmd.Context(), c.Registry, c.CleanType, args[0])
},
}

o.AddFlags(cmd)
c.AddFlags(cmd)
return cmd
}

func CleanCmd(ctx context.Context, regOpts options.RegistryOptions, imageRef string) error {
func CleanCmd(ctx context.Context, regOpts options.RegistryOptions, cleanType, imageRef string) error {
ref, err := name.ParseReference(imageRef)
if err != nil {
return err
}

remoteOpts := regOpts.GetRegistryClientOpts(ctx)

sigRef, err := ociremote.SignatureTag(ref, ociremote.WithRemoteOptions(remoteOpts...))
if err != nil {
return err
}
fmt.Println(sigRef)

fmt.Fprintln(os.Stderr, "Deleting signature metadata...")
attRef, err := ociremote.AttestationTag(ref, ociremote.WithRemoteOptions(remoteOpts...))
if err != nil {
return err
}

err = remote.Delete(sigRef, remoteOpts...)
sbomRef, err := ociremote.SBOMTag(ref, ociremote.WithRemoteOptions(remoteOpts...))
if err != nil {
return err
}

var cleanTags []name.Tag
switch cleanType {
case "signature":
cleanTags = []name.Tag{sigRef}
case "sbom":
cleanTags = []name.Tag{sbomRef}
case "attestation":
dlorenc marked this conversation as resolved.
Show resolved Hide resolved
cleanTags = []name.Tag{attRef}
case "all":
cleanTags = []name.Tag{sigRef, attRef, sbomRef}
}

for _, t := range cleanTags {
fmt.Fprintf(os.Stderr, "Removing %s from %s\n", t.String(), imageRef)

err = remote.Delete(t, remoteOpts...)
if err != nil {
developer-guy marked this conversation as resolved.
Show resolved Hide resolved
fmt.Fprintf(os.Stderr, "could not delete %s from %s\n: %v\n", t.String(), imageRef, err)
}
}

return nil
}
29 changes: 29 additions & 0 deletions cmd/cosign/cli/options/clean.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2022 The Sigstore Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package options

import "github.com/spf13/cobra"

type CleanOptions struct {
Registry RegistryOptions
CleanType string
}

var _ Interface = (*CleanOptions)(nil)

func (c *CleanOptions) AddFlags(cmd *cobra.Command) {
c.Registry.AddFlags(cmd)
cmd.Flags().StringVarP(&c.CleanType, "type", "", "all", "a type of clean: <signature|attestation|sbom|all> (default: all)")
}
1 change: 1 addition & 0 deletions doc/cosign_clean.md

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

4 changes: 2 additions & 2 deletions test/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func TestSignVerifyClean(t *testing.T) {
must(download.SignatureCmd(ctx, options.RegistryOptions{}, imgName), t)

// Now clean signature from the given image
must(cli.CleanCmd(ctx, options.RegistryOptions{}, imgName), t)
must(cli.CleanCmd(ctx, options.RegistryOptions{}, "all", imgName), t)

// It doesn't work
mustErr(verify(pubKeyPath, imgName, true, nil, ""), t)
Expand Down Expand Up @@ -196,7 +196,7 @@ func TestImportSignVerifyClean(t *testing.T) {
must(download.SignatureCmd(ctx, options.RegistryOptions{}, imgName), t)

// Now clean signature from the given image
must(cli.CleanCmd(ctx, options.RegistryOptions{}, imgName), t)
must(cli.CleanCmd(ctx, options.RegistryOptions{}, "all", imgName), t)

// It doesn't work
mustErr(verify(pubKeyPath, imgName, true, nil, ""), t)
Expand Down