/
hasSourceAt.go
289 lines (260 loc) · 8.29 KB
/
hasSourceAt.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
//
// 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 between sources and packages (HasSourceAt)
type srcMapLink struct {
ThisID string
SourceID string
PackageID string
KnownSince time.Time
Justification string
Origin string
Collector string
}
func (n *srcMapLink) ID() string { return n.ThisID }
func (n *srcMapLink) Key() string {
return hashKey(strings.Join([]string{
n.SourceID,
n.PackageID,
timeKey(n.KnownSince),
n.Justification,
n.Origin,
n.Collector,
}, ":"))
}
func (n *srcMapLink) Neighbors(allowedEdges edgeMap) []string {
out := make([]string, 0, 2)
if allowedEdges[model.EdgeHasSourceAtPackage] {
out = append(out, n.PackageID)
}
if allowedEdges[model.EdgeHasSourceAtSource] {
out = append(out, n.SourceID)
}
return out
}
func (n *srcMapLink) BuildModelNode(ctx context.Context, c *demoClient) (model.Node, error) {
return c.buildHasSourceAt(ctx, n, nil, true)
}
// Ingest HasSourceAt
func (c *demoClient) IngestHasSourceAts(ctx context.Context, pkgs []*model.IDorPkgInput, pkgMatchType *model.MatchFlags, sources []*model.IDorSourceInput, hasSourceAts []*model.HasSourceAtInputSpec) ([]string, error) {
var modelHasMetadataIDs []string
for i := range hasSourceAts {
hasMetadata, err := c.IngestHasSourceAt(ctx, *pkgs[i], *pkgMatchType, *sources[i], *hasSourceAts[i])
if err != nil {
return nil, gqlerror.Errorf("IngestHasSourceAt failed with err: %v", err)
}
modelHasMetadataIDs = append(modelHasMetadataIDs, hasMetadata)
}
return modelHasMetadataIDs, nil
}
func (c *demoClient) IngestHasSourceAt(ctx context.Context, packageArg model.IDorPkgInput, pkgMatchType model.MatchFlags, source model.IDorSourceInput, hasSourceAt model.HasSourceAtInputSpec) (string, error) {
return c.ingestHasSourceAt(ctx, packageArg, pkgMatchType, source, hasSourceAt, true)
}
func (c *demoClient) ingestHasSourceAt(ctx context.Context, packageArg model.IDorPkgInput, pkgMatchType model.MatchFlags, source model.IDorSourceInput, hasSourceAt model.HasSourceAtInputSpec, readOnly bool) (string, error) {
funcName := "IngestHasSourceAt"
in := &srcMapLink{
KnownSince: hasSourceAt.KnownSince.UTC(),
Justification: hasSourceAt.Justification,
Origin: hasSourceAt.Origin,
Collector: hasSourceAt.Collector,
}
lock(&c.m, readOnly)
defer unlock(&c.m, readOnly)
var pkgNameOrVersionNode pkgNameOrVersion
var err error
in.PackageID, pkgNameOrVersionNode, err = c.returnFoundPkgBasedOnMatchType(ctx, &packageArg, &pkgMatchType)
if err != nil {
return "", gqlerror.Errorf("%v :: %s", funcName, err)
}
srcName, err := c.returnFoundSource(ctx, &source)
if err != nil {
return "", gqlerror.Errorf("%v :: %s", funcName, err)
}
in.SourceID = srcName.ThisID
out, err := byKeykv[*srcMapLink](ctx, hsaCol, in.Key(), c)
if err == nil {
return out.ThisID, nil
}
if !errors.Is(err, kv.NotFoundError) {
return "", err
}
if readOnly {
c.m.RUnlock()
s, err := c.ingestHasSourceAt(ctx, packageArg, pkgMatchType, source, hasSourceAt, false)
c.m.RLock() // relock so that defer unlock does not panic
return s, err
}
in.ThisID = c.getNextID()
if err := c.addToIndex(ctx, hsaCol, in); err != nil {
return "", err
}
// set the backlinks
if err := pkgNameOrVersionNode.setSrcMapLinks(ctx, in.ThisID, c); err != nil {
return "", err
}
if err := srcName.setSrcMapLinks(ctx, in.ThisID, c); err != nil {
return "", err
}
if err := setkv(ctx, hsaCol, in, c); err != nil {
return "", err
}
return in.ThisID, nil
}
// Query HasSourceAt
func (c *demoClient) HasSourceAt(ctx context.Context, filter *model.HasSourceAtSpec) ([]*model.HasSourceAt, error) {
c.m.RLock()
defer c.m.RUnlock()
funcName := "HasSourceAt"
if filter != nil && filter.ID != nil {
link, err := byIDkv[*srcMapLink](ctx, *filter.ID, c)
if err != nil {
// Not found
return nil, nil
}
foundHasSourceAt, err := c.buildHasSourceAt(ctx, link, filter, true)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
return []*model.HasSourceAt{foundHasSourceAt}, nil
}
// Cant really search for an exact Pkg, as these can be linked to either
// names or versions, only search Source backedges.
var search []string
foundOne := false
if filter != nil && filter.Source != nil {
exactSource, err := c.exactSource(ctx, filter.Source)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
if exactSource != nil {
search = append(search, exactSource.SrcMapLinks...)
foundOne = true
}
}
var out []*model.HasSourceAt
if foundOne {
for _, id := range search {
link, err := byIDkv[*srcMapLink](ctx, id, c)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
out, err = c.addSrcIfMatch(ctx, out, filter, link)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
}
} else {
var done bool
scn := c.kv.Keys(hsaCol)
for !done {
var hsaKeys []string
var err error
hsaKeys, done, err = scn.Scan(ctx)
if err != nil {
return nil, err
}
for _, hsak := range hsaKeys {
link, err := byKeykv[*srcMapLink](ctx, hsaCol, hsak, c)
if err != nil {
return nil, err
}
out, err = c.addSrcIfMatch(ctx, out, filter, link)
if err != nil {
return nil, gqlerror.Errorf("%v :: %v", funcName, err)
}
}
}
}
return out, nil
}
func (c *demoClient) buildHasSourceAt(ctx context.Context, link *srcMapLink, filter *model.HasSourceAtSpec, ingestOrIDProvided bool) (*model.HasSourceAt, error) {
var p *model.Package
var s *model.Source
var err error
if filter != nil {
p, err = c.buildPackageResponse(ctx, link.PackageID, filter.Package)
if err != nil {
return nil, err
}
s, err = c.buildSourceResponse(ctx, link.SourceID, filter.Source)
if err != nil {
return nil, err
}
} else {
p, err = c.buildPackageResponse(ctx, link.PackageID, nil)
if err != nil {
return nil, err
}
s, err = c.buildSourceResponse(ctx, link.SourceID, nil)
if err != nil {
return nil, err
}
}
// if package not found during ingestion or if ID is provided in filter, send error. On query do not send error to continue search
if p == nil && ingestOrIDProvided {
return nil, gqlerror.Errorf("failed to retrieve package via packageID")
} else if p == nil && !ingestOrIDProvided {
return nil, nil
}
// if source not found during ingestion or if ID is provided in filter, send error. On query do not send error to continue search
if s == nil && ingestOrIDProvided {
return nil, gqlerror.Errorf("failed to retrieve source via sourceID")
} else if s == nil && !ingestOrIDProvided {
return nil, nil
}
newHSA := model.HasSourceAt{
ID: link.ThisID,
Package: p,
Source: s,
KnownSince: link.KnownSince,
Justification: link.Justification,
Origin: link.Origin,
Collector: link.Collector,
}
return &newHSA, nil
}
func (c *demoClient) addSrcIfMatch(ctx context.Context, out []*model.HasSourceAt,
filter *model.HasSourceAtSpec, link *srcMapLink) (
[]*model.HasSourceAt, error) {
if filter != nil && noMatch(filter.Justification, link.Justification) {
return out, nil
}
if filter != nil && noMatch(filter.Origin, link.Origin) {
return out, nil
}
if filter != nil && noMatch(filter.Collector, link.Collector) {
return out, nil
}
if filter != nil && filter.KnownSince != nil && !filter.KnownSince.Equal(link.KnownSince) {
return out, nil
}
foundHasSourceAt, err := c.buildHasSourceAt(ctx, link, filter, false)
if err != nil {
return nil, err
}
if foundHasSourceAt == nil {
return out, nil
}
return append(out, foundHasSourceAt), nil
}