Skip to content

Commit ff78510

Browse files
committed
vex: allow timeout to pull down VEX archive to be configurable
The timeout was hardcoded to 2m, this remains the default value but users have the option to configure it to a different value using updaters.config.rhel-vex.compressed_file_timeout. Signed-off-by: crozzy <joseph.crosland@gmail.com>
1 parent 98d3e3f commit ff78510

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

duration.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package claircore
2+
3+
import (
4+
"errors"
5+
"time"
6+
)
7+
8+
// Duration is a serializeable [time.Duration].
9+
type Duration time.Duration
10+
11+
// UnmarshalText implements [encoding.TextUnmarshaler].
12+
func (d *Duration) UnmarshalText(b []byte) error {
13+
dur, err := time.ParseDuration(string(b))
14+
if err != nil {
15+
return err
16+
}
17+
*d = Duration(dur)
18+
return nil
19+
}
20+
21+
// MarshalText implements [encoding.TextMarshaler].
22+
func (d *Duration) MarshalText() ([]byte, error) {
23+
if d == nil {
24+
return nil, errors.New("cannot marshal nil duration")
25+
}
26+
return []byte(time.Duration(*d).String()), nil
27+
}

rhel/vex/fetcher.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ import (
2727
)
2828

2929
var (
30-
compressedFileTimeout = 2 * time.Minute
31-
deletedTemplate = `{"document":{"tracking":{"id":"%s","status":"deleted"}}}`
32-
cvePathRegex = regexp.MustCompile(`^\d{4}/(cve-\d{4}-\d{4,}).json$`)
30+
deletedTemplate = `{"document":{"tracking":{"id":"%s","status":"deleted"}}}`
31+
cvePathRegex = regexp.MustCompile(`^\d{4}/(cve-\d{4}-\d{4,}).json$`)
3332
)
3433

3534
// Fetch pulls data down from the Red Hat VEX endpoints. The order of operations is:
@@ -107,7 +106,7 @@ func (u *Updater) Fetch(ctx context.Context, hint driver.Fingerprint) (io.ReadCl
107106
}
108107

109108
if processArchive {
110-
rctx, cancel := context.WithTimeout(ctx, compressedFileTimeout)
109+
rctx, cancel := context.WithTimeout(ctx, u.compressedFileTimeout)
111110
defer cancel()
112111

113112
if compressedURL == nil {

rhel/vex/updater.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/quay/zlog"
1313

14+
"github.com/quay/claircore"
1415
"github.com/quay/claircore/libvuln/driver"
1516
)
1617

@@ -28,28 +29,31 @@ const (
2829
//doc:url updater
2930
BaseURL = "https://security.access.redhat.com/data/csaf/v2/vex/"
3031

31-
latestFile = "archive_latest.txt"
32-
changesFile = "changes.csv"
33-
deletionsFile = "deletions.csv"
34-
lookBackToYear = 2014
35-
repoKey = "rhel-cpe-repository"
36-
updaterVersion = "4"
32+
defaultCompressedFileTimeout = 2 * time.Minute
33+
latestFile = "archive_latest.txt"
34+
changesFile = "changes.csv"
35+
deletionsFile = "deletions.csv"
36+
lookBackToYear = 2014
37+
repoKey = "rhel-cpe-repository"
38+
updaterVersion = "4"
3739
)
3840

3941
// Factory creates an Updater to process all of the Red Hat VEX data.
4042
//
4143
// [Configure] must be called before [UpdaterSet].
4244
type Factory struct {
43-
c *http.Client
44-
base *url.URL
45+
c *http.Client
46+
base *url.URL
47+
compressedFileTimeout time.Duration
4548
}
4649

4750
// UpdaterSet constructs one Updater
4851
func (f *Factory) UpdaterSet(_ context.Context) (driver.UpdaterSet, error) {
4952
us := driver.NewUpdaterSet()
5053
u := &Updater{
51-
url: f.base,
52-
client: f.c,
54+
url: f.base,
55+
client: f.c,
56+
compressedFileTimeout: f.compressedFileTimeout,
5357
}
5458
err := us.Add(u)
5559
if err != nil {
@@ -67,6 +71,8 @@ type FactoryConfig struct {
6771
//
6872
// Must include the trailing slash.
6973
URL string `json:"url" yaml:"url"`
74+
// CompressedFileTimeout sets the timeout for downloading the compressed VEX file.
75+
CompressedFileTimeout claircore.Duration `json:"compressed_file_timeout" yaml:"compressed_file_timeout"`
7076
}
7177

7278
// Configure implements driver.Configurable
@@ -88,14 +94,20 @@ func (f *Factory) Configure(ctx context.Context, cf driver.ConfigUnmarshaler, c
8894
if err != nil {
8995
return err
9096
}
97+
98+
f.compressedFileTimeout = defaultCompressedFileTimeout
99+
if cfg.CompressedFileTimeout != 0 {
100+
f.compressedFileTimeout = time.Duration(cfg.CompressedFileTimeout)
101+
}
91102
return nil
92103
}
93104

94105
// Updater is responsible from reading VEX data served at the URL
95106
// and creating vulnerabilities.
96107
type Updater struct {
97-
url *url.URL
98-
client *http.Client
108+
url *url.URL
109+
client *http.Client
110+
compressedFileTimeout time.Duration
99111
}
100112

101113
// fingerprint is used to track the state of the changes.csv and deletions.csv endpoints.

0 commit comments

Comments
 (0)