/
parser.go
54 lines (48 loc) · 1.57 KB
/
parser.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
package photon
import (
"context"
"encoding/xml"
"fmt"
"io"
"github.com/quay/goval-parser/oval"
"github.com/quay/zlog"
"github.com/quay/claircore"
"github.com/quay/claircore/internal/xmlutil"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/claircore/pkg/ovalutil"
)
var _ driver.Parser = (*Updater)(nil)
func (u *Updater) Parse(ctx context.Context, r io.ReadCloser) ([]*claircore.Vulnerability, error) {
ctx = zlog.ContextWithValues(ctx, "component", "photon/Updater.Parse")
zlog.Info(ctx).Msg("starting parse")
defer r.Close()
root := oval.Root{}
dec := xml.NewDecoder(r)
dec.CharsetReader = xmlutil.CharsetReader
if err := dec.Decode(&root); err != nil {
return nil, fmt.Errorf("photon: unable to decode OVAL document: %w", err)
}
zlog.Debug(ctx).Msg("xml decoded")
protoVulns := func(def oval.Definition) ([]*claircore.Vulnerability, error) {
return []*claircore.Vulnerability{
&claircore.Vulnerability{
Updater: u.Name(),
Name: def.Title,
Description: def.Description,
Issued: def.Advisory.Issued.Date,
Links: ovalutil.Links(def),
Severity: def.Advisory.Severity,
NormalizedSeverity: NormalizeSeverity(def.Advisory.Severity),
// each updater is configured to parse a photon release
// specific xml database. we'll use the updater's release
// to map the parsed vulnerabilities
Dist: releaseToDist(u.release),
},
}, nil
}
vulns, err := ovalutil.RPMDefsToVulns(ctx, &root, protoVulns)
if err != nil {
return nil, err
}
return vulns, nil
}