fortress-api-server 1.0.1

REST API server for Fortress secure database system
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
//! Server configuration management
//!
//! This module provides configuration structures for the Fortress server,
//! including network settings, security options, and feature flags.

use serde::{Deserialize, Serialize};
use std::net::SocketAddr;
use std::path::PathBuf;
use fortress_core::config::Config as CoreConfig;

/// Default server host
pub const DEFAULT_HOST: &str = "0.0.0.0";

/// Default server port
pub const DEFAULT_PORT: u16 = 8080;

/// Maximum request body size (default: 10MB)
pub const DEFAULT_MAX_BODY_SIZE: usize = 10 * 1024 * 1024;

/// Default request timeout in seconds
pub const DEFAULT_REQUEST_TIMEOUT: u64 = 30;

/// Default CORS origins
pub const DEFAULT_CORS_ORIGINS: &[&str] = &["*"];

/// Server configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServerConfig {
    /// Network configuration
    pub network: NetworkConfig,
    
    /// Security configuration
    pub security: SecurityConfig,
    
    /// Core Fortress configuration
    pub core: CoreConfig,
    
    /// Feature flags
    pub features: FeatureFlags,
    
    /// Logging configuration
    pub logging: LoggingConfig,
    
    /// Metrics configuration
    pub metrics: MetricsConfig,
    
    /// Storage configuration
    pub storage: CoreConfig,
}

impl Default for ServerConfig {
    fn default() -> Self {
        Self {
            network: NetworkConfig::default(),
            security: SecurityConfig::default(),
            core: CoreConfig::default(),
            features: FeatureFlags::default(),
            logging: LoggingConfig::default(),
            metrics: MetricsConfig::default(),
            storage: CoreConfig::default(),
        }
    }
}

/// Network configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NetworkConfig {
    /// Server bind address
    pub host: String,
    
    /// Server port
    pub port: u16,
    
    /// Maximum request body size in bytes
    pub max_body_size: usize,
    
    /// Request timeout in seconds
    pub request_timeout: u64,
    
    /// Keep-alive timeout in seconds
    pub keep_alive: u64,
    
    /// Maximum concurrent connections
    pub max_connections: usize,
}

impl Default for NetworkConfig {
    fn default() -> Self {
        Self {
            host: DEFAULT_HOST.to_string(),
            port: DEFAULT_PORT,
            max_body_size: DEFAULT_MAX_BODY_SIZE,
            request_timeout: DEFAULT_REQUEST_TIMEOUT,
            keep_alive: 75,
            max_connections: 10000,
        }
    }
}

impl NetworkConfig {
    /// Get the full bind address
    pub fn bind_address(&self) -> std::result::Result<SocketAddr, std::net::AddrParseError> {
        format!("{}:{}", self.host, self.port).parse()
    }
}

/// Security configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// JWT secret for authentication
    pub jwt_secret: String,
    
    /// Token expiration time in seconds
    pub token_expiration: u64,
    
    /// CORS configuration
    pub cors: CorsConfig,
    
    /// Rate limiting
    pub rate_limit: RateLimitConfig,
    
    /// TLS configuration
    pub tls: Option<TlsConfig>,
}

impl Default for SecurityConfig {
    fn default() -> Self {
        Self {
            jwt_secret: generate_default_jwt_secret(),
            token_expiration: 3600, // 1 hour
            cors: CorsConfig::default(),
            rate_limit: RateLimitConfig::default(),
            tls: None,
        }
    }
}

/// CORS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CorsConfig {
    /// Allowed origins
    pub allowed_origins: Vec<String>,
    
    /// Allowed methods
    pub allowed_methods: Vec<String>,
    
    /// Allowed headers
    pub allowed_headers: Vec<String>,
    
    /// Allow credentials
    pub allow_credentials: bool,
}

impl Default for CorsConfig {
    fn default() -> Self {
        Self {
            allowed_origins: DEFAULT_CORS_ORIGINS.iter().map(|s| s.to_string()).collect(),
            allowed_methods: vec![
                "GET".to_string(),
                "POST".to_string(),
                "PUT".to_string(),
                "DELETE".to_string(),
                "PATCH".to_string(),
                "OPTIONS".to_string(),
            ],
            allowed_headers: vec![
                "Content-Type".to_string(),
                "Authorization".to_string(),
                "X-Requested-With".to_string(),
            ],
            allow_credentials: false,
        }
    }
}

/// Rate limiting configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimitConfig {
    /// Enable rate limiting
    pub enabled: bool,
    
    /// Maximum requests per minute
    pub requests_per_minute: u32,
    
    /// Maximum requests per hour
    pub requests_per_hour: u32,
    
    /// Burst size
    pub burst_size: u32,
    
    /// Rate limiting algorithm
    #[serde(default = "default_rate_limit_algorithm")]
    pub algorithm: RateLimitAlgorithm,
    
    /// DDoS protection settings
    #[serde(default)]
    pub ddos_protection: DdosProtectionConfig,
}

/// Rate limiting algorithms
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RateLimitAlgorithm {
    /// Token bucket algorithm (default)
    TokenBucket,
    /// Sliding window counter
    SlidingWindow,
    /// Fixed window counter
    FixedWindow,
    /// Leaky bucket
    LeakyBucket,
}

/// DDoS protection configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DdosProtectionConfig {
    /// Enable DDoS protection
    pub enabled: bool,
    
    /// Global requests per second threshold
    pub global_rps_threshold: Option<u32>,
    
    /// IP requests per second threshold
    pub ip_rps_threshold: Option<u32>,
    
    /// Auto-block threshold
    pub auto_block_threshold: Option<u32>,
    
    /// Block duration in seconds
    pub block_duration_seconds: u64,
    
    /// Reputation decay rate per hour
    pub reputation_decay_rate: u8,
}

fn default_rate_limit_algorithm() -> RateLimitAlgorithm {
    RateLimitAlgorithm::TokenBucket
}

impl Default for RateLimitConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            requests_per_minute: 60,
            requests_per_hour: 1000,
            burst_size: 10,
            algorithm: RateLimitAlgorithm::TokenBucket,
            ddos_protection: DdosProtectionConfig::default(),
        }
    }
}

impl Default for DdosProtectionConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            global_rps_threshold: None,
            ip_rps_threshold: None,
            auto_block_threshold: None,
            block_duration_seconds: 300,
            reputation_decay_rate: 10,
        }
    }
}

/// TLS configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TlsConfig {
    /// Path to certificate file
    pub cert_path: PathBuf,
    
    /// Path to private key file
    pub key_path: PathBuf,
    
    /// CA certificate path (optional)
    pub ca_path: Option<PathBuf>,
}

/// OIDC configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OidcConfig {
    /// OIDC issuer URL
    pub issuer_url: String,
    
    /// Client ID
    pub client_id: String,
    
    /// Client secret
    pub client_secret: String,
    
    /// Redirect URI
    pub redirect_uri: String,
    
    /// Scopes to request
    pub scopes: Vec<String>,
    
    /// Enable PKCE
    pub enable_pkce: bool,
}

/// Feature flags
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FeatureFlags {
    /// Enable authentication
    pub auth_enabled: bool,
    
    /// Enable audit logging
    pub audit_enabled: bool,
    
    /// Enable metrics collection
    pub metrics_enabled: bool,
    
    /// Enable health checks
    pub health_enabled: bool,
    
    /// Enable multi-tenant support
    pub multi_tenant: bool,
    
    /// Enable field-level encryption
    pub field_encryption: bool,
    
    /// Enable OIDC/OAuth2 authentication
    pub oidc_enabled: bool,
    
    /// OIDC provider configuration
    pub oidc_config: Option<OidcConfig>,
}

impl Default for FeatureFlags {
    fn default() -> Self {
        Self {
            auth_enabled: true,
            audit_enabled: true,
            metrics_enabled: true,
            health_enabled: true,
            multi_tenant: true,
            field_encryption: true,
            oidc_enabled: false,
            oidc_config: None,
        }
    }
}

/// Logging configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggingConfig {
    /// Log level (trace, debug, info, warn, error)
    pub level: String,
    
    /// Enable JSON logging
    pub json_format: bool,
    
    /// Log file path (optional)
    pub file_path: Option<PathBuf>,
    
    /// Enable request logging
    pub log_requests: bool,
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: "info".to_string(),
            json_format: false,
            file_path: None,
            log_requests: true,
        }
    }
}

/// Metrics configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MetricsConfig {
    /// Enable Prometheus metrics
    pub prometheus_enabled: bool,
    
    /// Metrics endpoint path
    pub metrics_path: String,
    
    /// Metrics collection interval in seconds
    pub collection_interval: u64,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            prometheus_enabled: true,
            metrics_path: "/metrics".to_string(),
            collection_interval: 60,
        }
    }
}

/// Generate a default JWT secret for development
fn generate_default_jwt_secret() -> String {
    use rand::Rng;
    let mut secret = String::with_capacity(64);
    let mut rng = rand::thread_rng();
    
    for _ in 0..64 {
        let chars = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        secret.push(chars[rng.gen_range(0..chars.len())] as char);
    }
    
    secret
}

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

    #[test]
    fn test_default_config() {
        let config = ServerConfig::default();
        
        assert_eq!(config.network.host, DEFAULT_HOST);
        assert_eq!(config.network.port, DEFAULT_PORT);
        assert_eq!(config.security.token_expiration, 3600);
        assert!(config.features.auth_enabled);
        assert!(config.features.audit_enabled);
    }

    #[test]
    fn test_bind_address() {
        let network = NetworkConfig::default();
        let addr = network.bind_address().expect("Default bind address should be valid");
        assert_eq!(addr.port(), DEFAULT_PORT);
    }

    #[test]
    fn test_jwt_secret_generation() {
        let secret1 = generate_default_jwt_secret();
        let secret2 = generate_default_jwt_secret();
        
        assert_eq!(secret1.len(), 64);
        assert_eq!(secret2.len(), 64);
        assert_ne!(secret1, secret2);
    }
}