alien-core 3.3.73

Deploy software into your customers' cloud accounts and keep it fully managed
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
//! Resources of a type an extension defines, kept as JSON.
//!
//! Alien deserializes `Resource`, `ResourceOutputs` and `ResourceHeartbeatData` by their type
//! tag. A tag of the form `<extension>/<type>` that Alien does not define loads as one of the
//! types here, so a stack, its state and its heartbeats can carry an extension's resource and
//! serialize it back unchanged.

use crate::error::ErrorData;
use crate::resource::{ResourceDefinition, ResourceOutputsDefinition, ResourceRef, ResourceType};
use alien_error::AlienError;
use serde_json::{Map, Value};
use std::any::Any;

/// Type prefixes no extension may use: `alien/` is Alien's own namespace, and `experimental/`
/// holds built-in types that are not stable yet, such as `experimental/aws-opensearch`.
pub const RESERVED_TYPE_PREFIXES: &[&str] = &["alien", "experimental"];

/// Whether a type tag Alien does not define names an extension's type: `<extension>/<type>`,
/// both parts non-empty, under a prefix that is not reserved. Any other unknown tag stays an
/// error, so a typo in a built-in type name is refused instead of loading as custom.
pub(crate) fn is_custom_resource_type(tag: &str) -> bool {
    match tag.split_once('/') {
        Some((extension, name)) => {
            !extension.is_empty()
                && !name.is_empty()
                && !RESERVED_TYPE_PREFIXES.contains(&extension)
        }
        None => false,
    }
}

/// Configuration of a resource whose type an extension defines.
///
/// `config` holds every field but `type`, including `id`, so the resource serializes back
/// exactly as it was read.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomResource {
    resource_type: ResourceType,
    id: String,
    config: Map<String, Value>,
}

impl CustomResource {
    /// Builds one from the JSON object `Resource` read, with its `type` tag removed.
    pub fn from_untagged(resource_type: &str, value: Value) -> Result<Self, String> {
        let Value::Object(config) = value else {
            return Err(format!(
                "resource of type '{resource_type}' must be a JSON object"
            ));
        };
        let id = config
            .get("id")
            .and_then(Value::as_str)
            .ok_or_else(|| format!("resource of type '{resource_type}' has no string 'id'"))?
            .to_string();
        Ok(Self {
            resource_type: ResourceType::from(resource_type),
            id,
            config,
        })
    }

    /// The resource's fields, without `type`.
    pub fn config(&self) -> &Map<String, Value> {
        &self.config
    }
}

impl ResourceDefinition for CustomResource {
    fn get_resource_type(&self) -> ResourceType {
        self.resource_type.clone()
    }

    fn id(&self) -> &str {
        &self.id
    }

    /// Alien cannot read references out of a config it does not know. A custom resource's
    /// dependencies travel in `ResourceEntry.dependencies` instead.
    fn get_dependencies(&self) -> Vec<ResourceRef> {
        Vec::new()
    }

    /// The type and id can't change, as for Alien's own types; which other fields may is the
    /// controller's to say when it starts the update.
    fn validate_update(&self, new_config: &dyn ResourceDefinition) -> crate::error::Result<()> {
        if new_config.get_resource_type() != self.resource_type {
            return Err(AlienError::new(ErrorData::UnexpectedResourceType {
                resource_id: self.id.clone(),
                expected: self.resource_type.clone(),
                actual: new_config.get_resource_type(),
            }));
        }
        if new_config.id() != self.id {
            return Err(AlienError::new(ErrorData::InvalidResourceUpdate {
                resource_id: self.id.clone(),
                reason: "the 'id' field is immutable".to_string(),
            }));
        }
        Ok(())
    }

    /// Alien cannot tell whether a custom resource holds data, so it never deletes a
    /// half-created one to create it again.
    fn replace_after_failed_create_is_safe(&self) -> bool {
        false
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }

    fn box_clone(&self) -> Box<dyn ResourceDefinition> {
        Box::new(self.clone())
    }

    fn resource_eq(&self, other: &dyn ResourceDefinition) -> bool {
        other.as_any().downcast_ref::<CustomResource>() == Some(self)
    }

    fn to_json_value(&self) -> serde_json::Result<Value> {
        Ok(Value::Object(self.config.clone()))
    }
}

/// Outputs of a resource whose type an extension defines.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CustomResourceOutputs {
    resource_type: ResourceType,
    fields: Map<String, Value>,
}

impl CustomResourceOutputs {
    pub(crate) fn new(resource_type: ResourceType, fields: Map<String, Value>) -> Self {
        Self {
            resource_type,
            fields,
        }
    }

    /// Builds one from the JSON object `ResourceOutputs` read, with its `type` tag removed.
    pub fn from_untagged(resource_type: &str, value: Value) -> Result<Self, String> {
        let Value::Object(fields) = value else {
            return Err(format!(
                "outputs of type '{resource_type}' must be a JSON object"
            ));
        };
        Ok(Self {
            resource_type: ResourceType::from(resource_type),
            fields,
        })
    }

    /// The output fields, without `type`.
    pub fn fields(&self) -> &Map<String, Value> {
        &self.fields
    }
}

impl ResourceOutputsDefinition for CustomResourceOutputs {
    fn get_resource_type(&self) -> ResourceType {
        self.resource_type.clone()
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn box_clone(&self) -> Box<dyn ResourceOutputsDefinition> {
        Box::new(self.clone())
    }

    fn outputs_eq(&self, other: &dyn ResourceOutputsDefinition) -> bool {
        other.as_any().downcast_ref::<CustomResourceOutputs>() == Some(self)
    }

    fn to_json_value(&self) -> serde_json::Result<Value> {
        Ok(Value::Object(self.fields.clone()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Resource, ResourceOutputs, Stack, Worker, WorkerCode};
    use serde_json::json;

    /// A custom resource keeps its type and id across an update; other fields are the
    /// controller's to judge.
    #[test]
    fn an_update_keeps_the_type_and_id() {
        let zone = |resource_type: &str, id: &str, domain: &str| {
            CustomResource::from_untagged(
                resource_type,
                serde_json::json!({ "id": id, "domain": domain }),
            )
            .unwrap()
        };
        let current = zone("acme/dns-zone", "zone", "a.example");
        current
            .validate_update(&zone("acme/dns-zone", "zone", "b.example"))
            .unwrap();
        assert!(current
            .validate_update(&zone("acme/dns-zone", "other", "a.example"))
            .is_err());
        assert!(current
            .validate_update(&zone("acme/record", "zone", "a.example"))
            .is_err());
    }

    /// A stack with a worker that depends on an extension's DNS zone, as alien.ts writes it.
    fn stack_json() -> Value {
        let api = Worker::new("api".to_string())
            .code(WorkerCode::Image {
                image: "api:1".to_string(),
            })
            .permissions("api".to_string())
            .build();
        json!({
            "id": "acme",
            "resources": {
                "zone": {
                    "config": { "type": "acme/dns-zone", "id": "zone", "domain": "internal.acme.dev" },
                    "lifecycle": "live",
                    "dependencies": []
                },
                "api": {
                    "config": serde_json::to_value(Resource::new(api)).unwrap(),
                    "lifecycle": "live",
                    "dependencies": [{ "type": "acme/dns-zone", "id": "zone" }]
                }
            }
        })
    }

    #[test]
    fn only_an_unreserved_extension_prefix_names_a_custom_type() {
        for (tag, custom) in [
            ("acme/dns-zone", true),
            ("worker", false),
            ("not-a-resource", false),
            ("alien/dns-zone", false),
            ("experimental/dns-zone", false),
            ("/dns-zone", false),
            ("acme/", false),
        ] {
            assert_eq!(is_custom_resource_type(tag), custom, "{tag}");
        }
    }

    /// The tags serde lists in its `unknown variant` refusal: every tag the type defines.
    fn listed_tags(refusal: impl std::fmt::Display) -> Vec<String> {
        let message = refusal.to_string();
        let tags: Vec<String> = message
            .split('`')
            .skip(1)
            .step_by(2)
            .filter(|tag| *tag != "not-a-resource")
            .map(str::to_string)
            .collect();
        assert!(tags.len() > 10, "could not read the tags out of: {message}");
        tags
    }

    // The heartbeat deserializer applies the custom rule before the built-in tags, and platform
    // tells a custom heartbeat apart by its slash; both hold only while no built-in tag is custom.
    #[test]
    fn no_built_in_tag_names_a_custom_type() {
        let resources = serde_json::from_value::<Resource>(json!({ "type": "not-a-resource" }))
            .expect_err("an unknown tag is refused");
        let outputs =
            serde_json::from_value::<ResourceOutputs>(json!({ "type": "not-a-resource" }))
                .expect_err("an unknown tag is refused");
        let heartbeats = serde_json::from_value::<crate::ResourceHeartbeatData>(json!({
            "resourceType": "not-a-resource",
            "data": {}
        }))
        .expect_err("an unknown tag is refused");

        for tag in listed_tags(resources)
            .into_iter()
            .chain(listed_tags(outputs))
            .chain(listed_tags(heartbeats))
        {
            assert!(!is_custom_resource_type(&tag), "{tag}");
        }
    }

    #[test]
    fn custom_resource_round_trips_through_stack_json() {
        let stack: Stack = serde_json::from_value(stack_json()).expect("the stack loads");
        let zone = &stack.resources["zone"].config;
        let custom = zone
            .downcast_ref::<CustomResource>()
            .expect("the extension's type is a CustomResource");
        assert_eq!(custom.id(), "zone");
        assert_eq!(zone.resource_type().as_ref(), "acme/dns-zone");
        assert_eq!(custom.config()["domain"], "internal.acme.dev");

        let again = serde_json::to_value(&stack).expect("the stack serializes");
        assert_eq!(
            again["resources"]["zone"]["config"],
            stack_json()["resources"]["zone"]["config"]
        );
        let reloaded: Stack = serde_json::from_value(again).expect("it loads again");
        assert_eq!(reloaded, stack);
    }

    #[test]
    fn built_in_types_still_load_as_themselves() {
        let stack: Stack = serde_json::from_value(stack_json()).expect("the stack loads");
        let api = &stack.resources["api"].config;
        assert!(api.downcast_ref::<Worker>().is_some());
        assert!(api.downcast_ref::<CustomResource>().is_none());
    }

    #[test]
    fn custom_resource_without_id_is_refused() {
        let err =
            serde_json::from_value::<Resource>(json!({ "type": "acme/dns-zone", "domain": "x" }))
                .expect_err("no id");
        assert!(err.to_string().contains("no string 'id'"), "{err}");
    }

    #[test]
    fn custom_resource_is_never_replaced_after_a_failed_create() {
        let resource: Resource =
            serde_json::from_value(json!({ "type": "acme/dns-zone", "id": "zone" })).unwrap();
        assert!(!resource.replace_after_failed_create_is_safe());
    }

    #[test]
    fn custom_outputs_round_trip() {
        let tagged = json!({ "type": "acme/dns-zone", "zoneId": "Z123" });
        let outputs: ResourceOutputs =
            serde_json::from_value(tagged.clone()).expect("outputs load");
        let custom = outputs
            .downcast_ref::<CustomResourceOutputs>()
            .expect("CustomResourceOutputs");
        assert_eq!(custom.fields()["zoneId"], "Z123");
        assert_eq!(serde_json::to_value(&outputs).unwrap(), tagged);
    }

    #[test]
    fn unknown_type_outside_an_extension_namespace_is_refused() {
        for tag in ["not-a-resource", "alien/dns-zone", "experimental/dns-zone"] {
            let resource = serde_json::from_value::<Resource>(json!({ "type": tag, "id": "x" }))
                .expect_err(tag);
            assert!(
                resource.to_string().contains("unknown variant"),
                "{resource}"
            );
            let outputs =
                serde_json::from_value::<ResourceOutputs>(json!({ "type": tag })).expect_err(tag);
            assert!(outputs.to_string().contains("unknown variant"), "{outputs}");
        }
    }

    /// A release records no minimum Alien version for its extensions: a binary that has none
    /// of the stack's extensions loaded still reads and writes back a state with their
    /// resources unchanged, since a custom resource's config, controller state and outputs are
    /// kept as JSON.
    #[test]
    fn a_stack_state_with_a_custom_resource_round_trips_without_its_extension() {
        let state = json!({
            "platform": "aws",
            "resourcePrefix": "k1234567",
            "resources": {
                "zone": {
                    "type": "acme/dns-zone",
                    "_internal": {
                        "type": "acme/dns-zone",
                        "_controllerStateVersion": 1,
                        "state": "Ready",
                        "zoneId": "Z123",
                        "nameServers": ["ns-1.example.net"]
                    },
                    "status": "running",
                    "outputs": { "type": "acme/dns-zone", "zoneId": "Z123" },
                    "config": { "type": "acme/dns-zone", "id": "zone", "domain": "internal.acme.dev" },
                    "previousConfig": { "type": "acme/dns-zone", "id": "zone", "domain": "old.acme.dev" },
                    "lifecycle": "live",
                    "lastFailedState": { "type": "acme/dns-zone", "state": "Creating" }
                }
            }
        });
        let loaded: crate::StackState =
            serde_json::from_value(state.clone()).expect("the state loads with no extension");
        assert_eq!(serde_json::to_value(&loaded).unwrap(), state);
    }

    #[test]
    fn built_in_type_with_a_malformed_body_is_refused() {
        serde_json::from_value::<Resource>(json!({ "type": "worker", "id": "api" }))
            .expect_err("a worker needs its code");
        serde_json::from_value::<ResourceOutputs>(json!({ "type": "storage" }))
            .expect_err("storage outputs need their fields");
    }
}