Skip to content

Commit

Permalink
feat: enhance clean cmd capability
Browse files Browse the repository at this point in the history
Signed-off-by: Batuhan Apaydın <batuhan.apaydin@trendyol.com>
  • Loading branch information
developer-guy committed Feb 9, 2022
1 parent 6b42e47 commit 1c72c23
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 11 deletions.
33 changes: 25 additions & 8 deletions cmd/cosign/cli/clean.go
Expand Up @@ -29,41 +29,58 @@ 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...")

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

var cleanTags []name.Tag
switch cleanType {
case "signature":
cleanTags = []name.Tag{sigRef}
case "attestation":
cleanTags = []name.Tag{sigRef}
case "all":
cleanTags = []name.Tag{sigRef, attRef}
}

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

err = remote.Delete(t, remoteOpts...)
if err != nil {
return err
}
}

return nil
}
29 changes: 29 additions & 0 deletions cmd/cosign/cli/options/clean.go
@@ -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|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.

6 changes: 3 additions & 3 deletions test/e2e_test.go
Expand Up @@ -167,8 +167,8 @@ 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)
1
// 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

0 comments on commit 1c72c23

Please sign in to comment.