This repository has been archived by the owner on Mar 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
implementation.go
265 lines (232 loc) · 7.52 KB
/
implementation.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
/*
Copyright 2022 Chainguard, Inc.
SPDX-License-Identifier: Apache-2.0
*/
package ctl
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"regexp"
"github.com/google/go-containerregistry/pkg/name"
gosarif "github.com/owenrumney/go-sarif/sarif"
ssldsse "github.com/secure-systems-lab/go-securesystemslib/dsse"
"github.com/sigstore/cosign/cmd/cosign/cli/options"
"github.com/sigstore/cosign/pkg/cosign"
"github.com/sigstore/cosign/pkg/oci/mutate"
ociremote "github.com/sigstore/cosign/pkg/oci/remote"
"github.com/sigstore/cosign/pkg/oci/static"
"github.com/sigstore/cosign/pkg/types"
"github.com/sirupsen/logrus"
"sigs.k8s.io/release-utils/util"
"chainguard.dev/vex/pkg/attestation"
"chainguard.dev/vex/pkg/sarif"
"chainguard.dev/vex/pkg/vex"
)
const IntotoPayloadType = "application/vnd.in-toto+json"
type Implementation interface {
ApplySingleVEX(*sarif.Report, *vex.VEX) (*sarif.Report, error)
SortDocuments([]*vex.VEX) []*vex.VEX
OpenVexData(Options, []string) ([]*vex.VEX, error)
Sort(docs []*vex.VEX) []*vex.VEX
AttestationBytes(*attestation.Attestation) ([]byte, error)
Attach(context.Context, *attestation.Attestation, string) error
SourceType(uri string) (string, error)
ReadImageAttestations(context.Context, Options, string) ([]*vex.VEX, error)
}
type defaultVexCtlImplementation struct{}
var cveRegexp regexp.Regexp
func init() {
cveRegexp = *regexp.MustCompile(`^(CVE-\d+-\d+)`)
}
func (impl *defaultVexCtlImplementation) SortDocuments(docs []*vex.VEX) []*vex.VEX {
return vex.Sort(docs)
}
func (impl *defaultVexCtlImplementation) ApplySingleVEX(report *sarif.Report, vexDoc *vex.VEX) (*sarif.Report, error) {
newReport := *report
logrus.Infof("VEX document contains %d statements", len(vexDoc.Statements))
logrus.Infof("+%v Runs: %d\n", report, len(report.Runs))
// Search for negative VEX statements, that is those that cancel a CVE
for i := range report.Runs {
newResults := []*gosarif.Result{}
logrus.Infof("Inspecting run #%d containing %d results", i, len(report.Runs[i].Results))
for _, res := range report.Runs[i].Results {
// Normalize the CVE IDs
m := cveRegexp.FindStringSubmatch(*res.RuleID)
if len(m) != 2 {
logrus.Errorf(
"Invalid rulename in sarif report, expected CVE identifier, got %s",
*res.RuleID,
)
newResults = append(newResults, res)
continue
}
id := m[1]
// TODO: Trim rule ID to CVE as Grype adds junk to the CVE ID
statement := vexDoc.StatementFromID(id)
logrus.Infof("Checking %s", id)
if statement != nil {
logrus.Infof("Statement is for %s and status is %s", statement.Vulnerability, statement.Status)
if statement.Status == vex.StatusNotAffected ||
statement.Status == vex.StatusFixed {
logrus.Infof("Found VEX Statement for %s: %s", id, statement.Status)
continue
}
}
newResults = append(newResults, res)
}
newReport.Runs[i].Results = newResults
}
return &newReport, nil
}
// OpenVexData returns a set of vex documents from the paths received
func (impl *defaultVexCtlImplementation) OpenVexData(opts Options, paths []string) ([]*vex.VEX, error) {
vexes := []*vex.VEX{}
for _, path := range paths {
var v *vex.VEX
var err error
switch opts.Format {
case "vex", "json", "":
v, err = vex.OpenJSON(path)
case "yaml":
v, err = vex.OpenYAML(path)
case "csaf":
v, err = vex.OpenCSAF(path, opts.Products)
}
if err != nil {
return nil, fmt.Errorf("opening document: %w", err)
}
vexes = append(vexes, v)
}
return vexes, nil
}
func (impl *defaultVexCtlImplementation) Sort(docs []*vex.VEX) []*vex.VEX {
return vex.Sort(docs)
}
func (impl *defaultVexCtlImplementation) AttestationBytes(att *attestation.Attestation) ([]byte, error) {
var b bytes.Buffer
if err := att.ToJSON(&b); err != nil {
return nil, fmt.Errorf("serializing attestation to json: %w", err)
}
return b.Bytes(), nil
}
func (impl *defaultVexCtlImplementation) Attach(ctx context.Context, att *attestation.Attestation, imageRef string) error {
env := ssldsse.Envelope{}
regOpts := options.RegistryOptions{}
remoteOpts, err := regOpts.ClientOpts(ctx)
if err != nil {
return fmt.Errorf("getting OCI remote options: %w", err)
}
var b bytes.Buffer
if err := att.ToJSON(&b); err != nil {
return fmt.Errorf("getting attestation JSON")
}
decoder := json.NewDecoder(&b)
for decoder.More() {
if err := decoder.Decode(&env); err != nil {
return err
}
payload, err := json.Marshal(env)
if err != nil {
return err
}
if env.PayloadType != IntotoPayloadType {
return fmt.Errorf("invalid payloadType %s on envelope. Expected %s", env.PayloadType, types.IntotoPayloadType)
}
ref, err := name.ParseReference(imageRef)
if err != nil {
return err
}
digest, err := ociremote.ResolveDigest(ref, remoteOpts...)
if err != nil {
return err
}
// Overwrite "ref" with a digest to avoid a race where we use a tag
// multiple times, and it potentially points to different things at
// each access.
ref = digest //nolint:ineffassign
opts := []static.Option{static.WithLayerMediaType(types.DssePayloadType)}
att, err := static.NewAttestation(payload, opts...)
if err != nil {
return err
}
se, err := ociremote.SignedEntity(digest, remoteOpts...)
if err != nil {
return err
}
newSE, err := mutate.AttachAttestationToEntity(se, att)
if err != nil {
return err
}
// Publish the signatures associated with this entity
err = ociremote.WriteAttestations(digest.Repository, newSE, remoteOpts...)
if err != nil {
return err
}
}
return nil
}
// SourceType returns a string indicating what kind of vex
// source a URI points to
func (impl *defaultVexCtlImplementation) SourceType(uri string) (string, error) {
if util.Exists(uri) {
return "file", nil
}
_, err := name.ParseReference(uri)
if err == nil {
return "image", nil
}
return "", errors.New("unable to resolve the vex source location")
}
// DownloadAttestation
func (impl *defaultVexCtlImplementation) ReadImageAttestations(
ctx context.Context, opts Options, refString string,
) (vexes []*vex.VEX, err error) {
// Parsae the image reference
ref, err := name.ParseReference(refString)
if err != nil {
return nil, fmt.Errorf("parsing image reference: %w", err)
}
regOpts := options.RegistryOptions{}
remoteOpts, err := regOpts.ClientOpts(ctx)
if err != nil {
return nil, fmt.Errorf("getting OCI remote options: %w", err)
}
payloads, err := cosign.FetchAttestationsForReference(ctx, ref, remoteOpts...)
if err != nil {
return nil, fmt.Errorf("fetching attached attestation: %w", err)
}
vexes = []*vex.VEX{}
for _, dssePayload := range payloads {
vexData, err := impl.ReadSignedVEX(dssePayload)
if err != nil {
return nil, fmt.Errorf("opening dsse payload: %w", err)
}
vexes = append(vexes, vexData)
}
return vexes, nil
}
// ReadSignedVEX returns the vex data inside a signed envelope
func (impl *defaultVexCtlImplementation) ReadSignedVEX(dssePayload cosign.AttestationPayload) (*vex.VEX, error) {
if dssePayload.PayloadType != IntotoPayloadType {
logrus.Info("Signed envelope does not contain an in-toto attestation")
return nil, nil
}
data, err := base64.StdEncoding.DecodeString(dssePayload.PayLoad)
if err != nil {
return nil, fmt.Errorf("decoding signed attestation: %w", err)
}
fmt.Printf("%s\n", string(data))
// Unmarshall the attestation
att := &attestation.Attestation{}
if err := json.Unmarshal(data, att); err != nil {
return nil, fmt.Errorf("unmarshalling attestation JSON: %w", err)
}
if att.PredicateType != vex.MimeType {
return nil, nil
}
return &att.Predicate, nil
}