Skip to content

Commit

Permalink
Read from durationpb instead of int64 durations (#7146)
Browse files Browse the repository at this point in the history
Switch to reading grpc duration values from the new durationpb protofbuf
fields, completely ignoring the old int64 fields.

Part 2 of 3 for #7097
  • Loading branch information
pgporada authored Nov 13, 2023
1 parent dc2ef15 commit 279a4d5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
5 changes: 5 additions & 0 deletions core/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"
"unicode"

"google.golang.org/protobuf/types/known/durationpb"
"gopkg.in/go-jose/go-jose.v2"
)

Expand Down Expand Up @@ -216,6 +217,10 @@ func IsAnyNilOrZero(vals ...interface{}) bool {
if len(v) == 0 {
return true
}
case *durationpb.Duration:
if v == nil || v.AsDuration() == time.Duration(0) {
return true
}
default:
if reflect.ValueOf(v).IsZero() {
return true
Expand Down
7 changes: 7 additions & 0 deletions core/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"testing"
"time"

"google.golang.org/protobuf/types/known/durationpb"
"gopkg.in/go-jose/go-jose.v2"

"github.com/letsencrypt/boulder/test"
Expand Down Expand Up @@ -135,6 +136,12 @@ func TestIsAnyNilOrZero(t *testing.T) {

test.Assert(t, IsAnyNilOrZero(1, ""), "Mixed values seen as non-zero")
test.Assert(t, IsAnyNilOrZero("", 1), "Mixed values seen as non-zero")

var d *durationpb.Duration
var zeroDuration time.Duration
test.Assert(t, IsAnyNilOrZero(d), "Pointer to uninitialized durationpb.Duration seen as non-zero")
test.Assert(t, IsAnyNilOrZero(durationpb.New(zeroDuration)), "*durationpb.Duration containing an zero value time.Duration is seen as non-zero")
test.Assert(t, !IsAnyNilOrZero(durationpb.New(666)), "A *durationpb.Duration with valid inner duration is seen as zero")
}

func TestUniqueLowerNames(t *testing.T) {
Expand Down
23 changes: 21 additions & 2 deletions sa/sa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,17 @@ func TestFQDNSets(t *testing.T) {
test.AssertNotError(t, err, "Failed to add name set")
test.AssertNotError(t, tx.Commit(), "Failed to commit transaction")

threeHours := time.Hour * 3
// Invalid Window
req := &sapb.CountFQDNSetsRequest{
Domains: names,
WindowNS: 0,
Window: nil,
}
_, err = sa.CountFQDNSets(ctx, req)
test.AssertErrorIs(t, err, errIncompleteRequest)

threeHours := time.Hour * 3
req = &sapb.CountFQDNSetsRequest{
Domains: names,
WindowNS: threeHours.Nanoseconds(),
Window: durationpb.New(threeHours),
Expand Down Expand Up @@ -977,8 +986,18 @@ func TestFQDNSetTimestampsForWindow(t *testing.T) {
test.AssertNotError(t, err, "Failed to open transaction")

names := []string{"a.example.com", "B.example.com"}
window := time.Hour * 3

// Invalid Window
req := &sapb.CountFQDNSetsRequest{
Domains: names,
WindowNS: 0,
Window: nil,
}
_, err = sa.FQDNSetTimestampsForWindow(ctx, req)
test.AssertErrorIs(t, err, errIncompleteRequest)

window := time.Hour * 3
req = &sapb.CountFQDNSetsRequest{
Domains: names,
WindowNS: window.Nanoseconds(),
Window: durationpb.New(window),
Expand Down
8 changes: 4 additions & 4 deletions sa/saro.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func (ssa *SQLStorageAuthority) CountOrders(ctx context.Context, req *sapb.Count
// CountFQDNSets counts the total number of issuances, for a set of domains,
// that occurred during a given window of time.
func (ssa *SQLStorageAuthorityRO) CountFQDNSets(ctx context.Context, req *sapb.CountFQDNSetsRequest) (*sapb.Count, error) {
if req.WindowNS == 0 || len(req.Domains) == 0 {
if core.IsAnyNilOrZero(req.Window) || len(req.Domains) == 0 {
return nil, errIncompleteRequest
}

Expand All @@ -519,7 +519,7 @@ func (ssa *SQLStorageAuthorityRO) CountFQDNSets(ctx context.Context, req *sapb.C
WHERE setHash = ?
AND issued > ?`,
core.HashNames(req.Domains),
ssa.clk.Now().Add(-time.Duration(req.WindowNS)),
ssa.clk.Now().Add(-req.Window.AsDuration()),
)
return &sapb.Count{Count: count}, err
}
Expand All @@ -532,7 +532,7 @@ func (ssa *SQLStorageAuthority) CountFQDNSets(ctx context.Context, req *sapb.Cou
// certificate, issued for a set of domains, during a given window of time,
// starting from the most recent issuance.
func (ssa *SQLStorageAuthorityRO) FQDNSetTimestampsForWindow(ctx context.Context, req *sapb.CountFQDNSetsRequest) (*sapb.Timestamps, error) {
if req.WindowNS == 0 || len(req.Domains) == 0 {
if core.IsAnyNilOrZero(req.Window) || len(req.Domains) == 0 {
return nil, errIncompleteRequest
}
type row struct {
Expand All @@ -547,7 +547,7 @@ func (ssa *SQLStorageAuthorityRO) FQDNSetTimestampsForWindow(ctx context.Context
AND issued > ?
ORDER BY issued DESC`,
core.HashNames(req.Domains),
ssa.clk.Now().Add(-time.Duration(req.WindowNS)),
ssa.clk.Now().Add(-req.Window.AsDuration()),
)
if err != nil {
return nil, err
Expand Down

0 comments on commit 279a4d5

Please sign in to comment.