grapsus-config 0.5.12

Configuration loading and validation for Grapsus reverse proxy
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
# KDL Configuration Format

Grapsus uses [KDL](https://kdl.dev/) as its primary configuration format. KDL is a document language designed for human readability while being easy to parse.

## Why KDL?

- **Human-friendly**: Clean syntax, easy to read and write
- **Diff-friendly**: Minimal syntax noise for version control
- **Type-safe**: Supports strings, numbers, booleans, and null
- **Comments**: Both line (`//`) and block (`/* */`) comments
- **No ambiguity**: Clear structure without YAML's gotchas

## Basic Syntax

### Nodes

Nodes are the basic building blocks:

```kdl
// Simple node with a value
server-name "grapsus"

// Node with multiple arguments
target "127.0.0.1" 3000

// Node with properties (named arguments)
listener address="0.0.0.0:8080" protocol="http"

// Node with children (block)
server {
    worker-threads 4
    max-connections 10000
}
```

### Values

```kdl
// Strings (quoted)
name "my-service"

// Numbers
port 8080
timeout 30.5
weight 1

// Booleans
enabled true
disabled false

// Null
optional-field null
```

### Comments

```kdl
// Line comment

/*
 * Block comment
 * spanning multiple lines
 */

server {
    worker-threads 4  // Inline comment
}
```

## Grapsus Configuration Structure

### Top-Level Nodes

```kdl
// Schema version (required for compatibility)
schema-version "1.0"

// Server configuration
server {
    // ...
}

// Listener definitions
listeners {
    listener "name" {
        // ...
    }
}

// Route definitions
routes {
    route "name" {
        // ...
    }
}

// Upstream pool definitions
upstreams {
    upstream "name" {
        // ...
    }
}

// Filter definitions
filters {
    filter "name" {
        // ...
    }
}

// Agent definitions
agents {
    agent "name" {
        // ...
    }
}

// WAF configuration
waf {
    // ...
}

// Observability settings
observability {
    // ...
}

// Global limits
limits {
    // ...
}

// Cache configuration
cache {
    // ...
}
```

## Configuration Examples

### Server Block

```kdl
server {
    worker-threads 0          // 0 = auto-detect CPU cores
    max-connections 10000
    graceful-shutdown-timeout-secs 30
    daemon false
    pid-file "/var/run/grapsus.pid"
    user "grapsus"
    group "grapsus"
    trace-id-format "tinyflake"  // or "uuid"
    auto-reload true
}
```

### Listeners Block

```kdl
listeners {
    // HTTP listener
    listener "http" {
        address "0.0.0.0:8080"
        protocol "http"
        request-timeout-secs 60
        keepalive-timeout-secs 75
        default-route "catch-all"
    }

    // HTTPS listener with TLS
    listener "https" {
        address "0.0.0.0:8443"
        protocol "https"
        request-timeout-secs 60

        tls {
            cert-file "/etc/ssl/certs/server.crt"
            key-file "/etc/ssl/private/server.key"
            min-version "tls1.2"
            client-auth false
            ocsp-stapling true

            // SNI support for multiple domains
            additional-certs {
                cert hostnames=["api.example.com"] {
                    cert-file "/etc/ssl/certs/api.crt"
                    key-file "/etc/ssl/private/api.key"
                }
            }
        }
    }

    // HTTP/2 listener
    listener "h2" {
        address "0.0.0.0:8443"
        protocol "h2"
        max-concurrent-streams 100
    }
}
```

### Routes Block

```kdl
routes {
    // Basic route
    route "api" {
        priority "normal"
        matches {
            path-prefix "/api"
        }
        upstream "api-backend"
    }

    // Route with multiple match conditions
    route "admin" {
        priority "high"
        matches {
            path-prefix "/admin"
            host "admin.example.com"
            method ["GET" "POST"]
        }
        upstream "admin-backend"
        waf-enabled true
    }

    // Static file serving
    route "static" {
        matches {
            path-prefix "/static"
        }
        service-type "static"
        static-files {
            root "/var/www/static"
            index "index.html"
            cache-control "public, max-age=3600"
            compress true
        }
    }

    // Built-in handler
    route "health" {
        priority "high"
        matches {
            path "/health"
        }
        service-type "builtin"
        builtin-handler "health"
    }

    // Inference route (LLM)
    route "llm" {
        matches {
            path-prefix "/v1/chat"
        }
        service-type "inference"
        upstream "openai-backend"
        inference {
            provider "openai"
            rate-limit {
                tokens-per-minute 100000
                burst-tokens 10000
            }
        }
    }

    // Route with policies
    route "api-protected" {
        matches {
            path-prefix "/api/v2"
        }
        upstream "api-backend"
        policies {
            timeout-secs 30
            max-body-size "10MB"
            failure-mode "closed"
            request-headers {
                set {
                    "X-Forwarded-Proto" "https"
                }
                remove ["X-Debug"]
            }
        }
        filters ["auth" "rate-limit"]
    }
}
```

### Upstreams Block

```kdl
upstreams {
    // Simple upstream
    upstream "backend" {
        target "127.0.0.1:3000"
    }

    // Multiple targets with load balancing
    upstream "api-cluster" {
        target "10.0.0.1:8080" weight=3
        target "10.0.0.2:8080" weight=2
        target "10.0.0.3:8080" weight=1

        load-balancing "weighted-round-robin"

        health-check {
            type "http"
            path "/health"
            interval-secs 10
            timeout-secs 5
            healthy-threshold 2
            unhealthy-threshold 3
        }

        connection-pool {
            max-connections 100
            max-idle 20
            idle-timeout-secs 60
        }

        timeouts {
            connect-secs 10
            request-secs 60
            read-secs 30
            write-secs 30
        }
    }

    // Upstream with TLS
    upstream "secure-backend" {
        target "api.internal:443"

        tls {
            sni "api.internal"
            insecure-skip-verify false
            ca-cert "/etc/ssl/certs/internal-ca.crt"
        }

        http-version {
            min-version 2
            max-version 2
        }
    }
}
```

### Filters Block

```kdl
filters {
    // Rate limiting filter
    filter "rate-limit" {
        type "rate-limit"
        max-rps 100
        burst 20
        key "client-ip"
        on-limit "reject"
        status-code 429
    }

    // Headers filter
    filter "security-headers" {
        type "headers"
        phase "response"
        set {
            "X-Frame-Options" "DENY"
            "X-Content-Type-Options" "nosniff"
            "Strict-Transport-Security" "max-age=31536000"
        }
    }

    // Compression filter
    filter "compress" {
        type "compress"
        algorithms ["gzip" "brotli"]
        min-size 1024
        level 6
    }

    // CORS filter
    filter "cors" {
        type "cors"
        allowed-origins ["https://example.com"]
        allowed-methods ["GET" "POST" "PUT" "DELETE"]
        allow-credentials true
        max-age-secs 86400
    }

    // GeoIP filter
    filter "geo-block" {
        type "geo"
        database-path "/etc/grapsus/GeoLite2-Country.mmdb"
        action "block"
        countries ["RU" "CN" "KP"]
        on-failure "open"
    }

    // Agent filter
    filter "auth" {
        type "agent"
        agent "auth-agent"
        timeout-ms 100
        failure-mode "closed"
    }
}
```

### Agents Block

```kdl
agents {
    // Unix socket agent
    agent "waf-agent" {
        type "waf"
        transport {
            unix-socket "/var/run/waf-agent.sock"
        }
        events ["request-headers" "request-body"]
        timeout-ms 50
        failure-mode "open"
        max-request-body-bytes 1048576
    }

    // gRPC agent
    agent "auth-agent" {
        type "auth"
        transport {
            grpc {
                address "localhost:50051"
                tls {
                    insecure-skip-verify false
                    ca-cert "/etc/ssl/certs/ca.crt"
                }
            }
        }
        events ["request-headers"]
        timeout-ms 100
        failure-mode "closed"
        circuit-breaker {
            failure-threshold 5
            recovery-timeout-secs 30
        }
    }
}
```

### WAF Block

```kdl
waf {
    engine "coraza"
    mode "prevention"  // or "detection", "off"
    audit-log true

    ruleset {
        crs-version "4.0"
        paranoia-level 1
        anomaly-threshold 5
        custom-rules-dir "/etc/grapsus/waf-rules"

        exclusions {
            exclusion {
                rule-ids ["920170" "920180"]
                scope "path" "/api/upload"
            }
        }
    }

    body-inspection {
        inspect-request-body true
        inspect-response-body false
        max-inspection-bytes 1048576
        content-types [
            "application/json"
            "application/x-www-form-urlencoded"
            "multipart/form-data"
        ]
        decompress false
        max-decompression-ratio 100.0
    }
}
```

### Observability Block

```kdl
observability {
    metrics {
        enabled true
        address "0.0.0.0:9090"
        path "/metrics"
        high-cardinality false
    }

    logging {
        level "info"
        format "json"
        timestamps true

        access-log {
            enabled true
            file "/var/log/grapsus/access.log"
            format "json"
            sample-rate 1.0
            include-trace-id true
        }

        error-log {
            enabled true
            file "/var/log/grapsus/error.log"
            level "warn"
        }

        audit-log {
            enabled true
            file "/var/log/grapsus/audit.log"
            log-blocked true
            log-waf-events true
        }
    }

    tracing {
        backend {
            otlp {
                endpoint "http://jaeger:4317"
            }
        }
        sampling-rate 0.01
        service-name "grapsus"
    }
}
```

### Limits Block

```kdl
limits {
    max-header-size-bytes 8192
    max-header-count 100
    max-body-size-bytes 10485760  // 10MB
    max-connections-per-client 100
}
```

### Cache Block

```kdl
cache {
    enabled true
    backend "memory"  // or "disk", "hybrid"
    max-size-bytes 104857600  // 100MB
    lock-timeout-secs 10

    // For disk backend
    disk-path "/var/cache/grapsus"
    disk-shards 16
}
```

#### Per-Route Cache Configuration

Cache can be configured per-route inside the `policies` block:

```kdl
route "api" {
    matches {
        path-prefix "/api"
    }
    upstream "api-backend"
    policies {
        cache {
            enabled true
            default-ttl-secs 300
            vary-headers "Accept" "Accept-Encoding"
            stale-while-revalidate-secs 60

            // Exclude specific file extensions from caching
            exclude-extensions "php" "html" "asp"

            // Exclude paths using glob patterns (*, **, ?)
            exclude-paths "/wp-admin/**" "/login" "/api/auth/**"
        }
    }
}
```

The `exclude-extensions` and `exclude-paths` options are useful when a broad route pattern (e.g., `path-prefix "/"`) should cache most content but skip certain dynamic paths or file types.

## Property Naming

KDL properties use `kebab-case`:

```kdl
// Correct
max-connections 1000
request-timeout-secs 60
waf-enabled true

// Incorrect (will not parse correctly)
maxConnections 1000
request_timeout_secs 60
wafEnabled true
```

## Lists and Arrays

Arrays are specified with brackets:

```kdl
// Array of strings
methods ["GET" "POST" "PUT"]

// Array in a match condition
matches {
    method ["GET" "POST"]
    path-prefix "/api"
}

// Array of numbers
ports [8080 8081 8082]
```

## Multi-Line Strings

For long values, use raw strings:

```kdl
custom-error-page r#"
<!DOCTYPE html>
<html>
<body>
<h1>Error</h1>
</body>
</html>
"#
```

## Including Other Files

While KDL itself doesn't have includes, Grapsus's multi-file loader supports directory-based configuration:

```
config/
├── main.kdl
├── routes/
│   └── *.kdl
└── upstreams/
    └── *.kdl
```