-
Notifications
You must be signed in to change notification settings - Fork 79
/
fetcher.go
155 lines (141 loc) · 3.84 KB
/
fetcher.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
package ovalutil
import (
"compress/bzip2"
"compress/gzip"
"context"
"fmt"
"io"
"net/http"
"net/url"
"github.com/rs/zerolog"
"github.com/quay/claircore/libvuln/driver"
"github.com/quay/claircore/pkg/tmp"
)
// Compressor is used by Fetcher to decompress data it fetches.
type Compressor uint
//go:generate stringer -type Compressor -linecomment
// These are the kinds of Compession a Fetcher can deal with.
const (
CompressionNone Compressor = iota // none
CompressionGzip // gzip
CompressionBzip2 // bzip2
)
// ParseCompressor reports the Compressor indicated by the passed in string.
func ParseCompressor(s string) (c Compressor, err error) {
switch s {
case "gz", "gzip":
c = CompressionGzip
case "bz2", "bzip2":
c = CompressionBzip2
case "", "none":
c = CompressionNone
default:
return c, fmt.Errorf("ovalutil: unknown compression scheme %q", s)
}
return c, nil
}
// Fetcher implements the driver.FetcherNG interface.
//
// Fetcher expects all of its exported members to be filled out appropriately,
// and may panic if not.
type Fetcher struct {
Compression Compressor
URL *url.URL
Client *http.Client
}
// Fetch fetches the resource as specified by Fetcher.URL and
// Fetcher.Compression, using the client provided as Fetcher.Client.
//
// Fetch makes GET requests, and will make conditional requests using the
// passed-in hint as an HTTP date. The returned hint will be an HTTP date if the
// server sends a Last-Modified header.
//
// Tmp.File is used to return a ReadCloser that outlives the passed-in context.
func (f *Fetcher) Fetch(ctx context.Context, hint driver.Fingerprint) (io.ReadCloser, driver.Fingerprint, error) {
log := zerolog.Ctx(ctx).With().
Str("component", "pkg/ovalutil/Fetcher.Fetch").
Logger()
ctx = log.WithContext(ctx)
log.Info().Str("database", f.URL.String()).Msg("starting fetch")
req := http.Request{
Method: http.MethodGet,
Header: http.Header{
"User-Agent": {"claircore/pkg/ovalutil.Fetcher"},
},
URL: f.URL,
Proto: "HTTP/1.1",
ProtoMajor: 1,
ProtoMinor: 1,
Host: f.URL.Host,
}
if hint != "" {
log.Debug().
Str("hint", string(hint)).
Msg("using hint")
req.Header.Set("If-Modified-Since", string(hint))
}
res, err := f.Client.Do(req.WithContext(ctx))
if res != nil {
defer res.Body.Close()
}
if err != nil {
return nil, hint, err
}
switch res.StatusCode {
case http.StatusNotModified:
return nil, hint, driver.Unchanged
case http.StatusOK:
// break
default:
return nil, hint, fmt.Errorf("ovalutil: fetcher got unexpected HTTP response: %d (%s)", res.StatusCode, res.Status)
}
log.Debug().Msg("request ok")
var r io.Reader
switch f.Compression {
case CompressionNone:
r = res.Body
case CompressionGzip:
r, err = gzip.NewReader(res.Body)
if err != nil {
return nil, hint, err
}
case CompressionBzip2:
r = bzip2.NewReader(res.Body)
default:
panic(fmt.Sprintf("ovalutil: programmer error: unknown compression scheme: %v", f.Compression))
}
log.Debug().
Str("compression", f.Compression.String()).
Msg("found compression scheme")
tf, err := tmp.NewFile("", "fetcher.")
if err != nil {
return nil, hint, err
}
log.Debug().
Str("path", tf.Name()).
Msg("using tempfile")
success := false
defer func() {
if !success {
log.Debug().Msg("unsuccessful, cleaing up tempfile")
if err := tf.Close(); err != nil {
log.Warn().Err(err).Msg("failed to close tempfile")
}
}
}()
if _, err := io.Copy(tf, r); err != nil {
return nil, hint, err
}
if o, err := tf.Seek(0, io.SeekStart); err != nil || o != 0 {
return nil, hint, err
}
log.Debug().Msg("decompressed and buffered database")
if t := res.Header.Get("Last-Modified"); t != "" {
hint = driver.Fingerprint(t)
}
log.Debug().
Str("hint", string(hint)).
Msg("using new hint")
success = true
return tf, hint, nil
}