euv_core/dynamic/manifest/
impl.rs1use super::*;
2
3impl fmt::Display for DynamicReleaseError {
5 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
15 match self {
16 Self::InvalidSignature(msg) => write!(f, "invalid release signature: {msg}"),
17 Self::AbiIncompatible {
18 host_abi,
19 min_abi,
20 max_abi,
21 } => write!(
22 f,
23 "incompatible host ABI {host_abi} (required {min_abi}..={max_abi})"
24 ),
25 Self::UnsupportedDeliveryMode { allowed, incoming } => write!(
26 f,
27 "delivery mode {incoming:?} is not permitted under policy {allowed:?}"
28 ),
29 Self::MissingCapability(cap) => write!(f, "missing required host capability: {cap}"),
30 Self::BrokenDependencyClosure(msg) => {
31 write!(f, "broken release dependency closure: {msg}")
32 }
33 Self::HashMismatch { expected, actual } => {
34 write!(f, "sha256 mismatch: expected {expected}, actual {actual}")
35 }
36 Self::NonMonotonicRelease { current, incoming } => write!(
37 f,
38 "{ERR_NON_MONOTONIC_RELEASE}: current={current}, incoming={incoming}"
39 ),
40 Self::BlobMissing(sha) => write!(f, "content-addressed blob not found: {sha}"),
41 Self::RouteNotFound(route) => write!(f, "route not declared in manifest: {route}"),
42 Self::InterfaceMismatch {
43 expected_interface,
44 actual_interface,
45 } => write!(
46 f,
47 "component interface mismatch: expected {expected_interface}, actual {actual_interface}"
48 ),
49 Self::StateMigrationFailed(msg) => write!(f, "state migration failed: {msg}"),
50 Self::UnsafeSwapPoint(reason) => {
51 write!(f, "swap deferred at unsafe boundary: {reason}")
52 }
53 Self::StateCatchUpConflict {
54 base_revision,
55 live_revision,
56 } => write!(
57 f,
58 "state delta catch-up conflict from rev {base_revision} to rev {live_revision}"
59 ),
60 Self::ProtocolIncompatible(msg) => write!(f, "protocol incompatible: {msg}"),
61 Self::NetworkError(msg) => write!(f, "update network error: {msg}"),
62 Self::StorageError(msg) => write!(f, "update storage error: {msg}"),
63 }
64 }
65}
66
67impl Error for DynamicReleaseError {}
69
70impl BlobArtifact {
72 #[must_use]
78 pub fn artifact_key(&self) -> String {
79 format_artifact_key(self.get_module_id(), self.get_version())
80 }
81
82 #[must_use]
96 pub fn with_contract(
97 mut self,
98 interface_id: &str,
99 interface_version: u32,
100 state_schema_version: u32,
101 min_host_abi: u32,
102 max_host_abi: u32,
103 ) -> Self {
104 self.set_interface_id(interface_id.to_string())
105 .set_interface_version(interface_version)
106 .set_state_schema_version(state_schema_version)
107 .set_min_host_abi(min_host_abi)
108 .set_max_host_abi(max_host_abi);
109 self
110 }
111
112 #[must_use]
122 pub fn with_dependency(mut self, dep_key: String) -> Self {
123 self.get_mut_dependencies().push(dep_key);
124 self
125 }
126
127 #[must_use]
137 pub fn with_entrypoint(mut self, entrypoint: String) -> Self {
138 self.set_entrypoint(entrypoint);
139 self
140 }
141
142 #[must_use]
152 pub fn with_capability(mut self, capability: String) -> Self {
153 self.get_mut_required_capabilities().push(capability);
154 self
155 }
156}
157
158impl RouteManifest {
160 #[must_use]
171 pub fn with_slot(mut self, slot_key: String, artifact_key: String) -> Self {
172 let _: Option<String> = self.get_mut_slot_bindings().insert(slot_key, artifact_key);
173 self
174 }
175
176 pub fn seal_transitive_hashes(
186 &mut self,
187 artifacts: &BTreeMap<String, BlobArtifact>,
188 ) -> Result<&mut Self, DynamicReleaseError> {
189 let hashes: BTreeSet<String> = compute_route_transitive_hashes(self, artifacts)?;
190 self.set_transitive_hashes(hashes);
191 Ok(self)
192 }
193}
194
195impl ReleaseManifest {
197 #[must_use]
203 pub fn rollback_of_seq(&self) -> Option<u64> {
204 let Self { rollback_of, .. } = *self;
205 rollback_of
206 }
207
208 pub fn insert_artifact(&mut self, artifact: BlobArtifact) -> &mut Self {
218 let key: String = artifact.artifact_key();
219 let _: Option<BlobArtifact> = self.get_mut_artifacts().insert(key, artifact);
220 self
221 }
222
223 pub fn insert_route(
233 &mut self,
234 mut route: RouteManifest,
235 ) -> Result<&mut Self, DynamicReleaseError> {
236 let normalized: String = normalize_dynamic_route(route.get_route());
237 route.set_route(normalized.clone());
238 let hashes: BTreeSet<String> =
239 compute_route_transitive_hashes(&route, self.get_artifacts())?;
240 route.set_transitive_hashes(hashes);
241 let _: Option<RouteManifest> = self.get_mut_routes().insert(normalized, route);
242 Ok(self)
243 }
244
245 pub fn canonical_signing_bytes(&self) -> Result<Vec<u8>, DynamicReleaseError> {
251 let mut unsigned: Self = self.clone();
252 unsigned.set_signature(String::new());
253 match to_string(&unsigned) {
254 Ok(json) => Ok(json.into_bytes()),
255 Err(err) => Err(DynamicReleaseError::StorageError(format!(
256 "{ERR_MANIFEST_SERIALIZE}: {err}"
257 ))),
258 }
259 }
260
261 pub fn manifest_sha256(&self) -> Result<String, DynamicReleaseError> {
267 let json: String = match to_string(self) {
268 Ok(encoded) => encoded,
269 Err(err) => {
270 return Err(DynamicReleaseError::StorageError(format!(
271 "{ERR_MANIFEST_SERIALIZE}: {err}"
272 )));
273 }
274 };
275 Ok(sha256_hex(json.as_bytes()))
276 }
277
278 pub fn sign(&mut self, signing_key: &[u8]) -> Result<&mut Self, DynamicReleaseError> {
288 let payload: Vec<u8> = self.canonical_signing_bytes()?;
289 let sig: String = sign_release_payload(&payload, signing_key);
290 self.set_signature(sig);
291 Ok(self)
292 }
293
294 pub fn verify_signature(&self, verify_key: &[u8]) -> Result<(), DynamicReleaseError> {
304 let payload: Vec<u8> = self.canonical_signing_bytes()?;
305 if verify_release_signature(&payload, self.get_signature(), verify_key) {
306 Ok(())
307 } else {
308 Err(DynamicReleaseError::InvalidSignature(
309 ERR_INVALID_SIGNATURE.to_string(),
310 ))
311 }
312 }
313
314 pub fn validate_for_host(
327 &self,
328 host_abi: u32,
329 host_capabilities: &[String],
330 allowed_mode: DeliveryChannelMode,
331 max_artifact_bytes: u64,
332 ) -> Result<(), DynamicReleaseError> {
333 if self.get_manifest_schema() != DYNAMIC_MANIFEST_SCHEMA_V1 {
334 return Err(DynamicReleaseError::ProtocolIncompatible(format!(
335 "unsupported manifest schema v{}",
336 self.get_manifest_schema()
337 )));
338 }
339 if allowed_mode == DeliveryChannelMode::DeclarativeData
340 && self.get_delivery_mode() == DeliveryChannelMode::WasmComponent
341 {
342 return Err(DynamicReleaseError::UnsupportedDeliveryMode {
343 allowed: allowed_mode,
344 incoming: self.get_delivery_mode(),
345 });
346 }
347 if host_abi < self.get_min_host_abi() || host_abi > self.get_max_host_abi() {
348 return Err(DynamicReleaseError::AbiIncompatible {
349 host_abi,
350 min_abi: self.get_min_host_abi(),
351 max_abi: self.get_max_host_abi(),
352 });
353 }
354 for cap in self.get_required_capabilities().iter() {
355 if !host_capabilities.iter().any(|c: &String| c == cap) {
356 return Err(DynamicReleaseError::MissingCapability(cap.clone()));
357 }
358 }
359 for (key, artifact) in self.get_artifacts().iter() {
360 if !is_valid_sha256_hex(artifact.get_sha256()) {
361 return Err(DynamicReleaseError::BrokenDependencyClosure(format!(
362 "{ERR_INVALID_HASH_FORMAT}: {key}"
363 )));
364 }
365 if max_artifact_bytes > 0 && artifact.get_size_bytes() > max_artifact_bytes {
366 return Err(DynamicReleaseError::BrokenDependencyClosure(format!(
367 "{ERR_ARTIFACT_TOO_LARGE}: {key} ({} > {max_artifact_bytes})",
368 artifact.get_size_bytes()
369 )));
370 }
371 if allowed_mode == DeliveryChannelMode::DeclarativeData
372 && artifact.get_kind() == ArtifactKind::WasmComponent
373 {
374 return Err(DynamicReleaseError::UnsupportedDeliveryMode {
375 allowed: allowed_mode,
376 incoming: DeliveryChannelMode::WasmComponent,
377 });
378 }
379 if artifact.get_min_host_abi() > 0
380 && (host_abi < artifact.get_min_host_abi()
381 || (artifact.get_max_host_abi() > 0 && host_abi > artifact.get_max_host_abi()))
382 {
383 return Err(DynamicReleaseError::AbiIncompatible {
384 host_abi,
385 min_abi: artifact.get_min_host_abi(),
386 max_abi: artifact.get_max_host_abi(),
387 });
388 }
389 for cap in artifact.get_required_capabilities().iter() {
390 if !host_capabilities.iter().any(|c: &String| c == cap) {
391 return Err(DynamicReleaseError::MissingCapability(cap.clone()));
392 }
393 }
394 }
395 for (route_key, route_manifest) in self.get_routes().iter() {
396 let computed: BTreeSet<String> =
397 compute_route_transitive_hashes(route_manifest, self.get_artifacts())?;
398 if route_manifest.get_transitive_hashes() != &computed {
399 return Err(DynamicReleaseError::BrokenDependencyClosure(format!(
400 "{ERR_INCOMPLETE_HASH_CLOSURE}: {route_key}"
401 )));
402 }
403 }
404 Ok(())
405 }
406
407 #[must_use]
413 pub fn all_blob_hashes(&self) -> BTreeSet<String> {
414 self.get_artifacts()
415 .values()
416 .map(|art: &BlobArtifact| art.get_sha256().clone())
417 .collect()
418 }
419
420 #[must_use]
430 pub fn missing_blob_hashes(&self, cached_hashes: &HashSet<String>) -> Vec<String> {
431 self.all_blob_hashes()
432 .into_iter()
433 .filter(|sha: &String| !cached_hashes.contains(sha))
434 .collect()
435 }
436
437 #[must_use]
447 pub fn affected_routes_since(&self, previous: Option<&Self>) -> Vec<String> {
448 let Some(prev) = previous else {
449 return self.get_routes().keys().cloned().collect();
450 };
451 let mut changed: Vec<String> = Vec::new();
452 for (route_path, next_route) in self.get_routes().iter() {
453 match prev.get_routes().get(route_path) {
454 Some(prev_route) if prev_route == next_route => {}
455 _ => changed.push(route_path.clone()),
456 }
457 }
458 for prev_route_path in prev.get_routes().keys() {
459 if !self.get_routes().contains_key(prev_route_path) {
460 changed.push(prev_route_path.clone());
461 }
462 }
463 changed.sort();
464 changed
465 }
466}
467
468impl ClientNegotiation {
470 #[must_use]
476 pub fn preferred_track_str(&self) -> Option<&str> {
477 let Self {
478 preferred_track, ..
479 } = self;
480 preferred_track.as_deref()
481 }
482
483 #[must_use]
493 pub fn with_preferred_track(mut self, track: String) -> Self {
494 self.set_preferred_track(Some(track));
495 self
496 }
497
498 #[must_use]
508 pub fn with_capabilities(mut self, capabilities: Vec<String>) -> Self {
509 self.set_capabilities(capabilities);
510 self
511 }
512}