Skip to main content

microvms_core/control/
connector.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Network connectors: a closed intent enum, and the ARN each one derives (TRAP-4).
3//!
4//! # Why an enum rather than a string
5//!
6//! Two reasons, and both are measurements.
7//!
8//! The API takes a **fully-qualified ARN** and rejects the bare name with "Malformed
9//! network connector ARN" (measured 2026-08-05). `NetworkConnector` in the service model
10//! is just `{type: string, max: 2048, min: 1}` — no pattern, no enum — so a free-form
11//! parameter passes every check the model states and fails on the wire, and the value
12//! that reads most natural to write (`"ALL_INGRESS"`) is exactly the one that fails.
13//! Deriving the ARN from an intent means the caller states what they want and the
14//! spelling is not theirs to get wrong.
15//!
16//! # TRAP-11, revised: the shell connector is a variant, on measured ground
17//!
18//! `SHELL_INGRESS` **is** a variant here, and it was not always. This module used to
19//! omit it on the claim that it gated a console-only debugging flow — "not a
20//! programmatic exec path despite the name" — and that omission made requesting it
21//! unwriteable. `docs/PLATFORM.md` (measured 2026-08-15) refutes the claim: the shell
22//! endpoint is a real PTY over a WebSocket, and it is programmatically drivable. The
23//! ground that actually holds is narrower — **one interactive session is not
24//! programmatic exec**: no exec ids, no idempotency, no separated stdout/stderr, no exit
25//! codes. So the variant exists for callers that want the PTY, and the exec path never
26//! requests it; the lifecycle test in [`crate::control::microvm`] asserts a launch
27//! carries exactly the connectors its caller asked for.
28//!
29//! Two measured constraints travel with the variant (both from `docs/PLATFORM.md`):
30//! `ALL_INGRESS` cannot combine with any other ingress connector, and the platform says
31//! so only at token-mint time — `RunMicrovm` accepts the invalid set, the VM reaches
32//! RUNNING, and it bills until something asks for a shell token.
33//! [`crate::control::ControlPlane::run_microvm`] refuses the combination locally
34//! instead. The pair that works is `[HTTP_INGRESS, SHELL_INGRESS]`.
35//!
36//! The sibling half of TRAP-11 — the shell-auth operation — is still closed by the
37//! absence of a method on [`crate::control::ControlPlane`]; see that module's docs.
38
39use crate::region::Region;
40
41/// The Lambda-managed connectors this client will name, and no others.
42///
43/// Named for the *intent* rather than for the wire value, because the intent is what a
44/// caller has: "let the proxy reach the VM" and "let the VM reach the internet". The
45/// wire spellings ([`ConnectorIntent::wire_name`]) are an implementation detail of the
46/// ARN.
47#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
48pub enum ConnectorIntent {
49    /// Lets the endpoint proxy reach the VM. Required for any session to work.
50    ///
51    /// The union that cannot be intersected: the platform refuses to combine it with
52    /// any other ingress connector, and only at token-mint time — see the module docs.
53    /// A VM that needs a shell requests [`ConnectorIntent::HttpIngress`] plus
54    /// [`ConnectorIntent::ShellIngress`] instead of this.
55    AllIngress,
56    /// Lets the endpoint proxy reach the VM's HTTP surface, without the shell.
57    ///
58    /// The finer-grained sibling of [`ConnectorIntent::AllIngress`], and the half of the
59    /// measured pair `[HTTP_INGRESS, SHELL_INGRESS]` that keeps the daemon reachable
60    /// (`docs/PLATFORM.md`, measured 2026-08-15).
61    HttpIngress,
62    /// Lets the VM mint shell tokens and serve its PTY WebSocket.
63    ///
64    /// One interactive session, not programmatic exec — the module docs carry the
65    /// revision of TRAP-11 that admitted this variant. Never combined with
66    /// [`ConnectorIntent::AllIngress`]; the pair that launches and mints is with
67    /// [`ConnectorIntent::HttpIngress`].
68    ShellIngress,
69    /// Lets the VM reach the internet.
70    ///
71    /// Omitted by default, which is how you get a VM with no outbound network — the
72    /// right default for a daemon that needs none.
73    Egress,
74}
75
76impl ConnectorIntent {
77    /// Every intent, so a test can enumerate the complete set.
78    ///
79    /// Maintained by hand; the tests below assert its length, so an edit here is a
80    /// deliberate one. The set grew from two to four when TRAP-11 was revised — see the
81    /// module docs.
82    pub const ALL: [ConnectorIntent; 4] = [
83        ConnectorIntent::AllIngress,
84        ConnectorIntent::HttpIngress,
85        ConnectorIntent::ShellIngress,
86        ConnectorIntent::Egress,
87    ];
88
89    /// The connector's name as the ARN spells it.
90    ///
91    /// `INTERNET_EGRESS` rather than `EGRESS`: the wire name is not the intent's name,
92    /// which is the second reason this is a table rather than a `Display` derive.
93    pub fn wire_name(self) -> &'static str {
94        match self {
95            ConnectorIntent::AllIngress => "ALL_INGRESS",
96            ConnectorIntent::HttpIngress => "HTTP_INGRESS",
97            ConnectorIntent::ShellIngress => "SHELL_INGRESS",
98            ConnectorIntent::Egress => "INTERNET_EGRESS",
99        }
100    }
101
102    /// The fully-qualified ARN for this connector in `region`.
103    ///
104    /// One interpolation for both directions. The Python client's earlier shape derived
105    /// the egress ARN by string-replacing `ALL_INGRESS` inside the ingress one, which
106    /// produced a valid ARN only as long as the two names never became substrings of
107    /// each other (`sandbox.py:391-398`).
108    ///
109    /// The doubled `aws-network-connector` segment is not a typo: the resource type and
110    /// the resource name are both present, and the format is copied from
111    /// `sandbox.py:398` rather than reconstructed from the ARN grammar.
112    pub fn arn(self, region: &Region) -> String {
113        format!(
114            "arn:aws:lambda:{}:aws:network-connector:aws-network-connector:{}",
115            region.as_str(),
116            self.wire_name()
117        )
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    /// The exact ARN, as a literal, for the region the measurements were taken in. The
126    /// format came from a measurement rather than from the ARN grammar, so a
127    /// reconstruction of it in the test would agree with a reconstruction in the code.
128    #[test]
129    fn the_ingress_arn_is_the_measured_literal() {
130        assert_eq!(
131            ConnectorIntent::AllIngress.arn(&Region::UsEast1),
132            "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:ALL_INGRESS"
133        );
134    }
135
136    /// The egress ARN, likewise — and note the wire name is `INTERNET_EGRESS` while the
137    /// variant is `Egress`, which is the thing a `Display` derive would have got wrong.
138    #[test]
139    fn the_egress_arn_uses_the_internet_egress_wire_name() {
140        assert_eq!(
141            ConnectorIntent::Egress.arn(&Region::UsEast1),
142            "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:INTERNET_EGRESS"
143        );
144    }
145
146    /// The region is interpolated rather than fixed, for every region including the
147    /// escape hatch. TRAP-4 is "for the request region", and a hardcoded `us-east-1`
148    /// would pass both tests above.
149    #[test]
150    fn the_arn_carries_the_request_region_for_every_region() {
151        for region in crate::region::MICROVM_REGIONS {
152            let arn = ConnectorIntent::AllIngress.arn(&region);
153            assert!(
154                arn.contains(&format!(":lambda:{}:aws:", region.as_str())),
155                "{arn}"
156            );
157        }
158        let unlisted = Region::unlisted("me-south-1");
159        assert!(
160            ConnectorIntent::Egress
161                .arn(&unlisted)
162                .contains(":lambda:me-south-1:aws:"),
163            "the escape hatch still derives an ARN for its own region"
164        );
165    }
166
167    /// TRAP-11, rewritten on the ground that holds. This test used to assert no intent
168    /// names `SHELL_INGRESS`, standing on the claim that the shell was a console-only
169    /// debugging path — a claim `docs/PLATFORM.md` (measured 2026-08-15) refutes: the
170    /// shell endpoint is a real PTY and programmatically drivable. What holds instead is
171    /// that **one interactive session is not programmatic exec**, so the variant exists,
172    /// exactly one intent renders it, and the check on the rendered ARNs stays — a
173    /// variant named something else must not render `SHELL_INGRESS` either.
174    ///
175    /// The other half of the revised guard — a launch carries exactly the connectors its
176    /// caller asked for — lives with the lifecycle test in `microvm.rs`.
177    ///
178    /// **Falsification** — add a second variant whose wire name contains `SHELL`, or
179    /// rename [`ConnectorIntent::ShellIngress`]'s wire name, and this fails.
180    #[test]
181    fn shell_ingress_is_one_deliberate_intent() {
182        assert_eq!(
183            ConnectorIntent::ALL.len(),
184            4,
185            "four intents, and shell is deliberately one of them"
186        );
187        let shells: Vec<ConnectorIntent> = ConnectorIntent::ALL
188            .into_iter()
189            .filter(|intent| intent.wire_name().contains("SHELL"))
190            .collect();
191        assert_eq!(
192            shells,
193            vec![ConnectorIntent::ShellIngress],
194            "exactly one intent names the shell, and it is the one that says so"
195        );
196        assert_eq!(
197            ConnectorIntent::ShellIngress.arn(&Region::UsEast1),
198            "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:SHELL_INGRESS"
199        );
200    }
201
202    /// `HTTP_INGRESS`, the measured literal — the finer-grained ingress that pairs with
203    /// the shell connector, since `ALL_INGRESS` cannot combine with either
204    /// (`docs/PLATFORM.md`, measured 2026-08-15).
205    #[test]
206    fn the_http_ingress_arn_is_the_measured_literal() {
207        assert_eq!(
208            ConnectorIntent::HttpIngress.arn(&Region::UsEast1),
209            "arn:aws:lambda:us-east-1:aws:network-connector:aws-network-connector:HTTP_INGRESS"
210        );
211    }
212
213    /// No two intents render the same ARN. The string-replace shape this replaced
214    /// could produce two identical ARNs if the names ever became substrings of one
215    /// another, and that is the failure this asserts against — now across the whole
216    /// set rather than the original pair.
217    #[test]
218    fn no_two_intents_render_the_same_arn() {
219        let arns: Vec<String> = ConnectorIntent::ALL
220            .iter()
221            .map(|intent| intent.arn(&Region::EuWest1))
222            .collect();
223        for (i, a) in arns.iter().enumerate() {
224            for b in &arns[i + 1..] {
225                assert_ne!(a, b);
226            }
227        }
228        assert!(!arns[0].contains("INTERNET"), "{}", arns[0]);
229    }
230
231    /// Every derived ARN clears the model's `NetworkConnector` bounds (min 1, max 2048),
232    /// including for the longest region name. The model states no pattern, so length is
233    /// the only constraint there is to check — and it is checked here rather than
234    /// trusted because a 2048-character ARN would be rejected on the wire with the same
235    /// "malformed" message a bare name gets.
236    #[test]
237    fn every_derived_arn_fits_the_models_connector_bounds() {
238        for region in crate::region::MICROVM_REGIONS {
239            for intent in ConnectorIntent::ALL {
240                let arn = intent.arn(&region);
241                assert!((1..=2048).contains(&arn.len()), "{arn}");
242                assert!(arn.starts_with("arn:aws:lambda:"), "{arn}");
243            }
244        }
245    }
246}