Files
MikrotikManager/backend/scripts/acceptance/chr-netflow-collector-container.py
T

326 lines
18 KiB
Python

"""Fixed, opt-in local Docker owner for the disposable NetFlow collector."""
import argparse
import json
import os
import re
import secrets
import stat
import subprocess
import sys
from pathlib import Path
PG_IMAGE = 'postgres@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873'
NODE_IMAGE = 'git.shx.one/denozord/mikrotikmanager-backend-mcp-acceptance@sha256:151ab635caca5f2f6b50a9d0c27980a1559d7bd67f0e3a8d2b74cfa3f9ec6945'
CID = re.compile(r'[a-f0-9]{64}\Z')
RUN = re.compile(r'[a-f0-9]{8}\Z')
LABEL = 'io.mmapp.netflow.run'
ENV = {'PATH': '/usr/sbin:/usr/bin:/sbin:/bin', 'LC_ALL': 'C',
'HOME': '/nonexistent', 'DOCKER_CONFIG': '/nonexistent'}
def require(value, code):
if not value:
raise ValueError(code)
def root(run):
require(isinstance(run, str) and RUN.fullmatch(run), 'RUN_ID_REFUSED')
return '/tmp/mcp-netflow-' + run
def mounts(source, target, readonly=False):
return ['--mount', 'type=bind,src=' + source + ',dst=' + target + (',readonly' if readonly else '')]
def build_pg(run, uid, gid):
base = root(run)
require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED')
return [
'create', '--pull=never', '--name', 'mm-nf-pg-' + run,
'--label', LABEL + '=' + run, '--label', 'io.mmapp.netflow.role=postgres',
'--network=none', '--user', f'{uid}:{gid}', '--read-only',
'--cap-drop=ALL', '--security-opt=no-new-privileges',
'--memory=192m', '--memory-swap=192m',
'--tmpfs', '/tmp:rw,nosuid,nodev,noexec,mode=1777,size=32m',
'--tmpfs', '/var/run/postgresql:rw,nosuid,nodev,noexec,mode=1777,size=1m',
*mounts(base + '/pg', '/owned-pg'),
*mounts(base + '/pg-socket', base + '/pg-socket'),
*mounts(base + '/pg-password', '/run/mm-netflow-pg-secret', True),
'--env', 'PGDATA=/owned-pg/data', '--env', 'POSTGRES_USER=mcp_test',
'--env', 'POSTGRES_DB=mcp_netflow_' + run,
'--env', 'POSTGRES_PASSWORD_FILE=/run/mm-netflow-pg-secret',
'--env', 'POSTGRES_INITDB_ARGS=--auth-local=scram-sha-256 --auth-host=scram-sha-256',
PG_IMAGE, 'postgres', '-c', 'listen_addresses=', '-c', 'unix_socket_directories=' + base + '/pg-socket',
'-c', 'unix_socket_permissions=0700',
]
def build_node(run, uid, gid, anchor_id, port, namespace, host_namespace, packet_digest):
base = root(run)
require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED')
require(isinstance(anchor_id, str) and CID.fullmatch(anchor_id), 'ANCHOR_ID_REFUSED')
require(isinstance(packet_digest, str) and CID.fullmatch(packet_digest), 'PACKET_PIN_REFUSED')
require(type(port) is int and 49152 <= port <= 65535, 'COLLECTOR_PORT_REFUSED')
require(all(type(pair) in (tuple, list) and len(pair) == 2 and
all(type(value) is int and 0 < value < 2**64 for value in pair)
for pair in (namespace, host_namespace)) and list(namespace) != list(host_namespace), 'NAMESPACE_PINS_REFUSED')
return [
'create', '--pull=never', '--name', 'mm-nf-node-' + run,
'--label', LABEL + '=' + run, '--label', 'io.mmapp.netflow.role=collector',
'--network=container:' + anchor_id, '--user', f'{uid}:{gid}', '--read-only',
'--cap-drop=ALL', '--security-opt=no-new-privileges', '--interactive', '--tty',
'--memory=320m', '--memory-swap=320m',
'--tmpfs', '/tmp:rw,nosuid,nodev,noexec,mode=1777,size=32m',
*mounts(base + '/collector', base),
*mounts(base + '/pg-socket', base + '/pg-socket'),
*mounts(base + '/pg-password', '/run/mm-netflow-pg-secret', True),
*mounts(base + '/packet/dist', '/app/backend/dist', True),
*mounts(base + '/packet/drizzle', '/app/backend/drizzle', True),
*mounts(base + '/packet/contracts-dist', '/app/packages/contracts/dist', True),
*mounts(base + '/packet/manifest.json', '/run/mm-netflow-packet.json', True),
'--workdir', base, '--env', 'MMAPP_OWN_CHR_NETFLOW_COLLECTOR=true',
'--env', 'MMAPP_NETFLOW_PACKET_SHA256=' + packet_digest,
'--entrypoint', 'node', NODE_IMAGE,
'--max-old-space-size=160', '--max-semi-space-size=8',
'/app/backend/dist/test/traffic-flow-chr-collector-bootstrap.js',
'--container-collector-cycle', run, str(port), *map(str, namespace), *map(str, host_namespace),
]
def capture_args(run, first, second, port):
root(run)
require((first, second) == ('198.18.241.2', '198.18.241.9') and
type(port) is int and 49152 <= port <= 65535, 'CAPTURE_SCOPE_REFUSED')
return ['nsenter', '--net=/run/netns/nf' + run + 'nc', '--',
'tcpdump', '-n', '-p', '-U', '-i', 'nf' + run + 'cv', '-y', 'EN10MB', '-s', '4138',
'-c', '64', '-w', '-', 'udp and dst host 198.18.242.3 and dst port ' + str(port) +
' and (src host ' + first + ' or src host ' + second + ')']
def local_docker():
require(sys.platform == 'linux' and os.geteuid() == 0 and
os.environ.get('MMAPP_OWN_CHR_NETFLOW_CONTAINER') == 'true', 'DOCKER_OWNER_REFUSED')
socket = Path('/var/run/docker.sock')
require(stat.S_ISSOCK(socket.stat().st_mode), 'LOCAL_DOCKER_SOCKET_UNAVAILABLE')
choices = [Path(folder, 'docker') for folder in ('/usr/bin', '/usr/sbin', '/bin')]
binary = next((path.resolve(strict=True) for path in choices if path.is_file()), None)
require(binary is not None and binary.stat().st_uid == 0 and not binary.stat().st_mode & 0o022,
'DOCKER_BINARY_UNPINNED')
def call(*args):
result = subprocess.run([str(binary), '--host', 'unix:///var/run/docker.sock', *args],
env=ENV, stdin=subprocess.DEVNULL, stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, timeout=30)
require(result.returncode == 0 and len(result.stdout) <= 256 * 1024, 'DOCKER_OUTCOME_UNCONFIRMED')
return result.stdout.decode('utf-8', 'strict').strip()
def attach(cid):
require(CID.fullmatch(cid) and sys.stdin.isatty() and sys.stdout.isatty(), 'EXACT_TTY_REFUSED')
return subprocess.run([str(binary), '--host', 'unix:///var/run/docker.sock', 'start', '--attach', '--interactive', cid],
env=ENV, timeout=330).returncode
return call, attach
def prepare_private_storage(run, uid, gid):
"""Create only new private bind sources. A retained fixture is never reused."""
require(sys.platform == 'linux' and os.geteuid() == 0 and
os.environ.get('MMAPP_OWN_CHR_NETFLOW_CONTAINER') == 'true', 'DOCKER_OWNER_REFUSED')
require(type(uid) is int and 0 < uid <= 65534 and type(gid) is int and 0 < gid <= 65534, 'FIXTURE_ACCOUNT_REFUSED')
base = Path(root(run))
info = base.lstat()
require(base.resolve(strict=True) == base and stat.S_ISDIR(info.st_mode) and info.st_uid == uid and
stat.S_IMODE(info.st_mode) == 0o700, 'FIXTURE_ROOT_REFUSED')
for name in ('pg', 'pg-socket', 'collector'):
directory = base / name
directory.mkdir(mode=0o700)
os.chown(directory, uid, gid, follow_symlinks=False)
fd = os.open(base / 'pg-password', os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW, 0o400)
with os.fdopen(fd, 'wb') as output:
output.write((secrets.token_hex(32) + '\n').encode('ascii'))
output.flush()
os.fsync(output.fileno())
os.fchown(output.fileno(), uid, gid)
class CollectorContainerOwner:
"""Outer Linux parent retains each acknowledged ID and journals it.
An unknown create/start/retire acknowledgement requires reconciliation.
It never searches by label, restarts, prunes, or removes a matched name.
"""
def __init__(self, run, docker, checkpoint):
root(run)
self.run, self.docker, self.checkpoint = run, docker, checkpoint
self.ids = {}
self.attempted = set()
self.verified_exits = {}
self.anchor_start = None
self.adopted = None
self.topology_retired = False
def image(self, reference):
rows = json.loads(self.docker('image', 'inspect', reference))
require(len(rows) == 1 and reference in rows[0]['RepoDigests'] and rows[0]['Os'] == 'linux' and
re.fullmatch(r'sha256:[a-f0-9]{64}', rows[0]['Id']), 'CACHED_IMAGE_UNPINNED')
return rows[0]['Id']
def inspect(self, cid, role, image_id, network):
require(CID.fullmatch(cid), 'CONTAINER_ID_UNPINNED')
rows = json.loads(self.docker('container', 'inspect', cid))
require(len(rows) == 1, 'CONTAINER_UNCONFIRMED')
row = rows[0]
labels = row['Config']['Labels']
memory = (192 if role == 'postgres' else 320) * 1024 * 1024
require(row['Id'] == cid and row['Image'] == image_id and labels[LABEL] == self.run and
labels['io.mmapp.netflow.role'] == role and row['HostConfig']['NetworkMode'] == network and
not row['HostConfig']['Privileged'] and not row['HostConfig']['PortBindings'] and
row['HostConfig']['ReadonlyRootfs'] is True and row['HostConfig']['CapDrop'] == ['ALL'] and
not row['HostConfig']['CapAdd'] and row['HostConfig']['Memory'] == memory and
row['HostConfig']['MemorySwap'] == memory, 'CONTAINER_SCOPE_DRIFT')
return row['State']
def _create(self, role, argv, reference, network):
require(role not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
self.attempted.add(role)
image_id = self.image(reference)
self.checkpoint(role, 'create-intent', None)
cid = self.docker(*argv)
require(CID.fullmatch(cid), 'CREATE_ACK_UNCONFIRMED')
self.checkpoint(role, 'create-acknowledged', cid)
state = self.inspect(cid, role, image_id, network)
require(state['Status'] == 'created' and state['Running'] is False, 'CREATED_STATE_UNCONFIRMED')
self.ids[role] = (cid, image_id, network)
self.checkpoint(role, 'create-verified', cid)
return cid
def create_anchor(self, uid, gid):
return self._create('postgres', build_pg(self.run, uid, gid), PG_IMAGE, 'none')
def create_collector(self, uid, gid, port, namespace, host_namespace, packet_digest, assert_packet):
anchor_id, image_id, network = self.ids['postgres']
anchor = self.inspect(anchor_id, 'postgres', image_id, network)
require(anchor['Running'] is True and anchor['Pid'] > 0, 'ANCHOR_PID_UNCONFIRMED')
require(self.adopted is not None, 'C_ADOPTION_UNCONFIRMED')
adopted, read_mount = self.adopted
require(adopted == self.anchor_namespace() and adopted['namespace'] == list(namespace) and
adopted['hostNamespace'] == list(host_namespace) and read_mount() == adopted['namespace'],
'C_ADOPTION_DRIFT')
assert_packet()
return self._create('collector', build_node(self.run, uid, gid, anchor_id, port, namespace, host_namespace, packet_digest),
NODE_IMAGE, 'container:' + anchor_id)
def start_anchor(self):
cid, image_id, network = self.ids['postgres']
require('postgres-start' not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
self.attempted.add('postgres-start')
self.checkpoint('postgres', 'start-intent', cid)
require(self.docker('start', cid) == cid, 'START_ACK_UNCONFIRMED')
state = self.inspect(cid, 'postgres', image_id, network)
require(state['Running'] is True and type(state['Pid']) is int and state['Pid'] > 0 and
re.fullmatch(r'\d{4}-\d{2}-\d{2}T[0-9:.]+Z', state['StartedAt']), 'ANCHOR_PID_UNCONFIRMED')
self.checkpoint('postgres', 'start-verified', cid)
self.anchor_start = (state['Pid'], state['StartedAt'])
return state['Pid']
def anchor_namespace(self):
"""Return live anchor and host netns pins for the topology owner."""
require(sys.platform == 'linux' and os.geteuid() == 0, 'LINUX_OWNER_REQUIRED')
cid, image_id, network = self.ids['postgres']
state = self.inspect(cid, 'postgres', image_id, network)
pid = state['Pid']
started_at = state['StartedAt']
require(state['Running'] is True and self.anchor_start == (pid, started_at), 'ANCHOR_PID_UNCONFIRMED')
pin = Path('/proc', str(pid), 'ns/net').stat()
host = Path('/proc/self/ns/net').stat()
namespace, host_namespace = [pin.st_dev, pin.st_ino], [host.st_dev, host.st_ino]
latest = self.inspect(cid, 'postgres', image_id, network)
require(latest['Running'] is True and latest['Pid'] == pid and latest['StartedAt'] == started_at and
namespace != host_namespace,
'ANCHOR_NAMESPACE_DRIFT')
return {'containerId': cid, 'pid': pid, 'startedAt': started_at,
'namespace': namespace, 'hostNamespace': host_namespace}
def create_linux_topology(self, executor, backend, journal, planned, pins):
"""Bridge the exact live anchor into the existing guarded Linux owner."""
name = 'nf' + self.run + 'nc'
require(pins['nonce'][:8] == self.run and planned['names']['C'] == name, 'TOPOLOGY_RUN_DIFFERS')
require('topology-adopt' not in self.attempted, 'C_ADOPTION_REPLAY_REFUSED')
self.attempted.add('topology-adopt')
result = executor(backend, journal, planned, pins, read_collector_anchor=self.anchor_namespace)
adopted = journal.value['collectorAnchor']
require(adopted == self.anchor_namespace() and backend.namespace(name) == adopted['namespace'],
'C_ADOPTION_DRIFT')
self.adopted = (adopted, lambda: backend.namespace(name))
return result
def assert_linux_anchor_exited(self, recorded):
"""Validate the journalled container and its exact exit before C cleanup."""
cid, image_id, network = self.ids['postgres']
require(recorded['containerId'] == cid and 'postgres' in self.verified_exits and
('collector' not in self.ids or 'collector' in self.verified_exits), 'ANCHOR_EXIT_NOT_OWNED')
state = self.inspect(cid, 'postgres', image_id, network)
require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and
state['StartedAt'] == recorded['startedAt'] == self.verified_exits['postgres'], 'ANCHOR_EXIT_UNCONFIRMED')
def cleanup_linux_topology(self, executor, backend, journal, planned):
require(self.adopted is not None and self.adopted[0] == journal.value.get('collectorAnchor') and
backend.root_baseline() == journal.value.get('rootBaseline'), 'TOPOLOGY_CLEANUP_SCOPE_DRIFT')
executor(backend, journal, planned, assert_collector_anchor_exited=self.assert_linux_anchor_exited)
require(journal.value['state'] == 'retired' and backend.root_baseline() == journal.value['rootBaseline'],
'TOPOLOGY_CLEANUP_UNCONFIRMED')
self.topology_retired = True
def start_collector(self, attach):
cid, image_id, network = self.ids['collector']
require('collector-start' not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
require(self.adopted is not None and self.adopted[0] == self.anchor_namespace() and
self.adopted[1]() == self.adopted[0]['namespace'], 'C_ADOPTION_DRIFT')
self.attempted.add('collector-start')
self.checkpoint('collector', 'start-intent', cid)
status = attach(cid)
state = self.inspect(cid, 'collector', image_id, network)
require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and
state['ExitCode'] == status and re.fullmatch(r'\d{4}-\d{2}-\d{2}T[0-9:.]+Z', state['StartedAt']),
'COLLECTOR_EXIT_UNCONFIRMED')
self.checkpoint('collector', 'exit-verified', cid)
self.verified_exits['collector'] = state['StartedAt']
require(status == 0, 'COLLECTOR_RUN_REFUSED')
def stop_anchor(self, assert_restored):
cid, image_id, network = self.ids['postgres']
require('postgres-stop' not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
require('collector' not in self.ids or 'collector' in self.verified_exits, 'COLLECTOR_EXIT_UNCONFIRMED')
self.attempted.add('postgres-stop')
assert_restored()
self.checkpoint('postgres', 'stop-intent', cid)
current = self.inspect(cid, 'postgres', image_id, network)
require(current['Running'] is True and self.anchor_start == (current['Pid'], current['StartedAt']), 'ANCHOR_START_DRIFT')
require(self.docker('stop', '--time', '15', cid) == cid, 'STOP_ACK_UNCONFIRMED')
state = self.inspect(cid, 'postgres', image_id, network)
require(state['Running'] is False and state['Status'] == 'exited' and state['Pid'] == 0 and
state['StartedAt'] == self.anchor_start[1] and state['ExitCode'] == 0, 'ANCHOR_EXIT_UNCONFIRMED')
self.checkpoint('postgres', 'exit-verified', cid)
self.verified_exits['postgres'] = state['StartedAt']
def retire_exited(self, role):
cid, image_id, network = self.ids[role]
operation = role + '-retire'
require(operation not in self.attempted, 'CONTAINER_REPLAY_REFUSED')
require(role != 'postgres' or self.adopted is None or self.topology_retired, 'C_RETIREMENT_UNCONFIRMED')
self.attempted.add(operation)
state = self.inspect(cid, role, image_id, network)
require(role in self.verified_exits and state['Running'] is False and state['Status'] == 'exited' and
state['Pid'] == 0 and state['StartedAt'] == self.verified_exits[role], 'EXIT_NOT_CONFIRMED')
self.checkpoint(role, 'retire-intent', cid)
require(self.docker('rm', cid) == cid, 'RETIRE_ACK_UNCONFIRMED')
self.checkpoint(role, 'retire-acknowledged', cid)
if __name__ == '__main__':
cli = argparse.ArgumentParser()
cli.add_argument('--check-only', required=True, metavar='RUN_ID')
args = cli.parse_args()
print(json.dumps({'fixtureRoot': root(args.check_only), 'postgresImage': PG_IMAGE, 'nodeImage': NODE_IMAGE,
'requiresOwnedCollectorNamespace': True, 'dockerUsed': False, 'networkUsed': False}))