Idunn routes: an optional [route.member] renders a second nginx server block and sets GAMECULT_IDUNN_MEMBER_BIND; Idunn-owned launch names live in one list
The member candidate port is derived from the candidate port; both stable sockets get firewall rules; native targets render the same bytes as before. Builds. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e2dee7c16a
commit
7805c0aa9a
3 changed files with 510 additions and 80 deletions
|
|
@ -10723,6 +10723,7 @@ mod tests {
|
|||
private_port_end,
|
||||
config_path: PathBuf::from(format!("/etc/nginx/idunn-stream-routes/{route_id}.conf")),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,12 +9,27 @@ pub const TARGET_DECLARATION_SCHEMA: &str = "gamecult.idunn.target_declaration.v
|
|||
pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v2";
|
||||
pub const IDUNN_RUNTIME_BUNDLE_ENVIRONMENT: &str = "GAMECULT_IDUNN_RUNTIME_BUNDLE";
|
||||
pub const IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT: &str = "GAMECULT_IDUNN_CANDIDATE_BIND";
|
||||
pub const IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT: &str = "GAMECULT_IDUNN_MEMBER_BIND";
|
||||
pub const IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT: &str = "GAMECULT_IDUNN_PROCESS_WRITE_LEASE";
|
||||
pub const RUNTIME_PRESENCE_IDENTITY_BINDING: &str = "GAMECULT_RUNTIME_PRESENCE_IDENTITY";
|
||||
/// A host actuator hands the workload its activation credential as a file
|
||||
/// named by this variable; on the Idunn host it arrives as a parent-only
|
||||
/// descriptor instead and this name is never set.
|
||||
pub const IDUNN_ACTIVATION_CREDENTIAL_ENVIRONMENT: &str = "GAMECULT_IDUNN_ACTIVATION_CREDENTIAL";
|
||||
/// systemd hands a unit its credential directory under this name; Idunn owns
|
||||
/// the directory, so no binding may set it.
|
||||
pub const SYSTEMD_CREDENTIALS_DIRECTORY_ENVIRONMENT: &str = "CREDENTIALS_DIRECTORY";
|
||||
/// Every launch-environment name Idunn sets or reserves. A binding may name
|
||||
/// none of them; this is the one list both the binding check and the
|
||||
/// launch-input check read.
|
||||
pub(crate) const IDUNN_OWNED_LAUNCH_ENVIRONMENT: [&str; 6] = [
|
||||
IDUNN_RUNTIME_BUNDLE_ENVIRONMENT,
|
||||
IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT,
|
||||
IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT,
|
||||
IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT,
|
||||
IDUNN_ACTIVATION_CREDENTIAL_ENVIRONMENT,
|
||||
SYSTEMD_CREDENTIALS_DIRECTORY_ENVIRONMENT,
|
||||
];
|
||||
pub const RUNTIME_PRESENCE_IDENTITY_FD_NAME: &str = "gamecult-runtime-presence-identity";
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
|
|
@ -509,8 +524,7 @@ impl WorkloadBinding {
|
|||
{
|
||||
require_environment_name(name)?;
|
||||
ensure!(
|
||||
name != IDUNN_RUNTIME_BUNDLE_ENVIRONMENT
|
||||
&& name != IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT,
|
||||
!IDUNN_OWNED_LAUNCH_ENVIRONMENT.contains(&name.as_str()),
|
||||
"operator binding attempts to replace Idunn-owned workload environment"
|
||||
);
|
||||
}
|
||||
|
|
@ -667,40 +681,87 @@ pub struct RouteBinding {
|
|||
pub private_port_end: u16,
|
||||
pub config_path: PathBuf,
|
||||
pub reload_unit: String,
|
||||
/// A second listener for the route's members. When set, members reach the
|
||||
/// workload straight through a second nginx server block, and the
|
||||
/// workload's launcher answers presence only.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub member: Option<MemberListenerBinding>,
|
||||
}
|
||||
|
||||
/// The member listener of a route: where members connect, and the first of the
|
||||
/// private ports the workload binds for them. The private range is as wide as
|
||||
/// the route's own.
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct MemberListenerBinding {
|
||||
pub stable_endpoint: String,
|
||||
pub private_port_start: u16,
|
||||
}
|
||||
|
||||
impl RouteBinding {
|
||||
pub(crate) fn stable_socket(&self) -> Result<(IpAddr, u16)> {
|
||||
let authority = match self.driver {
|
||||
RouteDriver::NginxStreamTcp => self
|
||||
.stable_endpoint
|
||||
.strip_prefix("http://")
|
||||
.or_else(|| self.stable_endpoint.strip_prefix("tcp://")),
|
||||
RouteDriver::NginxStreamUdp => self.stable_endpoint.strip_prefix("rudp://"),
|
||||
}
|
||||
.context("stable endpoint transport does not match its route driver")?;
|
||||
ensure!(
|
||||
!authority
|
||||
.chars()
|
||||
.any(|character| matches!(character, '/' | '?' | '#' | '@' | '[' | ']')),
|
||||
"stable endpoint is not a plain IP address and port"
|
||||
);
|
||||
let (host, port) = authority
|
||||
.rsplit_once(':')
|
||||
.context("stable endpoint has no port")?;
|
||||
let host = host
|
||||
.parse::<IpAddr>()
|
||||
.context("stable endpoint host is not an IP address")?;
|
||||
ensure!(
|
||||
matches!(host, IpAddr::V4(_)),
|
||||
"route binding v2 stable endpoints use explicit IPv4 addresses"
|
||||
);
|
||||
let port = port
|
||||
.parse::<u16>()
|
||||
.context("stable endpoint port is not a u16")?;
|
||||
ensure!(port > 0, "stable endpoint port is zero");
|
||||
Ok((host, port))
|
||||
parse_stable_socket(self.driver, &self.stable_endpoint, "stable endpoint")
|
||||
}
|
||||
|
||||
/// The member listener's stable socket, when the route has one.
|
||||
pub(crate) fn member_stable_socket(&self) -> Result<Option<(IpAddr, u16)>> {
|
||||
self.member
|
||||
.as_ref()
|
||||
.map(|member| {
|
||||
parse_stable_socket(self.driver, &member.stable_endpoint, "member endpoint")
|
||||
})
|
||||
.transpose()
|
||||
}
|
||||
|
||||
/// The private port the workload binds for members when its presence
|
||||
/// candidate is `candidate_port`. Derived, never stored: the member range
|
||||
/// is the private range shifted to the member start.
|
||||
pub(crate) fn member_candidate_port(&self, candidate_port: u16) -> Result<Option<u16>> {
|
||||
let Some(member) = self.member.as_ref() else {
|
||||
return Ok(None);
|
||||
};
|
||||
ensure!(
|
||||
(self.private_port_start..=self.private_port_end).contains(&candidate_port),
|
||||
"candidate port is outside the route binding"
|
||||
);
|
||||
let offset = candidate_port - self.private_port_start;
|
||||
let port = member
|
||||
.private_port_start
|
||||
.checked_add(offset)
|
||||
.context("member private port overflows")?;
|
||||
Ok(Some(port))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_stable_socket(driver: RouteDriver, endpoint: &str, what: &str) -> Result<(IpAddr, u16)> {
|
||||
let authority = match driver {
|
||||
RouteDriver::NginxStreamTcp => endpoint
|
||||
.strip_prefix("http://")
|
||||
.or_else(|| endpoint.strip_prefix("tcp://")),
|
||||
RouteDriver::NginxStreamUdp => endpoint.strip_prefix("rudp://"),
|
||||
}
|
||||
.with_context(|| format!("{what} transport does not match its route driver"))?;
|
||||
ensure!(
|
||||
!authority
|
||||
.chars()
|
||||
.any(|character| matches!(character, '/' | '?' | '#' | '@' | '[' | ']')),
|
||||
"{what} is not a plain IP address and port"
|
||||
);
|
||||
let (host, port) = authority
|
||||
.rsplit_once(':')
|
||||
.with_context(|| format!("{what} has no port"))?;
|
||||
let host = host
|
||||
.parse::<IpAddr>()
|
||||
.with_context(|| format!("{what} host is not an IP address"))?;
|
||||
ensure!(
|
||||
matches!(host, IpAddr::V4(_)),
|
||||
"route binding v2 endpoints use explicit IPv4 addresses"
|
||||
);
|
||||
let port = port
|
||||
.parse::<u16>()
|
||||
.with_context(|| format!("{what} port is not a u16"))?;
|
||||
ensure!(port > 0, "{what} port is zero");
|
||||
Ok((host, port))
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
|
|
@ -924,6 +985,14 @@ impl TargetDeclaration {
|
|||
== self.service.route_required,
|
||||
"routed services must require exactly one Idunn-owned candidate bind"
|
||||
);
|
||||
ensure!(
|
||||
!self
|
||||
.service
|
||||
.required_environment
|
||||
.contains(IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT)
|
||||
|| self.service.route_required,
|
||||
"only routed services may require the Idunn-owned member bind"
|
||||
);
|
||||
ensure!(
|
||||
self.service
|
||||
.required_environment
|
||||
|
|
@ -1381,7 +1450,35 @@ impl OperatorBinding {
|
|||
route.private_port_start < route.private_port_end,
|
||||
"candidate rollout requires at least two private ports"
|
||||
);
|
||||
route.stable_socket()?;
|
||||
let stable = route.stable_socket()?;
|
||||
if let Some(member) = route.member.as_ref() {
|
||||
ensure!(
|
||||
route.driver == RouteDriver::NginxStreamTcp,
|
||||
"only a TCP route may carry a member listener"
|
||||
);
|
||||
require_value(&member.stable_endpoint, "member endpoint")?;
|
||||
let member_stable = route
|
||||
.member_stable_socket()?
|
||||
.context("route has no member listener")?;
|
||||
ensure!(
|
||||
member_stable != stable,
|
||||
"member endpoint equals the route's stable endpoint"
|
||||
);
|
||||
ensure!(
|
||||
member.private_port_start > 0,
|
||||
"member private port range starts at zero"
|
||||
);
|
||||
let width = route.private_port_end - route.private_port_start;
|
||||
let member_end = member
|
||||
.private_port_start
|
||||
.checked_add(width)
|
||||
.context("member private port range overflows")?;
|
||||
ensure!(
|
||||
member_end < route.private_port_start
|
||||
|| member.private_port_start > route.private_port_end,
|
||||
"member private port range overlaps the route's private range"
|
||||
);
|
||||
}
|
||||
require_absolute_path(&route.config_path, "route config")?;
|
||||
require_unit(&route.reload_unit, "route reload unit")?;
|
||||
}
|
||||
|
|
@ -1559,12 +1656,7 @@ impl OperatorBinding {
|
|||
.keys()
|
||||
.chain(self.workload.secret_environment_names())
|
||||
.collect();
|
||||
for owned in [
|
||||
IDUNN_RUNTIME_BUNDLE_ENVIRONMENT,
|
||||
IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT,
|
||||
IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT,
|
||||
IDUNN_ACTIVATION_CREDENTIAL_ENVIRONMENT,
|
||||
] {
|
||||
for owned in IDUNN_OWNED_LAUNCH_ENVIRONMENT {
|
||||
ensure!(
|
||||
!workload_names.contains(&owned.to_string()),
|
||||
"operator binding cannot replace Idunn-owned launch environment {owned}"
|
||||
|
|
@ -1586,6 +1678,9 @@ impl OperatorBinding {
|
|||
}
|
||||
if declaration.service.route_required {
|
||||
available_environment.insert(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT.into());
|
||||
if self.route.as_ref().is_some_and(|route| route.member.is_some()) {
|
||||
available_environment.insert(IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT.into());
|
||||
}
|
||||
}
|
||||
if declaration.write_lease_required() {
|
||||
available_environment.insert(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT.into());
|
||||
|
|
@ -1933,10 +2028,10 @@ fn require_host(value: &str) -> Result<()> {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
|
||||
const RECIPE: &str = r#"
|
||||
pub(crate) const RECIPE: &str = r#"
|
||||
schema = "gamecult.idunn.target_declaration.v1"
|
||||
target = "test-service"
|
||||
source_stamp_environment = "TEST_SERVICE_BUILD_COMMIT"
|
||||
|
|
@ -2008,7 +2103,7 @@ compatibility = "v1"
|
|||
startup = "before-promotion"
|
||||
"#;
|
||||
|
||||
const BINDING: &str = r#"
|
||||
pub(crate) const BINDING: &str = r#"
|
||||
schema = "gamecult.idunn.operator_binding.v2"
|
||||
target = "test-service"
|
||||
profiles = ["aetheria", "full-gamecult"]
|
||||
|
|
@ -2235,6 +2330,166 @@ nodes = ["yggdrasil"]
|
|||
}
|
||||
}
|
||||
|
||||
/// The recipe and binding of a routed service whose members connect to a
|
||||
/// listener of their own.
|
||||
pub(crate) fn member_listener_pair() -> (String, String) {
|
||||
let recipe = RECIPE.replace(
|
||||
"required_environment = [",
|
||||
"required_environment = [\"GAMECULT_IDUNN_MEMBER_BIND\", ",
|
||||
);
|
||||
let binding = BINDING.replace(
|
||||
"reload_unit = \"nginx.service\"\n",
|
||||
"reload_unit = \"nginx.service\"\n\n[route.member]\nstable_endpoint = \"http://127.0.0.1:18840\"\nprivate_port_start = 18841\n",
|
||||
);
|
||||
(recipe, binding)
|
||||
}
|
||||
|
||||
fn member_binding_with(replacement: (&str, &str)) -> Result<OperatorBinding> {
|
||||
let (recipe, binding) = member_listener_pair();
|
||||
let recipe = TargetDeclaration::parse(&recipe)?;
|
||||
let binding = OperatorBinding::parse(&binding.replace(replacement.0, replacement.1))?;
|
||||
binding.admit(&recipe)?;
|
||||
Ok(binding)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_member_listener_admits_with_a_recipe_that_requires_the_member_bind() {
|
||||
let binding = member_binding_with(("never-matches", "")).unwrap();
|
||||
let route = binding.route.unwrap();
|
||||
assert_eq!(route.member_stable_socket().unwrap().unwrap().1, 18840);
|
||||
assert_eq!(route.member_candidate_port(18831).unwrap(), Some(18841));
|
||||
assert_eq!(route.member_candidate_port(18839).unwrap(), Some(18849));
|
||||
assert!(route.member_candidate_port(18840).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_binding_without_a_member_listener_has_no_member_ports() {
|
||||
let binding = OperatorBinding::parse(BINDING).unwrap();
|
||||
let route = binding.route.unwrap();
|
||||
assert_eq!(route.member_stable_socket().unwrap(), None);
|
||||
assert_eq!(route.member_candidate_port(18831).unwrap(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_member_listener_needs_a_recipe_that_requires_the_member_bind() {
|
||||
let (recipe, binding) = member_listener_pair();
|
||||
// Bound but not required: the recipe never asked for the variable.
|
||||
let plain_recipe = TargetDeclaration::parse(RECIPE).unwrap();
|
||||
assert!(
|
||||
OperatorBinding::parse(&binding)
|
||||
.unwrap()
|
||||
.admit(&plain_recipe)
|
||||
.is_err()
|
||||
);
|
||||
// Required but not bound: the variable would never be set.
|
||||
let recipe = TargetDeclaration::parse(&recipe).unwrap();
|
||||
assert!(
|
||||
OperatorBinding::parse(BINDING)
|
||||
.unwrap()
|
||||
.admit(&recipe)
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn only_a_routed_recipe_may_require_the_member_bind() {
|
||||
let recipe = RECIPE
|
||||
.replace("route_required = true", "route_required = false")
|
||||
.replace(
|
||||
"required_environment = [\"GAMECULT_IDUNN_CANDIDATE_BIND\", ",
|
||||
"required_environment = [\"GAMECULT_IDUNN_MEMBER_BIND\", ",
|
||||
);
|
||||
assert!(recipe.contains("GAMECULT_IDUNN_MEMBER_BIND"));
|
||||
assert!(TargetDeclaration::parse(&recipe).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn member_listener_bindings_are_validated() {
|
||||
let start = |value: &str| {
|
||||
member_binding_with((
|
||||
"private_port_start = 18841",
|
||||
&format!("private_port_start = {value}"),
|
||||
))
|
||||
};
|
||||
// Overlapping the route's own private range, in either direction.
|
||||
assert!(start("18835").is_err());
|
||||
assert!(start("18825").is_err());
|
||||
// Adjacent but disjoint is fine.
|
||||
assert!(start("18840").is_ok());
|
||||
// Zero, and a width that overflows u16.
|
||||
assert!(start("0").is_err());
|
||||
assert!(start("65530").is_err());
|
||||
// The member endpoint may not be the route's stable endpoint.
|
||||
assert!(member_binding_with(("http://127.0.0.1:18840", "http://127.0.0.1:18830")).is_err());
|
||||
// The same transport and address rules as the stable endpoint.
|
||||
assert!(member_binding_with(("http://127.0.0.1:18840", "rudp://127.0.0.1:18840")).is_err());
|
||||
assert!(member_binding_with(("http://127.0.0.1:18840", "http://example.test:18840")).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_udp_route_cannot_carry_a_member_listener() {
|
||||
let (recipe, binding) = member_listener_pair();
|
||||
let binding = binding
|
||||
.replace("nginx-stream-tcp", "nginx-stream-udp")
|
||||
.replace("http://127.0.0.1:18830", "rudp://127.0.0.1:18830")
|
||||
.replace("http://127.0.0.1:18840", "rudp://127.0.0.1:18840");
|
||||
let recipe = TargetDeclaration::parse(&recipe).unwrap();
|
||||
assert!(
|
||||
OperatorBinding::parse(&binding)
|
||||
.and_then(|binding| binding.admit(&recipe))
|
||||
.is_err()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn idunn_owned_names_are_reserved_in_one_list() {
|
||||
let recipe = TargetDeclaration::parse(RECIPE).unwrap();
|
||||
let owned = [
|
||||
"GAMECULT_IDUNN_RUNTIME_BUNDLE",
|
||||
"GAMECULT_IDUNN_CANDIDATE_BIND",
|
||||
"GAMECULT_IDUNN_MEMBER_BIND",
|
||||
"GAMECULT_IDUNN_PROCESS_WRITE_LEASE",
|
||||
"GAMECULT_IDUNN_ACTIVATION_CREDENTIAL",
|
||||
"CREDENTIALS_DIRECTORY",
|
||||
];
|
||||
let mut listed = IDUNN_OWNED_LAUNCH_ENVIRONMENT.to_vec();
|
||||
listed.sort_unstable();
|
||||
let mut expected = owned.to_vec();
|
||||
expected.sort_unstable();
|
||||
assert_eq!(listed, expected);
|
||||
|
||||
let as_environment = |name: &str| {
|
||||
BINDING.replace(
|
||||
"[workload.argument_bindings]",
|
||||
&format!("[workload.environment]\n{name} = \"value\"\n\n[workload.argument_bindings]"),
|
||||
)
|
||||
};
|
||||
let as_secret = |name: &str| {
|
||||
BINDING.replace(
|
||||
"[workload.secret_files]",
|
||||
&format!("[workload.secret_files]\n{name} = \"/etc/gamecult/test-service/other.cc\""),
|
||||
)
|
||||
};
|
||||
// The fixtures are well formed: an unowned name parses in both
|
||||
// shapes, so the refusals below are about the name.
|
||||
assert!(OperatorBinding::parse(&as_environment("UNOWNED_NAME")).is_ok());
|
||||
assert!(OperatorBinding::parse(&as_secret("UNOWNED_NAME")).is_ok());
|
||||
for name in owned {
|
||||
assert!(
|
||||
OperatorBinding::parse(&as_environment(name))
|
||||
.and_then(|binding| binding.admit(&recipe))
|
||||
.is_err(),
|
||||
"{name} accepted as environment"
|
||||
);
|
||||
assert!(
|
||||
OperatorBinding::parse(&as_secret(name))
|
||||
.and_then(|binding| binding.admit(&recipe))
|
||||
.is_err(),
|
||||
"{name} accepted as a secret file"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn state_slot_path_cannot_escape_the_operator_owned_root() {
|
||||
let input = RECIPE.replace(
|
||||
|
|
|
|||
256
src/drivers.rs
256
src/drivers.rs
|
|
@ -22,7 +22,8 @@ use crate::control_plane::SequenceAdmittedWarming;
|
|||
use crate::deployment::{
|
||||
ArtifactOutput, ArtifactSource, DockerRunnerBinding, GitlinkBinding,
|
||||
IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT, IDUNN_RUNTIME_BUNDLE_ENVIRONMENT,
|
||||
IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, LaunchArgument, OperatorBinding,
|
||||
IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT, IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT,
|
||||
LaunchArgument, OperatorBinding,
|
||||
RUNTIME_PRESENCE_IDENTITY_BINDING, RUNTIME_PRESENCE_IDENTITY_FD_NAME, RouteBinding,
|
||||
RouteDriver, SourceSelectionPolicy, TargetDeclaration, WorkloadNetwork,
|
||||
};
|
||||
|
|
@ -3377,10 +3378,32 @@ impl SystemdTransientWorkloadDriver {
|
|||
.is_none(),
|
||||
"operator binding attempts to replace the Idunn candidate bind"
|
||||
);
|
||||
let member_port = binding
|
||||
.route
|
||||
.as_ref()
|
||||
.map(|binding_route| binding_route.member_candidate_port(port))
|
||||
.transpose()?
|
||||
.flatten();
|
||||
match member_port {
|
||||
Some(member_port) => ensure!(
|
||||
environment
|
||||
.insert(
|
||||
IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT.into(),
|
||||
format!("{host}:{member_port}"),
|
||||
)
|
||||
.is_none(),
|
||||
"operator binding attempts to replace the Idunn member bind"
|
||||
),
|
||||
None => ensure!(
|
||||
!environment.contains_key(IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT),
|
||||
"workload without a member listener carries an Idunn member bind"
|
||||
),
|
||||
}
|
||||
}
|
||||
None => ensure!(
|
||||
!environment.contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT),
|
||||
"unrouted workload carries an Idunn candidate bind"
|
||||
!environment.contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT)
|
||||
&& !environment.contains_key(IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT),
|
||||
"unrouted workload carries an Idunn route bind"
|
||||
),
|
||||
}
|
||||
match &binding.process_write_lease {
|
||||
|
|
@ -5241,50 +5264,67 @@ impl NginxRouteDriver {
|
|||
}
|
||||
}
|
||||
|
||||
/// The one firewall rule this route owns: inbound to the stable endpoint's
|
||||
/// address and port, its transport's protocol, tagged with the route id.
|
||||
fn endpoint_rule(&self) -> Result<Vec<OsString>> {
|
||||
let (host, port) = self.binding.stable_socket()?;
|
||||
/// The firewall rules this route owns, one per stable socket (the
|
||||
/// presence endpoint, then the member endpoint when the binding has one):
|
||||
/// inbound to the socket's address and port, its transport's protocol.
|
||||
fn endpoint_rules(&self) -> Result<Vec<Vec<OsString>>> {
|
||||
let protocol = match self.binding.driver {
|
||||
RouteDriver::NginxStreamTcp => "tcp",
|
||||
RouteDriver::NginxStreamUdp => "udp",
|
||||
};
|
||||
Ok(vec![
|
||||
OsString::from("allow"),
|
||||
OsString::from("in"),
|
||||
OsString::from("to"),
|
||||
OsString::from(host.to_string()),
|
||||
OsString::from("port"),
|
||||
OsString::from(port.to_string()),
|
||||
OsString::from("proto"),
|
||||
OsString::from(protocol),
|
||||
])
|
||||
let mut sockets = vec![self.binding.stable_socket()?];
|
||||
sockets.extend(self.binding.member_stable_socket()?);
|
||||
Ok(sockets
|
||||
.into_iter()
|
||||
.map(|(host, port)| {
|
||||
vec![
|
||||
OsString::from("allow"),
|
||||
OsString::from("in"),
|
||||
OsString::from("to"),
|
||||
OsString::from(host.to_string()),
|
||||
OsString::from("port"),
|
||||
OsString::from(port.to_string()),
|
||||
OsString::from("proto"),
|
||||
OsString::from(protocol),
|
||||
]
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn admit_endpoint(&self) -> Result<()> {
|
||||
let mut args = self.endpoint_rule()?;
|
||||
args.push(OsString::from("comment"));
|
||||
args.push(OsString::from(format!(
|
||||
"Idunn route {}",
|
||||
self.binding.route_id
|
||||
)));
|
||||
self.command(&self.ufw_program, args)
|
||||
.context("admitting the stable endpoint on the host firewall")?;
|
||||
for mut args in self.endpoint_rules()? {
|
||||
args.push(OsString::from("comment"));
|
||||
args.push(OsString::from(format!(
|
||||
"Idunn route {}",
|
||||
self.binding.route_id
|
||||
)));
|
||||
self.command(&self.ufw_program, args)
|
||||
.context("admitting a route endpoint on the host firewall")?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn withdraw_endpoint(&self) -> Result<()> {
|
||||
let mut args = vec![OsString::from("delete")];
|
||||
args.extend(self.endpoint_rule()?);
|
||||
match self.command(&self.ufw_program, args) {
|
||||
Ok(_) => Ok(()),
|
||||
// ufw reports a rule that is already gone as a failure; for a
|
||||
// withdrawal that is the state being asked for.
|
||||
Err(error) if format!("{error:#}").contains("non-existent") => Ok(()),
|
||||
Err(error) => {
|
||||
Err(error).context("withdrawing the stable endpoint from the host firewall")
|
||||
let mut first_error = None;
|
||||
for rule in self.endpoint_rules()? {
|
||||
let mut args = vec![OsString::from("delete")];
|
||||
args.extend(rule);
|
||||
match self.command(&self.ufw_program, args) {
|
||||
Ok(_) => {}
|
||||
// ufw reports a rule that is already gone as a failure; for a
|
||||
// withdrawal that is the state being asked for.
|
||||
Err(error) if format!("{error:#}").contains("non-existent") => {}
|
||||
Err(error) => {
|
||||
first_error.get_or_insert(
|
||||
error.context("withdrawing a route endpoint from the host firewall"),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
match first_error {
|
||||
Some(error) => Err(error),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn command<I, S>(&self, program: &Path, args: I) -> Result<Output>
|
||||
|
|
@ -5345,8 +5385,20 @@ impl NginxRouteDriver {
|
|||
RouteDriver::NginxStreamTcp => {
|
||||
let (stable_host, stable_port) =
|
||||
endpoint_host_port(&route.stable_endpoint, endpoint_prefix)?;
|
||||
let member_blocks = match (
|
||||
self.binding.member_stable_socket()?,
|
||||
self.binding.member_candidate_port(candidate_port)?,
|
||||
) {
|
||||
(Some((member_host, member_stable_port)), Some(member_port)) => {
|
||||
let member_upstream = format!("{upstream}_member");
|
||||
format!(
|
||||
"upstream {member_upstream} {{\n server {candidate_host}:{member_port};\n}}\nserver {{\n listen {member_host}:{member_stable_port};\n proxy_pass {member_upstream};\n}}\n"
|
||||
)
|
||||
}
|
||||
_ => String::new(),
|
||||
};
|
||||
format!(
|
||||
"# Idunn Expected {expected_projection_sha256}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n}}\nserver {{\n listen {stable_host}:{stable_port};\n proxy_pass {upstream};\n}}\n"
|
||||
"# Idunn Expected {expected_projection_sha256}\nupstream {upstream} {{\n server {candidate_host}:{candidate_port};\n}}\nserver {{\n listen {stable_host}:{stable_port};\n proxy_pass {upstream};\n}}\n{member_blocks}"
|
||||
)
|
||||
}
|
||||
RouteDriver::NginxStreamUdp => {
|
||||
|
|
@ -8366,7 +8418,7 @@ fn ensure_source_directory(path: &Path, identity: Option<ProcessIdentity>) -> Re
|
|||
}
|
||||
}
|
||||
|
||||
fn authority_lock_path(path: &Path) -> PathBuf {
|
||||
pub(crate) fn authority_lock_path(path: &Path) -> PathBuf {
|
||||
let mut lock = path.as_os_str().to_os_string();
|
||||
lock.push(".lock");
|
||||
PathBuf::from(lock)
|
||||
|
|
@ -8698,6 +8750,7 @@ mod tests {
|
|||
private_port_end: port,
|
||||
config_path: temp.path().join("service.conf"),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
})
|
||||
.request_candidate_runtime_presence(&candidate, "candidate-probe")
|
||||
}
|
||||
|
|
@ -9521,6 +9574,7 @@ Content-Le".to_vec()), |_| {}),
|
|||
private_port_end: 17998,
|
||||
config_path: temp.path().join("service.conf"),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
});
|
||||
let mut candidate = expected();
|
||||
candidate.route = Some(cultnet_rs::IdunnExpectedRoute {
|
||||
|
|
@ -12253,6 +12307,7 @@ processes 9
|
|||
private_port_end: 17979,
|
||||
config_path: temp.path().join("odin.conf"),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
},
|
||||
nginx_program: program("nginx", "#!/bin/sh\nexit 0\n")?,
|
||||
systemd_run_program: program("systemd-run", "#!/bin/sh\nexit 64\n")?,
|
||||
|
|
@ -12262,12 +12317,15 @@ processes 9
|
|||
};
|
||||
assert_eq!(
|
||||
driver
|
||||
.endpoint_rule()?
|
||||
.endpoint_rules()?
|
||||
.iter()
|
||||
.map(|value| value.to_string_lossy().into_owned())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "),
|
||||
"allow in to 10.77.0.1 port 17971 proto udp"
|
||||
.map(|rule| rule
|
||||
.iter()
|
||||
.map(|value| value.to_string_lossy().into_owned())
|
||||
.collect::<Vec<_>>()
|
||||
.join(" "))
|
||||
.collect::<Vec<_>>(),
|
||||
vec!["allow in to 10.77.0.1 port 17971 proto udp"]
|
||||
);
|
||||
|
||||
driver.restore(None, &Unmetered)?;
|
||||
|
|
@ -12327,6 +12385,7 @@ processes 9
|
|||
private_port_end: 4109,
|
||||
config_path: temp.join("service.conf"),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
},
|
||||
&RouteActuators {
|
||||
nginx: program("nginx")?,
|
||||
|
|
@ -12361,6 +12420,117 @@ processes 9
|
|||
})
|
||||
}
|
||||
|
||||
fn with_member_listener(driver: &mut NginxRouteDriver) {
|
||||
driver.binding.member = Some(crate::deployment::MemberListenerBinding {
|
||||
stable_endpoint: "http://127.0.0.1:4120".into(),
|
||||
private_port_start: 4121,
|
||||
});
|
||||
}
|
||||
|
||||
/// The fragment as the base commit rendered it, computed from its format
|
||||
/// string and not from the code under test.
|
||||
#[cfg(unix)]
|
||||
fn base_fragment(sha: &str, candidate_port: u16) -> String {
|
||||
format!(
|
||||
"# Idunn Expected {sha}\nupstream idunn_service_route {{\n server 127.0.0.1:{candidate_port};\n}}\nserver {{\n listen 127.0.0.1:4103;\n proxy_pass idunn_service_route;\n}}\n"
|
||||
)
|
||||
}
|
||||
|
||||
/// A route without a member listener renders the bytes it always did; a
|
||||
/// member listener adds a second upstream and server block to the one
|
||||
/// fragment, under the one Expected comment.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn a_member_listener_renders_a_second_block_only_when_bound() -> Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
let (mut driver, mut candidate, _calls) = stubbed_route_driver(temp.path())?;
|
||||
let sha = candidate.canonical_sha256()?;
|
||||
assert_eq!(
|
||||
String::from_utf8(driver.render(&candidate)?)?,
|
||||
base_fragment(&sha, 4104)
|
||||
);
|
||||
|
||||
with_member_listener(&mut driver);
|
||||
let member_blocks = |member_port: u16| {
|
||||
format!(
|
||||
"upstream idunn_service_route_member {{\n server 127.0.0.1:{member_port};\n}}\nserver {{\n listen 127.0.0.1:4120;\n proxy_pass idunn_service_route_member;\n}}\n"
|
||||
)
|
||||
};
|
||||
let rendered = String::from_utf8(driver.render(&candidate)?)?;
|
||||
assert_eq!(rendered, base_fragment(&sha, 4104) + &member_blocks(4121));
|
||||
assert_eq!(rendered.matches("# Idunn Expected").count(), 1);
|
||||
assert_eq!(rendered.matches("server {").count(), 2);
|
||||
|
||||
candidate.route.as_mut().unwrap().candidate_endpoint = "http://127.0.0.1:4106".into();
|
||||
let sha = candidate.canonical_sha256()?;
|
||||
assert_eq!(
|
||||
String::from_utf8(driver.render(&candidate)?)?,
|
||||
base_fragment(&sha, 4106) + &member_blocks(4123)
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Both stable sockets are admitted on the firewall, and both withdrawn.
|
||||
#[cfg(unix)]
|
||||
#[test]
|
||||
fn both_route_endpoints_are_admitted_and_withdrawn() -> Result<()> {
|
||||
let temp = tempfile::tempdir()?;
|
||||
let (mut driver, _candidate, calls) = stubbed_route_driver(temp.path())?;
|
||||
with_member_listener(&mut driver);
|
||||
driver.admit_endpoint()?;
|
||||
driver.withdraw_endpoint()?;
|
||||
let log = fs::read_to_string(&calls)?;
|
||||
let ufw: Vec<&str> = log.lines().filter(|line| line.starts_with("ufw ")).collect();
|
||||
assert_eq!(
|
||||
ufw,
|
||||
vec![
|
||||
"ufw allow in to 127.0.0.1 port 4103 proto tcp comment Idunn route service-route",
|
||||
"ufw allow in to 127.0.0.1 port 4120 proto tcp comment Idunn route service-route",
|
||||
"ufw delete allow in to 127.0.0.1 port 4103 proto tcp",
|
||||
"ufw delete allow in to 127.0.0.1 port 4120 proto tcp",
|
||||
]
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Idunn sets the member bind for a bound member listener, derived from the
|
||||
/// candidate, and for no other workload.
|
||||
#[test]
|
||||
fn the_member_bind_is_set_only_for_a_bound_member_listener() -> Result<()> {
|
||||
use crate::deployment::tests::{BINDING, RECIPE, member_listener_pair};
|
||||
|
||||
let driver = SystemdTransientWorkloadDriver::default();
|
||||
let mut routed = expected();
|
||||
routed.route = Some(cultnet_rs::IdunnExpectedRoute {
|
||||
route_id: "test-service-public".into(),
|
||||
transport: "http".into(),
|
||||
stable_endpoint: "http://127.0.0.1:18830".into(),
|
||||
candidate_endpoint: "http://127.0.0.1:18833".into(),
|
||||
});
|
||||
let bundle = Path::new("/etc/gamecult/test-service/runtime/bundle");
|
||||
let unit = "idunn-test-service-abc";
|
||||
|
||||
let (recipe, binding) = member_listener_pair();
|
||||
let binding = OperatorBinding::parse(&binding)?;
|
||||
binding.admit(&TargetDeclaration::parse(&recipe)?)?;
|
||||
let environment = driver.launch_environment(&binding, bundle, &routed, unit)?;
|
||||
assert_eq!(
|
||||
environment.get(IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT).map(String::as_str),
|
||||
Some("127.0.0.1:18843")
|
||||
);
|
||||
assert_eq!(
|
||||
environment.get(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT).map(String::as_str),
|
||||
Some("127.0.0.1:18833")
|
||||
);
|
||||
|
||||
let plain = OperatorBinding::parse(BINDING)?;
|
||||
plain.admit(&TargetDeclaration::parse(RECIPE)?)?;
|
||||
let environment = driver.launch_environment(&plain, bundle, &routed, unit)?;
|
||||
assert!(!environment.contains_key(IDUNN_RUNTIME_MEMBER_BIND_ENVIRONMENT));
|
||||
assert!(environment.contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A refused forward change stops before any host mutation: no program
|
||||
/// runs, no fragment is written, and no private-mount unit starts.
|
||||
#[cfg(unix)]
|
||||
|
|
@ -12680,6 +12850,7 @@ processes 9
|
|||
private_port_end: 4109,
|
||||
config_path: config.clone(),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
},
|
||||
nginx_program: program("nginx")?,
|
||||
systemd_run_program: program("systemd-run")?,
|
||||
|
|
@ -12740,6 +12911,7 @@ processes 9
|
|||
private_port_end: 27879,
|
||||
config_path: config_path.clone(),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
});
|
||||
assert_eq!(driver.current_configuration()?, None);
|
||||
// What systemd leaves behind when it materializes the preflight's bind
|
||||
|
|
@ -12766,6 +12938,7 @@ processes 9
|
|||
private_port_end: 27879,
|
||||
config_path: "/etc/nginx/idunn-stream-routes/odin-rudp.conf".into(),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
});
|
||||
let mut candidate = expected();
|
||||
candidate.route = Some(cultnet_rs::IdunnExpectedRoute {
|
||||
|
|
@ -12827,6 +13000,7 @@ processes 9
|
|||
private_port_end: 4109,
|
||||
config_path: config.clone(),
|
||||
reload_unit: "nginx.service".into(),
|
||||
member: None,
|
||||
};
|
||||
let driver = NginxRouteDriver {
|
||||
binding,
|
||||
|
|
|
|||
Loading…
Reference in a new issue