pub const INTERNET_EGRESS_AVAILABLE: bool = false;
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.";
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.";
pub fn internet_egress_available() -> bool {
INTERNET_EGRESS_AVAILABLE || test_switch::forced()
}
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),
)
}
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;
pub const FLIPPED_GATEWAY_IMAGE: &str = "ghcr.io/alienplatform/alien-egress-gateway@sha256:40a22a2aebf068da82d4310043875b383cdeba9cfa0aa90a649d67f70398a037";
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())
}
pub fn enable_internet_egress_for_process() {
let _ = IMAGE_FOR_PROCESS.set(FLIPPED_GATEWAY_IMAGE);
FORCED_FOR_PROCESS.store(true, Ordering::SeqCst);
}
#[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));
}
}
pub fn enable_internet_egress_for_test() -> InternetEgressForTest {
enable_internet_egress_with_gateway_image_for_test(FLIPPED_GATEWAY_IMAGE)
}
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,
};
pub const ATTACHMENT_CIDR: &str = "192.168.250.0/28";
pub const GATEWAY_CIDR: &str = "192.168.251.0/27";
pub const GATEWAY_HOSTS: [u8; 2] = [10, 11];
pub const CONTAINER_PROXY_PORT: u16 = 3128;
pub const ADMIN_PORT: u16 = 3129;
pub const DEFAULT_PROXY_PORT: u16 = 22;
pub const GATEWAY_TAG: &str = "alien-sandbox-gateway";
pub const ATTACHMENT_ROLE_PERMISSIONS: [&str; 2] = [
"compute.networkAttachments.get",
"compute.networkAttachments.update",
];
pub const ROLE_PROPAGATION_SECONDS: u64 = 180;
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()
}
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()
}
pub const DEFAULT_MACHINE_TYPE: &str = "e2-standard-2";
pub const BOOT_IMAGE: &str = "projects/ubuntu-os-cloud/global/images/family/ubuntu-2404-lts-amd64";
pub const BOOT_DISK_GB: u32 = 20;
pub fn service_account_id(resource_prefix: &str) -> String {
format!("sbx-gw-{resource_prefix}")
.chars()
.take(30)
.collect::<String>()
.trim_matches('-')
.to_string()
}
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(" || ")
}
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";
pub fn vm(index: usize) -> String {
format!("sbx-gateway-{index}")
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RuleSource {
Attachment,
Gateway,
Workload,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FirewallRule {
pub suffix: &'static str,
pub priority: u32,
pub allow: bool,
pub sources: Vec<RuleSource>,
pub tcp_ports: Option<Vec<u16>>,
pub gateway_only: bool,
pub log: bool,
}
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,
},
]
}
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()
}
pub fn attachment_path(project: &str, region: &str, name: &str) -> String {
format!("projects/{project}/regions/{region}/networkAttachments/{name}")
}
pub const REGIONS_WITH_PRIVATE_SERVICE_CONNECT: &[&str] = &["us-central1"];
pub fn region_refusal(region: &str) -> Option<String> {
if REGIONS_WITH_PRIVATE_SERVICE_CONNECT.contains(®ion) {
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 ")
))
}
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";
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
}
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::*;
#[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());
}
#[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!(crate::sandbox_egress::gateway_image_serves_links(
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());
}
}