Skip to content
Draft
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
135 changes: 134 additions & 1 deletion crates/countersign-gateway/src/runtime/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ pub(crate) struct Config {
pub workload_token: Option<PathBuf>,
pub workload: Option<WorkloadBinding>,
pub destinations: Vec<Rule>,
/// The HTTP proxy every destination goes through, unless its rule says
/// `"egress": "direct"` (ADR 0003).
#[serde(rename = "egress_proxy")]
egress_proxy_url: Option<String>,
#[serde(skip)]
pub egress_proxy: Option<EgressProxy>,
#[serde(default)]
pub denied_networks: Vec<IpNet>,
pub shared: Option<Shared>,
Expand Down Expand Up @@ -67,6 +73,77 @@ impl fmt::Display for WorkerSandbox {
}
}

/// The egress proxy's address, as the operator wrote it: a name or an IP
/// literal, resolved on every connection.
#[derive(Debug, Clone)]
pub(crate) struct EgressProxy {
pub host: String,
pub port: u16,
}

impl EgressProxy {
fn parse(url: &str) -> Result<Self> {
let shape = || {
invalid(
"egress_proxy must be an http URL with a host and a port, such as http://proxy.internal:3128",
)
};
let parsed = reqwest::Url::parse(url).map_err(|_| shape())?;
if parsed.scheme() == "https" {
return Err(invalid(
"egress_proxy must be an http URL: the Gateway sends CONNECT to the egress proxy in plain text and does not support an https egress proxy",
));
}
if !parsed.username().is_empty() || parsed.password().is_some() {
return Err(invalid(
"egress_proxy must not carry credentials: the Gateway does not authenticate to the egress proxy",
));
}
// The URL parser forgets a port it knows as the default, so look for
// one in the text: after the host, outside an IPv6 literal's brackets.
let authority = url
.split_once("://")
.map_or("", |(_, rest)| rest)
.split(['/', '?', '#'])
.next()
.unwrap_or_default();
let written_port = authority
.rsplit(']')
.next()
.and_then(|tail| tail.rsplit_once(':'))
.is_some_and(|(_, port)| !port.is_empty());
let (Some(host), Some(port)) = (parsed.host_str(), parsed.port_or_known_default()) else {
return Err(shape());
};
if parsed.scheme() != "http"
|| !written_port
|| port == 0
|| parsed.path() != "/"
|| parsed.query().is_some()
|| parsed.fragment().is_some()
{
return Err(shape());
}
Ok(Self {
host: host
.trim_start_matches('[')
.trim_end_matches(']')
.to_owned(),
port,
})
}
}

impl fmt::Display for EgressProxy {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.host.contains(':') {
write!(f, "[{}]:{}", self.host, self.port)
} else {
write!(f, "{}:{}", self.host, self.port)
}
}
}

fn default_uncertainty() -> u64 {
1000
}
Expand All @@ -90,14 +167,43 @@ pub(crate) struct Rule {
pub approval: ApprovalMode,
#[serde(default)]
pub private_networks: Vec<IpNet>,
#[serde(default)]
pub egress: Egress,
pub roots: PathBuf,
#[serde(default)]
pub revocation: Option<countersign_pki::Revocation>,
}

/// A destination's route to the site (ADR 0003).
#[derive(Deserialize, Debug, Clone, Copy, Default, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub(crate) enum Egress {
/// Through the egress proxy when the Gateway has one, else direct. The
/// operator never writes it: it is what a rule without `egress` means.
#[default]
#[serde(skip_deserializing)]
Default,
/// Always direct, with the Gateway's own address policy.
Direct,
}

impl Config {
pub fn load(path: &Path) -> Result<Self> {
let config: Self = serde_json::from_slice(&fs::read(path)?)?;
let mut config: Self = serde_json::from_slice(&fs::read(path)?)?;
config.egress_proxy = config
.egress_proxy_url
.as_deref()
.map(EgressProxy::parse)
.transpose()?;
if let Some(rule) = config.destinations.iter().find(|rule| {
config.egress_proxy_for(rule).is_some() && !rule.private_networks.is_empty()
}) {
return Err(io::Error::other(format!(
"destination rule {:?} has private_networks and goes through the egress proxy, which does not check them: remove private_networks, or set \"egress\": \"direct\" on the rule",
rule.id
))
.into());
}
let sidecar = config.shared.is_none();
if config.workload.is_some() != sidecar
|| config.workload_token.is_some() != sidecar
Expand Down Expand Up @@ -130,6 +236,33 @@ impl Config {
Ok(config)
}

/// The egress proxy this rule's destinations go through, if any.
pub fn egress_proxy_for(&self, rule: &Rule) -> Option<&EgressProxy> {
match rule.egress {
Egress::Direct => None,
Egress::Default => self.egress_proxy.as_ref(),
}
}

/// Which destinations leave their address policy to the egress proxy, in
/// words for the startup log.
pub fn left_to_egress_proxy(&self) -> String {
let ids = self
.destinations
.iter()
.filter(|rule| self.egress_proxy_for(rule).is_some())
.map(|rule| rule.id.as_str())
.collect::<Vec<_>>();
if ids.is_empty() {
"carries the address policy of no destination".to_owned()
} else {
format!(
"carries the address policy of destinations: {}",
ids.join(", ")
)
}
}

pub fn policy(&self) -> Result<DestinationPolicy> {
if self
.destinations
Expand Down
120 changes: 120 additions & 0 deletions crates/countersign-gateway/src/runtime/egress.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
//! The proxied leg to a site: a plain-text `CONNECT` through the egress proxy
//! (ADR 0003). The egress proxy resolves the site's name and carries its
//! address policy; the Gateway resolves only the proxy.

use std::{error::Error, fmt, net::SocketAddr};

use countersign_protocol::Origin;
use ipnet::IpNet;
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpStream,
};

use super::{Result, config::EgressProxy, invalid};

/// The most the Gateway reads of the egress proxy's answer before the tunnel.
const MAX_HEAD: usize = 8 * 1024;

/// The egress proxy answered the `CONNECT` with something other than 2xx.
#[derive(Debug)]
pub struct ProxyAnswer(pub u16);

impl fmt::Display for ProxyAnswer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "the egress proxy answered {}", self.0)
}
}

impl Error for ProxyAnswer {}

/// The `Countersign-Error` value for a proxied leg that failed (ADR 0003).
pub fn error_value(error: &(dyn Error + Send + Sync + 'static)) -> &'static str {
match error.downcast_ref::<ProxyAnswer>() {
Some(ProxyAnswer(400..=499)) => "proxy_denied",
// As a direct route reports a site it cannot reach.
Some(ProxyAnswer(500..=599)) => "upstream_failure",
// Unreachable, resolved only inside `denied_networks`, out of time
// (the deadline covers the whole leg), or no usable HTTP answer.
_ => "proxy_unavailable",
}
}

/// Opens a tunnel to `origin` through the egress proxy. On success the stream
/// carries the site's bytes from its first byte: the Gateway reads the
/// proxy's answer no further than the end of its head.
pub async fn tunnel(proxy: &EgressProxy, origin: &Origin, denied: &[IpNet]) -> Result<TcpStream> {
// Resolved on every connection, so a load balancer may change addresses.
// Only `denied_networks` applies: the proxy normally sits on a private one.
let addresses = tokio::net::lookup_host((proxy.host.as_str(), proxy.port))
.await?
.filter(|address| {
let ip = address.ip().to_canonical();
!denied.iter().any(|block| block.contains(&ip))
})
.collect::<Vec<SocketAddr>>();
if addresses.is_empty() {
return Err(invalid("egress proxy resolved to no allowed addresses"));
}
let mut stream = TcpStream::connect(&addresses[..]).await?;
stream
.write_all(format!("CONNECT {origin} HTTP/1.1\r\nHost: {origin}\r\n\r\n").as_bytes())
.await?;
match answer(&mut stream).await? {
200..=299 => Ok(stream),
code => Err(ProxyAnswer(code).into()),
}
}

/// Reads the egress proxy's answer to the `CONNECT` and returns its status
/// code. It reads past the status line only for a 2xx answer, up to the head's
/// end, and gives up as soon as the bytes cannot be an HTTP answer: a proxy
/// that keeps the connection open after any other answer gets no wait.
async fn answer(stream: &mut TcpStream) -> Result<u16> {
const VERSION: &[u8] = b"HTTP/1.";
let not_http = || invalid("egress proxy gave no HTTP answer");
let mut line = Vec::with_capacity(64);
let mut code = None;
for _ in 0..MAX_HEAD {
// One byte at a time, so no byte of the tunnel is consumed.
let byte = stream.read_u8().await?;
if byte != b'\n' {
line.push(byte);
if code.is_none() && !line.starts_with(VERSION) && !VERSION.starts_with(&line) {
return Err(not_http());
}
continue;
}
// A line ends at LF, with or without CR before it (RFC 9112, 2.2).
if line.last() == Some(&b'\r') {
line.pop();
}
match code {
None => {
let status = status(&line).ok_or_else(not_http)?;
if !(200..300).contains(&status) {
return Ok(status);
}
code = Some(status);
}
Some(status) if line.is_empty() => return Ok(status),
Some(_) => {}
}
line.clear();
}
Err(invalid("egress proxy answer exceeds the limit"))
}

/// The status code of a status line, given without its line end.
fn status(line: &[u8]) -> Option<u16> {
let line = std::str::from_utf8(line).ok()?;
let mut parts = line.splitn(3, ' ');
if !matches!(parts.next()?, "HTTP/1.1" | "HTTP/1.0") {
return None;
}
let code = parts.next()?;
if code.len() != 3 || !code.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
code.parse().ok()
}
Loading
Loading