regent-sdk 0.9.1

Multi-paradigm configuration management system as a library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
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
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
//! Iptables attribute for firewall rule management
//!
//! This module provides the `IptablesBlockExpectedState` type for managing iptables/ip6tables
//! firewall rules and chains. It supports creating, modifying, and deleting rules across
//! different tables (filter, nat, mangle, raw, security) using a strongly-typed API.
//!
//! **Compatible OS:** Linux (all distributions)
//!
//! # Serialization Behavior
//!
//! All enums in this module use `#[serde(rename_all = "PascalCase")]` for YAML serialization,
//! producing human-readable PascalCase format (e.g., `Input`, `Tcp`, `Output`).
//! However, they implement `std::fmt::Display` to output iptables-native format when generating
//! shell commands (e.g., `INPUT`, `tcp`, `OUTPUT`, `tcp-reset`). This ensures YAML remains
//! user-friendly while generated iptables commands use the correct syntax.
//!
//! # Examples
//!
//! ## Rust API
//!
//! ```no_run
//! use regent_sdk::state::attribute::network::iptables::{IptablesBlockExpectedState, IpVersion};
//! use regent_sdk::{Attribute, ExpectedState, Privilege};
//!
//! // Allow SSH on port 22 using convenience method (defaults to IPv4)
//! let ssh_rule = IptablesBlockExpectedState::allow_ssh(None);
//!
//! // Allow HTTP on port 80 at position 1
//! let http_rule = IptablesBlockExpectedState::allow_tcp_port(80, Some(1));
//!
//! // Allow DNS on port 53 (UDP)
//! let dns_rule = IptablesBlockExpectedState::allow_udp_port(53, None);
//!
//! // Drop all incoming traffic (default deny)
//! let deny_all = IptablesBlockExpectedState::drop_all_incoming(None);
//!
//! let expected_state = ExpectedState::new()
//!     .with_attribute(Attribute::iptables(ssh_rule, Privilege::WithSudo, None))
//!     .with_attribute(Attribute::iptables(http_rule, Privilege::WithSudo, None))
//!     .build();
//! ```
//!
//! ## YAML API
//!
//! ```yaml
//! Attributes:
//!   - Name: Allow SSH on port 22
//!     Privilege: !WithSudo
//!     Detail: !Iptables
//!       State: Present
//!       IpVersion: V4
//!       Action: Append
//!       Table: Filter
//!       Chain: INPUT
//!       Criteria:
//!         Protocol: Tcp
//!         DestinationPort: 22
//!       Target: Accept
//! ```

use crate::error::RegentError;
use crate::hosts::managed_host::InternalApiCallOutcome;
use crate::hosts::managed_host::{AssessCompliance, ReachCompliance, Timeout};
use crate::hosts::properties::{HostProperties, OsKind};
use crate::secrets::SecretProvidersPool;
use crate::state::Check;
use crate::state::attribute::HostHandler;
use crate::state::attribute::Privilege;
use crate::state::attribute::Remediation;
use crate::state::attribute::RemediationsList;
use crate::state::compliance::AttributeComplianceAssessment;

use std::time::Duration;

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::net::IpAddr;
use std::str::FromStr;

/// A Self-validated CIDR block or single IP address
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CidrBlock {
    addr: IpAddr,
    prefix: Option<u8>,
}

impl CidrBlock {
    /// Manually parse and validate an IP or CIDR string.
    pub fn parse(s: &str) -> Result<Self, String> {
        let parts: Vec<&str> = s.split('/').collect();

        if parts.is_empty() || parts.len() > 2 {
            return Err(format!("Invalid CIDR format: '{}'", s));
        }

        let addr = IpAddr::from_str(parts[0])
            .map_err(|e| format!("Invalid IP address '{}': {}", parts[0], e))?;

        let prefix = if parts.len() == 2 {
            let p: u8 = parts[1]
                .parse()
                .map_err(|_| format!("Invalid subnet prefix: '{}'", parts[1]))?;

            let max_prefix = match addr {
                IpAddr::V4(_) => 32,
                IpAddr::V6(_) => 128,
            };

            if p > max_prefix {
                return Err(format!(
                    "Subnet prefix /{} is out of bounds for this IP type (max {})",
                    p, max_prefix
                ));
            }
            Some(p)
        } else {
            None
        };

        Ok(CidrBlock { addr, prefix })
    }

    pub fn to_string(&self) -> String {
        match self.prefix {
            Some(p) => format!("{}/{}", self.addr, p),
            None => self.addr.to_string(),
        }
    }
}

impl Serialize for CidrBlock {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}

impl<'de> Deserialize<'de> for CidrBlock {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        CidrBlock::parse(&s).map_err(serde::de::Error::custom)
    }
}

/// IP version for iptables commands
///
/// Serializes to YAML as `V4` or `V6`.
/// Displays as `iptables` or `ip6tables` for command generation via `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum IpVersion {
    V4,
    V6,
}

impl std::fmt::Display for IpVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IpVersion::V4 => write!(f, "iptables"),
            IpVersion::V6 => write!(f, "ip6tables"),
        }
    }
}

fn default_ip_family() -> IpVersion {
    IpVersion::V4
}

fn is_v4_default(family: &IpVersion) -> bool {
    matches!(family, IpVersion::V4)
}

/// Action for inserting a rule into a chain
///
/// Serializes to YAML as `Append` or `Insert` with a `Position` field.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum IptablesInsertionAction {
    Append,
    #[serde(rename_all = "PascalCase")]
    Insert {
        position: u32,
    },
}

/// Network protocol for iptables rule matching
///
/// Serializes to YAML as `Tcp`, `Udp`, `Icmp`, or `All`.
/// Displays as `tcp`, `udp`, `icmp`, or `all` for command generation via `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum Protocol {
    #[serde(rename_all = "PascalCase")]
    Tcp {
        #[serde(skip_serializing_if = "Option::is_none")]
        source_port: Option<PortSpec>,
        #[serde(skip_serializing_if = "Option::is_none")]
        dest_port: Option<PortSpec>,
        #[serde(skip_serializing_if = "Option::is_none")]
        tcp_flags: Option<TcpFlagsMatch>,
    },
    #[serde(rename_all = "PascalCase")]
    Udp {
        #[serde(skip_serializing_if = "Option::is_none")]
        source_port: Option<PortSpec>,
        #[serde(skip_serializing_if = "Option::is_none")]
        dest_port: Option<PortSpec>,
    },
    #[serde(rename_all = "PascalCase")]
    Icmp {
        #[serde(skip_serializing_if = "Option::is_none")]
        icmp_type: Option<u8>,
        #[serde(skip_serializing_if = "Option::is_none")]
        icmp_code: Option<u8>,
    },
    All,
}

impl std::fmt::Display for Protocol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Protocol::Tcp { .. } => write!(f, "tcp"),
            Protocol::Udp { .. } => write!(f, "udp"),
            Protocol::Icmp { .. } => write!(f, "icmp"),
            Protocol::All => write!(f, "all"),
        }
    }
}

/// TCP Flag matching options
///
/// Serializes to YAML as `Syn`, `Ack`, `Fin`, `Rst`, `Psh`, `Urg`, `FinRst`, `All`, or `None`.
/// Displays as `SYN`, `ACK`, `FIN`, `RST`, `PSH`, `URG`, `FIN,RST`, `ALL`, or `NONE` for command
/// generation via `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum TcpFlag {
    Syn,
    Ack,
    Fin,
    Rst,
    Psh,
    Urg,
    FinRst,
    All,
    None,
}

impl std::fmt::Display for TcpFlag {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            TcpFlag::Syn => write!(f, "SYN"),
            TcpFlag::Ack => write!(f, "ACK"),
            TcpFlag::Fin => write!(f, "FIN"),
            TcpFlag::Rst => write!(f, "RST"),
            TcpFlag::Psh => write!(f, "PSH"),
            TcpFlag::Urg => write!(f, "URG"),
            TcpFlag::FinRst => write!(f, "FIN,RST"),
            TcpFlag::All => write!(f, "ALL"),
            TcpFlag::None => write!(f, "NONE"),
        }
    }
}

/// TCP flags match specification (-m tcp --tcp-flags)
///
/// `mask` specifies which flags to check, and `comp` specifies which of those must be set.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct TcpFlagsMatch {
    pub mask: Vec<TcpFlag>,
    pub comp: Vec<TcpFlag>,
}

/// Rate limiting specification (-m limit)
///
/// Serializes to YAML with `Rate`, `Unit`, and optional `Burst` fields.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct RateLimit {
    pub rate: u32,
    pub unit: RateUnit,
    pub burst: Option<u32>,
}

/// Rate unit for limit matching (-m limit)
///
/// Serializes to YAML as `Second`, `Minute`, `Hour`, or `Day`.
/// Displays as `/second`, `/minute`, `/hour`, or `/day` for command generation via
/// `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum RateUnit {
    Second,
    Minute,
    Hour,
    Day,
}

impl std::fmt::Display for RateUnit {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RateUnit::Second => write!(f, "/second"),
            RateUnit::Minute => write!(f, "/minute"),
            RateUnit::Hour => write!(f, "/hour"),
            RateUnit::Day => write!(f, "/day"),
        }
    }
}

/// Connection tracking states (-m conntrack --ctstate)
///
/// Serializes to YAML as `New`, `Established`, `Related`, `Invalid`, or `Untracked`.
/// Displays as `NEW`, `ESTABLISHED`, `RELATED`, `INVALID`, or `UNTRACKED` for command
/// generation via `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum ConnectionState {
    New,
    Established,
    Related,
    Invalid,
    Untracked,
}

impl std::fmt::Display for ConnectionState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ConnectionState::New => write!(f, "NEW"),
            ConnectionState::Established => write!(f, "ESTABLISHED"),
            ConnectionState::Related => write!(f, "RELATED"),
            ConnectionState::Invalid => write!(f, "INVALID"),
            ConnectionState::Untracked => write!(f, "UNTRACKED"),
        }
    }
}

/// Connection tracking match specification (-m conntrack --ctstate)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct ConntrackMatch {
    pub states: Vec<ConnectionState>,
}

/// User/Group ownership matching (-m owner)
///
/// Serializes to YAML as `UidOwner("username")` or `GidOwner("groupname")`.
/// Displays as `--uid-owner username` or `--gid-owner groupname` for command generation via
/// `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum OwnerMatch {
    UidOwner(String),
    GidOwner(String),
}

impl std::fmt::Display for OwnerMatch {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OwnerMatch::UidOwner(uid) => write!(f, "--uid-owner {}", uid),
            OwnerMatch::GidOwner(gid) => write!(f, "--gid-owner {}", gid),
        }
    }
}

/// Match criteria for iptables rules
///
/// Contains all possible matching criteria for an iptables rule. All fields are optional
/// and use `Invert` wrapper for fields that support negation with `!` in iptables syntax.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct MatchCriteria {
    pub protocol: Protocol,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub source: Option<Invert<CidrBlock>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub destination: Option<Invert<CidrBlock>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub network_interface_in: Option<Invert<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub network_interface_out: Option<Invert<String>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub fragment: Option<bool>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit: Option<RateLimit>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub conntrack: Option<Invert<ConntrackMatch>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub owner: Option<Invert<OwnerMatch>>,

    #[serde(skip_serializing_if = "Option::is_none")]
    pub comment: Option<String>,
}

/// Rejection types for the REJECT target (--reject-with)
///
/// Serializes to YAML as `IcmpPortUnreachable`, `IcmpNetUnreachable`, `TcpReset`, or `EchoReply`.
/// Displays as `icmp-port-unreachable`, `icmp-net-unreachable`, `tcp-reset`, or `echo-reply`
/// for command generation via `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum RejectWith {
    IcmpPortUnreachable,
    IcmpNetUnreachable,
    TcpReset,
    EchoReply,
}

impl std::fmt::Display for RejectWith {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RejectWith::IcmpPortUnreachable => write!(f, "icmp-port-unreachable"),
            RejectWith::IcmpNetUnreachable => write!(f, "icmp-net-unreachable"),
            RejectWith::TcpReset => write!(f, "tcp-reset"),
            RejectWith::EchoReply => write!(f, "echo-reply"),
        }
    }
}

/// Target action for an iptables rule (-j, -g)
///
/// Serializes to YAML as `Accept`, `Drop`, `Reject`, `Log`, `Return`, `Jump`, `Goto`, or `Custom`.
/// The `Reject` and `Log` variants support additional options.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum IptablesTarget {
    Accept,
    Drop,
    #[serde(rename_all = "PascalCase")]
    Reject {
        #[serde(skip_serializing_if = "Option::is_none")]
        with: Option<RejectWith>,
    },
    #[serde(rename_all = "PascalCase")]
    Log {
        #[serde(skip_serializing_if = "Option::is_none")]
        prefix: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        level: Option<u8>,
    },
    Return,
    Jump(String),
    Goto(String),
    Custom(String),
}

/// Chains unique to the Raw table
///
/// Serializes to YAML as `Prerouting`, `Output`, or `Custom("name")`.
/// Displays as `PREROUTING`, `OUTPUT`, or custom name for command generation via
/// `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum RawChain {
    Prerouting,
    Output,
    Custom(String),
}

impl std::fmt::Display for RawChain {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            RawChain::Prerouting => write!(f, "PREROUTING"),
            RawChain::Output => write!(f, "OUTPUT"),
            RawChain::Custom(s) => write!(f, "{}", s),
        }
    }
}

/// Chains unique to the Filter table
///
/// Serializes to YAML as `Input`, `Forward`, `Output`, or `Custom("name")`.
/// Displays as `INPUT`, `FORWARD`, `OUTPUT`, or custom name for command generation via
/// `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum FilterChain {
    Input,
    Forward,
    Output,
    Custom(String),
}

impl std::fmt::Display for FilterChain {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FilterChain::Input => write!(f, "INPUT"),
            FilterChain::Forward => write!(f, "FORWARD"),
            FilterChain::Output => write!(f, "OUTPUT"),
            FilterChain::Custom(s) => write!(f, "{}", s),
        }
    }
}

/// Chains unique to the Nat table
///
/// Serializes to YAML as `Prerouting`, `Input`, `Output`, `Postrouting`, or `Custom("name")`.
/// Displays as `PREROUTING`, `INPUT`, `OUTPUT`, `POSTROUTING`, or custom name for command
/// generation via `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum NatChain {
    Prerouting,
    Input,
    Output,
    Postrouting,
    Custom(String),
}

impl std::fmt::Display for NatChain {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            NatChain::Prerouting => write!(f, "PREROUTING"),
            NatChain::Input => write!(f, "INPUT"),
            NatChain::Output => write!(f, "OUTPUT"),
            NatChain::Postrouting => write!(f, "POSTROUTING"),
            NatChain::Custom(s) => write!(f, "{}", s),
        }
    }
}

/// Chains available in Mangle table
///
/// Serializes to YAML as `Prerouting`, `Input`, `Forward`, `Output`, `Postrouting`, or
/// `Custom("name")`.
/// Displays as `PREROUTING`, `INPUT`, `FORWARD`, `OUTPUT`, `POSTROUTING`, or custom name
/// for command generation via `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum MangleChain {
    Prerouting,
    Input,
    Forward,
    Output,
    Postrouting,
    Custom(String),
}

impl std::fmt::Display for MangleChain {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MangleChain::Prerouting => write!(f, "PREROUTING"),
            MangleChain::Input => write!(f, "INPUT"),
            MangleChain::Forward => write!(f, "FORWARD"),
            MangleChain::Output => write!(f, "OUTPUT"),
            MangleChain::Postrouting => write!(f, "POSTROUTING"),
            MangleChain::Custom(s) => write!(f, "{}", s),
        }
    }
}

/// Chains available in Security table
///
/// Serializes to YAML as `Input`, `Forward`, `Output`, or `Custom("name")`.
/// Displays as `INPUT`, `FORWARD`, `OUTPUT`, or custom name for command generation via
/// `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum SecurityChain {
    Input,
    Forward,
    Output,
    Custom(String),
}

impl std::fmt::Display for SecurityChain {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SecurityChain::Input => write!(f, "INPUT"),
            SecurityChain::Forward => write!(f, "FORWARD"),
            SecurityChain::Output => write!(f, "OUTPUT"),
            SecurityChain::Custom(s) => write!(f, "{}", s),
        }
    }
}

/// An enum representing a table, bundling ONLY valid chains for that table.
///
/// Serializes to YAML with a `Table` tag and `Details` content using PascalCase for all fields.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "Table", content = "Details", rename_all = "PascalCase")]
pub enum IptablesRule {
    #[serde(rename_all = "PascalCase")]
    Raw {
        chain: RawChain,
        criteria: MatchCriteria,
        target: IptablesTarget,
    },
    #[serde(rename_all = "PascalCase")]
    Filter {
        chain: FilterChain,
        criteria: MatchCriteria,
        target: IptablesTarget,
    },
    #[serde(rename_all = "PascalCase")]
    Nat {
        chain: NatChain,
        criteria: MatchCriteria,
        target: IptablesTarget,
    },
    #[serde(rename_all = "PascalCase")]
    Mangle {
        chain: MangleChain,
        criteria: MatchCriteria,
        target: IptablesTarget,
    },
    #[serde(rename_all = "PascalCase")]
    Security {
        chain: SecurityChain,
        criteria: MatchCriteria,
        target: IptablesTarget,
    },
}

/// Desired state of an iptables rule
///
/// Serializes to YAML as `Present` or `Absent`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum RuleExpectedState {
    Present,
    Absent,
}

/// Wraps any match value to indicate whether it should be matched normally or inverted (!)
///
/// Serializes to YAML as `{ Value: ..., Inverted: true/false }` when inverted is true,
/// or just the inner value when inverted is false (due to `skip_serializing_if`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Invert<T> {
    pub value: T,
    #[serde(
        rename = "Inverted",
        default,
        skip_serializing_if = "std::ops::Not::not"
    )]
    pub inverted: bool,
}

impl<T> Invert<T> {
    pub fn new(value: T) -> Self {
        Self {
            value,
            inverted: false,
        }
    }

    pub fn inverted(value: T) -> Self {
        Self {
            value,
            inverted: true,
        }
    }
}

/// Represents a single port or a contiguous port range (e.g., "1024:65535")
///
/// Serializes to YAML as a single number or `{ Start: ..., End: ... }`.
/// Displays as `port` or `start:end` for command generation via `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase", untagged)]
pub enum PortRange {
    Single(u16),
    Range {
        #[serde(rename = "Start")]
        start: u16,
        #[serde(rename = "End")]
        end: u16,
    },
}

impl std::fmt::Display for PortRange {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PortRange::Single(port) => write!(f, "{}", port),
            PortRange::Range { start, end } => write!(f, "{}:{}", start, end),
        }
    }
}

/// Represents either a single port match or a multiport list
///
/// Serializes to YAML as a single number, a range object, or a list of port ranges.
/// Displays as a comma-separated list of ports/ranges for command generation via
/// `std::fmt::Display`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase", untagged)]
pub enum PortSpec {
    Single(u16),
    Range {
        #[serde(rename = "Start")]
        start: u16,
        #[serde(rename = "End")]
        end: u16,
    },
    List(Vec<PortRange>),
}

impl std::fmt::Display for PortSpec {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            PortSpec::Single(port) => write!(f, "{}", port),
            PortSpec::Range { start, end } => write!(f, "{}:{}", start, end),
            PortSpec::List(ports) => {
                let port_strs: Vec<String> = ports.iter().map(|p| p.to_string()).collect();
                write!(f, "{}", port_strs.join(","))
            }
        }
    }
}

/// Desired state for an iptables/ip6tables rule
///
/// Serializes to YAML with a `State` tag (`Present` or `Absent`) and uses PascalCase for all
/// fields. The `IpVersion` defaults to V4 and is omitted when V4.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "State", rename_all = "PascalCase")]
pub enum IptablesBlockExpectedState {
    #[serde(rename_all = "PascalCase")]
    Present {
        #[serde(default = "default_ip_family", skip_serializing_if = "is_v4_default")]
        ip_version: IpVersion,
        action: IptablesInsertionAction,
        #[serde(flatten)]
        rule: IptablesRule,
    },
    #[serde(rename_all = "PascalCase")]
    Absent {
        #[serde(default = "default_ip_family")]
        ip_version: IpVersion,
        #[serde(flatten)]
        rule: IptablesRule,
    },
}

// ── Block expected state ──────────────────────────────────────────────────────

/// Configuration for an iptables/ip6tables rule or chain
///
/// Use the new strongly-typed API to create firewall rules with various matching criteria
/// and actions.
///
/// All types in this module serialize to YAML using PascalCase naming convention for
/// user-friendliness, while implementing `Display` to output the correct iptables command
/// syntax (e.g., chain names in UPPERCASE, reject types in lowercase-with-hyphens).
///
/// # Examples
///
/// ## Rust API
///
/// ```no_run
/// use regent_sdk::state::attribute::network::iptables::{
///     IptablesBlockExpectedState, IpVersion, IptablesInsertionAction,
///     IptablesRule, FilterChain, Protocol, MatchCriteria, IptablesTarget
/// };
/// use regent_sdk::state::attribute::network::iptables::PortSpec;
/// use regent_sdk::{Attribute, ExpectedState, Privilege};
///
/// // Allow SSH on port 22 using convenience method (defaults to IPv4)
/// let ssh_rule = IptablesBlockExpectedState::allow_ssh(None);
///
/// // Or use the full API for more complex rules
impl IptablesBlockExpectedState {
    pub fn present(
        ip_version: IpVersion,
        action: IptablesInsertionAction,
        rule: IptablesRule,
    ) -> IptablesBlockExpectedState {
        IptablesBlockExpectedState::Present {
            ip_version,
            action,
            rule,
        }
    }

    pub fn absent(ip_version: IpVersion, rule: IptablesRule) -> IptablesBlockExpectedState {
        IptablesBlockExpectedState::Absent { ip_version, rule }
    }

    /// Allow incoming TCP traffic on a specific port in the INPUT chain.
    /// This is a convenience method for the common use case of opening a TCP port.
    ///
    /// # Arguments
    /// * `port` - The TCP port number to allow
    /// * `position` - Optional rule position for insertion (uses Append if None)
    pub fn allow_tcp_port(port: u16, position: Option<u32>) -> IptablesBlockExpectedState {
        Self::allow_tcp_port_with_ip(port, IpVersion::V4, position)
    }

    /// Allow incoming TCP traffic on a specific port in the INPUT chain with custom IP version.
    ///
    /// # Arguments
    /// * `port` - The TCP port number to allow
    /// * `ip_version` - IP version (V4 or V6)
    /// * `position` - Optional rule position for insertion (uses Append if None)
    pub fn allow_tcp_port_with_ip(
        port: u16,
        ip_version: IpVersion,
        position: Option<u32>,
    ) -> IptablesBlockExpectedState {
        let action = match position {
            Some(pos) => IptablesInsertionAction::Insert { position: pos },
            None => IptablesInsertionAction::Append,
        };
        IptablesBlockExpectedState::Present {
            ip_version,
            action,
            rule: IptablesRule::Filter {
                chain: FilterChain::Input,
                criteria: MatchCriteria {
                    protocol: Protocol::Tcp {
                        source_port: None,
                        dest_port: Some(PortSpec::Single(port)),
                        tcp_flags: None,
                    },
                    source: None,
                    destination: None,
                    network_interface_in: None,
                    network_interface_out: None,
                    fragment: None,
                    limit: None,
                    conntrack: None,
                    owner: None,
                    comment: None,
                },
                target: IptablesTarget::Accept,
            },
        }
    }

    /// Allow incoming UDP traffic on a specific port in the INPUT chain.
    /// This is a convenience method for the common use case of opening a UDP port.
    ///
    /// # Arguments
    /// * `port` - The UDP port number to allow
    /// * `position` - Optional rule position for insertion (uses Append if None)
    pub fn allow_udp_port(port: u16, position: Option<u32>) -> IptablesBlockExpectedState {
        Self::allow_udp_port_with_ip(port, IpVersion::V4, position)
    }

    /// Allow incoming UDP traffic on a specific port in the INPUT chain with custom IP version.
    ///
    /// # Arguments
    /// * `port` - The UDP port number to allow
    /// * `ip_version` - IP version (V4 or V6)
    /// * `position` - Optional rule position for insertion (uses Append if None)
    pub fn allow_udp_port_with_ip(
        port: u16,
        ip_version: IpVersion,
        position: Option<u32>,
    ) -> IptablesBlockExpectedState {
        let action = match position {
            Some(pos) => IptablesInsertionAction::Insert { position: pos },
            None => IptablesInsertionAction::Append,
        };
        IptablesBlockExpectedState::Present {
            ip_version,
            action,
            rule: IptablesRule::Filter {
                chain: FilterChain::Input,
                criteria: MatchCriteria {
                    protocol: Protocol::Udp {
                        source_port: None,
                        dest_port: Some(PortSpec::Single(port)),
                    },
                    source: None,
                    destination: None,
                    network_interface_in: None,
                    network_interface_out: None,
                    fragment: None,
                    limit: None,
                    conntrack: None,
                    owner: None,
                    comment: None,
                },
                target: IptablesTarget::Accept,
            },
        }
    }

    /// Allow incoming SSH traffic on port 22 in the INPUT chain.
    /// This is a convenience method for the common use case of enabling SSH access.
    ///
    /// # Arguments
    /// * `position` - Optional rule position for insertion (uses Append if None)
    pub fn allow_ssh(position: Option<u32>) -> IptablesBlockExpectedState {
        Self::allow_ssh_with_ip(IpVersion::V4, position)
    }

    /// Allow incoming SSH traffic on port 22 in the INPUT chain with custom IP version.
    ///
    /// # Arguments
    /// * `ip_version` - IP version (V4 or V6)
    /// * `position` - Optional rule position for insertion (uses Append if None)
    pub fn allow_ssh_with_ip(
        ip_version: IpVersion,
        position: Option<u32>,
    ) -> IptablesBlockExpectedState {
        Self::allow_tcp_port_with_ip(22, ip_version, position)
    }

    /// Drop all incoming traffic on the INPUT chain.
    /// This is a convenience method for creating a default deny policy.
    ///
    /// # Arguments
    /// * `position` - Optional rule position for insertion (uses Append if None)
    pub fn drop_all_incoming(position: Option<u32>) -> IptablesBlockExpectedState {
        Self::drop_all_incoming_with_ip(IpVersion::V4, position)
    }

    /// Drop all incoming traffic on the INPUT chain with custom IP version.
    ///
    /// # Arguments
    /// * `ip_version` - IP version (V4 or V6)
    /// * `position` - Optional rule position for insertion (uses Append if None)
    pub fn drop_all_incoming_with_ip(
        ip_version: IpVersion,
        position: Option<u32>,
    ) -> IptablesBlockExpectedState {
        let action = match position {
            Some(pos) => IptablesInsertionAction::Insert { position: pos },
            None => IptablesInsertionAction::Append,
        };
        IptablesBlockExpectedState::Present {
            ip_version,
            action,
            rule: IptablesRule::Filter {
                chain: FilterChain::Input,
                criteria: MatchCriteria {
                    protocol: Protocol::All,
                    source: None,
                    destination: None,
                    network_interface_in: None,
                    network_interface_out: None,
                    fragment: None,
                    limit: None,
                    conntrack: None,
                    owner: None,
                    comment: None,
                },
                target: IptablesTarget::Drop,
            },
        }
    }
}

impl Timeout for IptablesBlockExpectedState {
    fn default_timeout(&self) -> Duration {
        Duration::from_secs(5)
    }
}

// ── Check ─────────────────────────────────────────────────────────────────────

impl Check for IptablesBlockExpectedState {
    fn check(&self) -> Result<(), RegentError> {
        // Extract the rule which is present in both Present and Absent variants
        let rule = match self {
            IptablesBlockExpectedState::Present { rule, .. }
            | IptablesBlockExpectedState::Absent { rule, .. } => rule,
        };

        // Check that custom chain names are not empty
        match rule {
            IptablesRule::Raw {
                chain: RawChain::Custom(c),
                ..
            }
            | IptablesRule::Filter {
                chain: FilterChain::Custom(c),
                ..
            }
            | IptablesRule::Nat {
                chain: NatChain::Custom(c),
                ..
            }
            | IptablesRule::Mangle {
                chain: MangleChain::Custom(c),
                ..
            }
            | IptablesRule::Security {
                chain: SecurityChain::Custom(c),
                ..
            } => {
                if c.is_empty() {
                    return Err(RegentError::IncoherentExpectedState(
                        "custom chain name must not be empty.".to_string(),
                    ));
                }
            }
            _ => {}
        }

        // In the new structure, the target is always present in IptablesRule
        // so we don't need to check for at least one actionable

        // For Present variant, check that if action is Insert, it has a position
        if let IptablesBlockExpectedState::Present { action, .. } = self {
            if let IptablesInsertionAction::Insert { position: _ } = action {
                // Position is required in the Insert variant, so it's always valid
            }
        }

        Ok(())
    }

    fn check_host_compatibility(
        &self,
        host_properties: &HostProperties,
    ) -> Result<(), RegentError> {
        match host_properties.os_kind() {
            OsKind::Linux(_) => Ok(()),
            incompatible_os_kind => Err(RegentError::IncompatibleHost(format!(
                "Host is {:?} but iptables is only supported on Linux",
                incompatible_os_kind
            ))),
        }
    }
}

// ── AssessCompliance ──────────────────────────────────────────────────────────

/// Helper function to get the table argument from an IptablesRule
fn get_table_arg(rule: &IptablesRule) -> String {
    match rule {
        IptablesRule::Raw {
            chain: _,
            criteria: _,
            target: _,
        } => "-t raw",
        IptablesRule::Filter {
            chain: _,
            criteria: _,
            target: _,
        } => "-t filter",
        IptablesRule::Nat {
            chain: _,
            criteria: _,
            target: _,
        } => "-t nat",
        IptablesRule::Mangle {
            chain: _,
            criteria: _,
            target: _,
        } => "-t mangle",
        IptablesRule::Security {
            chain: _,
            criteria: _,
            target: _,
        } => "-t security",
    }
    .to_string()
}

/// Helper function to get the chain name from an IptablesRule
fn get_chain_name(rule: &IptablesRule) -> String {
    match rule {
        IptablesRule::Raw { chain, .. } => chain.to_string(),
        IptablesRule::Filter { chain, .. } => chain.to_string(),
        IptablesRule::Nat { chain, .. } => chain.to_string(),
        IptablesRule::Mangle { chain, .. } => chain.to_string(),
        IptablesRule::Security { chain, .. } => chain.to_string(),
    }
}

/// Helper function to convert IpVersion to a string for Display
fn ip_version_to_str(ip_version: &IpVersion) -> String {
    ip_version.to_string()
}

impl<Handler: HostHandler> AssessCompliance<Handler> for IptablesBlockExpectedState {
    async fn assess_compliance(
        &self,
        host_handler: &mut Handler,
        host_properties: &Option<HostProperties>,
        privilege: &Privilege,
        _optional_secret_provider: &Option<SecretProvidersPool>,
    ) -> Result<AttributeComplianceAssessment, RegentError> {
        // Early check: verify we're on a compatible host (Linux)
        if let Some(props) = host_properties {
            self.check_host_compatibility(props)?;
        }

        // Extract ip_version, action, and rule from the IptablesBlockExpectedState
        let (ip_version, expected_state, action, rule) = match self {
            IptablesBlockExpectedState::Present {
                ip_version,
                action,
                rule,
            } => (
                ip_version,
                RuleExpectedState::Present,
                Some(action.clone()),
                rule,
            ),
            IptablesBlockExpectedState::Absent { ip_version, rule } => {
                (ip_version, RuleExpectedState::Absent, None, rule)
            }
        };

        let binary = ip_version_to_str(&ip_version);
        let table_arg = get_table_arg(rule);
        let chain_name = get_chain_name(rule);

        let mut remediations: Vec<Remediation> = Vec::new();

        // Build the rule arguments from the MatchCriteria and IptablesTarget
        let rule_args = build_rule_args_from_new_structures(rule);

        // Check if the rule exists
        let check_rule_cmd = build_cmd(
            &binary,
            &table_arg,
            &format!("-C {} {}", chain_name, rule_args),
        );
        let check_rule_cmd = format!("{} 2>/dev/null", check_rule_cmd);
        let check_result = host_handler
            .run_command(&check_rule_cmd, &Privilege::None)
            .await
            .unwrap();

        match expected_state {
            RuleExpectedState::Present => {
                if check_result.return_code != 0 {
                    // Rule doesn't exist, need to add it
                    let insertion_action = action.unwrap_or(IptablesInsertionAction::Append);
                    match insertion_action {
                        IptablesInsertionAction::Append => {
                            remediations.push(Remediation::Iptables(IptablesApiCall::from(
                                IptablesModuleInternalApiCall::AppendRule {
                                    binary: binary.to_string(),
                                    table_arg: table_arg.clone(),
                                    chain: chain_name.clone(),
                                    rule_args: rule_args.clone(),
                                },
                                privilege.clone(),
                            )));
                        }
                        IptablesInsertionAction::Insert { position } => {
                            remediations.push(Remediation::Iptables(IptablesApiCall::from(
                                IptablesModuleInternalApiCall::InsertRule {
                                    binary: binary.to_string(),
                                    table_arg: table_arg.clone(),
                                    chain: chain_name.clone(),
                                    rule_num: Some(position),
                                    rule_args: rule_args.clone(),
                                },
                                privilege.clone(),
                            )));
                        }
                    }
                }
            }
            RuleExpectedState::Absent => {
                if check_result.return_code == 0 {
                    // Rule exists but should be absent, need to delete it
                    remediations.push(Remediation::Iptables(IptablesApiCall::from(
                        IptablesModuleInternalApiCall::DeleteRule {
                            binary: binary.to_string(),
                            table_arg: table_arg.clone(),
                            chain: chain_name.clone(),
                            rule_args: rule_args.clone(),
                        },
                        privilege.clone(),
                    )));
                }
            }
        }

        if remediations.is_empty() {
            Ok(AttributeComplianceAssessment::Compliant)
        } else {
            Ok(AttributeComplianceAssessment::NonCompliant(
                RemediationsList::from(remediations)?,
            ))
        }
    }
}

// ── Internal API call enum ────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub enum IptablesModuleInternalApiCall {
    CreateChain {
        binary: String,
        table_arg: String,
        chain: String,
    },
    FlushAndDeleteChain {
        binary: String,
        table_arg: String,
        chain: String,
    },
    SetPolicy {
        binary: String,
        table_arg: String,
        chain: String,
        policy: String,
    },
    AppendRule {
        binary: String,
        table_arg: String,
        chain: String,
        rule_args: String,
    },
    InsertRule {
        binary: String,
        table_arg: String,
        chain: String,
        rule_num: Option<u32>,
        rule_args: String,
    },
    DeleteRule {
        binary: String,
        table_arg: String,
        chain: String,
        rule_args: String,
    },
}

impl std::fmt::Display for IptablesModuleInternalApiCall {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            IptablesModuleInternalApiCall::CreateChain {
                binary,
                table_arg,
                chain,
            } => {
                write!(f, "{} create chain {}", binary, chain)?;
                if !table_arg.is_empty() {
                    write!(f, " ({})", table_arg)?;
                }
                Ok(())
            }
            IptablesModuleInternalApiCall::FlushAndDeleteChain {
                binary,
                table_arg,
                chain,
            } => {
                write!(f, "{} flush and delete chain {}", binary, chain)?;
                if !table_arg.is_empty() {
                    write!(f, " ({})", table_arg)?;
                }
                Ok(())
            }
            IptablesModuleInternalApiCall::SetPolicy {
                binary,
                table_arg,
                chain,
                policy,
            } => {
                write!(f, "{} set policy {} on chain {}", binary, policy, chain)?;
                if !table_arg.is_empty() {
                    write!(f, " ({})", table_arg)?;
                }
                Ok(())
            }
            IptablesModuleInternalApiCall::AppendRule {
                binary,
                table_arg,
                chain,
                rule_args,
            } => {
                write!(f, "{} append rule to {} : {}", binary, chain, rule_args)?;
                if !table_arg.is_empty() {
                    write!(f, " ({})", table_arg)?;
                }
                Ok(())
            }
            IptablesModuleInternalApiCall::InsertRule {
                binary,
                table_arg,
                chain,
                rule_num,
                rule_args,
            } => {
                match rule_num {
                    Some(n) => write!(
                        f,
                        "{} insert rule at {} in {} : {}",
                        binary, n, chain, rule_args
                    )?,
                    None => write!(f, "{} insert rule in {} : {}", binary, chain, rule_args)?,
                }
                if !table_arg.is_empty() {
                    write!(f, " ({})", table_arg)?;
                }
                Ok(())
            }
            IptablesModuleInternalApiCall::DeleteRule {
                binary,
                table_arg,
                chain,
                rule_args,
            } => {
                write!(f, "{} delete rule from {} : {}", binary, chain, rule_args)?;
                if !table_arg.is_empty() {
                    write!(f, " ({})", table_arg)?;
                }
                Ok(())
            }
        }
    }
}

// ── ApiCall struct ────────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct IptablesApiCall {
    pub api_call: IptablesModuleInternalApiCall,
    privilege: Privilege,
}

impl IptablesApiCall {
    pub fn display(&self) -> String {
        format!("{}", self.api_call)
    }

    fn from(api_call: IptablesModuleInternalApiCall, privilege: Privilege) -> IptablesApiCall {
        IptablesApiCall {
            api_call,
            privilege,
        }
    }
}

impl Check for IptablesApiCall {
    fn check(&self) -> Result<(), RegentError> {
        Ok(())
    }

    fn check_host_compatibility(
        &self,
        host_properties: &HostProperties,
    ) -> Result<(), RegentError> {
        match host_properties.os_kind() {
            OsKind::Linux(_) => Ok(()),
            incompatible_os_kind => Err(RegentError::IncompatibleHost(format!(
                "Host is {:?} but iptables is only supported on Linux",
                incompatible_os_kind
            ))),
        }
    }
}

impl<Handler: HostHandler> ReachCompliance<Handler> for IptablesApiCall {
    async fn call(
        &self,
        host_handler: &mut Handler,
        host_properties: &Option<HostProperties>,
        _optional_secret_provider: &Option<SecretProvidersPool>,
    ) -> Result<InternalApiCallOutcome, RegentError> {
        // Early check: verify we're on a compatible host (Linux)
        if let Some(props) = host_properties {
            self.check_host_compatibility(props)?;
        }

        let cmd = match &self.api_call {
            IptablesModuleInternalApiCall::CreateChain {
                binary,
                table_arg,
                chain,
            } => build_cmd(binary, table_arg, &format!("-N {}", chain)),
            IptablesModuleInternalApiCall::FlushAndDeleteChain {
                binary,
                table_arg,
                chain,
            } => {
                let flush = build_cmd(binary, table_arg, &format!("-F {}", chain));
                let delete = build_cmd(binary, table_arg, &format!("-X {}", chain));
                format!("{} && {}", flush, delete)
            }
            IptablesModuleInternalApiCall::SetPolicy {
                binary,
                table_arg,
                chain,
                policy,
            } => build_cmd(binary, table_arg, &format!("-P {} {}", chain, policy)),
            IptablesModuleInternalApiCall::AppendRule {
                binary,
                table_arg,
                chain,
                rule_args,
            } => build_cmd(binary, table_arg, &format!("-A {} {}", chain, rule_args)),
            IptablesModuleInternalApiCall::InsertRule {
                binary,
                table_arg,
                chain,
                rule_num,
                rule_args,
            } => match rule_num {
                Some(n) => build_cmd(
                    binary,
                    table_arg,
                    &format!("-I {} {} {}", chain, n, rule_args),
                ),
                None => build_cmd(binary, table_arg, &format!("-I {} {}", chain, rule_args)),
            },
            IptablesModuleInternalApiCall::DeleteRule {
                binary,
                table_arg,
                chain,
                rule_args,
            } => build_cmd(binary, table_arg, &format!("-D {} {}", chain, rule_args)),
        };

        let result = host_handler
            .run_command(&cmd, &self.privilege)
            .await
            .unwrap();

        if result.return_code == 0 {
            // Post-operation verification: verify the rule is in the correct position
            let verification_result =
                verify_rule_position(host_handler, &self.api_call, &self.privilege).await;

            if verification_result {
                Ok(InternalApiCallOutcome::Success(None))
            } else {
                Ok(InternalApiCallOutcome::Failure(
                    "Command succeeded but post-verification failed: rule position does not match expected".to_string(),
                ))
            }
        } else {
            Ok(InternalApiCallOutcome::Failure(format!(
                "RC: {}, STDOUT: {}, STDERR: {}",
                result.return_code, result.stdout, result.stderr
            )))
        }
    }
}

/// Verify that a rule is in the correct position after insertion.
/// This implements post-application verification for idempotency.
async fn verify_rule_position<Handler: HostHandler>(
    host_handler: &mut Handler,
    api_call: &IptablesModuleInternalApiCall,
    privilege: &Privilege,
) -> bool {
    match api_call {
        IptablesModuleInternalApiCall::AppendRule {
            binary,
            table_arg,
            chain,
            rule_args,
        } => {
            // For append, verify the rule exists and is at the end
            let check_cmd = build_cmd(binary, table_arg, &format!("-C {} {}", chain, rule_args));
            let check_result = host_handler
                .run_command(&check_cmd, privilege)
                .await
                .unwrap();
            check_result.return_code == 0
        }
        IptablesModuleInternalApiCall::InsertRule {
            binary,
            table_arg,
            chain,
            rule_num,
            rule_args,
        } => {
            // For insert, verify the rule exists and is at the expected position
            let check_cmd = build_cmd(binary, table_arg, &format!("-C {} {}", chain, rule_args));
            let check_result = host_handler
                .run_command(&check_cmd, privilege)
                .await
                .unwrap();

            if check_result.return_code != 0 {
                return false; // Rule doesn't exist
            }

            // If a specific position was requested, verify it
            if let Some(expected_position) = rule_num {
                // List all rules in the chain and check position
                let list_cmd = build_cmd(
                    binary,
                    table_arg,
                    &format!("-L {} --line-numbers -n", chain),
                );
                let list_result = host_handler
                    .run_command(&list_cmd, privilege)
                    .await
                    .unwrap();

                if list_result.return_code == 0 {
                    // Parse the output to find the rule position
                    for line in list_result.stdout.lines() {
                        if line.contains(rule_args.as_str()) {
                            // Extract the line number
                            let parts: Vec<&str> = line.split_whitespace().collect();
                            if parts.len() >= 1 {
                                if let Ok(actual_position) = parts[0].parse::<u32>() {
                                    return actual_position == *expected_position;
                                }
                            }
                            break; // Found the rule, but couldn't parse position
                        }
                    }
                    return false; // Rule not found or position mismatch
                }
                false
            } else {
                // No specific position requested, just verify rule exists
                true
            }
        }
        IptablesModuleInternalApiCall::DeleteRule { .. } => {
            // For delete, the rule should no longer exist
            // This is handled by the command return code, so we just return true
            true
        }
        IptablesModuleInternalApiCall::CreateChain { .. } => {
            // For chain creation, we can verify the chain exists
            // This is handled by the command return code, so we just return true
            true
        }
        IptablesModuleInternalApiCall::FlushAndDeleteChain { .. } => {
            // For chain deletion, we can verify the chain no longer exists
            // This is handled by the command return code, so we just return true
            true
        }
        IptablesModuleInternalApiCall::SetPolicy { .. } => {
            // For policy changes, verification would require listing and parsing the chain
            // This is complex and the command return code is usually sufficient
            true
        }
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

/// Builds a full iptables command string from a binary name, an optional table argument,
/// and the command flags/arguments. When `table_arg` is empty the space before the flags
/// is omitted so the resulting command is clean.
fn build_cmd(binary: &str, table_arg: &str, flags: &str) -> String {
    if table_arg.is_empty() {
        format!("{} {}", binary, flags)
    } else {
        format!("{} {} {}", binary, table_arg, flags)
    }
}

/// Builds the rule-specification string from the fields of an `IptablesBlockExpectedState`.
/// The order of arguments follows the conventional iptables ordering: match criteria first,
/// then the target (jump/goto) with its options at the end.
/// Helper function to convert Protocol to iptables protocol string
fn protocol_to_str(proto: &Protocol) -> String {
    proto.to_string()
}

/// Helper function to convert PortSpec to iptables port string
fn port_spec_to_str(port: &PortSpec) -> String {
    port.to_string()
}

/// Helper function to convert TcpFlag to iptables flag string
fn tcp_flag_to_str(flag: &TcpFlag) -> &'static str {
    match flag {
        TcpFlag::Syn => "SYN",
        TcpFlag::Ack => "ACK",
        TcpFlag::Fin => "FIN",
        TcpFlag::Rst => "RST",
        TcpFlag::Psh => "PSH",
        TcpFlag::Urg => "URG",
        TcpFlag::FinRst => "FIN,RST",
        TcpFlag::All => "ALL",
        TcpFlag::None => "NONE",
    }
}

/// Helper function to convert RateUnit to iptables rate unit string
fn rate_unit_to_str(unit: &RateUnit) -> &'static str {
    match unit {
        RateUnit::Second => "/second",
        RateUnit::Minute => "/minute",
        RateUnit::Hour => "/hour",
        RateUnit::Day => "/day",
    }
}

/// Helper function to convert ConnectionState to iptables conntrack state string
fn connection_state_to_str(state: &ConnectionState) -> &'static str {
    match state {
        ConnectionState::New => "NEW",
        ConnectionState::Established => "ESTABLISHED",
        ConnectionState::Related => "RELATED",
        ConnectionState::Invalid => "INVALID",
        ConnectionState::Untracked => "UNTRACKED",
    }
}

/// Helper function to convert OwnerMatch to iptables owner match string
fn owner_match_to_str(owner: &OwnerMatch) -> String {
    match owner {
        OwnerMatch::UidOwner(uid) => format!("--uid-owner {}", uid),
        OwnerMatch::GidOwner(gid) => format!("--gid-owner {}", gid),
    }
}

/// Builds the rule-specification string from the new strongly-typed structures.
fn build_rule_args_from_new_structures(rule: &IptablesRule) -> String {
    let mut parts: Vec<String> = Vec::new();

    // Extract criteria and target from the rule
    let (criteria, target) = match rule {
        IptablesRule::Raw {
            criteria, target, ..
        }
        | IptablesRule::Filter {
            criteria, target, ..
        }
        | IptablesRule::Nat {
            criteria, target, ..
        }
        | IptablesRule::Mangle {
            criteria, target, ..
        }
        | IptablesRule::Security {
            criteria, target, ..
        } => (criteria, target),
    };

    // Protocol
    parts.push(format!("-p {}", protocol_to_str(&criteria.protocol)));

    // Source (with inversion support)
    if let Some(ref src) = criteria.source {
        let prefix = if src.inverted { "! -s " } else { "-s " };
        parts.push(format!("{} {}", prefix, src.value.to_string()));
    }

    // Destination (with inversion support)
    if let Some(ref dst) = criteria.destination {
        let prefix = if dst.inverted { "! -d " } else { "-d " };
        parts.push(format!("{} {}", prefix, dst.value.to_string()));
    }

    // Input interface (with inversion support)
    if let Some(ref iface) = criteria.network_interface_in {
        let prefix = if iface.inverted { "! -i " } else { "-i " };
        parts.push(format!("{} {}", prefix, iface.value));
    }

    // Output interface (with inversion support)
    if let Some(ref iface) = criteria.network_interface_out {
        let prefix = if iface.inverted { "! -o " } else { "-o " };
        parts.push(format!("{} {}", prefix, iface.value));
    }

    // Source port
    if let Some(ref sport) = get_source_port(&criteria.protocol) {
        parts.push(format!("--sport {}", port_spec_to_str(sport)));
    }

    // Destination port
    if let Some(ref dport) = get_dest_port(&criteria.protocol) {
        parts.push(format!("--dport {}", port_spec_to_str(dport)));
    }

    // TCP flags
    if let Some(ref tcp_flags) = get_tcp_flags(&criteria.protocol) {
        let mask_strs: Vec<&str> = tcp_flags.mask.iter().map(|f| tcp_flag_to_str(f)).collect();
        let comp_strs: Vec<&str> = tcp_flags.comp.iter().map(|f| tcp_flag_to_str(f)).collect();
        parts.push(format!(
            "-m tcp --tcp-flags {} {}",
            mask_strs.join(","),
            comp_strs.join(",")
        ));
    }

    // Connection tracking state (with inversion support)
    if let Some(ref conntrack) = criteria.conntrack {
        let prefix = if conntrack.inverted {
            "! -m conntrack --ctstate "
        } else {
            "-m conntrack --ctstate "
        };
        let states: Vec<&str> = conntrack
            .value
            .states
            .iter()
            .map(|s| connection_state_to_str(s))
            .collect();
        parts.push(format!("{} {}", prefix, states.join(",")));
    }

    // Rate limiting
    if let Some(ref limit) = criteria.limit {
        let rate_str = format!("{} {}", limit.rate, rate_unit_to_str(&limit.unit));
        let mut limit_part = format!("-m limit --limit {}", rate_str);
        if let Some(ref burst) = limit.burst {
            limit_part.push_str(&format!(" --limit-burst {}", burst));
        }
        parts.push(limit_part);
    }

    // Fragment (with inversion support)
    if let Some(ref fragment) = criteria.fragment {
        if *fragment {
            parts.push("-f".to_string());
        }
        // Note: For inverted fragment (! -f), we would need to track it differently
        // For now, we assume fragment is not inverted in the new structure
    }

    // ICMP type and code
    if let Some(ref icmp_type) = get_icmp_type(&criteria.protocol) {
        parts.push(format!("--icmp-type {}", icmp_type));
    }
    if let Some(ref icmp_code) = get_icmp_code(&criteria.protocol) {
        parts.push(format!("--icmp-code {}", icmp_code));
    }

    // Owner match (with inversion support)
    if let Some(ref owner) = criteria.owner {
        let prefix = if owner.inverted {
            "! -m owner "
        } else {
            "-m owner "
        };
        parts.push(format!("{} {}", prefix, owner_match_to_str(&owner.value)));
    }

    // Comment
    if let Some(ref comment) = criteria.comment {
        parts.push(format!("-m comment --comment '{}'", comment));
    }

    // Target
    parts.push(target_to_str(target));

    parts.join(" ")
}

/// Helper functions to extract protocol-specific fields
fn get_source_port(proto: &Protocol) -> Option<&PortSpec> {
    match proto {
        Protocol::Tcp {
            source_port: Some(p),
            ..
        }
        | Protocol::Udp {
            source_port: Some(p),
            ..
        } => Some(p),
        _ => None,
    }
}

fn get_dest_port(proto: &Protocol) -> Option<&PortSpec> {
    match proto {
        Protocol::Tcp {
            dest_port: Some(p), ..
        }
        | Protocol::Udp {
            dest_port: Some(p), ..
        } => Some(p),
        _ => None,
    }
}

fn get_tcp_flags(proto: &Protocol) -> Option<&TcpFlagsMatch> {
    match proto {
        Protocol::Tcp {
            tcp_flags: Some(f), ..
        } => Some(f),
        _ => None,
    }
}

fn get_icmp_type(proto: &Protocol) -> Option<u8> {
    match proto {
        Protocol::Icmp {
            icmp_type: Some(t), ..
        } => Some(*t),
        _ => None,
    }
}

fn get_icmp_code(proto: &Protocol) -> Option<u8> {
    match proto {
        Protocol::Icmp {
            icmp_code: Some(c), ..
        } => Some(*c),
        _ => None,
    }
}

/// Helper function to convert IptablesTarget to iptables target string
fn target_to_str(target: &IptablesTarget) -> String {
    match target {
        IptablesTarget::Accept => "-j ACCEPT".to_string(),
        IptablesTarget::Drop => "-j DROP".to_string(),
        IptablesTarget::Reject {
            with: Some(reject_with),
        } => {
            format!("-j REJECT --reject-with {}", reject_with)
        }
        IptablesTarget::Reject { with: None } => "-j REJECT".to_string(),
        IptablesTarget::Log {
            prefix: Some(p),
            level: Some(l),
        } => {
            format!("-j LOG --log-prefix '{}' --log-level {}", p, l)
        }
        IptablesTarget::Log {
            prefix: Some(p),
            level: None,
        } => {
            format!("-j LOG --log-prefix '{}'", p)
        }
        IptablesTarget::Log {
            prefix: None,
            level: Some(l),
        } => {
            format!("-j LOG --log-level {}", l)
        }
        IptablesTarget::Log {
            prefix: None,
            level: None,
        } => "-j LOG".to_string(),
        IptablesTarget::Return => "-j RETURN".to_string(),
        IptablesTarget::Jump(target) => format!("-j {}", target),
        IptablesTarget::Goto(target) => format!("-g {}", target),
        IptablesTarget::Custom(target) => format!("-j {}", target),
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

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

    #[test]
    fn test_deserialize_filter_tcp_present_append() {
        let yaml = r#"
IpVersion: V4
State: Present
Action: Append
Table: Filter
Details:
    Target: Accept
    Chain: Input
    Criteria:
        Protocol:
            Tcp:
                SourcePort: 80
                DestPort: 443
                TcpFlags:
                    Mask: [Syn, Ack]
                    Comp: [Syn]
        Source:
            Value: "192.168.1.0/24"
            Inverted: false
        Comment: "Allow HTTPS web traffic"
    "#;

        let state: IptablesBlockExpectedState = yaml_serde::from_str(yaml).unwrap();

        match state {
            IptablesBlockExpectedState::Present {
                ip_version,
                action,
                rule,
            } => {
                assert_eq!(ip_version, IpVersion::V4);
                assert_eq!(action, IptablesInsertionAction::Append);
                match rule {
                    IptablesRule::Filter {
                        chain,
                        criteria,
                        target,
                    } => {
                        assert_eq!(chain, FilterChain::Input);
                        assert_eq!(target, IptablesTarget::Accept);
                        assert!(criteria.comment.is_some());
                    }
                    _ => panic!("Expected Filter rule"),
                }
            }
            _ => panic!("Expected Present state"),
        }
    }

    #[test]
    fn test_deserialize_nat_udp_absent() {
        let yaml = r#"
State: Absent
IpVersion: V6
Table: Nat
Details:
    Chain: Postrouting
    Criteria:
        Protocol:
            Udp:
                SourcePort: 53
                DestPort:
                    Start: 1024
                    End: 65535
        Destination:
            Value: "2001:db8::/32"
            Inverted: true
    Target: 
        Reject:
            With: IcmpPortUnreachable
        "#;

        let state: IptablesBlockExpectedState = yaml_serde::from_str(yaml).unwrap();

        match state {
            IptablesBlockExpectedState::Absent { ip_version, rule } => {
                assert_eq!(ip_version, IpVersion::V6);
                match rule {
                    IptablesRule::Nat {
                        chain,
                        criteria,
                        target,
                    } => {
                        assert_eq!(chain, NatChain::Postrouting);
                        assert_eq!(
                            target,
                            IptablesTarget::Reject {
                                with: Some(RejectWith::IcmpPortUnreachable)
                            }
                        );
                        if let Protocol::Udp { dest_port, .. } = criteria.protocol {
                            assert_eq!(
                                dest_port,
                                Some(PortSpec::Range {
                                    start: 1024,
                                    end: 65535
                                })
                            );
                        } else {
                            panic!("Expected UDP protocol");
                        }
                    }
                    _ => panic!("Expected Nat rule"),
                }
            }
            _ => panic!("Expected Absent state"),
        }
    }

    #[test]
    fn test_deserialize_mangle_conntrack_owner() {
        let yaml = r#"
State: Present
Action: Append
Table: Mangle
Details:
    Chain: Output
    Criteria:
        Protocol: All
        Conntrack:
            Value:
                States: [New, Established]
            Inverted: false
        Owner:
            Value:
                UidOwner: "www-data"
            Inverted: true
    Target:
        Log:
            Prefix: "LOG_MARK: "
            Level: 4
        "#;

        let state: IptablesBlockExpectedState = yaml_serde::from_str(yaml).unwrap();

        match state {
            IptablesBlockExpectedState::Present { rule, .. } => match rule {
                IptablesRule::Mangle { chain, target, .. } => {
                    assert_eq!(chain, MangleChain::Output);
                    assert_eq!(
                        target,
                        IptablesTarget::Log {
                            prefix: Some("LOG_MARK: ".to_string()),
                            level: Some(4),
                        }
                    );
                }
                _ => panic!("Expected Mangle rule"),
            },
            _ => panic!("Expected Present state"),
        }
    }

    #[test]
    fn test_yaml_vs_rust_api_equivalence_ssh() {
        // YAML representation
        let yaml = r#"
State: Present
IpVersion: V4
Action: Append
Table: Filter
Details:
    Chain: Input
    Criteria:
        Protocol:
            Tcp:
                DestPort: 22
    Target: Accept
"#;
        let yaml_state: IptablesBlockExpectedState = yaml_serde::from_str(yaml).unwrap();

        // Rust API representation
        let rust_state = IptablesBlockExpectedState::allow_ssh(None);

        assert_eq!(yaml_state, rust_state);
    }

    #[test]
    fn test_yaml_vs_rust_api_equivalence_http() {
        // YAML representation
        let yaml = r#"
State: Present
IpVersion: V4
Action: Append
Table: Filter
Details:
    Chain: Input
    Criteria:
        Protocol:
            Tcp:
                DestPort: 80
    Target: Accept
"#;
        let yaml_state: IptablesBlockExpectedState = yaml_serde::from_str(yaml).unwrap();

        // Rust API representation
        let rust_state = IptablesBlockExpectedState::allow_tcp_port(80, None);

        assert_eq!(yaml_state, rust_state);
    }

    #[test]
    fn test_yaml_vs_rust_api_equivalence_drop_all() {
        // YAML representation
        let yaml = r#"
State: Present
IpVersion: V4
Action: Append
Table: Filter
Details:
    Chain: Input
    Criteria:
        Protocol: All
    Target: Drop
"#;
        let yaml_state: IptablesBlockExpectedState = yaml_serde::from_str(yaml).unwrap();

        // Rust API representation
        let rust_state = IptablesBlockExpectedState::drop_all_incoming(None);

        assert_eq!(yaml_state, rust_state);
    }

    #[test]
    fn test_yaml_vs_rust_api_equivalence_udp() {
        // YAML representation
        let yaml = r#"
State: Present
IpVersion: V4
Action: Append
Table: Filter
Details:
    Chain: Input
    Criteria:
        Protocol:
            Udp:
                DestPort: 53
    Target: Accept
"#;
        let yaml_state: IptablesBlockExpectedState = yaml_serde::from_str(yaml).unwrap();

        // Rust API representation
        let rust_state = IptablesBlockExpectedState::allow_udp_port(53, None);

        assert_eq!(yaml_state, rust_state);
    }

    #[test]
    fn test_yaml_vs_rust_api_equivalence_complex_rule() {
        // YAML representation of a more complex rule
        let yaml = r#"
State: Present
IpVersion: V4
Action:
    Insert:
        Position: 1
Table: Filter
Details:
    Chain: Input
    Criteria:
        Protocol:
            Tcp:
                DestPort: 443
        Source:
            Value: "192.168.1.0/24"
            Inverted: false
    Target: Accept
"#;
        let yaml_state: IptablesBlockExpectedState = yaml_serde::from_str(yaml).unwrap();

        // Rust API representation
        let rust_state = IptablesBlockExpectedState::present(
            IpVersion::V4,
            IptablesInsertionAction::Insert { position: 1 },
            IptablesRule::Filter {
                chain: FilterChain::Input,
                criteria: MatchCriteria {
                    protocol: Protocol::Tcp {
                        source_port: None,
                        dest_port: Some(PortSpec::Single(443)),
                        tcp_flags: None,
                    },
                    source: Some(Invert::new(CidrBlock::parse("192.168.1.0/24").unwrap())),
                    destination: None,
                    network_interface_in: None,
                    network_interface_out: None,
                    fragment: None,
                    limit: None,
                    conntrack: None,
                    owner: None,
                    comment: None,
                },
                target: IptablesTarget::Accept,
            },
        );

        assert_eq!(yaml_state, rust_state);
    }
}