diff --git a/README.md b/README.md index dc99b02..5b848e0 100644 --- a/README.md +++ b/README.md @@ -1159,7 +1159,7 @@ lme /etc/linux-manager/linux-manager.json ~/notes.txt | Key | Content | | --- | --- | | `configuration` | `linux-manager.json` with the defaults applied, without its secret options at any level (`psk`, `database`, and any option named like a secret, password, token or key) | -| `network` | The external and internal interface, their addresses, the internal gateway, `local.cidrs`, `site_to_site`: each connection with `name`, `local_identity`, `remote_identity`, `local_cidrs` and `remote_cidrs`, and the IPv4 routes as `ip -j -4 route` prints them | +| `network` | The external and internal interface, their addresses, the internal gateway, `local.cidrs`, `site_to_site`: each connection with `name`, `local_identity`, `remote_identity`, `local_cidrs` and `remote_cidrs`, `interfaces`: each interface but loopback with `name`, `state`, `mac`, `ipv4`, its IPv4 addresses, and `ipv6`, its global IPv6 addresses, as `address/prefix`, `null` when `ip` fails, and the IPv4 routes as `ip -j -4 route` prints them | | `ipsec` | `connections` and `sas`: one object per connection and per IKE SA, read from `swanctl --list-conns --pretty` and `swanctl --list-sas --pretty`, with the connection's name under `name` and swanctl's keys and string values | | `wireguard` | The public key and port of `wg0`, and each peer's public key, endpoint, allowed IPs, handshake age and traffic; no private or preshared key | | `docker` | `docker.exposed_ports` and the rules of the `LINUX-MANAGER-DOCKER` chain | @@ -2202,8 +2202,10 @@ name, left of Sign out, opens the account page, and the terminal icon left of it remote identity and the subnets it resolved, and how long the connection has been up, and their IPsec security associations, child SAs, connections and connection children, from the server's `/config`. The other IPsec connections, IKEv2 access, are on the IKEv2 tab of Remote Access. -- **Connectivity**, Network tab: the external and the internal interface with their addresses, the - internal gateway and the local CIDRs, and the IPv4 routes, from the server's `/config`; the panels +- **Connectivity**, Network tab: the Interfaces panel lists every interface but loopback by name + with its type, `external` when it holds an IPv4 default route and `internal` otherwise, its + state, IPv4 and IPv6 addresses and MAC address; the Router panel, shown while the router is on, + the internal gateway and the local CIDRs; and the IPv4 routes, from the server's `/config`; the panels fold, hide, move and change width, and the browser keeps their layout. - **Remote Access** (`/remote-access-manager`): a tab each for IKEv2, OpenVPN and WireGuard access, from the server's `/remote-access` and `/status/full`: the diff --git a/debian-package/debian/changelog b/debian-package/debian/changelog index 6c97759..f5f24b8 100644 --- a/debian-package/debian/changelog +++ b/debian-package/debian/changelog @@ -32,6 +32,11 @@ linux-manager (0.41.0-1) unstable; urgency=medium WireGuard peers and the VPN profiles are refused. * The Manager hides the Remote Access page while the VPN is off, and shows only the Network tab of the Connectivity page while the router is off. + * The Network tab lists every interface but loopback by name, with its + type, external when it holds the IPv4 default route and internal + otherwise, state, addresses and MAC address; the internal gateway and the + local CIDRs are in a Router panel shown while the router is on. /config + has the interfaces in network.interfaces. * The LDAP schema is under 1.3.6.1.4.1.67380.10.1.2, the arc listed in OID-REGISTRY.txt. * Ubuntu 26.04 (resolute) is the only supported release: the build image @@ -41,7 +46,9 @@ linux-manager (0.41.0-1) unstable; urgency=medium * A NAT rule that the configuration no longer needs is removed from the running firewall at once, not only at the next boot. * tests/remote/run.py tests the components on Ubuntu 26.04 instances of an - LXD remote. + LXD remote, and tests/remote/vm.py starts and removes one server + VM there to work with, reached through ports 7080, 7081 and 2222 (SSH) + of the LXD host, by default at the address of the remote's URL. -- Slawomir Koszewski Sat, 21 Nov 2026 23:39:03 +0100 diff --git a/debian-package/src/usr/lib/linux-manager/linuxmanager_web/status.py b/debian-package/src/usr/lib/linux-manager/linuxmanager_web/status.py index e75fb09..db02e0d 100644 --- a/debian-package/src/usr/lib/linux-manager/linuxmanager_web/status.py +++ b/debian-package/src/usr/lib/linux-manager/linuxmanager_web/status.py @@ -125,6 +125,31 @@ def routes(): return None +def interfaces(): + """The network interfaces but loopback, each {name, state, mac, ipv4, ipv6} from ip -j addr show, with its IPv4 + and its global IPv6 addresses as address/prefix, or None when ip fails.""" + result = run('ip', '-j', 'addr', 'show') + if result is None or result.returncode != 0: + return None + try: + links = json.loads(result.stdout) + except ValueError: + return None + + def addresses(link, family): + return [f'{entry["local"]}/{entry["prefixlen"]}' for entry in link.get('addr_info', []) + if entry.get('family') == family and 'local' in entry + and (family == 'inet' or entry.get('scope') == 'global')] + + return [{ + 'name': link['ifname'], + 'state': link.get('operstate'), + 'mac': link.get('address'), + 'ipv4': addresses(link, 'inet'), + 'ipv6': addresses(link, 'inet6'), + } for link in links if link.get('link_type') != 'loopback' and 'ifname' in link] + + def wireguard_details(): """The interface and the peers of wg0, with endpoints and allowed IPs; no private or preshared key.""" result = run('wg', 'show', 'wg0', 'dump') @@ -214,7 +239,7 @@ def details(state): oidc = configuration.get('oidc', {}).get('enabled') is True return { 'configuration': configuration, - 'network': dict(state.get('network', {}), routes=routes()), + 'network': dict(state.get('network', {}), interfaces=interfaces(), routes=routes()), 'ipsec': { 'connections': swanctl('--list-conns'), 'sas': swanctl('--list-sas'), diff --git a/docs/index.md b/docs/index.md index 9289e32..f501f19 100644 --- a/docs/index.md +++ b/docs/index.md @@ -17,7 +17,7 @@ Back returns to the previous topic and the house icon to this one. | --- | --- | | Router | [Overview](router.md), [Metrics](metrics.md), [Logs](logs.md), [Backup](backup.md), [System](system.md) | | Identities | [Users](users.md), [Groups](groups.md), [Service accounts](service-accounts.md) | -| Connectivity | [Site-to-site connections](site-to-site.md) | +| Connectivity | [Status](site-to-site.md#status-tab), [Network](site-to-site.md#network-tab), [Connections](site-to-site.md#connections-tab) | | Remote Access | [IKEv2, OpenVPN and WireGuard](remote-access.md) | | Certificates | [Certificates](certificates.md) | | Web Sites | [Sites](sites.md), [Files](site-files.md), [DNS](dns.md) | diff --git a/docs/site-to-site.md b/docs/site-to-site.md index 2fcf3ad..47620d5 100644 --- a/docs/site-to-site.md +++ b/docs/site-to-site.md @@ -1,6 +1,6 @@ -# Site-to-site connections +# Connectivity -The Connectivity page: the server's IPsec site-to-site connections and its network. +The Connectivity page: the server's network and its IPsec site-to-site connections. Site-to-site connections need the router component (`router.enabled`); while it is off, the page shows only the Network tab. @@ -13,9 +13,13 @@ connections and connection children. IKEv2 access connections are on the IKEv2 t ## Network tab -The external and the internal interface with their addresses, the internal gateway, the local -CIDRs and the IPv4 routes. The panels fold, hide, move and change width; the browser keeps their -layout. +- **Interfaces:** every interface but loopback by name, with its type, its state, its IPv4 and + global IPv6 addresses and its MAC address. The type is `external` for an interface that holds + an IPv4 default route, and `internal` for one that does not route to the internet. +- **Router:** the internal gateway and the local CIDRs, while the router is on. +- **Routes:** the IPv4 routes. + +The panels fold, hide, move and change width; the browser keeps their layout. ## Connections tab diff --git a/tests/remote/README.md b/tests/remote/README.md index d08c4ff..e5b39df 100644 --- a/tests/remote/README.md +++ b/tests/remote/README.md @@ -1,6 +1,6 @@ # Remote tests -`run.py` installs the newest `out/linux-manager_*_all.deb` into LXD instances of a remote and +`run.py` installs the newest `out/linux-manager_*_all.deb` into instances of an LXD remote and checks the router and VPN components in three scenarios: - `core`: one interface, router and VPN off; @@ -44,5 +44,52 @@ its end. The administrator password and the pre-shared key are generated per run `tmp/remote-test.env`. The run prints PASS, FAIL or SKIP per check and exits non-zero on a failure; WireGuard is skipped when the host kernel has no WireGuard module. -Not covered: IKEv2 client access, HTTPS with Let's Encrypt, the OpenID Provider and passkeys, -PowerDNS answering from outside, and the Azure and GCP platform modules. +The configurations in `configs/` have placeholders that are filled in for each server: + +- `@PSK@`: the pre-shared key; +- `@ETH0@` and `@ETH1@`: the names of its interfaces; +- `@CIDR0@`: the network of its first address. + +## A VM to work with + +`vm.py` launches one server VM, `lm-dev` by default, on the LXD remote and leaves it +running. The VM is created in a project, `default` by default, with that project's default +profile, and takes its address from DHCP. It gets the newest package with the administrator +`admin` and a generated password, written to `tmp/vm-.env`, and a configuration from +`configs/`, `core.json` by default; `@ETH0@` is the interface of the VM's default route. + +The leased address is pinned on the VM's NIC, as NAT proxy devices require a static address. +Proxy devices then forward ports of the LXD host address `--listen` to the VM; by default that is +the IPv4 address of the host in the remote's URL (`lxc remote list`), which must be an address of +the LXD host itself: + +- `7080` to the Manager; +- `7081` to the status web service; +- `2222` to SSH, port `22`. + +The script prints the VM's address, the administrator's password, the proxied URLs, the command +of a shell in it and the command that removes it. + +```sh +tests/remote/vm.py +``` + +```sh +tests/remote/vm.py --remove +``` + +`--remove` deletes the VM and its env file. Options: + +| Option | Variable | Default | Meaning | +|---|---|---|---| +| `--remote` | `LXD_REMOTE` | required | The `lxc` remote | +| `--project` | | `default` | The project of the VM | +| `--name` | | `lm-dev` | The name of the VM; it must not exist | +| `--image` | `LXD_IMAGE` | `ubuntu:26.04` | The image of the VM | +| `--config` | | `core.json` | The configuration from `configs/` | +| `--listen` | `LXD_LISTEN` | the remote URL's host | An address of the LXD host the proxies listen on | + +## Scope of the tests + +Not covered by `run.py`: IKEv2 client access, HTTPS with Let's Encrypt, the OpenID Provider and +passkeys, PowerDNS answering from outside, and the Azure and GCP platform modules. diff --git a/tests/remote/configs/nat-off.json b/tests/remote/configs/nat-off.json index 240050b..9394be0 100644 --- a/tests/remote/configs/nat-off.json +++ b/tests/remote/configs/nat-off.json @@ -4,7 +4,7 @@ "vpn": {"enabled": false}, "interfaces": {"external": "@ETH0@"}, "wan": {"local_fqdn": "lm-nat.test"}, - "local": {"cidrs": ["172.31.11.0/24"]}, + "local": {"cidrs": ["@CIDR0@"]}, "wireguard": {"enabled": false, "address": "10.66.0.1/24", "listen_port": 51820}, "openvpn": {"enabled": false, "address_pool": "10.67.0.0/24", "port": 1194, "protocol": "udp"}, "https": {"enabled": false} diff --git a/tests/remote/configs/nat.json b/tests/remote/configs/nat.json index fd6f28b..c548b7f 100644 --- a/tests/remote/configs/nat.json +++ b/tests/remote/configs/nat.json @@ -4,7 +4,7 @@ "vpn": {"enabled": true}, "interfaces": {"external": "@ETH0@"}, "wan": {"local_fqdn": "lm-nat.test"}, - "local": {"cidrs": ["172.31.11.0/24"]}, + "local": {"cidrs": ["@CIDR0@"]}, "wireguard": {"enabled": true, "address": "10.66.0.1/24", "listen_port": 51820}, "openvpn": {"enabled": true, "address_pool": "10.67.0.0/24", "port": 1194, "protocol": "udp"}, "https": {"enabled": false} diff --git a/tests/remote/run.py b/tests/remote/run.py index a541764..188a1ed 100755 --- a/tests/remote/run.py +++ b/tests/remote/run.py @@ -10,6 +10,7 @@ check failed. import argparse import importlib +import ipaddress import json import os import pathlib @@ -26,7 +27,11 @@ SCENARIOS = sorted(path.stem for path in (HERE / 'scenarios').glob('*.py') if pa class Lab: - """The instances and networks of a run on one LXD remote and project, and the results of its checks.""" + """The instances and networks of a run on one LXD remote and project, and the results of its checks. + + A NIC is a (network, mac, address/prefix[, gateway]) tuple; an address of None is taken from + DHCP. + """ def __init__(self, remote, project, pool, image, keep, deb, admin_password, psk): self.remote = remote @@ -117,22 +122,21 @@ class Lab: self.networks.append(name) def instance(self, name, kind, user_data, *nics): - """Create and start a vm or container whose NICs are (network, mac, address/prefix[, gateway]) tuples. + """Create and start a vm or container with the NICs, whose static addresses are set through cloud-init. - The addresses are static, set through cloud-init. A container's NICs are eth0 and on, named - by LXD, as a veth has no permanent MAC address for netplan to match; a VM's NICs keep their - kernel names, which the initramfs sets before netplan could rename them, and are matched by - MAC address (nic_of). + A container's NICs are eth0 and on, named by LXD, as a veth has no permanent MAC address for + netplan to match; a VM's NICs keep their kernel names, which the initramfs sets before + netplan could rename them, and are matched by MAC address (nic_of). """ self.macs[name] = [nic[1] for nic in nics] - self.addresses[name] = nics[0][2].split('/')[0] + self.addresses[name] = nics[0][2] netcfg = 'version: 2\nethernets:\n' for index, (_, mac, address, *gateway) in enumerate(nics): if kind == 'vm': netcfg += f' nic{index}:\n match:\n macaddress: "{mac}"\n' else: netcfg += f' eth{index}:\n' - netcfg += f' addresses: [{address}]\n' + netcfg += f' addresses: [{address}]\n' if address else ' dhcp4: true\n' if gateway: netcfg += (f' routes:\n - to: default\n via: {gateway[0]}\n' f' nameservers:\n addresses: [{gateway[0]}]\n') @@ -151,7 +155,8 @@ class Lab: def ready(self, *names): """Wait for each instance's agent and cloud-init, then for its first address; False when one is not ready. - cloud-init's status 2, done with recoverable errors, counts as booted. + cloud-init's status 2, done with recoverable errors, counts as booted. An instance whose first + NIC takes its address from DHCP waits for any IPv4 address outside lo. """ for name in names: for _ in range(120): @@ -161,13 +166,14 @@ class Lab: else: print(f'{name} did not boot', file=sys.stderr) return False - pattern = f' inet {re.escape(self.addresses[name])}/' + address = self.addresses[name] + pattern = f' inet {re.escape(address.split("/")[0])}/' if address else r'^\d+: (?!lo\b)\S+ +inet ' for _ in range(24): - if re.search(pattern, self.on(name, 'ip', '-4', '-o', 'addr', 'show').stdout): + if re.search(pattern, self.on(name, 'ip', '-4', '-o', 'addr', 'show').stdout, re.M): break time.sleep(5) else: - print(f'{name} has no address {self.addresses[name]}', file=sys.stderr) + print(f'{name} has no address {address or "from DHCP"}', file=sys.stderr) return False return True @@ -181,6 +187,17 @@ class Lab: return line.split()[1].rstrip(':').split('@')[0] return None + def address(self, name): + """The IPv4 interface address of the instance's first NIC: the static one, else the one it holds.""" + if self.addresses.get(name): + return ipaddress.IPv4Interface(self.addresses[name]) + nic = self.nic_of(name, 0) + match = re.search(r' inet (\S+) ', self.on(name, 'ip', '-4', '-o', 'addr', 'show', 'dev', nic).stdout) \ + if nic else None + if not match: + raise RuntimeError(f'{name} has no IPv4 address on its first NIC') + return ipaddress.IPv4Interface(match.group(1)) + def client(self, name): """Install the tools of a VPN client; True when they are installed.""" return (self.on(name, 'apt-get', 'update', '-q').returncode == 0 and @@ -205,7 +222,11 @@ class Lab: return result.returncode == 0 and self.configure(name, config) def render(self, name, config): - """Replace the server's configuration, with the pre-shared key for @PSK@ and its NIC names for @ETH0@ and @ETH1@.""" + """Replace the server's configuration, with its placeholders filled in. + + @PSK@ is the pre-shared key, @ETH0@ and @ETH1@ the names of the server's NICs and @CIDR0@ the + network of its first address. + """ text = (HERE / 'configs' / config).read_text(encoding='utf-8').replace('@PSK@', self.psk) for index in (0, 1): placeholder = f'@ETH{index}@' @@ -214,6 +235,8 @@ class Lab: if nic is None: raise RuntimeError(f'{name} has no NIC {index} for {placeholder} of {config}') text = text.replace(placeholder, nic) + if '@CIDR0@' in text: + text = text.replace('@CIDR0@', str(self.address(name).network)) return self.push_text(text, name, '/etc/linux-manager/linux-manager.json', '0600') def configure(self, name, config): @@ -251,7 +274,8 @@ class Lab: server_key = self.on(server, 'cat', '/etc/wireguard/wg0.pub').stdout.strip() conf = (f'[Interface]\nPrivateKey = {private_key}\nAddress = {address}/32\n\n' f'[Peer]\nPublicKey = {server_key}\nEndpoint = {endpoint}:51820\nAllowedIPs = {allowed}\n') - return self.push_text(conf, client, '/etc/wireguard/wg0.conf', '0600') and self.on(client, 'bash', '/usr/bin/wg-quick', 'up', 'wg0').returncode == 0 + return (self.push_text(conf, client, '/etc/wireguard/wg0.conf', '0600') and + self.on(client, 'bash', '/usr/bin/wg-quick', 'up', 'wg0').returncode == 0) def wireguard_module(self, name): """True when the instance can create a WireGuard link, that is the host kernel has the module.""" diff --git a/tests/remote/scenarios/core.py b/tests/remote/scenarios/core.py index ee960c8..4e7f3e5 100644 --- a/tests/remote/scenarios/core.py +++ b/tests/remote/scenarios/core.py @@ -34,4 +34,5 @@ def run(lab): lab.expect('core: web state has router and vpn false', r'"router": false, "vpn": false', 'lm-core', 'cat', '/run/linux-manager/web.json') lab.expect('core: status web service answers the client', r'^200$', - 'lm-core-client', 'curl', '-s', '-o', '/dev/null', '-w', '%{http_code}', 'http://172.31.10.10:7081/status') + 'lm-core-client', 'curl', '-s', '-o', '/dev/null', '-w', '%{http_code}', + f'http://{lab.address("lm-core").ip}:7081/status') diff --git a/tests/remote/scenarios/nat.py b/tests/remote/scenarios/nat.py index 23ee3d5..b5f9861 100644 --- a/tests/remote/scenarios/nat.py +++ b/tests/remote/scenarios/nat.py @@ -18,6 +18,9 @@ def run(lab): lab.result('nat: package installed and configured', lab.server('lm-nat', 'nat.json')) lab.echo_server('lm-nat-target') ext = re.escape(lab.nic_of('lm-nat', 0) or '') + server = str(lab.address('lm-nat').ip) + target = str(lab.address('lm-nat-target').ip) + from_server = f'^{re.escape(server)}$' lab.expect('nat: setup succeeded', r'^success$', 'lm-nat', 'systemctl', 'show', '-p', 'Result', '--value', 'linux-manager-setup') @@ -34,9 +37,9 @@ def run(lab): lab.result('nat: VPN user has a certificate', user is not None and user.get('certificate') is True) if lab.wireguard_module('lm-nat-client'): - if lab.wireguard_client('lm-nat-client', 'lm-nat', '172.31.11.10', '10.66.0.2', '172.31.11.20/32'): - lab.expect('nat: WireGuard client reaches the target from the server address', r'^172\.31\.11\.10$', - 'lm-nat-client', 'curl', '-s', '--max-time', '10', 'http://172.31.11.20:8080/') + if lab.wireguard_client('lm-nat-client', 'lm-nat', server, '10.66.0.2', f'{target}/32'): + lab.expect('nat: WireGuard client reaches the target from the server address', from_server, + 'lm-nat-client', 'curl', '-s', '--max-time', '10', f'http://{target}:8080/') else: lab.result('nat: WireGuard client set up', False) lab.on('lm-nat-client', 'bash', '/usr/bin/wg-quick', 'down', 'wg0') @@ -46,16 +49,16 @@ def run(lab): profile = (lab.json_of('lm-nat', 'linux-manager-user', '--json', 'profile', 'alice') or {}).get('profile', '') lab.push_text(profile, 'lm-nat-client', '/root/alice.ovpn', '0600') lab.push_text(f'alice\n{lab.admin_password}\n', 'lm-nat-client', '/root/alice.pass', '0600') - lab.on('lm-nat-client', 'sh', '-c', 'echo "172.31.11.10 lm-nat.test" >>/etc/hosts') + lab.on('lm-nat-client', 'sh', '-c', f'echo "{server} lm-nat.test" >>/etc/hosts') lab.on('lm-nat-client', 'openvpn', '--config', '/root/alice.ovpn', '--auth-user-pass', '/root/alice.pass', - '--route-nopull', '--disable-dco', '--route', '172.31.11.20', '255.255.255.255', + '--route-nopull', '--disable-dco', '--route', target, '255.255.255.255', '--daemon', '--log', '/root/openvpn.log') for _ in range(12): if lab.on('lm-nat-client', 'grep', '-q', 'Initialization Sequence Completed', '/root/openvpn.log').returncode == 0: break time.sleep(5) - lab.expect('nat: OpenVPN client reaches the target from the server address', r'^172\.31\.11\.10$', - 'lm-nat-client', 'curl', '-s', '--max-time', '10', 'http://172.31.11.20:8080/') + lab.expect('nat: OpenVPN client reaches the target from the server address', from_server, + 'lm-nat-client', 'curl', '-s', '--max-time', '10', f'http://{target}:8080/') lab.on('lm-nat-client', 'pkill', 'openvpn') lab.configure('lm-nat', 'nat-off.json') diff --git a/tests/remote/vm.py b/tests/remote/vm.py new file mode 100755 index 0000000..a773ac2 --- /dev/null +++ b/tests/remote/vm.py @@ -0,0 +1,175 @@ +#!/usr/bin/env python3 +"""Launch one Linux Manager server VM on an LXD remote to work with, or remove it: vm.py [--remove]. + +The VM is launched on the remote (--remote or LXD_REMOTE) in --project with that project's +default profile. It gets the newest out/linux-manager_*_all.deb with the administrator admin and +a generated password, and a configuration from configs/ (core.json by default), whose @ETH0@ is +the interface of the VM's default route, @CIDR0@ its network and @PSK@ a generated key. The +password and the key are written to tmp/vm-NAME.env. The VM's leased address is pinned on its +NIC, and NAT proxy devices forward ports 7080 and 7081 of the host address --listen (or LXD_LISTEN, +by default the IPv4 address of the host of the remote's URL) to the same ports of the VM, and port +2222 to its SSH port. --remove deletes the VM and that file. +""" + +import argparse +import ipaddress +import json +import os +import pathlib +import re +import secrets +import socket +import subprocess +import sys +import time +import urllib.parse + +HERE = pathlib.Path(__file__).resolve().parent +ROOT = HERE.parent.parent +# The host port and the VM port of each proxy: the Manager, the status web service and SSH. +PROXIES = ((7080, 7080), (7081, 7081), (2222, 22)) + + +def inside(args, *command, input=None): + """Run the command in the VM with the input, and return its CompletedProcess.""" + return subprocess.run(['lxc', 'exec', f'{args.remote}:{args.name}', '--project', args.project, '-T', '--', + *command], input=input, capture_output=True, text=True) + + +def push(args, source, path, mode): + """Copy the local file into the VM at the path with the mode; True when it was copied.""" + return subprocess.run(['lxc', 'file', 'push', str(source), f'{args.remote}:{args.name}{path}', + '--project', args.project, '--mode', mode], capture_output=True).returncode == 0 + + +def main(): + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument('--remote', default=os.environ.get('LXD_REMOTE'), help='the lxc remote (LXD_REMOTE)') + parser.add_argument('--project', default='default', help='the project of the VM (default default)') + parser.add_argument('--name', default='lm-dev', help='the name of the VM (default lm-dev)') + parser.add_argument('--image', default=os.environ.get('LXD_IMAGE', 'ubuntu:26.04'), + help='the image of the VM (LXD_IMAGE, default ubuntu:26.04)') + parser.add_argument('--config', default='core.json', help='the configuration from configs/ (default core.json)') + parser.add_argument('--listen', default=os.environ.get('LXD_LISTEN'), + help='the LXD host address the proxies listen on (LXD_LISTEN, default the IPv4 address ' + 'of the remote URL\'s host)') + parser.add_argument('--remove', action='store_true', help='delete the VM and its env file') + args = parser.parse_args() + if not args.remote: + parser.error('--remote or LXD_REMOTE is required') + if not args.remove and not args.listen: + # The default listen address is the IPv4 address of the remote's host in its lxc URL. + remotes = json.loads(subprocess.run(['lxc', 'remote', 'list', '--format', 'json'], capture_output=True, + text=True, check=True).stdout) + host = urllib.parse.urlsplit(remotes.get(args.remote, {}).get('Addr', '')).hostname + try: + args.listen = socket.getaddrinfo(host, None, socket.AF_INET)[0][4][0] if host else None + except socket.gaierror: + pass + if not args.listen: + parser.error(f'no IPv4 address of the host of remote {args.remote}; give --listen or LXD_LISTEN') + where = f'{args.remote}:{args.name} (project {args.project})' + exists = subprocess.run(['lxc', 'info', f'{args.remote}:{args.name}', '--project', args.project], + capture_output=True).returncode == 0 + env = ROOT / 'tmp' / f'vm-{args.name}.env' + + if args.remove: + if not exists: + sys.exit(f'no VM {where}') + subprocess.run(['lxc', 'delete', '--force', f'{args.remote}:{args.name}', '--project', args.project], + check=True) + env.unlink(missing_ok=True) + print(f'removed {where}') + return + + if exists: + sys.exit(f'{where} exists; remove it with --remove or choose another --name') + config = HERE / 'configs' / args.config + if not config.is_file(): + parser.error(f'no configs/{args.config}') + debs = sorted((ROOT / 'out').glob('linux-manager_*_all.deb')) + if not debs: + sys.exit('no out/linux-manager_*_all.deb; run ./build.sh first') + + password, psk = secrets.token_hex(16), secrets.token_hex(24) + env.parent.mkdir(parents=True, exist_ok=True) + env.touch(mode=0o600) + env.chmod(0o600) + env.write_text(f'ADMIN_USERNAME=admin\nADMIN_PASSWORD={password}\nPSK={psk}\n', encoding='utf-8') + + print(f'launching {where} from {args.image} with {debs[-1].name}; proxies listen on {args.listen}') + subprocess.run(['lxc', 'launch', args.image, f'{args.remote}:{args.name}', '--vm', '-c', 'limits.cpu=2', + '-c', 'limits.memory=4GiB', + '-c', 'cloud-init.user-data=' + (HERE / 'cloud-init' / 'server.yaml').read_text(encoding='utf-8'), + '--project', args.project], check=True) + + # Wait for the agent and cloud-init (status 2 is done with recoverable errors), then for the default route. + for _ in range(120): + if inside(args, 'cloud-init', 'status', '--wait').returncode in (0, 2): + break + time.sleep(5) + else: + sys.exit(f'{args.name} did not boot; remove it with --remove') + for _ in range(24): + route = re.search(r'\bdev (\S+)', inside(args, 'ip', '-4', '-o', 'route', 'show', 'default').stdout) + address = route and re.search(r' inet (\S+) ', + inside(args, 'ip', '-4', '-o', 'addr', 'show', 'dev', route.group(1)).stdout) + if address: + break + time.sleep(5) + else: + sys.exit(f'{args.name} has no default route or address; remove it with --remove') + nic, interface = route.group(1), ipaddress.IPv4Interface(address.group(1)) + + # Pin the leased address on the profile's NIC and proxy the host's --listen ports to the VM. + mac = inside(args, 'cat', f'/sys/class/net/{nic}/address').stdout.strip() + expanded = subprocess.run(['lxc', 'config', 'show', '--expanded', f'{args.remote}:{args.name}', + '--project', args.project], capture_output=True, text=True, check=True).stdout + device = next((m.group(1) for m in re.finditer(r'^\s*volatile\.(\S+)\.hwaddr: "?([0-9a-f:]+)"?$', expanded, re.M) + if m.group(2) == mac), None) + if not device: + sys.exit(f'no LXD device of {args.name} has the MAC address {mac} of {nic}') + subprocess.run(['lxc', 'config', 'device', 'override', f'{args.remote}:{args.name}', device, + f'ipv4.address={interface.ip}', '--project', args.project], check=True) + for host_port, vm_port in PROXIES: + subprocess.run(['lxc', 'config', 'device', 'add', f'{args.remote}:{args.name}', f'proxy-{vm_port}', 'proxy', + f'listen=tcp:{args.listen}:{host_port}', f'connect=tcp:{interface.ip}:{vm_port}', 'nat=true', + '--project', args.project], check=True) + + # Install the package with the test administrator. + if not push(args, debs[-1], '/tmp/linux-manager.deb', '0644'): + sys.exit(f'could not copy {debs[-1].name} into {args.name}') + preseed = (f'linux-manager linux-manager/admin_username string admin\n' + f'linux-manager linux-manager/admin_password password {password}\n') + if inside(args, 'debconf-set-selections', input=preseed).returncode != 0: + sys.exit('debconf-set-selections failed') + result = inside(args, 'env', 'DEBIAN_FRONTEND=noninteractive', 'apt-get', 'install', '-y', '/tmp/linux-manager.deb') + log = ROOT / 'tmp' / f'vm-{args.name}-install.log' + log.write_text(result.stdout + result.stderr, encoding='utf-8') + if result.returncode != 0: + sys.exit(f'the package did not install; see {log}') + + # Apply the configuration twice, as the server's certificates follow the first run. + text = (config.read_text(encoding='utf-8').replace('@ETH0@', nic).replace('@CIDR0@', str(interface.network)) + .replace('@PSK@', psk)) + rendered = ROOT / 'tmp' / f'vm-{args.name}.json' + rendered.touch(mode=0o600) + rendered.chmod(0o600) + rendered.write_text(text, encoding='utf-8') + pushed = push(args, rendered, '/etc/linux-manager/linux-manager.json', '0600') + rendered.unlink() + if not pushed: + sys.exit(f'could not copy {args.config} into {args.name}') + for _ in range(2): + if inside(args, 'systemctl', 'restart', 'linux-manager-setup').returncode != 0: + sys.exit(f'linux-manager-setup failed with {args.config}; see journalctl -u linux-manager-setup in the VM') + + print(f'{args.name} is up at {interface.ip} with {args.config}') + print(f'administrator: admin password: {password} (also in {env})') + print(f'manager: http://{args.listen}:7080/ status: http://{args.listen}:7081/ ssh: {args.listen} port 2222') + print(f'shell: lxc exec {args.remote}:{args.name} --project {args.project} -- su -l') + print(f'remove: {sys.argv[0]} --remote {args.remote} --project {args.project} --name {args.name} --remove') + + +if __name__ == '__main__': + main() diff --git a/tests/remote/wireguard_peer.py b/tests/remote/wireguard_peer.py index cfba7f4..8feeb04 100644 --- a/tests/remote/wireguard_peer.py +++ b/tests/remote/wireguard_peer.py @@ -7,7 +7,7 @@ import sys sys.path.insert(0, '/usr/lib/linux-manager') -import linuxmanager_users as users # noqa: E402 +import linuxmanager_users as users public_key, address = sys.argv[1:3] with users.session() as connection: diff --git a/web/src/NetworkPanel.tsx b/web/src/NetworkPanel.tsx index 9905655..6f152f2 100644 --- a/web/src/NetworkPanel.tsx +++ b/web/src/NetworkPanel.tsx @@ -13,29 +13,48 @@ const LAYOUT_KEY = 'linux-manager-ui.network-layout' const readDetails = () => request('GET', '/config', DETAILS_SHAPE) /** - * The server's network, from its /config: the external and the internal interface with their addresses, the internal - * gateway and the local CIDRs, and the IPv4 routes. The panels are on a board whose layout is kept in the browser. + * The server's network, from its /config: the interfaces but loopback, external when they hold an IPv4 default route + * and internal otherwise; the internal gateway and the local CIDRs while the router is on; and the IPv4 routes. The + * panels are on a board whose layout is kept in the browser. */ export default function NetworkPanel() { const { data: details, error, busy, refresh } = useRead(readDetails) const board = useBoard(LAYOUT_KEY) const network = details?.network + const router = details?.configuration.router?.enabled === true + // An interface holding an IPv4 default route is external, every other one internal. + const external = new Set(network?.routes?.filter((route) => route.dst === 'default').map((route) => route.dev)) const panels: BoardPanel[] = network ? [ { - id: 'network', - title: 'Network', + id: 'interfaces', + title: 'Interfaces', + content: ( + [ + link.name, + external.has(link.name) ? 'external' : 'internal', + text(link.state), + text(link.ipv4), + text(link.ipv6), + text(link.mac), + ])} + /> + ), + }, + ...(router ? [{ + id: 'router', + title: 'Router', content: ( ), - }, + }] : []), { id: 'routes', title: 'Routes', diff --git a/web/src/api.ts b/web/src/api.ts index f35ebf4..9416f96 100644 --- a/web/src/api.ts +++ b/web/src/api.ts @@ -73,7 +73,9 @@ export type RouterDetails = { local_cidrs?: string[] /** Each site-to-site connection with the identities and subnets the server resolved for it. */ site_to_site?: { name: string; local_identity: string; remote_identity: string; local_cidrs: string[]; remote_cidrs: string[] }[] - routes: { dst: string; gateway?: string; dev: string; protocol?: string; scope?: string; prefsrc?: string; metric?: number; flags: string[] }[] | null + /** The interfaces but loopback, with their IPv4 and global IPv6 addresses as address/prefix; null when ip fails. */ + interfaces?: { name: string; state: string | null; mac: string | null; ipv4: string[]; ipv6: string[] }[] | null + routes:{ dst: string; gateway?: string; dev: string; protocol?: string; scope?: string; prefsrc?: string; metric?: number; flags: string[] }[] | null } /** Objects of swanctl --list-conns and --list-sas; the values are strings as swanctl prints them. */ ipsec: { @@ -635,6 +637,13 @@ export const DETAILS_SHAPE = object({ local_cidrs: list(string), remote_cidrs: list(string), }))), + interfaces: optional(nullable(list(object({ + name: string, + state: nullable(string), + mac: nullable(string), + ipv4: list(string), + ipv6: list(string), + })))), routes: nullable(list(object({ dst: string, gateway: optional(string),