canic-host 0.68.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
use crate::evidence_envelope::{
    EvidenceEnvelopeV1, EvidenceTargetV1, ExitClassV1, InputFingerprintV1, PayloadSchemaRefV1,
    PayloadSchemaStabilityV1,
};
use serde::{Deserialize, Serialize, de};
use std::{collections::BTreeMap, path::Path};
use thiserror::Error as ThisError;

///
/// PolicyGateError
///
#[derive(Debug, ThisError)]
pub enum PolicyGateError {
    #[error("invalid policy: {0}")]
    InvalidPolicy(String),

    #[error("failed to parse policy TOML: {0}")]
    Toml(#[from] toml::de::Error),

    #[error("failed to parse evidence envelope JSON: {0}")]
    Json(#[from] serde_json::Error),

    #[error("failed to fingerprint policy input: {0}")]
    Io(#[from] std::io::Error),
}

///
/// CiPolicyV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct CiPolicyV1 {
    pub schema_version: u32,
    pub envelope: PolicyEnvelopeRulesV1,
    pub exit_class: PolicyExitClassRulesV1,
    pub summary: Option<PolicySummaryRulesV1>,
    pub build_provenance: Option<PolicyBuildProvenanceRulesV1>,
    #[serde(default)]
    pub required_input: Vec<PolicyRequiredInputRuleV1>,
}

///
/// PolicyEnvelopeRulesV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct PolicyEnvelopeRulesV1 {
    pub required_schema: String,
    pub allowed_payload_schemas: Option<Vec<String>>,
    pub allowed_payload_stability: Option<Vec<PayloadSchemaStabilityV1>>,
}

///
/// PolicyExitClassRulesV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct PolicyExitClassRulesV1 {
    pub allowed: Vec<ExitClassV1>,
}

///
/// PolicySummaryRulesV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct PolicySummaryRulesV1 {
    #[serde(default)]
    pub fail_on_evidence_conflicts: bool,
    #[serde(default)]
    pub fail_on_blocked_actions: bool,
    pub allow_missing_or_stale_evidence: Option<bool>,
}

///
/// PolicyBuildProvenanceRulesV1
///
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct PolicyBuildProvenanceRulesV1 {
    pub(super) rules: Vec<PolicyBuildProvenanceRuleV1>,
}

///
/// PolicyBuildProvenanceRuleV1
///
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum PolicyBuildProvenanceRuleV1 {
    CleanSource,
    CargoLock,
    WasmGzip,
    Sha256,
    PackageIdentityMatchesTarget,
}

impl PolicyBuildProvenanceRulesV1 {
    pub(super) fn is_enabled(&self, rule: PolicyBuildProvenanceRuleV1) -> bool {
        self.rules.contains(&rule)
    }
}

impl<'de> Deserialize<'de> for PolicyBuildProvenanceRulesV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        const FIELDS: &[&str] = &[
            "require_clean_source",
            "require_cargo_lock",
            "require_wasm_gzip",
            "require_sha256",
            "require_package_identity_matches_target",
        ];
        let values = BTreeMap::<String, bool>::deserialize(deserializer)?;
        let mut rules = Vec::new();
        for (key, enabled) in values {
            let rule = match key.as_str() {
                "require_clean_source" => PolicyBuildProvenanceRuleV1::CleanSource,
                "require_cargo_lock" => PolicyBuildProvenanceRuleV1::CargoLock,
                "require_wasm_gzip" => PolicyBuildProvenanceRuleV1::WasmGzip,
                "require_sha256" => PolicyBuildProvenanceRuleV1::Sha256,
                "require_package_identity_matches_target" => {
                    PolicyBuildProvenanceRuleV1::PackageIdentityMatchesTarget
                }
                unknown => return Err(de::Error::unknown_field(unknown, FIELDS)),
            };
            if enabled {
                rules.push(rule);
            }
        }
        Ok(Self { rules })
    }
}

///
/// PolicyRequiredInputRuleV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct PolicyRequiredInputRuleV1 {
    pub kind: String,
    pub schema: Option<String>,
}

///
/// PolicyGateRequest
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PolicyGateRequest<'a> {
    pub policy_source: &'a str,
    pub policy_path: &'a Path,
    pub envelope_path: &'a Path,
    pub fingerprint_root: &'a Path,
    pub envelope: EvidenceEnvelopeV1,
}

///
/// ProjectEvidenceManifestGateRequest
///
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProjectEvidenceManifestGateRequest<'a> {
    pub policy_source: &'a str,
    pub policy_path: &'a Path,
    pub manifest_source: &'a str,
    pub manifest_path: &'a Path,
    pub fingerprint_root: &'a Path,
}

///
/// ProjectEvidenceManifestV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ProjectEvidenceManifestV1 {
    pub schema_version: u32,
    pub project: ProjectEvidenceManifestProjectV1,
    pub evidence: Vec<ProjectEvidenceManifestEntryV1>,
}

///
/// ProjectEvidenceManifestProjectV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ProjectEvidenceManifestProjectV1 {
    pub name: String,
    pub root: String,
}

///
/// ProjectEvidenceManifestEntryV1
///
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ProjectEvidenceManifestEntryV1 {
    pub kind: String,
    pub path: String,
    pub required: bool,
    pub payload_schema: String,
    pub target: ProjectEvidenceManifestTargetV1,
}

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

///
/// PolicyGateReportV1
///
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct PolicyGateReportV1 {
    pub schema_version: u32,
    pub policy_schema_version: u32,
    pub policy_file_fingerprint: InputFingerprintV1,
    pub evaluated_envelope_fingerprint: InputFingerprintV1,
    pub evaluated_envelope_exit_class: ExitClassV1,
    pub evaluated_payload_schema: PayloadSchemaRefV1,
    pub evaluated_target: EvidenceTargetV1,
    pub policy_status: PolicyEvaluationStatusV1,
    pub gate_exit_class: ExitClassV1,
    pub requirements: Vec<PolicyRequirementV1>,
    pub findings: Vec<PolicyFindingV1>,
}

///
/// ProjectEvidenceGateReportV1
///
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ProjectEvidenceGateReportV1 {
    pub schema_version: u32,
    pub manifest_schema_version: u32,
    pub project_name: String,
    pub policy_file_fingerprint: InputFingerprintV1,
    pub manifest_file_fingerprint: InputFingerprintV1,
    pub policy_status: PolicyEvaluationStatusV1,
    pub gate_exit_class: ExitClassV1,
    pub evidence: Vec<ProjectEvidenceGateEntryReportV1>,
}

///
/// ProjectEvidenceGateEntryReportV1
///
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ProjectEvidenceGateEntryReportV1 {
    pub kind: String,
    pub path: String,
    pub required: bool,
    pub expected_payload_schema: String,
    pub expected_target: ProjectEvidenceManifestTargetV1,
    pub status: PolicyEvaluationStatusV1,
    pub gate_exit_class: ExitClassV1,
    pub evaluated_envelope_fingerprint: Option<InputFingerprintV1>,
    pub policy_report: Option<PolicyGateReportV1>,
    pub findings: Vec<PolicyFindingV1>,
}

///
/// PolicyRequirementV1
///
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct PolicyRequirementV1 {
    pub requirement_id: String,
    pub status: PolicyEvaluationStatusV1,
    pub exit_class: ExitClassV1,
    pub finding_codes: Vec<String>,
}

///
/// PolicyFindingV1
///
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct PolicyFindingV1 {
    pub code: String,
    pub severity: PolicyFindingSeverityV1,
    pub message: String,
    pub requirement_id: Option<String>,
    pub subject: Option<String>,
    pub expected: Option<serde_json::Value>,
    pub actual: Option<serde_json::Value>,
    pub evidence_path: Option<String>,
    pub target: Option<EvidenceTargetV1>,
    pub related_input: Option<String>,
}

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

///
/// PolicyEvaluationStatusV1
///
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum PolicyEvaluationStatusV1 {
    Passed,
    Failed,
}

impl ProjectEvidenceManifestTargetV1 {
    pub(super) const fn has_selector(&self) -> bool {
        self.deployment.is_some()
            || self.fleet.is_some()
            || self.role.is_some()
            || self.profile.is_some()
            || self.network.is_some()
    }

    pub(super) fn matches_envelope_target(&self, target: &EvidenceTargetV1) -> bool {
        self.deployment
            .as_ref()
            .is_none_or(|expected| target.deployment.as_ref() == Some(expected))
            && self
                .fleet
                .as_ref()
                .is_none_or(|expected| target.fleet.as_ref() == Some(expected))
            && self
                .role
                .as_ref()
                .is_none_or(|expected| target.role.as_ref() == Some(expected))
            && self
                .profile
                .as_ref()
                .is_none_or(|expected| target.profile.as_ref() == Some(expected))
            && self
                .network
                .as_ref()
                .is_none_or(|expected| target.network.as_ref() == Some(expected))
    }
}

impl PolicyFindingV1 {
    pub(super) fn error(
        code: &str,
        message: impl Into<String>,
        requirement_id: &str,
        exit_class: ExitClassV1,
    ) -> Self {
        Self {
            code: code.to_string(),
            severity: PolicyFindingSeverityV1::Error,
            message: message.into(),
            requirement_id: Some(requirement_id.to_string()),
            subject: Some(exit_class_subject(exit_class).to_string()),
            expected: None,
            actual: None,
            evidence_path: None,
            target: None,
            related_input: None,
        }
    }

    pub(super) fn warning(code: &str, message: impl Into<String>, requirement_id: &str) -> Self {
        Self {
            code: code.to_string(),
            severity: PolicyFindingSeverityV1::Warning,
            message: message.into(),
            requirement_id: Some(requirement_id.to_string()),
            subject: Some("success_with_warnings".to_string()),
            expected: None,
            actual: None,
            evidence_path: None,
            target: None,
            related_input: None,
        }
    }

    pub(super) fn expected(mut self, expected: serde_json::Value) -> Self {
        self.expected = Some(expected);
        self
    }

    pub(super) fn actual(mut self, actual: serde_json::Value) -> Self {
        self.actual = Some(actual);
        self
    }

    pub(super) fn exit_class(&self) -> ExitClassV1 {
        match self.subject.as_deref() {
            Some("evidence_conflict") => ExitClassV1::EvidenceConflict,
            Some("missing_required_evidence") => ExitClassV1::MissingRequiredEvidence,
            _ => ExitClassV1::BlockedByPolicy,
        }
    }
}

const fn exit_class_subject(exit_class: ExitClassV1) -> &'static str {
    match exit_class {
        ExitClassV1::EvidenceConflict => "evidence_conflict",
        ExitClassV1::MissingRequiredEvidence => "missing_required_evidence",
        _ => "blocked_by_policy",
    }
}