Skip to main content

alien_core/import/data/
mod.rs

1//! Typed ImportData payloads by cloud and resource.
2
3use serde::{Deserialize, Deserializer};
4
5pub mod aws;
6pub mod azure;
7pub mod gcp;
8pub mod kubernetes_cluster;
9
10pub use aws::{
11    AwsAiImportData, AwsArtifactRegistryImportData, AwsBuildImportData,
12    AwsComputeClusterImportData, AwsEmailDkimTokenImportData, AwsEmailDomainImportData,
13    AwsEmailImportData, AwsKeyImportData, AwsKvImportData, AwsNetworkImportData,
14    AwsOpenSearchImportData, AwsPostgresImportData, AwsQueueImportData, AwsRedisImportData,
15    AwsRemoteBindingsImportData, AwsRemoteStackManagementImportData, AwsSandboxImportData,
16    AwsSandboxNetwork, AwsServiceAccountImportData, AwsStorageImportData, AwsVaultImportData,
17    AwsWorkerImportData,
18};
19pub use azure::{
20    AzureAiImportData, AzureArtifactRegistryImportData, AzureBuildImportData,
21    AzureComputeClusterImportData, AzureContainerAppsEnvironmentImportData,
22    AzureFlexibleServerPostgresImportData, AzureKeyImportData, AzureKvImportData,
23    AzureManagedRedisImportData, AzureNetworkImportData, AzureQueueImportData,
24    AzureRemoteBindingsImportData, AzureRemoteStackManagementImportData,
25    AzureResourceGroupImportData, AzureSandboxImportData, AzureSandboxNetwork,
26    AzureServiceAccountImportData, AzureServiceActivationImportData,
27    AzureServiceBusNamespaceImportData, AzureStorageAccountImportData, AzureStorageImportData,
28    AzureVaultImportData, AzureWorkerImportData,
29};
30pub use gcp::{
31    GcpAgentPlatformEngineImportData, GcpAiImportData, GcpArtifactRegistryImportData,
32    GcpBuildImportData, GcpComputeClusterImportData, GcpKeyImportData, GcpKvImportData,
33    GcpMemorystoreImportData, GcpNetworkImportData, GcpPostgresImportData, GcpQueueImportData,
34    GcpRemoteBindingsImportData, GcpRemoteStackManagementImportData, GcpSandboxImportData,
35    GcpServiceAccountImportData, GcpServiceActivationImportData, GcpStorageImportData,
36    GcpVaultImportData, GcpWorkerImportData,
37};
38pub use kubernetes_cluster::{
39    AzureApplicationGatewayForContainersBootstrap, KubernetesClusterImportData,
40};
41
42pub(crate) fn deserialize_bool_from_bool_or_string<'de, D>(
43    deserializer: D,
44) -> Result<bool, D::Error>
45where
46    D: Deserializer<'de>,
47{
48    bool_from_bool_or_string(serde_json::Value::deserialize(deserializer)?)
49}
50
51/// Optional form of [`deserialize_bool_from_bool_or_string`]: `null` stays `None`. Pair it with
52/// `#[serde(default)]` so an absent field is also `None`.
53pub(crate) fn deserialize_optional_bool_from_bool_or_string<'de, D>(
54    deserializer: D,
55) -> Result<Option<bool>, D::Error>
56where
57    D: Deserializer<'de>,
58{
59    match serde_json::Value::deserialize(deserializer)? {
60        serde_json::Value::Null => Ok(None),
61        value => bool_from_bool_or_string(value).map(Some),
62    }
63}
64
65fn bool_from_bool_or_string<E: serde::de::Error>(value: serde_json::Value) -> Result<bool, E> {
66    match value {
67        serde_json::Value::Bool(value) => Ok(value),
68        serde_json::Value::String(value) if value.eq_ignore_ascii_case("true") => Ok(true),
69        serde_json::Value::String(value) if value.eq_ignore_ascii_case("false") => Ok(false),
70        other => Err(E::custom(format!(
71            "expected boolean or boolean string, got {other}"
72        ))),
73    }
74}
75
76/// A single port from a JSON number or a numeric string (CloudFormation stringifies every leaf
77/// of a custom resource's properties).
78pub(crate) fn deserialize_u16_from_number_or_string<'de, D>(
79    deserializer: D,
80) -> Result<u16, D::Error>
81where
82    D: Deserializer<'de>,
83{
84    match serde_json::Value::deserialize(deserializer)? {
85        serde_json::Value::Number(number) => number
86            .as_u64()
87            .and_then(|n| u16::try_from(n).ok())
88            .ok_or_else(|| serde::de::Error::custom(format!("port {number} is out of range"))),
89        serde_json::Value::String(text) => text
90            .parse::<u16>()
91            .map_err(|_| serde::de::Error::custom(format!("expected a port number, got {text:?}"))),
92        other => Err(serde::de::Error::custom(format!(
93            "expected a port number or numeric string, got {other}"
94        ))),
95    }
96}
97
98/// Same reason as the boolean helper: CloudFormation stringifies the leaves of a custom
99/// resource's properties, so a declared port list arrives as `["3000"]` rather than `[3000]`.
100pub(crate) fn deserialize_u16_vec_from_numbers_or_strings<'de, D>(
101    deserializer: D,
102) -> Result<Vec<u16>, D::Error>
103where
104    D: Deserializer<'de>,
105{
106    let values = Vec::<serde_json::Value>::deserialize(deserializer)?;
107    values
108        .into_iter()
109        .map(|value| match value {
110            serde_json::Value::Number(number) => number
111                .as_u64()
112                .and_then(|n| u16::try_from(n).ok())
113                .ok_or_else(|| serde::de::Error::custom(format!("port {number} is out of range"))),
114            serde_json::Value::String(text) => text.parse::<u16>().map_err(|_| {
115                serde::de::Error::custom(format!("expected a port number, got {text:?}"))
116            }),
117            other => Err(serde::de::Error::custom(format!(
118                "expected a port number or numeric string, got {other}"
119            ))),
120        })
121        .collect()
122}
123
124#[cfg(all(test, feature = "jsonschema"))]
125mod schema_snapshots {
126    use super::*;
127    use indexmap::IndexMap;
128
129    fn schema<T: schemars::JsonSchema>() -> serde_json::Value {
130        let mut schema =
131            serde_json::to_value(schemars::schema_for!(T)).expect("schema should serialize");
132        // Dependency features can enable preserve_order; object key order is not schema data.
133        schema.sort_all_objects();
134        schema
135    }
136
137    #[test]
138    fn import_data_schema_snapshot() {
139        let schemas = IndexMap::from([
140            ("aws_ai", schema::<AwsAiImportData>()),
141            (
142                "aws_artifact_registry",
143                schema::<AwsArtifactRegistryImportData>(),
144            ),
145            ("aws_build", schema::<AwsBuildImportData>()),
146            (
147                "aws_compute_cluster",
148                schema::<AwsComputeClusterImportData>(),
149            ),
150            ("aws_email", schema::<AwsEmailImportData>()),
151            ("aws_function", schema::<AwsWorkerImportData>()),
152            ("aws_kv", schema::<AwsKvImportData>()),
153            ("aws_key", schema::<AwsKeyImportData>()),
154            ("aws_sandbox", schema::<AwsSandboxImportData>()),
155            ("aws_network", schema::<AwsNetworkImportData>()),
156            ("aws_open_search", schema::<AwsOpenSearchImportData>()),
157            ("aws_postgres", schema::<AwsPostgresImportData>()),
158            ("aws_redis", schema::<AwsRedisImportData>()),
159            ("aws_queue", schema::<AwsQueueImportData>()),
160            (
161                "aws_remote_stack_management",
162                schema::<AwsRemoteStackManagementImportData>(),
163            ),
164            (
165                "aws_service_account",
166                schema::<AwsServiceAccountImportData>(),
167            ),
168            ("aws_storage", schema::<AwsStorageImportData>()),
169            ("aws_vault", schema::<AwsVaultImportData>()),
170            ("azure_ai", schema::<AzureAiImportData>()),
171            (
172                "azure_artifact_registry",
173                schema::<AzureArtifactRegistryImportData>(),
174            ),
175            ("azure_build", schema::<AzureBuildImportData>()),
176            (
177                "azure_container_apps_environment",
178                schema::<AzureContainerAppsEnvironmentImportData>(),
179            ),
180            (
181                "azure_compute_cluster",
182                schema::<AzureComputeClusterImportData>(),
183            ),
184            ("azure_function", schema::<AzureWorkerImportData>()),
185            ("azure_kv", schema::<AzureKvImportData>()),
186            ("azure_key", schema::<AzureKeyImportData>()),
187            ("azure_sandbox", schema::<AzureSandboxImportData>()),
188            ("azure_network", schema::<AzureNetworkImportData>()),
189            (
190                "azure_postgres",
191                schema::<AzureFlexibleServerPostgresImportData>(),
192            ),
193            ("azure_queue", schema::<AzureQueueImportData>()),
194            ("azure_redis", schema::<AzureManagedRedisImportData>()),
195            (
196                "azure_remote_stack_management",
197                schema::<AzureRemoteStackManagementImportData>(),
198            ),
199            (
200                "azure_resource_group",
201                schema::<AzureResourceGroupImportData>(),
202            ),
203            (
204                "azure_service_account",
205                schema::<AzureServiceAccountImportData>(),
206            ),
207            (
208                "azure_service_activation",
209                schema::<AzureServiceActivationImportData>(),
210            ),
211            (
212                "azure_service_bus_namespace",
213                schema::<AzureServiceBusNamespaceImportData>(),
214            ),
215            ("azure_storage", schema::<AzureStorageImportData>()),
216            (
217                "azure_storage_account",
218                schema::<AzureStorageAccountImportData>(),
219            ),
220            ("azure_vault", schema::<AzureVaultImportData>()),
221            (
222                "gcp_agent_platform_engine",
223                schema::<GcpAgentPlatformEngineImportData>(),
224            ),
225            ("gcp_ai", schema::<GcpAiImportData>()),
226            (
227                "gcp_artifact_registry",
228                schema::<GcpArtifactRegistryImportData>(),
229            ),
230            ("gcp_build", schema::<GcpBuildImportData>()),
231            (
232                "gcp_compute_cluster",
233                schema::<GcpComputeClusterImportData>(),
234            ),
235            ("gcp_function", schema::<GcpWorkerImportData>()),
236            ("gcp_kv", schema::<GcpKvImportData>()),
237            ("gcp_key", schema::<GcpKeyImportData>()),
238            ("gcp_network", schema::<GcpNetworkImportData>()),
239            ("gcp_postgres", schema::<GcpPostgresImportData>()),
240            ("gcp_redis", schema::<GcpMemorystoreImportData>()),
241            ("gcp_queue", schema::<GcpQueueImportData>()),
242            (
243                "gcp_remote_stack_management",
244                schema::<GcpRemoteStackManagementImportData>(),
245            ),
246            (
247                "kubernetes_cluster",
248                schema::<KubernetesClusterImportData>(),
249            ),
250            (
251                "gcp_service_account",
252                schema::<GcpServiceAccountImportData>(),
253            ),
254            (
255                "gcp_service_activation",
256                schema::<GcpServiceActivationImportData>(),
257            ),
258            ("gcp_sandbox", schema::<GcpSandboxImportData>()),
259            ("gcp_storage", schema::<GcpStorageImportData>()),
260            ("gcp_vault", schema::<GcpVaultImportData>()),
261        ]);
262
263        insta::assert_json_snapshot!("import_data_schemas", schemas);
264    }
265}
266
267#[cfg(test)]
268mod tests {
269    use super::*;
270    use serde_json::json;
271
272    #[test]
273    fn aws_redis_import_data_accepts_cloudformation_stringified_leaves() {
274        let redis: AwsRedisImportData = serde_json::from_value(json!({
275            "replicationGroupId": "rg",
276            "subnetGroupName": "subnets",
277            "securityGroupId": "sg-123",
278            "parameterGroupName": "params",
279            "authSecretArn": "arn:aws:secretsmanager:us-east-2:123456789012:secret:auth",
280            "endpoint": "rg.cache.amazonaws.com",
281            "port": "6379",
282            "nodeType": "cache.r7g.large",
283            "engineVersion": "7.2",
284            "highAvailability": "false",
285        }))
286        .expect("redis import data should parse CloudFormation's stringified leaves");
287        assert_eq!(redis.port, 6379);
288        assert!(!redis.high_availability);
289    }
290
291    #[test]
292    fn aws_postgres_import_data_accepts_cloudformation_stringified_leaves() {
293        let postgres: AwsPostgresImportData = serde_json::from_value(json!({
294            "clusterIdentifier": "db",
295            "instanceIdentifier": "db-1",
296            "subnetGroupName": "subnets",
297            "securityGroupId": "sg-123",
298            "passwordSecretArn": "arn:aws:secretsmanager:us-east-2:123456789012:secret:pw",
299            "managedPassword": "true",
300            "clusterEndpoint": "db.cluster.rds.amazonaws.com",
301            "database": "app",
302            "username": "app",
303            "engineVersion": "16.4",
304        }))
305        .expect("postgres import data should parse CloudFormation's stringified leaves");
306        assert!(postgres.managed_password);
307    }
308
309    #[test]
310    fn aws_import_data_accepts_cloudformation_stringified_leaves() {
311        let network: AwsNetworkImportData = serde_json::from_value(json!({
312            "vpcId": "vpc-123",
313            "cidrBlock": null,
314            "internetGatewayId": null,
315            "natGatewayId": null,
316            "eipAllocationId": null,
317            "publicSubnetIds": ["subnet-public"],
318            "privateSubnetIds": ["subnet-private"],
319            "publicRouteTableId": null,
320            "privateRouteTableId": null,
321            "securityGroupId": "sg-123",
322            "availabilityZones": [],
323            "isByoVpc": "true",
324        }))
325        .expect("network import data should parse");
326        assert!(network.is_byo_vpc);
327
328        let remote_stack_management: AwsRemoteStackManagementImportData =
329            serde_json::from_value(json!({
330                "roleName": "alien-manager",
331                "roleArn": "arn:aws:iam::123456789012:role/alien-manager",
332                "managementPermissionsApplied": "true",
333            }))
334            .expect("remote stack management import data should parse");
335        assert!(remote_stack_management.management_permissions_applied);
336
337        let service_account: AwsServiceAccountImportData = serde_json::from_value(json!({
338            "roleName": "alien-worker",
339            "roleArn": "arn:aws:iam::123456789012:role/alien-worker",
340            "stackPermissionsApplied": "false",
341        }))
342        .expect("service account import data should parse");
343        assert!(!service_account.stack_permissions_applied);
344
345        // CloudFormation stringifies every leaf of a custom resource's properties, so this is the
346        // shape the importer actually receives — a sandbox declared with open egress and a preview
347        // port reaches it as strings, and rejecting them fails the customer's stack at setup.
348        let sandbox: AwsSandboxImportData = serde_json::from_value(json!({
349            "imageIdentifier": "runner",
350            "imageArn": "arn:aws:lambda:us-east-2:123456789012:microvm-image/runner",
351            "imageVersion": "1",
352            "egressConnectorArns": [],
353            "allowEgress": "true",
354            "previewPorts": ["3000", "8080"],
355        }))
356        .expect("sandbox import data should parse CloudFormation's stringified leaves");
357        assert!(sandbox.allow_egress);
358        assert_eq!(sandbox.preview_ports, vec![3000, 8080]);
359
360        let native: AwsSandboxImportData = serde_json::from_value(json!({
361            "imageIdentifier": "runner",
362            "imageArn": "arn:aws:lambda:us-east-2:123456789012:microvm-image/runner",
363            "imageVersion": "1",
364            "egressConnectorArns": [],
365            "allowEgress": false,
366            "previewPorts": [3000],
367        }))
368        .expect("sandbox import data should still parse native JSON types");
369        assert!(!native.allow_egress);
370        assert_eq!(native.preview_ports, vec![3000]);
371    }
372
373    /// CloudFormation delivers every boolean property as a string, so a setup that declares a
374    /// customer encryption key or a CloudFormation-created EKS cluster registers with "true" or
375    /// "false" here. Rejecting the string form fails registration and rolls the customer's stack back.
376    #[test]
377    fn stringified_key_and_kubernetes_cluster_booleans_parse() {
378        let key = |probe: serde_json::Value| -> Result<AwsKeyImportData, serde_json::Error> {
379            let mut data = json!({ "keyArn": "arn:aws:kms:us-east-1:123456789012:key/k-1" });
380            if !probe.is_null() {
381                data["availabilityProbe"] = probe;
382            }
383            serde_json::from_value(data)
384        };
385        assert!(key(json!("true")).expect("string true").availability_probe);
386        assert!(
387            !key(json!("false"))
388                .expect("string false")
389                .availability_probe
390        );
391        assert!(key(json!(true)).expect("native true").availability_probe);
392        assert!(!key(json!(false)).expect("native false").availability_probe);
393        assert!(
394            !key(serde_json::Value::Null)
395                .expect("an absent probe defaults to false")
396                .availability_probe
397        );
398        assert!(
399            key(json!("yes")).is_err(),
400            "a non-boolean string must be rejected"
401        );
402        assert!(key(json!(1)).is_err(), "a number must be rejected");
403
404        let cluster = |ready: Option<serde_json::Value>| -> Result<
405            KubernetesClusterImportData,
406            serde_json::Error,
407        > {
408            let mut data = json!({
409                "provider": "eks",
410                "ownership": "managed",
411                "namespace": "alien",
412                "clusterName": "acme",
413            });
414            if let Some(ready) = ready {
415                data["cloudMetadataReady"] = ready;
416            }
417            serde_json::from_value(data)
418        };
419        assert_eq!(
420            cluster(Some(json!("true")))
421                .expect("string true")
422                .cloud_metadata_ready,
423            Some(true)
424        );
425        assert_eq!(
426            cluster(Some(json!("false")))
427                .expect("string false")
428                .cloud_metadata_ready,
429            Some(false)
430        );
431        assert_eq!(
432            cluster(Some(json!(true)))
433                .expect("native true")
434                .cloud_metadata_ready,
435            Some(true)
436        );
437        assert_eq!(
438            cluster(Some(json!(false)))
439                .expect("native false")
440                .cloud_metadata_ready,
441            Some(false)
442        );
443        assert_eq!(
444            cluster(Some(serde_json::Value::Null))
445                .expect("null")
446                .cloud_metadata_ready,
447            None
448        );
449        assert_eq!(cluster(None).expect("absent").cloud_metadata_ready, None);
450        assert!(
451            cluster(Some(json!("ready"))).is_err(),
452            "a non-boolean string must be rejected"
453        );
454    }
455
456    /// A runtime-provisioned sandbox registers without an image; the build role and bundle take its
457    /// place. A contract that silently drops those fields as unknown fails only after `terraform
458    /// apply` or stack creation has already succeeded, in the customer's own account.
459    #[test]
460    fn a_runtime_provisioned_sandbox_registers_without_an_image() {
461        let runtime: AwsSandboxImportData = serde_json::from_value(json!({
462            "egressConnectorArns": ["arn:aws:lambda:us-east-2:123456789012:network-connector/c-1"],
463            "allowEgress": "false",
464            "previewPorts": ["8080"],
465            "buildRoleArn": "arn:aws:iam::123456789012:role/acme-agents-build",
466            "bundleUri": "s3://acme-artifacts-us-east-2/agents/bundle.zip",
467        }))
468        .expect("a runtime-provisioned sandbox must register without image fields");
469
470        assert_eq!(runtime.image_arn, None);
471        assert_eq!(runtime.image_identifier, None);
472        assert_eq!(runtime.image_version, None);
473        assert_eq!(
474            runtime.build_role_arn.as_deref(),
475            Some("arn:aws:iam::123456789012:role/acme-agents-build")
476        );
477        assert_eq!(
478            runtime.bundle_uri.as_deref(),
479            Some("s3://acme-artifacts-us-east-2/agents/bundle.zip")
480        );
481        assert_eq!(runtime.preview_ports, vec![8080]);
482
483        // The setup-built shape carries no build inputs, so the two are distinguishable rather
484        // than merely both accepted.
485        let frozen: AwsSandboxImportData = serde_json::from_value(json!({
486            "imageIdentifier": "runner",
487            "imageArn": "arn:aws:lambda:us-east-2:123456789012:microvm-image/runner",
488            "imageVersion": "1.0",
489        }))
490        .expect("the setup-built shape still parses");
491        assert_eq!(frozen.build_role_arn, None);
492        assert_eq!(frozen.bundle_uri, None);
493        assert_eq!(frozen.image_version.as_deref(), Some("1.0"));
494    }
495}