/
certifyGood.go
372 lines (340 loc) · 10.6 KB
/
certifyGood.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//
// Copyright 2023 The GUAC 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 keyvalue
import (
"context"
"errors"
"strings"
"time"
"github.com/vektah/gqlparser/v2/gqlerror"
"github.com/guacsec/guac/pkg/assembler/graphql/model"
"github.com/guacsec/guac/pkg/assembler/kv"
)
// Internal data: link that a package/source/artifact is good
type goodLink struct {
ThisID string
PackageID string
ArtifactID string
SourceID string
Justification string
Origin string
Collector string
KnownSince time.Time
}
func (n *goodLink) ID() string { return n.ThisID }
func (n *goodLink) Key() string {
return hashKey(strings.Join([]string{
n.PackageID,
n.ArtifactID,
n.SourceID,
n.Justification,
n.Origin,
n.Collector,
timeKey(n.KnownSince),
}, ":"))
}
func (n *goodLink) Neighbors(allowedEdges edgeMap) []string {
out := make([]string, 0, 1)
if n.PackageID != "" && allowedEdges[model.EdgeCertifyGoodPackage] {
out = append(out, n.PackageID)
}
if n.ArtifactID != "" && allowedEdges[model.EdgeCertifyGoodArtifact] {
out = append(out, n.ArtifactID)
}
if n.SourceID != "" && allowedEdges[model.EdgeCertifyGoodSource] {
out = append(out, n.SourceID)
}
return out
}
func (n *goodLink) BuildModelNode(ctx context.Context, c *demoClient) (model.Node, error) {
return c.buildCertifyGood(ctx, n, nil, true)
}
// Ingest CertifyGood
func (c *demoClient) IngestCertifyGoods(ctx context.Context, subjects model.PackageSourceOrArtifactInputs, pkgMatchType *model.MatchFlags, certifyGoods []*model.CertifyGoodInputSpec) ([]string, error) {
var modelCertifyGoods []string
for i := range certifyGoods {
var certifyGood string
var err error
if len(subjects.Packages) > 0 {
subject := model.PackageSourceOrArtifactInput{Package: subjects.Packages[i]}
certifyGood, err = c.IngestCertifyGood(ctx, subject, pkgMatchType, *certifyGoods[i])
if err != nil {
return nil, gqlerror.Errorf("IngestCertifyGood failed with err: %v", err)
}
} else if len(subjects.Sources) > 0 {
subject := model.PackageSourceOrArtifactInput{Source: subjects.Sources[i]}
certifyGood, err = c.IngestCertifyGood(ctx, subject, pkgMatchType, *certifyGoods[i])
if err != nil {
return nil, gqlerror.Errorf("IngestCertifyGood failed with err: %v", err)
}
} else {
subject := model.PackageSourceOrArtifactInput{Artifact: subjects.Artifacts[i]}
certifyGood, err = c.IngestCertifyGood(ctx, subject, pkgMatchType, *certifyGoods[i])
if err != nil {
return nil, gqlerror.Errorf("IngestCertifyGood failed with err: %v", err)
}
}
modelCertifyGoods = append(modelCertifyGoods, certifyGood)
}
return modelCertifyGoods, nil
}
func (c *demoClient) IngestCertifyGood(ctx context.Context, subject model.PackageSourceOrArtifactInput, pkgMatchType *model.MatchFlags, certifyGood model.CertifyGoodInputSpec) (string, error) {
return c.ingestCertifyGood(ctx, subject, pkgMatchType, certifyGood, true)
}
func (c *demoClient) ingestCertifyGood(ctx context.Context, subject model.PackageSourceOrArtifactInput, pkgMatchType *model.MatchFlags, certifyGood model.CertifyGoodInputSpec, readOnly bool) (string, error) {
funcName := "IngestCertifyGood"
in := &goodLink{
Justification: certifyGood.Justification,
Origin: certifyGood.Origin,
Collector: certifyGood.Collector,
KnownSince: certifyGood.KnownSince.UTC(),
}
lock(&c.m, readOnly)
defer unlock(&c.m, readOnly)
var foundPkgNameOrVersionNode pkgNameOrVersion
var foundArtStruct *artStruct
var foundSrcName *srcNameNode
if subject.Package != nil {
var err error
in.PackageID, foundPkgNameOrVersionNode, err = c.returnFoundPkgBasedOnMatchType(ctx, subject.Package, pkgMatchType)
if err != nil {
return "", gqlerror.Errorf("%v :: %s", funcName, err)
}
} else if subject.Artifact != nil {
var err error
foundArtStruct, err = c.returnFoundArtifact(ctx, subject.Artifact)
if err != nil {
return "", gqlerror.Errorf("%v :: %s", funcName, err)
}
in.ArtifactID = foundArtStruct.ID()
} else {
var err error
foundSrcName, err = c.returnFoundSource(ctx, subject.Source)
if err != nil {
return "", gqlerror.Errorf("%v :: %s", funcName, err)
}
in.SourceID = foundSrcName.ID()
}
out, err := byKeykv[*goodLink](ctx, cgCol, in.Key(), c)
if err == nil {
return out.ThisID, nil
}
if !errors.Is(err, kv.NotFoundError) {
return "", err
}
if readOnly {
c.m.RUnlock()
b, err := c.ingestCertifyGood(ctx, subject, pkgMatchType, certifyGood, false)
c.m.RLock() // relock so that defer unlock does not panic
return b, err
}
in.ThisID = c.getNextID()
if err := c.addToIndex(ctx, cgCol, in); err != nil {
return "", err
}
if foundPkgNameOrVersionNode != nil {
if err := foundPkgNameOrVersionNode.setCertifyGoodLinks(ctx, in.ThisID, c); err != nil {
return "", err
}
} else if foundArtStruct != nil {
if err := foundArtStruct.setCertifyGoodLinks(ctx, in.ThisID, c); err != nil {
return "", err
}
} else {
if err := foundSrcName.setCertifyGoodLinks(ctx, in.ThisID, c); err != nil {
return "", err
}
}
if err := setkv(ctx, cgCol, in, c); err != nil {
return "", err
}
return in.ThisID, nil
}
// Query CertifyGood
func (c *demoClient) CertifyGood(ctx context.Context, filter *model.CertifyGoodSpec) ([]*model.CertifyGood, error) {
funcName := "CertifyGood"
c.m.RLock()
defer c.m.RUnlock()
if filter != nil && filter.ID != nil {
link, err := byIDkv[*goodLink](ctx, *filter.ID, c)
if err != nil {
// Not found
return nil, nil
}
foundCertifyGood, err := c.buildCertifyGood(ctx, link, filter, true)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
return []*model.CertifyGood{foundCertifyGood}, nil
}
// Cant really search for an exact Pkg, as these can be linked to either
// names or versions, and version could be empty.
var search []string
foundOne := false
if filter != nil && filter.Subject != nil && filter.Subject.Artifact != nil {
exactArtifact, err := c.artifactExact(ctx, filter.Subject.Artifact)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
if exactArtifact != nil {
search = append(search, exactArtifact.GoodLinks...)
foundOne = true
}
}
if !foundOne && filter != nil && filter.Subject != nil && filter.Subject.Source != nil {
exactSource, err := c.exactSource(ctx, filter.Subject.Source)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
if exactSource != nil {
search = append(search, exactSource.GoodLinks...)
foundOne = true
}
}
var out []*model.CertifyGood
if foundOne {
for _, id := range search {
link, err := byIDkv[*goodLink](ctx, id, c)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
out, err = c.addCGIfMatch(ctx, out, filter, link)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
}
} else {
var done bool
scn := c.kv.Keys(cgCol)
for !done {
var cgKeys []string
var err error
cgKeys, done, err = scn.Scan(ctx)
if err != nil {
return nil, err
}
for _, cgk := range cgKeys {
link, err := byKeykv[*goodLink](ctx, cgCol, cgk, c)
if err != nil {
return nil, err
}
out, err = c.addCGIfMatch(ctx, out, filter, link)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
}
}
}
return out, nil
}
func (c *demoClient) addCGIfMatch(ctx context.Context, out []*model.CertifyGood,
filter *model.CertifyGoodSpec, link *goodLink) (
[]*model.CertifyGood, error) {
if filter != nil {
if noMatch(filter.Justification, link.Justification) ||
noMatch(filter.Collector, link.Collector) ||
noMatch(filter.Origin, link.Origin) ||
filter.KnownSince != nil && filter.KnownSince.After(link.KnownSince) {
return out, nil
}
}
foundCertifyGood, err := c.buildCertifyGood(ctx, link, filter, false)
if err != nil {
return nil, err
}
if foundCertifyGood == nil {
return out, nil
}
return append(out, foundCertifyGood), nil
}
func (c *demoClient) buildCertifyGood(ctx context.Context, link *goodLink, filter *model.CertifyGoodSpec, ingestOrIDProvided bool) (*model.CertifyGood, error) {
var p *model.Package
var a *model.Artifact
var s *model.Source
var err error
if filter != nil && filter.Subject != nil {
if filter.Subject.Package != nil && link.PackageID != "" {
p, err = c.buildPackageResponse(ctx, link.PackageID, filter.Subject.Package)
if err != nil {
return nil, err
}
}
if filter.Subject.Artifact != nil && link.ArtifactID != "" {
a, err = c.buildArtifactResponse(ctx, link.ArtifactID, filter.Subject.Artifact)
if err != nil {
return nil, err
}
}
if filter.Subject.Source != nil && link.SourceID != "" {
s, err = c.buildSourceResponse(ctx, link.SourceID, filter.Subject.Source)
if err != nil {
return nil, err
}
}
} else {
if link.PackageID != "" {
p, err = c.buildPackageResponse(ctx, link.PackageID, nil)
if err != nil {
return nil, err
}
}
if link.ArtifactID != "" {
a, err = c.buildArtifactResponse(ctx, link.ArtifactID, nil)
if err != nil {
return nil, err
}
}
if link.SourceID != "" {
s, err = c.buildSourceResponse(ctx, link.SourceID, nil)
if err != nil {
return nil, err
}
}
}
var subj model.PackageSourceOrArtifact
if link.PackageID != "" {
if p == nil && ingestOrIDProvided {
return nil, gqlerror.Errorf("failed to retrieve package via packageID")
} else if p == nil && !ingestOrIDProvided {
return nil, nil
}
subj = p
}
if link.ArtifactID != "" {
if a == nil && ingestOrIDProvided {
return nil, gqlerror.Errorf("failed to retrieve artifact via artifactID")
} else if a == nil && !ingestOrIDProvided {
return nil, nil
}
subj = a
}
if link.SourceID != "" {
if s == nil && ingestOrIDProvided {
return nil, gqlerror.Errorf("failed to retrieve source via sourceID")
} else if s == nil && !ingestOrIDProvided {
return nil, nil
}
subj = s
}
certifyGood := model.CertifyGood{
ID: link.ThisID,
Subject: subj,
Justification: link.Justification,
Origin: link.Origin,
Collector: link.Collector,
KnownSince: link.KnownSince.UTC(),
}
return &certifyGood, nil
}