Skip to content

Commit fd78542

Browse files
committed
RA tests: use inmem rate limit source
1 parent bac5602 commit fd78542

File tree

6 files changed

+16
-47
lines changed

6 files changed

+16
-47
lines changed

ra/ra_test.go

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"fmt"
1717
"math/big"
1818
mrand "math/rand/v2"
19-
"os"
2019
"regexp"
2120
"strconv"
2221
"strings"
@@ -40,7 +39,6 @@ import (
4039

4140
akamaipb "github.com/letsencrypt/boulder/akamai/proto"
4241
capb "github.com/letsencrypt/boulder/ca/proto"
43-
"github.com/letsencrypt/boulder/cmd"
4442
"github.com/letsencrypt/boulder/config"
4543
"github.com/letsencrypt/boulder/core"
4644
corepb "github.com/letsencrypt/boulder/core/proto"
@@ -60,7 +58,6 @@ import (
6058
rapb "github.com/letsencrypt/boulder/ra/proto"
6159
"github.com/letsencrypt/boulder/ratelimit"
6260
"github.com/letsencrypt/boulder/ratelimits"
63-
bredis "github.com/letsencrypt/boulder/redis"
6461
"github.com/letsencrypt/boulder/sa"
6562
sapb "github.com/letsencrypt/boulder/sa/proto"
6663
"github.com/letsencrypt/boulder/test"
@@ -283,7 +280,7 @@ func newAcctKey(t *testing.T) []byte {
283280
return acctKey
284281
}
285282

286-
func initAuthorities(t *testing.T) (*DummyValidationAuthority, sapb.StorageAuthorityClient, *RegistrationAuthorityImpl, *ratelimits.RedisSource, clock.FakeClock, func()) {
283+
func initAuthorities(t *testing.T) (*DummyValidationAuthority, sapb.StorageAuthorityClient, *RegistrationAuthorityImpl, ratelimits.Source, clock.FakeClock, func()) {
287284
err := json.Unmarshal(AccountKeyJSONA, &AccountKeyA)
288285
test.AssertNotError(t, err, "Failed to unmarshal public JWK")
289286
err = json.Unmarshal(AccountKeyJSONB, &AccountKeyB)
@@ -352,39 +349,11 @@ func initAuthorities(t *testing.T) (*DummyValidationAuthority, sapb.StorageAutho
352349
},
353350
}, nil, nil, 0, log, metrics.NoopRegisterer)
354351

355-
var source *ratelimits.RedisSource
356-
var limiter *ratelimits.Limiter
357-
var txnBuilder *ratelimits.TransactionBuilder
358-
if strings.Contains(os.Getenv("BOULDER_CONFIG_DIR"), "test/config-next") {
359-
rc := bredis.Config{
360-
Username: "unittest-rw",
361-
TLS: cmd.TLSConfig{
362-
CACertFile: "../test/certs/ipki/minica.pem",
363-
CertFile: "../test/certs/ipki/localhost/cert.pem",
364-
KeyFile: "../test/certs/ipki/localhost/key.pem",
365-
},
366-
Lookups: []cmd.ServiceDomain{
367-
{
368-
Service: "redisratelimits",
369-
Domain: "service.consul",
370-
},
371-
},
372-
LookupDNSAuthority: "consul.service.consul",
373-
}
374-
rc.PasswordConfig = cmd.PasswordConfig{
375-
PasswordFile: "../test/secrets/ratelimits_redis_password",
376-
}
377-
ring, err := bredis.NewRingFromConfig(rc, stats, log)
378-
test.AssertNotError(t, err, "making redis ring client")
379-
source = ratelimits.NewRedisSource(ring.Ring, fc, stats)
380-
test.AssertNotNil(t, source, "source should not be nil")
381-
err = source.Ping(context.Background())
382-
test.AssertNotError(t, err, "Ping should not error")
383-
limiter, err = ratelimits.NewLimiter(fc, source, stats)
384-
test.AssertNotError(t, err, "making limiter")
385-
txnBuilder, err = ratelimits.NewTransactionBuilder("../test/config-next/wfe2-ratelimit-defaults.yml", "")
386-
test.AssertNotError(t, err, "making transaction composer")
387-
}
352+
rlSource := ratelimits.NewInmemSource()
353+
limiter, err := ratelimits.NewLimiter(fc, rlSource, stats)
354+
test.AssertNotError(t, err, "making limiter")
355+
txnBuilder, err := ratelimits.NewTransactionBuilder("../test/config-next/wfe2-ratelimit-defaults.yml", "")
356+
test.AssertNotError(t, err, "making transaction composer")
388357

389358
testKeyPolicy, err := goodkey.NewPolicy(nil, nil)
390359
test.AssertNotError(t, err, "making keypolicy")
@@ -401,7 +370,7 @@ func initAuthorities(t *testing.T) (*DummyValidationAuthority, sapb.StorageAutho
401370
ra.CA = ca
402371
ra.OCSP = &mocks.MockOCSPGenerator{}
403372
ra.PA = pa
404-
return va, sa, ra, source, fc, cleanUp
373+
return va, sa, ra, rlSource, fc, cleanUp
405374
}
406375

407376
func TestValidateContacts(t *testing.T) {

ratelimits/limiter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var allowedDecision = &Decision{allowed: true, remaining: math.MaxInt64}
3434
// utilizing a leaky bucket-style approach.
3535
type Limiter struct {
3636
// source is used to store buckets. It must be safe for concurrent use.
37-
source source
37+
source Source
3838
clk clock.Clock
3939

4040
spendLatency *prometheus.HistogramVec
@@ -43,7 +43,7 @@ type Limiter struct {
4343

4444
// NewLimiter returns a new *Limiter. The provided source must be safe for
4545
// concurrent use.
46-
func NewLimiter(clk clock.Clock, source source, stats prometheus.Registerer) (*Limiter, error) {
46+
func NewLimiter(clk clock.Clock, source Source, stats prometheus.Registerer) (*Limiter, error) {
4747
spendLatency := prometheus.NewHistogramVec(prometheus.HistogramOpts{
4848
Name: "ratelimits_spend_latency",
4949
Help: fmt.Sprintf("Latency of ratelimit checks labeled by limit=[name] and decision=[%s|%s], in seconds", Allowed, Denied),

ratelimits/limiter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
const tenZeroZeroTwo = "10.0.0.2"
2222

2323
// newTestLimiter constructs a new limiter.
24-
func newTestLimiter(t *testing.T, s source, clk clock.FakeClock) *Limiter {
24+
func newTestLimiter(t *testing.T, s Source, clk clock.FakeClock) *Limiter {
2525
l, err := NewLimiter(clk, s, metrics.NoopRegisterer)
2626
test.AssertNotError(t, err, "should not error")
2727
return l

ratelimits/source.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
// ErrBucketNotFound indicates that the bucket was not found.
1111
var ErrBucketNotFound = fmt.Errorf("bucket not found")
1212

13-
// source is an interface for creating and modifying TATs.
14-
type source interface {
13+
// Source is an interface for creating and modifying TATs.
14+
type Source interface {
1515
// BatchSet stores the TATs at the specified bucketKeys (formatted as
1616
// 'name:id'). Implementations MUST ensure non-blocking operations by
1717
// either:
@@ -64,9 +64,9 @@ type inmem struct {
6464
m map[string]time.Time
6565
}
6666

67-
var _ source = (*inmem)(nil)
67+
var _ Source = (*inmem)(nil)
6868

69-
func newInmem() *inmem {
69+
func NewInmemSource() *inmem {
7070
return &inmem{m: make(map[string]time.Time)}
7171
}
7272

ratelimits/source_redis.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// Compile-time check that RedisSource implements the source interface.
15-
var _ source = (*RedisSource)(nil)
15+
var _ Source = (*RedisSource)(nil)
1616

1717
// RedisSource is a ratelimits source backed by sharded Redis.
1818
type RedisSource struct {

ratelimits/source_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ import (
77
)
88

99
func newInmemTestLimiter(t *testing.T, clk clock.FakeClock) *Limiter {
10-
return newTestLimiter(t, newInmem(), clk)
10+
return newTestLimiter(t, NewInmemSource(), clk)
1111
}

0 commit comments

Comments
 (0)