limiteron 0.3.0-rc.7

Unified flow control framework for Rust
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
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: MIT
//! Admin API 的 OpenAPI 3.0.3 文档构建
//!
//! [`openapi_document`] 是文档的单一事实来源:路由清单与 `AdminService`
//! 契约、`routes::create_router` 注册表保持一一对应;产物经
//! `docs/openapi.json` 入库并由防漂移测试守卫(见 `tests/openapi_drift.rs`,
//! `UPDATE_OPENAPI=1` 环境变量可重写产物)。
//!
//! schema 手写(无 schemars 依赖):Admin API 的请求/响应类型是小而稳定的
//! serde 面,手写 schema 使文档不依赖反射宏链,演进由防漂移测试兜底。

/// OpenAPI 文档版本元信息
pub const OPENAPI_VERSION: &str = "3.0.3";
/// Admin API 文档标题
pub const TITLE: &str = "Limiteron Admin API";
/// 与 `CARGO_PKG_VERSION` 同步的 API 版本
pub const API_VERSION: &str = env!("CARGO_PKG_VERSION");

/// 通用响应包装的 schema 引用构造
fn response_schema(data_schema: &str) -> serde_json::Value {
    serde_json::json!({
        "type": "object",
        "required": ["success", "message"],
        "properties": {
            "success": { "type": "boolean" },
            "message": { "type": "string" },
            "data": { "$ref": data_schema }
        }
    })
}

/// JSON 响应的 OpenAPI response 构造
fn json_response(description: &str, data_schema: &str) -> serde_json::Value {
    serde_json::json!({
        "description": description,
        "content": {
            "application/json": {
                "schema": { "allOf": [
                    { "$ref": "#/components/schemas/ApiResponse" },
                    { "type": "object",
                      "properties": { "data": { "$ref": data_schema } } }
                ]}
            }
        }
    })
}

/// 错误响应构造(ApiResponse 包装、data 为 null)
fn error_response(description: &str) -> serde_json::Value {
    serde_json::json!({
        "description": description,
        "content": {
            "application/json": {
                "schema": { "$ref": "#/components/schemas/ApiResponse" }
            }
        }
    })
}

/// 带 Bearer 安全要求的 operation 构造
fn operation(summary: &str, tag: &str, mut operation: serde_json::Value) -> serde_json::Value {
    operation["summary"] = serde_json::json!(summary);
    operation["tags"] = serde_json::json!([tag]);
    if operation.get("responses").is_none() {
        operation["responses"] = serde_json::json!({});
    }
    operation
}

/// 构建完整 OpenAPI 文档
///
/// 路由清单与 `routes::create_router` 一一对应(含探针/指标三个 bypass
/// 端点);写操作标注 `security: BearerAuth`。
#[must_use]
pub fn openapi_document() -> serde_json::Value {
    let bearer = serde_json::json!([{ "BearerAuth": [] }]);
    let ok_json = |desc: &str, schema: &str| json_response(desc, schema);

    serde_json::json!({
        "openapi": OPENAPI_VERSION,
        "info": {
            "title": TITLE,
            "version": API_VERSION,
            "description": "Limiteron admin control plane: rate limit status, ban management, quota adjustment, circuit breaker status and rule hot reload. All endpoints except probes/metrics require a Bearer API key.",
        },
        "paths": {
            "/healthz": operation("Liveness probe", "probes", serde_json::json!({
                "get": {
                    "responses": {
                        "200": { "description": "Process is alive", "content": { "application/json": { "schema": { "type": "object", "properties": { "status": { "type": "string" } } } } } }
                    }
                }
            })),
            "/readyz": operation("Readiness probe (component-level aggregation)", "probes", serde_json::json!({
                "get": {
                    "responses": {
                        "200": { "description": "All components healthy" },
                        "503": { "description": "One or more components unhealthy; response body contains details" }
                    }
                }
            })),
            "/metrics": operation("Prometheus text-format metrics", "probes", serde_json::json!({
                "get": {
                    "responses": {
                        "200": { "description": "text/plain; version=0.0.4 exposition" }
                    }
                }
            })),
            "/api/v1/status": operation("Overall system status", "status", serde_json::json!({
                "get": {
                    "security": bearer,
                    "responses": {
                        "200": ok_json("System status", "#/components/schemas/SystemStatus"),
                        "401": error_response("Authentication failed")
                    }
                }
            })),
            "/api/v1/status/circuit-breaker": operation("Circuit breaker status", "status", serde_json::json!({
                "get": {
                    "security": bearer,
                    "responses": {
                        "200": ok_json("Circuit breaker status", "#/components/schemas/CircuitBreakerStatus"),
                        "503": error_response("circuit-breaker not configured")
                    }
                }
            })),
            "/api/v1/introspect": operation("Runtime introspection snapshot (rules, decision chains, bans and circuit breakers aggregated)", "status", serde_json::json!({
                "get": {
                    "security": bearer,
                    "responses": {
                        "200": { "description": "Introspection snapshot JSON (structure extends with the feature combination)",
                                 "content": { "application/json": { "schema": { "type": "object" } } } }
                    }
                }
            })),
            "/api/v1/config": operation("Rule hot reload (atomic config swap; previous config retained on failure)", "config", serde_json::json!({
                "post": {
                    "security": bearer,
                    "requestBody": {
                        "required": true,
                        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/FlowControlConfig" } } }
                    },
                    "responses": {
                        "200": { "description": "Application report", "content": { "application/json": { "schema": { "type": "object" } } } },
                        "400": error_response("Validation failed; previous config retained")
                    }
                }
            })),
            "/api/v1/check/batch": operation("Batch check (N keys decided in one request)", "decision", serde_json::json!({
                "post": {
                    "security": bearer,
                    "requestBody": {
                        "required": true,
                        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/BatchCheckBody" } } }
                    },
                    "responses": {
                        "200": { "description": "Per-item decision results", "content": { "application/json": { "schema": { "type": "object" } } } },
                        "400": error_response("Empty batch or above the 1000-item limit")
                    }
                }
            })),
            "/api/v1/tokens/prefetch": operation("Batch token prefetch", "decision", serde_json::json!({
                "post": {
                    "security": bearer,
                    "requestBody": {
                        "required": true,
                        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/TokenPrefetchBody" } } }
                    },
                    "responses": {
                        "200": { "description": "Per-item granted results and summary", "content": { "application/json": { "schema": { "type": "object" } } } },
                        "400": error_response("Empty batch or above the 1000-item limit")
                    }
                }
            })),
            "/api/v1/ban": operation("Create ban (ip/user/mac/geo)", "ban", serde_json::json!({
                "post": {
                    "security": bearer,
                    "requestBody": {
                        "required": true,
                        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/CreateBanRequest" } } }
                    },
                    "responses": {
                        "201": ok_json("Ban details", "#/components/schemas/BanResponse"),
                        "400": error_response("Target or reason validation failed"),
                        "403": error_response("Operator not authorized"),
                        "500": error_response("Internal error"),
                        "503": error_response("ban-manager not configured")
                    }
                }
            })),
            "/api/v1/ban/{target}": operation("Remove ban (?type=ip|user|mac|geo|cidr)", "ban", serde_json::json!({
                "delete": {
                    "security": bearer,
                    "parameters": [
                        { "name": "target", "in": "path", "required": true, "schema": { "type": "string" } },
                        { "name": "type", "in": "query", "required": false,
                          "schema": { "type": "string", "enum": ["ip", "user", "mac", "geo", "cidr"] } }
                    ],
                    "requestBody": {
                        "required": true,
                        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/UnbanRequest" } } }
                    },
                    "responses": {
                        "200": ok_json("Removal result", "#/components/schemas/Unit"),
                        "400": error_response("Unsupported type"),
                        "404": error_response("Ban not found"),
                        "500": error_response("Internal error"),
                        "503": error_response("ban-manager not configured")
                    }
                }
            })),
            "/api/v1/quota/{user_id}": operation("Reset a user's quota usage (new_limit=0); rejects per-tenant quota limit updates", "quota", serde_json::json!({
                "put": {
                    "security": bearer,
                    "parameters": [
                        { "name": "user_id", "in": "path", "required": true, "schema": { "type": "string" } }
                    ],
                    "requestBody": {
                        "required": true,
                        "content": { "application/json": { "schema": { "$ref": "#/components/schemas/UpdateQuotaRequest" } } }
                    },
                    "responses": {
                        "200": ok_json("Operation result", "#/components/schemas/UpdateQuotaResponse"),
                        "400": error_response("Unsupported operation"),
                        "500": error_response("Internal error"),
                        "503": error_response("quota-control not configured")
                    }
                }
            }))
        },
        "components": {
            "securitySchemes": {
                "BearerAuth": { "type": "http", "scheme": "bearer" }
            },
            "schemas": {
                "ApiResponse": response_schema("#/components/schemas/Unit"),
                "Unit": { "type": "object", "nullable": true },
                "SystemStatus": {
                    "type": "object",
                    "required": ["total_requests", "blocked_requests", "success_rate"],
                    "properties": {
                        "total_requests": { "type": "integer", "format": "int64" },
                        "blocked_requests": { "type": "integer", "format": "int64" },
                        "success_rate": { "type": "number", "format": "double" },
                        "active_bans": { "type": "integer", "description": "Present when the ban-manager feature is enabled" },
                        "circuit_breaker": { "type": "string", "description": "Present when the circuit-breaker feature is enabled" }
                    }
                },
                "CircuitBreakerStatus": {
                    "type": "object",
                    "required": ["state", "failure_rate", "slow_call_rate"],
                    "properties": {
                        "state": { "type": "string" },
                        "failure_rate": { "type": "number", "format": "double" },
                        "slow_call_rate": { "type": "number", "format": "double" }
                    }
                },
                "FlowControlConfig": {
                    "type": "object",
                    "required": ["version", "global", "rules"],
                    "properties": {
                        "version": { "type": "string" },
                        "global": { "type": "object" },
                        "rules": { "type": "array", "items": { "type": "object" } }
                    },
                    "description": "Full FlowControlConfig (fields are defined in the config module; only the top-level skeleton is kept here)"
                },
                "BatchCheckBody": {
                    "type": "object",
                    "required": ["requests"],
                    "properties": {
                        "requests": {
                            "type": "array",
                            "maxItems": 1000,
                            "items": {
                                "type": "object",
                                "properties": {
                                    "user_id": { "type": "string", "nullable": true },
                                    "ip": { "type": "string", "nullable": true },
                                    "path": { "type": "string", "nullable": true },
                                    "method": { "type": "string", "nullable": true }
                                }
                            }
                        }
                    }
                },
                "TokenPrefetchBody": {
                    "type": "object",
                    "required": ["items"],
                    "properties": {
                        "items": {
                            "type": "array",
                            "maxItems": 1000,
                            "items": {
                                "type": "object",
                                "required": ["key", "tokens"],
                                "properties": {
                                    "key": { "type": "string" },
                                    "tokens": { "type": "integer", "format": "int64" }
                                }
                            }
                        }
                    }
                },
                "BanTarget": {
                    "type": "object",
                    "required": ["type", "value"],
                    "description": "ip/user/mac use a string value; geo uses an object with a country_code field; cidr uses a string value",
                    "properties": {
                        "type": { "type": "string", "enum": ["ip", "user", "mac", "geo", "cidr"] },
                        "value": {}
                    }
                },
                "CreateBanRequest": {
                    "type": "object",
                    "required": ["target", "reason"],
                    "properties": {
                        "target": { "$ref": "#/components/schemas/BanTarget" },
                        "reason": { "type": "string" },
                        "operator": { "type": "string", "nullable": true, "description": "Deprecated: ignored by the server; identity is determined by the API key mapping" },
                        "duration_secs": { "type": "integer", "format": "int64", "nullable": true }
                    }
                },
                "BanResponse": {
                    "type": "object",
                    "required": ["id", "ban_times", "expires_at", "is_manual"],
                    "properties": {
                        "id": { "type": "string" },
                        "ban_times": { "type": "integer" },
                        "expires_at": { "type": "integer", "format": "int64" },
                        "is_manual": { "type": "boolean" }
                    }
                },
                "UnbanRequest": {
                    "type": "object",
                    "properties": {
                        "reason": { "type": "string", "nullable": true },
                        "operator": { "type": "string", "nullable": true, "description": "Deprecated: ignored by the server" }
                    }
                },
                "UpdateQuotaRequest": {
                    "type": "object",
                    "required": ["resource", "new_limit"],
                    "properties": {
                        "resource": { "type": "string" },
                        "new_limit": { "type": "integer", "format": "int64", "description": "0 = reset signal" },
                        "duration_secs": { "type": "integer", "format": "int64", "nullable": true }
                    }
                },
                "UpdateQuotaResponse": {
                    "type": "object",
                    "required": ["success"],
                    "properties": {
                        "success": { "type": "boolean" },
                        "expires_at": { "type": "integer", "format": "int64", "nullable": true }
                    }
                }
            }
        }
    })
}

/// 序列化为入库产物格式(pretty JSON + 尾换行)
#[must_use]
pub fn openapi_document_pretty() -> String {
    let mut out =
        serde_json::to_string_pretty(&openapi_document()).expect("openapi document serializes");
    out.push('\n');
    out
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn document_is_valid_structure() {
        let doc = openapi_document();
        assert_eq!(doc["openapi"], OPENAPI_VERSION);
        assert_eq!(doc["info"]["version"], API_VERSION);
        let paths = doc["paths"].as_object().expect("paths object");
        // 12 条路由与 routes::create_router 一一对应
        assert_eq!(
            paths.len(),
            12,
            "route list must stay in sync with create_router: {paths:?}"
        );
        for key in [
            "/healthz",
            "/readyz",
            "/metrics",
            "/api/v1/status",
            "/api/v1/status/circuit-breaker",
            "/api/v1/introspect",
            "/api/v1/config",
            "/api/v1/check/batch",
            "/api/v1/tokens/prefetch",
            "/api/v1/ban",
            "/api/v1/ban/{target}",
            "/api/v1/quota/{user_id}",
        ] {
            assert!(paths.contains_key(key), "missing route {key}");
        }
    }

    #[test]
    fn local_refs_resolve() {
        // 所有内部 $ref 指向已定义 schema
        let doc = openapi_document();
        let text = serde_json::to_string(&doc).unwrap();
        let schemas = doc["components"]["schemas"].as_object().unwrap();
        const REF_PREFIX: &str = "#/components/schemas/";
        let mut search_from = 0;
        while let Some(at) = text[search_from..].find(REF_PREFIX) {
            let start = search_from + at + REF_PREFIX.len();
            let rest = &text[start..];
            let end = rest.find('"').expect("ref terminates with quote");
            let name = &rest[..end];
            assert!(
                name.is_empty() || schemas.contains_key(name),
                "dangling $ref: {name}"
            );
            search_from = start;
        }
    }
}