harborshield 0.1.0

A Rust port of Whalewall, to automate management of firewall rules for Docker containers
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
mod common;
pub mod docker;
pub mod error;
pub mod transaction;

use crate::{
    Error, Result,
    docker::config::{Config, RuleContext, ToNftablesRule},
    nftables::{
        docker::{
            check_docker_chains, check_harborshield_chain_exists, check_jump_rules_exist,
            create_harborshield_chain, create_jump_rules,
        },
        transaction::NftablesTransaction,
    },
};
use bon::{Builder, builder};
use common::helpers;
use nftables::{
    batch::Batch,
    expr::{Expression, Meta, MetaKey, NamedExpression, Payload, PayloadField, SetItem, Verdict},
    helper::NftablesError,
    schema::{Chain, FlushObject, NfCmd, NfListObject, Rule},
    stmt::{Counter, JumpTarget, Log, LogLevel, Match, Operator, Queue, Statement, VerdictMap},
    types::NfFamily,
};
use std::borrow::Cow;
use std::sync::Arc;
use tokio::sync::Mutex;
use tracing::{debug, info, warn};

// Constants for Docker filter table integration
pub const FILTER_TABLE: &str = "filter";
pub const DOCKER_USER_CHAIN: &str = "DOCKER-USER";
pub const INPUT_CHAIN: &str = "INPUT";
pub const OUTPUT_CHAIN: &str = "OUTPUT";
pub const HARBORSHIELD_CHAIN: &str = "harborshield";

#[derive(Builder)]
/// Nftables client that integrates with Docker's filter table
pub struct NftablesClient {
    #[builder(default = Arc::new(Mutex::new(Batch::new())))]
    batch: Arc<Mutex<Batch<'static>>>,
    #[builder(default = NfFamily::IP)]
    pub family: NfFamily,
}

impl NftablesClient {
    /// Clear the harborshield chain (flush rules)
    pub async fn clear_table(&mut self) -> Result<()> {
        info!("Clearing harborshield chain rules");

        // First, clear all Harborshield container chains
        self.clear_all_container_chains()
            .await
            .map_err(|e| Error::Nftables {
                message: format!("Failed to clear container chains: {}", e),
                command: None,
                exit_code: None,
                stderr: None,
            })?;

        let mut batch = self.batch.lock().await;

        // Flush the harborshield chain by deleting and recreating it
        batch.delete(NfListObject::Chain(Chain {
            family: self.family,
            table: Cow::Borrowed(FILTER_TABLE),
            name: Cow::Borrowed(HARBORSHIELD_CHAIN),
            newname: None,
            handle: None,
            _type: None,
            hook: None,
            prio: None,
            dev: None,
            policy: None,
        }));

        // Recreate empty chain
        batch.add(NfListObject::Chain(Chain {
            family: self.family,
            table: Cow::Borrowed(FILTER_TABLE),
            name: Cow::Borrowed(HARBORSHIELD_CHAIN),
            newname: None,
            handle: None,
            _type: None,
            hook: None,
            prio: None,
            dev: None,
            policy: None,
        }));

        drop(batch);
        self.apply().await.map_err(|e| Error::Nftables {
            message: format!("Failed to apply table clear: {}", e),
            command: Some("nft flush chain".to_string()),
            exit_code: None,
            stderr: None,
        })?;
        Ok(())
    }

    /// Clear all Harborshield container chains from the filter table
    async fn clear_all_container_chains(&self) -> Result<()> {
        debug!("Clearing all Harborshield container chains");

        // Get a list of all chains in the filter table
        let list_output = std::process::Command::new("nft")
            .args(&["list", "table", "ip", FILTER_TABLE, "-j"])
            .output()
            .map_err(|e| Error::Nftables {
                message: format!("Failed to list filter table: {}", e),
                command: Some("nft list table ip filter -j".to_string()),
                exit_code: None,
                stderr: Some(e.to_string()),
            })?;

        if !list_output.status.success() {
            debug!(
                "Failed to list filter table: {}",
                String::from_utf8_lossy(&list_output.stderr)
            );
            return Ok(()); // Don't fail if we can't list
        }

        let output_str = String::from_utf8_lossy(&list_output.stdout);

        // Parse JSON to find all chains starting with "hs-"
        if let Some(json) = serde_json::from_str::<serde_json::Value>(&output_str).ok() {
            json.get("nftables")
                .and_then(|n| n.as_array())
                .into_iter()
                .flatten()
                .filter_map(|item| item.get("chain"))
                .filter_map(|chain| chain.get("name").and_then(|n| n.as_str()))
                .filter(|name| name.starts_with("hs-"))
                .for_each(|name| {
                    // Flush and delete the chain
                    let _ = std::process::Command::new("nft")
                        .args(&["flush", "chain", "ip", FILTER_TABLE, name])
                        .output();

                    let _ = std::process::Command::new("nft")
                        .args(&["delete", "chain", "ip", FILTER_TABLE, name])
                        .output();

                    debug!("Deleted chain {}", name);
                });
        }

        Ok(())
    }

    /// Initialize base rules in Docker's filter table
    pub async fn init_base_chains(&mut self) -> Result<()> {
        // Check what chains exist in the filter table
        let (has_filter, has_docker_user, has_input, has_output) =
            check_docker_chains().await.map_err(|e| Error::Nftables {
                message: format!("Failed to check Docker chains: {}", e),
                command: Some("nft list tables".to_string()),
                exit_code: None,
                stderr: None,
            })?;

        if !has_filter {
            return Err(Error::Config {
                message: "Docker filter table not found".to_string(),
                location: "init_base_chains".to_string(),
                suggestion: Some(
                    "Ensure Docker is running and using nftables. You may need to:\n\
                    1. Start Docker service: sudo systemctl start docker\n\
                    2. Check if Docker is using nftables: sudo iptables -V\n\
                    3. If using iptables-legacy, switch to nftables:\n\
                       - sudo update-alternatives --set iptables /usr/sbin/iptables-nft\n\
                       - sudo update-alternatives --set ip6tables /usr/sbin/ip6tables-nft\n\
                    4. Restart Docker after switching: sudo systemctl restart docker"
                        .to_string(),
                ),
            });
        }

        if !has_docker_user {
            warn!(
                "DOCKER-USER chain not found. Harborshield will create jump rules from INPUT/OUTPUT chains only. \
                Docker's built-in firewall rules may take precedence over Harborshield rules."
            );
        }

        let mut batch = self.batch.lock().await;

        // Check if harborshield chain already exists
        let harborshield_exists =
            check_harborshield_chain_exists()
                .await
                .map_err(|e| Error::Nftables {
                    message: format!("Failed to check harborshield chain existence: {}", e),
                    command: Some("nft list chain ip filter harborshield".to_string()),
                    exit_code: None,
                    stderr: None,
                })?;

        if !harborshield_exists {
            // Create harborshield chain in filter table
            create_harborshield_chain(&mut batch, self.family);
        }

        // Check which jump rules already exist
        let (docker_jump_exists, input_jump_exists, output_jump_exists) = check_jump_rules_exist()
            .await
            .map_err(|e| Error::Nftables {
                message: format!("Failed to check jump rules: {}", e),
                command: Some("nft list table ip filter".to_string()),
                exit_code: None,
                stderr: None,
            })?;

        // Create jump rules only if they don't exist
        if !docker_jump_exists || !input_jump_exists || !output_jump_exists {
            create_jump_rules(
                &mut batch,
                self.family,
                has_docker_user && !docker_jump_exists,
                has_input && !input_jump_exists,
                has_output && !output_jump_exists,
            );
        }

        // We no longer need to create a named set - we use inline verdict maps

        // Apply the batch
        let json =
            serde_json::to_string(&batch.clone().to_nftables()).map_err(|e| Error::Json(e))?;
        drop(batch); // Release the lock

        // Execute nft command
        let mut child = std::process::Command::new("nft")
            .args(&["-j", "-f", "-"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .map_err(|e| Error::Nftables {
                message: format!("Failed to spawn nft process: {}", e),
                command: Some("nft -j -f -".to_string()),
                exit_code: None,
                stderr: Some(e.to_string()),
            })?;

        // Write to stdin
        {
            use std::io::Write;
            let mut stdin = child.stdin.take().ok_or_else(|| Error::Nftables {
                message: "Failed to get stdin handle".to_string(),
                command: Some("nft -j -f -".to_string()),
                exit_code: None,
                stderr: None,
            })?;

            stdin
                .write_all(json.as_bytes())
                .map_err(|e| Error::Nftables {
                    message: format!("Failed to write to nft stdin: {}", e),
                    command: Some("nft -j -f -".to_string()),
                    exit_code: None,
                    stderr: Some(e.to_string()),
                })?;
        } // stdin is dropped here, closing the pipe

        // Wait for output
        let output = child.wait_with_output().map_err(|e| Error::Nftables {
            message: format!("Failed to read nft output: {}", e),
            command: Some("nft -j -f -".to_string()),
            exit_code: None,
            stderr: Some(e.to_string()),
        })?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);

            // Check if the error is about objects already existing
            if stderr.contains("File exists") {
                debug!("Some nftables objects already exist, continuing...");
            } else {
                return Err(Error::Config {
                    message: format!("Failed to initialize Docker integration: {}", stderr),
                    location: "init_base_chains".to_string(),
                    suggestion: Some(
                        "Check nftables permissions and Docker installation".to_string(),
                    ),
                });
            }
        }

        Ok(())
    }

    /// Create container-specific chain
    pub async fn create_container_chain(
        &mut self,
        container_id: &str,
        container_name: &str,
    ) -> Result<String> {
        let chain_name = format!(
            "hs-{}-{}",
            container_name.replace(['_', '.', '/'], "-"),
            &container_id[..12.min(container_id.len())]
        );

        // Check if chain already exists
        match helpers::find_chain(FILTER_TABLE, &chain_name) {
            Ok(Some(_existing_chain)) => {
                debug!(
                    "Container chain {} already exists, skipping creation",
                    chain_name
                );
                return Ok(chain_name);
            }
            Ok(None) => {
                // Chain doesn't exist, continue with creation
            }
            Err(e) => {
                // If the error is because chain doesn't exist, that's fine - we'll create it
                // The find_chain function will fail if the chain doesn't exist
                // This is expected for new containers
                debug!(
                    "Chain {} does not exist yet ({}), will create it",
                    chain_name, e
                );
                // Chain doesn't exist, continue with creation
            }
        }

        // Add chain to the current batch instead of creating a new one
        let mut batch = self.batch.lock().await;

        // Single container chain (matching Go implementation)
        batch.add(NfListObject::Chain(Chain {
            family: self.family,
            table: Cow::Borrowed(FILTER_TABLE),
            name: Cow::Owned(chain_name.clone()),
            newname: None,
            handle: None,
            _type: None,
            hook: None,
            prio: None,
            dev: None,
            policy: None,
        }));

        debug!("Added container chain {} to batch", chain_name);

        Ok(chain_name)
    }

    /// Update verdict map rules with current container mappings
    pub async fn update_container_verdict_maps(
        &mut self,
        container_mappings: &[(String, String, Vec<String>)], // (container_id, container_name, ips)
    ) -> Result<()> {
        debug!(
            "Updating verdict maps for {} containers",
            container_mappings.len()
        );

        // If no mappings, nothing to do
        if container_mappings.is_empty() {
            return Ok(());
        }

        let mut batch = self.batch.lock().await;

        if let Some(harborshield_chain) = helpers::find_chain(FILTER_TABLE, HARBORSHIELD_CHAIN)? {
            batch.add_cmd(NfCmd::Flush(FlushObject::Chain(
                harborshield_chain.to_owned(),
            )));
        }

        // Build set items for all containers
        let mut set_items = Vec::new();
        for (container_id, container_name, ips) in container_mappings {
            let chain_name = format!(
                "hs-{}-{}",
                container_name.replace(['_', '.', '/'], "-"),
                &container_id[..12.min(container_id.len())]
            );

            for ip in ips {
                set_items.push(SetItem::Mapping(
                    Expression::String(Cow::Owned(ip.clone())),
                    Expression::Verdict(Verdict::Jump(JumpTarget {
                        target: Cow::Owned(chain_name.clone()),
                    })),
                ));
            }
        }

        if !set_items.is_empty() {
            // Create the verdict map expression using a proper set
            let map_expr = Expression::Named(NamedExpression::Set(set_items));

            // Create source IP lookup rule
            let src_rule = Rule {
                family: self.family,
                table: Cow::Borrowed(FILTER_TABLE),
                chain: Cow::Borrowed(HARBORSHIELD_CHAIN),
                expr: Cow::Owned(vec![Statement::VerdictMap(VerdictMap {
                    key: Expression::Named(NamedExpression::Payload(Payload::PayloadField(
                        PayloadField {
                            protocol: Cow::Borrowed("ip"),
                            field: Cow::Borrowed("saddr"),
                        },
                    ))),
                    data: map_expr.clone(),
                })]),
                handle: None,
                index: None,
                comment: Some(Cow::Borrowed("Container source IP verdict map")),
            };

            // Create destination IP lookup rule
            let dst_rule = Rule {
                family: self.family,
                table: Cow::Borrowed(FILTER_TABLE),
                chain: Cow::Borrowed(HARBORSHIELD_CHAIN),
                expr: Cow::Owned(vec![Statement::VerdictMap(VerdictMap {
                    key: Expression::Named(NamedExpression::Payload(Payload::PayloadField(
                        PayloadField {
                            protocol: Cow::Borrowed("ip"),
                            field: Cow::Borrowed("daddr"),
                        },
                    ))),
                    data: map_expr,
                })]),
                handle: None,
                index: None,
                comment: Some(Cow::Borrowed("Container destination IP verdict map")),
            };

            batch.add(NfListObject::Rule(src_rule));
            batch.add(NfListObject::Rule(dst_rule));
        }

        // Apply the batch
        let json =
            serde_json::to_string(&batch.clone().to_nftables()).map_err(|e| Error::Json(e))?;
        drop(batch);

        let mut child = std::process::Command::new("nft")
            .args(&["-j", "-f", "-"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .map_err(|e| Error::Nftables {
                message: format!("Failed to spawn nft process: {}", e),
                command: Some("nft -j -f -".to_string()),
                exit_code: None,
                stderr: Some(e.to_string()),
            })?;

        // Write to stdin
        {
            use std::io::Write;
            let mut stdin = child.stdin.take().ok_or_else(|| Error::Nftables {
                message: "Failed to get stdin handle".to_string(),
                command: Some("nft -j -f -".to_string()),
                exit_code: None,
                stderr: None,
            })?;

            stdin
                .write_all(json.as_bytes())
                .map_err(|e| Error::Nftables {
                    message: format!("Failed to write to nft stdin: {}", e),
                    command: Some("nft -j -f -".to_string()),
                    exit_code: None,
                    stderr: Some(e.to_string()),
                })?;
        } // stdin is dropped here, closing the pipe

        // Wait for output
        let output = child.wait_with_output().map_err(|e| Error::Nftables {
            message: format!("Failed to update verdict map rules: {}", e),
            command: Some("nft -j -f -".to_string()),
            exit_code: None,
            stderr: Some(e.to_string()),
        })?;

        if !output.status.success() {
            return Err(Error::Config {
                message: format!(
                    "Failed to update verdict map rules: {}",
                    String::from_utf8_lossy(&output.stderr)
                ),
                location: "update_container_verdict_maps".to_string(),
                suggestion: Some("Check nftables syntax".to_string()),
            });
        }

        self.reset().await.map_err(|e| Error::Nftables {
            message: format!("Failed to reset nftables batch: {}", e),
            command: None,
            exit_code: None,
            stderr: None,
        })?;

        debug!(
            "Updated verdict maps for {} containers",
            container_mappings.len()
        );
        Ok(())
    }

    /// Delete a container chain
    pub async fn delete_container_chain(
        &mut self,
        container_id: &str,
        container_name: &str,
    ) -> Result<()> {
        let chain_name = format!(
            "hs-{}-{}",
            container_name.replace(['_', '.', '/'], "-"),
            &container_id[..12.min(container_id.len())]
        );

        let mut batch = self.batch.lock().await;

        batch.delete(NfListObject::Chain(Chain {
            family: self.family,
            table: Cow::Borrowed(FILTER_TABLE),
            name: Cow::Owned(chain_name),
            newname: None,
            handle: None,
            _type: None,
            hook: None,
            prio: None,
            dev: None,
            policy: None,
        }));

        Ok(())
    }

    /// Rebuild container chain with config
    pub async fn rebuild_container_chain(
        &mut self,
        container_id: &str,
        container_name: &str,
        container_ips: &[std::net::IpAddr],
        container_ports: &[(u16, String)],
        config: &Config,
    ) -> Result<()> {
        let chain_name = format!(
            "hs-{}-{}",
            container_name.replace(['_', '.', '/'], "-"),
            &container_id[..12.min(container_id.len())]
        );

        // First, flush the chain to remove all existing rules
        let flush_output = std::process::Command::new("nft")
            .args(&["flush", "chain", "ip", FILTER_TABLE, &chain_name])
            .output()
            .map_err(|e| Error::Nftables {
                message: format!("Failed to flush container chain: {}", e),
                command: Some(format!(
                    "nft flush chain ip {} {}",
                    FILTER_TABLE, &chain_name
                )),
                exit_code: None,
                stderr: Some(e.to_string()),
            })?;

        if !flush_output.status.success() {
            debug!(
                "Failed to flush container chain (may not exist yet): {}",
                String::from_utf8_lossy(&flush_output.stderr)
            );
        }

        // Create a transaction to add all rules in correct order
        let mut transaction = NftablesTransaction::builder().family(self.family).build();

        // Add rules from config
        NftablesTransaction::add_container_rules_to_transaction(
            self.family,
            &mut transaction,
            container_id,
            container_name,
            container_ips,
            container_ports,
            config,
        )
        .map_err(|e| Error::Nftables {
            message: format!("Failed to add container rules to transaction: {}", e),
            command: None,
            exit_code: None,
            stderr: None,
        })?;

        // IMPORTANT: Add DROP rule at the end
        NftablesTransaction::add_container_drop_rule_to_transaction(
            self.family,
            &mut transaction,
            container_id,
            container_name,
        )
        .map_err(|e| Error::Nftables {
            message: format!("Failed to add container rules to transaction: {}", e),
            command: None,
            exit_code: None,
            stderr: None,
        })?;

        // Commit the transaction
        transaction.commit().await.map_err(|e| Error::Nftables {
            message: format!("Failed to commit nftables transaction: {}", e),
            command: Some("nft -j -f -".to_string()),
            exit_code: None,
            stderr: None,
        })?;

        debug!(
            "Rebuilt container chain {} with all rules in correct order",
            chain_name
        );

        Ok(())
    }

    /// Disable container rules (delete chain) for a transaction
    pub fn disable_container_rules(
        &mut self,
        transaction: &mut NftablesTransaction,
        container_id: &str,
        container_name: &str,
    ) -> Result<()> {
        // Flush the container chain to remove all rules
        let chain_name = format!(
            "hs-{}-{}",
            container_name.replace(['_', '.', '/'], "-"),
            &container_id[..12.min(container_id.len())]
        );

        transaction.flush_chain(FILTER_TABLE, &chain_name);

        debug!(
            "Disabled rules for container {} ({})",
            container_name, container_id
        );
        Ok(())
    }

    /// Apply all pending changes
    pub async fn apply(&mut self) -> Result<()> {
        info!("Applying nftables ruleset");
        let batch = self.batch.lock().await;
        let nftables = batch.clone().to_nftables();

        // Debug log the JSON being sent to nftables
        if let Ok(json) = serde_json::to_string_pretty(&nftables) {
            debug!("Applying nftables JSON: {}", json);
        }

        drop(batch);

        match nftables::helper::apply_and_return_ruleset(&nftables) {
            Ok(_) => {
                // Reset the batch after successful application
                self.reset().await.map_err(|e| Error::Nftables {
                    message: format!("Failed to reset nftables batch: {}", e),
                    command: None,
                    exit_code: None,
                    stderr: None,
                })?;
                Ok(())
            }
            Err(e) => {
                let error_msg = match &e {
                    NftablesError::NftFailed {
                        program,
                        hint,
                        stdout,
                        stderr,
                    } => {
                        format!(
                            "nft command failed - program: {:?}, hint: {}, stdout: '{}', stderr: '{}'",
                            program, hint, stdout, stderr
                        )
                    }
                    _ => format!("Failed to apply nftables rules: {:?}", e),
                };

                tracing::error!("NFTables error: {}", error_msg);

                Err(crate::Error::Config {
                    message: error_msg,
                    location: "nftables".to_string(),
                    suggestion: Some("Check nftables permissions and syntax".to_string()),
                })
            }
        }
    }

    /// Reset the batch for new operations
    pub async fn reset(&mut self) -> Result<()> {
        let mut batch = self.batch.lock().await;
        *batch = Batch::new();
        Ok(())
    }

    /// Add rules from a Config directly (new direct translation)
    pub async fn add_rules_from_config(
        &mut self,
        container_id: &str,
        container_name: &str,
        container_ips: &[std::net::IpAddr],
        container_ports: &[(u16, String)],
        config: &Config,
    ) -> Result<()> {
        let chain_name = format!(
            "hs-{}-{}",
            container_name.replace(['_', '.', '/'], "-"),
            &container_id[..12.min(container_id.len())]
        );

        let ctx = RuleContext {
            container_id,
            container_name,
            container_ips,
            container_ports,
            chain_name: &chain_name,
            table_name: FILTER_TABLE,
            family: self.family,
        };

        let mut batch = self.batch.lock().await;

        // Add mapped port rules
        if config.mapped_ports.localhost.allow {
            debug!(
                "Creating localhost rules for container {} with ports: {:?}",
                container_name, container_ports
            );
            // Create a rule for each container port
            for (port, protocol) in container_ports {
                debug!(
                    "Creating localhost rule for container {} port {} protocol {}",
                    container_name, port, protocol
                );
                let mut statements = Vec::new();

                // Match source IP as localhost
                statements.push(Statement::Match(Match {
                    left: Expression::Named(NamedExpression::Payload(Payload::PayloadField(
                        PayloadField {
                            protocol: Cow::Borrowed("ip"),
                            field: Cow::Borrowed("saddr"),
                        },
                    ))),
                    right: Expression::String(Cow::Borrowed("127.0.0.1")),
                    op: Operator::EQ,
                }));

                // Match protocol
                statements.push(Statement::Match(Match {
                    left: Expression::Named(NamedExpression::Meta(Meta {
                        key: MetaKey::L4proto,
                    })),
                    right: Expression::Number(match protocol.to_lowercase().as_str() {
                        "tcp" => 6,
                        "udp" => 17,
                        _ => 6,
                    }),
                    op: Operator::EQ,
                }));

                // Match destination port
                statements.push(Statement::Match(Match {
                    left: Expression::Named(NamedExpression::Payload(Payload::PayloadField(
                        PayloadField {
                            protocol: Cow::Owned(protocol.clone()),
                            field: Cow::Borrowed("dport"),
                        },
                    ))),
                    right: Expression::Number(*port as u32),
                    op: Operator::EQ,
                }));

                // Add counter
                statements.push(Statement::Counter(Counter::Anonymous(None)));

                // Add log if configured
                if !config.mapped_ports.localhost.log_prefix.is_empty() {
                    statements.push(Statement::Log(Some(Log {
                        prefix: Some(Cow::Owned(config.mapped_ports.localhost.log_prefix.clone())),
                        group: None,
                        snaplen: None,
                        queue_threshold: None,
                        level: Some(LogLevel::Info),
                        flags: None,
                    })));
                }

                // Add verdict
                let verdict = if !config.mapped_ports.localhost.verdict.chain.is_empty() {
                    debug!(
                        "Using jump verdict to chain: {}",
                        config.mapped_ports.localhost.verdict.chain
                    );
                    Statement::Jump(JumpTarget {
                        target: Cow::Owned(config.mapped_ports.localhost.verdict.chain.clone()),
                    })
                } else if config.mapped_ports.localhost.verdict.queue > 0 {
                    debug!(
                        "Using queue verdict with num: {}",
                        config.mapped_ports.localhost.verdict.queue
                    );
                    Statement::Queue(Queue {
                        num: Expression::Number(config.mapped_ports.localhost.verdict.queue as u32),
                        flags: None,
                    })
                } else {
                    debug!("Using accept verdict");
                    Statement::Accept(None)
                };
                statements.push(verdict);

                let rule = Rule {
                    family: ctx.family,
                    table: Cow::Owned(ctx.table_name.to_string()),
                    chain: Cow::Owned(ctx.chain_name.to_string()),
                    expr: Cow::Owned(statements),
                    handle: None,
                    index: None,
                    comment: Some(Cow::Owned(format!(
                        "Allow {} port {} from localhost for {}",
                        protocol, port, container_name
                    ))),
                };

                debug!(
                    "Adding localhost rule to batch for container {} chain {}",
                    container_name, ctx.chain_name
                );
                batch.add(NfListObject::Rule(rule));
            }
        }

        if config.mapped_ports.external.allow {
            // Create a rule for each container port
            for (port, protocol) in container_ports {
                let mut statements = Vec::new();

                // Only add NOT localhost check if no specific IPs are configured
                if config.mapped_ports.external.ips.is_empty() {
                    // Match NOT localhost (external traffic)
                    statements.push(Statement::Match(Match {
                        left: Expression::Named(NamedExpression::Payload(Payload::PayloadField(
                            PayloadField {
                                protocol: Cow::Borrowed("ip"),
                                field: Cow::Borrowed("saddr"),
                            },
                        ))),
                        right: Expression::String(Cow::Borrowed("127.0.0.1")),
                        op: Operator::NEQ,
                    }));
                }

                // Match protocol
                statements.push(Statement::Match(Match {
                    left: Expression::Named(NamedExpression::Meta(Meta {
                        key: MetaKey::L4proto,
                    })),
                    right: Expression::Number(match protocol.to_lowercase().as_str() {
                        "tcp" => 6,
                        "udp" => 17,
                        _ => 6,
                    }),
                    op: Operator::EQ,
                }));

                // Match destination port
                statements.push(Statement::Match(Match {
                    left: Expression::Named(NamedExpression::Payload(Payload::PayloadField(
                        PayloadField {
                            protocol: Cow::Owned(protocol.clone()),
                            field: Cow::Borrowed("dport"),
                        },
                    ))),
                    right: Expression::Number(*port as u32),
                    op: Operator::EQ,
                }));

                // Add source IP filtering if configured
                if !config.mapped_ports.external.ips.is_empty() {
                    // Create a set expression for multiple IPs/ranges
                    let mut set_items = Vec::new();
                    for addr_range in &config.mapped_ports.external.ips {
                        match addr_range {
                            crate::docker::config::AddrOrRange::Addr(ip) => {
                                set_items.push(SetItem::Element(Expression::String(Cow::Owned(
                                    ip.to_string(),
                                ))));
                            }
                            crate::docker::config::AddrOrRange::Range(start, end) => {
                                set_items.push(SetItem::Element(Expression::Range(Box::new(
                                    nftables::expr::Range {
                                        range: [
                                            Expression::String(Cow::Owned(start.to_string())),
                                            Expression::String(Cow::Owned(end.to_string())),
                                        ],
                                    },
                                ))));
                            }
                            crate::docker::config::AddrOrRange::Net(net) => {
                                // For CIDR networks, use the Prefix expression type
                                set_items.push(SetItem::Element(Expression::Named(
                                    NamedExpression::Prefix(nftables::expr::Prefix {
                                        addr: Box::new(Expression::String(Cow::Owned(
                                            net.addr().to_string(),
                                        ))),
                                        len: net.prefix_len() as u32,
                                    }),
                                )));
                            }
                        }
                    }

                    // If we have only one IP/range, use direct match
                    if set_items.len() == 1 {
                        if let SetItem::Element(expr) = &set_items[0] {
                            statements.push(Statement::Match(Match {
                                left: Expression::Named(NamedExpression::Payload(
                                    Payload::PayloadField(PayloadField {
                                        protocol: Cow::Borrowed("ip"),
                                        field: Cow::Borrowed("saddr"),
                                    }),
                                )),
                                right: expr.clone(),
                                op: Operator::EQ,
                            }));
                        }
                    } else if set_items.len() > 1 {
                        // For multiple IPs, use a set match
                        statements.push(Statement::Match(Match {
                            left: Expression::Named(NamedExpression::Payload(
                                Payload::PayloadField(PayloadField {
                                    protocol: Cow::Borrowed("ip"),
                                    field: Cow::Borrowed("saddr"),
                                }),
                            )),
                            right: Expression::Named(NamedExpression::Set(set_items)),
                            op: Operator::EQ,
                        }));
                    }
                }

                // Add counter
                statements.push(Statement::Counter(Counter::Anonymous(None)));

                // Add log if configured
                if !config.mapped_ports.external.log_prefix.is_empty() {
                    statements.push(Statement::Log(Some(Log {
                        prefix: Some(Cow::Owned(config.mapped_ports.external.log_prefix.clone())),
                        group: None,
                        snaplen: None,
                        queue_threshold: None,
                        level: Some(LogLevel::Info),
                        flags: None,
                    })));
                }

                // Add verdict
                let verdict = if !config.mapped_ports.external.verdict.chain.is_empty() {
                    Statement::Jump(JumpTarget {
                        target: Cow::Owned(config.mapped_ports.external.verdict.chain.clone()),
                    })
                } else if config.mapped_ports.external.verdict.queue > 0 {
                    Statement::Queue(Queue {
                        num: Expression::Number(config.mapped_ports.external.verdict.queue as u32),
                        flags: None,
                    })
                } else {
                    Statement::Accept(None)
                };
                statements.push(verdict);

                let rule = Rule {
                    family: ctx.family,
                    table: Cow::Owned(ctx.table_name.to_string()),
                    chain: Cow::Owned(ctx.chain_name.to_string()),
                    expr: Cow::Owned(statements),
                    handle: None,
                    index: None,
                    comment: Some(Cow::Owned(format!(
                        "Allow {} port {} from external for {}",
                        protocol, port, container_name
                    ))),
                };

                batch.add(NfListObject::Rule(rule));
            }
        }

        // Add output rules
        for (i, output_rule) in config.output.iter().enumerate() {
            if !output_rule.skip {
                let rule = output_rule
                    .to_nftables_rule(
                        &ctx,
                        Some(format!("Output rule {} for {}", i + 1, container_name)),
                    )
                    .map_err(|e| Error::Nftables {
                        message: format!("Failed to add container rules to transaction: {}", e),
                        command: None,
                        exit_code: None,
                        stderr: None,
                    })?;
                batch.add(NfListObject::Rule(rule));
            }
        }

        debug!(
            "Finished adding rules to batch for container {}. Localhost rules: {}, External rules: {}, Output rules: {}",
            container_name,
            if config.mapped_ports.localhost.allow {
                container_ports.len()
            } else {
                0
            },
            if config.mapped_ports.external.allow {
                container_ports.len()
            } else {
                0
            },
            config.output.iter().filter(|r| !r.skip).count()
        );

        Ok(())
    }
}

// Re-export minimal types needed by other modules
pub use nftables::schema::NfListObject as NftObject;