Skip to content

Commit 13a33ce

Browse files
committed
Review feedback
1 parent 395edc1 commit 13a33ce

2 files changed

Lines changed: 42 additions & 34 deletions

File tree

observer/probers/ccadb/ccadb.go

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,6 @@ func (c CCADBConf) MakeProber(collectors map[string]prometheus.Collector) (probe
7979
}
8080
}
8181

82-
crlsSuccess, ok := collectors["ccadb_crls_success"]
83-
if !ok {
84-
return nil, fmt.Errorf("CCADB prober did not receive metric \"ccadb_crls_success\"")
85-
}
86-
8782
crlRegexp := `^http://[a-z0-9-]+\.c\.lencr\.org/\d+\.crl$`
8883
if c.CRLRegexp != "" {
8984
crlRegexp = c.CRLRegexp
@@ -100,8 +95,6 @@ func (c CCADBConf) MakeProber(collectors map[string]prometheus.Collector) (probe
10095
caOwner: caOwner,
10196
crlAgeLimit: ageLimitDuration,
10297
crlRegexp: re,
103-
104-
crlsSuccess: crlsSuccess,
10598
}, nil
10699
}
107100

@@ -110,14 +103,7 @@ func (c CCADBConf) MakeProber(collectors map[string]prometheus.Collector) (probe
110103
// objects, indexed by the name of the Prometheus metric. If no objects were
111104
// constructed, nil is returned.
112105
func (c CCADBConf) Instrument() map[string]prometheus.Collector {
113-
crlsSuccess := prometheus.Collector(prometheus.NewCounter(
114-
prometheus.CounterOpts{
115-
Name: "ccadb_crls_success",
116-
Help: "Fetching and linting all CRLs succeeded.",
117-
}))
118-
return map[string]prometheus.Collector{
119-
"ccadb_crls_success": crlsSuccess,
120-
}
106+
return nil
121107
}
122108

123109
func getIDP(crl *x509.RevocationList) (string, error) {
@@ -147,8 +133,6 @@ type CCADBProber struct {
147133
caOwner string
148134
crlAgeLimit time.Duration
149135
crlRegexp *regexp.Regexp
150-
151-
crlsSuccess prometheus.Collector
152136
}
153137

154138
func (c CCADBProber) Kind() string {
@@ -253,7 +237,7 @@ func getCSV(ctx context.Context, url string) ([]string, *csv.Reader, error) {
253237
reader := csv.NewReader(bytes.NewReader(body))
254238
header, err := reader.Read()
255239
if err != nil {
256-
return nil, nil, err
240+
return nil, nil, fmt.Errorf("%q: %w", url, err)
257241
}
258242

259243
return header, reader, nil
@@ -282,6 +266,9 @@ func (c CCADBProber) getDecadeIntermediates(ctx context.Context, decade int) (ma
282266
}
283267

284268
pemIndex := slices.Index(header, "X.509 Certificate (PEM)")
269+
if pemIndex == -1 {
270+
return nil, fmt.Errorf("no column named \"X.509 Certificate (PEM)\" in %s", url)
271+
}
285272

286273
ret := make(map[string]*x509.Certificate)
287274
for {
@@ -290,7 +277,7 @@ func (c CCADBProber) getDecadeIntermediates(ctx context.Context, decade int) (ma
290277
break
291278
}
292279
if err != nil {
293-
return nil, err
280+
return nil, fmt.Errorf("%q: %w", url, err)
294281
}
295282

296283
if len(record) < pemIndex {
@@ -322,23 +309,35 @@ func (c CCADBProber) getCRLURLs(ctx context.Context, issuers map[string]*x509.Ce
322309
return nil, err
323310
}
324311

325-
ownerIndex := slices.Index(header, "CA Owner")
326-
crlIndex := slices.Index(header, "JSON Array of Partitioned CRLs")
327-
skidIndex := slices.Index(header, "Subject Key Identifier")
328-
certificateNameIndex := slices.Index(header, "Certificate Name")
312+
const (
313+
owner = "CA Owner"
314+
crl = "JSON Array of Partitioned CRLs"
315+
skid = "Subject Key Identifier"
316+
certificateName = "Certificate Name"
317+
)
318+
319+
columns := map[string]int{}
320+
for _, headerName := range []string{owner, crl, skid, certificateName} {
321+
index := slices.Index(header, headerName)
322+
if index == -1 {
323+
return nil, fmt.Errorf("no column named %q in %s", headerName, c.allCertificatesCSVURL)
324+
}
325+
columns[headerName] = index
326+
}
327+
329328
allCRLs := make(map[string][]string)
330329
for {
331330
record, err := reader.Read()
332331
if err == io.EOF {
333332
break
334333
}
335334
if err != nil {
336-
return nil, err
335+
return nil, fmt.Errorf("%q: %w", c.allCertificatesCSVURL, err)
337336
}
338-
if record[ownerIndex] != c.caOwner {
337+
if record[columns[owner]] != c.caOwner {
339338
continue
340339
}
341-
crlJSON := record[crlIndex]
340+
crlJSON := record[columns[crl]]
342341
if crlJSON == "" {
343342
continue
344343
}
@@ -347,8 +346,8 @@ func (c CCADBProber) getCRLURLs(ctx context.Context, issuers map[string]*x509.Ce
347346
if err != nil {
348347
return nil, err
349348
}
350-
certificateName := record[certificateNameIndex]
351-
skidBase64 := record[skidIndex]
349+
certificateName := record[columns[certificateName]]
350+
skidBase64 := record[columns[skid]]
352351
skid, err := base64.StdEncoding.DecodeString(skidBase64)
353352
if err != nil {
354353
return nil, err

observer/probers/ccadb/retryhttp.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,39 @@ func getBody(ctx context.Context, url string) ([]byte, error) {
2121
}
2222
defer resp.Body.Close()
2323

24-
body, err := io.ReadAll(resp.Body)
24+
body, err := io.ReadAll(&io.LimitedReader{R: resp.Body, N: 100_000_000})
2525
if err != nil {
2626
return nil, err
2727
}
2828

2929
if resp.StatusCode != http.StatusOK {
30-
return nil, fmt.Errorf("http status %d for %q: %s", resp.StatusCode, url, string(body[:400]))
30+
// Truncate the response body in case it's too big to be useful in logs.
31+
if len(body) > 400 {
32+
body = body[:400]
33+
}
34+
return nil, fmt.Errorf("http status %d for %q: %s", resp.StatusCode, url, string(body))
3135
}
3236

3337
return body, nil
3438
}
3539

3640
// httpGet is a simple wrapper around http.Client.Do that will retry on a fixed backoff schedule
3741
func httpGet(ctx context.Context, url string) ([]byte, error) {
38-
// A fixed exponential backoff schedule. The final value is zero so that we don't sleep before
39-
// returning the final error.
42+
// A fixed exponential backoff schedule.
4043
var err error
41-
for _, backoff := range []int{1000, 1250, 1562, 1953, 2441, 3051, 3814, 4768, 5960, 7450, 9313, 11641, 0} {
44+
for _, backoff := range []int{0, 1000, 1250, 1562, 1953, 2441, 3051, 3814, 4768, 5960, 7450, 9313, 11641} {
45+
select {
46+
case <-ctx.Done():
47+
return nil, ctx.Err()
48+
default:
49+
}
50+
// This isn't a `case <-time.After`, so we give priority to `<-ctx.Done()` even on the first iteration.
51+
time.Sleep(time.Duration(backoff) * time.Millisecond)
4252
var body []byte
4353
body, err = getBody(ctx, url)
4454
if err == nil {
4555
return body, nil
4656
}
47-
time.Sleep(time.Duration(backoff) * time.Millisecond)
4857
}
4958
return nil, err
5059
}

0 commit comments

Comments
 (0)