Skip to content

Commit 05e6315

Browse files
ratelimits: Supporting additions for admin tooling (#8279)
- Export `ValidateLimit()` for use in the admin tool. - Add utility functions `DumpOverrides()` and `LoadOverridesByBucketKey()` to dump/load overrides to/from a YAML file. - Export `Limit` and several of its fields to support calls to `LoadOverridesByBucketKey()` and `ValidateLimit()`, and to return results from `DumpOverrides()`. - Add `BuildBucketKey()`, which builds and validates bucket keys based on the limit name and provided components. - Also add a `MarshalYAML()` method to `config.Duration`. Part of #8165
1 parent c1ce0c8 commit 05e6315

File tree

14 files changed

+927
-237
lines changed

14 files changed

+927
-237
lines changed

config/duration.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ func (d *Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
6767
d.Duration = dur
6868
return nil
6969
}
70+
71+
// MarshalYAML returns the string form of the duration, as a string.
72+
func (d Duration) MarshalYAML() (any, error) {
73+
return d.Duration.String(), nil
74+
}

identifier/identifier.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,24 @@ func NewIP(ip netip.Addr) ACMEIdentifier {
122122
}
123123
}
124124

125+
// FromString converts a string to an ACMEIdentifier.
126+
func FromString(identStr string) ACMEIdentifier {
127+
ip, err := netip.ParseAddr(identStr)
128+
if err == nil {
129+
return NewIP(ip)
130+
}
131+
return NewDNS(identStr)
132+
}
133+
134+
// FromStringSlice converts a slice of strings to a slice of ACMEIdentifier.
135+
func FromStringSlice(identStrs []string) ACMEIdentifiers {
136+
var idents ACMEIdentifiers
137+
for _, identStr := range identStrs {
138+
idents = append(idents, FromString(identStr))
139+
}
140+
return idents
141+
}
142+
125143
// fromX509 extracts the Subject Alternative Names from a certificate or CSR's fields, and
126144
// returns a slice of ACMEIdentifiers.
127145
func fromX509(commonName string, dnsNames []string, ipAddresses []net.IP) ACMEIdentifiers {

ratelimits/gcra.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
// returns a Decision struct with the result of the decision and the updated
1111
// TAT. The cost must be 0 or greater and <= the burst capacity of the limit.
1212
func maybeSpend(clk clock.Clock, txn Transaction, tat time.Time) *Decision {
13-
if txn.cost < 0 || txn.cost > txn.limit.burst {
13+
if txn.cost < 0 || txn.cost > txn.limit.Burst {
1414
// The condition above is the union of the conditions checked in Check
1515
// and Spend methods of Limiter. If this panic is reached, it means that
1616
// the caller has introduced a bug.
@@ -67,7 +67,7 @@ func maybeSpend(clk clock.Clock, txn Transaction, tat time.Time) *Decision {
6767
// or greater. A cost will only be refunded up to the burst capacity of the
6868
// limit. A partial refund is still considered successful.
6969
func maybeRefund(clk clock.Clock, txn Transaction, tat time.Time) *Decision {
70-
if txn.cost < 0 || txn.cost > txn.limit.burst {
70+
if txn.cost < 0 || txn.cost > txn.limit.Burst {
7171
// The condition above is checked in the Refund method of Limiter. If
7272
// this panic is reached, it means that the caller has introduced a bug.
7373
panic("invalid cost for maybeRefund")
@@ -80,7 +80,7 @@ func maybeRefund(clk clock.Clock, txn Transaction, tat time.Time) *Decision {
8080
// The TAT is in the past, therefore the bucket is full.
8181
return &Decision{
8282
allowed: false,
83-
remaining: txn.limit.burst,
83+
remaining: txn.limit.Burst,
8484
retryIn: time.Duration(0),
8585
resetIn: time.Duration(0),
8686
newTAT: tat,

ratelimits/gcra_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
func TestDecide(t *testing.T) {
1414
clk := clock.NewFake()
15-
limit := &limit{burst: 10, count: 1, period: config.Duration{Duration: time.Second}}
15+
limit := &Limit{Burst: 10, Count: 1, Period: config.Duration{Duration: time.Second}}
1616
limit.precompute()
1717

1818
// Begin by using 1 of our 10 requests.
@@ -139,7 +139,7 @@ func TestDecide(t *testing.T) {
139139

140140
func TestMaybeRefund(t *testing.T) {
141141
clk := clock.NewFake()
142-
limit := &limit{burst: 10, count: 1, period: config.Duration{Duration: time.Second}}
142+
limit := &Limit{Burst: 10, Count: 1, Period: config.Duration{Duration: time.Second}}
143143
limit.precompute()
144144

145145
// Begin by using 1 of our 10 requests.

0 commit comments

Comments
 (0)