Skip to main content

alien_core/resources/
signing_verifier.rs

1use crate::resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef, ResourceType};
2use alien_error::AlienError;
3use bon::Builder;
4use serde::{Deserialize, Serialize};
5use std::any::Any;
6use std::fmt::Debug;
7
8/// A protected install's verifier, `<prefix>-verifier`: the only principal that changes what
9/// runs. Worker controllers hand it the cloud writes they build, and machine rolls go through
10/// it; it checks them against the signed release before anything is written.
11///
12/// Setup creates it, and only setup changes it, so it is Frozen only. A preflight adds it to a
13/// stack with signing settings that has a function or a machine cluster; a stack can't declare
14/// it, and its id is reserved.
15///
16/// Maps to:
17/// - AWS: a Lambda function and its role
18/// - GCP: a Cloud Run service and its service account
19/// - Azure: a Container App in a managed environment of its own
20#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
21#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
22#[serde(rename_all = "camelCase", deny_unknown_fields)]
23#[builder(start_fn = new)]
24pub struct SigningVerifier {
25    /// Identifier of the verifier, always [`SigningVerifier::ID`].
26    #[builder(start_fn)]
27    pub id: String,
28}
29
30impl SigningVerifier {
31    /// The resource type identifier for the verifier.
32    pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("verifier");
33
34    /// The verifier's resource id, reserved: no stack resource may take it.
35    pub const ID: &'static str = "verifier";
36
37    /// Returns the verifier's identifier.
38    pub fn id(&self) -> &str {
39        &self.id
40    }
41}
42
43/// Whether `stack` is protected and runs machines: its verifier launches every machine, and only
44/// into the subnets setup fixes for it.
45pub fn verifier_launches_machines(stack: &crate::Stack) -> bool {
46    let has = |resource_type: ResourceType| {
47        stack
48            .resources()
49            .any(|(_, entry)| entry.config.resource_type() == resource_type)
50    };
51    has(SigningVerifier::RESOURCE_TYPE) && has(crate::ComputeCluster::RESOURCE_TYPE)
52}
53
54/// Resource outputs for the verifier.
55#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
56#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
57#[serde(rename_all = "camelCase")]
58pub struct SigningVerifierOutputs {
59    /// The verifier's name in the cloud: `<prefix>-verifier`.
60    pub name: String,
61}
62
63impl ResourceDefinition for SigningVerifier {
64    fn get_resource_type(&self) -> ResourceType {
65        Self::RESOURCE_TYPE
66    }
67
68    fn id(&self) -> &str {
69        &self.id
70    }
71
72    fn get_dependencies(&self) -> Vec<ResourceRef> {
73        Vec::new()
74    }
75
76    fn validate_update(&self, new_config: &dyn ResourceDefinition) -> crate::error::Result<()> {
77        match new_config.as_any().downcast_ref::<SigningVerifier>() {
78            Some(new) if new.id == self.id => Ok(()),
79            Some(_) => Err(AlienError::new(
80                crate::error::ErrorData::InvalidResourceUpdate {
81                    resource_id: self.id.clone(),
82                    reason: "the 'id' field is immutable".to_string(),
83                },
84            )),
85            None => Err(AlienError::new(
86                crate::error::ErrorData::UnexpectedResourceType {
87                    resource_id: self.id.clone(),
88                    expected: Self::RESOURCE_TYPE,
89                    actual: new_config.get_resource_type(),
90                },
91            )),
92        }
93    }
94
95    fn as_any(&self) -> &dyn Any {
96        self
97    }
98
99    fn as_any_mut(&mut self) -> &mut dyn Any {
100        self
101    }
102
103    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
104        Box::new(self.clone())
105    }
106
107    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
108        other.as_any().downcast_ref::<SigningVerifier>() == Some(self)
109    }
110
111    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
112        serde_json::to_value(self)
113    }
114}
115
116impl ResourceOutputsDefinition for SigningVerifierOutputs {
117    fn get_resource_type(&self) -> ResourceType {
118        SigningVerifier::RESOURCE_TYPE.clone()
119    }
120
121    fn as_any(&self) -> &dyn Any {
122        self
123    }
124
125    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
126        Box::new(self.clone())
127    }
128
129    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
130        other.as_any().downcast_ref::<SigningVerifierOutputs>() == Some(self)
131    }
132
133    fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
134        serde_json::to_value(self)
135    }
136}