-
Notifications
You must be signed in to change notification settings - Fork 79
/
scanner.go
306 lines (277 loc) · 8.08 KB
/
scanner.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
package rhcc
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/fs"
"net/http"
"os"
"strings"
"time"
"github.com/quay/zlog"
"github.com/quay/claircore"
"github.com/quay/claircore/indexer"
"github.com/quay/claircore/pkg/rhctag"
"github.com/quay/claircore/pkg/tarfs"
"github.com/quay/claircore/rhel/dockerfile"
"github.com/quay/claircore/rhel/internal/common"
)
var (
_ indexer.PackageScanner = (*scanner)(nil)
_ indexer.RPCScanner = (*scanner)(nil)
)
type scanner struct {
upd *common.Updater
client *http.Client
cfg ScannerConfig
}
// ScannerConfig is the configuration for the package scanner.
//
// The interaction between the "URL" and "File" members is the same as described
// in the [github.com/quay/claircore/rhel.RepositoryScannerConfig] documentation.
//
// By convention, it's in a "rhel_containerscanner" key.
type ScannerConfig struct {
// Name2ReposMappingURL is a URL where a mapping file can be fetched.
//
// See also [DefaultName2ReposMappingURL]
Name2ReposMappingURL string `json:"name2repos_mapping_url" yaml:"name2repos_mapping_url"`
// Name2ReposMappingFile is a path to a local mapping file.
Name2ReposMappingFile string `json:"name2repos_mapping_file" yaml:"name2repos_mapping_file"`
// Timeout is a timeout for all network calls made to update the mapping
// file.
//
// The default is 10 seconds.
Timeout time.Duration `json:"timeout" yaml:"timeout"`
}
// DefaultName2ReposMappingURL is the default URL with a mapping file provided by Red
// Hat.
const DefaultName2ReposMappingURL = "https://access.redhat.com/security/data/metrics/container-name-repos-map.json"
// Configure implements [indexer.RPCScanner].
func (s *scanner) Configure(ctx context.Context, f indexer.ConfigDeserializer, c *http.Client) error {
ctx = zlog.ContextWithValues(ctx, "component", "rhel/rhcc/scanner.Configure")
s.client = c
if err := f(&s.cfg); err != nil {
return err
}
if s.cfg.Timeout == 0 {
s.cfg.Timeout = 10 * time.Second
}
var mf *mappingFile
switch {
case s.cfg.Name2ReposMappingURL == "" && s.cfg.Name2ReposMappingFile == "":
// defaults
s.cfg.Name2ReposMappingURL = DefaultName2ReposMappingURL
case s.cfg.Name2ReposMappingURL != "" && s.cfg.Name2ReposMappingFile == "":
// remote only
case s.cfg.Name2ReposMappingFile != "":
// local only
f, err := os.Open(s.cfg.Name2ReposMappingFile)
if err != nil {
return err
}
defer f.Close()
mf = &mappingFile{}
if err := json.NewDecoder(f).Decode(mf); err != nil {
return err
}
}
s.upd = common.NewUpdater(s.cfg.Name2ReposMappingURL, mf)
tctx, done := context.WithTimeout(ctx, s.cfg.Timeout)
defer done()
s.upd.Get(tctx, c)
return nil
}
// Name implements [indexer.VersionedScanner].
func (s *scanner) Name() string { return "rhel_containerscanner" }
// Version implements [indexer.VersionedScanner].
func (s *scanner) Version() string { return "1" }
// Kind implements [indexer.VersionedScanner].
func (s *scanner) Kind() string { return "package" }
// Scan performs a package scan on the given layer and returns all
// the RHEL container identifying metadata
// Scan implements [indexer.PackageScanner].
func (s *scanner) Scan(ctx context.Context, l *claircore.Layer) ([]*claircore.Package, error) {
ctx = zlog.ContextWithValues(ctx, "component", "rhel/rhcc/scanner.Scan")
const (
compLabel = `com.redhat.component`
nameLabel = `name`
archLabel = `architecture`
)
if err := ctx.Err(); err != nil {
return nil, err
}
// add source package from component label
labels, p, err := findLabels(ctx, l)
switch {
case errors.Is(err, nil):
case errors.Is(err, errNotFound):
return nil, nil
default:
return nil, err
}
vr := getVR(p)
rhctagVersion, err := rhctag.Parse(vr)
if err != nil {
// This can happen for containers which don't use semantic versioning,
// such as UBI.
return nil, nil
}
var buildName, arch, name string
for _, chk := range []struct {
Found *string
Want string
}{
{&buildName, compLabel},
{&arch, archLabel},
{&name, nameLabel},
} {
var ok bool
(*chk.Found), ok = labels[chk.Want]
if !ok {
zlog.Info(ctx).Str("label", chk.Want).Msg("expected label not found in dockerfile")
return nil, nil
}
}
minorRange := rhctagVersion.MinorStart()
src := claircore.Package{
Kind: claircore.SOURCE,
Name: buildName,
Version: vr,
NormalizedVersion: minorRange.Version(true),
PackageDB: p,
Arch: arch,
RepositoryHint: `rhcc`,
}
pkgs := []*claircore.Package{&src}
tctx, done := context.WithTimeout(ctx, s.cfg.Timeout)
defer done()
vi, err := s.upd.Get(tctx, s.client)
if err != nil && vi == nil {
return nil, err
}
v, ok := vi.(*mappingFile)
if !ok || v == nil {
return nil, fmt.Errorf("rhcc: unable to create a mappingFile object")
}
repos, ok := v.Data[name]
if ok {
zlog.Debug(ctx).Str("name", name).
Msg("name present in mapping file")
} else {
// Didn't find external_repos in mapping, use name label as package
// name.
repos = []string{name}
}
for _, name := range repos {
// Add each external repo as a binary package. The same container image
// can ship to multiple repos eg. `"toolbox-container":
// ["rhel8/toolbox", "ubi8/toolbox"]`. Therefore, we want a binary
// package entry for each.
pkgs = append(pkgs, &claircore.Package{
Kind: claircore.BINARY,
Name: name,
Version: vr,
NormalizedVersion: minorRange.Version(true),
Source: &src,
PackageDB: p,
Arch: arch,
RepositoryHint: `rhcc`,
})
}
return pkgs, nil
}
// MappingFile is a struct for mapping file between container NAME label and
// container registry repository location.
type mappingFile struct {
Data map[string][]string `json:"data"`
}
func findLabels(ctx context.Context, layer *claircore.Layer) (map[string]string, string, error) {
r, err := layer.Reader()
if err != nil {
return nil, "", err
}
defer r.Close()
sys, err := tarfs.New(r)
if err != nil {
return nil, "", err
}
ms, err := fs.Glob(sys, "root/buildinfo/Dockerfile-*")
if err != nil { // Can only return ErrBadPattern.
panic("progammer error: " + err.Error())
}
if len(ms) == 0 {
return nil, "", errNotFound
}
zlog.Debug(ctx).
Strs("paths", ms).
Msg("found possible buildinfo Dockerfile(s)")
var p string
for _, m := range ms {
if strings.Count(m, "-") > 1 {
p = m
break
}
}
if p == "" {
return nil, "", errNotFound
}
zlog.Info(ctx).
Str("path", p).
Msg("found buildinfo Dockerfile")
f, err := sys.Open(p)
if err != nil {
return nil, "", err
}
defer f.Close()
labels, err := dockerfile.GetLabels(ctx, f)
if err != nil {
return nil, "", err
}
return labels, p, nil
}
var errNotFound = errors.New("not found")
// GetVR extracts the version-release string from the provided string ending in
// an NVR.
//
// Panics if passed malformed input.
func getVR(nvr string) string {
if strings.Count(nvr, "-") < 2 {
panic("programmer error: not an nvr string: " + nvr)
}
i := strings.LastIndexByte(nvr, '-')
i = strings.LastIndexByte(nvr[:i], '-')
return nvr[i+1:]
}
type reposcanner struct{}
var _ indexer.RepositoryScanner = (*reposcanner)(nil)
// Name implements [indexer.VersionedScanner].
func (s *reposcanner) Name() string { return "rhel_containerscanner" }
// Version implements [indexer.VersionedScanner].
func (s *reposcanner) Version() string { return "1" }
// Kind implements [indexer.VersionedScanner].
func (s *reposcanner) Kind() string { return "repository" }
// Scan implements [indexer.RepositoryScanner].
func (s *reposcanner) Scan(ctx context.Context, l *claircore.Layer) ([]*claircore.Repository, error) {
ctx = zlog.ContextWithValues(ctx, "component", "rhel/rhcc/reposcanner.Scan")
r, err := l.Reader()
if err != nil {
return nil, err
}
defer r.Close()
sys, err := tarfs.New(r)
if err != nil {
return nil, err
}
ms, err := fs.Glob(sys, "root/buildinfo/Dockerfile-*")
if err != nil { // Can only return ErrBadPattern.
panic("progammer error")
}
if len(ms) == 0 {
return nil, nil
}
zlog.Debug(ctx).
Msg("found buildinfo Dockerfile")
return []*claircore.Repository{&goldRepo}, nil
}