-
Notifications
You must be signed in to change notification settings - Fork 501
/
clean.go
127 lines (110 loc) · 3.31 KB
/
clean.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// Copyright 2021 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 cli
import (
"context"
"errors"
"fmt"
"net/http"
"os"
"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/spf13/cobra"
"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/sigstore/cosign/pkg/cosign"
ociremote "github.com/sigstore/cosign/pkg/oci/remote"
)
func Clean() *cobra.Command {
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(), c.Registry, c.CleanType, args[0], c.Force)
},
}
c.AddFlags(cmd)
return cmd
}
func CleanCmd(ctx context.Context, regOpts options.RegistryOptions, cleanType, imageRef string, force bool) error {
if !force {
ok, err := cosign.ConfirmPrompt(prompt(cleanType), true /*destructive*/)
if err != nil {
return err
}
if !ok {
return nil
}
}
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
}
attRef, err := ociremote.AttestationTag(ref, ociremote.WithRemoteOptions(remoteOpts...))
if err != nil {
return err
}
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":
cleanTags = []name.Tag{attRef}
case "all":
cleanTags = []name.Tag{sigRef, attRef, sbomRef}
}
for _, t := range cleanTags {
if err := remote.Delete(t, remoteOpts...); err != nil {
var te *transport.Error
if errors.As(err, &te) && te.StatusCode == http.StatusNotFound {
// If the tag doesn't exist, some registries may
// respond with a 404, which shouldn't be considered an
// error.
} else {
fmt.Fprintf(os.Stderr, "could not delete %s from %s\n: %v\n", t, imageRef, err)
}
} else {
fmt.Fprintf(os.Stderr, "Removed %s from %s\n", t, imageRef)
}
}
return nil
}
func prompt(cleanType string) string {
switch cleanType {
case "signature":
return "WARNING: this will remove all signatures from the image"
case "sbom":
return "WARNING: this will remove all SBOMs from the image"
case "attestation":
return "WARNING: this will remove all attestations from the image"
case "all":
return "WARNING: this will remove all signatures, SBOMs and attestations from the image"
}
return ""
}