From dfca73f3013809425c3e8a1e32b2ab8143ad72be Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Fri, 6 Nov 2026 21:48:44 -0600 Subject: [PATCH] Add network sandbox configuration renderer --- scripts/network-sandbox-config.py | 89 ++++++++++++++++++++ tests/jeannie/test_network_sandbox_config.py | 30 +++++++ 2 files changed, 119 insertions(+) create mode 100644 scripts/network-sandbox-config.py create mode 100644 tests/jeannie/test_network_sandbox_config.py diff --git a/scripts/network-sandbox-config.py b/scripts/network-sandbox-config.py new file mode 100644 index 0000000..d55fee5 --- /dev/null +++ b/scripts/network-sandbox-config.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +"""Render the intended OpenWrt VLAN sandbox configuration for review. + +This command only writes files in an explicitly supplied output directory. It +does not create guests, change bridges, or enable the existing OpenWrt path. +""" +import argparse +import ipaddress +import json +import sys +from pathlib import Path + + +SUBNETS = {10: ipaddress.ip_network('172.29.210.0/24'), + 20: ipaddress.ip_network('172.29.220.0/24')} +BLOCKED_NETWORKS = ['10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', + '100.64.0.0/10', '169.254.0.0/16'] + + +def build_configs(): + network = ["config interface 'loopback'", "\toption device 'lo'", + "\toption proto 'static'", "\toption ipaddr '127.0.0.1'", + "\toption netmask '255.0.0.0'", '', + "config interface 'wan'", "\toption device 'eth0'", + "\toption proto 'dhcp'", "\toption peerdns '0'", + "\tlist dns '1.1.1.1'", "\tlist dns '9.9.9.9'", ''] + dhcp = ["config dnsmasq", "\toption domainneeded '1'", + "\toption boguspriv '1'", "\toption rebind_protection '1'", + "\toption leasefile '/tmp/dhcp.leases'", + "\toption resolvfile '/tmp/resolv.conf.d/resolv.conf.auto'", ''] + firewall = ["config defaults", "\toption input 'REJECT'", + "\toption output 'ACCEPT'", "\toption forward 'REJECT'", '', + "config zone", "\toption name 'wan'", "\tlist network 'wan'", + "\toption input 'REJECT'", "\toption output 'ACCEPT'", + "\toption forward 'REJECT'", "\toption masq '1'", '', + "config rule", "\toption name 'Allow-WAN-DHCP'", + "\toption src 'wan'", "\toption proto 'udp'", + "\toption dest_port '68'", "\toption family 'ipv4'", + "\toption target 'ACCEPT'", ''] + for vid, subnet in SUBNETS.items(): + name = f'lab{vid}' + gateway = str(next(subnet.hosts())) + network += [f"config interface '{name}'", f"\toption device 'eth1.{vid}'", + "\toption proto 'static'", f"\toption ipaddr '{gateway}'", + f"\toption netmask '{subnet.netmask}'", ''] + dhcp += [f"config dhcp '{name}'", f"\toption interface '{name}'", + "\toption start '100'", "\toption limit '100'", + "\toption leasetime '12h'", ''] + firewall += ["config zone", f"\toption name '{name}'", + f"\tlist network '{name}'", "\toption input 'REJECT'", + "\toption output 'ACCEPT'", "\toption forward 'REJECT'", '', + "config rule", f"\toption name 'Allow-{name}-DNS-DHCP'", + f"\toption src '{name}'", "\toption proto 'tcp udp'", + "\toption dest_port '53 67'", "\toption family 'ipv4'", + "\toption target 'ACCEPT'", ''] + for blocked in BLOCKED_NETWORKS: + firewall += ["config rule", f"\toption name 'Block-{name}-{blocked.replace('/', '-')}'", + f"\toption src '{name}'", "\toption dest 'wan'", + f"\toption dest_ip '{blocked}'", "\toption family 'ipv4'", + "\toption proto 'all'", "\toption target 'REJECT'", ''] + firewall += ["config forwarding", f"\toption src '{name}'", + "\toption dest 'wan'", ''] + return {'network': '\n'.join(network), 'dhcp': '\n'.join(dhcp), + 'firewall': '\n'.join(firewall)} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument('--output-dir', type=Path, required=True) + args = parser.parse_args() + try: + if not args.output_dir.is_dir(): + raise ValueError('Output directory must already exist') + configs = build_configs() + paths = {name: args.output_dir / name for name in configs} + if any(path.exists() for path in paths.values()): + raise ValueError('Refusing to overwrite existing configuration files') + for name, content in configs.items(): + paths[name].write_text(content, encoding='utf-8') + print(json.dumps({'output_dir': str(args.output_dir), + 'files': list(configs), 'applied': False}, indent=2)) + except (OSError, ValueError) as exc: + print(f'Network sandbox config: {exc}', file=sys.stderr) + return 1 + return 0 + + +if __name__ == '__main__': + sys.exit(main()) diff --git a/tests/jeannie/test_network_sandbox_config.py b/tests/jeannie/test_network_sandbox_config.py new file mode 100644 index 0000000..3562a2a --- /dev/null +++ b/tests/jeannie/test_network_sandbox_config.py @@ -0,0 +1,30 @@ +"""Offline checks for the proposed OpenWrt sandbox configuration.""" +import importlib.util +import ipaddress +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[2] +spec = importlib.util.spec_from_file_location('sandbox_config', ROOT / 'scripts/network-sandbox-config.py') +module = importlib.util.module_from_spec(spec) +spec.loader.exec_module(module) + + +class NetworkSandboxConfigTests(unittest.TestCase): + def test_vlan_networks_are_disjoint(self): + self.assertFalse(module.SUBNETS[10].overlaps(module.SUBNETS[20])) + self.assertFalse(module.SUBNETS[10].overlaps(ipaddress.ip_network('192.168.100.0/24'))) + + def test_default_deny_and_private_network_rules(self): + configs = module.build_configs() + self.assertIn("option forward 'REJECT'", configs['firewall']) + for vid in (10, 20): + self.assertIn(f"option device 'eth1.{vid}'", configs['network']) + self.assertIn(f"option src 'lab{vid}'", configs['firewall']) + for cidr in module.BLOCKED_NETWORKS: + self.assertIn(f"option dest_ip '{cidr}'", configs['firewall']) + self.assertNotIn("option dest 'lab20'", configs['firewall']) + + +if __name__ == '__main__': + unittest.main()