-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
336 lines (279 loc) · 9.43 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"time"
"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/purl"
"github.com/aquasecurity/trivy/pkg/report/predicate"
"github.com/aquasecurity/trivy/pkg/sbom"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/static"
ctypes "github.com/google/go-containerregistry/pkg/v1/types"
"github.com/spdx/tools-golang/spdx"
"github.com/spf13/cobra"
)
const (
// ref. https://github.com/opencontainers/image-spec/blob/dd7fd714f5406d39db5fd0602a0e6090929dc85e/annotations.md#pre-defined-annotation-keys
annotationKeyCreated = "org.opencontainers.artifact.created"
annotationKeyDescription = "org.opencontainers.artifact.description"
// Use a Media Type registered with IANA.
// ref. https://github.com/opencontainers/image-spec/blob/dd7fd714f5406d39db5fd0602a0e6090929dc85e/artifact.md#artifact-manifest-property-descriptions
// ref. https://www.iana.org/assignments/media-types/media-types.xhtml
mediaKeyCycloneDX = "application/vnd.cyclonedx+json"
mediaKeySPDX = "application/spdx+json"
// 2023/4/4: Since there is no MediaType specialized for vulnerability information registered with IANA, we use the json type.
mediaKeyCosignVuln = "application/json"
)
var errFailedSBOMDetection = fmt.Errorf("failed to detect SBOM")
var errFailedVulnDetection = fmt.Errorf("failed to detect Cosign Vulnerability")
type referrer struct {
annotations map[string]string
mediaType ctypes.MediaType
bytes []byte
targetRepo name.Digest
targetDesc v1.Descriptor
}
func (r *referrer) Image() (v1.Image, error) {
img, err := mutate.Append(empty.Image, mutate.Addendum{
Layer: static.NewLayer(r.bytes, r.mediaType),
})
if err != nil {
return nil, fmt.Errorf("error appending layer: %w", err)
}
img = mutate.MediaType(img, r.targetDesc.MediaType)
img = mutate.ConfigMediaType(img, r.mediaType)
img = mutate.Annotations(img, r.annotations).(v1.Image)
img = mutate.Subject(img, r.targetDesc).(v1.Image)
return img, nil
}
func (r *referrer) Tag(img v1.Image) (name.Digest, error) {
digest, err := img.Digest()
if err != nil {
return name.Digest{}, fmt.Errorf("error getting image digest: %w", err)
}
tag, err := name.NewDigest(
fmt.Sprintf("%s/%s@%s", r.targetRepo.RegistryStr(), r.targetRepo.RepositoryStr(), digest.String()),
)
if err != nil {
return name.Digest{}, fmt.Errorf("error creating new digest: %w", err)
}
return tag, nil
}
func repoFromPurl(purlStr string) (name.Digest, error) {
p, err := purl.FromString(purlStr)
if err != nil {
return name.Digest{}, fmt.Errorf("error parsing purl: %w", err)
}
url := p.Qualifiers.Map()["repository_url"]
if url == "" {
return name.Digest{}, fmt.Errorf("repository_url not found")
}
digest, err := name.NewDigest(fmt.Sprintf("%s@%s", url, p.Version))
if err != nil {
return name.Digest{}, fmt.Errorf("error creating new digest: %w", err)
}
return digest, nil
}
func repoFromSpdx(spdx spdx.Document2_2) (name.Digest, error) {
for _, pkg := range spdx.Packages {
if pkg.PackageName == spdx.CreationInfo.DocumentName {
for _, ref := range pkg.PackageExternalReferences {
if ref.Category == "PACKAGE-MANAGER" {
return repoFromPurl(ref.Locator)
}
}
}
}
return name.Digest{}, fmt.Errorf("error getting repository from SPDX")
}
func tryReferrerFromSBOM(r io.Reader) (referrer, error) {
b, err := io.ReadAll(r)
if err != nil {
return referrer{}, fmt.Errorf("error reading: %w", err)
}
format, err := sbom.DetectFormat(bytes.NewReader(b))
if format == sbom.FormatUnknown {
return referrer{}, errFailedSBOMDetection
} else if err != nil {
return referrer{}, fmt.Errorf("error detecting SBOM format: %w", err)
}
decoded, err := sbom.Decode(bytes.NewReader(b), format)
if err != nil {
return referrer{}, fmt.Errorf("error decoding SBOM: %w", err)
}
var mediaType ctypes.MediaType
var anns map[string]string
var repo name.Digest
switch format {
case sbom.FormatCycloneDXJSON:
repo, err = repoFromPurl(decoded.CycloneDX.Metadata.Component.BOMRef)
if err != nil {
return referrer{}, fmt.Errorf("error getting repository from CycloneDX: %w", err)
}
anns = map[string]string{
annotationKeyDescription: "CycloneDX JSON SBOM",
}
mediaType = mediaKeyCycloneDX
case sbom.FormatSPDXJSON:
repo, err = repoFromSpdx(*decoded.SPDX)
if err != nil {
return referrer{}, fmt.Errorf("error getting repository from SPDX: %w", err)
}
anns = map[string]string{
annotationKeyDescription: "SPDX JSON SBOM",
}
mediaType = mediaKeySPDX
default:
return referrer{}, fmt.Errorf("unsupported format: %s", format)
}
log.Logger.Infof("SBOM detected: %s", format)
targetDesc, err := remote.Head(repo)
if err != nil {
return referrer{}, fmt.Errorf("error getting descriptor: %w", err)
}
return referrer{
annotations: anns,
mediaType: mediaType,
bytes: b,
targetRepo: repo,
targetDesc: *targetDesc,
}, nil
}
func tryReferrerFromVulnerability(r io.Reader) (referrer, error) {
b, err := io.ReadAll(r)
if err != nil {
return referrer{}, fmt.Errorf("error reading: %w", err)
}
var d predicate.CosignVulnPredicate
if err := json.Unmarshal(b, &d); err != nil {
return referrer{}, fmt.Errorf("failed to unmarshal vulnerability data: %w", errFailedVulnDetection)
}
if len(d.Scanner.Result.Metadata.RepoDigests) == 0 {
return referrer{}, fmt.Errorf("no RepoDigests found in vulnerability data: %w", errFailedVulnDetection)
}
repo, err := name.NewDigest(d.Scanner.Result.Metadata.RepoDigests[0])
if err != nil {
return referrer{}, fmt.Errorf("error creating new digest: %w", err)
}
targetDesc, err := remote.Head(repo)
if err != nil {
return referrer{}, fmt.Errorf("error fetching target descriptor: %w", err)
}
log.Logger.Infof("Cosign vulnerability data detected")
return referrer{
annotations: map[string]string{
annotationKeyDescription: "Vulnerability Scan Report",
annotationKeyCreated: time.Now().Format(time.RFC3339),
},
mediaType: ctypes.MediaType(mediaKeyCosignVuln),
bytes: b,
targetRepo: repo,
targetDesc: *targetDesc,
}, nil
}
func referrerFromReader(r io.Reader) (referrer, error) {
b, err := io.ReadAll(r)
if err != nil {
return referrer{}, fmt.Errorf("error reading: %w", err)
}
var ref referrer
ref, err = tryReferrerFromSBOM(bytes.NewReader(b))
if err == nil {
return ref, nil
} else if err != nil && err != errFailedSBOMDetection {
return referrer{}, fmt.Errorf("error processing SBOM: %w", err)
}
log.Logger.Infof("Failed to detect a valid SBOM: ensure the provided SBOM is generated by Trivy, as only Trivy-generated SBOMs are currently supported")
ref, err = tryReferrerFromVulnerability(bytes.NewReader(b))
if err == nil {
return ref, nil
} else if err != nil && err != errFailedVulnDetection {
return referrer{}, fmt.Errorf("error processing vulnerability: %w", err)
}
log.Logger.Infof("Failed to detect Cosign vulnerability format")
return referrer{}, fmt.Errorf("failed to detect referrer type")
}
func putReferrer(r io.Reader) error {
ref, err := referrerFromReader(r)
if err != nil {
return fmt.Errorf("error getting referrer: %w", err)
}
img, err := ref.Image()
if err != nil {
return fmt.Errorf("error getting image: %w", err)
}
tag, err := ref.Tag(img)
if err != nil {
return fmt.Errorf("error getting tag: %w", err)
}
log.Logger.Infof("Pushing referrer to %s", tag.String())
err = remote.Write(tag, img, remote.WithAuthFromKeychain(authn.DefaultKeychain))
if err != nil {
return fmt.Errorf("error pushing referrer: %w", err)
}
return nil
}
func main() {
rootCmd := &cobra.Command{
Short: "A Trivy plugin for oci referrers",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return fmt.Errorf("error getting debug flag: %w", err)
}
quiet, err := cmd.Flags().GetBool("quiet")
if err != nil {
return fmt.Errorf("error getting quiet flag: %w", err)
}
if err := log.InitLogger(debug, quiet); err != nil {
return err
}
return nil
},
}
rootCmd.PersistentFlags().BoolP("debug", "d", false, "debug mode")
rootCmd.PersistentFlags().BoolP("quiet", "q", false, "suppress log output")
putCmd := &cobra.Command{
Use: "put",
Short: "put a referrer to the oci registry",
Example: ` trivy image -q -f cyclonedx YOUR_IMAGE | trivy referrer put
# Put SBOM attestation
trivy referrer put -f sbom.json`,
RunE: func(cmd *cobra.Command, args []string) error {
path, err := cmd.Flags().GetString("file")
if err != nil {
return fmt.Errorf("error getting file path: %w", err)
}
var reader io.Reader
if path != "" {
fp, err := os.Open(path)
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}
defer fp.Close()
reader = fp
} else {
reader = os.Stdin
}
err = putReferrer(reader)
if err != nil {
return fmt.Errorf("error putting referrer: %w", err)
}
return nil
},
}
putCmd.Flags().StringP("file", "f", "", "file path. If a file path is not specified, it will accept input from the standard input.")
rootCmd.AddCommand(putCmd)
if err := putCmd.Execute(); err != nil {
log.Logger.Fatal(err)
}
}