alien-core 3.3.72

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
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
//! The GCP wiring that sends every Internet sandbox's outbound traffic through the egress gateway.
//!
//! Agent Platform connects a sandbox to the deployment's VPC through a Private Service Connect
//! interface: a network attachment on a dedicated subnet. Traffic from the sandbox enters the VPC
//! from that subnet, and the firewall lets it reach the gateway's proxy and nothing else.
//!
//! Setup creates the wiring twice, as Terraform and as direct API calls. Both render from the
//! description in this file (names, ranges, ports, role permissions and firewall rules), and a
//! differential test compares the two, so the two setup surfaces cannot drift apart.

/// The one switch for Google Cloud internet egress through the gateway. While it is `false`,
/// every plan-time path (build, release, the Terraform and CloudFormation render, deployment
/// preparation) refuses a Google Cloud sandbox with egress `internet`, so no setup creates the
/// network attachment, the gateway VMs or Private Service Connect resources, and no template
/// pins a project to Private Service Connect. Flipping it to `true` is the whole release step,
/// once the on-demand live test (`alien-test`, `gcp_sandbox_internet_live`) passes.
pub const INTERNET_EGRESS_AVAILABLE: bool = false;

/// The refusal while [`INTERNET_EGRESS_AVAILABLE`] is off. Build, render and deployment checks
/// all give this text.
pub const INTERNET_SANDBOX_UNAVAILABLE: &str = "Google Cloud internet egress through the egress \
     gateway is not available yet. Use egress none, or deploy the sandbox on another cloud.";

/// Said at plan time for a Google Cloud sandbox on Google's default network. The first sandbox a
/// project runs fixes its networking for good, so a project that started on the default network
/// can never host a gateway-backed `internet` sandbox.
pub const DEFAULT_PLACEMENT_IS_PERMANENT: &str = "this sandbox uses Google's default network. \
     A Google Cloud project whose first sandbox used the default network can never run sandboxes \
     with egress `internet` through the deployment's egress gateway, even after every sandbox is \
     deleted. To use egress `internet` later, deploy to a new project.";

/// Whether Google Cloud sandboxes may declare egress `internet`: the switch, or a test that
/// turned it on for its own thread or its whole process.
pub fn internet_egress_available() -> bool {
    INTERNET_EGRESS_AVAILABLE || test_switch::forced()
}

/// Whether this stack's Google Cloud internet sandboxes can be planned: the switch is on and the
/// gateway image setup will run serves the admin protocol the sandbox binding requires. A gateway
/// without it would fail every sandbox create after setup built the gateway VMs, so the flip
/// also bumps the release's gateway pin. Build, render and deployment checks all ask this.
pub fn internet_egress_available_for(stack: &crate::Stack) -> bool {
    internet_egress_available()
        && crate::sandbox_egress::gateway_image_serves_admin_protocol(
            crate::sandbox_egress::planned_gateway_image(stack),
        )
}

/// The gateway image a test installs in place of the release pin, if one turned it on.
pub(crate) fn gateway_image_override() -> Option<&'static str> {
    test_switch::gateway_image()
}

#[cfg(any(test, feature = "test-gcp-internet-egress"))]
mod test_switch {
    use std::cell::Cell;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::OnceLock;

    /// The released public gateway image the flip pins (v3.3.71, by digest). It serves the admin
    /// protocol the sandbox binding requires, and it is what the live test runs.
    pub const FLIPPED_GATEWAY_IMAGE: &str = "ghcr.io/alienplatform/alien-egress-gateway@sha256:69d97f5aceb41f5bc8eee09e497cd967f04ef19b649150a472607153446ef7d8";

    thread_local! {
        static FORCED: Cell<bool> = const { Cell::new(false) };
        static IMAGE: Cell<Option<&'static str>> = const { Cell::new(None) };
    }

    static FORCED_FOR_PROCESS: AtomicBool = AtomicBool::new(false);
    static IMAGE_FOR_PROCESS: OnceLock<&'static str> = OnceLock::new();

    pub(super) fn forced() -> bool {
        FORCED.with(Cell::get) || FORCED_FOR_PROCESS.load(Ordering::SeqCst)
    }

    pub(super) fn gateway_image() -> Option<&'static str> {
        IMAGE
            .with(Cell::get)
            .or_else(|| IMAGE_FOR_PROCESS.get().copied())
    }

    /// Turns Google Cloud internet egress on for every thread of this process, for good, with
    /// [`FLIPPED_GATEWAY_IMAGE`] as the gateway: the live test, whose build, manager and
    /// controllers run on the runtime's worker threads, plans and deploys the stack the flipped
    /// release would. Never used by unit tests, which share a process and must see the switch off.
    pub fn enable_internet_egress_for_process() {
        let _ = IMAGE_FOR_PROCESS.set(FLIPPED_GATEWAY_IMAGE);
        FORCED_FOR_PROCESS.store(true, Ordering::SeqCst);
    }

    /// Keeps the switch on for the current thread until it is dropped.
    #[must_use = "the switch turns off again when the guard is dropped"]
    pub struct InternetEgressForTest(());

    impl Drop for InternetEgressForTest {
        fn drop(&mut self) {
            FORCED.with(|forced| forced.set(false));
            IMAGE.with(|image| image.set(None));
        }
    }

    /// Turns Google Cloud internet egress on for the current thread, as the flip release would:
    /// the switch on and [`FLIPPED_GATEWAY_IMAGE`] as the gateway. Tests only; production code
    /// never reaches this.
    pub fn enable_internet_egress_for_test() -> InternetEgressForTest {
        enable_internet_egress_with_gateway_image_for_test(FLIPPED_GATEWAY_IMAGE)
    }

    /// Turns the switch on for the current thread with `image` as the gateway, for a test of a
    /// flip that did not bump the gateway pin.
    pub fn enable_internet_egress_with_gateway_image_for_test(
        image: &'static str,
    ) -> InternetEgressForTest {
        FORCED.with(|forced| forced.set(true));
        IMAGE.with(|slot| slot.set(Some(image)));
        InternetEgressForTest(())
    }
}

#[cfg(not(any(test, feature = "test-gcp-internet-egress")))]
mod test_switch {
    pub(super) fn forced() -> bool {
        false
    }

    pub(super) fn gateway_image() -> Option<&'static str> {
        None
    }
}

#[cfg(any(test, feature = "test-gcp-internet-egress"))]
pub use test_switch::{
    enable_internet_egress_for_process, enable_internet_egress_for_test,
    enable_internet_egress_with_gateway_image_for_test, InternetEgressForTest,
    FLIPPED_GATEWAY_IMAGE,
};

/// Range of the attachment's own subnet. Sandbox traffic enters the VPC from addresses in it. A
/// regular subnet of at least /28 that is not 100.64.0.0/20 or 240.0.0.0/4.
pub const ATTACHMENT_CIDR: &str = "192.168.250.0/28";

/// Range of the gateway subnet, the only subnet with Cloud NAT.
pub const GATEWAY_CIDR: &str = "192.168.251.0/27";

/// Host numbers of the two gateway VMs inside the gateway subnet (`.10` and `.11`). GCP reserves
/// the first two and last two addresses of a subnet.
pub const GATEWAY_HOSTS: [u8; 2] = [10, 11];

/// The port the gateway container's proxy listens on.
pub const CONTAINER_PROXY_PORT: u16 = 3128;

/// The port of the gateway's authenticated policy administration. Never reachable from the
/// attachment range.
pub const ADMIN_PORT: u16 = 3129;

/// The port the sandboxes connect to on the gateway VMs, the one setting that says which port
/// crosses from Agent Platform's tenant into the VPC. Only tcp/22 does today (measured, not
/// documented by Google). Every place that needs the port reads it from the setup-recorded
/// network data, which setup writes from this default.
pub const DEFAULT_PROXY_PORT: u16 = 22;

/// The network tag the gateway VMs carry and the firewall rules target.
pub const GATEWAY_TAG: &str = "alien-sandbox-gateway";

/// Permissions the Agent Platform service agent needs on the attachment. It must hold them, and
/// they must have propagated, before the first template names the attachment: a template that
/// fails on them leaves that attachment unusable for good.
pub const ATTACHMENT_ROLE_PERMISSIONS: [&str; 2] = [
    "compute.networkAttachments.get",
    "compute.networkAttachments.update",
];

/// Seconds setup waits after binding the role, before anything creates a template. Three minutes
/// worked in a measurement and ninety seconds did not.
pub const ROLE_PROPAGATION_SECONDS: u64 = 180;

/// A name the wiring gives a resource: `{resource prefix}-{suffix}`, at most 63 characters with no
/// leading or trailing dash. Terraform computes the same string in HCL.
pub fn resource_name(resource_prefix: &str, suffix: &str) -> String {
    let full = format!("{resource_prefix}-{suffix}");
    full.chars()
        .take(63)
        .collect::<String>()
        .trim_matches('-')
        .to_string()
}

/// Custom role ids allow letters, digits, `_` and `.`, at most 64 characters.
pub fn role_id(resource_prefix: &str) -> String {
    let full = format!("{resource_prefix}_sandbox_attachment");
    full.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '.' {
                c
            } else {
                '_'
            }
        })
        .take(64)
        .collect()
}

/// The gateway VMs' machine type unless the setup says otherwise: at least 2 vCPUs and 4 GiB.
pub const DEFAULT_MACHINE_TYPE: &str = "e2-standard-2";

/// The gateway VMs' boot image.
pub const BOOT_IMAGE: &str = "projects/ubuntu-os-cloud/global/images/family/ubuntu-2404-lts-amd64";

/// The gateway VMs' boot disk size.
pub const BOOT_DISK_GB: u32 = 20;

/// Account id of the gateways' identity: `sbx-gw-` and the prefix, at most 30 characters and
/// never ending in a dash. The Terraform emitter renders the same with `trim(substr(..))`.
pub fn service_account_id(resource_prefix: &str) -> String {
    format!("sbx-gw-{resource_prefix}")
        .chars()
        .take(30)
        .collect::<String>()
        .trim_matches('-')
        .to_string()
}

/// The condition that limits the gateways' secret access to the given secrets of the deployment
/// vault. `secrets` are the secret names without the vault prefix.
pub fn secret_condition(project_number: &str, vault_prefix: &str, secrets: &[&str]) -> String {
    secrets
        .iter()
        .map(|secret| {
            format!(
                "resource.name.startsWith(\"projects/{project_number}/secrets/{vault_prefix}-{secret}/\")"
            )
        })
        .collect::<Vec<_>>()
        .join(" || ")
}

/// Name suffixes of every resource of the wiring.
pub mod suffix {
    pub const ATTACHMENT_SUBNET: &str = "sbx-attachment";
    pub const ATTACHMENT: &str = "sbx-attachment";
    pub const GATEWAY_SUBNET: &str = "sbx-gateway";
    pub const ROUTER: &str = "sbx-gateway-router";
    pub const NAT: &str = "sbx-gateway-nat";
    pub const GATEWAY_SERVICE_ACCOUNT: &str = "sandbox_gateway";
    pub const FIREWALL_PROXY: &str = "sbx-allow-proxy";
    pub const FIREWALL_ADMIN: &str = "sbx-allow-admin";
    pub const FIREWALL_DENY: &str = "sbx-deny-attachment";

    /// Name suffix of gateway VM `index`.
    pub fn vm(index: usize) -> String {
        format!("sbx-gateway-{index}")
    }
}

/// Where a firewall rule's traffic comes from.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuleSource {
    /// The attachment subnet: sandbox traffic.
    Attachment,
    /// The gateway subnet: the peer gateway.
    Gateway,
    /// The deployment's workload subnet: the manager's path to the admin port.
    Workload,
}

/// One firewall rule of the wiring. Every rule is ingress to the gateway VMs (the tag) except the
/// catch-all deny, which covers the whole VPC from the attachment range.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FirewallRule {
    pub suffix: &'static str,
    pub priority: u32,
    pub allow: bool,
    pub sources: Vec<RuleSource>,
    /// `None` is every protocol and port.
    pub tcp_ports: Option<Vec<u16>>,
    /// Targets only the gateway VMs. A rule that does not covers the whole network.
    pub gateway_only: bool,
    pub log: bool,
}

/// The firewall rules, in priority order. `proxy_port` is the port sandboxes connect to.
///
/// - the attachment subnet reaches the gateways on `proxy_port` and nothing else;
/// - the admin port answers only the peer gateway and the deployment's workloads;
/// - everything else from the attachment subnet is denied and logged, in the whole VPC.
///
/// The VPC denies ingress by default. The deny rule exists to log, and to win over any broad allow
/// a customer's network carries at a lower priority.
pub fn firewall_rules(proxy_port: u16) -> Vec<FirewallRule> {
    vec![
        FirewallRule {
            suffix: suffix::FIREWALL_PROXY,
            priority: 900,
            allow: true,
            sources: vec![RuleSource::Attachment],
            tcp_ports: Some(vec![proxy_port]),
            gateway_only: true,
            log: true,
        },
        FirewallRule {
            suffix: suffix::FIREWALL_ADMIN,
            priority: 900,
            allow: true,
            sources: vec![RuleSource::Gateway, RuleSource::Workload],
            tcp_ports: Some(vec![ADMIN_PORT]),
            gateway_only: true,
            log: false,
        },
        FirewallRule {
            suffix: suffix::FIREWALL_DENY,
            priority: 1000,
            allow: false,
            sources: vec![RuleSource::Attachment],
            tcp_ports: None,
            gateway_only: false,
            log: true,
        },
    ]
}

/// The gateway VMs' private addresses, from the gateway range.
pub fn gateway_ips() -> Vec<String> {
    let base = GATEWAY_CIDR
        .split('/')
        .next()
        .expect("a CIDR has an address");
    let prefix: Vec<&str> = base.split('.').take(3).collect();
    GATEWAY_HOSTS
        .iter()
        .map(|host| format!("{}.{host}", prefix.join(".")))
        .collect()
}

/// Full path of the network attachment a template names.
pub fn attachment_path(project: &str, region: &str, name: &str) -> String {
    format!("projects/{project}/regions/{region}/networkAttachments/{name}")
}

/// Regions where Agent Platform accepts a sandbox template with a network attachment and private
/// ingress. Measured by creating one in each region: us-central1 accepts it, and us-east1,
/// us-east4, us-west1, europe-west1, europe-west2, europe-west4 and asia-southeast1 answer "not yet
/// available in region". Google lists no regions for it, so this is an allowlist: a region joins
/// it when a template there is accepted. A sandbox that declares `internet` anywhere else is
/// refused at plan time. It never falls back to open outbound.
pub const REGIONS_WITH_PRIVATE_SERVICE_CONNECT: &[&str] = &["us-central1"];

/// Why `internet` cannot be hosted in `region`, or `None` when it can.
pub fn region_refusal(region: &str) -> Option<String> {
    if REGIONS_WITH_PRIVATE_SERVICE_CONNECT.contains(&region) {
        return None;
    }
    Some(format!(
        "egress internet on GCP sends sandbox traffic through a Private Service Connect \
         interface, which Agent Platform does not offer in {region}; use {}",
        REGIONS_WITH_PRIVATE_SERVICE_CONNECT.join(" or ")
    ))
}

/// The refusal for a project whose sandbox placement is already pinned to the default network.
pub const PLACEMENT_PINNED_MESSAGE: &str = "egress internet on GCP needs a project that has \
never run a sandbox without the deployment network: Google pins a project's sandbox placement \
with its first sandbox and cannot change it. Deploy to a new project, or use egress none";

/// The gateway VM's boot script with three `%s` values, in order: the project id, the region and
/// the JSON of the secrets vault binding. Terraform fills them with `format`, direct setup with
/// [`startup_script`]. It installs Docker, writes the environment file and a unit that redirects
/// the proxy port from the attachment range to the container's own port (limited to that range,
/// so no other source reaches the proxy through it) and runs the gateway as one hardened,
/// unprivileged container. The container is unchanged: it listens on 3128.
pub fn startup_script_template(image: &str) -> String {
    let redirect = if DEFAULT_PROXY_PORT == CONTAINER_PROXY_PORT {
        String::new()
    } else {
        format!(
            "ExecStartPre=/bin/sh -c 'iptables -t nat -C PREROUTING -s {cidr} -p tcp --dport {port} -j REDIRECT --to-ports {to} || iptables -t nat -I PREROUTING 1 -s {cidr} -p tcp --dport {port} -j REDIRECT --to-ports {to}'",
            cidr = ATTACHMENT_CIDR,
            port = DEFAULT_PROXY_PORT,
            to = CONTAINER_PROXY_PORT,
        )
    };
    let lines = [
        "#!/bin/bash".to_string(),
        "set -u".to_string(),
        "for i in $(seq 1 30); do apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y docker.io iptables && break; sleep 10; done".to_string(),
        format!("for i in $(seq 1 30); do docker pull {image} && break; sleep 10; done"),
        "umask 077".to_string(),
        "cat > /etc/alien-egress-gateway.env <<'ENV'".to_string(),
        "ALIEN_DEPLOYMENT_TYPE=gcp".to_string(),
        "GCP_PROJECT_ID=%s".to_string(),
        "GCP_REGION=%s".to_string(),
        "ALIEN_SECRETS_BINDING=%s".to_string(),
        "RUST_LOG=warn".to_string(),
        "ENV".to_string(),
        "cat > /etc/systemd/system/alien-egress-gateway.service <<'UNIT'".to_string(),
        "[Unit]".to_string(),
        "Description=Alien egress gateway".to_string(),
        "After=docker.service network-online.target".to_string(),
        "Requires=docker.service".to_string(),
        "[Service]".to_string(),
        "Restart=always".to_string(),
        "RestartSec=3".to_string(),
        "ExecStartPre=-/usr/bin/docker rm -f gateway".to_string(),
        redirect,
        format!("ExecStart=/usr/bin/docker run --name gateway --env-file /etc/alien-egress-gateway.env --user 1000:1000 --cap-drop ALL --memory 2g --security-opt no-new-privileges --read-only --tmpfs /tmp -p {CONTAINER_PROXY_PORT}:{CONTAINER_PROXY_PORT} -p {ADMIN_PORT}:{ADMIN_PORT} {image}"),
        "[Install]".to_string(),
        "WantedBy=multi-user.target".to_string(),
        "UNIT".to_string(),
        "systemctl daemon-reload".to_string(),
        "systemctl enable alien-egress-gateway.service".to_string(),
        "systemctl restart alien-egress-gateway.service".to_string(),
    ];
    let mut text = lines
        .into_iter()
        .filter(|line| !line.is_empty())
        .collect::<Vec<_>>()
        .join("\n");
    text.push('\n');
    text
}

/// [`startup_script_template`] with its values filled for direct setup.
pub fn startup_script(image: &str, project: &str, region: &str, vault_prefix: &str) -> String {
    let binding = serde_json::json!({"service": "secret-manager", "vaultPrefix": vault_prefix});
    let mut values = [project.to_string(), region.to_string(), binding.to_string()].into_iter();
    let mut out = String::new();
    let mut rest = startup_script_template(image);
    while let Some(index) = rest.find("%s") {
        out.push_str(&rest[..index]);
        out.push_str(&values.next().expect("three values"));
        rest = rest[index + 2..].to_string();
    }
    out.push_str(&rest);
    out
}

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

    /// The switch ships off; a test can turn it on for its own thread only, and the guard
    /// turns it off again.
    #[test]
    fn internet_egress_is_off_until_the_switch_flips() {
        assert!(!INTERNET_EGRESS_AVAILABLE);
        assert!(!internet_egress_available());
        {
            let _on = enable_internet_egress_for_test();
            assert!(internet_egress_available());
            std::thread::spawn(|| assert!(!internet_egress_available()))
                .join()
                .unwrap();
        }
        assert!(!internet_egress_available());
    }

    /// The flip needs both the switch and a gateway image that serves the admin protocol: the
    /// switch alone, with the image this release pins today, still refuses.
    #[test]
    fn internet_egress_needs_a_gateway_image_that_serves_the_admin_protocol() {
        let stack = crate::Stack::new("s".into()).build();
        assert!(!internet_egress_available_for(&stack));
        {
            let _on = enable_internet_egress_with_gateway_image_for_test(
                crate::sandbox_egress::SANDBOX_EGRESS_GATEWAY_IMAGE,
            );
            assert_eq!(
                internet_egress_available_for(&stack),
                crate::sandbox_egress::gateway_image_serves_admin_protocol(
                    crate::sandbox_egress::SANDBOX_EGRESS_GATEWAY_IMAGE
                )
            );
        }
        {
            let _on = enable_internet_egress_for_test();
            assert!(crate::sandbox_egress::gateway_image_serves_admin_protocol(
                FLIPPED_GATEWAY_IMAGE
            ));
            assert_eq!(
                crate::sandbox_egress::planned_gateway_image(&stack),
                FLIPPED_GATEWAY_IMAGE
            );
            assert!(internet_egress_available_for(&stack));
        }
        assert_eq!(
            crate::sandbox_egress::installed_gateway_image(),
            crate::sandbox_egress::SANDBOX_EGRESS_GATEWAY_IMAGE
        );
    }

    #[test]
    fn the_boot_script_fills_its_three_values_and_names_the_port_from_the_constant() {
        let text = startup_script(
            "registry.example/gw@sha256:abc",
            "proj",
            "us-central1",
            "p-secrets",
        );
        assert!(!text.contains("%s"), "{text}");
        assert!(text.contains("GCP_PROJECT_ID=proj") && text.contains("GCP_REGION=us-central1"));
        assert!(
            text.contains(r#"{"service":"secret-manager","vaultPrefix":"p-secrets"}"#),
            "{text}"
        );
        assert!(text.contains(&format!("--dport {DEFAULT_PROXY_PORT}")));
        assert_eq!(startup_script_template("i").matches("%s").count(), 3);
    }

    #[test]
    fn the_service_account_id_fits_the_limit() {
        assert_eq!(service_account_id("acme"), "sbx-gw-acme");
        let long = service_account_id(&format!("{}-", "a".repeat(40)));
        assert!(long.len() <= 30 && !long.ends_with('-'), "{long}");
    }

    #[test]
    fn the_secret_condition_names_each_secret_path() {
        let text = secret_condition("123", "p-secrets", &["a", "b"]);
        assert_eq!(
            text,
            r#"resource.name.startsWith("projects/123/secrets/p-secrets-a/") || resource.name.startsWith("projects/123/secrets/p-secrets-b/")"#
        );
    }

    #[test]
    fn names_are_clamped_and_never_end_in_a_dash() {
        let name = resource_name(&"a".repeat(70), "sbx-attachment");
        assert!(name.len() <= 63);
        assert!(!name.ends_with('-'));
        assert_eq!(resource_name("acme", "sbx-gateway-0"), "acme-sbx-gateway-0");
    }

    #[test]
    fn the_gateway_addresses_sit_in_the_gateway_range() {
        assert_eq!(gateway_ips(), vec!["192.168.251.10", "192.168.251.11"]);
    }

    #[test]
    fn only_the_proxy_port_is_open_to_the_attachment() {
        for port in [DEFAULT_PROXY_PORT, 2222] {
            let rules = firewall_rules(port);
            for rule in rules.iter().filter(|r| r.allow) {
                if rule.sources.contains(&RuleSource::Attachment) {
                    assert_eq!(rule.tcp_ports, Some(vec![port]));
                    assert!(rule.gateway_only);
                }
            }
            let deny = rules.iter().find(|r| !r.allow).unwrap();
            assert!(
                deny.log
                    && deny.priority
                        > rules
                            .iter()
                            .filter(|r| r.allow)
                            .map(|r| r.priority)
                            .max()
                            .unwrap()
            );
            assert!(!rules
                .iter()
                .any(|r| r.sources.contains(&RuleSource::Attachment)
                    && r.tcp_ports == Some(vec![ADMIN_PORT])));
        }
    }

    #[test]
    fn a_region_without_private_service_connect_is_refused_by_name() {
        for region in ["us-east4", "europe-west4", "asia-southeast1"] {
            let text = region_refusal(region).expect("refused");
            assert!(text.contains(region) && text.contains("us-central1"));
        }
        assert!(region_refusal("us-central1").is_none());
    }
}