/
querybuilder.go
131 lines (122 loc) · 3.37 KB
/
querybuilder.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
package postgres
import (
"fmt"
"strconv"
"strings"
"github.com/doug-martin/goqu/v8"
_ "github.com/doug-martin/goqu/v8/dialect/postgres"
"github.com/quay/claircore"
"github.com/quay/claircore/internal/vulnstore"
"github.com/quay/claircore/libvuln/driver"
)
// getQueryBuilder validates a IndexRecord and creates a query string for vulnerability matching
func buildGetQuery(record *claircore.IndexRecord, opts *vulnstore.GetOpts) (string, error) {
matchers := opts.Matchers
psql := goqu.Dialect("postgres")
exps := []goqu.Expression{}
// Add package name as first condition in query.
if record.Package.Name == "" {
return "", fmt.Errorf("IndexRecord must provide a Package.Name")
}
packageQuery := goqu.And(
goqu.Ex{"package_name": record.Package.Name},
goqu.Ex{"package_kind": record.Package.Kind},
)
exps = append(exps, packageQuery)
// If the package has a source, convert the first expression to an OR.
if record.Package.Source.Name != "" {
sourcePackageQuery := goqu.And(
goqu.Ex{"package_name": record.Package.Source.Name},
goqu.Ex{"package_kind": record.Package.Source.Kind},
)
or := goqu.Or(
packageQuery,
sourcePackageQuery,
)
exps[0] = or
}
// add matchers
seen := make(map[driver.MatchConstraint]struct{})
for _, m := range matchers {
if _, ok := seen[m]; ok {
continue
}
var ex goqu.Ex
switch m {
case driver.PackageModule:
ex = goqu.Ex{"package_module": record.Package.Module}
case driver.DistributionDID:
ex = goqu.Ex{"dist_id": record.Distribution.DID}
case driver.DistributionName:
ex = goqu.Ex{"dist_name": record.Distribution.Name}
case driver.DistributionVersionID:
ex = goqu.Ex{"dist_version_id": record.Distribution.VersionID}
case driver.DistributionVersion:
ex = goqu.Ex{"dist_version": record.Distribution.Version}
case driver.DistributionVersionCodeName:
ex = goqu.Ex{"dist_version_code_name": record.Distribution.VersionCodeName}
case driver.DistributionPrettyName:
ex = goqu.Ex{"dist_pretty_name": record.Distribution.PrettyName}
case driver.DistributionCPE:
ex = goqu.Ex{"dist_cpe": record.Distribution.CPE}
case driver.DistributionArch:
ex = goqu.Ex{"dist_arch": record.Distribution.Arch}
case driver.RepositoryName:
ex = goqu.Ex{"repo_name": record.Repository.Name}
default:
return "", fmt.Errorf("was provided unknown matcher: %v", m)
}
exps = append(exps, ex)
seen[m] = struct{}{}
}
if opts.VersionFiltering {
v := &record.Package.NormalizedVersion
var lit strings.Builder
b := make([]byte, 0, 16)
lit.WriteString("'{")
for i := 0; i < 10; i++ {
if i != 0 {
lit.WriteByte(',')
}
lit.Write(strconv.AppendInt(b, int64(v.V[i]), 10))
}
lit.WriteString("}'::int[]")
exps = append(exps, goqu.And(
goqu.C("version_kind").Eq(v.Kind),
goqu.L("vulnerable_range @> "+lit.String()),
))
}
query := psql.Select(
"id",
"name",
"description",
"issued",
"links",
"severity",
"normalized_severity",
"package_name",
"package_version",
"package_module",
"package_arch",
"package_kind",
"dist_id",
"dist_name",
"dist_version",
"dist_version_code_name",
"dist_version_id",
"dist_arch",
"dist_cpe",
"dist_pretty_name",
"arch_operation",
"repo_name",
"repo_key",
"repo_uri",
"fixed_in_version",
"updater",
).From("vuln").Where(exps...)
sql, _, err := query.ToSQL()
if err != nil {
return "", err
}
return sql, nil
}