reasonkit-core 0.1.8

The Reasoning Engine — Auditable Reasoning for Production AI | Rust-Native | Turn Prompts into Protocols
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
//! Enterprise IP Access Control
//!
//! Implements IP allowlisting for enterprise customers with:
//! - CIDR range support
//! - Geo-IP blocking
//! - VPN/proxy detection
//! - Real-time configuration updates

use std::collections::HashMap;
use std::net::IpAddr;
use std::sync::Arc;
use tokio::sync::RwLock;
use uuid::Uuid;

use crate::security::audit::{AuditActor, AuditEvent, AuditEventType, AuditLogger, AuditOutcome};

/// Tenant IP configuration
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TenantIpConfig {
    pub tenant_id: Uuid,
    /// Feature flag: is IP allowlisting enabled
    pub allowlist_enabled: bool,
    /// Allowed IP addresses and CIDR ranges (as strings for serialization)
    pub allowed_networks: Vec<String>,
    /// Allowed countries (ISO 3166-1 alpha-2)
    pub allowed_countries: Option<Vec<String>>,
    /// Denied countries (takes precedence over allowed)
    pub denied_countries: Vec<String>,
    /// Allow requests from known VPN/proxy IPs
    pub allow_vpn: bool,
    /// Allow requests from known datacenter IPs
    pub allow_datacenter: bool,
    /// Last updated timestamp
    pub updated_at: chrono::DateTime<chrono::Utc>,
    /// Updated by (admin user ID)
    pub updated_by: Uuid,
}

impl Default for TenantIpConfig {
    fn default() -> Self {
        Self {
            tenant_id: Uuid::nil(),
            allowlist_enabled: false,
            allowed_networks: Vec::new(),
            allowed_countries: None,
            denied_countries: Vec::new(),
            allow_vpn: true,
            allow_datacenter: true,
            updated_at: chrono::Utc::now(),
            updated_by: Uuid::nil(),
        }
    }
}

/// IP validation result
#[derive(Debug, Clone)]
pub enum IpValidationResult {
    /// IP is allowed
    Allowed {
        matched_network: Option<String>,
        country: Option<String>,
    },
    /// IP is denied
    Denied {
        reason: IpDenialReason,
        details: String,
    },
    /// Tenant has no IP restrictions
    NoRestrictions,
}

/// Reasons for IP denial
#[derive(Debug, Clone)]
pub enum IpDenialReason {
    /// IP not in allowlist
    NotInAllowlist,
    /// Country not allowed
    CountryBlocked,
    /// Known VPN/proxy when not allowed
    VpnDetected,
    /// Known threat actor IP
    ThreatIntelMatch,
    /// Datacenter IP when not allowed
    DatacenterIp,
}

/// GeoIP lookup result
#[derive(Debug, Clone, Default)]
pub struct GeoIpInfo {
    pub country_code: Option<String>,
    pub country_name: Option<String>,
    pub city: Option<String>,
    pub is_vpn: bool,
    pub is_proxy: bool,
    pub is_datacenter: bool,
    pub asn: Option<u32>,
    pub org: Option<String>,
}

/// GeoIP provider trait
#[async_trait::async_trait]
pub trait GeoIpProvider: Send + Sync {
    async fn lookup(&self, ip: IpAddr) -> GeoIpInfo;
}

/// Threat intelligence provider trait
#[async_trait::async_trait]
pub trait ThreatIntelIpProvider: Send + Sync {
    async fn check_ip(&self, ip: IpAddr) -> Option<ThreatIpInfo>;
}

/// Threat intelligence result
#[derive(Debug, Clone)]
pub struct ThreatIpInfo {
    pub category: String,
    pub risk_score: u8,
}

/// Configuration error
#[derive(Debug, thiserror::Error)]
pub enum ConfigError {
    #[error("Network range too large: {0}")]
    NetworkTooLarge(String),
    #[error("Invalid network format: {0}")]
    InvalidNetwork(String),
    #[error("Invalid country code: {0}")]
    InvalidCountry(String),
}

/// IP validation cache entry
struct CacheEntry {
    result: IpValidationResult,
    expires_at: std::time::Instant,
}

/// Enterprise IP Access Controller
pub struct IpAccessController {
    /// Per-tenant configurations
    configs: Arc<RwLock<HashMap<Uuid, TenantIpConfig>>>,
    /// GeoIP database handle
    geoip: Option<Arc<dyn GeoIpProvider>>,
    /// Threat intelligence feed
    threat_intel: Option<Arc<dyn ThreatIntelIpProvider>>,
    /// Audit logger
    audit: Arc<AuditLogger>,
    /// Cache for recently validated IPs (tenant_id:ip -> result)
    cache: Arc<RwLock<HashMap<String, CacheEntry>>>,
    /// Cache TTL
    cache_ttl: std::time::Duration,
}

impl IpAccessController {
    /// Create a new IP access controller
    pub fn new(audit: Arc<AuditLogger>) -> Self {
        Self {
            configs: Arc::new(RwLock::new(HashMap::new())),
            geoip: None,
            threat_intel: None,
            audit,
            cache: Arc::new(RwLock::new(HashMap::new())),
            cache_ttl: std::time::Duration::from_secs(300), // 5 minutes
        }
    }

    /// Set GeoIP provider
    pub fn with_geoip(mut self, provider: Arc<dyn GeoIpProvider>) -> Self {
        self.geoip = Some(provider);
        self
    }

    /// Set threat intelligence provider
    pub fn with_threat_intel(mut self, provider: Arc<dyn ThreatIntelIpProvider>) -> Self {
        self.threat_intel = Some(provider);
        self
    }

    /// Validate an IP address for a tenant
    pub async fn validate(&self, tenant_id: Uuid, ip: IpAddr) -> IpValidationResult {
        // Check cache first
        if let Some(cached) = self.check_cache(tenant_id, ip).await {
            return cached;
        }

        let configs = self.configs.read().await;
        let config = match configs.get(&tenant_id) {
            Some(c) if c.allowlist_enabled => c.clone(),
            _ => return IpValidationResult::NoRestrictions,
        };
        drop(configs);

        // Check threat intelligence first (always)
        if let Some(ref threat_intel) = self.threat_intel {
            if let Some(threat) = threat_intel.check_ip(ip).await {
                let result = IpValidationResult::Denied {
                    reason: IpDenialReason::ThreatIntelMatch,
                    details: format!("IP flagged: {}", threat.category),
                };
                self.audit_denial(tenant_id, ip, &result).await;
                return result;
            }
        }

        // Get GeoIP info
        let geo_info = if let Some(ref geoip) = self.geoip {
            geoip.lookup(ip).await
        } else {
            GeoIpInfo::default()
        };

        // Check denied countries
        if let Some(country) = &geo_info.country_code {
            if config.denied_countries.contains(country) {
                let result = IpValidationResult::Denied {
                    reason: IpDenialReason::CountryBlocked,
                    details: format!("Country {} is blocked", country),
                };
                self.audit_denial(tenant_id, ip, &result).await;
                return result;
            }
        }

        // Check allowed countries (if specified)
        if let Some(allowed) = &config.allowed_countries {
            if let Some(country) = &geo_info.country_code {
                if !allowed.contains(country) {
                    let result = IpValidationResult::Denied {
                        reason: IpDenialReason::CountryBlocked,
                        details: format!("Country {} not in allowlist", country),
                    };
                    self.audit_denial(tenant_id, ip, &result).await;
                    return result;
                }
            }
        }

        // Check VPN/proxy
        if (geo_info.is_vpn || geo_info.is_proxy) && !config.allow_vpn {
            let result = IpValidationResult::Denied {
                reason: IpDenialReason::VpnDetected,
                details: "VPN/proxy connections not allowed".to_string(),
            };
            self.audit_denial(tenant_id, ip, &result).await;
            return result;
        }

        // Check datacenter
        if geo_info.is_datacenter && !config.allow_datacenter {
            let result = IpValidationResult::Denied {
                reason: IpDenialReason::DatacenterIp,
                details: "Datacenter IPs not allowed".to_string(),
            };
            self.audit_denial(tenant_id, ip, &result).await;
            return result;
        }

        // Check network allowlist
        for network_str in &config.allowed_networks {
            if self.ip_in_network(ip, network_str) {
                let result = IpValidationResult::Allowed {
                    matched_network: Some(network_str.clone()),
                    country: geo_info.country_code.clone(),
                };
                self.cache_result(tenant_id, ip, result.clone()).await;
                return result;
            }
        }

        // IP not in any allowed network
        let result = IpValidationResult::Denied {
            reason: IpDenialReason::NotInAllowlist,
            details: "IP not in any allowed network range".to_string(),
        };
        self.audit_denial(tenant_id, ip, &result).await;
        result
    }

    /// Update tenant IP configuration
    pub async fn update_config(
        &self,
        config: TenantIpConfig,
        admin_id: Uuid,
    ) -> Result<(), ConfigError> {
        // Validate network ranges
        for network in &config.allowed_networks {
            self.validate_network(network)?;
        }

        // Clear cache for this tenant
        self.invalidate_cache(config.tenant_id).await;

        // Update configuration
        let mut configs = self.configs.write().await;
        configs.insert(config.tenant_id, config.clone());
        drop(configs);

        // Audit log
        let _ = self
            .audit
            .log(
                AuditEvent::new(AuditEventType::IpConfigUpdated, AuditActor::Admin(admin_id))
                    .with_tenant(config.tenant_id)
                    .with_details(serde_json::json!({
                        "networks_count": config.allowed_networks.len(),
                        "countries_allowed": config.allowed_countries,
                        "countries_denied": config.denied_countries,
                        "allow_vpn": config.allow_vpn,
                        "allow_datacenter": config.allow_datacenter,
                    }))
                    .with_outcome(AuditOutcome::Success),
            )
            .await;

        Ok(())
    }

    /// Get tenant configuration
    pub async fn get_config(&self, tenant_id: Uuid) -> Option<TenantIpConfig> {
        let configs = self.configs.read().await;
        configs.get(&tenant_id).cloned()
    }

    fn validate_network(&self, network: &str) -> Result<(), ConfigError> {
        // Parse CIDR notation
        let parts: Vec<&str> = network.split('/').collect();
        if parts.len() != 2 {
            // Single IP is OK
            if parts.len() == 1 {
                // Validate IP format
                network
                    .parse::<IpAddr>()
                    .map_err(|_| ConfigError::InvalidNetwork(format!("Invalid IP: {}", network)))?;
                return Ok(());
            }
            return Err(ConfigError::InvalidNetwork(format!(
                "Invalid CIDR: {}",
                network
            )));
        }

        let prefix: u8 = parts[1]
            .parse()
            .map_err(|_| ConfigError::InvalidNetwork(format!("Invalid prefix: {}", network)))?;

        // Check if IP part is valid
        let ip: IpAddr = parts[0]
            .parse()
            .map_err(|_| ConfigError::InvalidNetwork(format!("Invalid IP: {}", network)))?;

        // Reject overly broad ranges
        match ip {
            IpAddr::V4(_) if prefix < 16 => {
                return Err(ConfigError::NetworkTooLarge(
                    "IPv4 networks must be /16 or smaller".to_string(),
                ));
            }
            IpAddr::V6(_) if prefix < 48 => {
                return Err(ConfigError::NetworkTooLarge(
                    "IPv6 networks must be /48 or smaller".to_string(),
                ));
            }
            _ => {}
        }

        Ok(())
    }

    fn ip_in_network(&self, ip: IpAddr, network: &str) -> bool {
        // Simple CIDR matching implementation
        let parts: Vec<&str> = network.split('/').collect();

        if parts.len() == 1 {
            // Single IP comparison
            return network.parse::<IpAddr>().map(|n| n == ip).unwrap_or(false);
        }

        if parts.len() != 2 {
            return false;
        }

        let network_ip: IpAddr = match parts[0].parse() {
            Ok(ip) => ip,
            Err(_) => return false,
        };

        let prefix: u8 = match parts[1].parse() {
            Ok(p) => p,
            Err(_) => return false,
        };

        match (ip, network_ip) {
            (IpAddr::V4(ip), IpAddr::V4(net)) => {
                let ip_bits = u32::from(ip);
                let net_bits = u32::from(net);
                let mask = if prefix == 0 {
                    0
                } else {
                    !0u32 << (32 - prefix)
                };
                (ip_bits & mask) == (net_bits & mask)
            }
            (IpAddr::V6(ip), IpAddr::V6(net)) => {
                let ip_bits = u128::from(ip);
                let net_bits = u128::from(net);
                let mask = if prefix == 0 {
                    0
                } else {
                    !0u128 << (128 - prefix)
                };
                (ip_bits & mask) == (net_bits & mask)
            }
            _ => false, // Mismatched IP versions
        }
    }

    async fn check_cache(&self, tenant_id: Uuid, ip: IpAddr) -> Option<IpValidationResult> {
        let key = format!("{}:{}", tenant_id, ip);
        let cache = self.cache.read().await;
        if let Some(entry) = cache.get(&key) {
            if entry.expires_at > std::time::Instant::now() {
                return Some(entry.result.clone());
            }
        }
        None
    }

    async fn cache_result(&self, tenant_id: Uuid, ip: IpAddr, result: IpValidationResult) {
        let key = format!("{}:{}", tenant_id, ip);
        let mut cache = self.cache.write().await;
        cache.insert(
            key,
            CacheEntry {
                result,
                expires_at: std::time::Instant::now() + self.cache_ttl,
            },
        );
    }

    async fn invalidate_cache(&self, tenant_id: Uuid) {
        let mut cache = self.cache.write().await;
        let prefix = format!("{}:", tenant_id);
        cache.retain(|k, _| !k.starts_with(&prefix));
    }

    async fn audit_denial(&self, tenant_id: Uuid, ip: IpAddr, result: &IpValidationResult) {
        if let IpValidationResult::Denied { reason, details } = result {
            let _ = self
                .audit
                .log(
                    AuditEvent::new(AuditEventType::AccessDenied, AuditActor::Anonymous)
                        .with_tenant(tenant_id)
                        .with_details(serde_json::json!({
                            "reason": format!("{:?}", reason),
                            "details": details,
                        }))
                        .with_ip(ip)
                        .with_outcome(AuditOutcome::Failure {
                            error_code: "IP_DENIED".to_string(),
                            error_message: details.clone(),
                        }),
                )
                .await;
        }
    }
}

/// No-op GeoIP provider for testing
pub struct NoOpGeoIpProvider;

#[async_trait::async_trait]
impl GeoIpProvider for NoOpGeoIpProvider {
    async fn lookup(&self, _ip: IpAddr) -> GeoIpInfo {
        GeoIpInfo::default()
    }
}

/// No-op threat intel provider for testing
pub struct NoOpThreatIntelIpProvider;

#[async_trait::async_trait]
impl ThreatIntelIpProvider for NoOpThreatIntelIpProvider {
    async fn check_ip(&self, _ip: IpAddr) -> Option<ThreatIpInfo> {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::security::audit::{AuditConfig, InMemoryAuditStore};

    fn create_test_controller() -> IpAccessController {
        let audit_store = Arc::new(InMemoryAuditStore::new());
        let audit = Arc::new(AuditLogger::new(audit_store, AuditConfig::default()));
        IpAccessController::new(audit)
    }

    #[tokio::test]
    async fn test_no_restrictions_when_disabled() {
        let controller = create_test_controller();
        let tenant_id = Uuid::new_v4();
        let ip: IpAddr = "192.168.1.1".parse().unwrap();

        let result = controller.validate(tenant_id, ip).await;
        assert!(matches!(result, IpValidationResult::NoRestrictions));
    }

    #[tokio::test]
    async fn test_allows_ip_in_allowlist() {
        let controller = create_test_controller();
        let tenant_id = Uuid::new_v4();
        let ip: IpAddr = "192.168.1.100".parse().unwrap();

        let config = TenantIpConfig {
            tenant_id,
            allowlist_enabled: true,
            allowed_networks: vec!["192.168.1.0/24".to_string()],
            ..Default::default()
        };

        controller
            .update_config(config, Uuid::new_v4())
            .await
            .unwrap();

        let result = controller.validate(tenant_id, ip).await;
        assert!(matches!(result, IpValidationResult::Allowed { .. }));
    }

    #[tokio::test]
    async fn test_denies_ip_not_in_allowlist() {
        let controller = create_test_controller();
        let tenant_id = Uuid::new_v4();
        let ip: IpAddr = "10.0.0.1".parse().unwrap();

        let config = TenantIpConfig {
            tenant_id,
            allowlist_enabled: true,
            allowed_networks: vec!["192.168.1.0/24".to_string()],
            ..Default::default()
        };

        controller
            .update_config(config, Uuid::new_v4())
            .await
            .unwrap();

        let result = controller.validate(tenant_id, ip).await;
        assert!(matches!(
            result,
            IpValidationResult::Denied {
                reason: IpDenialReason::NotInAllowlist,
                ..
            }
        ));
    }

    #[test]
    fn test_ip_in_network_v4() {
        let controller = create_test_controller();

        assert!(controller.ip_in_network("192.168.1.100".parse().unwrap(), "192.168.1.0/24"));
        assert!(controller.ip_in_network("192.168.1.1".parse().unwrap(), "192.168.1.0/24"));
        assert!(!controller.ip_in_network("192.168.2.1".parse().unwrap(), "192.168.1.0/24"));
        assert!(controller.ip_in_network("10.0.0.5".parse().unwrap(), "10.0.0.0/8"));
    }

    #[test]
    fn test_validate_network_rejects_large_ranges() {
        let controller = create_test_controller();

        // Should reject /8 for IPv4
        assert!(controller.validate_network("10.0.0.0/8").is_err());

        // Should accept /16
        assert!(controller.validate_network("192.168.0.0/16").is_ok());

        // Should accept single IP
        assert!(controller.validate_network("192.168.1.1").is_ok());
    }
}