canic-host 0.100.19

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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//! Module: install_root::fleet_registry_activation_journal
//!
//! Responsibility: own host recovery authority for the initial atomic Registry activation.
//! Does not own: Coordinator state, root mirror publication, Directory activation, or runtime activation.
//! Boundary: the exact all-Joining source and all-Active target are durable before mutation.

#[cfg(test)]
mod tests;

use crate::{
    durable_io::{
        RegularFileLockError, RegularFileReadError, create_new_bytes_with_parents,
        lock_regular_file_with_parents, read_optional_regular_bytes, write_bytes,
    },
    fleet_install_plan::PersistedFleetInstallPlan,
};
use canic_core::{
    control_plane_support::{config::ComponentTopology, ops::fleet_registry::FleetRegistryOps},
    dto::fleet_registry::{
        FleetRegistry, FleetRegistryActivationRequest, FleetRegistryActivationResponse,
        FleetRegistryManifest, FleetRegistryVersion,
    },
};
use serde::{Deserialize, Serialize};
use std::{
    io,
    path::{Path, PathBuf},
};
use thiserror::Error as ThisError;

const JOURNAL_FILE: &str = "fleet-registry-activation-journal.json";
const JOURNAL_LOCK_FILE: &str = "fleet-registry-activation-journal.lock";
const JOURNAL_SCHEMA_VERSION: u32 = 1;
// JSON can expand the bounded topology and two bounded Registry snapshots
// substantially beyond their canonical binary sizes.
const MAX_JOURNAL_BYTES: usize = 67_108_864;

#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub(super) enum FleetRegistryActivationPhase {
    Planned,
    ActivationInFlight,
    Activated,
    Verified,
}

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub(super) struct FleetRegistryActivationJournal {
    pub schema_version: u32,
    pub sequence: u64,
    pub phase: FleetRegistryActivationPhase,
    pub fleet_install_plan_digest: [u8; 32],
    pub component_topology: ComponentTopology,
    pub joining_registry: FleetRegistry,
    pub active_registry: FleetRegistry,
    pub request: FleetRegistryActivationRequest,
    pub response: Option<FleetRegistryActivationResponse>,
    pub verified_manifest: Option<FleetRegistryManifest>,
    pub verified_version: Option<FleetRegistryVersion>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub(super) struct ResolvedFleetRegistryActivation {
    pub journal: FleetRegistryActivationJournal,
    pub path: PathBuf,
    pub advanced: bool,
}

pub(super) struct PlanFleetRegistryActivationRequest<'a> {
    pub fleet_install_plan: &'a PersistedFleetInstallPlan,
    pub component_topology: ComponentTopology,
    pub joining_registry: FleetRegistry,
}

#[derive(Debug, ThisError)]
pub(super) enum FleetRegistryActivationJournalError {
    #[error("Fleet Registry activation journal already has different immutable authority: {path}")]
    ConflictingAuthority { path: PathBuf },

    #[error("invalid Fleet Registry activation journal {path}: {reason}")]
    InvalidDocument { path: PathBuf, reason: String },

    #[error(
        "Fleet Registry activation journal cannot transition from {observed:?} to {requested:?}"
    )]
    InvalidTransition {
        observed: FleetRegistryActivationPhase,
        requested: FleetRegistryActivationPhase,
    },

    #[error("failed to access Fleet Registry activation journal {path}: {source}")]
    Io {
        path: PathBuf,
        #[source]
        source: io::Error,
    },

    #[error("Fleet Registry activation journal is not a regular no-follow file: {path}")]
    UnsafeFile { path: PathBuf },

    #[error("Fleet Registry activation journal lock is not a regular no-follow file: {path}")]
    UnsafeLock { path: PathBuf },
}

pub(super) fn plan_fleet_registry_activation(
    request: PlanFleetRegistryActivationRequest<'_>,
) -> Result<ResolvedFleetRegistryActivation, FleetRegistryActivationJournalError> {
    let path = journal_path(&request.fleet_install_plan.path);
    let _lock = lock_journal(&path)?;
    let expected = planned_journal(&path, request)?;

    if let Some(observed) = load_optional_journal(&path)? {
        if same_immutable_authority(&observed, &expected) {
            return Ok(resolved(observed, path, false));
        }
        return Err(FleetRegistryActivationJournalError::ConflictingAuthority { path });
    }

    let bytes = encode_journal(&path, &expected)?;
    if let Err(source) = create_new_bytes_with_parents(&path, &bytes) {
        match load_optional_journal(&path)? {
            Some(observed) if same_immutable_authority(&observed, &expected) => {
                return Ok(resolved(observed, path, false));
            }
            Some(_) if source.kind() == io::ErrorKind::AlreadyExists => {
                return Err(FleetRegistryActivationJournalError::ConflictingAuthority { path });
            }
            _ => return Err(FleetRegistryActivationJournalError::Io { path, source }),
        }
    }

    let durable = load_required_journal(&path)?;
    if durable != expected {
        return Err(invalid(
            &path,
            "published journal differs from planned activation authority",
        ));
    }
    Ok(resolved(durable, path, true))
}

pub(super) fn begin_registry_activation(
    current: &ResolvedFleetRegistryActivation,
) -> Result<ResolvedFleetRegistryActivation, FleetRegistryActivationJournalError> {
    advance_without_evidence(
        current,
        FleetRegistryActivationPhase::Planned,
        FleetRegistryActivationPhase::ActivationInFlight,
    )
}

pub(super) fn record_registry_activated(
    current: &ResolvedFleetRegistryActivation,
    response: FleetRegistryActivationResponse,
) -> Result<ResolvedFleetRegistryActivation, FleetRegistryActivationJournalError> {
    validate_response(&current.path, &current.journal, &response)?;
    transition(
        current,
        FleetRegistryActivationPhase::ActivationInFlight,
        FleetRegistryActivationPhase::Activated,
        |next| next.response = Some(response),
    )
}

pub(super) fn record_registry_activation_verified(
    current: &ResolvedFleetRegistryActivation,
    manifest: FleetRegistryManifest,
    version: FleetRegistryVersion,
) -> Result<ResolvedFleetRegistryActivation, FleetRegistryActivationJournalError> {
    let expected_manifest = FleetRegistryOps::manifest(
        &current.journal.active_registry.authority,
        &current.journal.component_topology,
        &current.journal.active_registry,
    )
    .map_err(|error| invalid(&current.path, error.to_string()))?;
    let expected_version = FleetRegistryOps::version(
        &current.journal.active_registry.authority,
        &current.journal.component_topology,
        &current.journal.active_registry,
    )
    .map_err(|error| invalid(&current.path, error.to_string()))?;
    if manifest != expected_manifest || version != expected_version {
        return Err(invalid(
            &current.path,
            "verified active Registry evidence differs from planned authority",
        ));
    }
    transition(
        current,
        FleetRegistryActivationPhase::Activated,
        FleetRegistryActivationPhase::Verified,
        |next| {
            next.verified_manifest = Some(manifest);
            next.verified_version = Some(version);
        },
    )
}

fn planned_journal(
    path: &Path,
    request: PlanFleetRegistryActivationRequest<'_>,
) -> Result<FleetRegistryActivationJournal, FleetRegistryActivationJournalError> {
    if request.joining_registry.authority.binding.fleet != request.fleet_install_plan.plan.fleet
        || request
            .joining_registry
            .authority
            .binding
            .coordinator_subnet
            != request
                .fleet_install_plan
                .plan
                .coordinator
                .coordinator_subnet
    {
        return Err(invalid(
            path,
            "all-Joining Registry authority differs from the Fleet install plan",
        ));
    }
    let active_registry = FleetRegistryOps::compile_active(
        &request.joining_registry.authority,
        &request.component_topology,
        &request.joining_registry,
    )
    .map_err(|error| invalid(path, error.to_string()))?;
    let expected_registry = FleetRegistryOps::version(
        &request.joining_registry.authority,
        &request.component_topology,
        &request.joining_registry,
    )
    .map_err(|error| invalid(path, error.to_string()))?;
    let journal = FleetRegistryActivationJournal {
        schema_version: JOURNAL_SCHEMA_VERSION,
        sequence: 0,
        phase: FleetRegistryActivationPhase::Planned,
        fleet_install_plan_digest: request.fleet_install_plan.digest,
        component_topology: request.component_topology,
        joining_registry: request.joining_registry,
        active_registry,
        request: FleetRegistryActivationRequest { expected_registry },
        response: None,
        verified_manifest: None,
        verified_version: None,
    };
    validate_journal(path, &journal)?;
    Ok(journal)
}

fn advance_without_evidence(
    current: &ResolvedFleetRegistryActivation,
    expected: FleetRegistryActivationPhase,
    requested: FleetRegistryActivationPhase,
) -> Result<ResolvedFleetRegistryActivation, FleetRegistryActivationJournalError> {
    if current.journal.phase == requested {
        return Ok(resolved(
            current.journal.clone(),
            current.path.clone(),
            false,
        ));
    }
    transition(current, expected, requested, |_| {})
}

fn transition(
    current: &ResolvedFleetRegistryActivation,
    expected: FleetRegistryActivationPhase,
    requested: FleetRegistryActivationPhase,
    apply: impl FnOnce(&mut FleetRegistryActivationJournal),
) -> Result<ResolvedFleetRegistryActivation, FleetRegistryActivationJournalError> {
    if current.journal.phase != expected {
        return Err(FleetRegistryActivationJournalError::InvalidTransition {
            observed: current.journal.phase,
            requested,
        });
    }
    let _lock = lock_journal(&current.path)?;
    let observed = load_required_journal(&current.path)?;
    if observed != current.journal {
        return Err(invalid(
            &current.path,
            "journal changed before the requested transition",
        ));
    }

    let mut next = observed;
    next.sequence = next
        .sequence
        .checked_add(1)
        .ok_or_else(|| invalid(&current.path, "journal sequence exhausted"))?;
    next.phase = requested;
    apply(&mut next);
    let bytes = encode_journal(&current.path, &next)?;
    if let Err(source) = write_bytes(&current.path, &bytes) {
        match load_optional_journal(&current.path)? {
            Some(observed) if observed == next => {
                return Ok(resolved(next, current.path.clone(), true));
            }
            _ => {
                return Err(FleetRegistryActivationJournalError::Io {
                    path: current.path.clone(),
                    source,
                });
            }
        }
    }
    let durable = load_required_journal(&current.path)?;
    if durable != next {
        return Err(invalid(
            &current.path,
            "published transition differs from requested activation journal",
        ));
    }
    Ok(resolved(durable, current.path.clone(), true))
}

fn validate_journal(
    path: &Path,
    journal: &FleetRegistryActivationJournal,
) -> Result<(), FleetRegistryActivationJournalError> {
    if journal.schema_version != JOURNAL_SCHEMA_VERSION {
        return Err(invalid(path, "unsupported journal schema version"));
    }
    FleetRegistryOps::validate(
        &journal.joining_registry.authority,
        &journal.component_topology,
        &journal.joining_registry,
    )
    .map_err(|error| invalid(path, error.to_string()))?;
    let active = FleetRegistryOps::compile_active(
        &journal.joining_registry.authority,
        &journal.component_topology,
        &journal.joining_registry,
    )
    .map_err(|error| invalid(path, error.to_string()))?;
    if active != journal.active_registry {
        return Err(invalid(
            path,
            "active Registry is not the canonical transition target",
        ));
    }
    let expected_joining = FleetRegistryOps::version(
        &journal.joining_registry.authority,
        &journal.component_topology,
        &journal.joining_registry,
    )
    .map_err(|error| invalid(path, error.to_string()))?;
    if journal.request.expected_registry != expected_joining {
        return Err(invalid(
            path,
            "activation request differs from all-Joining authority",
        ));
    }
    validate_phase_evidence(path, journal)?;
    if let Some(response) = &journal.response {
        validate_response(path, journal, response)?;
    }
    if let Some(manifest) = &journal.verified_manifest {
        let expected = FleetRegistryOps::manifest(
            &journal.active_registry.authority,
            &journal.component_topology,
            &journal.active_registry,
        )
        .map_err(|error| invalid(path, error.to_string()))?;
        if manifest != &expected {
            return Err(invalid(path, "verified active Registry manifest differs"));
        }
    }
    if let Some(version) = &journal.verified_version {
        let expected = FleetRegistryOps::version(
            &journal.active_registry.authority,
            &journal.component_topology,
            &journal.active_registry,
        )
        .map_err(|error| invalid(path, error.to_string()))?;
        if version != &expected {
            return Err(invalid(path, "verified active Registry version differs"));
        }
    }
    Ok(())
}

fn validate_phase_evidence(
    path: &Path,
    journal: &FleetRegistryActivationJournal,
) -> Result<(), FleetRegistryActivationJournalError> {
    let expected_sequence = match journal.phase {
        FleetRegistryActivationPhase::Planned => 0,
        FleetRegistryActivationPhase::ActivationInFlight => 1,
        FleetRegistryActivationPhase::Activated => 2,
        FleetRegistryActivationPhase::Verified => 3,
    };
    let expected_evidence = match journal.phase {
        FleetRegistryActivationPhase::Planned
        | FleetRegistryActivationPhase::ActivationInFlight => (false, false, false),
        FleetRegistryActivationPhase::Activated => (true, false, false),
        FleetRegistryActivationPhase::Verified => (true, true, true),
    };
    let observed_evidence = (
        journal.response.is_some(),
        journal.verified_manifest.is_some(),
        journal.verified_version.is_some(),
    );
    if journal.sequence != expected_sequence || observed_evidence != expected_evidence {
        return Err(invalid(
            path,
            "phase differs from activation sequence or retained evidence",
        ));
    }
    Ok(())
}

fn validate_response(
    path: &Path,
    journal: &FleetRegistryActivationJournal,
    response: &FleetRegistryActivationResponse,
) -> Result<(), FleetRegistryActivationJournalError> {
    let version = FleetRegistryOps::version(
        &journal.active_registry.authority,
        &journal.component_topology,
        &journal.active_registry,
    )
    .map_err(|error| invalid(path, error.to_string()))?;
    if response.previous_version != journal.request.expected_registry || response.version != version
    {
        return Err(invalid(
            path,
            "activation response differs from planned source or target authority",
        ));
    }
    Ok(())
}

fn load_required_journal(
    path: &Path,
) -> Result<FleetRegistryActivationJournal, FleetRegistryActivationJournalError> {
    load_optional_journal(path)?.ok_or_else(|| invalid(path, "journal is missing"))
}

fn load_optional_journal(
    path: &Path,
) -> Result<Option<FleetRegistryActivationJournal>, FleetRegistryActivationJournalError> {
    let bytes = match read_optional_regular_bytes(path) {
        Ok(bytes) => bytes,
        Err(RegularFileReadError::NotRegular) => {
            return Err(FleetRegistryActivationJournalError::UnsafeFile {
                path: path.to_path_buf(),
            });
        }
        Err(RegularFileReadError::Io(source)) => {
            return Err(FleetRegistryActivationJournalError::Io {
                path: path.to_path_buf(),
                source,
            });
        }
    };
    let Some(bytes) = bytes else {
        return Ok(None);
    };
    if bytes.len() > MAX_JOURNAL_BYTES {
        return Err(invalid(path, "journal exceeds its byte bound"));
    }
    let journal = serde_json::from_slice::<FleetRegistryActivationJournal>(&bytes)
        .map_err(|error| invalid(path, error.to_string()))?;
    validate_journal(path, &journal)?;
    if encode_journal(path, &journal)? != bytes {
        return Err(invalid(path, "journal bytes are not canonical"));
    }
    Ok(Some(journal))
}

fn encode_journal(
    path: &Path,
    journal: &FleetRegistryActivationJournal,
) -> Result<Vec<u8>, FleetRegistryActivationJournalError> {
    validate_journal(path, journal)?;
    let bytes = serde_json::to_vec(journal).map_err(|error| invalid(path, error.to_string()))?;
    if bytes.len() > MAX_JOURNAL_BYTES {
        return Err(invalid(path, "journal exceeds its byte bound"));
    }
    Ok(bytes)
}

fn same_immutable_authority(
    observed: &FleetRegistryActivationJournal,
    expected: &FleetRegistryActivationJournal,
) -> bool {
    observed.schema_version == expected.schema_version
        && observed.fleet_install_plan_digest == expected.fleet_install_plan_digest
        && observed.component_topology == expected.component_topology
        && observed.joining_registry == expected.joining_registry
        && observed.active_registry == expected.active_registry
        && observed.request == expected.request
}

fn journal_path(plan_path: &Path) -> PathBuf {
    plan_path
        .parent()
        .expect("validated Fleet install plan path has an identity directory")
        .join(JOURNAL_FILE)
}

fn lock_journal(path: &Path) -> Result<std::fs::File, FleetRegistryActivationJournalError> {
    let lock_path = path.with_file_name(JOURNAL_LOCK_FILE);
    match lock_regular_file_with_parents(&lock_path) {
        Ok(lock) => Ok(lock),
        Err(RegularFileLockError::NotRegular) => {
            Err(FleetRegistryActivationJournalError::UnsafeLock { path: lock_path })
        }
        Err(RegularFileLockError::Io(source)) => Err(FleetRegistryActivationJournalError::Io {
            path: lock_path,
            source,
        }),
        #[cfg(windows)]
        Err(RegularFileLockError::UnsupportedPlatform) => {
            Err(FleetRegistryActivationJournalError::Io {
                path: lock_path,
                source: io::Error::new(
                    io::ErrorKind::Unsupported,
                    "Fleet Registry activation journal locking is unsupported",
                ),
            })
        }
    }
}

const fn resolved(
    journal: FleetRegistryActivationJournal,
    path: PathBuf,
    advanced: bool,
) -> ResolvedFleetRegistryActivation {
    ResolvedFleetRegistryActivation {
        journal,
        path,
        advanced,
    }
}

fn invalid(path: &Path, reason: impl Into<String>) -> FleetRegistryActivationJournalError {
    FleetRegistryActivationJournalError::InvalidDocument {
        path: path.to_path_buf(),
        reason: reason.into(),
    }
}