Rebuild Idunn deployment transaction authority
This commit is contained in:
parent
676f6eeb80
commit
1fc0dadec3
4 changed files with 6644 additions and 952 deletions
4171
src/control_plane.rs
4171
src/control_plane.rs
File diff suppressed because it is too large
Load diff
|
|
@ -1,12 +1,14 @@
|
|||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
|
||||
use anyhow::{Context, Result, bail, ensure};
|
||||
use anyhow::{bail, ensure, Context, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const TARGET_DECLARATION_SCHEMA: &str = "gamecult.idunn.target_declaration.v1";
|
||||
pub const OPERATOR_BINDING_SCHEMA: &str = "gamecult.idunn.operator_binding.v1";
|
||||
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_PROCESS_WRITE_LEASE_ENVIRONMENT: &str = "GAMECULT_IDUNN_PROCESS_WRITE_LEASE";
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
|
|
@ -172,6 +174,9 @@ pub struct MigrationDeclaration {
|
|||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum StateKind {
|
||||
CultcacheFile,
|
||||
/// Process-owned state imposed by an external runtime contract. Idunn
|
||||
/// preserves and fences the file but never interprets its payload.
|
||||
ExternalFile,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
|
|
@ -332,8 +337,8 @@ pub enum RunnerDriver {
|
|||
#[serde(deny_unknown_fields)]
|
||||
pub struct WorkloadBinding {
|
||||
pub driver: WorkloadDriver,
|
||||
pub user: String,
|
||||
pub group: String,
|
||||
#[serde(default)]
|
||||
pub state_group: Option<String>,
|
||||
pub unit_prefix: String,
|
||||
pub release_root: PathBuf,
|
||||
#[serde(default)]
|
||||
|
|
@ -346,8 +351,6 @@ pub struct WorkloadBinding {
|
|||
#[serde(default)]
|
||||
pub read_write_paths: BTreeSet<PathBuf>,
|
||||
#[serde(default)]
|
||||
pub capabilities: BTreeSet<String>,
|
||||
#[serde(default)]
|
||||
pub devices: BTreeSet<PathBuf>,
|
||||
pub memory_mebibytes: u32,
|
||||
pub cpu_quota_percent: u32,
|
||||
|
|
@ -590,6 +593,20 @@ impl TargetDeclaration {
|
|||
.contains(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT),
|
||||
"service does not require the standard Idunn runtime bundle"
|
||||
);
|
||||
ensure!(
|
||||
self.service
|
||||
.required_environment
|
||||
.contains(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT)
|
||||
== self.service.route_required,
|
||||
"routed services must require exactly one Idunn-owned candidate bind"
|
||||
);
|
||||
ensure!(
|
||||
self.service
|
||||
.required_environment
|
||||
.contains(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT)
|
||||
== self.write_lease_required(),
|
||||
"state-writing services must require exactly one Idunn-owned process write lease"
|
||||
);
|
||||
for name in &self.service.optional_environment {
|
||||
require_environment_name(name)?;
|
||||
ensure!(
|
||||
|
|
@ -847,13 +864,19 @@ impl OperatorBinding {
|
|||
"runner {id} secret files and secret-read affordance disagree"
|
||||
);
|
||||
}
|
||||
require_identity(&self.workload.user, "workload user")?;
|
||||
require_identity(&self.workload.group, "workload group")?;
|
||||
let needs_state_group =
|
||||
self.workload.state_root.is_some() || !self.workload.read_write_paths.is_empty();
|
||||
ensure!(
|
||||
!matches!(self.workload.user.as_str(), "root" | "0")
|
||||
&& !matches!(self.workload.group.as_str(), "root" | "0"),
|
||||
"ordinary Idunn workloads cannot run as root"
|
||||
self.workload.state_group.is_some() == needs_state_group,
|
||||
"a fixed state group must exist exactly when the dynamic workload has writable paths"
|
||||
);
|
||||
if let Some(group) = &self.workload.state_group {
|
||||
require_identity(group, "workload state group")?;
|
||||
ensure!(
|
||||
!matches!(group.as_str(), "root" | "0"),
|
||||
"ordinary Idunn workload state cannot use the root group"
|
||||
);
|
||||
}
|
||||
require_id(&self.workload.unit_prefix, "workload unit prefix")?;
|
||||
require_id(&self.runtime_identity.runtime_id, "runtime id")?;
|
||||
require_id(
|
||||
|
|
@ -952,9 +975,6 @@ impl OperatorBinding {
|
|||
);
|
||||
}
|
||||
}
|
||||
for capability in &self.workload.capabilities {
|
||||
require_value(capability, "workload capability")?;
|
||||
}
|
||||
for name in self
|
||||
.workload
|
||||
.environment
|
||||
|
|
@ -962,6 +982,11 @@ impl OperatorBinding {
|
|||
.chain(self.workload.secret_files.keys())
|
||||
{
|
||||
require_environment_name(name)?;
|
||||
ensure!(
|
||||
name != IDUNN_RUNTIME_BUNDLE_ENVIRONMENT
|
||||
&& name != IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT,
|
||||
"operator binding attempts to replace Idunn-owned workload environment"
|
||||
);
|
||||
}
|
||||
for value in self.workload.environment.values() {
|
||||
require_value(value, "workload environment value")?;
|
||||
|
|
@ -996,11 +1021,11 @@ impl OperatorBinding {
|
|||
);
|
||||
ensure!(
|
||||
self.placement.desired_replicas == 1,
|
||||
"operator binding v1 admits exactly one replica"
|
||||
"operator binding v2 admits exactly one replica"
|
||||
);
|
||||
ensure!(
|
||||
self.placement.nodes.len() == 1,
|
||||
"operator binding v1 admits exactly one node"
|
||||
"operator binding v2 admits exactly one node"
|
||||
);
|
||||
for node in &self.placement.nodes {
|
||||
require_id(node, "placement node")?;
|
||||
|
|
@ -1154,8 +1179,27 @@ impl OperatorBinding {
|
|||
&& !self
|
||||
.workload
|
||||
.secret_files
|
||||
.contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT),
|
||||
"operator binding cannot replace the standard Idunn runtime bundle"
|
||||
.contains_key(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT)
|
||||
&& !self
|
||||
.workload
|
||||
.environment
|
||||
.contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT)
|
||||
&& !self
|
||||
.workload
|
||||
.secret_files
|
||||
.contains_key(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT),
|
||||
"operator binding cannot replace Idunn-owned launch environment"
|
||||
);
|
||||
ensure!(
|
||||
!self
|
||||
.workload
|
||||
.environment
|
||||
.contains_key(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT)
|
||||
&& !self
|
||||
.workload
|
||||
.secret_files
|
||||
.contains_key(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT),
|
||||
"operator binding cannot replace the Idunn-owned process write lease"
|
||||
);
|
||||
let mut available_environment: BTreeSet<_> = self
|
||||
.workload
|
||||
|
|
@ -1165,6 +1209,12 @@ impl OperatorBinding {
|
|||
.cloned()
|
||||
.collect();
|
||||
available_environment.insert(IDUNN_RUNTIME_BUNDLE_ENVIRONMENT.into());
|
||||
if declaration.service.route_required {
|
||||
available_environment.insert(IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT.into());
|
||||
}
|
||||
if declaration.write_lease_required() {
|
||||
available_environment.insert(IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT.into());
|
||||
}
|
||||
ensure!(
|
||||
declaration
|
||||
.service
|
||||
|
|
@ -1193,6 +1243,22 @@ impl OperatorBinding {
|
|||
.collect();
|
||||
let bound_arguments: BTreeSet<_> =
|
||||
self.workload.argument_bindings.keys().cloned().collect();
|
||||
match (
|
||||
self.workload.state_root.as_ref(),
|
||||
self.workload.argument_bindings.get("state_root"),
|
||||
) {
|
||||
(Some(state_root), Some(argument)) => ensure!(
|
||||
Path::new(argument) == state_root.as_path(),
|
||||
"state_root argument binding differs from the admitted workload state root"
|
||||
),
|
||||
(Some(_), None) => bail!("stateful workload has no state_root argument binding"),
|
||||
(None, Some(_)) => bail!("stateless workload carries a state_root argument binding"),
|
||||
(None, None) => {}
|
||||
}
|
||||
ensure!(
|
||||
required_arguments.contains("state_root") == self.workload.state_root.is_some(),
|
||||
"recipe state_root argument must exist exactly when the workload has a state root"
|
||||
);
|
||||
ensure!(
|
||||
required_arguments == bound_arguments,
|
||||
"operator argument bindings do not exactly match the launch contract"
|
||||
|
|
@ -1503,7 +1569,7 @@ arguments = [
|
|||
]
|
||||
transport = "http"
|
||||
route_required = true
|
||||
required_environment = ["GAMECULT_IDUNN_RUNTIME_BUNDLE", "TEST_SERVICE_BIND"]
|
||||
required_environment = ["GAMECULT_IDUNN_CANDIDATE_BIND", "GAMECULT_IDUNN_PROCESS_WRITE_LEASE", "GAMECULT_IDUNN_RUNTIME_BUNDLE"]
|
||||
|
||||
[service.health]
|
||||
contract = "test-service.cultnet-service-health"
|
||||
|
|
@ -1534,7 +1600,7 @@ startup = "before-promotion"
|
|||
"#;
|
||||
|
||||
const BINDING: &str = r#"
|
||||
schema = "gamecult.idunn.operator_binding.v1"
|
||||
schema = "gamecult.idunn.operator_binding.v2"
|
||||
target = "test-service"
|
||||
profiles = ["aetheria", "full-gamecult"]
|
||||
|
||||
|
|
@ -1561,8 +1627,7 @@ tmpfs_mebibytes = 1024
|
|||
|
||||
[workload]
|
||||
driver = "systemd-transient"
|
||||
user = "test-service"
|
||||
group = "test-service"
|
||||
state_group = "test-service"
|
||||
unit_prefix = "idunn-test-service"
|
||||
release_root = "/srv/test-service/releases"
|
||||
state_root = "/var/lib/gamecult/test-service"
|
||||
|
|
@ -1572,9 +1637,6 @@ hardening = "strict"
|
|||
memory_mebibytes = 2048
|
||||
cpu_quota_percent = 200
|
||||
|
||||
[workload.environment]
|
||||
TEST_SERVICE_BIND = "idunn.private_endpoint"
|
||||
|
||||
[workload.argument_bindings]
|
||||
state_root = "/var/lib/gamecult/test-service"
|
||||
|
||||
|
|
@ -1693,13 +1755,33 @@ nodes = ["yggdrasil"]
|
|||
fn extra_operator_environment_is_rejected() {
|
||||
let recipe = TargetDeclaration::parse(RECIPE).unwrap();
|
||||
let input = BINDING.replace(
|
||||
"TEST_SERVICE_BIND = \"idunn.private_endpoint\"",
|
||||
"TEST_SERVICE_BIND = \"idunn.private_endpoint\"\nUNDECLARED = \"no\"",
|
||||
"[workload.argument_bindings]",
|
||||
"[workload.environment]\nUNDECLARED = \"no\"\n\n[workload.argument_bindings]",
|
||||
);
|
||||
let binding = OperatorBinding::parse(&input).unwrap();
|
||||
assert!(binding.admit(&recipe).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn operator_cannot_replace_idunn_owned_launch_inputs() {
|
||||
let recipe = TargetDeclaration::parse(RECIPE).unwrap();
|
||||
for name in [
|
||||
IDUNN_RUNTIME_BUNDLE_ENVIRONMENT,
|
||||
IDUNN_RUNTIME_CANDIDATE_BIND_ENVIRONMENT,
|
||||
IDUNN_PROCESS_WRITE_LEASE_ENVIRONMENT,
|
||||
] {
|
||||
let input = BINDING.replace(
|
||||
"[workload.argument_bindings]",
|
||||
&format!(
|
||||
"[workload.environment]\n{name} = \"forged\"\n\n[workload.argument_bindings]"
|
||||
),
|
||||
);
|
||||
let rejected = OperatorBinding::parse(&input)
|
||||
.and_then(|binding| binding.admit(&recipe).map(|_| ()));
|
||||
assert!(rejected.is_err());
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn state_slot_path_cannot_escape_the_operator_owned_root() {
|
||||
let input = RECIPE.replace(
|
||||
|
|
@ -1709,6 +1791,28 @@ nodes = ["yggdrasil"]
|
|||
assert!(TargetDeclaration::parse(&input).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mismatched_state_root_argument_binding_is_rejected() {
|
||||
let recipe = TargetDeclaration::parse(RECIPE).unwrap();
|
||||
let mismatched = BINDING.replace(
|
||||
"[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/test-service\"",
|
||||
"[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/another-service\"",
|
||||
);
|
||||
let binding = OperatorBinding::parse(&mismatched).unwrap();
|
||||
assert!(binding.admit(&recipe).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn external_file_state_keeps_process_bound_writer_and_lease_semantics() {
|
||||
let input = RECIPE.replace("kind = \"cultcache-file\"", "kind = \"external-file\"");
|
||||
let declaration = TargetDeclaration::parse(&input).unwrap();
|
||||
let slot = &declaration.state.as_ref().unwrap().slots[0];
|
||||
assert_eq!(slot.kind, StateKind::ExternalFile);
|
||||
assert_eq!(slot.writer, StateWriter::ProcessBoundSingleWriter);
|
||||
assert_eq!(slot.startup, StateStartup::CreateOrOpenAfterWriteLease);
|
||||
assert!(declaration.write_lease_required());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn state_slots_cannot_claim_overlapping_paths() {
|
||||
let input = RECIPE.replace(
|
||||
|
|
@ -1721,27 +1825,49 @@ nodes = ["yggdrasil"]
|
|||
#[test]
|
||||
fn workload_mount_cannot_bypass_write_lease_controlled_state() {
|
||||
let input = BINDING.replace(
|
||||
"cpu_quota_percent = 200\n\n[workload.environment]",
|
||||
"cpu_quota_percent = 200\nread_write_paths = [\"/var/lib/gamecult/test-service/world\"]\n\n[workload.environment]",
|
||||
"cpu_quota_percent = 200\n\n[workload.argument_bindings]",
|
||||
"cpu_quota_percent = 200\nread_write_paths = [\"/var/lib/gamecult/test-service/world\"]\n\n[workload.argument_bindings]",
|
||||
);
|
||||
assert!(OperatorBinding::parse(&input).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v1_binding_is_explicitly_singleton() {
|
||||
assert!(
|
||||
OperatorBinding::parse(
|
||||
&BINDING.replace("desired_replicas = 1", "desired_replicas = 2")
|
||||
)
|
||||
.is_err()
|
||||
fn v2_binding_is_explicitly_singleton() {
|
||||
assert!(OperatorBinding::parse(
|
||||
&BINDING.replace("desired_replicas = 1", "desired_replicas = 2")
|
||||
)
|
||||
.is_err());
|
||||
assert!(OperatorBinding::parse(&BINDING.replace(
|
||||
"nodes = [\"yggdrasil\"]",
|
||||
"nodes = [\"yggdrasil\", \"raven\"]"
|
||||
))
|
||||
.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn legacy_static_workload_identity_has_no_authority_in_v2() {
|
||||
assert!(OperatorBinding::parse(&BINDING.replace(
|
||||
"schema = \"gamecult.idunn.operator_binding.v2\"",
|
||||
"schema = \"gamecult.idunn.operator_binding.v1\"",
|
||||
),)
|
||||
.is_err());
|
||||
let with_static_identity = BINDING.replace(
|
||||
"driver = \"systemd-transient\"",
|
||||
"driver = \"systemd-transient\"\nuser = \"test-service\"\ngroup = \"test-service\"",
|
||||
);
|
||||
assert!(OperatorBinding::parse(&with_static_identity).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn writable_state_requires_one_non_root_state_group() {
|
||||
assert!(
|
||||
OperatorBinding::parse(&BINDING.replace(
|
||||
"nodes = [\"yggdrasil\"]",
|
||||
"nodes = [\"yggdrasil\", \"raven\"]"
|
||||
))
|
||||
.is_err()
|
||||
OperatorBinding::parse(&BINDING.replace("state_group = \"test-service\"\n", ""),)
|
||||
.is_err()
|
||||
);
|
||||
assert!(OperatorBinding::parse(
|
||||
&BINDING.replace("state_group = \"test-service\"", "state_group = \"root\""),
|
||||
)
|
||||
.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -1773,7 +1899,8 @@ nodes = ["yggdrasil"]
|
|||
.replace(
|
||||
"startup = \"create-or-open-after-write-lease\"",
|
||||
"startup = \"open-at-start\"",
|
||||
),
|
||||
)
|
||||
.replace("\"GAMECULT_IDUNN_PROCESS_WRITE_LEASE\", ", ""),
|
||||
)
|
||||
.unwrap();
|
||||
binding.admit(&read_only_recipe).unwrap();
|
||||
|
|
@ -1792,6 +1919,7 @@ nodes = ["yggdrasil"]
|
|||
"arguments = [\n { kind = \"literal\", value = \"--state-root\" },\n { kind = \"binding\", name = \"state_root\" },\n]",
|
||||
"arguments = []",
|
||||
);
|
||||
recipe_text = recipe_text.replace("\"GAMECULT_IDUNN_PROCESS_WRITE_LEASE\", ", "");
|
||||
let state_start = recipe_text.find("\n[state]\n").unwrap();
|
||||
let state_end = recipe_text.find("\n[[provides]]\n").unwrap();
|
||||
recipe_text.replace_range(state_start..state_end, "");
|
||||
|
|
@ -1799,6 +1927,7 @@ nodes = ["yggdrasil"]
|
|||
assert!(recipe.state.is_none());
|
||||
|
||||
let binding_text = BINDING
|
||||
.replace("state_group = \"test-service\"\n", "")
|
||||
.replace("state_root = \"/var/lib/gamecult/test-service\"\n", "")
|
||||
.replace(
|
||||
"[workload.argument_bindings]\nstate_root = \"/var/lib/gamecult/test-service\"\n\n",
|
||||
|
|
|
|||
|
|
@ -1,23 +1,24 @@
|
|||
use std::collections::{BTreeMap, BTreeSet};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{Context, Result, bail, ensure};
|
||||
use anyhow::{bail, ensure, Context, Result};
|
||||
pub use cultnet_rs::IdunnExpectedIncarnationRecord as ExpectedIncarnation;
|
||||
use cultnet_rs::{
|
||||
GameCultRuntimeCapability, IDUNN_EXPECTED_INCARNATION_SCHEMA, IdunnExpectedDependency,
|
||||
IdunnExpectedRoute,
|
||||
IdunnExpectedCapability, IdunnExpectedDependency, IdunnExpectedRoute,
|
||||
IDUNN_EXPECTED_INCARNATION_SCHEMA,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::control_plane::SequenceAdmittedReady;
|
||||
use crate::deployment::{
|
||||
CapabilityDependency, DependencyKind, ExternalCapabilityBinding, OperatorBinding,
|
||||
ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration, TargetDeclaration,
|
||||
capability_compatible,
|
||||
capability_compatible, CapabilityDependency, DependencyKind, ExternalCapabilityBinding,
|
||||
OperatorBinding, ServiceTransport, SourceSelectionPolicy, StartupOrder, StateDeclaration,
|
||||
TargetDeclaration,
|
||||
};
|
||||
|
||||
pub const SOURCE_SELECTION_FACTS_SCHEMA: &str = "idunn.source_selection_facts.v1";
|
||||
pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v1";
|
||||
pub const COMPILED_DEPLOYMENT_PLAN_SCHEMA: &str = "idunn.compiled_deployment_plan.v2";
|
||||
pub const SEALED_RELEASE_SCHEMA: &str = "idunn.sealed_release.v1";
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
|
|
@ -132,13 +133,15 @@ impl SourceSelectionFacts {
|
|||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(tag = "kind", rename_all = "kebab-case", deny_unknown_fields)]
|
||||
pub enum ExpectedProviderAuthority {
|
||||
ManagedIncarnation {
|
||||
pub enum DependencyProviderAuthority {
|
||||
ManagedReady {
|
||||
target: String,
|
||||
incarnation_id: String,
|
||||
plan_id: String,
|
||||
sealed_release_id: String,
|
||||
expected_projection_sha256: String,
|
||||
odin_topology_correlation_sha256: String,
|
||||
odin_topology_publisher_sequence: u64,
|
||||
},
|
||||
ExternalOperatorBinding {
|
||||
binding_target: String,
|
||||
|
|
@ -147,9 +150,9 @@ pub enum ExpectedProviderAuthority {
|
|||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct ExpectedProviderRef {
|
||||
pub struct DependencyProviderRef {
|
||||
pub provider_id: String,
|
||||
pub authority: ExpectedProviderAuthority,
|
||||
pub authority: DependencyProviderAuthority,
|
||||
pub capability: String,
|
||||
pub schema: String,
|
||||
pub compatibility: String,
|
||||
|
|
@ -157,29 +160,39 @@ pub struct ExpectedProviderRef {
|
|||
pub endpoint: Option<String>,
|
||||
}
|
||||
|
||||
impl ExpectedProviderRef {
|
||||
impl DependencyProviderRef {
|
||||
pub fn validate(&self) -> Result<()> {
|
||||
require_token(&self.provider_id, "expected provider id")?;
|
||||
require_token(&self.provider_id, "dependency provider id")?;
|
||||
require_contract(&self.capability, &self.schema, &self.compatibility)?;
|
||||
ensure!(self.capacity > 0, "expected provider capacity is zero");
|
||||
ensure!(self.capacity > 0, "dependency provider capacity is zero");
|
||||
if let Some(endpoint) = &self.endpoint {
|
||||
require_value(endpoint, "expected provider endpoint")?;
|
||||
require_value(endpoint, "dependency provider endpoint")?;
|
||||
}
|
||||
match &self.authority {
|
||||
ExpectedProviderAuthority::ManagedIncarnation {
|
||||
DependencyProviderAuthority::ManagedReady {
|
||||
target,
|
||||
incarnation_id,
|
||||
plan_id,
|
||||
sealed_release_id,
|
||||
expected_projection_sha256,
|
||||
odin_topology_correlation_sha256,
|
||||
odin_topology_publisher_sequence,
|
||||
} => {
|
||||
require_token(target, "managed provider target")?;
|
||||
require_token(incarnation_id, "managed provider incarnation")?;
|
||||
require_sha256(plan_id, "managed provider plan")?;
|
||||
require_sha256(sealed_release_id, "managed provider sealed release")?;
|
||||
require_sha256(expected_projection_sha256, "managed expected projection")?;
|
||||
require_sha256(
|
||||
odin_topology_correlation_sha256,
|
||||
"managed Odin topology correlation",
|
||||
)?;
|
||||
ensure!(
|
||||
*odin_topology_publisher_sequence > 0,
|
||||
"managed Odin topology sequence is zero"
|
||||
);
|
||||
}
|
||||
ExpectedProviderAuthority::ExternalOperatorBinding { binding_target } => {
|
||||
DependencyProviderAuthority::ExternalOperatorBinding { binding_target } => {
|
||||
require_token(binding_target, "external binding target")?;
|
||||
ensure!(
|
||||
self.endpoint.is_some(),
|
||||
|
|
@ -204,14 +217,14 @@ impl ExpectedProviderRef {
|
|||
fn is_external_binding(&self) -> bool {
|
||||
matches!(
|
||||
&self.authority,
|
||||
ExpectedProviderAuthority::ExternalOperatorBinding { .. }
|
||||
DependencyProviderAuthority::ExternalOperatorBinding { .. }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn external_binding_provider_refs(
|
||||
binding: &OperatorBinding,
|
||||
) -> Result<Vec<ExpectedProviderRef>> {
|
||||
) -> Result<Vec<DependencyProviderRef>> {
|
||||
binding.validate()?;
|
||||
let providers = binding
|
||||
.external_capabilities
|
||||
|
|
@ -227,10 +240,10 @@ pub fn external_binding_provider_refs(
|
|||
fn expected_external_provider(
|
||||
binding_target: &str,
|
||||
capability: &ExternalCapabilityBinding,
|
||||
) -> ExpectedProviderRef {
|
||||
ExpectedProviderRef {
|
||||
) -> DependencyProviderRef {
|
||||
DependencyProviderRef {
|
||||
provider_id: capability.provider_id.clone(),
|
||||
authority: ExpectedProviderAuthority::ExternalOperatorBinding {
|
||||
authority: DependencyProviderAuthority::ExternalOperatorBinding {
|
||||
binding_target: binding_target.to_owned(),
|
||||
},
|
||||
capability: capability.capability.clone(),
|
||||
|
|
@ -241,34 +254,61 @@ fn expected_external_provider(
|
|||
}
|
||||
}
|
||||
|
||||
fn managed_expected_provider_refs(
|
||||
expected_incarnations: &[ExpectedIncarnation],
|
||||
) -> Result<Vec<ExpectedProviderRef>> {
|
||||
/// Projects one authenticated Ready correlation into dependency-selection
|
||||
/// inputs. Authentication is not replay admission: the control-plane store
|
||||
/// must atomically admit `publisher_sequence` before calling this function and
|
||||
/// must re-check the selected receipt before promotion.
|
||||
fn managed_ready_provider_refs(
|
||||
admitted: &SequenceAdmittedReady,
|
||||
) -> Result<Vec<DependencyProviderRef>> {
|
||||
let expected = admitted.expected();
|
||||
let topology = admitted.authenticated();
|
||||
expected.validate()?;
|
||||
let expected_projection_sha256 = expected.canonical_sha256()?;
|
||||
let observed = topology.record();
|
||||
ensure!(
|
||||
observed.expected_projection_sha256 == expected_projection_sha256,
|
||||
"Ready provider topology names a different Expected projection"
|
||||
);
|
||||
ensure!(
|
||||
observed.target == expected.target && observed.runtime_id == expected.runtime_id,
|
||||
"Ready provider topology names a different target or runtime"
|
||||
);
|
||||
ensure!(
|
||||
observed.expected
|
||||
&& observed.present
|
||||
&& observed.ready
|
||||
&& observed.observed_presence_state.as_deref() == Some("active")
|
||||
&& observed.disagreements.is_empty(),
|
||||
"managed provider topology is not Ready"
|
||||
);
|
||||
let odin_topology_correlation_sha256 = admitted.evidence_sha256().to_owned();
|
||||
let endpoint = expected
|
||||
.route
|
||||
.as_ref()
|
||||
.map(|route| route.candidate_endpoint.clone());
|
||||
let mut providers = Vec::new();
|
||||
for expected in expected_incarnations {
|
||||
expected.validate()?;
|
||||
let expected_projection_sha256 = expected.canonical_sha256()?;
|
||||
let endpoint = expected
|
||||
.route
|
||||
.as_ref()
|
||||
.map(|route| route.candidate_endpoint.clone());
|
||||
for capability in &expected.capabilities {
|
||||
providers.push(ExpectedProviderRef {
|
||||
provider_id: expected.runtime_id.clone(),
|
||||
authority: ExpectedProviderAuthority::ManagedIncarnation {
|
||||
target: expected.target.clone(),
|
||||
incarnation_id: expected.incarnation_id.clone(),
|
||||
plan_id: expected.plan_id.clone(),
|
||||
sealed_release_id: expected.sealed_release_id.clone(),
|
||||
expected_projection_sha256: expected_projection_sha256.clone(),
|
||||
},
|
||||
capability: capability.capability.clone(),
|
||||
schema: capability.schema.clone(),
|
||||
compatibility: capability.compatibility.clone(),
|
||||
capacity: capability.capacity,
|
||||
endpoint: endpoint.clone(),
|
||||
});
|
||||
}
|
||||
for capability in &observed.observed_capabilities {
|
||||
providers.push(DependencyProviderRef {
|
||||
provider_id: observed.runtime_id.clone(),
|
||||
authority: DependencyProviderAuthority::ManagedReady {
|
||||
target: expected.target.clone(),
|
||||
incarnation_id: expected.incarnation_id.clone(),
|
||||
plan_id: expected.plan_id.clone(),
|
||||
sealed_release_id: expected.sealed_release_id.clone(),
|
||||
expected_projection_sha256: expected_projection_sha256.clone(),
|
||||
odin_topology_correlation_sha256: odin_topology_correlation_sha256.clone(),
|
||||
odin_topology_publisher_sequence: admitted.publisher_sequence(),
|
||||
},
|
||||
capability: capability.capability.clone(),
|
||||
schema: capability.schema.clone(),
|
||||
compatibility: capability.compatibility.clone(),
|
||||
capacity: capability.capacity,
|
||||
endpoint: endpoint.clone(),
|
||||
});
|
||||
}
|
||||
for provider in &providers {
|
||||
provider.validate()?;
|
||||
}
|
||||
Ok(providers)
|
||||
}
|
||||
|
|
@ -277,7 +317,7 @@ fn managed_expected_provider_refs(
|
|||
#[serde(deny_unknown_fields)]
|
||||
pub struct DependencySelection {
|
||||
pub requirement: CapabilityDependency,
|
||||
pub provider: Option<ExpectedProviderRef>,
|
||||
pub provider: Option<DependencyProviderRef>,
|
||||
}
|
||||
|
||||
impl DependencySelection {
|
||||
|
|
@ -321,7 +361,7 @@ impl DependencySelection {
|
|||
) = match &self.provider {
|
||||
None => (None, None, None, None),
|
||||
Some(provider) => match &provider.authority {
|
||||
ExpectedProviderAuthority::ManagedIncarnation {
|
||||
DependencyProviderAuthority::ManagedReady {
|
||||
expected_projection_sha256,
|
||||
..
|
||||
} => (
|
||||
|
|
@ -330,7 +370,7 @@ impl DependencySelection {
|
|||
Some(expected_projection_sha256.clone()),
|
||||
provider.endpoint.clone(),
|
||||
),
|
||||
ExpectedProviderAuthority::ExternalOperatorBinding { .. } => (
|
||||
DependencyProviderAuthority::ExternalOperatorBinding { .. } => (
|
||||
Some(provider.provider_id.clone()),
|
||||
Some("external-operator-binding".into()),
|
||||
None,
|
||||
|
|
@ -355,7 +395,7 @@ impl DependencySelection {
|
|||
|
||||
pub fn select_dependencies(
|
||||
declaration: &TargetDeclaration,
|
||||
providers: &[ExpectedProviderRef],
|
||||
providers: &[DependencyProviderRef],
|
||||
) -> Result<Vec<DependencySelection>> {
|
||||
declaration.validate()?;
|
||||
let mut unique_contracts = BTreeSet::new();
|
||||
|
|
@ -368,7 +408,7 @@ pub fn select_dependencies(
|
|||
provider.schema.as_str(),
|
||||
provider.compatibility.as_str(),
|
||||
)),
|
||||
"expected provider contract is duplicated"
|
||||
"dependency provider contract is duplicated"
|
||||
);
|
||||
}
|
||||
for conflict in &declaration.conflicts {
|
||||
|
|
@ -508,14 +548,14 @@ impl CompiledDeploymentPlan {
|
|||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn compile_deployment_plan(
|
||||
pub(crate) fn compile_deployment_plan(
|
||||
recipe_bytes: &[u8],
|
||||
binding_bytes: &[u8],
|
||||
source: SourceSelectionFacts,
|
||||
incarnation_id: impl Into<String>,
|
||||
candidate_port: Option<u16>,
|
||||
created_at_unix_millis: u64,
|
||||
managed_expected_incarnations: &[ExpectedIncarnation],
|
||||
managed_ready_providers: &[SequenceAdmittedReady],
|
||||
) -> Result<CompiledDeploymentPlan> {
|
||||
let recipe_text = std::str::from_utf8(recipe_bytes).context("recipe is not UTF-8")?;
|
||||
let binding_text = std::str::from_utf8(binding_bytes).context("binding is not UTF-8")?;
|
||||
|
|
@ -533,7 +573,10 @@ pub fn compile_deployment_plan(
|
|||
created_at_unix_millis >= source.selected_at_unix_millis,
|
||||
"deployment plan predates source selection"
|
||||
);
|
||||
let mut providers = managed_expected_provider_refs(managed_expected_incarnations)?;
|
||||
let mut providers = Vec::new();
|
||||
for provider in managed_ready_providers {
|
||||
providers.extend(managed_ready_provider_refs(provider)?);
|
||||
}
|
||||
providers.extend(external_binding_provider_refs(&binding)?);
|
||||
let dependencies = select_dependencies(&declaration, &providers)?;
|
||||
let mut plan = CompiledDeploymentPlan {
|
||||
|
|
@ -697,11 +740,11 @@ impl SealedRelease {
|
|||
let mut capabilities = declaration
|
||||
.provides
|
||||
.iter()
|
||||
.map(|capability| GameCultRuntimeCapability {
|
||||
.map(|capability| IdunnExpectedCapability {
|
||||
capability: capability.capability.clone(),
|
||||
schema: capability.schema.clone(),
|
||||
compatibility: capability.compatibility.clone(),
|
||||
capacity: capability.capacity,
|
||||
minimum_capacity: capability.capacity,
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
capabilities.sort_by(|left, right| {
|
||||
|
|
@ -951,6 +994,13 @@ fn sha256_id(bytes: &[u8]) -> String {
|
|||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use cultnet_rs::{
|
||||
authenticate_odin_runtime_topology_correlation, enroll_service_identity_at,
|
||||
verify_runtime_authority, GameCultProviderHealthIdentity, GameCultRuntimeCapability,
|
||||
IdunnRuntimeActivationLaunch, IdunnServiceIdentity, OdinRuntimeTopologyCorrelationPurpose,
|
||||
OdinRuntimeTopologyCorrelationRecord, OdinTopologyAuthenticationContext,
|
||||
OdinTopologyIdentity, ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA,
|
||||
};
|
||||
|
||||
const RECIPE: &str = r#"
|
||||
schema = "gamecult.idunn.target_declaration.v1"
|
||||
|
|
@ -981,9 +1031,12 @@ executable = true
|
|||
|
||||
[service]
|
||||
executable_artifact = "daemon"
|
||||
arguments = [
|
||||
{ kind = "binding", name = "state_root" },
|
||||
]
|
||||
transport = "http"
|
||||
route_required = true
|
||||
required_environment = ["GAMECULT_IDUNN_RUNTIME_BUNDLE", "SERVICE_BIND"]
|
||||
required_environment = ["GAMECULT_IDUNN_CANDIDATE_BIND", "GAMECULT_IDUNN_RUNTIME_BUNDLE"]
|
||||
|
||||
[service.health]
|
||||
contract = "service.health"
|
||||
|
|
@ -1013,7 +1066,7 @@ compatibility = "v1"
|
|||
"#;
|
||||
|
||||
const BINDING: &str = r#"
|
||||
schema = "gamecult.idunn.operator_binding.v1"
|
||||
schema = "gamecult.idunn.operator_binding.v2"
|
||||
target = "service"
|
||||
|
||||
[repository]
|
||||
|
|
@ -1038,8 +1091,7 @@ tmpfs_mebibytes = 512
|
|||
|
||||
[workload]
|
||||
driver = "systemd-transient"
|
||||
user = "service"
|
||||
group = "service"
|
||||
state_group = "service"
|
||||
unit_prefix = "idunn-service"
|
||||
release_root = "/srv/service/releases"
|
||||
state_root = "/var/lib/gamecult/service"
|
||||
|
|
@ -1049,8 +1101,8 @@ hardening = "strict"
|
|||
memory_mebibytes = 1024
|
||||
cpu_quota_percent = 100
|
||||
|
||||
[workload.environment]
|
||||
SERVICE_BIND = "idunn.private_endpoint"
|
||||
[workload.argument_bindings]
|
||||
state_root = "/var/lib/gamecult/service"
|
||||
|
||||
[runtime_identity]
|
||||
runtime_id = "service-yggdrasil"
|
||||
|
|
@ -1124,17 +1176,94 @@ nodes = ["yggdrasil"]
|
|||
stable_endpoint: "tcp://10.77.0.1:17871".into(),
|
||||
candidate_endpoint: "tcp://127.0.0.1:17871".into(),
|
||||
}),
|
||||
capabilities: vec![GameCultRuntimeCapability {
|
||||
capabilities: vec![IdunnExpectedCapability {
|
||||
capability: "odin.verse-rendezvous".into(),
|
||||
schema: "odin.verse-topology.v1".into(),
|
||||
compatibility: "v1".into(),
|
||||
capacity: 1,
|
||||
minimum_capacity: 1,
|
||||
}],
|
||||
dependencies: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn ready_odin_provider(runtime_id: &str, capacity: u32) -> SequenceAdmittedReady {
|
||||
(|| -> Result<SequenceAdmittedReady> {
|
||||
let root = tempfile::tempdir()?;
|
||||
let provider = enroll_service_identity_at::<GameCultProviderHealthIdentity>(
|
||||
&root.path().join("provider.cc"),
|
||||
)?;
|
||||
let idunn =
|
||||
enroll_service_identity_at::<IdunnServiceIdentity>(&root.path().join("idunn.cc"))?;
|
||||
let odin_signer =
|
||||
enroll_service_identity_at::<OdinTopologyIdentity>(&root.path().join("odin.cc"))?;
|
||||
let mut expected = odin();
|
||||
expected.runtime_id = runtime_id.into();
|
||||
expected.expected_signer_identity_id = provider.entry().identity_id.clone();
|
||||
let launch = IdunnRuntimeActivationLaunch::issue(&expected, digest(b'8'), 100, &idunn)?;
|
||||
let activation = launch.activation().clone();
|
||||
launch.write_credential(std::io::sink())?;
|
||||
let authority = verify_runtime_authority(
|
||||
&expected,
|
||||
&activation,
|
||||
&idunn.trust_anchor()?,
|
||||
&provider.entry().public_key,
|
||||
)?;
|
||||
let mut topology = OdinRuntimeTopologyCorrelationRecord {
|
||||
schema_version: ODIN_RUNTIME_TOPOLOGY_CORRELATION_SCHEMA.into(),
|
||||
target: expected.target.clone(),
|
||||
expected_projection_sha256: expected.canonical_sha256()?,
|
||||
expected: true,
|
||||
current_activation_sha256: Some(activation.canonical_sha256()?),
|
||||
signed_presence_sha256: Some(digest(b'9')),
|
||||
observed_presence_state: Some("active".into()),
|
||||
observed_presence_publisher_sequence: Some(1),
|
||||
observed_write_lease_sha256: None,
|
||||
observed_capabilities: vec![GameCultRuntimeCapability {
|
||||
capability: "odin.verse-rendezvous".into(),
|
||||
schema: "odin.verse-topology.v1".into(),
|
||||
compatibility: "v1".into(),
|
||||
capacity,
|
||||
}],
|
||||
runtime_id: expected.runtime_id.clone(),
|
||||
runtime_instance_id: Some(activation.runtime_instance_id.clone()),
|
||||
present: true,
|
||||
ready: true,
|
||||
dependencies: Vec::new(),
|
||||
disagreements: Vec::new(),
|
||||
signer_identity_id: odin_signer.entry().identity_id.clone(),
|
||||
publisher_sequence: 7,
|
||||
observed_at_unix_millis: 110,
|
||||
signature_algorithm: "ed25519".into(),
|
||||
signature: Vec::new(),
|
||||
};
|
||||
topology.signature = odin_signer
|
||||
.sign::<OdinRuntimeTopologyCorrelationPurpose>(
|
||||
&topology.unsigned_signature_payload()?,
|
||||
)
|
||||
.signature;
|
||||
let bytes = topology.canonical_bytes()?;
|
||||
let authenticated = authenticate_odin_runtime_topology_correlation(
|
||||
&bytes,
|
||||
&authority,
|
||||
None,
|
||||
&odin_signer.entry().public_key,
|
||||
OdinTopologyAuthenticationContext {
|
||||
trusted_received_at_unix_millis: 120,
|
||||
maximum_age_millis: 30,
|
||||
maximum_future_skew_millis: 5,
|
||||
},
|
||||
)?;
|
||||
SequenceAdmittedReady::for_test(&expected, authenticated, 120)
|
||||
})()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn plan() -> CompiledDeploymentPlan {
|
||||
let providers = [ready_odin_provider("odin-yggdrasil", 1)];
|
||||
plan_with(&providers)
|
||||
}
|
||||
|
||||
fn plan_with(providers: &[SequenceAdmittedReady]) -> CompiledDeploymentPlan {
|
||||
compile_deployment_plan(
|
||||
RECIPE.as_bytes(),
|
||||
BINDING.as_bytes(),
|
||||
|
|
@ -1142,7 +1271,7 @@ nodes = ["yggdrasil"]
|
|||
"service-incarnation-1",
|
||||
Some(18001),
|
||||
110,
|
||||
&[odin()],
|
||||
providers,
|
||||
)
|
||||
.unwrap()
|
||||
}
|
||||
|
|
@ -1170,8 +1299,9 @@ nodes = ["yggdrasil"]
|
|||
|
||||
#[test]
|
||||
fn compiler_parses_the_exact_bytes_it_receipts() {
|
||||
let first = plan();
|
||||
let second = plan();
|
||||
let providers = [ready_odin_provider("odin-yggdrasil", 1)];
|
||||
let first = plan_with(&providers);
|
||||
let second = plan_with(&providers);
|
||||
assert_eq!(first, second);
|
||||
assert_eq!(first.recipe_blob.as_slice(), RECIPE.as_bytes());
|
||||
assert_eq!(first.binding_blob.as_slice(), BINDING.as_bytes());
|
||||
|
|
@ -1183,18 +1313,17 @@ nodes = ["yggdrasil"]
|
|||
"contract = \"service.health\"",
|
||||
"contract = \"service.health.v2\"",
|
||||
);
|
||||
assert!(
|
||||
compile_deployment_plan(
|
||||
changed.as_bytes(),
|
||||
BINDING.as_bytes(),
|
||||
source(RECIPE),
|
||||
"service-incarnation-2",
|
||||
Some(18002),
|
||||
111,
|
||||
&[odin()],
|
||||
)
|
||||
.is_err()
|
||||
);
|
||||
let providers = [ready_odin_provider("odin-yggdrasil", 1)];
|
||||
assert!(compile_deployment_plan(
|
||||
changed.as_bytes(),
|
||||
BINDING.as_bytes(),
|
||||
source(RECIPE),
|
||||
"service-incarnation-2",
|
||||
Some(18002),
|
||||
111,
|
||||
&providers,
|
||||
)
|
||||
.is_err());
|
||||
let mut corrupted = first;
|
||||
corrupted
|
||||
.binding_blob
|
||||
|
|
@ -1247,16 +1376,29 @@ nodes = ["yggdrasil"]
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn dependency_selection_uses_expected_identity_without_readiness() {
|
||||
fn dependency_selection_uses_observed_ready_capacity_and_stable_identity_order() {
|
||||
let declaration = TargetDeclaration::parse(RECIPE).unwrap();
|
||||
let mut later = odin();
|
||||
later.runtime_id = "odin-z".into();
|
||||
let providers = managed_expected_provider_refs(&[later, odin()]).unwrap();
|
||||
let admitted = [
|
||||
ready_odin_provider("odin-z", 4),
|
||||
ready_odin_provider("odin-yggdrasil", 2),
|
||||
];
|
||||
let providers = admitted
|
||||
.iter()
|
||||
.flat_map(|provider| managed_ready_provider_refs(provider).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
let selected = select_dependencies(&declaration, &providers).unwrap();
|
||||
assert_eq!(
|
||||
selected[0].provider.as_ref().unwrap().provider_id,
|
||||
"odin-yggdrasil"
|
||||
);
|
||||
assert_eq!(selected[0].provider.as_ref().unwrap().capacity, 2);
|
||||
assert!(matches!(
|
||||
&selected[0].provider.as_ref().unwrap().authority,
|
||||
DependencyProviderAuthority::ManagedReady {
|
||||
odin_topology_publisher_sequence: 7,
|
||||
..
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -1276,7 +1418,7 @@ nodes = ["yggdrasil"]
|
|||
let selected = select_dependencies(&declaration, &providers).unwrap();
|
||||
assert!(matches!(
|
||||
&providers[0].authority,
|
||||
ExpectedProviderAuthority::ExternalOperatorBinding { .. }
|
||||
DependencyProviderAuthority::ExternalOperatorBinding { .. }
|
||||
));
|
||||
assert_eq!(providers[0].capacity, 2);
|
||||
assert_eq!(
|
||||
|
|
@ -1349,7 +1491,7 @@ nodes = ["yggdrasil"]
|
|||
"service-incarnation-sorted",
|
||||
Some(18002),
|
||||
111,
|
||||
&[odin()],
|
||||
&[ready_odin_provider("odin-yggdrasil", 1)],
|
||||
)
|
||||
.unwrap();
|
||||
let release = SealedRelease::new(
|
||||
|
|
|
|||
2356
src/drivers.rs
2356
src/drivers.rs
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue