alien_core/resources/
key.rs1use crate::{
2 error::{ErrorData, Result},
3 resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef, ResourceType},
4};
5use alien_error::AlienError;
6use bon::Builder;
7use serde::{Deserialize, Serialize};
8use std::any::Any;
9
10#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Builder)]
12#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
13#[serde(rename_all = "camelCase", deny_unknown_fields)]
14#[builder(start_fn = new)]
15pub struct Key {
16 #[builder(start_fn)]
18 pub id: String,
19}
20
21pub fn key_description(key_id: &str) -> String {
23 format!("Encryption key '{key_id}'")
24}
25
26impl Key {
27 pub const RESOURCE_TYPE: ResourceType = ResourceType::from_static("key");
28
29 pub fn id(&self) -> &str {
30 &self.id
31 }
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
40#[serde(tag = "provider", rename_all = "lowercase", deny_unknown_fields)]
41pub enum KeyFingerprint {
42 Aws { key_arn: String },
44 Gcp { crypto_key_name: String },
46 Azure {
48 vault_resource_id: String,
49 key_name: String,
50 lineage_version_id: String,
51 },
52}
53
54#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
56#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))]
57#[serde(rename_all = "camelCase")]
58pub struct KeyOutputs {
59 #[serde(default, skip_serializing_if = "Option::is_none")]
62 pub available: Option<bool>,
63 pub fingerprint: KeyFingerprint,
65 pub wrapping_key_id: String,
67}
68
69impl ResourceDefinition for Key {
70 fn get_resource_type(&self) -> ResourceType {
71 Self::RESOURCE_TYPE
72 }
73
74 fn id(&self) -> &str {
75 &self.id
76 }
77
78 fn replace_after_failed_create_is_safe(&self) -> bool {
79 false
81 }
82
83 fn get_dependencies(&self) -> Vec<ResourceRef> {
84 Vec::new()
85 }
86
87 fn validate_update(&self, new_config: &dyn ResourceDefinition) -> Result<()> {
88 let Some(new_key) = new_config.as_any().downcast_ref::<Self>() else {
89 return Err(AlienError::new(ErrorData::UnexpectedResourceType {
90 resource_id: self.id.clone(),
91 expected: Self::RESOURCE_TYPE,
92 actual: new_config.get_resource_type(),
93 }));
94 };
95
96 if self.id != new_key.id {
97 return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
98 resource_id: self.id.clone(),
99 reason: "the 'id' field is immutable".to_string(),
100 }));
101 }
102
103 Ok(())
104 }
105
106 fn as_any(&self) -> &dyn Any {
107 self
108 }
109
110 fn as_any_mut(&mut self) -> &mut dyn Any {
111 self
112 }
113
114 fn box_clone(&self) -> Box<dyn ResourceDefinition> {
115 Box::new(self.clone())
116 }
117
118 fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
119 other.as_any().downcast_ref::<Self>() == Some(self)
120 }
121
122 fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
123 serde_json::to_value(self)
124 }
125}
126
127impl ResourceOutputsDefinition for KeyOutputs {
128 fn is_available(&self) -> bool {
129 self.available.unwrap_or(true)
130 }
131
132 fn get_resource_type(&self) -> ResourceType {
133 Key::RESOURCE_TYPE
134 }
135
136 fn as_any(&self) -> &dyn Any {
137 self
138 }
139
140 fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
141 Box::new(self.clone())
142 }
143
144 fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
145 other.as_any().downcast_ref::<Self>() == Some(self)
146 }
147
148 fn to_json_value(&self) -> serde_json::Result<serde_json::Value> {
149 serde_json::to_value(self)
150 }
151}
152
153#[cfg(test)]
154mod tests {
155 use super::*;
156
157 #[test]
158 fn provider_rotation_preserves_the_key_family_fingerprint() {
159 let fingerprint = KeyFingerprint::Gcp {
160 crypto_key_name: "projects/example/locations/us/keyRings/data/cryptoKeys/customer"
161 .to_string(),
162 };
163 let before = KeyOutputs {
164 available: None,
165 fingerprint: fingerprint.clone(),
166 wrapping_key_id: "projects/example/locations/us/keyRings/data/cryptoKeys/customer/cryptoKeyVersions/1".to_string(),
167 };
168 let after = KeyOutputs {
169 available: None,
170 fingerprint,
171 wrapping_key_id: "projects/example/locations/us/keyRings/data/cryptoKeys/customer/cryptoKeyVersions/2".to_string(),
172 };
173
174 assert_eq!(before.fingerprint, after.fingerprint);
175 assert_ne!(before.wrapping_key_id, after.wrapping_key_id);
176 }
177}