forked from metalbear-co/mirrord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the same output rules for both meshed and not meshed networks (me…
…talbear-co#2264) * Use the same output rules for both meshed and not meshed networks * Tidy Up * Changelog * More Tidy * Update failing test * Don't parallel iptable updates * Update Changelog
- Loading branch information
1 parent
94ce214
commit b2b57f8
Showing
7 changed files
with
335 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix incoming network interception via port-forward when "stealing" traffic with a mesh like linkerd or istio (Using the same `OUTPUT` iptable rules for both meshed and not meshed networks) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::sync::Arc; | ||
|
||
use async_trait::async_trait; | ||
use mirrord_protocol::Port; | ||
use nix::unistd::getgid; | ||
use tracing::warn; | ||
|
||
use crate::{ | ||
error::Result, | ||
steal::ip_tables::{chain::IPTableChain, IPTables, Redirect}, | ||
}; | ||
|
||
pub(crate) struct OutputRedirect<IPT: IPTables> { | ||
pub(crate) managed: IPTableChain<IPT>, | ||
} | ||
|
||
impl<IPT> OutputRedirect<IPT> | ||
where | ||
IPT: IPTables, | ||
{ | ||
const ENTRYPOINT: &'static str = "OUTPUT"; | ||
|
||
pub fn create(ipt: Arc<IPT>, chain_name: String) -> Result<Self> { | ||
let managed = IPTableChain::create(ipt, chain_name)?; | ||
|
||
let gid = getgid(); | ||
managed | ||
.add_rule(&format!("-m owner --gid-owner {gid} -p tcp -j RETURN")) | ||
.inspect_err(|_| { | ||
warn!("Unable to create iptable rule with \"--gid-owner {gid}\" filter") | ||
})?; | ||
|
||
Ok(OutputRedirect { managed }) | ||
} | ||
|
||
pub fn load(ipt: Arc<IPT>, chain_name: String) -> Result<Self> { | ||
let managed = IPTableChain::create(ipt, chain_name)?; | ||
|
||
Ok(OutputRedirect { managed }) | ||
} | ||
} | ||
|
||
/// This wrapper adds a new rule to the NAT OUTPUT chain to redirect "localhost" traffic as well | ||
/// Note: OUTPUT chain is only traversed for packets produced by local applications | ||
#[async_trait] | ||
impl<IPT> Redirect for OutputRedirect<IPT> | ||
where | ||
IPT: IPTables + Send + Sync, | ||
{ | ||
async fn mount_entrypoint(&self) -> Result<()> { | ||
self.managed.inner().add_rule( | ||
Self::ENTRYPOINT, | ||
&format!("-j {}", self.managed.chain_name()), | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn unmount_entrypoint(&self) -> Result<()> { | ||
self.managed.inner().remove_rule( | ||
Self::ENTRYPOINT, | ||
&format!("-j {}", self.managed.chain_name()), | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn add_redirect(&self, redirected_port: Port, target_port: Port) -> Result<()> { | ||
let redirect_rule = format!( | ||
"-o lo -m tcp -p tcp --dport {redirected_port} -j REDIRECT --to-ports {target_port}" | ||
); | ||
|
||
self.managed.add_rule(&redirect_rule)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn remove_redirect(&self, redirected_port: Port, target_port: Port) -> Result<()> { | ||
let redirect_rule = format!( | ||
"-o lo -m tcp -p tcp --dport {redirected_port} -j REDIRECT --to-ports {target_port}" | ||
); | ||
|
||
self.managed.remove_rule(&redirect_rule)?; | ||
|
||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.