/
process.go
291 lines (252 loc) · 9.02 KB
/
process.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
//
// Copyright 2022 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 process
import (
"bytes"
"compress/bzip2"
"context"
"encoding/xml"
"fmt"
"io"
"path/filepath"
"strings"
"go.uber.org/zap"
uuid "github.com/gofrs/uuid"
"github.com/guacsec/guac/pkg/blob"
"github.com/guacsec/guac/pkg/emitter"
"github.com/guacsec/guac/pkg/events"
"github.com/guacsec/guac/pkg/handler/collector"
"github.com/guacsec/guac/pkg/handler/processor"
"github.com/guacsec/guac/pkg/handler/processor/csaf"
"github.com/guacsec/guac/pkg/handler/processor/cyclonedx"
"github.com/guacsec/guac/pkg/handler/processor/deps_dev"
"github.com/guacsec/guac/pkg/handler/processor/dsse"
"github.com/guacsec/guac/pkg/handler/processor/guesser"
"github.com/guacsec/guac/pkg/handler/processor/ite6"
"github.com/guacsec/guac/pkg/handler/processor/open_vex"
"github.com/guacsec/guac/pkg/handler/processor/scorecard"
"github.com/guacsec/guac/pkg/handler/processor/spdx"
"github.com/guacsec/guac/pkg/logging"
jsoniter "github.com/json-iterator/go"
"github.com/klauspost/compress/zstd"
"gocloud.dev/pubsub"
)
var (
documentProcessors = map[processor.DocumentType]processor.DocumentProcessor{}
json = jsoniter.ConfigCompatibleWithStandardLibrary
)
func init() {
_ = RegisterDocumentProcessor(&ite6.ITE6Processor{}, processor.DocumentITE6Generic)
_ = RegisterDocumentProcessor(&ite6.ITE6Processor{}, processor.DocumentITE6SLSA)
_ = RegisterDocumentProcessor(&ite6.ITE6Processor{}, processor.DocumentITE6Vul)
_ = RegisterDocumentProcessor(&dsse.DSSEProcessor{}, processor.DocumentDSSE)
_ = RegisterDocumentProcessor(&spdx.SPDXProcessor{}, processor.DocumentSPDX)
_ = RegisterDocumentProcessor(&csaf.CSAFProcessor{}, processor.DocumentCsaf)
_ = RegisterDocumentProcessor(&open_vex.OpenVEXProcessor{}, processor.DocumentOpenVEX)
_ = RegisterDocumentProcessor(&scorecard.ScorecardProcessor{}, processor.DocumentScorecard)
_ = RegisterDocumentProcessor(&cyclonedx.CycloneDXProcessor{}, processor.DocumentCycloneDX)
_ = RegisterDocumentProcessor(&deps_dev.DepsDev{}, processor.DocumentDepsDev)
}
func RegisterDocumentProcessor(p processor.DocumentProcessor, d processor.DocumentType) error {
if _, ok := documentProcessors[d]; ok {
documentProcessors[d] = p
return fmt.Errorf("the document processor is being overwritten: %s", d)
}
documentProcessors[d] = p
return nil
}
// Subscribe receives the CD event and decodes the event to obtain the blob store key.
// The key is used to retrieve the "document" from the blob store to be processed and ingested.
func Subscribe(ctx context.Context, em collector.Emitter, blobStore *blob.BlobStore, emPubSub *emitter.EmitterPubSub) error {
logger := logging.FromContext(ctx)
uuid, err := uuid.NewV4()
if err != nil {
return fmt.Errorf("failed to get uuid with the following error: %w", err)
}
uuidString := uuid.String()
sub, err := emPubSub.Subscribe(ctx)
if err != nil {
return fmt.Errorf("[processor: %s] failed to create new pubsub: %w", uuidString, err)
}
// should still continue if there are errors since problem is with individual documents
processFunc := func(d *pubsub.Message) error {
blobStoreKey, err := events.DecodeEventSubject(ctx, d.Body)
if err != nil {
logger.Errorf("[processor: %s] failed decode event: %v", uuidString, err)
return nil
}
// start up the child logger with the hash of the document
// this initializes a new logger instead of re-assigning to logger so that we don't add multiple values for a single key in the logs
childLogger := logger.With(zap.String(logging.DocumentHash, blobStoreKey))
childLogger.Debugf("[processor: %s] starting child logger", uuidString)
documentBytes, err := blobStore.Read(ctx, blobStoreKey)
if err != nil {
childLogger.Errorf("[processor: %s] failed read document to blob store: %v", uuidString, err)
return nil
}
doc := processor.Document{}
if err = json.Unmarshal(documentBytes, &doc); err != nil {
childLogger.Errorf("[processor: %s] failed unmarshal the document bytes: %v", uuidString, err)
return nil
}
doc.ChildLogger = childLogger
if err := em(&doc); err != nil {
childLogger.Errorf("[processor: %s] failed transportFunc: %v", uuidString, err)
childLogger.Errorf("[processor: %s] message id: %s not acknowledged in pusbub", uuidString, d.LoggableID)
return nil
}
// ack the message from the queue once the ingestion has occurred via the Emitter (em) function specified above
d.Ack()
childLogger.Infof("[processor: %s] message acknowledged in pusbub", uuidString)
return nil
}
err = sub.GetDataFromSubscriber(ctx, processFunc, uuidString)
if err != nil {
return fmt.Errorf("[processor: %s] failed to get data from %s: %w", uuidString, emPubSub.ServiceURL, err)
}
if err := sub.CloseSubscriber(ctx); err != nil {
return fmt.Errorf("[processor: %s] failed to close subscriber: %s, with error: %w", uuidString, emPubSub.ServiceURL, err)
}
return nil
}
// Process processes the documents received from the collector to determine
// their format and document type.
func Process(ctx context.Context, i *processor.Document) (processor.DocumentTree, error) {
node, err := processHelper(ctx, i)
if err != nil {
return nil, err
}
return processor.DocumentTree(node), nil
}
func processHelper(ctx context.Context, doc *processor.Document) (*processor.DocumentNode, error) {
ds, err := processDocument(ctx, doc)
if err != nil {
return nil, err
}
children := make([]*processor.DocumentNode, len(ds))
for i, d := range ds {
d.SourceInformation = doc.SourceInformation
n, err := processHelper(ctx, d)
if err != nil {
return nil, err
}
children[i] = n
}
return &processor.DocumentNode{
Document: doc,
Children: children,
}, nil
}
func processDocument(ctx context.Context, i *processor.Document) ([]*processor.Document, error) {
if err := decodeDocument(ctx, i); err != nil {
return nil, err
}
if err := preProcessDocument(ctx, i); err != nil {
return nil, err
}
if err := validateFormat(i); err != nil {
return nil, err
}
err := validateDocument(i)
if err != nil {
return nil, err
}
ds, err := unpackDocument(i)
if err != nil {
return nil, fmt.Errorf("unable to unpack document: %w", err)
}
return ds, nil
}
func preProcessDocument(ctx context.Context, i *processor.Document) error {
docType, format, err := guesser.GuessDocument(ctx, i)
if err != nil {
return fmt.Errorf("unable to guess document type: %w", err)
}
i.Type = docType
i.Format = format
return nil
}
func validateFormat(i *processor.Document) error {
switch i.Format {
case processor.FormatJSON:
if !json.Valid(i.Blob) {
return fmt.Errorf("invalid JSON document")
}
case processor.FormatXML:
if err := xml.Unmarshal(i.Blob, &struct{}{}); err != nil {
return fmt.Errorf("invalid XML document")
}
case processor.FormatUnknown:
return nil
default:
return fmt.Errorf("invalid document format type: %v", i.Format)
}
return nil
}
func validateDocument(i *processor.Document) error {
p, ok := documentProcessors[i.Type]
if !ok {
return fmt.Errorf("no document processor registered for type: %s", i.Type)
}
return p.ValidateSchema(i) // nolint:wrapcheck
}
func unpackDocument(i *processor.Document) ([]*processor.Document, error) {
p, ok := documentProcessors[i.Type]
if !ok {
return nil, fmt.Errorf("no document processor registered for type: %s", i.Type)
}
return p.Unpack(i) // nolint:wrapcheck
}
func decodeDocument(ctx context.Context, i *processor.Document) error {
logger := logging.FromContext(ctx)
var reader io.Reader
var err error
if i.Encoding == "" {
ext := filepath.Ext(i.SourceInformation.Source)
encoding, ok := processor.EncodingExts[strings.ToLower(ext)]
if ok {
i.Encoding = encoding
} else {
err := guesser.GuessEncoding(ctx, i)
if err != nil {
return fmt.Errorf("failure while attempting to detect file encoding: %w", err)
}
}
}
logger.Infof("Decoding document with encoding: %v", i.Encoding)
switch i.Encoding {
case processor.EncodingBzip2:
reader = bzip2.NewReader(bytes.NewReader(i.Blob))
case processor.EncodingZstd:
reader, err = zstd.NewReader(bytes.NewReader(i.Blob))
if err != nil {
return fmt.Errorf("unable to create zstd reader: %w", err)
}
}
if reader != nil {
if err := decompressDocument(i, reader); err != nil {
return fmt.Errorf("unable to decode document: %w", err)
}
}
return nil
}
func decompressDocument(i *processor.Document, reader io.Reader) error {
uncompressed, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("unable to decompress document: %w", err)
}
i.Blob = uncompressed
return nil
}