Skip to content

Commit bed64c0

Browse files
authored
cert-checker: remove use of SelectNullInt (#8734)
In its place, use SelectOne. This will allow us to remove SelectNullInt from `borp`, since we don't use it elsewhere in Boulder. Additional context: letsencrypt/borp#13
1 parent 231c89e commit bed64c0

2 files changed

Lines changed: 30 additions & 36 deletions

File tree

cmd/cert-checker/main.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"context"
66
"crypto/x509"
7-
"database/sql"
87
"encoding/json"
98
"flag"
109
"fmt"
@@ -90,7 +89,6 @@ type reportEntry struct {
9089
type certDB interface {
9190
Select(ctx context.Context, i any, query string, args ...any) ([]any, error)
9291
SelectOne(ctx context.Context, i any, query string, args ...any) error
93-
SelectNullInt(ctx context.Context, query string, args ...any) (sql.NullInt64, error)
9492
}
9593

9694
// A function that looks up a precertificate by serial and returns its DER bytes. Used for
@@ -147,8 +145,6 @@ func newChecker(saDbMap certDB,
147145
// findStartingID returns the lowest `id` in the certificates table within the
148146
// time window specified. The time window is a half-open interval [begin, end).
149147
func (c *certChecker) findStartingID(ctx context.Context, begin, end time.Time) (int64, error) {
150-
var output sql.NullInt64
151-
var err error
152148
var retries int
153149

154150
// Rather than querying `MIN(id)` across that whole window, we query it across the first
@@ -161,8 +157,10 @@ func (c *certChecker) findStartingID(ctx context.Context, begin, end time.Time)
161157
queryEnd := begin.Add(time.Hour)
162158

163159
for queryBegin.Compare(end) < 0 {
164-
output, err = c.dbMap.SelectNullInt(
160+
var output *int64
161+
err := c.dbMap.SelectOne(
165162
ctx,
163+
&output,
166164
`SELECT MIN(id) FROM certificates
167165
WHERE issued >= :begin AND
168166
issued < :end`,
@@ -185,7 +183,7 @@ func (c *certChecker) findStartingID(ctx context.Context, begin, end time.Time)
185183
// MIN() returns NULL if there were no matching rows
186184
// https://pkg.go.dev/database/sql#NullInt64
187185
// Valid is true if Int64 is not NULL
188-
if !output.Valid {
186+
if output == nil {
189187
// No matching rows, try the next hour
190188
queryBegin = queryBegin.Add(time.Hour)
191189
queryEnd = queryEnd.Add(time.Hour)
@@ -195,7 +193,7 @@ func (c *certChecker) findStartingID(ctx context.Context, begin, end time.Time)
195193
continue
196194
}
197195

198-
return output.Int64, nil
196+
return *output, nil
199197
}
200198

201199
// Fell through the loop without finding a valid ID

cmd/cert-checker/main_test.go

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ import (
99
"crypto/rsa"
1010
"crypto/x509"
1111
"crypto/x509/pkix"
12-
"database/sql"
1312
"encoding/asn1"
1413
"encoding/pem"
15-
"errors"
1614
"log"
1715
"math/big"
1816
mrand "math/rand/v2"
@@ -389,25 +387,20 @@ func TestGetAndProcessCerts(t *testing.T) {
389387
// asked for the actual rows.
390388
type mismatchedCountDB struct{}
391389

392-
// `getCerts` calls `SelectInt` first to determine how many rows there are
393-
// matching the `getCertsCountQuery` criteria. For this mock we return
394-
// a non-zero number
395-
func (db mismatchedCountDB) SelectNullInt(_ context.Context, _ string, _ ...any) (sql.NullInt64, error) {
396-
return sql.NullInt64{
397-
Int64: 99999,
398-
Valid: true,
399-
},
400-
nil
401-
}
402-
403390
// `getCerts` then calls `Select` to retrieve the Certificate rows. We pull
404391
// a dastardly switch-a-roo here and return an empty set
405392
func (db mismatchedCountDB) Select(_ context.Context, output any, _ string, _ ...any) ([]any, error) {
406393
return nil, nil
407394
}
408395

409-
func (db mismatchedCountDB) SelectOne(_ context.Context, _ any, _ string, _ ...any) error {
410-
return errors.New("unimplemented")
396+
// `getCerts` calls `SelectOne` first to determine how many rows there are
397+
// matching the `getCertsCountQuery` criteria. For this mock we return
398+
// a non-zero number
399+
func (db mismatchedCountDB) SelectOne(_ context.Context, holder any, _ string, _ ...any) error {
400+
h := holder.(**int64)
401+
var nines int64 = 99999
402+
*h = &nines
403+
return nil
411404
}
412405

413406
/*
@@ -445,11 +438,12 @@ type emptyDB struct {
445438
certDB
446439
}
447440

448-
// SelectNullInt is a method that returns a false sql.NullInt64 struct to
449-
// mock a null DB response
450-
func (db emptyDB) SelectNullInt(_ context.Context, _ string, _ ...any) (sql.NullInt64, error) {
451-
return sql.NullInt64{Valid: false},
452-
nil
441+
// SelectOne is a method that stores `nil` in the passed int64 holder.
442+
// It's used to mock a null DB response (i.e. MIN across now rows).
443+
func (db emptyDB) SelectOne(_ context.Context, holder any, _ string, _ ...any) error {
444+
h := holder.(**int64)
445+
*h = nil
446+
return nil
453447
}
454448

455449
// TestGetCertsNullResults tests that a null response from the database will
@@ -473,16 +467,22 @@ type lateDB struct {
473467
selectedACert bool
474468
}
475469

476-
// SelectNullInt is a method that returns a false sql.NullInt64 struct to
477-
// mock a null DB response
478-
func (db *lateDB) SelectNullInt(_ context.Context, _ string, args ...any) (sql.NullInt64, error) {
470+
// SelectOne is a method that stores `nil` in the passed int64 holder.
471+
// It's used to mock a null DB response (i.e. MIN across now rows).
472+
func (db lateDB) SelectOne(_ context.Context, holder any, _ string, args ...any) error {
473+
h := holder.(**int64)
474+
479475
args2 := args[0].(map[string]any)
480476
begin := args2["begin"].(time.Time)
481477
end := args2["end"].(time.Time)
482478
if begin.Compare(db.issuedTime) < 0 && end.Compare(db.issuedTime) > 0 {
483-
return sql.NullInt64{Int64: 23, Valid: true}, nil
479+
var twentythree int64 = 23
480+
*h = &twentythree
481+
return nil
484482
}
485-
return sql.NullInt64{Valid: false}, nil
483+
484+
*h = nil
485+
return nil
486486
}
487487

488488
func (db *lateDB) Select(_ context.Context, output any, _ string, args ...any) ([]any, error) {
@@ -492,10 +492,6 @@ func (db *lateDB) Select(_ context.Context, output any, _ string, args ...any) (
492492
return nil, nil
493493
}
494494

495-
func (db *lateDB) SelectOne(_ context.Context, _ any, _ string, _ ...any) error {
496-
return nil
497-
}
498-
499495
// TestGetCertsLate checks for correct behavior when certificates exist only late in the provided window.
500496
func TestGetCertsLate(t *testing.T) {
501497
clk := clock.NewFake()

0 commit comments

Comments
 (0)