Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom bridge interface names #736

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dfw"
version = "1.3.0"
version = "1.3.1"
edition = "2021"
authors = ["Pit Kleyersburg <pitkley@googlemail.com>"]
license = "MIT/Apache-2.0"
Expand Down
29 changes: 13 additions & 16 deletions src/iptables/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,7 @@ impl Process<Iptables> for ContainerToContainer {

if let Some(same_network_verdict) = self.same_network_verdict {
for network in ctx.network_map.values() {
let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -304,7 +303,7 @@ impl Process<Iptables> for ContainerToContainerRule {
"network" => format!("{:?}", network)));

let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand All @@ -327,7 +326,7 @@ impl Process<Iptables> for ContainerToContainerRule {
o!("network_name" => &network.name,
"src_network" => format!("{:?}", src_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -359,7 +358,7 @@ impl Process<Iptables> for ContainerToContainerRule {
o!("network_name" => &network.name,
"dst_network" => format!("{:?}", dst_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -415,8 +414,7 @@ impl Process<Iptables> for ContainerToWiderWorld {
"external_network_interface" => external_network_interface,
"default_policy" => &self.default_policy));
for network in ctx.network_map.values() {
let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -464,7 +462,7 @@ impl Process<Iptables> for ContainerToWiderWorldRule {
o!("network_name" => &network.name,
"src_network" => format!("{:?}", src_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand All @@ -479,7 +477,7 @@ impl Process<Iptables> for ContainerToWiderWorldRule {
);
}
} else {
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -540,8 +538,7 @@ impl Process<Iptables> for ContainerToHost {

// Default policy
for network in ctx.network_map.values() {
let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -578,7 +575,7 @@ impl Process<Iptables> for ContainerToHostRule {
"network" => format!("{:?}", network)));

let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -671,7 +668,7 @@ impl Process<Iptables> for WiderWorldToContainerRule {
"network" => format!("{:?}", network)));

let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -883,7 +880,7 @@ impl Process<Iptables> for ContainerDNATRule {
"network" => format!("{:?}", network)));

let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand All @@ -901,7 +898,7 @@ impl Process<Iptables> for ContainerDNATRule {
o!("network_name" => &network.name,
"src_network" => format!("{:?}", src_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -937,7 +934,7 @@ impl Process<Iptables> for ContainerDNATRule {
o!("network_name" => &network.name,
"dst_network" => format!("{:?}", dst_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down
32 changes: 14 additions & 18 deletions src/nftables/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ impl Process<Nftables> for ContainerToContainer {

if let Some(same_network_verdict) = self.same_network_verdict {
for network in ctx.network_map.values() {
let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -370,7 +369,7 @@ impl Process<Nftables> for ContainerToContainerRule {
o!("network_name" => &self.network,
"network" => format!("{:?}", network)));
let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand All @@ -394,7 +393,7 @@ impl Process<Nftables> for ContainerToContainerRule {
o!("network_name" => &network.name,
"src_network" => format!("{:?}", src_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -426,8 +425,7 @@ impl Process<Nftables> for ContainerToContainerRule {
o!("network_name" => &network.name,
"dst_network" => format!("{:?}", dst_network)));

let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -474,8 +472,7 @@ impl Process<Nftables> for ContainerToWiderWorld {
"external_network_interface" => external_network_interface,
"default_policy" => &self.default_policy));
for network in ctx.network_map.values() {
let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -512,7 +509,7 @@ impl Process<Nftables> for ContainerToWiderWorldRule {
if let Some(ref network) = self.network {
if let Some(network) = ctx.network_map.get(network) {
let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand All @@ -530,7 +527,7 @@ impl Process<Nftables> for ContainerToWiderWorldRule {
o!("network_name" => &network.name,
"src_network" => format!("{:?}", src_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand All @@ -545,7 +542,7 @@ impl Process<Nftables> for ContainerToWiderWorldRule {
);
}
} else {
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -603,8 +600,7 @@ impl Process<Nftables> for ContainerToHost {

// Default policy
for network in ctx.network_map.values() {
let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -642,7 +638,7 @@ impl Process<Nftables> for ContainerToHostRule {
"network" => format!("{:?}", network)));

let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -821,7 +817,7 @@ impl Process<Nftables> for WiderWorldToContainerRule {
"network" => format!("{:?}", network)));

let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -1003,7 +999,7 @@ impl Process<Nftables> for ContainerDNATRule {
"network" => format!("{:?}", network)));

let network_id = network.id.as_ref().expect("Docker network ID missing");
let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand All @@ -1021,7 +1017,7 @@ impl Process<Nftables> for ContainerDNATRule {
o!("network_name" => &network.name,
"src_network" => format!("{:?}", src_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down Expand Up @@ -1057,7 +1053,7 @@ impl Process<Nftables> for ContainerDNATRule {
o!("network_name" => &network.name,
"dst_network" => format!("{:?}", dst_network)));

let bridge_name = get_bridge_name(network_id)?;
let bridge_name = get_bridge_name(network)?;
trace!(ctx.logger, "Got bridge name";
o!("network_name" => &network.name,
"bridge_name" => &bridge_name));
Expand Down
18 changes: 12 additions & 6 deletions src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ where
let network_map =
get_network_map(&networks).ok_or_else(|| format_err!("no networks found"))?;
trace!(logger, "Got map of networks";
o!("container_map" => format!("{:#?}", container_map)));
o!("network_map" => format!("{:#?}", network_map)));

let external_network_interfaces = dfw
.global_defaults
Expand Down Expand Up @@ -215,11 +215,17 @@ impl Default for ProcessingOptions {
}
}

pub(crate) fn get_bridge_name(network_id: &str) -> Result<String> {
if network_id.len() < 12 {
bail!("network has to be longer than 12 characters");
}
Ok(format!("br-{}", &network_id[..12]))
pub(crate) fn get_bridge_name(network: &Network) -> Result<String> {
return network.options.as_ref()
.and_then(|options| options.get("com.docker.network.bridge.name"))
.map(|value| Ok(value.to_owned()))
.unwrap_or_else(|| {
let network_id = network.id.as_ref().expect("Docker network ID missing");
if network_id.len() < 12 {
bail!("network has to be longer than 12 characters");
}
return Ok(format!("br-{}", &network_id[..12]));
});
}

pub(crate) fn get_network_for_container(
Expand Down