Skip to content

Commit ef11dd9

Browse files
hdurand0710Gopher Bot
authored andcommitted
BUG/MEDIUM: fix error on rate-limit-requests
With this commit, we allow to enable or not rate limit based on the present of rate-limit-request annotation. If rate-limit-request is set, then other annotations related to rate-limit have default values: - "rate-limit-size": "100k", - "rate-limit-period": "1s", - "rate-limit-status-code": "403", but can be set as annotation too. Commit 'MINOR: rate-limit: add rate-limit-whitelist annotation to exclude IPs' did reveal that we were missing a default value for rate-limit-requests. This was generating error logs during each cycle.
1 parent 7c9711f commit ef11dd9

3 files changed

Lines changed: 73 additions & 24 deletions

File tree

pkg/annotations/common/main.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,30 +52,27 @@ func GetK8sPath(annotationName string, annotations ...map[string]string) (ns, na
5252
}
5353

5454
var DefaultValues = map[string]string{
55-
"auth-realm": "Protected Content",
56-
"check": "true",
57-
"cors-allow-origin": "*",
58-
"cors-allow-methods": "*",
59-
"cors-allow-headers": "*",
60-
"cors-max-age": "5s",
61-
"cookie-indirect": "true",
62-
"cookie-nocache": "true",
63-
"cookie-type": "insert",
64-
"forwarded-for": "true",
65-
"load-balance": "roundrobin",
66-
"rate-limit-size": "100k",
67-
"rate-limit-period": "1s",
68-
"rate-limit-status-code": "403",
69-
"request-capture-len": "128",
70-
"ssl-redirect-code": "302",
71-
"request-redirect-code": "302",
72-
"ssl-redirect-port": "8443",
73-
"ssl-passthrough": "false",
74-
"server-ssl": "false",
75-
"scale-server-slots": "42",
76-
"client-crt-optional": "false",
77-
"tls-alpn": "h2,http/1.1",
78-
"quic-alt-svc-max-age": "60",
55+
"auth-realm": "Protected Content",
56+
"check": "true",
57+
"cors-allow-origin": "*",
58+
"cors-allow-methods": "*",
59+
"cors-allow-headers": "*",
60+
"cors-max-age": "5s",
61+
"cookie-indirect": "true",
62+
"cookie-nocache": "true",
63+
"cookie-type": "insert",
64+
"forwarded-for": "true",
65+
"load-balance": "roundrobin",
66+
"request-capture-len": "128",
67+
"ssl-redirect-code": "302",
68+
"request-redirect-code": "302",
69+
"ssl-redirect-port": "8443",
70+
"ssl-passthrough": "false",
71+
"server-ssl": "false",
72+
"scale-server-slots": "42",
73+
"client-crt-optional": "false",
74+
"tls-alpn": "h2,http/1.1",
75+
"quic-alt-svc-max-age": "60",
7976
}
8077

8178
// GetValuesAndIndices retrieves values of a specific annotation from multiple annotations maps.

pkg/haproxy/rules/reqRatelimit.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ type ReqRateLimit struct {
2020
WhitelistMaps []maps.Path // Pattern file references
2121
}
2222

23+
const (
24+
defaultRateLimitStatueCode = "403"
25+
)
26+
2327
func (r ReqRateLimit) GetType() Type {
2428
return REQ_RATELIMIT
2529
}
@@ -28,8 +32,18 @@ func (r ReqRateLimit) Create(client api.HAProxyClient, frontend *models.Frontend
2832
if frontend.Mode == "tcp" {
2933
return errors.New("request Track cannot be configured in TCP mode")
3034
}
35+
36+
// ReqsLimit == 0 means rate-limit disabled
37+
if r.ReqsLimit == 0 {
38+
return nil
39+
}
3140
condTest := fmt.Sprintf("{ sc0_http_req_rate(%s) gt %d }", r.TableName, r.ReqsLimit)
3241

42+
err := r.applyDefaults()
43+
if err != nil {
44+
return err
45+
}
46+
3347
// Build whitelist conditions if configured
3448
// If whitelist is set, only apply rate limiting if source IP is NOT in the whitelist
3549
if len(r.WhitelistIPs) > 0 || len(r.WhitelistMaps) > 0 {
@@ -58,3 +72,14 @@ func (r ReqRateLimit) Create(client api.HAProxyClient, frontend *models.Frontend
5872
}
5973
return client.FrontendHTTPRequestRuleCreate(0, frontend.Name, httpRule, ingressACL)
6074
}
75+
76+
func (r *ReqRateLimit) applyDefaults() error {
77+
if r.DenyStatusCode == 0 {
78+
code, err := utils.ParseInt(defaultRateLimitStatueCode)
79+
if err != nil {
80+
return err
81+
}
82+
r.DenyStatusCode = code
83+
}
84+
return nil
85+
}

pkg/haproxy/rules/reqTrack.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ type ReqTrack struct {
1818
TrackKey string
1919
}
2020

21+
const (
22+
defaultPeriod = "1s"
23+
defaultTableSize = "100k"
24+
)
25+
2126
func (r ReqTrack) GetType() Type {
2227
return REQ_TRACK
2328
}
@@ -26,6 +31,10 @@ func (r ReqTrack) Create(client api.HAProxyClient, frontend *models.Frontend, in
2631
if frontend.Mode == "tcp" {
2732
return errors.New("request Track cannot be configured in TCP mode")
2833
}
34+
err := r.applyDefaults()
35+
if err != nil {
36+
return err
37+
}
2938

3039
// Create tracking table.
3140
if !client.BackendUsed(r.TableName) {
@@ -54,3 +63,21 @@ func (r ReqTrack) Create(client api.HAProxyClient, frontend *models.Frontend, in
5463
}
5564
return client.FrontendHTTPRequestRuleCreate(0, frontend.Name, httpRule, ingressACL)
5665
}
66+
67+
func (r *ReqTrack) applyDefaults() error {
68+
if r.TablePeriod == nil {
69+
period, err := utils.ParseTime(defaultPeriod)
70+
if err != nil {
71+
return err
72+
}
73+
r.TablePeriod = utils.PtrInt64(*period)
74+
}
75+
if r.TableSize == nil {
76+
size, err := utils.ParseSize(defaultTableSize)
77+
if err != nil {
78+
return err
79+
}
80+
r.TableSize = size
81+
}
82+
return nil
83+
}

0 commit comments

Comments
 (0)