From c2eab9db8c55ea2c593ae0bf46f70ed3ccccc459 Mon Sep 17 00:00:00 2001 From: wlsdnd2367 <230291092+wlsdnd2367@users.noreply.github.com> Date: Thu, 5 Nov 2026 12:58:44 +0900 Subject: [PATCH 1/3] Support existing Kubernetes image-agent Pods over fixed SSH bridge --- docs/README.md | 5 + docs/vm-image-prefetch.md | 23 +- docs/vm-pod-image-agent.md | 121 +++++++++ scripts/README.md | 7 + scripts/vm_image_prefetch.py | 43 +++- scripts/vm_pod_image_bridge.py | 246 ++++++++++++++++++ tests/README.md | 7 + tests/test_vm_pod_image_bridge.py | 407 ++++++++++++++++++++++++++++++ 8 files changed, 852 insertions(+), 7 deletions(-) create mode 100644 docs/vm-pod-image-agent.md create mode 100644 scripts/vm_pod_image_bridge.py create mode 100644 tests/test_vm_pod_image_bridge.py diff --git a/docs/README.md b/docs/README.md index b998f7d..fe0a004 100644 --- a/docs/README.md +++ b/docs/README.md @@ -57,3 +57,8 @@ Registry 등록의 호환성 증거를 기록합니다. `prob/for_user/` 참가자 제공 파일의 ZIP 생성, 무결성 manifest, Actions artifact 전달 방식과 Backend·Object Storage 역할 경계를 정의합니다. + +### `vm-pod-image-agent.md` + +기존 DaemonSet을 재설치하지 않고 고정 SSH 브리지로 연결합니다. node/Pod UID· +imageID·root 설정을 검증하며 실제 설치와 운영 실행은 별도 승인으로 진행합니다. diff --git a/docs/vm-image-prefetch.md b/docs/vm-image-prefetch.md index 508b445..4ada2e8 100644 --- a/docs/vm-image-prefetch.md +++ b/docs/vm-image-prefetch.md @@ -27,7 +27,21 @@ Broker 후보 자격·예약·Scheduler fallback도 변경하지 않습니다. ## VM 설치 조건 -다운로드는 기존 Runtime의 [고정 SSH 명령과 agent](https://github.com/MSG-CTF/secure-provisioner/blob/619423a35876d3e823df17b812cb0b1539001c13/docs/operations/image-preparation.md#L93)를 사용합니다. +다운로드는 기존 Runtime agent를 사용합니다. 호스트형과 Pod형을 명시적으로 선택하며, +접속한 노드에서 자동 추측하거나 실패 시 다른 경로로 fallback하지 않습니다. + +### 기존 Pod형 agent가 설치된 노드 + +`runtime-image-preparation/runtime-image-agent` DaemonSet이 이미 있는 경우 새 agent를 +중복 설치하지 않습니다. [Pod형 SSH 연결](vm-pod-image-agent.md)의 고정 브리지와 +root 소유 설정을 준비하고 `nodes.json`에 `agent_transport: kubernetes`를 명시합니다. +해당 모드에는 아래 호스트 Runtime 바이너리나 기존 preflight가 필요하지 않습니다. +브리지가 기존 Pod에서 `/usr/local/bin/image-agent --kubernetes`를 호출하며 실제 +Node UID·machine/boot·Pod UID·선언 이미지·실행 imageID를 전후에 대조합니다. + +### 호스트형 agent + +기존 [고정 SSH 명령과 agent](https://github.com/MSG-CTF/secure-provisioner/blob/619423a35876d3e823df17b812cb0b1539001c13/docs/operations/image-preparation.md#L93)를 사용합니다. - `/usr/local/libexec/runtime-image-agent`: 검토한 Runtime 바이너리. VM별 SHA-256을 고정합니다. - `/etc/secure-provisioner/image-agent.json`: 실제 Node UID와 K3s binary 절대경로를 담은 기존 설정. @@ -98,12 +112,17 @@ NOT_IN_THIS_MAINTENANCE입니다. 제외를 준비 성공으로 집계하지 않 | architecture | AMD64 또는 ARM64 | | ssh_host, ssh_port, ssh_user | 일반 SSH 주소·포트·전용 계정 | | ssh_host_key | 별도 보안 경로로 확인한 `ssh-ed25519 BASE64` 공개 host key | -| agent_sha256 | 설치된 Runtime agent의 검토된 binary SHA-256 | +| agent_sha256 | 호스트형만: 설치된 Runtime agent의 검토된 binary SHA-256 | | image_fs_path | 실제 K3s containerd 데이터 filesystem 경로 | | reserve_bytes | 보존·운영용 최소 잔여 공간 | | max_additional_bytes | 이번 전체 정비의 추가 공간 예산 | | per_image_workspace_bytes | 이미지 한 건의 임시·압축 해제 포함 작업 여유 | +Pod형은 `agent_sha256` 대신 `agent_transport: kubernetes`, node_name, +agent_pod_name, agent_pod_uid, agent_image, agent_image_id를 모두 지정합니다. +agent 이미지·실행 imageID는 각각 확인한 정확한 GHCR digest 참조로 고정합니다. +Pod 재생성으로 UID가 바뀌면 새 identity를 확인한 뒤 계획과 노드 설정을 다시 승인합니다. + 동일 architecture의 입력 VM 전체를 각 release의 approved_target_ids가 포함해야 합니다. 한 정비 계획은 최대50개 VM·100개 release·1000개 이미지/VM 쌍까지 허용합니다. VM 주소·Node UID·machine ID 중복이나 누락을 허용하지 않습니다. host key를 현장에서 diff --git a/docs/vm-pod-image-agent.md b/docs/vm-pod-image-agent.md new file mode 100644 index 0000000..b15f7f9 --- /dev/null +++ b/docs/vm-pod-image-agent.md @@ -0,0 +1,121 @@ +# 기존 Pod형 image-agent의 SSH 연결 + +호스트에 `runtime-image-agent` 파일이 없더라도 Runtime의 DaemonSet agent가 실행 +중일 수 있습니다. 이 경로는 기존 agent를 그대로 호출하는 **수동 정비용 전송 어댑터**입니다. +새 다운로드 엔진·Runtime API·Pod·DaemonSet을 만들거나 기존 agent를 재배포하지 않습니다. + +```text +기존 vm_image_prefetch 계획 검수·승인 + → 고정 SSH 명령 + → root 소유 vm-image-pod-bridge + → 같은 노드의 승인된 agent Pod exec + → 기존 image-agent의 inspect/pull/내용 검증 + → 전후 Node/Pod/호스트 identity 확인 + → 기존 파이프라인의 최종 전체 캐시 재검사 +``` + +## 실제 Runtime 계약과 같은 부분 + +- 전용 Namespace `runtime-image-preparation`, agent label과 container `agent`를 사용합니다. +- 실행 명령은 `/usr/local/bin/image-agent --kubernetes`이며 JSON은 image·architecture·pull뿐입니다. +- K3s containerd의 같은 저장소를 확인하고, 불완전한 이미지에만 기존 agent가 pull을 수행합니다. +- Runtime 요청 등록·정책 승인·현재 readiness 등록이나 Backend operation 생성은 수행하지 않습니다. +- agent Pod가 Ready라는 사실만으로 문제 이미지의 private GHCR 인증·내용 완전성·실행 승인을 증명하지 않습니다. + +## 고정 설치 파일과 권한 + +관리자가 승인한 정비에서 다음 두 새 파일만 준비합니다. **이 workflow는 설치하지 않습니다.** + +| 고정 경로 | 내용 | +|---|---| +| `/usr/local/libexec/vm-image-pod-bridge` | 이 저장소의 `scripts/vm_pod_image_bridge.py`, root:root,0755 | +| `/etc/secure-provisioner/vm-image-pod-bridge.json` | 검수한 node/Pod identity와 경로, root:root,0600 | + +`/usr/bin/python3`와 `/usr/local/bin/k3s`, root 소유·0600인 +`/etc/rancher/k3s/k3s.yaml`이 필요합니다. 별도 Python 패키지·가상환경은 필요하지 않습니다. +브리지는 절대 Python 경로와 isolated mode로 실행하며 사용자 Python 환경을 상속하지 않습니다. +모든 파일·상위 디렉터리는 root 소유이고 group/other 쓰기와 symlink를 거절합니다. + +설치 전에 파일·폴더가 이미 있는지 확인합니다. 기존 설치·권한·설정은 임의 덮어쓰기하지 +않으며 폴더 생성이 필요하면 관리자와 조율합니다. 리뷰한 소스·파일 checksum과 root 설정을 +확인한 뒤에만 다음 설치를 수행합니다. `curl | sudo ...` 형태로 실행하지 않습니다. + +```bash +sudo install -o root -g root -m 0755 vm_pod_image_bridge.py /usr/local/libexec/vm-image-pod-bridge +sudo install -o root -g root -m 0600 vm-image-pod-bridge.json /etc/secure-provisioner/vm-image-pod-bridge.json +``` + +SSH 정비 계정에는 가능하면 다음 **정확한 명령 2개**의 비대화식 sudo만 허용합니다. +임의 kubectl·Pod exec·셸·kubeconfig 읽기 권한을 이 도구의 필요 권한으로 간주하지 않습니다. + +```text +sudo -n /usr/local/libexec/vm-image-pod-bridge preflight +sudo -n /usr/local/libexec/vm-image-pod-bridge prepare +``` + +## root 설정과 노드 목록 + +설정 예시의 값은 실제 관측값으로 바꾸고 별도 보안 경로로 확인합니다. kubeconfig·토큰· +환경변수·명령 인자는 설정에 추가하지 않습니다. 다음 필드 이외에는 거절합니다. + +```json +{ + "schema_version": 1, + "node_name": "approved-node.example.internal", + "node_uid": "00000000-0000-4000-8000-000000000001", + "machine_id": "dddddddddddddddddddddddddddddddd", + "boot_id": "00000000-0000-4000-8000-000000000003", + "architecture": "AMD64", + "agent_pod_name": "runtime-image-agent-example", + "agent_pod_uid": "00000000-0000-4000-8000-000000000002", + "agent_image": "ghcr.io/msg-ctf/secure-provisioner-image-agent@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "agent_image_id": "ghcr.io/msg-ctf/secure-provisioner-image-agent@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "image_fs_path": "/var/lib/rancher/k3s/agent/containerd" +} +``` + +`nodes.json`의 해당 노드는 기존 SSH·target·예산·identity 필드를 유지하되 다음을 적용합니다. + +- `agent_transport`는 `kubernetes`로 명시합니다. 기본 호스트형과 섞어 사용할 수 있습니다. +- `agent_sha256`은 넣지 않습니다. 대신 root 설정과 같은 node_name·agent_pod_name· + agent_pod_uid·agent_image·agent_image_id를 추가합니다. +- 브리지 소스 checksum은 계획에 포함되고 preflight 결과와 대조합니다. +- Node UID와 Runtime/Broker target ID를 같은 값으로 추정하지 않습니다. target은 승인된 대응을 사용합니다. +- image와 imageID는 같은 Runtime agent package의 정확한 digest여야 합니다. OCI index와 실행 manifest가 + 다른 경우 각각 검토한 값을 명시합니다. `containerd://...` config ID를 manifest로 추정하지 않습니다. + +VM 부팅 후 실제 machine/boot·노드 UID를 확인합니다. Pod가 교체되면 이름뿐 아니라 UID도 +다시 승인합니다. 브리지가 새 Pod나 boot를 자동 신뢰하지 않습니다. 현재 관측값은 이전 관측과 +구분해 기록하며 과거 Ready를 현재 실행 권한으로 재사용하지 않습니다. + +## 실행 경계 + +우선 `{}` 입력의 preflight로 연결·identity·공간만 확인할 수 있습니다. 그 후 실제 운영 +release·전체 digest·승인 target·예산을 확정하고 기존 `scripts.vm_image_prefetch`의 +plan/approved SHA/apply 순서를 따릅니다. 이 어댑터 때문에 기존 출처·승인·만료·디스크· +단일 순차 실행·최종 재검사 조건을 제거하지 않습니다. 호스트형으로 자동 fallback하지 않습니다. + +브리지의 매 호출은 live Node/Ready와 host machine/boot/architecture를 대조합니다. +해당 노드·label에서 Ready agent가 정확히 하나여야 하며 이름·UID·이미지·실제 imageID· +컨테이너 Running/Ready가 고정값과 일치해야 합니다. prepare 전후에 같은 검사를 합니다. +Kubernetes exec에는 Pod UID 원자적 precondition이 없으므로 전후 검사를 전환 잠금으로 +오해하지 않습니다. 동시 DaemonSet 정비를 피하고, 호출 도중 변경되면 이미 cache 작업이 +일부 수행됐더라도 성공으로 기록하거나 다음 이미지로 진행하지 않습니다. + +호스트 subprocess는 고정 kubeconfig와 TLS 검증을 사용하고 shell·proxy·GitHub/Python +인증 환경을 상속하지 않습니다. Node/Pod JSON은 최대1MiB, agent 출력은64KiB, 입력은4KiB, +브리지 호출 전체는115초 예산이며 API 개별 조회는 최대5초입니다. 시간이 부족하면 전체 +검사를 생략하지 않고 중단합니다. 오류 본문·stderr·kubeconfig는 결과에 복사하지 않습니다. + +디스크 예산과 실행 대상 승인은 기존 중앙 파이프라인에서 검수합니다. 브리지 자체는 +Runtime의 실행 정책 승인·OS 강제 disk quota·SLA·Backend operation을 대신하지 않습니다. +공용 캐시 삭제·Pod 생성/삭제·active 전환·기존 Namespace/Service·NetworkPolicy 변경은 없습니다. + +## 고정 소스 대조 + +- [Runtime의 Pod 선택·exec 요청](https://github.com/MSG-CTF/secure-provisioner/blob/a4e58014bde0ee984b3a1e2120856b5ba1947a44/internal/k3s/image_agent.go) +- [Runtime의 kubernetes mode와 node identity](https://github.com/MSG-CTF/secure-provisioner/blob/a4e58014bde0ee984b3a1e2120856b5ba1947a44/cmd/image-agent/main.go) +- [기존 agent의 내용 검증과 pull](https://github.com/MSG-CTF/secure-provisioner/blob/a4e58014bde0ee984b3a1e2120856b5ba1947a44/internal/imageprep/agent.go) + +이 source 대조·모의 검사와 사용자가 전달한 메타데이터는 브리지 설치·실제 SSH·GHCR +문제 이미지 pull·현재 readiness의 독립 검증이 아닙니다. 실제 정비는 리뷰·승인 후 진행합니다. diff --git a/scripts/README.md b/scripts/README.md index 32b7470..5a21d1b 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -16,6 +16,13 @@ Provisioner API 요청으로 변환합니다. 생성 Operation을 조회한 뒤 계획 검수입니다. 실행·활성화·삭제·Runtime 설정은 변경하지 않습니다. [운영 준비와 입력 형식](../docs/vm-image-prefetch.md)을 참고합니다. +### `vm_pod_image_bridge.py` + +노드의 기존 Runtime agent Pod를 재설치하지 않고, 고정 SSH 명령에서 공식 +`image-agent --kubernetes`를 호출합니다. live node/host·Pod UID·선언/실행 imageID를 +전후 대조하고, 기존 파이프라인의 승인·예산·최종 검사 경계를 유지합니다. +[Pod형 연결·고정 파일 설치 조건](../docs/vm-pod-image-agent.md)을 참고합니다. + ### `audit_node_image_readiness.py` 기존 발행 bundle과 저장된 Runtime readiness JSON을 오프라인에서 대조합니다. diff --git a/scripts/vm_image_prefetch.py b/scripts/vm_image_prefetch.py index 7986bf1..3e5ce15 100644 --- a/scripts/vm_image_prefetch.py +++ b/scripts/vm_image_prefetch.py @@ -34,6 +34,10 @@ REPOSITORY = sources.publication.REPOSITORY AGENT_COMMAND = 'sudo -n /usr/local/libexec/runtime-image-agent' PREFLIGHT_COMMAND = 'sudo -n /usr/local/libexec/vm-image-cache-preflight' +POD_PREFLIGHT_COMMAND = 'sudo -n /usr/local/libexec/vm-image-pod-bridge preflight' +POD_AGENT_COMMAND = 'sudo -n /usr/local/libexec/vm-image-pod-bridge prepare' +POD_AGENT_IMAGE = re.compile(r'^ghcr\.io/msg-ctf/secure-provisioner-image-agent@sha256:[a-f0-9]{64}$') +POD_FIELDS = {'node_name', 'agent_pod_name', 'agent_pod_uid', 'agent_image', 'agent_image_id'} MAX_BYTES = 1024 * 1024 MAX_PAIRS = 1000 @@ -94,10 +98,16 @@ def checked_nodes(inventory): and 1 <= len(inventory['nodes']) <= 50) nodes, ids, machines, uids, hosts = [], set(), set(), set(), set() fields = {'node_id', 'target_id', 'node_uid', 'machine_id', 'boot_id', 'architecture', - 'ssh_host', 'ssh_port', 'ssh_user', 'ssh_host_key', 'agent_sha256', + 'ssh_host', 'ssh_port', 'ssh_user', 'ssh_host_key', 'image_fs_path', 'reserve_bytes', 'max_additional_bytes', 'per_image_workspace_bytes'} for row in inventory['nodes']: - require(isinstance(row, dict) and set(row) == fields) + require(isinstance(row, dict)) + transport = row.get('agent_transport', 'host') + require(transport in ('host', 'kubernetes')) + expected = fields | (POD_FIELDS | {'agent_transport'} if transport == 'kubernetes' else {'agent_sha256'}) + if transport == 'host' and 'agent_transport' in row: + expected |= {'agent_transport'} + require(set(row) == expected) for key in ('node_id', 'target_id', 'node_uid', 'machine_id', 'boot_id'): require(isinstance(row[key], str) and IDENTITY.fullmatch(row[key])) require(NAME.fullmatch(row['node_id']) and row['node_id'] not in ids) @@ -121,7 +131,18 @@ def checked_nodes(inventory): require(len(key) == 2 and key[0] == 'ssh-ed25519') decoded = base64.b64decode(key[1], validate=True) require(len(decoded) == 51 and decoded[:19] == b'\x00\x00\x00\x0bssh-ed25519\x00\x00\x00\x20') - require(isinstance(row['agent_sha256'], str) and SHA.fullmatch(row['agent_sha256'])) + if transport == 'host': + require(isinstance(row['agent_sha256'], str) and SHA.fullmatch(row['agent_sha256'])) + else: + for key in ('node_name', 'agent_pod_name'): + value = row[key] + require(isinstance(value, str) and len(value) <= 253 and '..' not in value + and re.fullmatch(r'[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?', value)) + for key in ('agent_pod_uid', 'node_uid', 'boot_id'): + require(isinstance(row[key], str) and str(UUID(row[key])) == row[key] and UUID(row[key]).int != 0) + require(re.fullmatch(r'[a-f0-9]{32}', row['machine_id'])) + for key in ('agent_image', 'agent_image_id'): + require(isinstance(row[key], str) and POD_AGENT_IMAGE.fullmatch(row[key])) path = row['image_fs_path'] require(isinstance(path, str) and path.startswith('/') and path != '/' and re.fullmatch(r'/[A-Za-z0-9_./-]+', path) and '..' not in Path(path).parts) @@ -222,6 +243,8 @@ def build_plan(scope, inventory, *, api=sources.publication.github, fetch=None, 'nodes': nodes, 'tasks': tasks, 'held': held, 'excluded': excluded, 'backend_active_independently_queried': False, 'preflight_sha256': hashlib.sha256(Path(__file__).with_name('vm_cache_preflight.py').read_bytes()).hexdigest()} + if any(node.get('agent_transport') == 'kubernetes' for node in nodes): + plan['pod_bridge_sha256'] = hashlib.sha256(Path(__file__).with_name('vm_pod_image_bridge.py').read_bytes()).hexdigest() plan['plan_sha256'] = plan_hash(plan) return plan @@ -272,6 +295,8 @@ def __init__(self, key_file, runner=bounded_command): def __call__(self, node, command, payload): require(command in (AGENT_COMMAND, PREFLIGHT_COMMAND)) + if node.get('agent_transport') == 'kubernetes': + command = POD_AGENT_COMMAND if command == AGENT_COMMAND else POD_PREFLIGHT_COMMAND with tempfile.TemporaryDirectory() as temporary: known = Path(temporary) / 'known_hosts' host = node['ssh_host'] if node['ssh_port'] == 22 else f"[{node['ssh_host']}]:{node['ssh_port']}" @@ -290,9 +315,17 @@ def __call__(self, node, command, payload): def check_host(node, plan, reply, initial_free=None, workspace=0): require(isinstance(reply, dict) and reply.get('schema_version') == 1) - for field in ('node_uid', 'machine_id', 'boot_id', 'architecture', 'image_fs_path', 'agent_sha256'): + for field in ('node_uid', 'machine_id', 'boot_id', 'architecture', 'image_fs_path'): require(reply.get(field) == node[field]) - require(reply.get('os') == 'linux' and reply.get('preflight_sha256') == plan['preflight_sha256']) + require(reply.get('os') == 'linux') + if node.get('agent_transport') == 'kubernetes': + require(reply.get('agent_transport') == 'kubernetes' + and reply.get('preflight_sha256') == plan['pod_bridge_sha256']) + for field in POD_FIELDS: + require(reply.get(field) == node[field]) + else: + require(reply.get('agent_sha256') == node['agent_sha256'] + and reply.get('preflight_sha256') == plan['preflight_sha256']) available, total = reply.get('available_bytes'), reply.get('total_bytes') require(type(available) is int and positive(total) and 0 <= available <= total) reserve = max(node['reserve_bytes'], (total + 4) // 5) diff --git a/scripts/vm_pod_image_bridge.py b/scripts/vm_pod_image_bridge.py new file mode 100644 index 0000000..8590032 --- /dev/null +++ b/scripts/vm_pod_image_bridge.py @@ -0,0 +1,246 @@ +#!/usr/bin/python3 -I +"""Fixed privileged transport to an already installed Runtime image-agent Pod.""" + +import sys +sys.dont_write_bytecode = True + +import hashlib +import json +import os +from pathlib import Path +import platform +import re +import selectors +import signal +import stat +import subprocess +import time +from uuid import UUID + + +CONFIG = Path('/etc/secure-provisioner/vm-image-pod-bridge.json') +K3S = Path('/usr/local/bin/k3s') +KUBECONFIG = Path('/etc/rancher/k3s/k3s.yaml') +NAMESPACE = 'runtime-image-preparation' +LABEL = 'app.kubernetes.io/name=runtime-image-agent' +AGENT_IMAGE = re.compile(r'^ghcr\.io/msg-ctf/secure-provisioner-image-agent@sha256:[a-f0-9]{64}$') +CHALLENGE_IMAGE = re.compile(r'^ghcr\.io/msg-ctf/challenges/[a-z0-9][a-z0-9-]{0,62}/[a-z0-9][a-z0-9-]{0,62}@sha256:[a-f0-9]{64}$') +DNS_NAME = re.compile(r'^[a-z0-9](?:[a-z0-9.-]{0,251}[a-z0-9])?$') +MAX_JSON = 1024 * 1024 +MAX_REQUEST = 4096 +ENVIRONMENT = {'PATH': '/usr/sbin:/usr/bin:/sbin:/bin', 'LANG': 'C.UTF-8', 'HOME': '/root'} + + +def require(condition): + if not condition: + raise ValueError('POD_AGENT_CHECK_FAILED') + + +def unique_object(pairs): + result = {} + for key, value in pairs: + require(key not in result) + result[key] = value + return result + + +def parse_json(raw, limit=MAX_JSON): + require(len(raw) <= limit) + return json.loads(raw, object_pairs_hook=unique_object, parse_constant=lambda _: require(False)) + + +def trusted_path(path, *, directory=False, private=False): + require(not path.is_symlink()) + info = path.stat() + require((stat.S_ISDIR(info.st_mode) if directory else stat.S_ISREG(info.st_mode)) + and info.st_uid == 0 and not info.st_mode & (0o077 if private else 0o022)) + for parent in path.parents: + info = parent.stat() + require(not parent.is_symlink() and info.st_uid == 0 and not info.st_mode & 0o022) + return path + + +def valid_uuid(value): + require(isinstance(value, str)) + parsed = UUID(value) + require(parsed.int != 0 and str(parsed) == value) + + +def load_config(): + require(os.geteuid() == 0) + trusted_path(Path(__file__)) + trusted_path(K3S) + trusted_path(KUBECONFIG, private=True) + path = trusted_path(CONFIG, private=True) + require(path.stat().st_size <= 64 * 1024) + with path.open('rb') as stream: + config = parse_json(stream.read(64 * 1024 + 1), 64 * 1024) + fields = {'schema_version', 'node_name', 'node_uid', 'machine_id', 'boot_id', 'architecture', + 'agent_pod_name', 'agent_pod_uid', 'agent_image', 'agent_image_id', 'image_fs_path'} + require(isinstance(config, dict) and set(config) == fields + and type(config['schema_version']) is int and config['schema_version'] == 1) + for key in ('node_name', 'agent_pod_name'): + require(isinstance(config[key], str) and DNS_NAME.fullmatch(config[key]) and '..' not in config[key]) + for key in ('node_uid', 'boot_id', 'agent_pod_uid'): + valid_uuid(config[key]) + require(isinstance(config['machine_id'], str) and re.fullmatch(r'[a-f0-9]{32}', config['machine_id'])) + require(config['architecture'] in ('AMD64', 'ARM64')) + for key in ('agent_image', 'agent_image_id'): + require(isinstance(config[key], str) and AGENT_IMAGE.fullmatch(config[key])) + raw = config['image_fs_path'] + require(isinstance(raw, str) and raw.startswith('/') and raw != '/' + and re.fullmatch(r'/[A-Za-z0-9_./-]+', raw) and '..' not in Path(raw).parts) + trusted_path(Path(raw), directory=True) + return config + + +def bounded_process(args, payload, timeout, limit): + require(timeout > 0 and len(payload) <= MAX_REQUEST) + process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, start_new_session=True, env=ENVIRONMENT) + selector, output = selectors.DefaultSelector(), bytearray() + deadline = time.monotonic() + timeout + try: + process.stdin.write(payload) + process.stdin.close() + selector.register(process.stdout, selectors.EVENT_READ) + while selector.get_map(): + remaining = deadline - time.monotonic() + require(remaining > 0) + for key, _ in selector.select(min(0.25, remaining)): + data = os.read(key.fd, 8192) + if data: + output.extend(data) + require(len(output) <= limit) + else: + selector.unregister(key.fileobj) + require(process.wait(timeout=max(0.01, deadline - time.monotonic())) == 0) + return parse_json(output, limit) + finally: + selector.close() + if process.poll() is None: + os.killpg(process.pid, signal.SIGKILL) + process.wait() + if not process.stdin.closed: + process.stdin.close() + process.stdout.close() + + +def kubectl(args, payload, deadline, *, agent=False): + timeout = min(110 if agent else 5, deadline - time.monotonic()) + require(timeout > 0) + command = [str(K3S), 'kubectl', '--kubeconfig', str(KUBECONFIG), + '--insecure-skip-tls-verify=false', f'--request-timeout={timeout:.3f}s', *args] + return bounded_process(command, payload, timeout, 64 * 1024 if agent else MAX_JSON) + + +def true_condition(row, kind): + conditions = row.get('status', {}).get('conditions') + require(isinstance(conditions, list)) + matches = [item for item in conditions if isinstance(item, dict) and item.get('type') == kind] + return len(matches) == 1 and matches[0].get('status') == 'True' + + +def inspect(config, deadline, run=kubectl): + node = run(['get', 'node', config['node_name'], '-o', 'json'], b'', deadline) + require(isinstance(node, dict)) + metadata, status = node.get('metadata', {}), node.get('status', {}) + info = status.get('nodeInfo', {}) + require(metadata.get('name') == config['node_name'] and metadata.get('uid') == config['node_uid'] + and not metadata.get('deletionTimestamp') and true_condition(node, 'Ready')) + for key, field in (('machineID', 'machine_id'), ('bootID', 'boot_id')): + require(info.get(key) == config[field]) + require(info.get('operatingSystem') == 'linux' and isinstance(info.get('architecture'), str) + and info['architecture'].upper() == config['architecture']) + require(Path('/etc/machine-id').read_text().strip() == config['machine_id'] + and Path('/proc/sys/kernel/random/boot_id').read_text().strip() == config['boot_id']) + arch = {'x86_64': 'AMD64', 'aarch64': 'ARM64'}.get(platform.machine()) + require(platform.system() == 'Linux' and arch == config['architecture']) + listed = run(['-n', NAMESPACE, 'get', 'pods', '-l', LABEL, + '--field-selector', 'spec.nodeName=' + config['node_name'], '-o', 'json'], b'', deadline) + require(isinstance(listed, dict) and isinstance(listed.get('items'), list) and len(listed['items']) <= 100) + ready = [] + for pod in listed['items']: + require(isinstance(pod, dict)) + if (pod.get('status', {}).get('phase') == 'Running' + and not pod.get('metadata', {}).get('deletionTimestamp') and true_condition(pod, 'Ready')): + ready.append(pod) + require(len(ready) == 1) + pod = ready[0] + metadata, spec, status = pod.get('metadata', {}), pod.get('spec', {}), pod.get('status', {}) + require(metadata.get('namespace') == NAMESPACE and metadata.get('name') == config['agent_pod_name'] + and metadata.get('uid') == config['agent_pod_uid'] + and metadata.get('labels', {}).get('app.kubernetes.io/name') == 'runtime-image-agent' + and spec.get('nodeName') == config['node_name']) + containers = spec.get('containers') + require(isinstance(containers, list) and len(containers) == 1 + and containers[0].get('name') == 'agent' and containers[0].get('image') == config['agent_image']) + statuses = status.get('containerStatuses') + require(isinstance(statuses, list) and len(statuses) == 1) + actual = statuses[0] + image_id = actual.get('imageID') + if isinstance(image_id, str) and image_id.startswith('docker-pullable://'): + image_id = image_id[len('docker-pullable://'):] + require(actual.get('name') == 'agent' and actual.get('ready') is True + and isinstance(actual.get('state', {}).get('running'), dict) + and image_id == config['agent_image_id']) + fs = Path(config['image_fs_path']) + trusted_path(fs, directory=True) + disk = os.statvfs(fs) + return {key: config[key] for key in ('node_name', 'node_uid', 'machine_id', 'boot_id', 'architecture', + 'agent_pod_name', 'agent_pod_uid', 'agent_image', 'agent_image_id', 'image_fs_path')} | { + 'schema_version': 1, 'agent_transport': 'kubernetes', 'node_uid_source': 'LIVE_KUBERNETES_AND_HOST_CHECK', + 'os': 'linux', 'available_bytes': disk.f_bavail * disk.f_frsize, + 'total_bytes': disk.f_blocks * disk.f_frsize, + 'preflight_sha256': hashlib.sha256(Path(__file__).read_bytes()).hexdigest()} + + +def handle(action, request, config, *, run=kubectl, check=inspect, deadline=None): + deadline = time.monotonic() + 115 if deadline is None else deadline + require(action in ('preflight', 'prepare') and isinstance(request, dict)) + if action == 'preflight': + require(not request) + return check(config, deadline, run) + require(set(request) == {'image', 'architecture', 'pull'} + and isinstance(request['image'], str) and CHALLENGE_IMAGE.fullmatch(request['image']) + and request['architecture'] == config['architecture'] and type(request['pull']) is bool) + before = check(config, deadline, run) + reply = run(['-n', NAMESPACE, 'exec', '-i', config['agent_pod_name'], '-c', 'agent', '--', + '/usr/local/bin/image-agent', '--kubernetes'], + json.dumps(request, separators=(',', ':')).encode(), deadline, agent=True) + after = check(config, deadline, run) + fields = ('node_uid', 'machine_id', 'boot_id', 'node_name', 'agent_pod_name', 'agent_pod_uid', + 'agent_image', 'agent_image_id', 'preflight_sha256') + require(all(before[key] == after[key] for key in fields)) + require(isinstance(reply, dict)) + if set(reply) == {'error_code'}: + code = reply['error_code'] + return {'error_code': code if code in ('CONTENT_INCOMPLETE', 'PLATFORM_MISMATCH') else 'AGENT_UNAVAILABLE'} + require(set(reply) == {'evidence'} and isinstance(reply['evidence'], dict)) + evidence = reply['evidence'] + require(set(evidence) == {'node_uid', 'machine_id', 'boot_id', 'os', 'architecture', 'image', + 'resolved_manifest_digest', 'complete'}) + require(all(evidence.get(key) == config[key] for key in ('node_uid', 'machine_id', 'boot_id', 'architecture')) + and evidence.get('image') == request['image'] and evidence.get('os') == 'linux' + and evidence.get('complete') is True + and isinstance(evidence.get('resolved_manifest_digest'), str) + and re.fullmatch(r'sha256:[a-f0-9]{64}', evidence['resolved_manifest_digest'])) + return {'evidence': evidence} + + +def main(): + try: + require(len(sys.argv) == 2 and sys.argv[1] in ('preflight', 'prepare')) + config = load_config() + request = parse_json(sys.stdin.buffer.read(MAX_REQUEST + 1), MAX_REQUEST) + reply = handle(sys.argv[1], request, config) + except (OSError, ValueError, TypeError, KeyError, AttributeError, OverflowError, + RecursionError, subprocess.SubprocessError): + print(json.dumps({'error_code': 'POD_AGENT_CHECK_FAILED'})) + return 1 + print(json.dumps(reply, separators=(',', ':'), allow_nan=False)) + return 0 + + +if __name__ == '__main__': + raise SystemExit(main()) diff --git a/tests/README.md b/tests/README.md index 5edd259..ba8ca74 100644 --- a/tests/README.md +++ b/tests/README.md @@ -12,6 +12,13 @@ 기존 Runtime agent 요청/결과 형식과 캐시 재사용, 실제 Bash main/event 가드도 검사합니다. 실제 VM 접속·pull·Pod·운영 전환을 실행하는 테스트는 아닙니다. +### `test_vm_pod_image_bridge.py` + +Pod형/호스트형 선택과 고정 SSH 명령, live Node·host identity·Ready·Pod UID·실행 +imageID, 변경·중복·변조·오류 비포함·입출력/시간 제한과 공통 최종 캐시 검사를 +검증합니다. subprocess 시험은 로컬 Python fixture만 사용하고 실제 Kubernetes· +SSH·GHCR·Pod 변경을 실행하지 않습니다. + ### `test_audit_node_image_readiness.py` Runtime 공식 예제의 canonical image_set_id, 보조 이미지 포함·대상/architecture diff --git a/tests/test_vm_pod_image_bridge.py b/tests/test_vm_pod_image_bridge.py new file mode 100644 index 0000000..723184d --- /dev/null +++ b/tests/test_vm_pod_image_bridge.py @@ -0,0 +1,407 @@ +import copy +import io +import json +import os +from pathlib import Path +import stat +import subprocess +import sys +import tempfile +from types import SimpleNamespace +import unittest +from unittest.mock import patch + +from scripts import vm_image_prefetch as vm +from scripts import vm_pod_image_bridge as bridge +from tests.test_vm_image_prefetch import build, evidence, inputs, NOW + + +IMAGE = 'ghcr.io/msg-ctf/challenges/web-one/web@sha256:' + 'b' * 64 +AGENT = 'ghcr.io/msg-ctf/secure-provisioner-image-agent@sha256:' + 'a' * 64 +NODE_UID = '00000000-0000-4000-8000-000000000001' +POD_UID = '00000000-0000-4000-8000-000000000002' +BOOT_ID = '00000000-0000-4000-8000-000000000003' + + +def config(): + return {'schema_version': 1, 'node_name': 'node-one.example.internal', 'node_uid': NODE_UID, + 'machine_id': 'd' * 32, 'boot_id': BOOT_ID, 'architecture': 'AMD64', + 'agent_pod_name': 'runtime-image-agent-one', 'agent_pod_uid': POD_UID, + 'agent_image': AGENT, 'agent_image_id': AGENT, + 'image_fs_path': '/var/lib/rancher/k3s/agent/containerd'} + + +def objects(cfg=None): + cfg = cfg or config() + node = {'metadata': {'name': cfg['node_name'], 'uid': cfg['node_uid']}, + 'status': {'nodeInfo': {'machineID': cfg['machine_id'], 'bootID': cfg['boot_id'], + 'operatingSystem': 'linux', 'architecture': 'amd64'}, + 'conditions': [{'type': 'Ready', 'status': 'True'}]}} + pod = {'metadata': {'name': cfg['agent_pod_name'], 'uid': cfg['agent_pod_uid'], + 'namespace': bridge.NAMESPACE, + 'labels': {'app.kubernetes.io/name': 'runtime-image-agent'}}, + 'spec': {'nodeName': cfg['node_name'], 'containers': [{'name': 'agent', 'image': cfg['agent_image']}]}, + 'status': {'phase': 'Running', 'conditions': [{'type': 'Ready', 'status': 'True'}], + 'containerStatuses': [{'name': 'agent', 'imageID': cfg['agent_image_id'], + 'ready': True, 'state': {'running': {}}}]}} + return node, pod + + +def request(pull=False): + return {'image': IMAGE, 'architecture': 'AMD64', 'pull': pull} + + +def agent_response(cfg=None): + cfg = cfg or config() + return {'evidence': {**{key: cfg[key] for key in ('node_uid', 'machine_id', 'boot_id', 'architecture')}, + 'os': 'linux', 'image': IMAGE, 'resolved_manifest_digest': 'sha256:' + 'b' * 64, + 'complete': True}} + + +class FakeKube: + def __init__(self, cfg=None, change=None, reply=None): + self.cfg = cfg or config() + self.calls, self.change = [], change + self.reply = reply if reply is not None else agent_response(self.cfg) + + def __call__(self, args, payload, deadline, *, agent=False): + self.calls.append((list(args), payload, agent)) + node, pod = objects(self.cfg) + if agent: + result = copy.deepcopy(self.reply) + elif args[:2] == ['get', 'node']: + result = node + else: + result = {'items': [pod]} + return self.change(args, result, agent, len(self.calls)) if self.change else result + + +class HostPatches: + def __enter__(self): + self.patchers = [ + patch.object(bridge, 'trusted_path', side_effect=lambda path, **kwargs: path), + patch.object(Path, 'read_text', lambda path: 'd' * 32 if str(path) == '/etc/machine-id' else BOOT_ID), + patch.object(bridge.platform, 'system', return_value='Linux'), + patch.object(bridge.platform, 'machine', return_value='x86_64'), + patch.object(bridge.os, 'statvfs', return_value=SimpleNamespace(f_bavail=10000, f_frsize=1, f_blocks=20000)), + ] + for item in self.patchers: + item.start() + return self + + def __exit__(self, *args): + for item in reversed(self.patchers): + item.stop() + + +class PodBridgeTests(unittest.TestCase): + def test_preflight_reads_live_node_and_pod_only(self): + kube = FakeKube() + with HostPatches(): + result = bridge.handle('preflight', {}, config(), run=kube) + self.assertEqual(result['node_uid_source'], 'LIVE_KUBERNETES_AND_HOST_CHECK') + self.assertEqual(result['node_uid'], NODE_UID) + self.assertEqual(result['agent_pod_uid'], POD_UID) + self.assertEqual(result['agent_image_id'], AGENT) + self.assertEqual(result['available_bytes'], 10000) + self.assertEqual(len(kube.calls), 2) + self.assertFalse(any(agent for _, _, agent in kube.calls)) + + def test_prepare_reuses_exact_runtime_exec_command_and_json(self): + for pull in (False, True): + kube = FakeKube() + with self.subTest(pull=pull), HostPatches(): + result = bridge.handle('prepare', request(pull), config(), run=kube) + self.assertEqual(result, agent_response()) + self.assertEqual(len(kube.calls), 5) + args, payload, agent = kube.calls[2] + self.assertTrue(agent) + self.assertEqual(args, ['-n', bridge.NAMESPACE, 'exec', '-i', 'runtime-image-agent-one', + '-c', 'agent', '--', '/usr/local/bin/image-agent', '--kubernetes']) + self.assertEqual(json.loads(payload), request(pull)) + self.assertFalse(any('apply' in args or 'delete' in args or 'crictl' in args for args, _, _ in kube.calls)) + + def test_node_uid_machine_boot_arch_and_ready_mismatch_stop_before_exec(self): + changes = [('uid', 'different'), ('machineID', 'wrong'), ('bootID', 'wrong'), + ('architecture', 'arm64'), ('operatingSystem', 'windows'), ('ready', 'False')] + for field, value in changes: + def change(args, row, agent, count): + if args[:2] == ['get', 'node']: + if field == 'uid': + row['metadata']['uid'] = value + elif field == 'ready': + row['status']['conditions'][0]['status'] = value + else: + row['status']['nodeInfo'][field] = value + return row + kube = FakeKube(change=change) + with self.subTest(field=field), HostPatches(), self.assertRaises(ValueError): + bridge.handle('prepare', request(True), config(), run=kube) + self.assertFalse(any(agent for _, _, agent in kube.calls)) + + def test_host_identity_or_platform_mismatch_cannot_be_ignored(self): + for target, value in (('system', 'Darwin'), ('machine', 'aarch64')): + kube = FakeKube() + with HostPatches(), patch.object(bridge.platform, target, return_value=value), self.assertRaises(ValueError): + bridge.handle('prepare', request(True), config(), run=kube) + self.assertFalse(any(agent for _, _, agent in kube.calls)) + with HostPatches(), patch.object(Path, 'read_text', return_value='wrong'), self.assertRaises(ValueError): + bridge.handle('preflight', {}, config(), run=FakeKube()) + + def test_pod_uid_node_image_status_and_ready_are_pinned(self): + for field in ('uid', 'name', 'node', 'image', 'image_id', 'container_ready', 'waiting', 'deleting', 'namespace'): + def change(args, row, agent, count): + if 'items' in row: + pod = row['items'][0] + if field in ('uid', 'name', 'namespace'): + pod['metadata'][field] = 'unexpected' + elif field == 'node': + pod['spec']['nodeName'] = 'another-node' + elif field == 'image': + pod['spec']['containers'][0]['image'] = AGENT.replace('a' * 64, 'c' * 64) + elif field == 'image_id': + pod['status']['containerStatuses'][0]['imageID'] = 'containerd://sha256:' + 'a' * 64 + elif field == 'container_ready': + pod['status']['containerStatuses'][0]['ready'] = False + elif field == 'waiting': + pod['status']['containerStatuses'][0]['state'] = {'waiting': {}} + else: + pod['metadata']['deletionTimestamp'] = '2026-11-05T00:00:00Z' + return row + kube = FakeKube(change=change) + with self.subTest(field=field), HostPatches(), self.assertRaises(ValueError): + bridge.handle('prepare', request(True), config(), run=kube) + self.assertFalse(any(agent for _, _, agent in kube.calls)) + + def test_duplicate_ready_agents_and_extra_containers_are_rejected(self): + for mode in ('duplicate', 'sidecar', 'conditions'): + def change(args, row, agent, count): + if 'items' in row: + if mode == 'duplicate': + row['items'].append(copy.deepcopy(row['items'][0])) + elif mode == 'sidecar': + row['items'][0]['spec']['containers'].append({'name': 'sidecar'}) + else: + row['items'][0]['status']['conditions'] *= 2 + return row + with self.subTest(mode=mode), HostPatches(), self.assertRaises(ValueError): + bridge.handle('preflight', {}, config(), run=FakeKube(change=change)) + + def test_approved_index_and_resolved_image_id_are_separate(self): + cfg = config() + cfg['agent_image_id'] = AGENT.replace('a' * 64, 'c' * 64) + with HostPatches(): + result = bridge.handle('preflight', {}, cfg, run=FakeKube(cfg)) + self.assertEqual(result['agent_image'], AGENT) + self.assertNotEqual(result['agent_image_id'], AGENT) + + def test_docker_pullable_prefix_is_normalized_without_guessing_config_ids(self): + def change(args, row, agent, count): + if 'items' in row: + row['items'][0]['status']['containerStatuses'][0]['imageID'] = 'docker-pullable://' + AGENT + return row + with HostPatches(): + result = bridge.handle('preflight', {}, config(), run=FakeKube(change=change)) + self.assertEqual(result['agent_image_id'], AGENT) + + def test_identity_drift_after_exec_is_failure_not_success(self): + def change(args, row, agent, count): + if count > 3 and 'items' in row: + row['items'][0]['metadata']['uid'] = 'recreated-pod' + return row + kube = FakeKube(change=change) + with HostPatches(), self.assertRaises(ValueError): + bridge.handle('prepare', request(True), config(), run=kube) + self.assertEqual(sum(agent for _, _, agent in kube.calls), 1) + + def test_agent_incomplete_is_preserved_and_unknown_errors_are_redacted(self): + for code, expected in (('CONTENT_INCOMPLETE', 'CONTENT_INCOMPLETE'), + ('PLATFORM_MISMATCH', 'PLATFORM_MISMATCH'), + ('private-token-from-error', 'AGENT_UNAVAILABLE')): + with self.subTest(code=code), HostPatches(): + result = bridge.handle('prepare', request(), config(), run=FakeKube(reply={'error_code': code})) + self.assertEqual(result, {'error_code': expected}) + + def test_false_wrong_identity_image_and_malformed_digest_are_rejected(self): + for change in ({'complete': False}, {'node_uid': 'wrong'}, {'machine_id': 'wrong'}, + {'image': 'wrong'}, {'architecture': 'ARM64'}, {'resolved_manifest_digest': 'bad'}): + reply = agent_response() + reply['evidence'].update(change) + with self.subTest(change=change), HostPatches(), self.assertRaises(ValueError): + bridge.handle('prepare', request(), config(), run=FakeKube(reply=reply)) + + def test_unexpected_agent_payload_is_not_returned(self): + for reply in ({'evidence': agent_response()['evidence'], 'body': 'private-value'}, + {'error_code': 'CONTENT_INCOMPLETE', 'evidence': agent_response()['evidence']}): + with HostPatches(), self.assertRaises(ValueError): + bridge.handle('prepare', request(), config(), run=FakeKube(reply=reply)) + + def test_requests_cannot_override_command_target_namespace_or_credentials(self): + cases = [dict(request(), pull='true'), dict(request(), image=IMAGE + '\ncommand'), + dict(request(), image='ghcr.io/other/project/web@sha256:' + 'a' * 64), + dict(request(), architecture='ARM64'), dict(request(), namespace='kube-system'), + dict(request(), token='private-token')] + for payload in cases: + kube = FakeKube() + with self.subTest(payload=payload), self.assertRaises(ValueError): + bridge.handle('prepare', payload, config(), run=kube) + self.assertEqual(kube.calls, []) + with self.assertRaises(ValueError): + bridge.handle('preflight', {'pull': True}, config(), run=FakeKube()) + + def test_config_is_exact_and_does_not_accept_commands_or_alternate_images(self): + for change in ({'schema_version': True}, {'k3s_binary': '/tmp/user-command'}, + {'agent_image': 'ghcr.io/other/project@sha256:' + 'a' * 64}, + {'node_name': 'node;command'}, {'agent_pod_uid': 'not-a-uuid'}, + {'machine_id': 'wrong'}, {'image_fs_path': '/var/../tmp'}): + cfg = config() + cfg.update(change) + with tempfile.TemporaryDirectory() as temporary: + path = Path(temporary) / 'config.json' + path.write_text(json.dumps(cfg)) + with self.subTest(change=change), patch.object(bridge, 'CONFIG', path), \ + patch.object(bridge.os, 'geteuid', return_value=0), \ + patch.object(bridge, 'trusted_path', side_effect=lambda value, **kwargs: value), \ + self.assertRaises(ValueError): + bridge.load_config() + + def test_readonly_config_and_private_root_ownership_are_required(self): + with patch.object(bridge.os, 'geteuid', return_value=1), self.assertRaises(ValueError): + bridge.load_config() + path = SimpleNamespace(is_symlink=lambda: False, + stat=lambda: SimpleNamespace(st_mode=stat.S_IFREG | 0o644, st_uid=0), parents=[]) + with self.assertRaises(ValueError): + bridge.trusted_path(path, private=True) + path.stat = lambda: SimpleNamespace(st_mode=stat.S_IFREG | 0o600, st_uid=1000) + with self.assertRaises(ValueError): + bridge.trusted_path(path) + + def test_json_duplicate_nonfinite_and_oversized_input_are_rejected(self): + for raw in (b'{"pull":false,"pull":true}', b'{"x":NaN}', b'x' * (bridge.MAX_REQUEST + 1)): + with self.subTest(raw=raw[:30]), self.assertRaises(ValueError): + bridge.parse_json(raw, bridge.MAX_REQUEST) + + def test_kubectl_uses_fixed_kubeconfig_tls_and_limits(self): + with patch.object(bridge, 'bounded_process', return_value={}) as run: + bridge.kubectl(['get', 'node', 'node-one', '-o', 'json'], b'', bridge.time.monotonic() + 10) + args, payload, timeout, limit = run.call_args.args + self.assertEqual(args[:4], ['/usr/local/bin/k3s', 'kubectl', '--kubeconfig', '/etc/rancher/k3s/k3s.yaml']) + self.assertIn('--insecure-skip-tls-verify=false', args) + self.assertTrue(any(value.startswith('--request-timeout=') for value in args)) + self.assertLessEqual(timeout, 5) + self.assertEqual(limit, bridge.MAX_JSON) + + def test_child_environment_excludes_auth_proxy_and_python_injection(self): + with patch.dict(os.environ, {'GH_TOKEN': 'private-token', 'HTTPS_PROXY': 'private-proxy', + 'PYTHONPATH': '/user/path', 'KUBECONFIG': '/user/config'}): + result = bridge.bounded_process([sys.executable, '-c', + 'import os,sys,json;sys.stdin.read();print(json.dumps({"clean": all(k not in os.environ for k in ("GH_TOKEN","HTTPS_PROXY","PYTHONPATH","KUBECONFIG"))}))'], b'{}', 3, 1000) + self.assertTrue(result['clean']) + + def test_child_output_and_time_are_bounded(self): + for code, timeout, limit in (('import sys;sys.stdin.read();print("x"*2000)', 3, 1000), + ('import time;time.sleep(2)', 0.03, 1000)): + with self.subTest(code=code), self.assertRaises((ValueError, subprocess.SubprocessError)): + bridge.bounded_process([sys.executable, '-c', code], b'{}', timeout, limit) + + def test_cli_errors_do_not_print_private_configuration(self): + with patch('sys.argv', ['bridge', 'preflight']), \ + patch.object(bridge, 'load_config', side_effect=ValueError('private-config-value')), \ + patch('sys.stdout', new_callable=io.StringIO) as output: + code = bridge.main() + self.assertEqual(code, 1) + self.assertEqual(json.loads(output.getvalue()), {'error_code': 'POD_AGENT_CHECK_FAILED'}) + self.assertNotIn('private-config-value', output.getvalue()) + + +def pod_inventory(): + scope, nodes = inputs() + cfg = config() + node = nodes['nodes'][0] + del node['agent_sha256'] + node.update(agent_transport='kubernetes', **{key: cfg[key] for key in ( + 'node_name', 'node_uid', 'machine_id', 'boot_id', 'agent_pod_name', 'agent_pod_uid', + 'agent_image', 'agent_image_id')}) + return scope, nodes + + +def bridge_reply(node, plan): + return {**{key: node[key] for key in vm.POD_FIELDS | { + 'node_uid', 'machine_id', 'boot_id', 'architecture', 'image_fs_path'}}, + 'schema_version': 1, 'os': 'linux', 'agent_transport': 'kubernetes', + 'available_bytes': 10000, 'total_bytes': 20000, + 'preflight_sha256': plan['pod_bridge_sha256']} + + +class PodTransportTests(unittest.TestCase): + def test_plan_pins_pod_identity_image_and_bridge_without_host_agent_hash(self): + scope, nodes = pod_inventory() + plan = build(scope, nodes) + self.assertTrue(plan['complete']) + self.assertIn('pod_bridge_sha256', plan) + self.assertNotIn('agent_sha256', plan['nodes'][0]) + self.assertEqual(plan['nodes'][0]['agent_pod_uid'], POD_UID) + + def test_fixed_ssh_commands_use_bridge_without_raw_kubectl_or_install(self): + scope, nodes = pod_inventory() + with tempfile.TemporaryDirectory() as temporary: + key = Path(temporary) / 'key' + key.write_text('private-fixture') + key.chmod(0o600) + called = [] + agent = vm.SSHAgent(key, runner=lambda args, payload: called.append(args[-1]) or {}) + agent(nodes['nodes'][0], vm.PREFLIGHT_COMMAND, {}) + agent(nodes['nodes'][0], vm.AGENT_COMMAND, request()) + self.assertEqual(called, [vm.POD_PREFLIGHT_COMMAND, vm.POD_AGENT_COMMAND]) + self.assertFalse(any('kubectl' in value or 'install' in value for value in called)) + + def test_bad_transport_partial_pins_or_tagged_agent_are_rejected(self): + for change in ({'agent_transport': 'auto'}, {'agent_image': AGENT.split('@')[0] + ':latest'}, + {'agent_pod_uid': 'wrong'}, {'node_name': 'node;command'}): + scope, nodes = pod_inventory() + nodes['nodes'][0].update(change) + with self.subTest(change=change), self.assertRaises(ValueError): + build(scope, nodes) + scope, nodes = pod_inventory() + del nodes['nodes'][0]['agent_image_id'] + with self.assertRaises(ValueError): + build(scope, nodes) + + def test_pod_or_bridge_drift_is_failure_before_any_pull(self): + for field, value in (('agent_pod_uid', 'wrong'), ('agent_image', AGENT.replace('a'*64, 'b'*64)), + ('preflight_sha256', 'e'*64)): + scope, nodes = pod_inventory() + plan, calls = build(scope, nodes), [] + def call(node, command, payload): + calls.append((command, payload)) + result = bridge_reply(node, plan) + result[field] = value + return result + with self.subTest(field=field): + report = vm.execute(plan, plan['plan_sha256'], call, now=NOW) + self.assertEqual(report['status'], 'FAILED') + self.assertFalse(any(command == vm.AGENT_COMMAND for command, _ in calls)) + + def test_pod_flow_keeps_shared_final_sweep_and_no_workload_activation(self): + scope, nodes = pod_inventory() + plan = build(scope, nodes) + def call(node, command, payload): + return bridge_reply(node, plan) if command == vm.PREFLIGHT_COMMAND else evidence(node, payload['image']) + report = vm.execute(plan, plan['plan_sha256'], call, now=NOW) + self.assertEqual(report['status'], 'SUCCEEDED') + self.assertTrue(report['final_sweep_complete']) + self.assertFalse(report['active_changed']) + self.assertFalse(report['pod_started']) + + def test_host_inventory_remains_unchanged_and_no_automatic_fallback(self): + original = build() + self.assertNotIn('pod_bridge_sha256', original) + self.assertNotIn('agent_transport', original['nodes'][0]) + scope, nodes = pod_inventory() + nodes['nodes'][0]['agent_sha256'] = 'd'*64 + with self.assertRaises(ValueError): + build(scope, nodes) + + +if __name__ == '__main__': + unittest.main() From 05b3752a53b7d36d2f5f67ac0a2ba64f7cb1854a Mon Sep 17 00:00:00 2001 From: wlsdnd2367 <230291092+wlsdnd2367@users.noreply.github.com> Date: Thu, 5 Nov 2026 13:02:25 +0900 Subject: [PATCH 2/3] Cover mixed transports and Pod generation approval --- docs/vm-pod-image-agent.md | 10 +++++++++ tests/test_vm_pod_image_bridge.py | 34 ++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/docs/vm-pod-image-agent.md b/docs/vm-pod-image-agent.md index b15f7f9..a328d13 100644 --- a/docs/vm-pod-image-agent.md +++ b/docs/vm-pod-image-agent.md @@ -95,6 +95,16 @@ release·전체 digest·승인 target·예산을 확정하고 기존 `scripts.vm plan/approved SHA/apply 순서를 따릅니다. 이 어댑터 때문에 기존 출처·승인·만료·디스크· 단일 순차 실행·최종 재검사 조건을 제거하지 않습니다. 호스트형으로 자동 fallback하지 않습니다. +리뷰한 파일과 설정의 설치 후, 다운로드 없는 초기 확인 명령은 다음과 같습니다. + +```bash +printf '%s' '{}' | sudo -n /usr/local/libexec/vm-image-pod-bridge preflight +``` + +첫 정비는 GitHub에 SSH 키를 등록하지 않고 맥의 기존 CLI로 실행할 수도 있습니다. +로컬 SSH 키 경로와 비공개 JSON을 사용하며 원래 plan 승인·identity·예산 검사는 같습니다. +GitHub Actions로 옮길 때만 별도 Environment·Secret·암호화 결과 설정을 준비합니다. + 브리지의 매 호출은 live Node/Ready와 host machine/boot/architecture를 대조합니다. 해당 노드·label에서 Ready agent가 정확히 하나여야 하며 이름·UID·이미지·실제 imageID· 컨테이너 Running/Ready가 고정값과 일치해야 합니다. prepare 전후에 같은 검사를 합니다. diff --git a/tests/test_vm_pod_image_bridge.py b/tests/test_vm_pod_image_bridge.py index 723184d..df4c841 100644 --- a/tests/test_vm_pod_image_bridge.py +++ b/tests/test_vm_pod_image_bridge.py @@ -13,7 +13,7 @@ from scripts import vm_image_prefetch as vm from scripts import vm_pod_image_bridge as bridge -from tests.test_vm_image_prefetch import build, evidence, inputs, NOW +from tests.test_vm_image_prefetch import build, evidence, host_reply, inputs, NOW IMAGE = 'ghcr.io/msg-ctf/challenges/web-one/web@sha256:' + 'b' * 64 @@ -291,6 +291,11 @@ def test_kubectl_uses_fixed_kubeconfig_tls_and_limits(self): self.assertLessEqual(timeout, 5) self.assertEqual(limit, bridge.MAX_JSON) + def test_expired_bridge_budget_does_not_start_kubectl(self): + with patch.object(bridge, 'bounded_process') as run, self.assertRaises(ValueError): + bridge.kubectl(['get', 'node', 'node-one'], b'', bridge.time.monotonic() - 1) + run.assert_not_called() + def test_child_environment_excludes_auth_proxy_and_python_injection(self): with patch.dict(os.environ, {'GH_TOKEN': 'private-token', 'HTTPS_PROXY': 'private-proxy', 'PYTHONPATH': '/user/path', 'KUBECONFIG': '/user/config'}): @@ -341,6 +346,7 @@ def test_plan_pins_pod_identity_image_and_bridge_without_host_agent_hash(self): self.assertIn('pod_bridge_sha256', plan) self.assertNotIn('agent_sha256', plan['nodes'][0]) self.assertEqual(plan['nodes'][0]['agent_pod_uid'], POD_UID) + self.assertNotIn(AGENT, plan['tasks'][0]['images']) def test_fixed_ssh_commands_use_bridge_without_raw_kubectl_or_install(self): scope, nodes = pod_inventory() @@ -402,6 +408,32 @@ def test_host_inventory_remains_unchanged_and_no_automatic_fallback(self): with self.assertRaises(ValueError): build(scope, nodes) + def test_mixed_host_and_pod_inventory_uses_each_approved_evidence(self): + scope, nodes = pod_inventory() + host = inputs()[1]['nodes'][0] + host.update(node_id='vm-two', target_id='target-two', node_uid='host-uid', + machine_id='host-machine', ssh_host='192.0.2.11') + nodes['nodes'].append(host) + scope['releases'][0]['approved_target_ids'].append('target-two') + plan = build(scope, nodes) + def call(node, command, payload): + if command == vm.PREFLIGHT_COMMAND: + return bridge_reply(node, plan) if node.get('agent_transport') == 'kubernetes' else host_reply(node, plan) + return evidence(node, payload['image']) + result = vm.execute(plan, plan['plan_sha256'], call, now=NOW) + self.assertEqual(result['status'], 'SUCCEEDED') + self.assertEqual(len(result['results']), 2) + self.assertTrue(result['final_sweep_complete']) + + def test_pod_recreation_requires_new_plan_approval(self): + scope, nodes = pod_inventory() + old = build(scope, nodes) + nodes['nodes'][0]['agent_pod_uid'] = '00000000-0000-4000-8000-000000000099' + new = build(scope, nodes) + self.assertNotEqual(old['plan_sha256'], new['plan_sha256']) + with self.assertRaises(ValueError): + vm.execute(new, old['plan_sha256'], lambda *_: self.fail('must not start SSH'), now=NOW) + if __name__ == '__main__': unittest.main() From 5d0657c167a046814c0471faeee4b9bb9d0eea24 Mon Sep 17 00:00:00 2001 From: ialleejy Date: Thu, 5 Nov 2026 16:56:59 +0900 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20=EC=9E=85=EB=A0=A5=20=EB=8C=80?= =?UTF-8?q?=EA=B8=B0=EB=A5=BC=20=ED=8F=AC=ED=95=A8=ED=95=9C=20Pod=20?= =?UTF-8?q?=EB=B8=8C=EB=A6=AC=EC=A7=80=20=EC=A0=84=EC=B2=B4=20=EC=A0=9C?= =?UTF-8?q?=ED=95=9C=EC=8B=9C=EA=B0=84=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/vm-pod-image-agent.md | 4 +- scripts/vm_pod_image_bridge.py | 30 ++++++++- tests/test_vm_pod_image_bridge.py | 107 ++++++++++++++++++++++++++++++ 3 files changed, 137 insertions(+), 4 deletions(-) diff --git a/docs/vm-pod-image-agent.md b/docs/vm-pod-image-agent.md index a328d13..59ae7cb 100644 --- a/docs/vm-pod-image-agent.md +++ b/docs/vm-pod-image-agent.md @@ -114,7 +114,9 @@ Kubernetes exec에는 Pod UID 원자적 precondition이 없으므로 전후 검 호스트 subprocess는 고정 kubeconfig와 TLS 검증을 사용하고 shell·proxy·GitHub/Python 인증 환경을 상속하지 않습니다. Node/Pod JSON은 최대1MiB, agent 출력은64KiB, 입력은4KiB, -브리지 호출 전체는115초 예산이며 API 개별 조회는 최대5초입니다. 시간이 부족하면 전체 +브리지 호출은 시작부터 설정 검사·입력 대기·전후 검사를 모두 포함해115초 예산을 사용하며, +입력 후 제한 시간을 다시 시작하지 않습니다. 입력이 끝나지 않아도 같은 기한에 중단합니다. +API 개별 조회는 최대5초입니다. 시간이 부족하면 전체 검사를 생략하지 않고 중단합니다. 오류 본문·stderr·kubeconfig는 결과에 복사하지 않습니다. 디스크 예산과 실행 대상 승인은 기존 중앙 파이프라인에서 검수합니다. 브리지 자체는 diff --git a/scripts/vm_pod_image_bridge.py b/scripts/vm_pod_image_bridge.py index 8590032..88a097c 100644 --- a/scripts/vm_pod_image_bridge.py +++ b/scripts/vm_pod_image_bridge.py @@ -28,6 +28,7 @@ DNS_NAME = re.compile(r'^[a-z0-9](?:[a-z0-9.-]{0,251}[a-z0-9])?$') MAX_JSON = 1024 * 1024 MAX_REQUEST = 4096 +CALL_TIMEOUT_SECONDS = 115 ENVIRONMENT = {'PATH': '/usr/sbin:/usr/bin:/sbin:/bin', 'LANG': 'C.UTF-8', 'HOME': '/root'} @@ -49,6 +50,27 @@ def parse_json(raw, limit=MAX_JSON): return json.loads(raw, object_pairs_hook=unique_object, parse_constant=lambda _: require(False)) +def read_request(stream, deadline): + output = bytearray() + descriptor = stream.fileno() + # poll supports both SSH pipes and regular-file redirection on Linux. + with selectors.PollSelector() as selector: + selector.register(descriptor, selectors.EVENT_READ) + while True: + remaining = deadline - time.monotonic() + require(remaining > 0) + if not selector.select(remaining): + continue + try: + data = os.read(descriptor, MAX_REQUEST + 1 - len(output)) + except BlockingIOError: + continue + if not data: + return parse_json(output, MAX_REQUEST) + output.extend(data) + require(len(output) <= MAX_REQUEST) + + def trusted_path(path, *, directory=False, private=False): require(not path.is_symlink()) info = path.stat() @@ -196,7 +218,7 @@ def inspect(config, deadline, run=kubectl): def handle(action, request, config, *, run=kubectl, check=inspect, deadline=None): - deadline = time.monotonic() + 115 if deadline is None else deadline + deadline = time.monotonic() + CALL_TIMEOUT_SECONDS if deadline is None else deadline require(action in ('preflight', 'prepare') and isinstance(request, dict)) if action == 'preflight': require(not request) @@ -229,11 +251,13 @@ def handle(action, request, config, *, run=kubectl, check=inspect, deadline=None def main(): + deadline = time.monotonic() + CALL_TIMEOUT_SECONDS try: require(len(sys.argv) == 2 and sys.argv[1] in ('preflight', 'prepare')) config = load_config() - request = parse_json(sys.stdin.buffer.read(MAX_REQUEST + 1), MAX_REQUEST) - reply = handle(sys.argv[1], request, config) + request = read_request(sys.stdin.buffer, deadline) + reply = handle(sys.argv[1], request, config, deadline=deadline) + require(time.monotonic() < deadline) except (OSError, ValueError, TypeError, KeyError, AttributeError, OverflowError, RecursionError, subprocess.SubprocessError): print(json.dumps({'error_code': 'POD_AGENT_CHECK_FAILED'})) diff --git a/tests/test_vm_pod_image_bridge.py b/tests/test_vm_pod_image_bridge.py index df4c841..f3edee7 100644 --- a/tests/test_vm_pod_image_bridge.py +++ b/tests/test_vm_pod_image_bridge.py @@ -7,6 +7,9 @@ import subprocess import sys import tempfile +import threading +import time +import socket from types import SimpleNamespace import unittest from unittest.mock import patch @@ -318,6 +321,110 @@ def test_cli_errors_do_not_print_private_configuration(self): self.assertEqual(json.loads(output.getvalue()), {'error_code': 'POD_AGENT_CHECK_FAILED'}) self.assertNotIn('private-config-value', output.getvalue()) + def test_open_stdin_without_bytes_expires(self): + reader, writer = os.pipe() + try: + with os.fdopen(reader, 'rb', buffering=0) as stream: + started = time.monotonic() + with self.assertRaises(ValueError): + bridge.read_request(stream, started + 0.04) + self.assertLess(time.monotonic() - started, 1) + finally: + os.close(writer) + + def test_partial_stdin_expires_before_sender_closes(self): + reader, writer = os.pipe() + try: + os.write(writer, b'{') + with os.fdopen(reader, 'rb', buffering=0) as stream: + started = time.monotonic() + with self.assertRaises(ValueError): + bridge.read_request(stream, started + 0.04) + self.assertLess(time.monotonic() - started, 1) + finally: + os.close(writer) + + def test_split_request_is_read_until_eof(self): + reader, writer = os.pipe() + def send(): + try: + os.write(writer, b'{') + time.sleep(0.02) + os.write(writer, b'}') + finally: + os.close(writer) + sender = threading.Thread(target=send) + sender.start() + try: + with os.fdopen(reader, 'rb', buffering=0) as stream: + self.assertEqual(bridge.read_request(stream, time.monotonic() + 2), {}) + finally: + sender.join(timeout=2) + self.assertFalse(sender.is_alive()) + + def test_regular_file_stdin_is_supported(self): + with tempfile.TemporaryFile() as stream: + stream.write(b'{}') + stream.seek(0) + self.assertEqual(bridge.read_request(stream, time.monotonic() + 1), {}) + + def test_input_limit_does_not_wait_for_eof(self): + receiver, sender = socket.socketpair() + try: + sender.settimeout(1) + sender.sendall(b'x' * (bridge.MAX_REQUEST + 1)) + with receiver.makefile('rb', buffering=0) as stream: + started = time.monotonic() + with self.assertRaises(ValueError): + bridge.read_request(stream, started + 1) + self.assertLess(time.monotonic() - started, 1) + finally: + receiver.close() + sender.close() + + def test_main_uses_one_deadline_for_configuration_input_and_operations(self): + clock = [10.0] + seen = [] + def configuration(): + clock[0] = 10.4 + return config() + def read(stream, deadline): + seen.append(deadline) + clock[0] = 10.7 + return {} + def operation(*args, deadline): + seen.append(deadline) + clock[0] = 10.9 + return {'schema_version': 1} + with patch('sys.argv', ['bridge', 'preflight']), \ + patch.object(bridge, 'CALL_TIMEOUT_SECONDS', 1), \ + patch.object(bridge.time, 'monotonic', side_effect=lambda: clock[0]), \ + patch.object(bridge, 'load_config', side_effect=configuration), \ + patch.object(bridge, 'read_request', side_effect=read), \ + patch.object(bridge, 'handle', side_effect=operation), \ + patch('sys.stdout', new_callable=io.StringIO): + self.assertEqual(bridge.main(), 0) + self.assertEqual(seen, [11.0, 11.0]) + + def test_configuration_time_can_exhaust_budget_before_stdin(self): + clock = [10.0] + def configuration(): + clock[0] = 12.0 + return config() + with tempfile.TemporaryFile() as stream: + stream.write(b'{}') + stream.seek(0) + with patch('sys.argv', ['bridge', 'preflight']), \ + patch('sys.stdin', SimpleNamespace(buffer=stream)), \ + patch.object(bridge, 'CALL_TIMEOUT_SECONDS', 1), \ + patch.object(bridge.time, 'monotonic', side_effect=lambda: clock[0]), \ + patch.object(bridge, 'load_config', side_effect=configuration), \ + patch.object(bridge, 'handle') as operation, \ + patch('sys.stdout', new_callable=io.StringIO) as output: + self.assertEqual(bridge.main(), 1) + self.assertEqual(json.loads(output.getvalue()), {'error_code': 'POD_AGENT_CHECK_FAILED'}) + operation.assert_not_called() + def pod_inventory(): scope, nodes = inputs()