oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
//! Production management handlers for RC.1 features
//!
//! This module provides HTTP endpoints for managing production features:
//! - Load balancing configuration and statistics
//! - Edge caching management and purging
//! - CDN support for static assets

use crate::edge_caching::{EdgeCacheStatistics, InvalidationStrategy};
use crate::load_balancing::{Backend, BackendStatistics, LoadBalancingStrategy};
use crate::server::AppState;
use axum::{
    extract::{Path, State},
    http::StatusCode,
    response::{IntoResponse, Json},
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tracing::instrument;

// ============================================================================
// Load Balancing Endpoints
// ============================================================================

/// Load balancer status response
#[derive(Debug, Serialize)]
pub struct LoadBalancerStatus {
    pub enabled: bool,
    pub strategy: String,
    pub backend_count: usize,
    pub healthy_backends: usize,
    pub sticky_sessions: bool,
}

/// GET /$/load-balancer/status - Get load balancer status
#[instrument(skip(state))]
pub async fn load_balancer_status(
    State(state): State<Arc<AppState>>,
) -> Result<Json<LoadBalancerStatus>, StatusCode> {
    if let Some(ref lb) = state.load_balancer {
        let stats = lb.get_statistics();
        let healthy = stats.values().filter(|s| s.is_healthy).count();

        // Get strategy name from config (using default for now)
        let strategy_name = "round_robin".to_string();

        Ok(Json(LoadBalancerStatus {
            enabled: true,
            strategy: strategy_name,
            backend_count: stats.len(),
            healthy_backends: healthy,
            sticky_sessions: false,
        }))
    } else {
        Err(StatusCode::SERVICE_UNAVAILABLE)
    }
}

/// GET /$/load-balancer/backends - List all backends
#[instrument(skip(state))]
pub async fn list_backends(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    if let Some(ref lb) = state.load_balancer {
        Json(lb.get_statistics()).into_response()
    } else {
        StatusCode::SERVICE_UNAVAILABLE.into_response()
    }
}

/// Request to add a backend
#[derive(Debug, Deserialize)]
pub struct AddBackendRequest {
    pub id: String,
    pub url: String,
    pub weight: Option<u32>,
    pub max_connections: Option<usize>,
    pub health_check_url: Option<String>,
}

/// POST /$/load-balancer/backends - Add a new backend
#[instrument(skip(state))]
pub async fn add_backend(
    State(state): State<Arc<AppState>>,
    Json(req): Json<AddBackendRequest>,
) -> Result<StatusCode, (StatusCode, String)> {
    if let Some(ref lb) = state.load_balancer {
        let backend = Backend {
            id: req.id,
            url: req.url,
            weight: req.weight.unwrap_or(1),
            max_connections: req.max_connections.unwrap_or(100),
            health_check_url: req.health_check_url,
            enabled: true,
        };

        lb.add_backend(backend)
            .map(|_| StatusCode::CREATED)
            .map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))
    } else {
        Err((
            StatusCode::SERVICE_UNAVAILABLE,
            "Load balancer not available".to_string(),
        ))
    }
}

/// DELETE /$/load-balancer/backends/:id - Remove a backend
#[instrument(skip(state))]
pub async fn remove_backend(
    Path(backend_id): Path<String>,
    State(state): State<Arc<AppState>>,
) -> Result<StatusCode, (StatusCode, String)> {
    if let Some(ref lb) = state.load_balancer {
        lb.remove_backend(&backend_id)
            .map(|_| StatusCode::NO_CONTENT)
            .map_err(|e| (StatusCode::BAD_REQUEST, e.to_string()))
    } else {
        Err((
            StatusCode::SERVICE_UNAVAILABLE,
            "Load balancer not available".to_string(),
        ))
    }
}

/// Response for backend selection
#[derive(Debug, Serialize)]
pub struct SelectedBackend {
    pub backend_id: String,
    pub url: String,
}

/// POST /$/load-balancer/select - Select a backend (for testing)
#[instrument(skip(state))]
pub async fn select_backend(
    State(state): State<Arc<AppState>>,
) -> Result<Json<SelectedBackend>, (StatusCode, String)> {
    if let Some(ref lb) = state.load_balancer {
        lb.select_backend(None, None)
            .map(|backend| {
                Json(SelectedBackend {
                    backend_id: backend.id,
                    url: backend.url,
                })
            })
            .map_err(|e| (StatusCode::SERVICE_UNAVAILABLE, e.to_string()))
    } else {
        Err((
            StatusCode::SERVICE_UNAVAILABLE,
            "Load balancer not available".to_string(),
        ))
    }
}

// ============================================================================
// Edge Caching Endpoints
// ============================================================================

/// GET /$/edge-cache/status - Get edge cache status
#[instrument(skip(state))]
pub async fn edge_cache_status(
    State(state): State<Arc<AppState>>,
) -> Result<Json<EdgeCacheStatistics>, StatusCode> {
    if let Some(ref cache) = state.edge_cache_manager {
        Ok(Json(cache.get_statistics()))
    } else {
        Err(StatusCode::SERVICE_UNAVAILABLE)
    }
}

/// Request to purge cache
#[derive(Debug, Deserialize)]
pub struct PurgeRequest {
    pub strategy: String,
    pub targets: Option<Vec<String>>,
}

/// POST /$/edge-cache/purge - Purge cache
#[instrument(skip(state))]
pub async fn purge_cache(
    State(state): State<Arc<AppState>>,
    Json(req): Json<PurgeRequest>,
) -> Result<StatusCode, (StatusCode, String)> {
    if let Some(ref cache) = state.edge_cache_manager {
        match req.strategy.as_str() {
            "all" => cache
                .purge_all()
                .await
                .map(|_| StatusCode::OK)
                .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string())),
            "tags" => {
                if let Some(tags) = req.targets {
                    cache
                        .purge_by_tags(tags)
                        .await
                        .map(|_| StatusCode::OK)
                        .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
                } else {
                    Err((StatusCode::BAD_REQUEST, "Tags required".to_string()))
                }
            }
            "dataset" => {
                if let Some(targets) = req.targets {
                    if let Some(dataset) = targets.first() {
                        cache
                            .purge_dataset(dataset)
                            .await
                            .map(|_| StatusCode::OK)
                            .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
                    } else {
                        Err((StatusCode::BAD_REQUEST, "Dataset name required".to_string()))
                    }
                } else {
                    Err((StatusCode::BAD_REQUEST, "Dataset name required".to_string()))
                }
            }
            _ => Err((StatusCode::BAD_REQUEST, "Invalid strategy".to_string())),
        }
    } else {
        Err((
            StatusCode::SERVICE_UNAVAILABLE,
            "Edge cache not available".to_string(),
        ))
    }
}

/// GET /$/edge-cache/headers - Get cache headers for a query
#[derive(Debug, Deserialize)]
pub struct CacheHeadersRequest {
    pub query: String,
    pub execution_time_ms: u64,
    pub response_size: usize,
}

/// POST /$/edge-cache/headers - Get recommended cache headers for a query
#[instrument(skip(state))]
pub async fn get_cache_headers(
    State(state): State<Arc<AppState>>,
    Json(req): Json<CacheHeadersRequest>,
) -> Result<Json<HashMap<String, String>>, StatusCode> {
    if let Some(ref cache) = state.edge_cache_manager {
        match cache.get_cache_headers(&req.query, req.execution_time_ms, req.response_size) {
            Some(headers) => Ok(Json(headers)),
            None => Ok(Json(HashMap::new())),
        }
    } else {
        Err(StatusCode::SERVICE_UNAVAILABLE)
    }
}

// ============================================================================
// CDN Static Asset Support
// ============================================================================

/// Static asset configuration
#[derive(Debug, Serialize)]
pub struct CdnConfig {
    pub enabled: bool,
    pub base_url: Option<String>,
    pub default_ttl_secs: u64,
    pub supported_types: Vec<String>,
}

/// GET /$/cdn/config - Get CDN configuration
#[instrument]
pub async fn cdn_config() -> Json<CdnConfig> {
    // CDN configuration (can be extended to read from config)
    Json(CdnConfig {
        enabled: true,
        base_url: None,          // Use same origin by default
        default_ttl_secs: 86400, // 24 hours
        supported_types: vec![
            "text/css".to_string(),
            "text/javascript".to_string(),
            "application/javascript".to_string(),
            "image/png".to_string(),
            "image/jpeg".to_string(),
            "image/svg+xml".to_string(),
            "font/woff2".to_string(),
            "font/woff".to_string(),
        ],
    })
}

/// Serve static asset with CDN headers
#[instrument(skip(_state))]
pub async fn serve_static_asset(
    Path(path): Path<String>,
    State(_state): State<Arc<AppState>>,
) -> impl IntoResponse {
    // Static asset serving with CDN-optimized headers
    // This is a placeholder - actual implementation would serve files from disk

    let content_type = match path.rsplit('.').next() {
        Some("css") => "text/css",
        Some("js") => "application/javascript",
        Some("png") => "image/png",
        Some("jpg") | Some("jpeg") => "image/jpeg",
        Some("svg") => "image/svg+xml",
        Some("woff2") => "font/woff2",
        Some("woff") => "font/woff",
        Some("json") => "application/json",
        Some("html") => "text/html",
        _ => "application/octet-stream",
    };

    // CDN-optimized headers
    let headers = [
        ("Content-Type", content_type),
        (
            "Cache-Control",
            "public, max-age=86400, stale-while-revalidate=3600",
        ),
        ("Vary", "Accept-Encoding"),
        ("X-Content-Type-Options", "nosniff"),
    ];

    // Return 404 for now - actual implementation would serve the file
    (StatusCode::NOT_FOUND, headers, "Static asset not found")
}

// ============================================================================
// Security Audit Endpoints
// ============================================================================

/// Security audit status
#[derive(Debug, Serialize)]
pub struct SecurityAuditStatus {
    pub enabled: bool,
    pub total_events: usize,
    pub critical_events: usize,
    pub last_scan: Option<String>,
    pub compliance_status: String,
}

/// GET /$/security/audit/status - Get security audit status
#[instrument(skip(state))]
pub async fn security_audit_status(
    State(state): State<Arc<AppState>>,
) -> Result<Json<SecurityAuditStatus>, StatusCode> {
    if let Some(ref auditor) = state.security_auditor {
        // Get vulnerabilities to determine status
        let vulnerabilities = auditor.get_vulnerabilities().await;
        let critical_count = vulnerabilities
            .iter()
            .filter(|v| matches!(v.severity, crate::security_audit::Severity::Critical))
            .count();

        Ok(Json(SecurityAuditStatus {
            enabled: true,
            total_events: vulnerabilities.len(),
            critical_events: critical_count,
            last_scan: Some(chrono::Utc::now().to_rfc3339()),
            compliance_status: if critical_count == 0 {
                "compliant".to_string()
            } else if critical_count <= 2 {
                "partial".to_string()
            } else {
                "non-compliant".to_string()
            },
        }))
    } else {
        Err(StatusCode::SERVICE_UNAVAILABLE)
    }
}

/// POST /$/security/audit/scan - Trigger a security scan
#[instrument(skip(state))]
pub async fn trigger_security_scan(
    State(state): State<Arc<AppState>>,
) -> Result<StatusCode, StatusCode> {
    if let Some(ref auditor) = state.security_auditor {
        // Trigger async scan
        let _ = auditor.perform_security_scan().await;
        Ok(StatusCode::ACCEPTED)
    } else {
        Err(StatusCode::SERVICE_UNAVAILABLE)
    }
}

// ============================================================================
// DDoS Protection Endpoints
// ============================================================================

/// DDoS protection status
#[derive(Debug, Serialize)]
pub struct DDoSStatus {
    pub enabled: bool,
    pub blocked_ips: usize,
    pub rate_limited_requests: u64,
    pub total_requests: u64,
    pub current_connections: usize,
}

/// GET /$/security/ddos/status - Get DDoS protection status
#[instrument(skip(state))]
pub async fn ddos_status(
    State(state): State<Arc<AppState>>,
) -> Result<Json<DDoSStatus>, StatusCode> {
    if let Some(ref protector) = state.ddos_protector {
        let stats = protector.get_statistics();
        Ok(Json(DDoSStatus {
            enabled: stats.enabled,
            blocked_ips: stats.blocked_ips_count,
            rate_limited_requests: stats.total_violations as u64,
            total_requests: stats.total_requests,
            current_connections: stats.total_ips_tracked,
        }))
    } else {
        Err(StatusCode::SERVICE_UNAVAILABLE)
    }
}

/// Request to manage IP
#[derive(Debug, Deserialize)]
pub struct IpManagementRequest {
    pub ip: String,
    pub action: String, // "whitelist", "blacklist", "unblock"
}

/// POST /$/security/ddos/manage-ip - Manage IP addresses
#[instrument(skip(state))]
pub async fn manage_ip(
    State(state): State<Arc<AppState>>,
    Json(req): Json<IpManagementRequest>,
) -> Result<StatusCode, (StatusCode, String)> {
    if let Some(ref protector) = state.ddos_protector {
        let ip: std::net::IpAddr = req
            .ip
            .parse()
            .map_err(|_| (StatusCode::BAD_REQUEST, "Invalid IP address".to_string()))?;

        match req.action.as_str() {
            "whitelist" => {
                protector
                    .whitelist_ip(ip)
                    .await
                    .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
                Ok(StatusCode::OK)
            }
            "blacklist" => {
                protector
                    .block_ip(ip, crate::ddos_protection::BlockReason::ManualBlock)
                    .await
                    .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
                Ok(StatusCode::OK)
            }
            "unblock" => {
                protector
                    .unblock_ip(ip)
                    .await
                    .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
                Ok(StatusCode::OK)
            }
            _ => Err((StatusCode::BAD_REQUEST, "Invalid action".to_string())),
        }
    } else {
        Err((
            StatusCode::SERVICE_UNAVAILABLE,
            "DDoS protection not available".to_string(),
        ))
    }
}

// ============================================================================
// Disaster Recovery Endpoints
// ============================================================================

/// Disaster recovery status
#[derive(Debug, Serialize)]
pub struct DisasterRecoveryStatus {
    pub enabled: bool,
    pub rpo_minutes: u64,
    pub rto_minutes: u64,
    pub last_recovery_point: Option<String>,
    pub health_status: String,
}

/// GET /$/recovery/status - Get disaster recovery status
#[instrument(skip(state))]
pub async fn disaster_recovery_status(
    State(state): State<Arc<AppState>>,
) -> Result<Json<DisasterRecoveryStatus>, StatusCode> {
    if let Some(ref recovery) = state.disaster_recovery {
        let status = recovery.get_status().await;
        Ok(Json(DisasterRecoveryStatus {
            enabled: status.enabled,
            rpo_minutes: status.rpo_minutes,
            rto_minutes: status.rto_minutes,
            last_recovery_point: status.last_recovery_test.map(|t| t.to_rfc3339()),
            health_status: if status.healthy {
                "healthy".to_string()
            } else {
                "degraded".to_string()
            },
        }))
    } else {
        Err(StatusCode::SERVICE_UNAVAILABLE)
    }
}

/// POST /$/recovery/create-point - Create a recovery point
#[instrument(skip(state))]
pub async fn create_recovery_point(
    State(state): State<Arc<AppState>>,
) -> Result<StatusCode, (StatusCode, String)> {
    if let Some(ref recovery) = state.disaster_recovery {
        recovery
            .create_recovery_point("Manual recovery point".to_string())
            .await
            .map(|_| StatusCode::CREATED)
            .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
    } else {
        Err((
            StatusCode::SERVICE_UNAVAILABLE,
            "Disaster recovery not available".to_string(),
        ))
    }
}

// ============================================================================
// Metrics Endpoint
// ============================================================================

/// GET /metrics - Prometheus metrics export
#[instrument(skip(state))]
pub async fn metrics_handler(
    State(state): State<Arc<AppState>>,
) -> Result<String, (StatusCode, String)> {
    if let Some(ref metrics_service) = state.metrics_service {
        let result: Result<String, crate::error::FusekiError> =
            metrics_service.get_prometheus_metrics().await;
        result.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))
    } else {
        Err((
            StatusCode::SERVICE_UNAVAILABLE,
            "Metrics service not available".to_string(),
        ))
    }
}

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

    #[test]
    fn test_cdn_config() {
        let config = CdnConfig {
            enabled: true,
            base_url: None,
            default_ttl_secs: 86400,
            supported_types: vec!["text/css".to_string()],
        };
        assert!(config.enabled);
        assert_eq!(config.default_ttl_secs, 86400);
    }

    #[test]
    fn test_load_balancer_status() {
        let status = LoadBalancerStatus {
            enabled: true,
            strategy: "round_robin".to_string(),
            backend_count: 3,
            healthy_backends: 3,
            sticky_sessions: false,
        };
        assert!(status.enabled);
        assert_eq!(status.backend_count, 3);
    }
}