"""CI-only, private-network, opaque TCP relay for ZITADEL localhost issuer. Run exclusively in a separate run-owned private Docker network namespace. This code never sees or parses HTTP requests, headers, query strings, credentials or cookies; it forwards bytes from IPv4 127.0.0.1:8080 to private proxy:80. No public bind, privileged sockets, credentials or filesystem writes. """ from __future__ import annotations import asyncio import errno import sys LISTEN_ADDRESS = "127.0.0.1" LISTEN_PORT = 8080 TARGET_ADDRESS = "proxy" TARGET_PORT = 80 CHUNK_SIZE = 65536 async def copy_bytes(reader: asyncio.StreamReader, writer: asyncio.StreamWriter) -> None: """Opaque, bounded-chunk bidirectional flow; never log payloads.""" try: while True: chunk = await reader.read(CHUNK_SIZE) if not chunk: break writer.write(chunk) await writer.drain() except (ConnectionError, BrokenPipeError, OSError): pass finally: try: writer.write_eof() except (AttributeError, ConnectionError, OSError): pass async def serve_client(reader: asyncio.StreamReader, client: asyncio.StreamWriter) -> None: upstream = None try: target_reader, upstream = await asyncio.wait_for( asyncio.open_connection(TARGET_ADDRESS, TARGET_PORT), timeout=7, ) await asyncio.gather( copy_bytes(reader, upstream), copy_bytes(target_reader, client), ) except (TimeoutError, ConnectionError, OSError): # Only safe stages go to stderr. No raw HTTP, cookies, secrets, URL. print("OIDC_CI_FORWARDER_UPSTREAM_NOT_AVAILABLE", file=sys.stderr, flush=True) finally: if upstream is not None: upstream.close() client.close() if upstream is not None: try: await asyncio.wait_for(upstream.wait_closed(), timeout=2) except (TimeoutError, ConnectionError, OSError): pass try: await asyncio.wait_for(client.wait_closed(), timeout=2) except (TimeoutError, ConnectionError, OSError): pass async def main() -> None: # A loopback-only listener: the runner never publishes host ports. try: server = await asyncio.start_server( serve_client, host=LISTEN_ADDRESS, port=LISTEN_PORT, family=2, # AF_INET, never an IPv6 wildcard backlog=64, reuse_address=True, limit=CHUNK_SIZE, ) except OSError as exc: if exc.errno == errno.EADDRINUSE: code = "ADDRESS_IN_USE" elif exc.errno == errno.EACCES: code = "PERMISSION_DENIED" elif exc.errno == errno.EADDRNOTAVAIL: code = "ADDRESS_NOT_AVAILABLE" else: code = "SOCKET_STARTUP_UNKNOWN" print("OIDC_CI_PYTHON_FORWARDER_STARTUP_" + code, file=sys.stderr, flush=True) raise SystemExit(1) from None print("ZITPLANE_OIDC_PYTHON_FORWARDER_LOOPBACK_LISTENING_PASS", flush=True) async with server: await server.serve_forever() if __name__ == "__main__": try: asyncio.run(main()) except KeyboardInterrupt: pass