Skip to content

Commit 564a826

Browse files
added some annotation validation
1 parent bf20392 commit 564a826

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

internal/configs/annotations.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,41 @@ func parseAnnotations(ingEx *IngressEx, baseCfgParams *ConfigParams, isPlus bool
454454
//gocyclo:ignore
455455
func parseRateLimitAnnotations(annotations map[string]string, cfgParams *ConfigParams, context apiObject) []error {
456456
errors := make([]error, 0)
457+
458+
rateLimitAnnotations := []string{
459+
"nginx.org/limit-req-rate",
460+
"nginx.org/limit-req-key",
461+
"nginx.org/limit-req-zone-size",
462+
"nginx.org/limit-req-burst",
463+
"nginx.org/limit-req-delay",
464+
"nginx.org/limit-req-no-delay",
465+
"nginx.org/limit-req-dry-run",
466+
"nginx.org/limit-req-log-level",
467+
"nginx.org/limit-req-reject-code",
468+
"nginx.org/limit-req-scale",
469+
}
470+
471+
hasRateLimitAnnotation := false
472+
for _, annotation := range rateLimitAnnotations {
473+
if _, exists := annotations[annotation]; exists {
474+
hasRateLimitAnnotation = true
475+
break
476+
}
477+
}
478+
if hasRateLimitAnnotation {
479+
mandatoryAnnotations := []string{
480+
"nginx.org/limit-req-rate",
481+
"nginx.org/limit-req-key",
482+
"nginx.org/limit-req-zone-size",
483+
}
484+
485+
for _, mandatory := range mandatoryAnnotations {
486+
if _, exists := annotations[mandatory]; !exists {
487+
errors = append(errors, fmt.Errorf("ingress %s/%s: rate-limiting configuration requires mandatory annotation %s", context.GetNamespace(), context.GetName(), mandatory))
488+
}
489+
}
490+
}
491+
457492
if requestRateLimit, exists := annotations["nginx.org/limit-req-rate"]; exists {
458493
if rate, err := ParseRequestRate(requestRateLimit); err != nil {
459494
errors = append(errors, fmt.Errorf("ingress %s/%s: invalid value for nginx.org/limit-req-rate: got %s: %w", context.GetNamespace(), context.GetName(), requestRateLimit, err))

internal/configs/annotations_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,39 @@ func TestParseRateLimitAnnotations(t *testing.T) {
215215
}, NewDefaultConfigParams(context.Background(), false), ctx); len(errors) == 0 {
216216
t.Error("No Errors when parsing invalid log level")
217217
}
218+
219+
if errors := parseRateLimitAnnotations(map[string]string{
220+
"nginx.org/limit-req-rate": "1r/s",
221+
}, NewDefaultConfigParams(context.Background(), false), ctx); len(errors) != 2 {
222+
t.Errorf("Expected 2 errors for missing mandatory annotations, got %d", len(errors))
223+
}
224+
225+
if errors := parseRateLimitAnnotations(map[string]string{
226+
"nginx.org/limit-req-rate": "1r/s",
227+
"nginx.org/limit-req-key": "${binary_remote_addr}",
228+
}, NewDefaultConfigParams(context.Background(), false), ctx); len(errors) != 1 {
229+
t.Errorf("Expected 1 error for missing mandatory annotation, got %d", len(errors))
230+
}
231+
232+
if errors := parseRateLimitAnnotations(map[string]string{
233+
"nginx.org/limit-req-burst": "10",
234+
}, NewDefaultConfigParams(context.Background(), false), ctx); len(errors) != 3 {
235+
t.Errorf("Expected 3 errors for missing all mandatory annotations, got %d", len(errors))
236+
}
237+
238+
if errors := parseRateLimitAnnotations(map[string]string{
239+
"nginx.org/proxy-connect-timeout": "30s",
240+
}, NewDefaultConfigParams(context.Background(), false), ctx); len(errors) != 0 {
241+
t.Errorf("Expected 0 errors for non rate-limiting annotations, got %d", len(errors))
242+
}
243+
244+
if errors := parseRateLimitAnnotations(map[string]string{
245+
"nginx.org/limit-req-rate": "1r/s",
246+
"nginx.org/limit-req-key": "${binary_remote_addr}",
247+
"nginx.org/limit-req-zone-size": "10m",
248+
}, NewDefaultConfigParams(context.Background(), false), ctx); len(errors) != 0 {
249+
t.Errorf("Expected 0 errors for complete mandatory annotations, got %d", len(errors))
250+
}
218251
}
219252

220253
func BenchmarkParseRewrites(b *testing.B) {

0 commit comments

Comments
 (0)