Skip to content

Commit 32dff02

Browse files
committed
DRY cleanup logic
1 parent 8f4ee2e commit 32dff02

2 files changed

Lines changed: 60 additions & 49 deletions

File tree

src/jail/linux/iptables.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ pub struct IPTablesRule {
1616
}
1717

1818
impl IPTablesRule {
19+
/// Create a rule object for an existing rule (for cleanup purposes)
20+
/// This doesn't add the rule, but will remove it when dropped
21+
pub fn new_existing(table: Option<&str>, chain: &str, rule_spec: Vec<&str>) -> Self {
22+
Self {
23+
table: table.map(|s| s.to_string()),
24+
chain: chain.to_string(),
25+
rule_spec: rule_spec.iter().map(|s| s.to_string()).collect(),
26+
added: true, // Mark as added so it will be removed on drop
27+
}
28+
}
29+
1930
/// Create and add a new iptables rule
2031
pub fn new(table: Option<&str>, chain: &str, rule_spec: Vec<&str>) -> Result<Self> {
2132
let mut args = Vec::new();

src/jail/linux/mod.rs

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,8 @@ impl Jail for LinuxJail {
725725
info!("Cleaning up orphaned Linux jail: {}", jail_id);
726726

727727
let namespace_name = format!("httpjail_{}", jail_id);
728-
let veth_host = format!("veth_h_{}", jail_id);
728+
let veth_host = format!("vh_{}", jail_id);
729+
let comment = format!("httpjail-{}", namespace_name);
729730

730731
// Clean up namespace-specific config directory
731732
let netns_etc = format!("/etc/netns/{}", namespace_name);
@@ -743,57 +744,56 @@ impl Jail for LinuxJail {
743744
.args(["link", "del", &veth_host])
744745
.output();
745746

746-
// Clean up iptables rules with matching comment
747-
let comment = format!("httpjail-{}", namespace_name);
748-
749-
// Remove MASQUERADE rule
750-
let _ = Command::new("iptables")
751-
.args([
752-
"-t",
753-
"nat",
754-
"-D",
747+
// Create temporary IPTablesRule objects to clean up iptables rules
748+
// Their Drop impl will remove the rules
749+
let _rules = vec![
750+
// MASQUERADE rule
751+
IPTablesRule::new_existing(
752+
Some("nat"),
755753
"POSTROUTING",
756-
"-s",
757-
LINUX_NS_SUBNET,
758-
"-m",
759-
"comment",
760-
"--comment",
761-
&comment,
762-
"-j",
763-
"MASQUERADE",
764-
])
765-
.output();
766-
767-
// Remove FORWARD rules
768-
let _ = Command::new("iptables")
769-
.args([
770-
"-D",
754+
vec![
755+
"-s",
756+
LINUX_NS_SUBNET,
757+
"-m",
758+
"comment",
759+
"--comment",
760+
&comment,
761+
"-j",
762+
"MASQUERADE",
763+
],
764+
),
765+
// FORWARD source rule
766+
IPTablesRule::new_existing(
767+
None,
771768
"FORWARD",
772-
"-s",
773-
LINUX_NS_SUBNET,
774-
"-m",
775-
"comment",
776-
"--comment",
777-
&comment,
778-
"-j",
779-
"ACCEPT",
780-
])
781-
.output();
782-
783-
let _ = Command::new("iptables")
784-
.args([
785-
"-D",
769+
vec![
770+
"-s",
771+
LINUX_NS_SUBNET,
772+
"-m",
773+
"comment",
774+
"--comment",
775+
&comment,
776+
"-j",
777+
"ACCEPT",
778+
],
779+
),
780+
// FORWARD destination rule
781+
IPTablesRule::new_existing(
782+
None,
786783
"FORWARD",
787-
"-d",
788-
LINUX_NS_SUBNET,
789-
"-m",
790-
"comment",
791-
"--comment",
792-
&comment,
793-
"-j",
794-
"ACCEPT",
795-
])
796-
.output();
784+
vec![
785+
"-d",
786+
LINUX_NS_SUBNET,
787+
"-m",
788+
"comment",
789+
"--comment",
790+
&comment,
791+
"-j",
792+
"ACCEPT",
793+
],
794+
),
795+
];
796+
// Rules will be cleaned up when _rules goes out of scope
797797

798798
Ok(())
799799
}

0 commit comments

Comments
 (0)