mxc-sdk 1.2.0

Importable library for starting MXC sandboxes in-process, with streaming stdio.
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! Typed Rust SDK input to shared request normalization.
//!
//! This module is public only because `mxc-sdk` owns the high-level SDK
//! types while `mxc_common` owns the private normalization boundary.

use crate::mxc_contract::ContractVersion;

use crate::mxc_common::common_request_ir::CommonRequestIR;
use crate::mxc_common::error::WxcError;
use crate::mxc_common::state_aware_input::StateAwareInput;
use crate::mxc_common::state_aware_operation::StateAwareOperation;
use crate::mxc_common::wire;

#[derive(Debug, Clone)]
pub struct SdkProcessInput {
    pub command_line: String,
    pub cwd: Option<String>,
    pub env: Option<Vec<String>>,
    pub inherit_default_env: Option<bool>,
    pub timeout: Option<u32>,
}

#[derive(Debug, Clone)]
pub struct SdkFilesystemInput {
    pub readwrite_paths: Vec<String>,
    pub readonly_paths: Vec<String>,
    pub denied_paths: Vec<String>,
}

#[derive(Debug, Clone, Copy)]
pub enum SdkNetworkAction {
    Allow,
    Deny,
}

#[derive(Debug, Clone, Copy)]
pub enum SdkNetworkProtocol {
    Tcp,
    Udp,
    Icmp,
    Any,
}

#[derive(Debug, Clone)]
pub struct SdkNetworkPeerInput {
    pub cidr: String,
    pub except: Option<Vec<String>>,
}

#[derive(Debug, Clone)]
pub struct SdkNetworkPortInput {
    pub protocol: Option<SdkNetworkProtocol>,
    pub port: Option<u16>,
    pub end_port: Option<u16>,
}

#[derive(Debug, Clone)]
pub struct SdkNetworkRuleInput {
    pub to: Option<Vec<SdkNetworkPeerInput>>,
    pub ports: Option<Vec<SdkNetworkPortInput>>,
}

#[derive(Debug, Clone)]
pub struct SdkNetworkEgressInput {
    pub default: Option<SdkNetworkAction>,
    pub allow: Option<Vec<SdkNetworkRuleInput>>,
    pub deny: Option<Vec<SdkNetworkRuleInput>>,
}

#[derive(Debug, Clone)]
pub struct SdkNetworkIngressInput {
    pub default: Option<SdkNetworkAction>,
    pub host_loopback: Option<SdkNetworkAction>,
}

#[derive(Debug, Clone)]
pub struct SdkNetworkInput {
    pub egress: Option<SdkNetworkEgressInput>,
    pub ingress: Option<SdkNetworkIngressInput>,
}

#[derive(Debug, Clone)]
pub struct SdkRuntimeConfigInput {
    pub network_proxy: Option<String>,
}

/// Common fields supplied by a trusted typed Rust SDK lifecycle request.
///
/// The selected contract is retained for compatibility, diagnostics, and
/// telemetry. The operation remains the sole phase and routing authority.
#[derive(Debug, Clone)]
pub struct SdkStateAwareInput {
    version: ContractVersion,
    operation: StateAwareOperation,
    pub process: Option<SdkProcessInput>,
    pub filesystem: Option<SdkFilesystemInput>,
    pub network: Option<SdkNetworkInput>,
    pub runtime_config: Option<SdkRuntimeConfigInput>,
    pub telemetry_opt_in: Option<bool>,
}

impl SdkStateAwareInput {
    /// Create typed lifecycle normalization input for the selected exact
    /// compatibility semantics.
    pub fn new(version: ContractVersion, operation: StateAwareOperation) -> Result<Self, WxcError> {
        if !matches!(
            version,
            ContractVersion::V1_0_0 | ContractVersion::V1_1_0Alpha
        ) {
            return Err(WxcError::ConfigParse(format!(
                "typed state-aware Rust SDK requests require schema version \
                 1.0.0 or 1.1.0-alpha, got {}",
                version.as_str()
            )));
        }
        let is_windows_sandbox = matches!(
            operation,
            StateAwareOperation::Provision(
                crate::mxc_common::state_aware_operation::StateAwareProvision::WindowsSandbox
            )
        ) || operation
            .sandbox_id()
            .is_some_and(|sandbox_id| sandbox_id.starts_with("wsb:"));
        if is_windows_sandbox {
            return Err(WxcError::ConfigParse(
                "typed state-aware Rust SDK requests do not support Windows Sandbox; \
                 use raw exact 1.1.0-alpha JSON"
                    .to_string(),
            ));
        }
        if wslc_port_mappings_requested(&operation) && version != ContractVersion::V1_1_0Alpha {
            return Err(WxcError::ConfigParse(format!(
                "WSLC state-aware provision port mappings require schema version \
                 1.1.0-alpha, got {}",
                version.as_str()
            )));
        }
        if wslc_session_requested(&operation) && version != ContractVersion::V1_1_0Alpha {
            return Err(WxcError::ConfigParse(format!(
                "state-aware session operations require schema version \
                 1.1.0-alpha, got {}",
                version.as_str()
            )));
        }
        operation.validate()?;
        Ok(Self {
            version,
            operation,
            process: None,
            filesystem: None,
            network: None,
            runtime_config: None,
            telemetry_opt_in: None,
        })
    }

    pub(crate) fn into_normalization_input(self) -> Result<StateAwareInput, WxcError> {
        let common = CommonRequestIR {
            schema: None,
            comment: None,
            source_contract: self.version,
            phase: None,
            sandbox_id: None,
            container_id: None,
            containment: None,
            process: self.process.map(|process| wire::Process {
                command_line: Some(process.command_line),
                cwd: process.cwd,
                env: process.env,
                inherit_default_env: process.inherit_default_env,
                timeout: process.timeout,
            }),
            lifecycle: None,
            process_container: None,
            lxc: None,
            wslc: None,
            filesystem: self.filesystem.map(|filesystem| wire::Filesystem {
                readwrite_paths: Some(filesystem.readwrite_paths),
                readonly_paths: Some(filesystem.readonly_paths),
                denied_paths: Some(filesystem.denied_paths),
            }),
            network: self.network.map(map_network),
            runtime_config: self.runtime_config.map(|runtime| wire::RuntimeConfig {
                network_proxy: runtime.network_proxy,
            }),
            ui: None,
            seatbelt: None,
            telemetry: self.telemetry_opt_in.map(|enabled| wire::Telemetry {
                enabled: Some(enabled),
            }),
            test_feature: None,
            windows_sandbox: None,
            hyperlight: None,
            microvm: None,
        };
        StateAwareInput::new(common, self.operation)
    }
}

/// WSLC port mappings exist only in the `1.1.0-alpha` exact contract.
fn wslc_port_mappings_requested(operation: &StateAwareOperation) -> bool {
    matches!(
        operation,
        StateAwareOperation::Provision(crate::mxc_common::state_aware_operation::StateAwareProvision::Wslc(
            Some(config)
        )) if config.port_mappings.is_some()
    )
}

fn wslc_session_requested(operation: &StateAwareOperation) -> bool {
    match operation {
        StateAwareOperation::ProvisionSession(_)
        | StateAwareOperation::DeprovisionSession { .. } => true,
        StateAwareOperation::Provision(
            crate::mxc_common::state_aware_operation::StateAwareProvision::Wslc(Some(config)),
        ) => config.session_id.is_some(),
        _ => false,
    }
}

fn map_network(network: SdkNetworkInput) -> wire::Network {
    wire::Network {
        egress: network.egress.map(|egress| wire::NetworkEgress {
            default: egress.default.map(map_action),
            allow: egress.allow.map(map_rules),
            deny: egress.deny.map(map_rules),
        }),
        ingress: network.ingress.map(|ingress| wire::NetworkIngress {
            default: ingress.default.map(map_action),
            host_loopback: ingress.host_loopback.map(map_action),
        }),
    }
}

fn map_rules(rules: Vec<SdkNetworkRuleInput>) -> Vec<wire::NetworkRule> {
    rules
        .into_iter()
        .map(|rule| wire::NetworkRule {
            to: rule.to.map(|peers| {
                peers
                    .into_iter()
                    .map(|peer| wire::NetworkPeer {
                        cidr: peer.cidr,
                        except: peer.except,
                    })
                    .collect()
            }),
            ports: rule.ports.map(|ports| {
                ports
                    .into_iter()
                    .map(|port| wire::NetworkPort {
                        protocol: port.protocol.map(map_protocol),
                        port: port.port,
                        end_port: port.end_port,
                    })
                    .collect()
            }),
        })
        .collect()
}

fn map_action(action: SdkNetworkAction) -> wire::NetworkAction {
    match action {
        SdkNetworkAction::Allow => wire::NetworkAction::Allow,
        SdkNetworkAction::Deny => wire::NetworkAction::Deny,
    }
}

fn map_protocol(protocol: SdkNetworkProtocol) -> wire::NetworkProtocol {
    match protocol {
        SdkNetworkProtocol::Tcp => wire::NetworkProtocol::Tcp,
        SdkNetworkProtocol::Udp => wire::NetworkProtocol::Udp,
        SdkNetworkProtocol::Icmp => wire::NetworkProtocol::Icmp,
        SdkNetworkProtocol::Any => wire::NetworkProtocol::Any,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn typed_v1_rejects_windows_sandbox_ids_for_every_non_provision_phase() {
        for operation in [
            StateAwareOperation::Start {
                sandbox_id: "wsb:abcd1234".to_string(),
            },
            StateAwareOperation::Exec {
                sandbox_id: "wsb:abcd1234".to_string(),
            },
            StateAwareOperation::Stop {
                sandbox_id: "wsb:abcd1234".to_string(),
            },
            StateAwareOperation::Deprovision {
                sandbox_id: "wsb:abcd1234".to_string(),
            },
        ] {
            let error = SdkStateAwareInput::new(ContractVersion::V1_0_0, operation)
                .expect_err("typed v1 must keep Windows Sandbox on the raw exact lane");
            assert!(error.to_string().contains("do not support Windows Sandbox"));
        }
    }

    fn mapping(windows_port: u16, container_port: u16) -> crate::mxc_common::models::PortMapping {
        crate::mxc_common::models::PortMapping {
            windows_port,
            container_port,
            protocol: "tcp".to_string(),
        }
    }

    fn wslc_provision(
        port_mappings: Option<Vec<crate::mxc_common::models::PortMapping>>,
    ) -> StateAwareOperation {
        StateAwareOperation::Provision(
            crate::mxc_common::state_aware_operation::StateAwareProvision::Wslc(Some(
                crate::mxc_common::models::WslcProvisionConfig {
                    image: None,
                    image_tar_path: None,
                    session_id: None,
                    port_mappings,
                },
            )),
        )
    }

    #[test]
    fn the_stable_contract_rejects_port_mappings_even_when_the_list_is_empty() {
        for mappings in [Vec::new(), vec![mapping(8080, 80)]] {
            let error =
                SdkStateAwareInput::new(ContractVersion::V1_0_0, wslc_provision(Some(mappings)))
                    .expect_err("1.0.0 does not declare the field");
            assert!(
                error.to_string().contains("1.1.0-alpha"),
                "the message must name the version that declares it; got: {error}"
            );
        }
    }

    #[test]
    fn the_alpha_contract_accepts_port_mappings() {
        SdkStateAwareInput::new(
            ContractVersion::V1_1_0Alpha,
            wslc_provision(Some(vec![mapping(8080, 80)])),
        )
        .expect("1.1.0-alpha declares the field");
    }

    #[test]
    fn absent_port_mappings_are_accepted_on_both_contracts() {
        for version in [ContractVersion::V1_0_0, ContractVersion::V1_1_0Alpha] {
            SdkStateAwareInput::new(version, wslc_provision(None))
                .expect("an absent list clears the gate on every supported contract");
        }
    }

    fn wslc_provision_in_session(session_id: Option<&str>) -> StateAwareOperation {
        StateAwareOperation::Provision(
            crate::mxc_common::state_aware_operation::StateAwareProvision::Wslc(Some(
                crate::mxc_common::models::WslcProvisionConfig {
                    session_id: session_id.map(str::to_string),
                    ..Default::default()
                },
            )),
        )
    }

    #[test]
    fn the_stable_contract_rejects_session_ids_even_when_empty() {
        for session_id in ["", "wslcsess:0123456789abcdef0123456789abcdef"] {
            let error = SdkStateAwareInput::new(
                ContractVersion::V1_0_0,
                wslc_provision_in_session(Some(session_id)),
            )
            .expect_err("1.0.0 does not declare wslc.provision.sessionId");
            assert!(
                error.to_string().contains("1.1.0-alpha"),
                "the message must name the contract that declares session placement: {error}"
            );
        }
    }

    #[test]
    fn the_alpha_contract_preserves_session_id_presence() {
        for session_id in ["", "wslcsess:0123456789abcdef0123456789abcdef"] {
            let operation = wslc_provision_in_session(Some(session_id));
            let input = SdkStateAwareInput::new(ContractVersion::V1_1_0Alpha, operation.clone())
                .expect("1.1.0-alpha declares wslc.provision.sessionId");
            let (_, observed) = input.into_normalization_input().unwrap().into_parts();
            assert_eq!(observed, operation);
        }
    }

    #[test]
    fn an_omitted_session_id_is_accepted_on_both_contracts() {
        for version in [ContractVersion::V1_0_0, ContractVersion::V1_1_0Alpha] {
            SdkStateAwareInput::new(version, wslc_provision_in_session(None))
                .expect("unnamed-session provisioning must remain compatible");
        }
    }

    #[test]
    fn the_constructor_validates_ports_before_normalization() {
        for mappings in [
            vec![mapping(0, 80)],
            vec![mapping(8080, 0)],
            vec![mapping(8080, 80), mapping(8080, 81)],
        ] {
            let error = SdkStateAwareInput::new(
                ContractVersion::V1_1_0Alpha,
                wslc_provision(Some(mappings)),
            )
            .expect_err("a direct caller bypasses the exact contract's structural checks");
            assert!(
                error.to_string().contains("portMappings"),
                "the message must name the field; got: {error}"
            );
        }
    }
}