canic-host 0.100.3

Host-side App build, Fleet install, deployment, and release-set library for Canic workspaces
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Module: release_set::infrastructure::persistence
//!
//! Responsibility: compile and immutably persist infrastructure evidence from one build.
//! Does not own: Cargo execution, release-build finalization, placement, or installation.
//! Boundary: only exact current-build outputs may populate the manifest under their durable ID.

#[cfg(test)]
mod tests;

use crate::{
    durable_io::{
        RegularFileReadError, create_new_bytes_with_parents, read_optional_regular_bytes,
    },
    release_build::{ReleaseBuildPlanError, ReleaseBuildPlanState, load_release_build_plan},
    release_set::validate_release_artifact_relative_path,
};
use std::{
    fs, io,
    path::{Path, PathBuf},
};

use canic_core::ids::ReleaseBuildId;
use sha2::{Digest, Sha256};
use thiserror::Error as ThisError;

use super::{
    CanicInfrastructureArtifactInput, CanicInfrastructureArtifactManifest,
    CanicInfrastructureArtifactManifestError, CanicInfrastructureRole,
};

const INFRASTRUCTURE_ARTIFACT_MANIFEST_FILE: &str = "infrastructure-artifact-manifest.json";

///
/// PersistedCanicInfrastructureArtifactManifest
///
/// Exact canonical infrastructure manifest admitted under one durable release-build identity.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PersistedCanicInfrastructureArtifactManifest {
    pub manifest: CanicInfrastructureArtifactManifest,
    pub digest: [u8; 32],
    pub path: PathBuf,
}

///
/// CanicInfrastructureArtifactBuildOutput
///
/// Qualified package, build identity, and exact artifact paths from one complete build.
///

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct CanicInfrastructureArtifactBuildOutput {
    pub role: CanicInfrastructureRole,
    pub package: String,
    pub release_build_id: ReleaseBuildId,
    pub wasm_path: PathBuf,
    pub wasm_gz_path: PathBuf,
}

///
/// CanicInfrastructureArtifactPersistenceError
///
/// Typed rejection while materializing or admitting durable infrastructure evidence.
///

#[derive(Debug, ThisError)]
pub enum CanicInfrastructureArtifactPersistenceError {
    #[error("infrastructure artifact {role:?} {kind} path is outside the ICP root: {path}")]
    ArtifactOutsideRoot {
        role: CanicInfrastructureRole,
        kind: &'static str,
        path: PathBuf,
    },

    #[error("failed to read infrastructure artifact {role:?} {kind} at {path}: {source}")]
    ArtifactRead {
        role: CanicInfrastructureRole,
        kind: &'static str,
        path: PathBuf,
        #[source]
        source: io::Error,
    },

    #[error(
        "infrastructure artifact manifest already exists with different canonical bytes: {path}"
    )]
    ConflictingManifest { path: PathBuf },

    #[error(
        "finalized release build {release_build_id} has no exact infrastructure artifact manifest"
    )]
    FinalizedWithoutExactManifest { release_build_id: ReleaseBuildId },

    #[error("infrastructure artifact {role:?} has an invalid {kind} path: {path}")]
    InvalidArtifactPath {
        role: CanicInfrastructureRole,
        kind: &'static str,
        path: PathBuf,
    },

    #[error("invalid infrastructure artifact manifest {path}: {reason}")]
    InvalidManifestDocument { path: PathBuf, reason: String },

    #[error(transparent)]
    Manifest(#[from] CanicInfrastructureArtifactManifestError),

    #[error("failed to access infrastructure artifact manifest {path}: {source}")]
    ManifestIo {
        path: PathBuf,
        #[source]
        source: io::Error,
    },

    #[error("infrastructure artifact manifest is missing: {path}")]
    MissingManifest { path: PathBuf },

    #[error("infrastructure artifact {role:?} has a non-UTF-8 {kind} path: {path}")]
    NonUtf8ArtifactPath {
        role: CanicInfrastructureRole,
        kind: &'static str,
        path: PathBuf,
    },

    #[error(transparent)]
    ReleaseBuild(#[from] ReleaseBuildPlanError),

    #[error("infrastructure artifact {role:?} {kind} is not a regular no-follow file: {path}")]
    UnsafeArtifact {
        role: CanicInfrastructureRole,
        kind: &'static str,
        path: PathBuf,
    },

    #[error("infrastructure artifact manifest is not a regular no-follow file: {path}")]
    UnsafeManifest { path: PathBuf },
}

struct MaterializedBuildOutput {
    role: CanicInfrastructureRole,
    package: String,
    release_build_id: ReleaseBuildId,
    wasm_relative_path: String,
    wasm: Vec<u8>,
    wasm_gz_relative_path: String,
    wasm_gz: Vec<u8>,
}

impl MaterializedBuildOutput {
    fn as_input(&self) -> CanicInfrastructureArtifactInput<'_> {
        CanicInfrastructureArtifactInput {
            role: self.role,
            package: &self.package,
            release_build_id: self.release_build_id,
            wasm_relative_path: &self.wasm_relative_path,
            wasm: &self.wasm,
            wasm_gz_relative_path: &self.wasm_gz_relative_path,
            wasm_gz: &self.wasm_gz,
        }
    }
}

/// Compile one manifest from qualified outputs and durably publish its canonical bytes.
pub fn compile_and_persist_canic_infrastructure_artifact_manifest(
    root: &Path,
    release_build_id: ReleaseBuildId,
    outputs: &[CanicInfrastructureArtifactBuildOutput],
) -> Result<PersistedCanicInfrastructureArtifactManifest, CanicInfrastructureArtifactPersistenceError>
{
    let release_build = load_release_build_plan(root, release_build_id)?;
    let materialized = outputs
        .iter()
        .map(|output| materialize_build_output(root, release_build_id, output))
        .collect::<Result<Vec<_>, _>>()?;
    let inputs = materialized
        .iter()
        .map(MaterializedBuildOutput::as_input)
        .collect::<Vec<_>>();
    let manifest = CanicInfrastructureArtifactManifest::compile(release_build_id, &inputs)?;
    let expected = persisted_manifest(root, manifest)?;
    let existing = load_optional_persisted_manifest(&expected.path, release_build_id)?;

    if matches!(release_build.state, ReleaseBuildPlanState::Finalized { .. }) {
        return match existing {
            Some(observed) if observed.manifest == expected.manifest => Ok(observed),
            _ => Err(
                CanicInfrastructureArtifactPersistenceError::FinalizedWithoutExactManifest {
                    release_build_id,
                },
            ),
        };
    }

    if let Some(observed) = existing {
        return if observed.manifest == expected.manifest {
            Ok(observed)
        } else {
            Err(
                CanicInfrastructureArtifactPersistenceError::ConflictingManifest {
                    path: expected.path,
                },
            )
        };
    }

    let canonical_bytes = expected.manifest.canonical_bytes()?;
    if let Err(source) = create_new_bytes_with_parents(&expected.path, &canonical_bytes) {
        match load_optional_persisted_manifest(&expected.path, release_build_id) {
            Ok(Some(observed)) if observed.manifest == expected.manifest => return Ok(observed),
            Ok(Some(_)) if source.kind() == io::ErrorKind::AlreadyExists => {
                return Err(
                    CanicInfrastructureArtifactPersistenceError::ConflictingManifest {
                        path: expected.path,
                    },
                );
            }
            _ => {
                return Err(CanicInfrastructureArtifactPersistenceError::ManifestIo {
                    path: expected.path,
                    source,
                });
            }
        }
    }

    load_persisted_canic_infrastructure_artifact_manifest(root, release_build_id)
}

/// Load and validate the exact canonical infrastructure manifest for one release build.
pub fn load_persisted_canic_infrastructure_artifact_manifest(
    root: &Path,
    release_build_id: ReleaseBuildId,
) -> Result<PersistedCanicInfrastructureArtifactManifest, CanicInfrastructureArtifactPersistenceError>
{
    let path = infrastructure_artifact_manifest_path(root, release_build_id);
    load_optional_persisted_manifest(&path, release_build_id)?
        .ok_or(CanicInfrastructureArtifactPersistenceError::MissingManifest { path })
}

fn materialize_build_output(
    root: &Path,
    release_build_id: ReleaseBuildId,
    output: &CanicInfrastructureArtifactBuildOutput,
) -> Result<MaterializedBuildOutput, CanicInfrastructureArtifactPersistenceError> {
    let role = output.role;
    if output.release_build_id != release_build_id {
        return Err(
            CanicInfrastructureArtifactManifestError::ReleaseBuildMismatch {
                role,
                expected: release_build_id,
                actual: output.release_build_id,
            }
            .into(),
        );
    }

    let wasm_relative_path = artifact_relative_path(root, role, "raw Wasm", &output.wasm_path)?;
    let wasm_gz_relative_path =
        artifact_relative_path(root, role, "gzip Wasm", &output.wasm_gz_path)?;
    let wasm = read_artifact(role, "raw Wasm", &output.wasm_path)?;
    let wasm_gz = read_artifact(role, "gzip Wasm", &output.wasm_gz_path)?;

    Ok(MaterializedBuildOutput {
        role,
        package: output.package.clone(),
        release_build_id: output.release_build_id,
        wasm_relative_path,
        wasm,
        wasm_gz_relative_path,
        wasm_gz,
    })
}

fn artifact_relative_path(
    root: &Path,
    role: CanicInfrastructureRole,
    kind: &'static str,
    path: &Path,
) -> Result<String, CanicInfrastructureArtifactPersistenceError> {
    let relative = path.strip_prefix(root).map_err(|_| {
        CanicInfrastructureArtifactPersistenceError::ArtifactOutsideRoot {
            role,
            kind,
            path: path.to_path_buf(),
        }
    })?;
    let relative = relative.to_str().ok_or_else(|| {
        CanicInfrastructureArtifactPersistenceError::NonUtf8ArtifactPath {
            role,
            kind,
            path: path.to_path_buf(),
        }
    })?;
    validate_release_artifact_relative_path(relative).map_err(|_| {
        CanicInfrastructureArtifactPersistenceError::InvalidArtifactPath {
            role,
            kind,
            path: path.to_path_buf(),
        }
    })?;

    let canonical_root = fs::canonicalize(root).map_err(|source| {
        CanicInfrastructureArtifactPersistenceError::ArtifactRead {
            role,
            kind,
            path: path.to_path_buf(),
            source,
        }
    })?;
    let parent = path.parent().ok_or_else(|| {
        CanicInfrastructureArtifactPersistenceError::InvalidArtifactPath {
            role,
            kind,
            path: path.to_path_buf(),
        }
    })?;
    let canonical_parent = fs::canonicalize(parent).map_err(|source| {
        CanicInfrastructureArtifactPersistenceError::ArtifactRead {
            role,
            kind,
            path: path.to_path_buf(),
            source,
        }
    })?;
    if !canonical_parent.starts_with(canonical_root) {
        return Err(
            CanicInfrastructureArtifactPersistenceError::ArtifactOutsideRoot {
                role,
                kind,
                path: path.to_path_buf(),
            },
        );
    }

    Ok(relative.to_string())
}

fn read_artifact(
    role: CanicInfrastructureRole,
    kind: &'static str,
    path: &Path,
) -> Result<Vec<u8>, CanicInfrastructureArtifactPersistenceError> {
    match read_optional_regular_bytes(path) {
        Ok(Some(bytes)) => Ok(bytes),
        Ok(None) => Err(CanicInfrastructureArtifactPersistenceError::ArtifactRead {
            role,
            kind,
            path: path.to_path_buf(),
            source: io::Error::new(io::ErrorKind::NotFound, "artifact is missing"),
        }),
        Err(RegularFileReadError::NotRegular) => Err(
            CanicInfrastructureArtifactPersistenceError::UnsafeArtifact {
                role,
                kind,
                path: path.to_path_buf(),
            },
        ),
        Err(RegularFileReadError::Io(source)) => {
            Err(CanicInfrastructureArtifactPersistenceError::ArtifactRead {
                role,
                kind,
                path: path.to_path_buf(),
                source,
            })
        }
        #[cfg(not(unix))]
        Err(RegularFileReadError::UnsupportedPlatform) => {
            Err(CanicInfrastructureArtifactPersistenceError::ArtifactRead {
                role,
                kind,
                path: path.to_path_buf(),
                source: io::Error::new(
                    io::ErrorKind::Unsupported,
                    "regular no-follow artifact reads are unsupported",
                ),
            })
        }
    }
}

fn persisted_manifest(
    root: &Path,
    manifest: CanicInfrastructureArtifactManifest,
) -> Result<PersistedCanicInfrastructureArtifactManifest, CanicInfrastructureArtifactPersistenceError>
{
    let digest = manifest.digest()?;
    let path = infrastructure_artifact_manifest_path(root, manifest.release_build_id);
    Ok(PersistedCanicInfrastructureArtifactManifest {
        manifest,
        digest,
        path,
    })
}

fn load_optional_persisted_manifest(
    path: &Path,
    release_build_id: ReleaseBuildId,
) -> Result<
    Option<PersistedCanicInfrastructureArtifactManifest>,
    CanicInfrastructureArtifactPersistenceError,
> {
    let bytes = match read_optional_regular_bytes(path) {
        Ok(bytes) => bytes,
        Err(RegularFileReadError::NotRegular) => {
            return Err(
                CanicInfrastructureArtifactPersistenceError::UnsafeManifest {
                    path: path.to_path_buf(),
                },
            );
        }
        Err(RegularFileReadError::Io(source)) => {
            return Err(CanicInfrastructureArtifactPersistenceError::ManifestIo {
                path: path.to_path_buf(),
                source,
            });
        }
        #[cfg(not(unix))]
        Err(RegularFileReadError::UnsupportedPlatform) => {
            return Err(CanicInfrastructureArtifactPersistenceError::ManifestIo {
                path: path.to_path_buf(),
                source: io::Error::new(
                    io::ErrorKind::Unsupported,
                    "regular no-follow manifest reads are unsupported",
                ),
            });
        }
    };
    let Some(bytes) = bytes else {
        return Ok(None);
    };

    let manifest: CanicInfrastructureArtifactManifest =
        serde_json::from_slice(&bytes).map_err(|error| {
            CanicInfrastructureArtifactPersistenceError::InvalidManifestDocument {
                path: path.to_path_buf(),
                reason: error.to_string(),
            }
        })?;
    if manifest.release_build_id != release_build_id {
        return Err(
            CanicInfrastructureArtifactPersistenceError::InvalidManifestDocument {
                path: path.to_path_buf(),
                reason: format!(
                    "manifest release build {} does not match path release build {release_build_id}",
                    manifest.release_build_id
                ),
            },
        );
    }
    let canonical = manifest.canonical_bytes()?;
    if canonical != bytes {
        return Err(
            CanicInfrastructureArtifactPersistenceError::InvalidManifestDocument {
                path: path.to_path_buf(),
                reason: "manifest bytes are not canonical".to_string(),
            },
        );
    }

    Ok(Some(PersistedCanicInfrastructureArtifactManifest {
        digest: Sha256::digest(&bytes).into(),
        manifest,
        path: path.to_path_buf(),
    }))
}

fn infrastructure_artifact_manifest_path(root: &Path, release_build_id: ReleaseBuildId) -> PathBuf {
    root.join(".canic")
        .join("release-builds")
        .join(release_build_id.to_string())
        .join(INFRASTRUCTURE_ARTIFACT_MANIFEST_FILE)
}