microsandbox 0.6.9

`microsandbox` is the core library for the microsandbox project.
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
//! Sparse construction-time configuration patches for sandbox builders.

use std::collections::BTreeMap;
use std::path::PathBuf;

use microsandbox_image::RegistryAuth;
#[cfg(feature = "net")]
use microsandbox_network::dns::Nameserver;
#[cfg(feature = "net")]
use microsandbox_network::policy::{Action, NetworkPolicy, Rule};
#[cfg(feature = "net")]
use microsandbox_types::{HostPattern, SecretInjection, SecretSource};
use microsandbox_types::{PullPolicy, Rlimit, SecurityProfile};
#[cfg(feature = "net")]
use zeroize::Zeroizing;

use super::{Patch, SandboxBuilder, VolumeMount};

//--------------------------------------------------------------------------------------------------
// Types
//--------------------------------------------------------------------------------------------------

/// A sparse, typed patch applied while constructing one sandbox.
///
/// Patches contain no file-loading or interpolation behavior. Callers may build them directly or
/// translate another representation, such as a CLI configuration file, into these SDK types.
#[derive(Clone, Default)]
pub struct SandboxConfigPatch {
    image: Option<SandboxImagePatch>,
    pull_policy: Option<PullPolicy>,
    registry_auth: Option<RegistryAuth>,
    resources: ResourceConfigPatch,
    runtime: RuntimeConfigPatch,
    filesystem: FilesystemConfigPatch,
    scripts: ScriptConfigPatch,
    #[cfg(feature = "net")]
    network: NetworkConfigPatch,
    #[cfg(feature = "net")]
    secrets: SecretConfigPatch,
}

/// A root filesystem selection carried by a [`SandboxConfigPatch`].
#[derive(Debug, Clone)]
pub enum SandboxImagePatch {
    /// A normal image spelling accepted by [`SandboxBuilder::image`].
    Image(String),
    /// An explicit OCI image and optional managed root-disk size in MiB.
    Oci {
        /// OCI image reference.
        reference: String,
        /// Optional managed root-disk size in MiB.
        root_disk_mib: Option<u32>,
    },
    /// A host disk image root filesystem.
    Disk {
        /// Host path to the disk image.
        path: PathBuf,
        /// Optional inner filesystem type.
        fstype: Option<String>,
    },
    /// A host directory root filesystem.
    Bind(PathBuf),
    /// A saved sandbox snapshot.
    Snapshot(String),
}

/// Sparse resource and lifecycle limits.
#[derive(Debug, Clone, Default)]
pub struct ResourceConfigPatch {
    cpus: Option<u8>,
    memory_mib: Option<u32>,
    max_duration_secs: Option<u64>,
    idle_timeout_secs: Option<u64>,
    rlimits: Option<Vec<Rlimit>>,
}

/// Sparse runtime configuration.
#[derive(Debug, Clone, Default)]
pub struct RuntimeConfigPatch {
    workdir: Option<String>,
    shell: Option<String>,
    user: Option<String>,
    hostname: Option<String>,
    security: Option<SecurityProfile>,
    entrypoint: Option<Vec<String>>,
    cmd: Option<Vec<String>>,
    env: Option<BTreeMap<String, String>>,
    labels: Option<BTreeMap<String, String>>,
    init: Option<InitConfigPatch>,
}

/// Sparse guest-init handoff configuration.
#[derive(Debug, Clone, Default)]
pub struct InitConfigPatch {
    cmd: Option<String>,
    args: Option<Vec<String>>,
    env: Option<BTreeMap<String, String>>,
}

/// Sparse filesystem configuration.
#[derive(Debug, Clone, Default)]
pub struct FilesystemConfigPatch {
    mounts: Option<Vec<VolumeMount>>,
    patch_file_operations: Option<Vec<Patch>>,
    patches: Option<Vec<Patch>>,
}

/// Sparse named script configuration.
#[derive(Debug, Clone, Default)]
pub struct ScriptConfigPatch {
    scripts: BTreeMap<String, String>,
}

/// Sparse network configuration.
#[cfg(feature = "net")]
#[derive(Clone, Default)]
pub struct NetworkConfigPatch {
    policy: NetworkPolicyConfigPatch,
    ports: Option<Vec<microsandbox_network::config::PublishedPort>>,
    dns: DnsConfigPatch,
    tls: TlsConfigPatch,
    trust_host_cas: Option<bool>,
    max_connections: Option<usize>,
}

/// Sparse network-policy configuration.
#[cfg(feature = "net")]
#[derive(Debug, Clone, Default)]
pub struct NetworkPolicyConfigPatch {
    base: Option<NetworkPolicy>,
    deny: Option<Vec<Rule>>,
    allow: Option<Vec<Rule>>,
}

/// Sparse DNS configuration.
#[cfg(feature = "net")]
#[derive(Debug, Clone, Default)]
pub struct DnsConfigPatch {
    rebind_protection: Option<bool>,
    nameservers: Option<Vec<Nameserver>>,
    query_timeout_ms: Option<u64>,
}

/// Sparse TLS-interception configuration.
#[cfg(feature = "net")]
#[derive(Debug, Clone, Default)]
pub struct TlsConfigPatch {
    enabled: Option<bool>,
    bypass: Option<Vec<String>>,
    verify_upstream: Option<bool>,
    block_quic: Option<bool>,
}

/// Sparse protected-secret configuration, keyed by guest environment variable name.
#[cfg(feature = "net")]
#[derive(Clone, Default)]
pub struct SecretConfigPatch {
    secrets: BTreeMap<String, SecretEntryConfigPatch>,
}

/// Sparse configuration for one protected secret.
#[cfg(feature = "net")]
#[derive(Clone, Default)]
pub struct SecretEntryConfigPatch {
    material: Option<SecretMaterial>,
    allowed_hosts: Option<Vec<HostPattern>>,
    injection: Option<SecretInjection>,
    require_tls_identity: Option<bool>,
}

#[cfg(feature = "net")]
#[derive(Clone)]
enum SecretMaterial {
    Literal(Zeroizing<String>),
    Source(SecretSource),
}

//--------------------------------------------------------------------------------------------------
// Methods: Root Patch
//--------------------------------------------------------------------------------------------------

impl SandboxConfigPatch {
    /// Create an empty patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Replace the image selection in this patch.
    pub fn image(mut self, image: SandboxImagePatch) -> Self {
        self.image = Some(image);
        self
    }

    /// Replace the pull policy in this patch.
    pub fn pull_policy(mut self, policy: PullPolicy) -> Self {
        self.pull_policy = Some(policy);
        self
    }

    /// Replace registry authentication in this patch.
    pub fn registry_auth(mut self, auth: RegistryAuth) -> Self {
        self.registry_auth = Some(auth);
        self
    }

    /// Overlay resource fields into this patch.
    pub fn resources(mut self, patch: ResourceConfigPatch) -> Self {
        self.resources = self.resources.overlay(patch);
        self
    }

    /// Overlay runtime fields into this patch.
    pub fn runtime(mut self, patch: RuntimeConfigPatch) -> Self {
        self.runtime = self.runtime.overlay(patch);
        self
    }

    /// Overlay filesystem fields into this patch.
    pub fn filesystem(mut self, patch: FilesystemConfigPatch) -> Self {
        self.filesystem = self.filesystem.overlay(patch);
        self
    }

    /// Overlay scripts into this patch.
    pub fn scripts(mut self, patch: ScriptConfigPatch) -> Self {
        self.scripts = self.scripts.overlay(patch);
        self
    }

    /// Overlay network fields into this patch.
    #[cfg(feature = "net")]
    pub fn network(mut self, patch: NetworkConfigPatch) -> Self {
        self.network = self.network.overlay(patch);
        self
    }

    /// Overlay protected secrets into this patch.
    #[cfg(feature = "net")]
    pub fn secrets(mut self, patch: SecretConfigPatch) -> Self {
        self.secrets = self.secrets.overlay(patch);
        self
    }

    /// Overlay another root or scoped patch, with the right-hand side taking precedence.
    pub fn overlay<P>(mut self, higher: P) -> Self
    where
        P: Into<SandboxConfigPatch>,
    {
        let higher = higher.into();
        replace(&mut self.image, higher.image);
        replace(&mut self.pull_policy, higher.pull_policy);
        replace(&mut self.registry_auth, higher.registry_auth);
        self.resources = self.resources.overlay(higher.resources);
        self.runtime = self.runtime.overlay(higher.runtime);
        self.filesystem = self.filesystem.overlay(higher.filesystem);
        self.scripts = self.scripts.overlay(higher.scripts);
        #[cfg(feature = "net")]
        {
            self.network = self.network.overlay(higher.network);
            self.secrets = self.secrets.overlay(higher.secrets);
        }
        self
    }

    /// Return the selected image, when this patch supplies one.
    pub fn image_selection(&self) -> Option<&SandboxImagePatch> {
        self.image.as_ref()
    }

    /// Return registry authentication, when this patch supplies it.
    pub fn registry_auth_ref(&self) -> Option<&RegistryAuth> {
        self.registry_auth.as_ref()
    }

    pub(super) fn apply_to(self, mut builder: SandboxBuilder) -> SandboxBuilder {
        if let Some(image) = self.image {
            builder = image.apply(builder);
        }
        if let Some(policy) = self.pull_policy {
            builder = builder.pull_policy(policy);
        }
        if let Some(auth) = self.registry_auth {
            builder = builder.registry(|registry| registry.auth(auth));
        }
        builder = self.resources.apply_to(builder);
        builder = self.runtime.apply_to(builder);
        builder = self.filesystem.apply_to(builder);
        builder = builder.config_scripts(self.scripts.scripts);
        #[cfg(feature = "net")]
        {
            builder = self.network.apply_to(builder);
            builder = self.secrets.apply_to(builder);
        }
        builder
    }
}

impl SandboxBuilder {
    /// Apply a sparse construction patch to this builder.
    ///
    /// Ordinary builder calls chained after `configure` retain last-write-wins precedence.
    pub fn configure(self, patch: SandboxConfigPatch) -> Self {
        patch.apply_to(self)
    }
}

impl SandboxImagePatch {
    /// Human-readable source label suitable for progress output.
    pub fn display(&self) -> String {
        match self {
            Self::Image(value) | Self::Snapshot(value) => value.clone(),
            Self::Oci { reference, .. } => reference.clone(),
            Self::Disk { path, .. } | Self::Bind(path) => path.display().to_string(),
        }
    }

    /// Return an OCI reference suitable for pre-pulling, when applicable.
    pub fn oci_reference(&self) -> Option<&str> {
        match self {
            Self::Image(value) if !microsandbox_utils::looks_like_local_path_text(value) => {
                Some(value)
            }
            Self::Oci { reference, .. } => Some(reference),
            Self::Image(_) | Self::Disk { .. } | Self::Bind(_) | Self::Snapshot(_) => None,
        }
    }

    pub(super) fn apply(self, builder: SandboxBuilder) -> SandboxBuilder {
        match self {
            Self::Image(image) => builder.override_image(image),
            Self::Oci {
                reference,
                root_disk_mib,
            } => match root_disk_mib {
                Some(size) => {
                    builder.override_image_with(|image| image.oci(reference).root_disk(size))
                }
                None => builder.override_image_with(|image| image.oci(reference)),
            },
            Self::Disk { path, fstype } => builder.override_image_with(|mut image| {
                image = image.disk(path);
                if let Some(fstype) = fstype {
                    image = image.fstype(fstype);
                }
                image
            }),
            Self::Bind(path) => builder.override_image_with(|image| image.bind(path)),
            Self::Snapshot(snapshot) => builder.config_snapshot(snapshot),
        }
    }
}

//--------------------------------------------------------------------------------------------------
// Methods: Scoped Patches
//--------------------------------------------------------------------------------------------------

impl ResourceConfigPatch {
    /// Create an empty resource patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the initial vCPU count.
    pub fn cpus(mut self, cpus: u8) -> Self {
        self.cpus = Some(cpus);
        self
    }

    /// Set guest memory in MiB.
    pub fn memory_mib(mut self, memory_mib: u32) -> Self {
        self.memory_mib = Some(memory_mib);
        self
    }

    /// Set the maximum sandbox lifetime in seconds.
    pub fn max_duration_secs(mut self, seconds: u64) -> Self {
        self.max_duration_secs = Some(seconds);
        self
    }

    /// Set the idle timeout in seconds.
    pub fn idle_timeout_secs(mut self, seconds: u64) -> Self {
        self.idle_timeout_secs = Some(seconds);
        self
    }

    /// Replace sandbox-wide resource limits.
    pub fn rlimits(mut self, rlimits: Vec<Rlimit>) -> Self {
        self.rlimits = Some(rlimits);
        self
    }

    /// Overlay another resource patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        replace(&mut self.cpus, higher.cpus);
        replace(&mut self.memory_mib, higher.memory_mib);
        replace(&mut self.max_duration_secs, higher.max_duration_secs);
        replace(&mut self.idle_timeout_secs, higher.idle_timeout_secs);
        replace(&mut self.rlimits, higher.rlimits);
        self
    }

    fn apply_to(self, mut builder: SandboxBuilder) -> SandboxBuilder {
        if let Some(cpus) = self.cpus {
            builder = builder.cpus(cpus);
        }
        if let Some(memory) = self.memory_mib {
            builder = builder.memory(memory);
        }
        if let Some(seconds) = self.max_duration_secs {
            builder = builder.max_duration(seconds);
        }
        if let Some(seconds) = self.idle_timeout_secs {
            builder = builder.idle_timeout(seconds);
        }
        for limit in self.rlimits.unwrap_or_default() {
            builder = builder.rlimit_range(limit.resource, limit.soft, limit.hard);
        }
        builder
    }
}

impl RuntimeConfigPatch {
    /// Create an empty runtime patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the default guest working directory.
    pub fn workdir(mut self, value: impl Into<String>) -> Self {
        self.workdir = Some(value.into());
        self
    }

    /// Set the command shell.
    pub fn shell(mut self, value: impl Into<String>) -> Self {
        self.shell = Some(value.into());
        self
    }

    /// Set the guest user.
    pub fn user(mut self, value: impl Into<String>) -> Self {
        self.user = Some(value.into());
        self
    }

    /// Set the guest hostname.
    pub fn hostname(mut self, value: impl Into<String>) -> Self {
        self.hostname = Some(value.into());
        self
    }

    /// Set the in-guest security profile.
    pub fn security(mut self, value: SecurityProfile) -> Self {
        self.security = Some(value);
        self
    }

    /// Replace the image entrypoint override.
    pub fn entrypoint(mut self, value: Vec<String>) -> Self {
        self.entrypoint = Some(value);
        self
    }

    /// Replace the image command override.
    pub fn cmd(mut self, value: Vec<String>) -> Self {
        self.cmd = Some(value);
        self
    }

    /// Overlay environment variables by key.
    pub fn env(mut self, value: BTreeMap<String, String>) -> Self {
        merge_map(&mut self.env, Some(value));
        self
    }

    /// Overlay labels by key.
    pub fn labels(mut self, value: BTreeMap<String, String>) -> Self {
        merge_map(&mut self.labels, Some(value));
        self
    }

    /// Overlay guest-init handoff fields.
    pub fn init(mut self, value: InitConfigPatch) -> Self {
        self.init = Some(match self.init.take() {
            Some(current) => current.overlay(value),
            None => value,
        });
        self
    }

    /// Overlay another runtime patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        replace(&mut self.workdir, higher.workdir);
        replace(&mut self.shell, higher.shell);
        replace(&mut self.user, higher.user);
        replace(&mut self.hostname, higher.hostname);
        replace(&mut self.security, higher.security);
        replace(&mut self.entrypoint, higher.entrypoint);
        replace(&mut self.cmd, higher.cmd);
        merge_map(&mut self.env, higher.env);
        merge_map(&mut self.labels, higher.labels);
        if let Some(init) = higher.init {
            self = self.init(init);
        }
        self
    }

    fn apply_to(self, mut builder: SandboxBuilder) -> SandboxBuilder {
        if let Some(value) = self.workdir {
            builder = builder.workdir(value);
        }
        if let Some(value) = self.shell {
            builder = builder.shell(value);
        }
        if let Some(value) = self.user {
            builder = builder.user(value);
        }
        if let Some(value) = self.hostname {
            builder = builder.hostname(value);
        }
        if let Some(value) = self.security {
            builder = builder.security(value);
        }
        if let Some(value) = self.entrypoint {
            builder = builder.entrypoint(value);
        }
        if let Some(value) = self.cmd {
            builder = builder.cmd(value);
        }
        if let Some(value) = self.env {
            builder = builder.envs(value);
        }
        if let Some(value) = self.labels {
            builder = builder.labels(value);
        }
        if let Some(init) = self.init {
            builder = init.apply_to(builder);
        }
        builder
    }
}

impl InitConfigPatch {
    /// Create an empty init patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the guest init command.
    pub fn cmd(mut self, value: impl Into<String>) -> Self {
        self.cmd = Some(value.into());
        self
    }

    /// Replace init arguments.
    pub fn args(mut self, value: Vec<String>) -> Self {
        self.args = Some(value);
        self
    }

    /// Overlay init environment variables by key.
    pub fn env(mut self, value: BTreeMap<String, String>) -> Self {
        merge_map(&mut self.env, Some(value));
        self
    }

    /// Overlay another init patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        replace(&mut self.cmd, higher.cmd);
        replace(&mut self.args, higher.args);
        merge_map(&mut self.env, higher.env);
        self
    }

    fn apply_to(self, mut builder: SandboxBuilder) -> SandboxBuilder {
        let Some(cmd) = self.cmd else {
            return builder.config_error("init.cmd is required when init configuration is present");
        };
        let args = self.args.unwrap_or_default();
        let env = self.env.unwrap_or_default();
        if args.is_empty() && env.is_empty() {
            builder = builder.init(cmd);
        } else {
            builder = builder.init_with(cmd, |init| init.args(args).envs(env));
        }
        builder
    }
}

impl FilesystemConfigPatch {
    /// Create an empty filesystem patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Replace mounts.
    pub fn mounts(mut self, value: Vec<VolumeMount>) -> Self {
        self.mounts = Some(value);
        self
    }

    /// Replace operations loaded from external patch files.
    pub fn patch_file_operations(mut self, value: Vec<Patch>) -> Self {
        self.patch_file_operations = Some(value);
        self
    }

    /// Replace inline root filesystem patches.
    pub fn patches(mut self, value: Vec<Patch>) -> Self {
        self.patches = Some(value);
        self
    }

    /// Overlay another filesystem patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        replace(&mut self.mounts, higher.mounts);
        replace(
            &mut self.patch_file_operations,
            higher.patch_file_operations,
        );
        replace(&mut self.patches, higher.patches);
        self
    }

    fn apply_to(self, mut builder: SandboxBuilder) -> SandboxBuilder {
        for mount in self.mounts.unwrap_or_default() {
            builder = builder.add_volume_mount(mount);
        }
        for patch in self.patch_file_operations.unwrap_or_default() {
            builder = builder.add_patch(patch);
        }
        for patch in self.patches.unwrap_or_default() {
            builder = builder.add_patch(patch);
        }
        builder
    }
}

impl ScriptConfigPatch {
    /// Create an empty script patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert or replace one named shell snippet.
    pub fn script(mut self, name: impl Into<String>, body: impl Into<String>) -> Self {
        self.scripts.insert(name.into(), body.into());
        self
    }

    /// Insert or replace several named shell snippets.
    pub fn scripts(
        mut self,
        scripts: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
    ) -> Self {
        self.scripts.extend(
            scripts
                .into_iter()
                .map(|(name, body)| (name.into(), body.into())),
        );
        self
    }

    /// Overlay another script patch by name.
    pub fn overlay(mut self, higher: Self) -> Self {
        self.scripts.extend(higher.scripts);
        self
    }
}

//--------------------------------------------------------------------------------------------------
// Methods: Network Patches
//--------------------------------------------------------------------------------------------------

#[cfg(feature = "net")]
impl NetworkConfigPatch {
    /// Create an empty network patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Overlay sparse policy fields.
    pub fn policy(mut self, value: NetworkPolicyConfigPatch) -> Self {
        self.policy = self.policy.overlay(value);
        self
    }

    /// Replace published ports.
    pub fn ports(mut self, value: Vec<microsandbox_network::config::PublishedPort>) -> Self {
        self.ports = Some(value);
        self
    }

    /// Overlay DNS fields.
    pub fn dns(mut self, value: DnsConfigPatch) -> Self {
        self.dns = self.dns.overlay(value);
        self
    }

    /// Overlay TLS fields.
    pub fn tls(mut self, value: TlsConfigPatch) -> Self {
        self.tls = self.tls.overlay(value);
        self
    }

    /// Set whether host certificate authorities are trusted in the guest.
    pub fn trust_host_cas(mut self, enabled: bool) -> Self {
        self.trust_host_cas = Some(enabled);
        self
    }

    /// Set the maximum number of concurrent network connections.
    pub fn max_connections(mut self, value: usize) -> Self {
        self.max_connections = Some(value);
        self
    }

    /// Overlay another network patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        self.policy = self.policy.overlay(higher.policy);
        replace(&mut self.ports, higher.ports);
        self.dns = self.dns.overlay(higher.dns);
        self.tls = self.tls.overlay(higher.tls);
        replace(&mut self.trust_host_cas, higher.trust_host_cas);
        replace(&mut self.max_connections, higher.max_connections);
        self
    }

    fn apply_to(self, mut builder: SandboxBuilder) -> SandboxBuilder {
        let (policy, configured_rules) = self.policy.materialize();
        let ports = self.ports;
        let dns = self.dns;
        let tls = self.tls;
        let trust_host_cas = self.trust_host_cas;
        let max_connections = self.max_connections;

        if policy.is_some() {
            builder = builder.config_network_rules(configured_rules);
        }
        builder = builder.network(move |mut network| {
            if let Some(policy) = policy {
                network = network.policy(policy);
            }
            if let Some(ports) = ports {
                for port in ports {
                    network = match port.protocol {
                        microsandbox_network::config::PortProtocol::Tcp => {
                            network.port_bind(port.host_bind, port.host_port, port.guest_port)
                        }
                        microsandbox_network::config::PortProtocol::Udp => {
                            network.port_udp_bind(port.host_bind, port.host_port, port.guest_port)
                        }
                    };
                }
            }
            if dns.is_present() {
                network = network.dns_overlay(move |mut value| {
                    if let Some(enabled) = dns.rebind_protection {
                        value = value.rebind_protection(enabled);
                    }
                    if let Some(nameservers) = dns.nameservers {
                        value = value.nameservers(nameservers);
                    }
                    if let Some(timeout) = dns.query_timeout_ms {
                        value = value.query_timeout_ms(timeout);
                    }
                    value
                });
            }
            if let Some(enabled) = tls.enabled {
                network = network.tls_overlay(move |mut value| {
                    value = value.enabled(enabled);
                    if let Some(bypass) = tls.bypass {
                        for pattern in bypass {
                            value = value.bypass(pattern);
                        }
                    }
                    if let Some(verify) = tls.verify_upstream {
                        value = value.verify_upstream(verify);
                    }
                    if let Some(block) = tls.block_quic {
                        value = value.block_quic(block);
                    }
                    value
                });
            }
            if let Some(enabled) = trust_host_cas {
                network = network.trust_host_cas(enabled);
            }
            if let Some(max) = max_connections {
                network = network.max_connections(max);
            }
            network
        });
        builder
    }
}

#[cfg(feature = "net")]
impl NetworkPolicyConfigPatch {
    /// Create an empty policy patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the base policy expanded before explicit allow and deny rules.
    pub fn base(mut self, value: NetworkPolicy) -> Self {
        self.base = Some(value);
        self
    }

    /// Replace explicit deny rules.
    pub fn deny_rules(mut self, value: Vec<Rule>) -> Self {
        self.deny = Some(value);
        self
    }

    /// Replace explicit allow rules.
    pub fn allow_rules(mut self, value: Vec<Rule>) -> Self {
        self.allow = Some(value);
        self
    }

    /// Overlay another policy patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        replace(&mut self.base, higher.base);
        replace(&mut self.deny, higher.deny);
        replace(&mut self.allow, higher.allow);
        self
    }

    fn materialize(self) -> (Option<NetworkPolicy>, Vec<Rule>) {
        if self.base.is_none() && self.deny.is_none() && self.allow.is_none() {
            return (None, Vec::new());
        }
        let base = self.base.unwrap_or_default();
        let allowlist = self.allow.as_ref().is_some_and(|rules| !rules.is_empty());
        let mut rules = self.deny.unwrap_or_default();
        rules.extend(self.allow.unwrap_or_default());
        let configured_rules = rules.clone();
        if !allowlist {
            rules.extend(base.rules);
        }
        (
            Some(NetworkPolicy {
                default_egress: if allowlist {
                    Action::Deny
                } else {
                    base.default_egress
                },
                default_ingress: base.default_ingress,
                rules,
            }),
            configured_rules,
        )
    }
}

#[cfg(feature = "net")]
impl DnsConfigPatch {
    /// Create an empty DNS patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable or disable DNS-rebinding protection.
    pub fn rebind_protection(mut self, enabled: bool) -> Self {
        self.rebind_protection = Some(enabled);
        self
    }

    /// Replace upstream nameservers.
    pub fn nameservers(mut self, values: Vec<Nameserver>) -> Self {
        self.nameservers = Some(values);
        self
    }

    /// Set the DNS query timeout in milliseconds.
    pub fn query_timeout_ms(mut self, value: u64) -> Self {
        self.query_timeout_ms = Some(value);
        self
    }

    /// Overlay another DNS patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        replace(&mut self.rebind_protection, higher.rebind_protection);
        replace(&mut self.nameservers, higher.nameservers);
        replace(&mut self.query_timeout_ms, higher.query_timeout_ms);
        self
    }

    fn is_present(&self) -> bool {
        self.rebind_protection.is_some()
            || self.nameservers.is_some()
            || self.query_timeout_ms.is_some()
    }
}

#[cfg(feature = "net")]
impl TlsConfigPatch {
    /// Create an empty TLS patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Enable or disable TLS interception.
    pub fn enabled(mut self, enabled: bool) -> Self {
        self.enabled = Some(enabled);
        self
    }

    /// Replace bypass patterns.
    pub fn bypass(mut self, value: Vec<String>) -> Self {
        self.bypass = Some(value);
        self
    }

    /// Enable or disable upstream certificate verification.
    pub fn verify_upstream(mut self, enabled: bool) -> Self {
        self.verify_upstream = Some(enabled);
        self
    }

    /// Enable or disable QUIC blocking while interception is active.
    pub fn block_quic(mut self, enabled: bool) -> Self {
        self.block_quic = Some(enabled);
        self
    }

    /// Overlay another TLS patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        replace(&mut self.enabled, higher.enabled);
        replace(&mut self.bypass, higher.bypass);
        replace(&mut self.verify_upstream, higher.verify_upstream);
        replace(&mut self.block_quic, higher.block_quic);
        self
    }
}

#[cfg(feature = "net")]
impl SecretConfigPatch {
    /// Create an empty secret patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Overlay one secret entry by name.
    pub fn secret(mut self, name: impl Into<String>, value: SecretEntryConfigPatch) -> Self {
        let name = name.into();
        match self.secrets.remove(&name) {
            Some(current) => {
                self.secrets.insert(name, current.overlay(value));
            }
            None => {
                self.secrets.insert(name, value);
            }
        }
        self
    }

    /// Overlay another secret patch by name and then by field.
    pub fn overlay(mut self, higher: Self) -> Self {
        for (name, patch) in higher.secrets {
            self = self.secret(name, patch);
        }
        self
    }

    fn apply_to(self, mut builder: SandboxBuilder) -> SandboxBuilder {
        for (name, patch) in self.secrets {
            let (value, source) = match patch.material {
                Some(SecretMaterial::Literal(value)) => (value, None),
                Some(SecretMaterial::Source(source)) => {
                    (Zeroizing::new(String::new()), Some(source))
                }
                None => (
                    Zeroizing::new(String::new()),
                    Some(SecretSource::Env { var: name.clone() }),
                ),
            };
            builder = builder.secret_entry(microsandbox_types::SecretEntry {
                env_var: name.clone(),
                value,
                source,
                placeholder: microsandbox_utils::secret::default_placeholder(&name),
                allowed_hosts: patch.allowed_hosts.unwrap_or_default(),
                injection: patch.injection.unwrap_or(SecretInjection {
                    headers: true,
                    basic_auth: false,
                    query_params: false,
                    body: false,
                }),
                on_violation: None,
                require_tls_identity: patch.require_tls_identity.unwrap_or(true),
            });
        }
        builder
    }
}

#[cfg(feature = "net")]
impl SecretEntryConfigPatch {
    /// Create an empty secret-entry patch.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set a literal secret value.
    pub fn literal(mut self, value: impl Into<String>) -> Self {
        self.material = Some(SecretMaterial::Literal(Zeroizing::new(value.into())));
        self
    }

    /// Set a host-side secret source.
    pub fn source(mut self, value: SecretSource) -> Self {
        self.material = Some(SecretMaterial::Source(value));
        self
    }

    /// Replace allowed host patterns.
    pub fn allowed_hosts(mut self, value: Vec<HostPattern>) -> Self {
        self.allowed_hosts = Some(value);
        self
    }

    /// Replace injection locations.
    pub fn injection(mut self, value: SecretInjection) -> Self {
        self.injection = Some(value);
        self
    }

    /// Set whether verified TLS identity is required.
    pub fn require_tls_identity(mut self, value: bool) -> Self {
        self.require_tls_identity = Some(value);
        self
    }

    /// Overlay another secret-entry patch.
    pub fn overlay(mut self, higher: Self) -> Self {
        replace(&mut self.material, higher.material);
        replace(&mut self.allowed_hosts, higher.allowed_hosts);
        replace(&mut self.injection, higher.injection);
        replace(&mut self.require_tls_identity, higher.require_tls_identity);
        self
    }
}

//--------------------------------------------------------------------------------------------------
// Trait Implementations
//--------------------------------------------------------------------------------------------------

impl From<ResourceConfigPatch> for SandboxConfigPatch {
    fn from(value: ResourceConfigPatch) -> Self {
        Self::default().resources(value)
    }
}

impl From<RuntimeConfigPatch> for SandboxConfigPatch {
    fn from(value: RuntimeConfigPatch) -> Self {
        Self::default().runtime(value)
    }
}

impl From<FilesystemConfigPatch> for SandboxConfigPatch {
    fn from(value: FilesystemConfigPatch) -> Self {
        Self::default().filesystem(value)
    }
}

impl From<ScriptConfigPatch> for SandboxConfigPatch {
    fn from(value: ScriptConfigPatch) -> Self {
        Self::default().scripts(value)
    }
}

#[cfg(feature = "net")]
impl From<NetworkConfigPatch> for SandboxConfigPatch {
    fn from(value: NetworkConfigPatch) -> Self {
        Self::default().network(value)
    }
}

#[cfg(feature = "net")]
impl From<SecretConfigPatch> for SandboxConfigPatch {
    fn from(value: SecretConfigPatch) -> Self {
        Self::default().secrets(value)
    }
}

//--------------------------------------------------------------------------------------------------
// Functions
//--------------------------------------------------------------------------------------------------

fn replace<T>(base: &mut Option<T>, higher: Option<T>) {
    if higher.is_some() {
        *base = higher;
    }
}

fn merge_map<K: Ord, V>(base: &mut Option<BTreeMap<K, V>>, higher: Option<BTreeMap<K, V>>) {
    if let Some(higher) = higher {
        base.get_or_insert_with(BTreeMap::new).extend(higher);
    }
}

//--------------------------------------------------------------------------------------------------
// Tests
//--------------------------------------------------------------------------------------------------

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

    #[tokio::test]
    async fn scoped_overlays_are_right_hand_wins_and_map_recursive() {
        let root = SandboxConfigPatch::new()
            .image(SandboxImagePatch::Image("alpine".into()))
            .resources(ResourceConfigPatch::new().cpus(2).memory_mib(512))
            .runtime(RuntimeConfigPatch::new().env(BTreeMap::from([
                ("A".into(), "root".into()),
                ("B".into(), "root".into()),
            ])));
        let patch = root.overlay(ResourceConfigPatch::new().cpus(4)).overlay(
            RuntimeConfigPatch::new().env(BTreeMap::from([("A".into(), "scoped".into())])),
        );

        let config = SandboxBuilder::new("patch-test")
            .configure(patch)
            .build()
            .await
            .unwrap();
        assert_eq!(config.spec.resources.cpus, 4);
        assert_eq!(config.spec.resources.memory_mib, 512);
        assert!(
            config
                .spec
                .env
                .iter()
                .any(|value| value.key == "A" && value.value == "scoped")
        );
        assert!(
            config
                .spec
                .env
                .iter()
                .any(|value| value.key == "B" && value.value == "root")
        );
    }

    #[tokio::test]
    async fn explicit_builder_calls_override_configured_scalars() {
        let patch = SandboxConfigPatch::new()
            .image(SandboxImagePatch::Image("alpine".into()))
            .resources(ResourceConfigPatch::new().cpus(2).memory_mib(512));

        let config = SandboxBuilder::new("patch-test")
            .configure(patch)
            .cpus(6)
            .memory(1024)
            .build()
            .await
            .unwrap();
        assert_eq!(config.spec.resources.cpus, 6);
        assert_eq!(config.spec.resources.memory_mib, 1024);
    }

    #[tokio::test]
    async fn configured_scripts_use_the_final_shell() {
        let patch = SandboxConfigPatch::new()
            .image(SandboxImagePatch::Image("alpine".into()))
            .scripts(ScriptConfigPatch::new().script("hello", "echo hello"));

        let config = SandboxBuilder::new("patch-test")
            .configure(patch)
            .shell("bash")
            .build()
            .await
            .unwrap();
        assert_eq!(
            config.spec.runtime.scripts.get("hello").map(String::as_str),
            Some("#!/usr/bin/env bash\necho hello\n")
        );
    }

    #[tokio::test]
    async fn later_image_overrides_only_a_configured_snapshot() {
        let patch =
            SandboxConfigPatch::new().image(SandboxImagePatch::Snapshot("lower-priority".into()));
        let configured = SandboxBuilder::new("patch-test")
            .configure(patch)
            .image("alpine")
            .build()
            .await;
        assert!(configured.is_ok());

        let explicit = SandboxBuilder::new("patch-test")
            .from_snapshot("explicit")
            .image("alpine")
            .build()
            .await
            .unwrap_err();
        assert!(explicit.to_string().contains("mutually exclusive"));
    }
}