canic-host 0.56.0

Host-side build, install, deployment, and fleet-template 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
545
546
547
548
549
550
551
552
553
554
555
556
557
//! Stable evidence envelopes for CI/GitOps automation.

use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
    fs, io,
    path::{Component, Path},
    time::UNIX_EPOCH,
};

///
/// EvidenceEnvelopeV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EvidenceEnvelopeV1 {
    pub envelope_schema: PayloadSchemaRefV1,
    pub canic_version: String,
    pub command: CommandProvenanceV1,
    pub target: EvidenceTargetV1,
    pub generated_at: String,
    pub source_config: Option<InputFingerprintV1>,
    pub inputs: Vec<InputFingerprintV1>,
    pub payload_schema: PayloadSchemaRefV1,
    pub payload_sha256: Option<String>,
    pub payload: serde_json::Value,
    pub summary: EvidenceSummaryV1,
    pub exit_class: ExitClassV1,
}

///
/// CommandProvenanceV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct CommandProvenanceV1 {
    pub name: String,
    pub argv_normalized: Vec<String>,
    pub argv_redactions: Vec<String>,
    pub format: String,
}

///
/// EvidenceTargetV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EvidenceTargetV1 {
    pub kind: EvidenceTargetKindV1,
    pub deployment: Option<String>,
    pub fleet: Option<String>,
    pub role: Option<String>,
    pub profile: Option<String>,
    pub network: Option<String>,
}

///
/// EvidenceTargetKindV1
///
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceTargetKindV1 {
    Deployment,
    Fleet,
    FleetAdoption,
    Artifact,
    PolicyGate,
    Unknown,
}

///
/// PayloadSchemaRefV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct PayloadSchemaRefV1 {
    pub id: String,
    pub version: String,
    pub stability: PayloadSchemaStabilityV1,
}

impl PayloadSchemaRefV1 {
    #[must_use]
    pub fn stable(id: &str, version: &str) -> Self {
        Self {
            id: id.to_string(),
            version: version.to_string(),
            stability: PayloadSchemaStabilityV1::Stable,
        }
    }

    #[must_use]
    pub fn experimental(id: &str, version: &str) -> Self {
        Self {
            id: id.to_string(),
            version: version.to_string(),
            stability: PayloadSchemaStabilityV1::Experimental,
        }
    }

    #[must_use]
    pub fn internal(id: &str, version: &str) -> Self {
        Self {
            id: id.to_string(),
            version: version.to_string(),
            stability: PayloadSchemaStabilityV1::Internal,
        }
    }
}

///
/// PayloadSchemaStabilityV1
///
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PayloadSchemaStabilityV1 {
    Stable,
    Experimental,
    Internal,
}

///
/// InputFingerprintV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct InputFingerprintV1 {
    pub kind: String,
    pub path: Option<String>,
    pub path_display: InputPathDisplayV1,
    pub sha256: Option<String>,
    pub size_bytes: Option<u64>,
    pub modified_unix_secs: Option<u64>,
    pub schema: Option<PayloadSchemaRefV1>,
    pub note: Option<String>,
}

///
/// InputPathDisplayV1
///
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum InputPathDisplayV1 {
    Relative,
    AbsoluteRedacted,
    Omitted,
}

///
/// EvidenceSummaryV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EvidenceSummaryV1 {
    pub warnings: Vec<EvidenceMessageV1>,
    pub blocked_actions: Vec<EvidenceMessageV1>,
    pub missing_or_stale_evidence: Vec<EvidenceMessageV1>,
    pub evidence_conflicts: Vec<EvidenceMessageV1>,
}

///
/// EvidenceMessageV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct EvidenceMessageV1 {
    pub code: String,
    pub message: String,
    pub severity: EvidenceMessageSeverityV1,
    pub source: Option<String>,
    pub related_input: Option<String>,
}

impl EvidenceMessageV1 {
    #[must_use]
    pub fn new(
        code: &str,
        message: impl Into<String>,
        severity: EvidenceMessageSeverityV1,
    ) -> Self {
        Self {
            code: code.to_string(),
            message: message.into(),
            severity,
            source: None,
            related_input: None,
        }
    }
}

///
/// EvidenceMessageSeverityV1
///
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum EvidenceMessageSeverityV1 {
    Info,
    Warning,
    Error,
}

///
/// ExitClassV1
///
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum ExitClassV1 {
    Success,
    SuccessWithWarnings,
    BlockedByPolicy,
    EvidenceConflict,
    MissingRequiredEvidence,
    InvalidInput,
    ExecutionFailed,
    InternalError,
}

impl ExitClassV1 {
    #[must_use]
    pub const fn precedence(self) -> u8 {
        match self {
            Self::Success => 0,
            Self::SuccessWithWarnings => 1,
            Self::BlockedByPolicy => 2,
            Self::MissingRequiredEvidence => 3,
            Self::EvidenceConflict => 4,
            Self::InvalidInput => 5,
            Self::ExecutionFailed => 6,
            Self::InternalError => 7,
        }
    }

    #[must_use]
    pub const fn dominates(self, other: Self) -> bool {
        self.precedence() >= other.precedence()
    }
}

#[must_use]
pub fn combine_exit_classes(classes: impl IntoIterator<Item = ExitClassV1>) -> ExitClassV1 {
    classes
        .into_iter()
        .max_by_key(|class| class.precedence())
        .unwrap_or(ExitClassV1::Success)
}

#[must_use]
pub const fn evidence_summary_exit_class(
    summary: &EvidenceSummaryV1,
    missing_required_evidence: bool,
) -> ExitClassV1 {
    if !summary.evidence_conflicts.is_empty() {
        return ExitClassV1::EvidenceConflict;
    }
    if missing_required_evidence {
        return ExitClassV1::MissingRequiredEvidence;
    }
    if !summary.blocked_actions.is_empty() {
        return ExitClassV1::BlockedByPolicy;
    }
    if !summary.warnings.is_empty() || !summary.missing_or_stale_evidence.is_empty() {
        return ExitClassV1::SuccessWithWarnings;
    }

    ExitClassV1::Success
}

pub const EVIDENCE_ENVELOPE_SCHEMA_ID: &str = "canic.evidence_envelope.v1";
pub const ADOPTION_REPORT_SCHEMA_ID: &str = "canic.adoption_report.v1";
pub const DEPLOYMENT_CHECK_SCHEMA_ID: &str = "canic.deployment_check.v1";
pub const POLICY_GATE_REPORT_SCHEMA_ID: &str = "canic.policy_gate_report.v1";
pub const PROJECT_EVIDENCE_MANIFEST_SCHEMA_ID: &str = "canic.project_evidence_manifest.v1";
pub const PROJECT_EVIDENCE_GATE_REPORT_SCHEMA_ID: &str = "canic.project_evidence_gate_report.v1";

#[must_use]
pub fn evidence_envelope_schema() -> PayloadSchemaRefV1 {
    PayloadSchemaRefV1::stable(EVIDENCE_ENVELOPE_SCHEMA_ID, "1")
}

#[must_use]
pub fn adoption_report_schema() -> PayloadSchemaRefV1 {
    PayloadSchemaRefV1::experimental(ADOPTION_REPORT_SCHEMA_ID, "1")
}

#[must_use]
pub fn deployment_check_schema() -> PayloadSchemaRefV1 {
    PayloadSchemaRefV1::internal(DEPLOYMENT_CHECK_SCHEMA_ID, "1")
}

#[must_use]
pub fn policy_gate_report_schema() -> PayloadSchemaRefV1 {
    PayloadSchemaRefV1::stable(POLICY_GATE_REPORT_SCHEMA_ID, "1")
}

#[must_use]
pub fn project_evidence_manifest_schema() -> PayloadSchemaRefV1 {
    PayloadSchemaRefV1::stable(PROJECT_EVIDENCE_MANIFEST_SCHEMA_ID, "1")
}

#[must_use]
pub fn project_evidence_gate_report_schema() -> PayloadSchemaRefV1 {
    PayloadSchemaRefV1::stable(PROJECT_EVIDENCE_GATE_REPORT_SCHEMA_ID, "1")
}

#[must_use]
pub fn sha256_hex(bytes: &[u8]) -> String {
    hex_bytes(Sha256::digest(bytes))
}

pub fn json_payload_sha256<T>(payload: &T) -> Result<String, serde_json::Error>
where
    T: Serialize,
{
    Ok(sha256_hex(&serde_json::to_vec(payload)?))
}

pub fn file_input_fingerprint(
    kind: &str,
    path: &Path,
    root: &Path,
    schema: Option<PayloadSchemaRefV1>,
    note: Option<String>,
) -> io::Result<InputFingerprintV1> {
    let bytes = fs::read(path)?;
    let metadata = fs::metadata(path)?;
    let modified_unix_secs = metadata
        .modified()
        .ok()
        .and_then(|modified| modified.duration_since(UNIX_EPOCH).ok())
        .map(|duration| duration.as_secs());
    let path_summary = input_path_summary(path, root);

    Ok(InputFingerprintV1 {
        kind: kind.to_string(),
        path: path_summary.path,
        path_display: path_summary.display,
        sha256: Some(sha256_hex(&bytes)),
        size_bytes: Some(metadata.len()),
        modified_unix_secs,
        schema,
        note,
    })
}

#[must_use]
pub fn command_path_for_root(path: &Path, root: &Path) -> String {
    input_path_summary(path, root)
        .path
        .unwrap_or_else(|| "<redacted:absolute-outside-root>".to_string())
}

///
/// InputPathSummaryV1
///
#[derive(Clone, Debug, Eq, PartialEq)]
struct InputPathSummaryV1 {
    path: Option<String>,
    display: InputPathDisplayV1,
}

fn input_path_summary(path: &Path, root: &Path) -> InputPathSummaryV1 {
    let canonical_path = fs::canonicalize(path).ok();
    let canonical_root = fs::canonicalize(root).ok();

    if let (Some(canonical_path), Some(canonical_root)) = (canonical_path, canonical_root) {
        if let Ok(relative) = canonical_path.strip_prefix(canonical_root) {
            return InputPathSummaryV1 {
                path: Some(path_to_display(relative)),
                display: InputPathDisplayV1::Relative,
            };
        }
        return InputPathSummaryV1 {
            path: None,
            display: InputPathDisplayV1::AbsoluteRedacted,
        };
    }

    if path.is_absolute() {
        return InputPathSummaryV1 {
            path: None,
            display: InputPathDisplayV1::AbsoluteRedacted,
        };
    }

    InputPathSummaryV1 {
        path: Some(path_to_display(path)),
        display: InputPathDisplayV1::Relative,
    }
}

fn path_to_display(path: &Path) -> String {
    let mut components = Vec::new();

    for component in path.components() {
        match component {
            Component::Prefix(prefix) => {
                components.push(prefix.as_os_str().to_string_lossy().to_string());
            }
            Component::RootDir | Component::CurDir => {}
            Component::ParentDir => components.push("..".to_string()),
            Component::Normal(segment) => components.push(segment.to_string_lossy().to_string()),
        }
    }

    if components.is_empty() {
        ".".to_string()
    } else {
        components.join("/")
    }
}

fn hex_bytes(bytes: impl AsRef<[u8]>) -> String {
    const HEX: &[u8; 16] = b"0123456789abcdef";
    let bytes = bytes.as_ref();
    let mut output = String::with_capacity(bytes.len() * 2);
    for byte in bytes {
        output.push(HEX[(byte >> 4) as usize] as char);
        output.push(HEX[(byte & 0x0f) as usize] as char);
    }
    output
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    fn exit_class_serializes_to_snake_case() {
        let encoded = serde_json::to_string(&ExitClassV1::SuccessWithWarnings).expect("serialize");

        assert_eq!(encoded, "\"success_with_warnings\"");
    }

    #[test]
    fn exit_class_precedence_prefers_policy_relevant_failures() {
        assert_eq!(
            combine_exit_classes([
                ExitClassV1::SuccessWithWarnings,
                ExitClassV1::BlockedByPolicy,
                ExitClassV1::EvidenceConflict,
            ]),
            ExitClassV1::EvidenceConflict
        );
        assert!(ExitClassV1::InvalidInput.dominates(ExitClassV1::EvidenceConflict));
        assert!(ExitClassV1::InternalError.dominates(ExitClassV1::ExecutionFailed));
    }

    #[test]
    fn evidence_summary_exit_class_uses_stable_precedence() {
        let mut summary = EvidenceSummaryV1 {
            warnings: vec![EvidenceMessageV1::new(
                "test.warning",
                "warning",
                EvidenceMessageSeverityV1::Warning,
            )],
            blocked_actions: Vec::new(),
            missing_or_stale_evidence: Vec::new(),
            evidence_conflicts: Vec::new(),
        };

        assert_eq!(
            evidence_summary_exit_class(&summary, false),
            ExitClassV1::SuccessWithWarnings
        );

        summary.blocked_actions.push(EvidenceMessageV1::new(
            "test.blocked",
            "blocked",
            EvidenceMessageSeverityV1::Error,
        ));
        assert_eq!(
            evidence_summary_exit_class(&summary, false),
            ExitClassV1::BlockedByPolicy
        );
        assert_eq!(
            evidence_summary_exit_class(&summary, true),
            ExitClassV1::MissingRequiredEvidence
        );

        summary.evidence_conflicts.push(EvidenceMessageV1::new(
            "test.conflict",
            "conflict",
            EvidenceMessageSeverityV1::Error,
        ));
        assert_eq!(
            evidence_summary_exit_class(&summary, true),
            ExitClassV1::EvidenceConflict
        );
    }

    #[test]
    fn schema_refs_record_stability() {
        assert_eq!(
            evidence_envelope_schema(),
            PayloadSchemaRefV1 {
                id: "canic.evidence_envelope.v1".to_string(),
                version: "1".to_string(),
                stability: PayloadSchemaStabilityV1::Stable,
            }
        );
        assert_eq!(
            adoption_report_schema().stability,
            PayloadSchemaStabilityV1::Experimental
        );
        assert_eq!(
            deployment_check_schema().stability,
            PayloadSchemaStabilityV1::Internal
        );
    }

    #[test]
    fn file_input_fingerprint_uses_relative_path_under_root() {
        let root = temp_dir("canic-envelope-relative");
        let input = root.join("evidence").join("input.json");
        fs::create_dir_all(input.parent().expect("input parent")).expect("create parent");
        fs::write(&input, b"{\"ok\":true}").expect("write input");

        let fingerprint =
            file_input_fingerprint("input", &input, &root, None, None).expect("fingerprint");

        fs::remove_dir_all(&root).expect("clean temp dir");
        assert_eq!(fingerprint.path.as_deref(), Some("evidence/input.json"));
        assert_eq!(fingerprint.path_display, InputPathDisplayV1::Relative);
        assert_eq!(fingerprint.size_bytes, Some(11));
        assert!(
            fingerprint
                .sha256
                .as_deref()
                .is_some_and(|hash| hash.len() == 64)
        );
    }

    #[test]
    fn file_input_fingerprint_redacts_absolute_path_outside_root() {
        let root = temp_dir("canic-envelope-root");
        let outside = temp_dir("canic-envelope-outside");
        fs::create_dir_all(&root).expect("create root");
        fs::create_dir_all(&outside).expect("create outside");
        let input = outside.join("secret.json");
        fs::write(&input, b"secret").expect("write input");

        let fingerprint =
            file_input_fingerprint("input", &input, &root, None, None).expect("fingerprint");
        let command_path = command_path_for_root(&input, &root);

        fs::remove_dir_all(&root).expect("clean root");
        fs::remove_dir_all(&outside).expect("clean outside");
        assert_eq!(fingerprint.path, None);
        assert_eq!(
            fingerprint.path_display,
            InputPathDisplayV1::AbsoluteRedacted
        );
        assert_eq!(command_path, "<redacted:absolute-outside-root>");
    }

    fn temp_dir(name: &str) -> PathBuf {
        let suffix = std::time::SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("system clock before unix epoch")
            .as_nanos();
        std::env::temp_dir().join(format!("{name}-{suffix}"))
    }
}