inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
use crate::InfernoError;
use anyhow::Result;
use argon2::password_hash::{SaltString, rand_core::OsRng};
use argon2::{Argon2, PasswordHash, PasswordHasher, PasswordVerifier};
use chrono::{DateTime, Duration, Utc};
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation, decode, encode};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::{HashMap, HashSet, VecDeque};
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use tokio::fs;
use tokio::sync::{Mutex, RwLock};
use tracing::{debug, info, warn};

/// Security configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityConfig {
    /// Enable authentication
    pub auth_enabled: bool,
    /// JWT secret key (should be loaded from environment)
    pub jwt_secret: String,
    /// Token expiration time in hours
    pub token_expiry_hours: i64,
    /// Enable API key authentication
    pub api_key_enabled: bool,
    /// Enable rate limiting
    pub rate_limiting_enabled: bool,
    /// Maximum requests per minute per IP
    pub max_requests_per_minute: u32,
    /// Maximum requests per hour per user
    pub max_requests_per_hour: u32,
    /// Enable IP allowlist
    pub ip_allowlist_enabled: bool,
    /// Allowed IP addresses
    pub allowed_ips: Vec<String>,
    /// Enable IP blocklist
    pub ip_blocklist_enabled: bool,
    /// Blocked IP addresses
    pub blocked_ips: Vec<String>,
    /// Enable audit logging
    pub audit_logging_enabled: bool,
    /// Maximum model size for untrusted users (GB)
    pub max_model_size_gb: f64,
    /// Enable input validation
    pub input_validation_enabled: bool,
    /// Maximum input length
    pub max_input_length: usize,
    /// Enable output sanitization
    pub output_sanitization_enabled: bool,
    /// Enable SSL/TLS enforcement
    pub tls_required: bool,
    /// Minimum TLS version
    pub min_tls_version: String,
    /// Data directory for persistent storage
    pub data_dir: PathBuf,
}

impl SecurityConfig {
    /// Validate the security configuration, returning an error if critical settings are missing
    pub fn validate(&self) -> Result<(), String> {
        if self.auth_enabled && self.jwt_secret.is_empty() {
            return Err("JWT secret is required when authentication is enabled. \
                Set INFERNO_JWT_SECRET environment variable or configure jwt_secret in config."
                .to_string());
        }
        if self.jwt_secret.len() < 32 && !self.jwt_secret.is_empty() {
            return Err("JWT secret must be at least 32 characters for security. \
                Use a strong random secret."
                .to_string());
        }
        Ok(())
    }
}

impl Default for SecurityConfig {
    fn default() -> Self {
        // Try to load JWT secret from environment variable
        let jwt_secret = std::env::var("INFERNO_JWT_SECRET").unwrap_or_default();

        Self {
            auth_enabled: true,
            jwt_secret, // Loaded from INFERNO_JWT_SECRET environment variable
            token_expiry_hours: 24,
            api_key_enabled: true,
            rate_limiting_enabled: true,
            max_requests_per_minute: 60,
            max_requests_per_hour: 1000,
            ip_allowlist_enabled: false,
            allowed_ips: vec![],
            ip_blocklist_enabled: false,
            blocked_ips: vec![],
            audit_logging_enabled: true,
            max_model_size_gb: 5.0, // 5GB default
            input_validation_enabled: true,
            max_input_length: 10000,
            output_sanitization_enabled: true,
            tls_required: false,
            min_tls_version: "1.2".to_string(),
            data_dir: dirs::data_dir()
                .unwrap_or_else(|| PathBuf::from("."))
                .join("inferno")
                .join("security"),
        }
    }
}

/// User roles for authorization
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum UserRole {
    Admin,
    User,
    Guest,
    Service,
}

impl UserRole {
    pub fn has_permission(&self, permission: &Permission) -> bool {
        match self {
            UserRole::Admin => true, // Admins have all permissions
            UserRole::User => matches!(
                permission,
                Permission::ReadModels
                    | Permission::RunInference
                    | Permission::ReadMetrics
                    | Permission::UseStreaming
            ),
            UserRole::Guest => matches!(
                permission,
                Permission::ReadModels | Permission::RunInference
            ),
            UserRole::Service => matches!(
                permission,
                Permission::ReadModels
                    | Permission::RunInference
                    | Permission::ReadMetrics
                    | Permission::UseStreaming
                    | Permission::ManageCache
            ),
        }
    }
}

/// Permissions for fine-grained access control
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Permission {
    ReadModels,
    WriteModels,
    DeleteModels,
    RunInference,
    ManageCache,
    ReadMetrics,
    WriteConfig,
    ManageUsers,
    ViewAuditLogs,
    UseStreaming,
    UseDistributed,
    ManageQueue,
}

/// User information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    pub id: String,
    pub username: String,
    pub email: Option<String>,
    pub password_hash: Option<String>, // Argon2 hash
    pub role: UserRole,
    pub api_keys: Vec<ApiKey>,
    pub created_at: DateTime<Utc>,
    pub last_login: Option<DateTime<Utc>>,
    pub is_active: bool,
    pub permissions: HashSet<Permission>,
    pub rate_limit_override: Option<RateLimitConfig>,
}

/// API key for authentication
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApiKey {
    pub id: String,
    pub key_hash: String, // Store hashed version
    pub name: String,
    pub created_at: DateTime<Utc>,
    pub expires_at: Option<DateTime<Utc>>,
    pub last_used: Option<DateTime<Utc>>,
    pub is_active: bool,
    pub permissions: HashSet<Permission>,
}

/// JWT token claims
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenClaims {
    pub sub: String, // Subject (user ID)
    pub username: String,
    pub role: UserRole,
    pub exp: i64,    // Expiration time
    pub iat: i64,    // Issued at
    pub jti: String, // JWT ID for revocation
}

/// Rate limiting configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RateLimitConfig {
    pub requests_per_minute: u32,
    pub requests_per_hour: u32,
    pub requests_per_day: Option<u32>,
    pub burst_size: u32,
}

/// Rate limiter implementation
#[derive(Debug)]
pub struct RateLimiter {
    config: RateLimitConfig,
    minute_window: Arc<Mutex<VecDeque<DateTime<Utc>>>>,
    hour_window: Arc<Mutex<VecDeque<DateTime<Utc>>>>,
    day_window: Arc<Mutex<VecDeque<DateTime<Utc>>>>,
}

impl RateLimiter {
    pub fn new(config: RateLimitConfig) -> Self {
        Self {
            config,
            minute_window: Arc::new(Mutex::new(VecDeque::new())),
            hour_window: Arc::new(Mutex::new(VecDeque::new())),
            day_window: Arc::new(Mutex::new(VecDeque::new())),
        }
    }

    pub async fn check_rate_limit(&self) -> Result<bool> {
        let now = Utc::now();

        // Check minute window
        {
            let mut minute_window = self.minute_window.lock().await;
            let minute_ago = now - Duration::minutes(1);

            // Remove old entries
            while let Some(front) = minute_window.front() {
                if *front < minute_ago {
                    minute_window.pop_front();
                } else {
                    break;
                }
            }

            if minute_window.len() >= self.config.requests_per_minute as usize {
                return Ok(false);
            }

            minute_window.push_back(now);
        }

        // Check hour window
        {
            let mut hour_window = self.hour_window.lock().await;
            let hour_ago = now - Duration::hours(1);

            while let Some(front) = hour_window.front() {
                if *front < hour_ago {
                    hour_window.pop_front();
                } else {
                    break;
                }
            }

            if hour_window.len() >= self.config.requests_per_hour as usize {
                return Ok(false);
            }

            hour_window.push_back(now);
        }

        // Check day window if configured
        if let Some(daily_limit) = self.config.requests_per_day {
            let mut day_window = self.day_window.lock().await;
            let day_ago = now - Duration::days(1);

            while let Some(front) = day_window.front() {
                if *front < day_ago {
                    day_window.pop_front();
                } else {
                    break;
                }
            }

            if day_window.len() >= daily_limit as usize {
                return Ok(false);
            }

            day_window.push_back(now);
        }

        Ok(true)
    }

    pub async fn get_remaining_quota(&self) -> (u32, u32, Option<u32>) {
        let now = Utc::now();

        let minute_remaining = {
            let minute_window = self.minute_window.lock().await;
            let minute_ago = now - Duration::minutes(1);
            let recent_count = minute_window.iter().filter(|&&t| t >= minute_ago).count() as u32;
            self.config.requests_per_minute.saturating_sub(recent_count)
        };

        let hour_remaining = {
            let hour_window = self.hour_window.lock().await;
            let hour_ago = now - Duration::hours(1);
            let recent_count = hour_window.iter().filter(|&&t| t >= hour_ago).count() as u32;
            self.config.requests_per_hour.saturating_sub(recent_count)
        };

        let day_remaining = if let Some(daily_limit) = self.config.requests_per_day {
            let day_window = self.day_window.lock().await;
            let day_ago = now - Duration::days(1);
            let recent_count = day_window.iter().filter(|&&t| t >= day_ago).count() as u32;
            Some(daily_limit.saturating_sub(recent_count))
        } else {
            None
        };

        (minute_remaining, hour_remaining, day_remaining)
    }
}

/// Security manager for the application
#[derive(Debug)]
pub struct SecurityManager {
    config: SecurityConfig,
    pub users: Arc<RwLock<HashMap<String, User>>>,
    api_keys: Arc<RwLock<HashMap<String, String>>>, // key_hash -> user_id
    rate_limiters: Arc<RwLock<HashMap<String, RateLimiter>>>, // user_id/ip -> limiter
    ip_rate_limiters: Arc<RwLock<HashMap<IpAddr, RateLimiter>>>,
    blocked_tokens: Arc<RwLock<HashSet<String>>>, // Revoked JWT IDs
    audit_log: Arc<Mutex<Vec<AuditLogEntry>>>,
}

impl SecurityManager {
    pub fn new(config: SecurityConfig) -> Self {
        Self {
            config,
            users: Arc::new(RwLock::new(HashMap::new())),
            api_keys: Arc::new(RwLock::new(HashMap::new())),
            rate_limiters: Arc::new(RwLock::new(HashMap::new())),
            ip_rate_limiters: Arc::new(RwLock::new(HashMap::new())),
            blocked_tokens: Arc::new(RwLock::new(HashSet::new())),
            audit_log: Arc::new(Mutex::new(Vec::new())),
        }
    }

    /// Initialize with default users and API keys (legacy method)
    ///
    /// # Security Note
    /// The admin password MUST be set via INFERNO_ADMIN_PASSWORD environment variable.
    /// If not set, this method will return an error.
    pub async fn initialize_default_users(&self) -> Result<()> {
        info!("Initializing security manager");

        // Get admin password from environment variable - REQUIRED for security
        let admin_password = std::env::var("INFERNO_ADMIN_PASSWORD").map_err(|_| {
            InfernoError::Security(
                "INFERNO_ADMIN_PASSWORD environment variable is required to create admin user. \
                Set a strong password (at least 12 characters) before starting the application."
                    .to_string(),
            )
        })?;

        // Validate password strength
        if admin_password.len() < 12 {
            return Err(InfernoError::Security(
                "Admin password must be at least 12 characters for security".to_string(),
            )
            .into());
        }

        // Create default admin user with hashed password
        let admin_password_hash = self.hash_password(&admin_password)?;
        let admin_user = User {
            id: "admin".to_string(),
            username: "admin".to_string(),
            email: Some("admin@inferno.ai".to_string()),
            password_hash: Some(admin_password_hash),
            role: UserRole::Admin,
            api_keys: vec![],
            created_at: Utc::now(),
            last_login: None,
            is_active: true,
            permissions: HashSet::new(), // Admin has all permissions by default
            rate_limit_override: None,
        };

        self.create_user(admin_user).await?;

        // Create default service account
        let service_user = User {
            id: "service".to_string(),
            username: "service".to_string(),
            email: None,
            password_hash: None, // Service accounts use API keys instead
            role: UserRole::Service,
            api_keys: vec![],
            created_at: Utc::now(),
            last_login: None,
            is_active: true,
            permissions: HashSet::from([
                Permission::ReadModels,
                Permission::RunInference,
                Permission::ReadMetrics,
                Permission::UseStreaming,
                Permission::ManageCache,
            ]),
            rate_limit_override: Some(RateLimitConfig {
                requests_per_minute: 600,
                requests_per_hour: 10000,
                requests_per_day: Some(100000),
                burst_size: 100,
            }),
        };

        self.create_user(service_user).await?;

        Ok(())
    }

    /// Create a new user
    pub async fn create_user(&self, user: User) -> Result<()> {
        let mut users = self.users.write().await;

        if users.contains_key(&user.id) {
            return Err(InfernoError::Security(format!("User {} already exists", user.id)).into());
        }

        info!("Creating user: {} with role {:?}", user.username, user.role);
        let user_id = user.id.clone();
        users.insert(user_id.clone(), user);

        self.log_audit_event(AuditLogEntry {
            timestamp: Utc::now(),
            user_id: Some("system".to_string()),
            action: AuditAction::UserCreated,
            resource: Some(format!("user:{}", user_id)),
            ip_address: None,
            success: true,
            details: None,
        })
        .await;

        // Release the write lock before saving
        drop(users);

        // Save users to persistent storage
        self.save_users().await?;

        Ok(())
    }

    /// Delete a user
    pub async fn delete_user(&self, user_id: &str) -> Result<()> {
        let mut users = self.users.write().await;

        let user = users
            .remove(user_id)
            .ok_or_else(|| InfernoError::Security(format!("User {} not found", user_id)))?;

        info!("Deleting user: {} ({})", user.username, user_id);

        // Remove associated API keys
        let mut api_keys = self.api_keys.write().await;
        let keys_to_remove: Vec<String> = api_keys
            .iter()
            .filter(|(_, uid)| *uid == user_id)
            .map(|(key_hash, _)| key_hash.clone())
            .collect();

        for key_hash in keys_to_remove {
            api_keys.remove(&key_hash);
        }

        // Remove rate limiters
        let mut rate_limiters = self.rate_limiters.write().await;
        rate_limiters.remove(user_id);

        self.log_audit_event(AuditLogEntry {
            timestamp: Utc::now(),
            user_id: Some("system".to_string()),
            action: AuditAction::UserDeleted,
            resource: Some(format!("user:{}", user_id)),
            ip_address: None,
            success: true,
            details: Some(format!("Deleted user: {}", user.username)),
        })
        .await;

        Ok(())
    }

    /// Generate API key for a user
    pub async fn generate_api_key(
        &self,
        user_id: &str,
        key_name: &str,
        permissions: HashSet<Permission>,
        expires_in_days: Option<i64>,
    ) -> Result<String> {
        let mut users = self.users.write().await;

        let user = users
            .get_mut(user_id)
            .ok_or_else(|| InfernoError::Security(format!("User {} not found", user_id)))?;

        // Generate random API key
        let api_key = Self::generate_random_key();
        let key_hash = Self::hash_api_key(&api_key);

        let api_key_info = ApiKey {
            id: uuid::Uuid::new_v4().to_string(),
            key_hash: key_hash.clone(),
            name: key_name.to_string(),
            created_at: Utc::now(),
            expires_at: expires_in_days.map(|days| Utc::now() + Duration::days(days)),
            last_used: None,
            is_active: true,
            permissions,
        };

        user.api_keys.push(api_key_info);

        // Store key hash mapping
        let mut api_keys = self.api_keys.write().await;
        api_keys.insert(key_hash, user_id.to_string());

        info!("Generated API key '{}' for user {}", key_name, user_id);

        self.log_audit_event(AuditLogEntry {
            timestamp: Utc::now(),
            user_id: Some(user_id.to_string()),
            action: AuditAction::ApiKeyCreated,
            resource: Some(format!("api_key:{}", key_name)),
            ip_address: None,
            success: true,
            details: None,
        })
        .await;

        Ok(api_key)
    }

    /// Authenticate with API key
    pub async fn authenticate_api_key(&self, api_key: &str) -> Result<User> {
        let key_hash = Self::hash_api_key(api_key);

        let api_keys = self.api_keys.read().await;
        let user_id = api_keys
            .get(&key_hash)
            .ok_or_else(|| InfernoError::Security("Invalid API key".to_string()))?;

        let mut users = self.users.write().await;
        let user = users
            .get_mut(user_id)
            .ok_or_else(|| InfernoError::Security("User not found".to_string()))?;

        // Check if user is active
        if !user.is_active {
            return Err(InfernoError::Security("User account is disabled".to_string()).into());
        }

        // Find and update the API key
        for api_key_info in &mut user.api_keys {
            if api_key_info.key_hash == key_hash {
                // Check if key is active
                if !api_key_info.is_active {
                    return Err(InfernoError::Security("API key is disabled".to_string()).into());
                }

                // Check expiration
                if let Some(expires_at) = api_key_info.expires_at {
                    if expires_at < Utc::now() {
                        return Err(
                            InfernoError::Security("API key has expired".to_string()).into()
                        );
                    }
                }

                // Update last used
                api_key_info.last_used = Some(Utc::now());
                break;
            }
        }

        Ok(user.clone())
    }

    /// Generate JWT token for a user
    pub async fn generate_jwt_token(&self, user: &User) -> Result<String> {
        let expiration = Utc::now() + Duration::hours(self.config.token_expiry_hours);

        let claims = TokenClaims {
            sub: user.id.clone(),
            username: user.username.clone(),
            role: user.role.clone(),
            exp: expiration.timestamp(),
            iat: Utc::now().timestamp(),
            jti: uuid::Uuid::new_v4().to_string(),
        };

        let header = Header::new(Algorithm::HS256);
        let encoding_key = EncodingKey::from_secret(self.config.jwt_secret.as_ref());

        let token = encode(&header, &claims, &encoding_key)
            .map_err(|e| InfernoError::Security(format!("JWT encoding failed: {}", e)))?;

        Ok(token)
    }

    /// Verify JWT token
    pub async fn verify_jwt_token(&self, token: &str) -> Result<TokenClaims> {
        let decoding_key = DecodingKey::from_secret(self.config.jwt_secret.as_ref());
        let validation = Validation::new(Algorithm::HS256);

        let token_data = decode::<TokenClaims>(token, &decoding_key, &validation)
            .map_err(|e| InfernoError::Security(format!("JWT verification failed: {}", e)))?;

        let claims = token_data.claims;

        // Check if token is revoked
        let blocked_tokens = self.blocked_tokens.read().await;
        if blocked_tokens.contains(&claims.jti) {
            return Err(InfernoError::Security("Token has been revoked".to_string()).into());
        }

        // Check expiration
        if claims.exp < Utc::now().timestamp() {
            return Err(InfernoError::Security("Token has expired".to_string()).into());
        }

        Ok(claims)
    }

    /// Hash a password using Argon2
    pub fn hash_password(&self, password: &str) -> Result<String> {
        let salt = SaltString::generate(&mut OsRng);
        let argon2 = Argon2::default();

        let password_hash = argon2
            .hash_password(password.as_bytes(), &salt)
            .map_err(|e| InfernoError::Security(format!("Password hashing failed: {}", e)))?;

        Ok(password_hash.to_string())
    }

    /// Verify a password against its hash
    pub fn verify_password(&self, password: &str, hash: &str) -> Result<bool> {
        let parsed_hash = PasswordHash::new(hash)
            .map_err(|e| InfernoError::Security(format!("Invalid password hash: {}", e)))?;

        let argon2 = Argon2::default();

        match argon2.verify_password(password.as_bytes(), &parsed_hash) {
            Ok(()) => Ok(true),
            Err(_) => Ok(false),
        }
    }

    /// Authenticate user with username and password
    pub async fn authenticate_user(&self, username: &str, password: &str) -> Result<Option<User>> {
        let users = self.users.read().await;

        // Find user by username
        let user = users
            .values()
            .find(|u| u.username == username && u.is_active);

        if let Some(user) = user {
            if let Some(ref stored_hash) = user.password_hash {
                if self.verify_password(password, stored_hash)? {
                    // Update last login time
                    let mut user_copy = user.clone();
                    user_copy.last_login = Some(Utc::now());
                    let user_id = user_copy.id.clone();

                    // Drop read lock to acquire write lock
                    drop(users);

                    // Update user in storage
                    {
                        let mut users_write = self.users.write().await;
                        users_write.insert(user_id, user_copy.clone());
                    }

                    // Save to persistent storage
                    if let Err(e) = self.save_users().await {
                        warn!("Failed to save user update after login: {}", e);
                    }

                    return Ok(Some(user_copy));
                }
            }
        }

        Ok(None)
    }

    /// Check rate limit for a user or IP
    pub async fn check_rate_limit(&self, identifier: &str, ip: Option<IpAddr>) -> Result<bool> {
        if !self.config.rate_limiting_enabled {
            return Ok(true);
        }

        // Check user-specific rate limit
        let mut rate_limiters = self.rate_limiters.write().await;

        let user_limiter = rate_limiters
            .entry(identifier.to_string())
            .or_insert_with(|| {
                RateLimiter::new(RateLimitConfig {
                    requests_per_minute: self.config.max_requests_per_minute,
                    requests_per_hour: self.config.max_requests_per_hour,
                    requests_per_day: None,
                    burst_size: 10,
                })
            });

        if !user_limiter.check_rate_limit().await? {
            warn!("Rate limit exceeded for user: {}", identifier);
            return Ok(false);
        }

        // Check IP-based rate limit if provided
        if let Some(ip_addr) = ip {
            let mut ip_limiters = self.ip_rate_limiters.write().await;

            let ip_limiter = ip_limiters.entry(ip_addr).or_insert_with(|| {
                RateLimiter::new(RateLimitConfig {
                    requests_per_minute: self.config.max_requests_per_minute * 2,
                    requests_per_hour: self.config.max_requests_per_hour * 2,
                    requests_per_day: None,
                    burst_size: 20,
                })
            });

            if !ip_limiter.check_rate_limit().await? {
                warn!("Rate limit exceeded for IP: {}", ip_addr);
                return Ok(false);
            }
        }

        Ok(true)
    }

    /// Check if IP is allowed
    pub fn check_ip_access(&self, ip: &IpAddr) -> bool {
        // Check blocklist
        if self.config.ip_blocklist_enabled {
            let ip_str = ip.to_string();
            if self.config.blocked_ips.contains(&ip_str) {
                warn!("Blocked IP attempted access: {}", ip);
                return false;
            }
        }

        // Check allowlist
        if self.config.ip_allowlist_enabled {
            let ip_str = ip.to_string();
            if !self.config.allowed_ips.contains(&ip_str) {
                warn!("Non-allowlisted IP attempted access: {}", ip);
                return false;
            }
        }

        true
    }

    /// Validate input for security threats
    pub fn validate_input(&self, input: &str) -> Result<()> {
        if !self.config.input_validation_enabled {
            return Ok(());
        }

        // Check input length
        if input.len() > self.config.max_input_length {
            return Err(InfernoError::Security(format!(
                "Input exceeds maximum length of {} characters",
                self.config.max_input_length
            ))
            .into());
        }

        // Check for potential injection attacks
        let dangerous_patterns = [
            "<script",
            "javascript:",
            "onerror=",
            "onclick=",
            "../",
            "..\\",
            "%2e%2e",
            "0x",
            "\\x",
            "DROP TABLE",
            "DELETE FROM",
            "INSERT INTO",
            "cmd.exe",
            "/bin/sh",
            "powershell",
        ];

        let input_lower = input.to_lowercase();
        for pattern in &dangerous_patterns {
            if input_lower.contains(pattern) {
                warn!("Potentially dangerous input pattern detected: {}", pattern);
                return Err(InfernoError::Security(
                    "Input contains potentially dangerous content".to_string(),
                )
                .into());
            }
        }

        Ok(())
    }

    /// Sanitize output to prevent data leakage
    pub fn sanitize_output(&self, output: &str) -> String {
        use std::sync::OnceLock;

        // Pre-compiled regex patterns (compiled once, reused)
        // These patterns are known-valid, so unwrap in OnceLock is safe
        static API_KEY_PATTERN: OnceLock<Option<regex::Regex>> = OnceLock::new();
        static EMAIL_PATTERN: OnceLock<Option<regex::Regex>> = OnceLock::new();
        static IP_PATTERN: OnceLock<Option<regex::Regex>> = OnceLock::new();

        if !self.config.output_sanitization_enabled {
            return output.to_string();
        }

        // Remove potential sensitive information patterns
        let mut sanitized = output.to_string();

        // Remove potential API keys (simple pattern)
        let api_key_regex =
            API_KEY_PATTERN.get_or_init(|| regex::Regex::new(r"[A-Za-z0-9]{32,}").ok());
        if let Some(pattern) = api_key_regex {
            sanitized = pattern.replace_all(&sanitized, "[REDACTED]").to_string();
        }

        // Remove email addresses
        let email_regex = EMAIL_PATTERN.get_or_init(|| {
            regex::Regex::new(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}").ok()
        });
        if let Some(pattern) = email_regex {
            sanitized = pattern.replace_all(&sanitized, "[EMAIL]").to_string();
        }

        // Remove IP addresses
        let ip_regex =
            IP_PATTERN.get_or_init(|| regex::Regex::new(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b").ok());
        if let Some(pattern) = ip_regex {
            sanitized = pattern.replace_all(&sanitized, "[IP]").to_string();
        }

        sanitized
    }

    /// Log audit event
    pub async fn log_audit_event(&self, entry: AuditLogEntry) {
        if !self.config.audit_logging_enabled {
            return;
        }

        let mut audit_log = self.audit_log.lock().await;
        audit_log.push(entry.clone());

        // Keep only last 10000 entries in memory
        if audit_log.len() > 10000 {
            audit_log.drain(0..1000);
        }

        debug!("Audit log: {:?}", entry);
    }

    /// Get audit log entries
    pub async fn get_audit_log(&self, limit: Option<usize>) -> Vec<AuditLogEntry> {
        let audit_log = self.audit_log.lock().await;
        let limit = limit.unwrap_or(100);

        audit_log.iter().rev().take(limit).cloned().collect()
    }

    // Helper methods

    fn generate_random_key() -> String {
        use rand::Rng;
        const CHARSET: &[u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        let mut rng = rand::thread_rng();

        (0..32)
            .map(|_| {
                let idx = rng.gen_range(0..CHARSET.len());
                CHARSET[idx] as char
            })
            .collect()
    }

    fn hash_api_key(key: &str) -> String {
        let mut hasher = Sha256::new();
        hasher.update(key.as_bytes());
        format!("{:x}", hasher.finalize())
    }

    /// Revoke a JWT token
    pub async fn revoke_token(&self, jti: String) -> Result<()> {
        let mut blocked_tokens = self.blocked_tokens.write().await;
        blocked_tokens.insert(jti.clone());

        self.log_audit_event(AuditLogEntry {
            timestamp: Utc::now(),
            user_id: None,
            action: AuditAction::TokenRevoked,
            resource: Some(format!("token:{}", jti)),
            ip_address: None,
            success: true,
            details: None,
        })
        .await;

        Ok(())
    }

    /// Save users to persistent storage
    async fn save_users(&self) -> Result<()> {
        let users_file = self.config.data_dir.join("users.json");

        // Create directory if it doesn't exist
        if let Some(parent) = users_file.parent() {
            fs::create_dir_all(parent).await?;
        }

        let users = self.users.read().await;
        let serialized = serde_json::to_string_pretty(&*users)?;
        fs::write(&users_file, serialized).await?;

        debug!("Saved {} users to {}", users.len(), users_file.display());
        Ok(())
    }

    /// Load users from persistent storage
    async fn load_users(&self) -> Result<()> {
        let users_file = self.config.data_dir.join("users.json");

        if !users_file.exists() {
            debug!("Users file does not exist, starting with empty user store");
            return Ok(());
        }

        let content = fs::read_to_string(&users_file).await?;
        let loaded_users: HashMap<String, User> = serde_json::from_str(&content)?;

        let mut users = self.users.write().await;
        *users = loaded_users;

        info!("Loaded {} users from {}", users.len(), users_file.display());
        Ok(())
    }

    /// Initialize the security manager with persistence
    ///
    /// # Security Note
    /// If no users exist, a default admin user will be created using the
    /// INFERNO_ADMIN_PASSWORD environment variable. This is REQUIRED for security.
    pub async fn initialize(&self) -> Result<()> {
        // Validate security configuration
        if let Err(e) = self.config.validate() {
            warn!("Security configuration validation warning: {}", e);
        }

        // Load existing users
        if let Err(e) = self.load_users().await {
            warn!(
                "Failed to load users from storage: {}. Will create default admin user if needed.",
                e
            );
        }

        // Create default admin user if no users exist
        let users_count = {
            let users = self.users.read().await;
            users.len()
        };

        if users_count == 0 {
            info!("No users found, creating default admin user");

            // Get admin password from environment variable - REQUIRED
            let admin_password = std::env::var("INFERNO_ADMIN_PASSWORD").map_err(|_| {
                InfernoError::Security(
                    "INFERNO_ADMIN_PASSWORD environment variable is required to create admin user. \
                    Set a strong password (at least 12 characters) before starting the application."
                        .to_string(),
                )
            })?;

            // Validate password strength
            if admin_password.len() < 12 {
                return Err(InfernoError::Security(
                    "Admin password must be at least 12 characters for security".to_string(),
                )
                .into());
            }

            // Properly hash the password using Argon2
            let password_hash = self.hash_password(&admin_password)?;

            let default_user = User {
                id: "admin".to_string(),
                username: "admin".to_string(),
                email: Some("admin@localhost".to_string()),
                password_hash: Some(password_hash),
                role: UserRole::Admin,
                api_keys: vec![],
                created_at: chrono::Utc::now(),
                last_login: None,
                is_active: true,
                permissions: [
                    Permission::ReadModels,
                    Permission::WriteModels,
                    Permission::DeleteModels,
                    Permission::RunInference,
                    Permission::ManageCache,
                    Permission::ReadMetrics,
                    Permission::WriteConfig,
                    Permission::ManageUsers,
                    Permission::ViewAuditLogs,
                    Permission::UseStreaming,
                    Permission::UseDistributed,
                    Permission::ManageQueue,
                ]
                .into_iter()
                .collect(),
                rate_limit_override: None,
            };
            self.create_user(default_user).await?;
            self.save_users().await?;
        }

        Ok(())
    }

    /// Get all users for admin purposes
    pub async fn get_all_users(&self) -> Vec<User> {
        let users = self.users.read().await;
        users.values().cloned().collect()
    }

    /// Get a specific user by ID
    pub async fn get_user_by_id(&self, user_id: &str) -> Option<User> {
        let users = self.users.read().await;
        users.get(user_id).cloned()
    }

    /// Update a user
    pub async fn update_user(&self, user_id: &str, updated_user: User) -> Result<()> {
        let mut users = self.users.write().await;
        if users.contains_key(user_id) {
            users.insert(user_id.to_string(), updated_user);
            Ok(())
        } else {
            Err(anyhow::anyhow!("User not found"))
        }
    }
}

/// Audit log entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AuditLogEntry {
    pub timestamp: DateTime<Utc>,
    pub user_id: Option<String>,
    pub action: AuditAction,
    pub resource: Option<String>,
    pub ip_address: Option<IpAddr>,
    pub success: bool,
    pub details: Option<String>,
}

/// Audit actions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AuditAction {
    UserCreated,
    UserDeleted,
    UserModified,
    Login,
    Logout,
    ApiKeyCreated,
    ApiKeyRevoked,
    TokenRevoked,
    InferenceRequested,
    ModelLoaded,
    ModelDeleted,
    ConfigChanged,
    RateLimitExceeded,
    UnauthorizedAccess,
    SecurityViolation,
    ModelVerificationStarted,
    ModelVerificationCompleted,
    ModelVerificationFailed,
    SecurityScanStarted,
    SecurityScanCompleted,
    SecurityScanFailed,
}

/// Comprehensive security scanner for models and system components
#[derive(Debug)]
pub struct SecurityScanner {
    config: SecurityScanConfig,
    threat_signatures: ThreatSignatureDatabase,
    audit_logger: Arc<SecurityManager>,
}

/// Security scan configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityScanConfig {
    /// Enable file structure validation
    pub validate_file_structure: bool,
    /// Enable content scanning for embedded threats
    pub scan_embedded_content: bool,
    /// Enable metadata threat scanning
    pub scan_metadata_threats: bool,
    /// Enable signature verification
    pub verify_signatures: bool,
    /// Enable checksum verification
    pub verify_checksums: bool,
    /// Maximum file size to scan (bytes)
    pub max_scan_size: u64,
    /// Quarantine suspicious files
    pub quarantine_enabled: bool,
    /// Quarantine directory path
    pub quarantine_dir: PathBuf,
}

impl Default for SecurityScanConfig {
    fn default() -> Self {
        Self {
            validate_file_structure: true,
            scan_embedded_content: true,
            scan_metadata_threats: true,
            verify_signatures: false, // Requires signature infrastructure
            verify_checksums: true,
            max_scan_size: 50_000_000_000, // 50GB
            quarantine_enabled: true,
            quarantine_dir: PathBuf::from("./quarantine"),
        }
    }
}

/// Threat signature database for pattern matching
#[derive(Debug)]
struct ThreatSignatureDatabase {
    executable_patterns: Vec<Vec<u8>>,
    script_patterns: Vec<Vec<u8>>,
    suspicious_strings: Vec<String>,
    metadata_threats: Vec<String>,
}

impl Default for ThreatSignatureDatabase {
    fn default() -> Self {
        Self {
            executable_patterns: vec![
                b"\x4d\x5a".to_vec(),         // PE header (Windows executable)
                b"\x7f\x45\x4c\x46".to_vec(), // ELF header (Linux executable)
                b"\xfe\xed\xfa\xce".to_vec(), // Mach-O header (macOS executable)
                b"\xfe\xed\xfa\xcf".to_vec(), // Mach-O header (macOS executable)
                b"\xca\xfe\xba\xbe".to_vec(), // Java class file
                b"\x50\x4b\x03\x04".to_vec(), // ZIP file header
            ],
            script_patterns: vec![
                b"#!/bin/sh".to_vec(),
                b"#!/bin/bash".to_vec(),
                b"#!/usr/bin/env".to_vec(),
                b"<script".to_vec(),
                b"javascript:".to_vec(),
                b"data:text/html".to_vec(),
                b"eval(".to_vec(),
                b"exec(".to_vec(),
            ],
            suspicious_strings: vec![
                "password".to_string(),
                "api_key".to_string(),
                "secret".to_string(),
                "token".to_string(),
                "private_key".to_string(),
                "ssh_key".to_string(),
                "credential".to_string(),
                "backdoor".to_string(),
                "exploit".to_string(),
                "payload".to_string(),
            ],
            metadata_threats: vec![
                "exec".to_string(),
                "execute".to_string(),
                "script".to_string(),
                "command".to_string(),
                "shell".to_string(),
                "eval".to_string(),
                "import".to_string(),
                "require".to_string(),
                "load".to_string(),
                "include".to_string(),
                "__import__".to_string(),
                "subprocess".to_string(),
                "os.system".to_string(),
            ],
        }
    }
}

/// Security scan result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SecurityScanResult {
    pub file_path: PathBuf,
    pub scan_timestamp: DateTime<Utc>,
    pub scan_duration_ms: u64,
    pub threats_detected: Vec<ThreatDetection>,
    pub overall_risk_level: RiskLevel,
    pub file_quarantined: bool,
    pub scan_success: bool,
    pub error_message: Option<String>,
}

/// Individual threat detection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThreatDetection {
    pub threat_type: ThreatType,
    pub severity: ThreatSeverity,
    pub description: String,
    pub location: Option<String>,
    pub mitigation_advice: String,
}

/// Types of threats that can be detected
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ThreatType {
    EmbeddedExecutable,
    SuspiciousScript,
    DataExfiltration,
    MetadataThreats,
    InvalidFileStructure,
    SuspiciousSize,
    UnknownFormat,
    ChecksumMismatch,
    SignatureInvalid,
    PolicyViolation,
}

/// Threat severity levels
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ThreatSeverity {
    Critical,
    High,
    Medium,
    Low,
    Info,
}

/// Overall risk assessment
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RiskLevel {
    Critical,
    High,
    Medium,
    Low,
    Safe,
}

impl SecurityScanner {
    /// Create new security scanner
    pub fn new(config: SecurityScanConfig, audit_logger: Arc<SecurityManager>) -> Self {
        Self {
            config,
            threat_signatures: ThreatSignatureDatabase::default(),
            audit_logger,
        }
    }

    /// Perform comprehensive security scan on a file
    pub async fn scan_file(&self, file_path: &Path) -> Result<SecurityScanResult> {
        let start_time = std::time::Instant::now();
        let scan_timestamp = Utc::now();

        info!("Starting security scan for: {}", file_path.display());

        // Log audit event
        self.audit_logger
            .log_audit_event(AuditLogEntry {
                timestamp: scan_timestamp,
                user_id: None,
                action: AuditAction::SecurityScanStarted,
                resource: Some(file_path.to_string_lossy().to_string()),
                ip_address: None,
                success: true,
                details: None,
            })
            .await;

        let mut threats = Vec::new();
        let mut scan_success = true;
        let mut error_message = None;

        // Check if file exists and is accessible
        if !file_path.exists() {
            let error = "File does not exist".to_string();
            error_message = Some(error.clone());
            scan_success = false;

            return Ok(SecurityScanResult {
                file_path: file_path.to_path_buf(),
                scan_timestamp,
                scan_duration_ms: start_time.elapsed().as_millis() as u64,
                threats_detected: threats,
                overall_risk_level: RiskLevel::High,
                file_quarantined: false,
                scan_success,
                error_message,
            });
        }

        // Check file size
        let metadata = match tokio::fs::metadata(file_path).await {
            Ok(meta) => meta,
            Err(e) => {
                error_message = Some(format!("Failed to read file metadata: {}", e));
                scan_success = false;

                return Ok(SecurityScanResult {
                    file_path: file_path.to_path_buf(),
                    scan_timestamp,
                    scan_duration_ms: start_time.elapsed().as_millis() as u64,
                    threats_detected: threats,
                    overall_risk_level: RiskLevel::High,
                    file_quarantined: false,
                    scan_success,
                    error_message,
                });
            }
        };

        let file_size = metadata.len();

        // Check if file is too large to scan
        if file_size > self.config.max_scan_size {
            threats.push(ThreatDetection {
                threat_type: ThreatType::SuspiciousSize,
                severity: ThreatSeverity::Medium,
                description: format!("File size ({} bytes) exceeds maximum scan size", file_size),
                location: None,
                mitigation_advice: "Consider scanning with specialized tools for large files"
                    .to_string(),
            });
        } else {
            // Perform detailed scans
            if self.config.validate_file_structure {
                if let Err(e) = self.scan_file_structure(file_path, &mut threats).await {
                    warn!("File structure scan failed: {}", e);
                }
            }

            if self.config.scan_embedded_content {
                if let Err(e) = self.scan_embedded_content(file_path, &mut threats).await {
                    warn!("Embedded content scan failed: {}", e);
                }
            }

            if self.config.scan_metadata_threats {
                if let Err(e) = self.scan_metadata_threats(file_path, &mut threats).await {
                    warn!("Metadata threat scan failed: {}", e);
                }
            }
        }

        // Assess overall risk level
        let overall_risk_level = self.assess_risk_level(&threats);

        // Quarantine file if necessary
        let mut file_quarantined = false;
        if self.config.quarantine_enabled
            && matches!(overall_risk_level, RiskLevel::Critical | RiskLevel::High)
        {
            if let Err(e) = self.quarantine_file(file_path).await {
                warn!("Failed to quarantine file: {}", e);
            } else {
                file_quarantined = true;
                info!(
                    "File quarantined due to security threats: {}",
                    file_path.display()
                );
            }
        }

        let scan_duration_ms = start_time.elapsed().as_millis() as u64;

        // Log completion
        self.audit_logger
            .log_audit_event(AuditLogEntry {
                timestamp: Utc::now(),
                user_id: None,
                action: if scan_success {
                    AuditAction::SecurityScanCompleted
                } else {
                    AuditAction::SecurityScanFailed
                },
                resource: Some(file_path.to_string_lossy().to_string()),
                ip_address: None,
                success: scan_success,
                details: Some(format!(
                    "Threats: {}, Risk: {:?}, Duration: {}ms",
                    threats.len(),
                    overall_risk_level,
                    scan_duration_ms
                )),
            })
            .await;

        Ok(SecurityScanResult {
            file_path: file_path.to_path_buf(),
            scan_timestamp,
            scan_duration_ms,
            threats_detected: threats,
            overall_risk_level,
            file_quarantined,
            scan_success,
            error_message,
        })
    }

    async fn scan_file_structure(
        &self,
        file_path: &Path,
        threats: &mut Vec<ThreatDetection>,
    ) -> Result<()> {
        let extension = file_path
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("");

        match extension.to_lowercase().as_str() {
            "gguf" => self.validate_gguf_file(file_path, threats).await?,
            "onnx" => self.validate_onnx_file(file_path, threats).await?,
            "safetensors" => self.validate_safetensors_file(file_path, threats).await?,
            "bin" | "pt" | "pth" => self.validate_pytorch_file(file_path, threats).await?,
            _ => {
                threats.push(ThreatDetection {
                    threat_type: ThreatType::UnknownFormat,
                    severity: ThreatSeverity::Low,
                    description: format!("Unknown file format: {}", extension),
                    location: None,
                    mitigation_advice: "Verify file type and ensure it's a valid model format"
                        .to_string(),
                });
            }
        }

        Ok(())
    }

    async fn scan_embedded_content(
        &self,
        file_path: &Path,
        threats: &mut Vec<ThreatDetection>,
    ) -> Result<()> {
        let file_content = tokio::fs::read(file_path).await?;

        // Scan for executable patterns
        for pattern in &self.threat_signatures.executable_patterns {
            if let Some(position) = self.find_pattern(&file_content, pattern) {
                threats.push(ThreatDetection {
                    threat_type: ThreatType::EmbeddedExecutable,
                    severity: ThreatSeverity::Critical,
                    description: "Embedded executable code detected".to_string(),
                    location: Some(format!("Byte offset: {}", position)),
                    mitigation_advice: "Remove or verify embedded executable content".to_string(),
                });
            }
        }

        // Scan for script patterns
        for pattern in &self.threat_signatures.script_patterns {
            if let Some(position) = self.find_pattern(&file_content, pattern) {
                threats.push(ThreatDetection {
                    threat_type: ThreatType::SuspiciousScript,
                    severity: ThreatSeverity::High,
                    description: "Embedded script code detected".to_string(),
                    location: Some(format!("Byte offset: {}", position)),
                    mitigation_advice: "Review and validate embedded script content".to_string(),
                });
            }
        }

        // Check for excessive printable characters (potential data exfiltration)
        let printable_count = file_content
            .iter()
            .filter(|&b| *b >= 32 && *b <= 126)
            .count();
        let printable_ratio = printable_count as f64 / file_content.len() as f64;

        if printable_ratio > 0.7 {
            threats.push(ThreatDetection {
                threat_type: ThreatType::DataExfiltration,
                severity: ThreatSeverity::Medium,
                description: format!(
                    "High ratio of printable characters: {:.1}%",
                    printable_ratio * 100.0
                ),
                location: None,
                mitigation_advice: "Review file content for hidden data or text".to_string(),
            });
        }

        // Scan for suspicious strings
        let content_str = String::from_utf8_lossy(&file_content).to_lowercase();
        for suspicious_string in &self.threat_signatures.suspicious_strings {
            if content_str.contains(suspicious_string) {
                threats.push(ThreatDetection {
                    threat_type: ThreatType::DataExfiltration,
                    severity: ThreatSeverity::Medium,
                    description: format!("Suspicious string found: {}", suspicious_string),
                    location: None,
                    mitigation_advice: "Review file for embedded credentials or sensitive data"
                        .to_string(),
                });
            }
        }

        Ok(())
    }

    async fn scan_metadata_threats(
        &self,
        file_path: &Path,
        threats: &mut Vec<ThreatDetection>,
    ) -> Result<()> {
        let file_content = tokio::fs::read(file_path).await?;
        let content_str = String::from_utf8_lossy(&file_content).to_lowercase();

        for threat_pattern in &self.threat_signatures.metadata_threats {
            if content_str.contains(threat_pattern) {
                threats.push(ThreatDetection {
                    threat_type: ThreatType::MetadataThreats,
                    severity: ThreatSeverity::High,
                    description: format!("Suspicious metadata pattern: {}", threat_pattern),
                    location: None,
                    mitigation_advice: "Review model metadata for malicious content".to_string(),
                });
            }
        }

        Ok(())
    }

    async fn validate_gguf_file(
        &self,
        file_path: &Path,
        threats: &mut Vec<ThreatDetection>,
    ) -> Result<()> {
        let file_content = tokio::fs::read(file_path).await?;

        if file_content.len() < 8 {
            threats.push(ThreatDetection {
                threat_type: ThreatType::InvalidFileStructure,
                severity: ThreatSeverity::High,
                description: "GGUF file too small".to_string(),
                location: None,
                mitigation_advice: "Verify file integrity and re-download if necessary".to_string(),
            });
            return Ok(());
        }

        if &file_content[0..4] != b"GGUF" {
            threats.push(ThreatDetection {
                threat_type: ThreatType::InvalidFileStructure,
                severity: ThreatSeverity::High,
                description: "Invalid GGUF magic bytes".to_string(),
                location: Some("File header".to_string()),
                mitigation_advice: "File may be corrupted or not a valid GGUF file".to_string(),
            });
        }

        let version = u32::from_le_bytes([
            file_content[4],
            file_content[5],
            file_content[6],
            file_content[7],
        ]);

        if !(1..=3).contains(&version) {
            threats.push(ThreatDetection {
                threat_type: ThreatType::InvalidFileStructure,
                severity: ThreatSeverity::Medium,
                description: format!("Unsupported GGUF version: {}", version),
                location: Some("Version header".to_string()),
                mitigation_advice: "Update to a supported GGUF version".to_string(),
            });
        }

        Ok(())
    }

    async fn validate_onnx_file(
        &self,
        file_path: &Path,
        _threats: &mut Vec<ThreatDetection>,
    ) -> Result<()> {
        let file_content = tokio::fs::read(file_path).await?;

        if file_content.len() < 16 {
            return Err(anyhow::anyhow!("ONNX file too small"));
        }

        // Basic ONNX validation would go here
        // This is a placeholder for more comprehensive ONNX validation

        Ok(())
    }

    async fn validate_safetensors_file(
        &self,
        file_path: &Path,
        threats: &mut Vec<ThreatDetection>,
    ) -> Result<()> {
        let file_content = tokio::fs::read(file_path).await?;

        if file_content.len() < 8 {
            threats.push(ThreatDetection {
                threat_type: ThreatType::InvalidFileStructure,
                severity: ThreatSeverity::High,
                description: "SafeTensors file too small".to_string(),
                location: None,
                mitigation_advice: "Verify file integrity".to_string(),
            });
            return Ok(());
        }

        let header_length = u64::from_le_bytes([
            file_content[0],
            file_content[1],
            file_content[2],
            file_content[3],
            file_content[4],
            file_content[5],
            file_content[6],
            file_content[7],
        ]);

        if header_length > file_content.len() as u64 - 8 {
            threats.push(ThreatDetection {
                threat_type: ThreatType::InvalidFileStructure,
                severity: ThreatSeverity::High,
                description: "Invalid SafeTensors header length".to_string(),
                location: Some("File header".to_string()),
                mitigation_advice: "File may be corrupted".to_string(),
            });
        }

        Ok(())
    }

    async fn validate_pytorch_file(
        &self,
        file_path: &Path,
        threats: &mut Vec<ThreatDetection>,
    ) -> Result<()> {
        let file_content = tokio::fs::read(file_path).await?;

        // PyTorch files often start with a ZIP magic number or pickle protocol
        if file_content.len() >= 4 {
            if &file_content[0..4] == b"PK\x03\x04" {
                // ZIP-based PyTorch file
                debug!("Detected ZIP-based PyTorch file");
            } else if file_content[0] == 0x80 {
                // Pickle protocol
                debug!("Detected pickle-based PyTorch file");

                // Pickle files can be dangerous as they can execute arbitrary code
                threats.push(ThreatDetection {
                    threat_type: ThreatType::SuspiciousScript,
                    severity: ThreatSeverity::High,
                    description: "PyTorch pickle file detected - can execute arbitrary code"
                        .to_string(),
                    location: None,
                    mitigation_advice: "Use SafeTensors format instead of pickle for security"
                        .to_string(),
                });
            }
        }

        Ok(())
    }

    fn find_pattern(&self, haystack: &[u8], needle: &[u8]) -> Option<usize> {
        haystack
            .windows(needle.len())
            .position(|window| window == needle)
    }

    fn assess_risk_level(&self, threats: &[ThreatDetection]) -> RiskLevel {
        if threats.is_empty() {
            return RiskLevel::Safe;
        }

        let has_critical = threats
            .iter()
            .any(|t| matches!(t.severity, ThreatSeverity::Critical));
        let has_high = threats
            .iter()
            .any(|t| matches!(t.severity, ThreatSeverity::High));
        let medium_count = threats
            .iter()
            .filter(|t| matches!(t.severity, ThreatSeverity::Medium))
            .count();

        if has_critical {
            RiskLevel::Critical
        } else if has_high || medium_count >= 3 {
            RiskLevel::High
        } else if medium_count >= 1 {
            RiskLevel::Medium
        } else {
            RiskLevel::Low
        }
    }

    async fn quarantine_file(&self, file_path: &Path) -> Result<()> {
        // Create quarantine directory if it doesn't exist
        tokio::fs::create_dir_all(&self.config.quarantine_dir).await?;

        // Generate unique quarantine filename
        let timestamp = Utc::now().format("%Y%m%d_%H%M%S").to_string();
        let original_name = file_path
            .file_name()
            .and_then(|name| name.to_str())
            .unwrap_or("unknown");

        let quarantine_filename = format!("{}_{}", timestamp, original_name);
        let quarantine_path = self.config.quarantine_dir.join(quarantine_filename);

        // Move file to quarantine
        tokio::fs::rename(file_path, &quarantine_path).await?;

        // Create metadata file
        let metadata_path = quarantine_path.with_extension("quarantine_metadata.json");
        let metadata = serde_json::json!({
            "original_path": file_path,
            "quarantined_at": Utc::now(),
            "reason": "Security scan detected threats"
        });

        tokio::fs::write(&metadata_path, serde_json::to_string_pretty(&metadata)?).await?;

        info!(
            "File quarantined: {} -> {}",
            file_path.display(),
            quarantine_path.display()
        );

        Ok(())
    }
}