This commit is contained in:
commit
db82325ed7
216 files changed
+33718
No files matched your search
@@ -0,0 +1,9 @@
|
||||
ARG BASE_IMAGE=python:3.12-slim
|
||||
FROM ${BASE_IMAGE}
|
||||
WORKDIR /app
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY app.py runtime.py domain_runtime.py review_tools.py ./
|
||||
USER 10001:10001
|
||||
EXPOSE 8090
|
||||
CMD ["uvicorn","app:app","--host","0.0.0.0","--port","8090","--workers","1","--no-access-log"]
|
||||
@@ -0,0 +1,329 @@
|
||||
"""Trusted sandbox gateway. Every user operation executes in a restricted K8s Pod.
|
||||
|
||||
No Docker socket, hostPath, privileged execution or host subprocess is used.
|
||||
The gateway is private infrastructure and requires a cluster service account.
|
||||
"""
|
||||
import base64
|
||||
import hmac
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI, Header, HTTPException
|
||||
from kubernetes import client, config
|
||||
from kubernetes.client.rest import ApiException
|
||||
from kubernetes.stream import stream
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
TOKEN = os.environ.get("SANDBOX_TOKEN", "")
|
||||
NAMESPACE = os.environ.get("SANDBOX_NAMESPACE", "platform-sandboxes")
|
||||
REVIEW_NAMESPACE = os.environ.get("REVIEW_NAMESPACE", "platform-reviews")
|
||||
IMAGE = os.environ.get("SANDBOX_IMAGE", "")
|
||||
RUNTIME = os.environ.get("SANDBOX_RUNTIME_CLASS", "gvisor")
|
||||
LOCKS: dict[str, threading.Lock] = {}
|
||||
LOCK_GUARD = threading.Lock()
|
||||
MAX_OUTPUT = 100_000
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
if len(TOKEN) < 32 or not IMAGE or not RUNTIME:
|
||||
raise RuntimeError("Configure SANDBOX_TOKEN, SANDBOX_IMAGE and isolated runtime class")
|
||||
config.load_incluster_config()
|
||||
app.state.core = client.CoreV1Api()
|
||||
app.state.net = client.NetworkingV1Api()
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None)
|
||||
from runtime import router as runtime_router
|
||||
app.include_router(runtime_router)
|
||||
from domain_runtime import router as domain_router
|
||||
app.include_router(domain_router)
|
||||
|
||||
|
||||
class Tool(BaseModel):
|
||||
name: str = Field(max_length=40)
|
||||
arguments: dict
|
||||
|
||||
|
||||
def authorize(value: str | None):
|
||||
if len(TOKEN) < 32 or not hmac.compare_digest(value or "", "Bearer " + TOKEN):
|
||||
raise HTTPException(401, "Unauthorized")
|
||||
|
||||
|
||||
@app.get("/health/ready")
|
||||
def ready(authorization: str | None = Header(default=None)):
|
||||
authorize(authorization)
|
||||
try:
|
||||
app.state.core.list_namespaced_pod(NAMESPACE, limit=1, _request_timeout=(3, 5))
|
||||
except Exception:
|
||||
raise HTTPException(503, "Sandbox Kubernetes API unavailable") from None
|
||||
return {"ready": True}
|
||||
|
||||
|
||||
def workspace_name(project: str) -> str:
|
||||
try:
|
||||
return "dev-" + uuid.UUID(project).hex
|
||||
except ValueError:
|
||||
raise HTTPException(400, "Invalid project ID")
|
||||
|
||||
|
||||
def create_once(method, **kwargs):
|
||||
try:
|
||||
method(**kwargs)
|
||||
except ApiException as exc:
|
||||
if exc.status != 409:
|
||||
raise HTTPException(503, "Kubernetes resource provisioning failed") from None
|
||||
|
||||
|
||||
def ensure_workspace(project: str, review=False):
|
||||
if review and REVIEW_NAMESPACE == NAMESPACE:
|
||||
raise HTTPException(503,'Review namespace must be separate from development')
|
||||
name = workspace_name(project)
|
||||
namespace = REVIEW_NAMESPACE if review else NAMESPACE
|
||||
core, net = app.state.core, app.state.net
|
||||
# The namespace MUST enforce restricted Pod Security Admission and ResourceQuota.
|
||||
labels = {"platform/workspace": name}
|
||||
create_once(
|
||||
net.create_namespaced_network_policy,
|
||||
namespace=namespace,
|
||||
body={
|
||||
"apiVersion": "networking.k8s.io/v1", "kind": "NetworkPolicy",
|
||||
"metadata": {"name": name},
|
||||
"spec": {"podSelector": {"matchLabels": labels}, "policyTypes": ["Ingress", "Egress"],
|
||||
"ingress": [],
|
||||
"egress": [
|
||||
{"to": [{"namespaceSelector": {"matchLabels": {"kubernetes.io/metadata.name": "kube-system"}}}],
|
||||
"ports": [{"port": 53, "protocol": "UDP"}, {"port": 53, "protocol": "TCP"}]},
|
||||
{"to": [{"ipBlock": {"cidr": "0.0.0.0/0", "except": [
|
||||
"0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8",
|
||||
"169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24", "192.168.0.0/16",
|
||||
"198.18.0.0/15", "224.0.0.0/4", "240.0.0.0/4"]}}],
|
||||
"ports": [{"port": 443, "protocol": "TCP"}, {"port": 80, "protocol": "TCP"}]}
|
||||
]}
|
||||
})
|
||||
create_once(core.create_namespaced_persistent_volume_claim, namespace=namespace, body={
|
||||
"apiVersion": "v1", "kind": "PersistentVolumeClaim", "metadata": {"name": name},
|
||||
"spec": {"accessModes": ["ReadWriteOnce"], "resources": {"requests": {"storage": "5Gi"}}}})
|
||||
create_once(core.create_namespaced_pod, namespace=namespace, body={
|
||||
"apiVersion": "v1", "kind": "Pod", "metadata": {"name": name, "labels": labels},
|
||||
"spec": {"runtimeClassName": RUNTIME, "automountServiceAccountToken": False,
|
||||
"restartPolicy": "Always", "enableServiceLinks": False,
|
||||
"securityContext": {"runAsNonRoot": True, "runAsUser": 10001, "runAsGroup": 10001,
|
||||
"fsGroup": 10001, "seccompProfile": {"type": "RuntimeDefault"}},
|
||||
"containers": [{"name": "workspace", "image": IMAGE,
|
||||
"command": ["python3", "-c", "import time; time.sleep(2147483647)"],
|
||||
"workingDir": "/workspace",
|
||||
"securityContext": {"allowPrivilegeEscalation": False, "readOnlyRootFilesystem": True,
|
||||
"capabilities": {"drop": ["ALL"]}},
|
||||
"env": [{"name": "HOME", "value": "/workspace"}, {"name": "PIP_CACHE_DIR", "value": "/tmp/pip-cache"}],
|
||||
"resources": {"requests": {"cpu": "100m", "memory": "128Mi"},
|
||||
"limits": {"cpu": "1", "memory": "1Gi", "ephemeral-storage": "2Gi"}},
|
||||
"volumeMounts": [{"name": "work", "mountPath": "/workspace"}, {"name": "tmp", "mountPath": "/tmp"}]}],
|
||||
"volumes": [{"name": "work", "persistentVolumeClaim": {"claimName": name}},
|
||||
{"name": "tmp", "emptyDir": {"sizeLimit": "1Gi"}}]}})
|
||||
deadline = time.monotonic() + 90
|
||||
while time.monotonic() < deadline:
|
||||
pod = core.read_namespaced_pod(name, namespace)
|
||||
if pod.status.phase == "Running" and pod.status.container_statuses and all(s.ready for s in pod.status.container_statuses):
|
||||
return name
|
||||
if pod.status.phase in ("Failed", "Succeeded"):
|
||||
raise HTTPException(503, "Sandbox terminated; requires operator inspection")
|
||||
time.sleep(1)
|
||||
raise HTTPException(503, "Sandbox not ready; check isolated runtime, image and storage provisioner")
|
||||
|
||||
|
||||
# This code runs ONLY inside the workspace Pod via Kubernetes exec.
|
||||
SCRIPT = r'''
|
||||
import pathlib,os,json,base64,subprocess,signal,tempfile
|
||||
args=json.loads(base64.b64decode(PAYLOAD))
|
||||
root=pathlib.Path('/workspace').resolve()
|
||||
name=args['name']; values=args['arguments']
|
||||
def path(value):
|
||||
if not isinstance(value,str) or len(value)>1000: raise ValueError('Invalid path')
|
||||
target=(root/value).resolve()
|
||||
if not target.is_relative_to(root): raise ValueError('Path outside workspace')
|
||||
return target
|
||||
try:
|
||||
if name=='run_command':
|
||||
command=values.get('command','')
|
||||
if not isinstance(command,str) or not command or len(command)>10000: raise ValueError('Invalid command')
|
||||
# Output goes to bounded tmpfs, not an unbounded pipe in the gateway.
|
||||
with tempfile.TemporaryFile() as out:
|
||||
p=subprocess.Popen(['/bin/sh','-lc',command],cwd=root,stdout=out,stderr=subprocess.STDOUT,start_new_session=True)
|
||||
timed_out=False
|
||||
try: p.wait(timeout=120)
|
||||
except subprocess.TimeoutExpired:
|
||||
timed_out=True;os.killpg(p.pid,signal.SIGKILL);p.wait()
|
||||
out.seek(0);output=out.read(64001)
|
||||
result={'exitCode':p.returncode,'timedOut':timed_out,'output':output[:64000].decode('utf-8','replace'),'truncated':len(output)>64000}
|
||||
elif name=='review_export':
|
||||
result=review_export(root)
|
||||
elif name=='review_import':
|
||||
result=review_import(root,values)
|
||||
elif name=='review_validate':
|
||||
result=review_validate(root)
|
||||
elif name=='workspace_inspect':
|
||||
source_extensions={'.html','.htm','.js','.jsx','.ts','.tsx','.vue','.svelte','.css','.scss','.less','.py','.java','.go','.rs','.php','.rb','.cs','.sql'}
|
||||
excluded={'.git','node_modules','.venv','__pycache__','.next','dist','build','coverage'}
|
||||
files=[];source_count=0;total=0;visited=0;complete=True
|
||||
for directory,dirs,names in os.walk(root,followlinks=False):
|
||||
dirs[:]=sorted(d for d in dirs if d not in excluded and not (pathlib.Path(directory)/d).is_symlink())
|
||||
for filename in sorted(names):
|
||||
visited+=1
|
||||
if visited>5000:complete=False;break
|
||||
file=pathlib.Path(directory)/filename
|
||||
if file.is_symlink() or not file.is_file() or filename=='.env' or filename.startswith('.env.'):continue
|
||||
try:size=file.stat().st_size
|
||||
except OSError:continue
|
||||
total+=size
|
||||
if size>0 and file.suffix.lower() in source_extensions:source_count+=1
|
||||
if len(files)<100:files.append({'path':str(file.relative_to(root)),'bytes':size})
|
||||
if not complete:break
|
||||
result={'sourceFiles':source_count,'files':files,'totalBytes':total,'complete':complete}
|
||||
elif name=='archive_source':
|
||||
import io,zipfile
|
||||
output=io.BytesIO();total=0;count=0
|
||||
with zipfile.ZipFile(output,'w',zipfile.ZIP_DEFLATED) as archive:
|
||||
for file in root.rglob('*'):
|
||||
rel=file.relative_to(root)
|
||||
if any(part in {'.git','node_modules','.venv','__pycache__'} for part in rel.parts): continue
|
||||
if file.is_symlink() or not file.is_file(): continue
|
||||
target=path(str(rel));size=target.stat().st_size;total+=size;count+=1
|
||||
if total>10_000_000 or count>5000: raise ValueError('Source archive exceeds 10MB / 5000 files limit')
|
||||
archive.write(target,str(rel))
|
||||
result={'base64':base64.b64encode(output.getvalue()).decode(),'files':count}
|
||||
elif name in ('version_save','version_list'):
|
||||
def git(*arguments):
|
||||
return subprocess.run(['git','-c','core.hooksPath=/dev/null',*arguments],cwd=root,capture_output=True,text=True,timeout=25)
|
||||
if name=='version_save':
|
||||
message=values.get('message','')
|
||||
if not isinstance(message,str) or not message or len(message)>200: raise ValueError('Invalid version message')
|
||||
git('init');git('config','user.name','Workspace');git('config','user.email','workspace@localhost')
|
||||
ignore=root/'.git/info/exclude'
|
||||
if ignore.parent.is_symlink() or ignore.is_symlink(): raise ValueError('Invalid Git metadata path')
|
||||
with ignore.open('a') as f:f.write('\nnode_modules/\n.venv/\n.env\n.env.*\n!.env.example\n')
|
||||
added=git('add','-A')
|
||||
if added.returncode: raise ValueError('Git staging failed')
|
||||
committed=git('commit','-m',message)
|
||||
if committed.returncode and 'nothing to commit' not in committed.stdout: raise ValueError('Git commit failed')
|
||||
log=git('log','-30','--format=%H%x09%ct%x09%s')
|
||||
result={'versions':[dict(zip(['commit','timestamp','message'],line.split('\t',2))) for line in log.stdout.splitlines() if '\t' in line]}
|
||||
elif name=='list_files':
|
||||
target=path(values.get('path','.'))
|
||||
result={'files':[{'name':p.name,'directory':p.is_dir(),'symlink':p.is_symlink()} for p in sorted(target.iterdir())][:1000]}
|
||||
elif name=='read_file':
|
||||
target=path(values['path'])
|
||||
if target.stat().st_size>64000: raise ValueError('File exceeds 64KB reading limit')
|
||||
result={'content':target.read_text(encoding='utf-8')}
|
||||
elif name=='write_file':
|
||||
target=path(values['path']);content=values.get('content','')
|
||||
if not isinstance(content,str) or len(content.encode('utf-8'))>256000: raise ValueError('Content exceeds limit')
|
||||
target.parent.mkdir(parents=True,exist_ok=True);target=path(values['path'])
|
||||
target.write_text(content,encoding='utf-8');result={'written':len(content.encode('utf-8'))}
|
||||
elif name=='delete_file':
|
||||
target=path(values['path'])
|
||||
if not target.is_file(): raise ValueError('Only individual files may be deleted')
|
||||
target.unlink();result={'deleted':True}
|
||||
else: raise ValueError('Unknown tool')
|
||||
print(json.dumps(result,ensure_ascii=False))
|
||||
except Exception as exc:
|
||||
print(json.dumps({'error':str(exc)[:500]}))
|
||||
'''
|
||||
SCRIPT = Path(__file__).with_name('review_tools.py').read_text(encoding='utf-8') + '\n' + SCRIPT
|
||||
|
||||
|
||||
def read_exec_output(connection, limit):
|
||||
# _preload_content=True lets ApiClient parse JSON and coerce dictionaries back to
|
||||
# Python repr strings. Read raw stdout explicitly and keep stderr separate.
|
||||
deadline = time.monotonic() + 135
|
||||
chunks, size, error_size = [], 0, 0
|
||||
try:
|
||||
while connection.is_open():
|
||||
remaining = deadline - time.monotonic()
|
||||
if remaining <= 0:
|
||||
raise HTTPException(504, 'Sandbox execution timed out; result must be checked')
|
||||
connection.update(timeout=min(1, remaining))
|
||||
if connection.peek_stdout():
|
||||
chunk = connection.read_stdout()
|
||||
size += len(chunk.encode('utf-8'))
|
||||
if size > limit:
|
||||
raise HTTPException(502, 'Tool output exceeded limit')
|
||||
chunks.append(chunk)
|
||||
if connection.peek_stderr():
|
||||
error_size += len(connection.read_stderr().encode('utf-8'))
|
||||
if error_size > 64000:
|
||||
raise HTTPException(502, 'Sandbox diagnostic output exceeded limit')
|
||||
try:
|
||||
code = connection.returncode
|
||||
except Exception:
|
||||
raise HTTPException(502, 'Sandbox completion status is unknown') from None
|
||||
if code != 0:
|
||||
raise HTTPException(502, 'Sandbox wrapper did not complete successfully')
|
||||
return ''.join(chunks)
|
||||
finally:
|
||||
connection.close()
|
||||
|
||||
|
||||
@app.post("/workspaces/{project}/tools")
|
||||
def tool(project: str, request: Tool, authorization: str | None = Header(default=None)):
|
||||
return execute_tool(project,request,authorization,False)
|
||||
|
||||
|
||||
@app.post("/reviews/{project}/tools")
|
||||
def review_tool(project: str, request: Tool, authorization: str | None = Header(default=None)):
|
||||
return execute_tool(project,request,authorization,True)
|
||||
|
||||
|
||||
@app.delete('/reviews/{project}')
|
||||
def dispose_review(project: str, authorization: str | None = Header(default=None)):
|
||||
authorize(authorization)
|
||||
if REVIEW_NAMESPACE == NAMESPACE:raise HTTPException(503,'Review namespace must be separate')
|
||||
name=workspace_name(project)
|
||||
for operation in (app.state.core.delete_namespaced_pod,app.state.core.delete_namespaced_persistent_volume_claim,app.state.net.delete_namespaced_network_policy):
|
||||
try:operation(name,REVIEW_NAMESPACE,_request_timeout=(3,10))
|
||||
except ApiException as error:
|
||||
if error.status!=404:raise HTTPException(503,'Review cleanup failed') from None
|
||||
return {'cleanupRequested':True}
|
||||
|
||||
|
||||
def execute_tool(project,request,authorization,review):
|
||||
authorize(authorization)
|
||||
name = workspace_name(project)
|
||||
allowed = {'review_import','review_validate','workspace_inspect'} if review else {"list_files", "read_file", "write_file", "delete_file", "run_command", "version_save", "version_list", "archive_source", "workspace_inspect",'review_export'}
|
||||
if request.name not in allowed:
|
||||
raise HTTPException(400, "Unknown tool")
|
||||
payload = json.dumps(request.model_dump(), ensure_ascii=False).encode()
|
||||
if len(payload) > (16_000_000 if request.name=='review_import' else 300_000):
|
||||
raise HTTPException(413, "Tool payload too large")
|
||||
with LOCK_GUARD:
|
||||
lock = LOCKS.setdefault(('review:' if review else '')+name, threading.Lock())
|
||||
if not lock.acquire(timeout=1):
|
||||
raise HTTPException(409, "Workspace is busy")
|
||||
try:
|
||||
ensure_workspace(project,review)
|
||||
encoded = base64.b64encode(payload).decode()
|
||||
code = "import sys\nPAYLOAD=sys.stdin.read(" + str(len(encoded)) + ")\n" + SCRIPT
|
||||
# Streaming temporarily overrides ApiClient.request. Never share that client
|
||||
# with concurrent health checks, workspace provisioning, or another project.
|
||||
with client.ApiClient() as api_client:
|
||||
core = client.CoreV1Api(api_client)
|
||||
connection = stream(core.connect_get_namespaced_pod_exec, name, REVIEW_NAMESPACE if review else NAMESPACE,
|
||||
container="workspace", command=["timeout", "130", "python3", "-I", "-c", code],
|
||||
stderr=True, stdin=True, stdout=True, tty=False, _request_timeout=135,
|
||||
_preload_content=False)
|
||||
for offset in range(0,len(encoded),16384):connection.write_stdin(encoded[offset:offset+16384])
|
||||
output = read_exec_output(connection, 16_000_000 if request.name in {'archive_source','review_export'} else MAX_OUTPUT)
|
||||
try:
|
||||
return json.loads(output)
|
||||
except ValueError:
|
||||
raise HTTPException(502, "Sandbox returned invalid output") from None
|
||||
except ApiException:
|
||||
raise HTTPException(503, "Kubernetes sandbox operation failed") from None
|
||||
finally:
|
||||
lock.release()
|
||||
@@ -0,0 +1,143 @@
|
||||
"""Platform-owned Ingress and cert-manager resources; never executes tenant code."""
|
||||
import os
|
||||
import re
|
||||
import threading
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
from fastapi import APIRouter, Header, HTTPException
|
||||
from kubernetes import client
|
||||
from kubernetes.client.rest import ApiException
|
||||
from pydantic import BaseModel, Field
|
||||
from runtime import NAMESPACE, auth, blocked, guard, locks
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
class DomainRoute(BaseModel):
|
||||
projectId: uuid.UUID
|
||||
serviceId: uuid.UUID
|
||||
hostname: str = Field(min_length=3, max_length=253)
|
||||
generation: int = Field(ge=1)
|
||||
port: int = Field(default=80, ge=1, le=65535)
|
||||
remove: bool = False
|
||||
|
||||
|
||||
def hostname(value):
|
||||
if value != value.lower() or '.' not in value or re.fullmatch(r'[0-9.]+', value):
|
||||
raise HTTPException(400, 'Invalid domain hostname')
|
||||
if any(not re.fullmatch(r'[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?', label) for label in value.split('.')):
|
||||
raise HTTPException(400, 'Invalid domain hostname')
|
||||
return value
|
||||
|
||||
|
||||
def metadata(resource):
|
||||
return resource.get('metadata', {}) if isinstance(resource, dict) else resource.metadata.to_dict()
|
||||
|
||||
|
||||
def owned(resource, identity, generation):
|
||||
meta = metadata(resource)
|
||||
if (meta.get('labels') or {}).get('platform/domain') != identity:
|
||||
raise HTTPException(409, 'Existing resource is not owned by this domain binding')
|
||||
if int((meta.get('annotations') or {}).get('platform/generation', '0')) > generation:
|
||||
raise HTTPException(409, 'Stale domain operation')
|
||||
|
||||
|
||||
def upsert(create, read, patch, body, identity, generation):
|
||||
try:
|
||||
return create(body)
|
||||
except ApiException as error:
|
||||
if error.status != 409:
|
||||
raise
|
||||
current = read()
|
||||
owned(current, identity, generation)
|
||||
meta = metadata(current)
|
||||
body['metadata']['resourceVersion'] = meta.get('resourceVersion', meta.get('resource_version'))
|
||||
current_spec = current.get('spec') if isinstance(current, dict) else None
|
||||
if current_spec == body['spec'] and (meta.get('annotations') or {}).get('platform/generation') == str(generation):
|
||||
return current
|
||||
return patch(body)
|
||||
|
||||
|
||||
def certificate_ready(certificate, host, secret):
|
||||
spec, status = certificate.get('spec', {}), certificate.get('status', {})
|
||||
if host not in spec.get('dnsNames', []) or spec.get('secretName') != secret:
|
||||
return False
|
||||
generation = certificate.get('metadata', {}).get('generation')
|
||||
if not isinstance(generation, int) or generation < 1:
|
||||
return False
|
||||
if not any(c.get('type') == 'Ready' and c.get('status') == 'True' and c.get('observedGeneration') == generation for c in status.get('conditions', [])):
|
||||
return False
|
||||
try:
|
||||
return datetime.fromisoformat(status['notAfter'].replace('Z', '+00:00')) > datetime.now(timezone.utc)
|
||||
except (KeyError, ValueError, TypeError):
|
||||
return False
|
||||
|
||||
|
||||
def delete_owned(read, delete, identity, generation):
|
||||
try:
|
||||
resource = read()
|
||||
owned(resource, identity, generation)
|
||||
meta = metadata(resource)
|
||||
delete({'preconditions': {'uid': meta.get('uid'), 'resourceVersion': meta.get('resourceVersion', meta.get('resource_version'))}})
|
||||
except ApiException as error:
|
||||
if error.status != 404:
|
||||
raise
|
||||
|
||||
|
||||
@router.post('/runtime/domains/{domain}')
|
||||
def apply_domain(domain: uuid.UUID, request: DomainRoute, authorization: str | None = Header(default=None)):
|
||||
auth(authorization)
|
||||
identity, project = domain.hex, request.projectId.hex
|
||||
host = hostname(request.hostname)
|
||||
name, secret = 'domain-' + identity, 'domain-' + identity + '-tls'
|
||||
with guard:
|
||||
lock = locks.setdefault('route-' + identity, threading.Lock())
|
||||
if not lock.acquire(timeout=1):
|
||||
raise HTTPException(409, 'Domain operation in progress')
|
||||
core, network, custom = client.CoreV1Api(), client.NetworkingV1Api(), client.CustomObjectsApi()
|
||||
try:
|
||||
receipt_name = 'route-' + identity
|
||||
try:
|
||||
receipt = core.read_namespaced_config_map(receipt_name, NAMESPACE)
|
||||
data = receipt.data or {}
|
||||
if data.get('project') != project or data.get('hostname') != host:
|
||||
raise HTTPException(409, 'Domain receipt owner conflict')
|
||||
if int(data.get('generation', '0')) > request.generation or (int(data.get('generation', '0')) == request.generation and data.get('desired') == 'REMOVED' and not request.remove):
|
||||
raise HTTPException(409, 'Stale domain operation')
|
||||
core.patch_namespaced_config_map(receipt_name, NAMESPACE, {'metadata': {'resourceVersion': receipt.metadata.resource_version}, 'data': {'generation': str(request.generation), 'desired': 'REMOVED' if request.remove else 'ACTIVE'}})
|
||||
except ApiException as error:
|
||||
if error.status != 404:
|
||||
raise
|
||||
core.create_namespaced_config_map(NAMESPACE, {'metadata': {'name': receipt_name}, 'data': {'project': project, 'hostname': host, 'generation': str(request.generation), 'desired': 'REMOVED' if request.remove else 'ACTIVE'}})
|
||||
cert_read = lambda: custom.get_namespaced_custom_object('cert-manager.io', 'v1', NAMESPACE, 'certificates', name)
|
||||
if request.remove:
|
||||
delete_owned(lambda: network.read_namespaced_ingress(name, NAMESPACE), lambda body: network.delete_namespaced_ingress(name, NAMESPACE, body=body), identity, request.generation)
|
||||
delete_owned(cert_read, lambda body: custom.delete_namespaced_custom_object('cert-manager.io', 'v1', NAMESPACE, 'certificates', name, body=body), identity, request.generation)
|
||||
# Only a secret labelled by our Certificate secretTemplate is eligible for removal.
|
||||
delete_owned(lambda: core.read_namespaced_secret(secret, NAMESPACE), lambda body: core.delete_namespaced_secret(secret, NAMESPACE, body=body), identity, request.generation)
|
||||
return {'removed': True, 'generation': request.generation}
|
||||
if blocked(core, project):
|
||||
raise HTTPException(409, 'Project runtime is suspended')
|
||||
issuer = os.environ.get('RUNTIME_TLS_ISSUER', '')
|
||||
if not issuer:
|
||||
raise HTTPException(503, 'Configure a cert-manager ClusterIssuer before domain provisioning')
|
||||
stable = 'svc-' + request.serviceId.hex[:20]
|
||||
service = core.read_namespaced_service(stable, NAMESPACE)
|
||||
selected = service.spec.selector or {}
|
||||
if selected.get('platform/project') != project or selected.get('platform/service') != request.serviceId.hex or not any(p.port == request.port for p in service.spec.ports or []):
|
||||
raise HTTPException(409, 'Published service or port does not match this project')
|
||||
meta = {'name': name, 'labels': {'platform/domain': identity, 'platform/project': project}, 'annotations': {'platform/generation': str(request.generation)}}
|
||||
certificate = {'apiVersion': 'cert-manager.io/v1', 'kind': 'Certificate', 'metadata': dict(meta), 'spec': {'secretName': secret, 'dnsNames': [host], 'issuerRef': {'name': issuer, 'kind': 'ClusterIssuer'}, 'secretTemplate': {'labels': {'platform/domain': identity, 'platform/project': project}}}}
|
||||
upsert(lambda body: custom.create_namespaced_custom_object('cert-manager.io', 'v1', NAMESPACE, 'certificates', body), cert_read, lambda body: custom.patch_namespaced_custom_object('cert-manager.io', 'v1', NAMESPACE, 'certificates', name, body), certificate, identity, request.generation)
|
||||
ingress_meta = {**meta, 'annotations': {**meta['annotations'], 'nginx.ingress.kubernetes.io/ssl-redirect': 'true'}}
|
||||
ingress = {'apiVersion': 'networking.k8s.io/v1', 'kind': 'Ingress', 'metadata': ingress_meta, 'spec': {'ingressClassName': os.environ.get('RUNTIME_INGRESS_CLASS', 'nginx'), 'tls': [{'hosts': [host], 'secretName': secret}], 'rules': [{'host': host, 'http': {'paths': [{'path': '/', 'pathType': 'Prefix', 'backend': {'service': {'name': stable, 'port': {'number': request.port}}}}]}}]}}
|
||||
upsert(lambda body: network.create_namespaced_ingress(NAMESPACE, body), lambda: network.read_namespaced_ingress(name, NAMESPACE), lambda body: network.patch_namespaced_ingress(name, NAMESPACE, body), ingress, identity, request.generation)
|
||||
current_ingress = network.read_namespaced_ingress(name, NAMESPACE)
|
||||
pods = core.list_namespaced_pod(NAMESPACE, label_selector=','.join(k + '=' + v for k, v in selected.items())).items
|
||||
has_pods = any(p.status.phase == 'Running' and p.status.container_statuses and all(c.ready for c in p.status.container_statuses) for p in pods)
|
||||
route_ready = bool(current_ingress.status and current_ingress.status.load_balancer and current_ingress.status.load_balancer.ingress) and has_pods
|
||||
return {'configured': True, 'routeReady': route_ready, 'tlsReady': certificate_ready(cert_read(), host, secret), 'generation': request.generation}
|
||||
except ApiException:
|
||||
raise HTTPException(503, 'Domain resources not ready; check ingress controller, cert-manager and gateway RBAC') from None
|
||||
finally:
|
||||
lock.release()
|
||||
@@ -0,0 +1,4 @@
|
||||
fastapi==0.121.0
|
||||
uvicorn==0.38.0
|
||||
kubernetes==34.1.0
|
||||
pydantic==2.12.3
|
||||
@@ -0,0 +1,184 @@
|
||||
"""Trusted review helpers, embedded into Python exec ONLY inside isolated workspace Pods."""
|
||||
import base64
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import signal
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
import zipfile
|
||||
from html.parser import HTMLParser
|
||||
from urllib.parse import unquote, urlsplit
|
||||
|
||||
REVIEW_EXCLUDED = {'.git', 'node_modules', '.venv', '__pycache__', '.next', 'dist', 'build', 'coverage', '.ssh'}
|
||||
REVIEW_TEXT = {'.html', '.css', '.js', '.jsx', '.ts', '.tsx', '.vue', '.svelte', '.py', '.java', '.go', '.rs', '.php', '.json'}
|
||||
|
||||
|
||||
def review_secret(path):
|
||||
return any(part in REVIEW_EXCLUDED or part.startswith('.env') or part.startswith('.webup-review')
|
||||
for part in path.parts) or path.name in {'.npmrc', '.pypirc', '.netrc', 'credentials.json'} or path.suffix.lower() in {'.pem', '.key', '.p12', '.pfx'}
|
||||
|
||||
|
||||
def review_redact(value):
|
||||
value = re.sub(r'\bsk-[A-Za-z0-9_-]{16,}\b', '[REDACTED_KEY]', value)
|
||||
return re.sub(r'(?i)((?:api[_-]?key|secret|password|access[_-]?token)\s*[=:]\s*[\"\x27]?)[^\s\"\x27;,}]{8,}', r'\1[REDACTED]', value)
|
||||
|
||||
|
||||
def review_fingerprint(files):
|
||||
return hashlib.sha256(json.dumps(files, sort_keys=True, separators=(',', ':'), ensure_ascii=True).encode()).hexdigest()
|
||||
|
||||
|
||||
def review_export(root):
|
||||
files, samples, total, sample_chars = [], [], 0, 0
|
||||
archive = io.BytesIO()
|
||||
with zipfile.ZipFile(archive, 'w', zipfile.ZIP_DEFLATED) as output:
|
||||
for directory, dirs, names in os.walk(root, followlinks=False):
|
||||
dirs[:] = sorted(d for d in dirs if not review_secret(pathlib.Path(d)) and not (pathlib.Path(directory) / d).is_symlink())
|
||||
for name in sorted(names):
|
||||
file = pathlib.Path(directory) / name
|
||||
rel = file.relative_to(root)
|
||||
if review_secret(rel) or file.is_symlink() or not file.is_file():
|
||||
continue
|
||||
size = file.stat().st_size
|
||||
if size > 10_000_000 or total + size > 10_000_000 or len(files) >= 2000:
|
||||
raise ValueError('Review snapshot exceeds 10MB / 2000 files')
|
||||
with file.open('rb') as stream:
|
||||
data = stream.read(10_000_001)
|
||||
total += len(data)
|
||||
if total > 10_000_000 or len(data) > 10_000_000:
|
||||
raise ValueError('Review snapshot exceeds size limit')
|
||||
files.append({'path': rel.as_posix(), 'bytes': len(data), 'sha256': hashlib.sha256(data).hexdigest()})
|
||||
output.writestr(rel.as_posix(), data)
|
||||
if file.suffix.lower() in REVIEW_TEXT and file.name not in {'package-lock.json', 'yarn.lock'} and sample_chars < 14000:
|
||||
text = data[:64000].decode('utf-8', 'replace')
|
||||
shown = review_redact(text[:min(7000, 14000 - sample_chars)])
|
||||
samples.append({'path': rel.as_posix(), 'content': shown, 'truncated': len(shown) < len(text)})
|
||||
sample_chars += len(shown)
|
||||
files.sort(key=lambda entry: entry['path'])
|
||||
data = archive.getvalue()
|
||||
return {'sourceHash': review_fingerprint(files), 'archiveSha256': hashlib.sha256(data).hexdigest(),
|
||||
'archive': base64.b64encode(data).decode(), 'files': files, 'samples': samples,
|
||||
'complete': True, 'totalBytes': total}
|
||||
|
||||
|
||||
def review_import(root, values):
|
||||
if any(root.iterdir()):
|
||||
raise ValueError('Review workspace must be empty; snapshots cannot overwrite existing data')
|
||||
data = base64.b64decode(values['archive'], validate=True)
|
||||
if len(data) > 11_000_000 or hashlib.sha256(data).hexdigest() != values['archiveSha256']:
|
||||
raise ValueError('Snapshot archive integrity mismatch')
|
||||
files, entries, total = [], [], 0
|
||||
with zipfile.ZipFile(io.BytesIO(data)) as archive:
|
||||
for entry in archive.infolist():
|
||||
rel = pathlib.PurePosixPath(entry.filename.replace('\\', '/'))
|
||||
if rel.is_absolute() or '..' in rel.parts or review_secret(rel) or (entry.external_attr >> 16) & 0o170000 == 0o120000:
|
||||
raise ValueError('Unsafe review snapshot entry')
|
||||
if entry.is_dir():
|
||||
continue
|
||||
total += entry.file_size
|
||||
if total > 10_000_000 or len(entries) >= 2000 or entry.filename in {e[0] for e in entries}:
|
||||
raise ValueError('Invalid or oversized snapshot')
|
||||
content = archive.read(entry)
|
||||
if len(content) != entry.file_size:
|
||||
raise ValueError('Snapshot entry size mismatch')
|
||||
files.append({'path': str(rel), 'bytes': len(content), 'sha256': hashlib.sha256(content).hexdigest()})
|
||||
entries.append((str(rel), content))
|
||||
files.sort(key=lambda entry: entry['path'])
|
||||
if review_fingerprint(files) != values['sourceHash']:
|
||||
raise ValueError('Source fingerprint mismatch')
|
||||
for name, content in entries:
|
||||
target = root / name
|
||||
target.parent.mkdir(parents=True, exist_ok=True)
|
||||
target.write_bytes(content)
|
||||
(root / '.webup-review-manifest.json').write_text(json.dumps({'sourceHash': values['sourceHash'], 'files': files}))
|
||||
return {'imported': len(files), 'sourceHash': values['sourceHash']}
|
||||
|
||||
|
||||
def review_validate(root):
|
||||
manifest = json.loads((root / '.webup-review-manifest.json').read_text())
|
||||
deadline = time.monotonic() + 100
|
||||
steps, issues, html_files = [], [], []
|
||||
|
||||
def run(label, command, seconds=35):
|
||||
remaining = min(seconds, deadline - time.monotonic())
|
||||
if remaining <= 0:
|
||||
return {'name': label, 'status': 'NOT_RUN', 'reason': 'Review time budget exhausted'}
|
||||
env = {'PATH': os.environ.get('PATH', ''), 'HOME': str(root), 'CI': 'true', 'LANG': 'C.UTF-8', 'npm_config_cache': '/tmp/npm-cache'}
|
||||
with tempfile.TemporaryFile() as output:
|
||||
try:
|
||||
process = subprocess.Popen(command, cwd=root, env=env, stdout=output, stderr=subprocess.STDOUT, start_new_session=True)
|
||||
try:
|
||||
process.wait(timeout=remaining)
|
||||
status = 'PASSED' if process.returncode == 0 else 'FAILED'
|
||||
except subprocess.TimeoutExpired:
|
||||
os.killpg(process.pid, signal.SIGKILL); process.wait(); status = 'TIMEOUT'
|
||||
output.seek(0); captured = output.read(6001)
|
||||
return {'name': label, 'command': command, 'status': status, 'exitCode': process.returncode,
|
||||
'output': review_redact(captured[:6000].decode('utf-8', 'replace')), 'truncated': len(captured) > 6000}
|
||||
except OSError:
|
||||
return {'name': label, 'status': 'UNSUPPORTED', 'reason': 'Required tool is unavailable in review image'}
|
||||
|
||||
class Document(HTMLParser):
|
||||
def __init__(self):
|
||||
super().__init__(convert_charrefs=True); self.assets = []; self.title = False; self.ids=set(); self.anchors=[]
|
||||
def handle_starttag(self, tag, attrs):
|
||||
attrs = dict(attrs)
|
||||
if attrs.get('id'): self.ids.add(attrs['id'])
|
||||
if tag=='a' and attrs.get('href','').startswith('#') and len(attrs['href'])>1: self.anchors.append(unquote(attrs['href'][1:]))
|
||||
if tag == 'title': self.title = True
|
||||
if tag in {'script', 'img', 'source'} and attrs.get('src'): self.assets.append(attrs['src'])
|
||||
if tag == 'link' and attrs.get('rel') == 'stylesheet': self.assets.append(attrs.get('href', ''))
|
||||
|
||||
for entry in manifest['files']:
|
||||
file = root / entry['path']
|
||||
if file.suffix.lower() in {'.html', '.htm'}:
|
||||
html_files.append(entry['path'])
|
||||
try:
|
||||
document = Document(); document.feed(file.read_text()); document.close()
|
||||
if not document.title: issues.append({'file': entry['path'], 'message': 'HTML document has no title'})
|
||||
for anchor in document.anchors:
|
||||
if anchor not in document.ids: issues.append({'file': entry['path'], 'message': 'Missing local anchor target: #'+anchor})
|
||||
for asset in document.assets:
|
||||
url = urlsplit(asset)
|
||||
if url.scheme or url.netloc: continue
|
||||
target = ((root if url.path.startswith('/') else file.parent) / unquote(url.path).lstrip('/')).resolve()
|
||||
if not target.is_relative_to(root.resolve()) or not target.is_file():
|
||||
issues.append({'file': entry['path'], 'message': 'Missing or unsafe local asset: ' + asset})
|
||||
except (ValueError, OSError, UnicodeError):
|
||||
issues.append({'file': entry['path'], 'message': 'HTML could not be parsed as UTF-8'})
|
||||
if html_files:
|
||||
steps.append({'name': 'html-assets', 'status': 'FAILED' if issues else 'PASSED', 'files': html_files, 'issues': issues})
|
||||
if (root / 'package.json').is_file():
|
||||
try:
|
||||
package = json.loads((root / 'package.json').read_text())
|
||||
if any((root / lock).exists() for lock in ('pnpm-lock.yaml', 'yarn.lock', 'bun.lockb', 'bun.lock')):
|
||||
steps.append({'name': 'dependencies', 'status': 'UNSUPPORTED', 'reason': 'This runner currently requires npm; another lockfile was detected'})
|
||||
else:
|
||||
install = run('dependencies', ['npm', 'ci' if (root / 'package-lock.json').is_file() else 'install', '--no-audit', '--no-fund'], 45)
|
||||
steps.append(install)
|
||||
if install['status'] == 'PASSED':
|
||||
scripts = package.get('scripts', {})
|
||||
for name in ('build', 'test'):
|
||||
steps.append(run(name, ['npm', 'run', name], 35) if scripts.get(name) else {'name': name, 'status': 'NOT_CONFIGURED'})
|
||||
except (ValueError, OSError):
|
||||
steps.append({'name': 'package-json', 'status': 'FAILED', 'reason': 'Invalid package.json'})
|
||||
elif any((root / f['path']).suffix == '.py' for f in manifest['files']):
|
||||
steps.append(run('python-syntax', ['python3', '-I', '-m', 'compileall', '-q', '.']))
|
||||
steps.append({'name': 'python-tests', 'status': 'NOT_RUN', 'reason': 'Dependency installation and Python integration tests are not configured in this runner'})
|
||||
elif html_files:
|
||||
steps.append({'name': 'build', 'status': 'NOT_APPLICABLE', 'reason': 'Plain static HTML has no build step'})
|
||||
else:
|
||||
steps.append({'name': 'build', 'status': 'UNSUPPORTED', 'reason': 'Project build system is not supported by this runner'})
|
||||
changed = []
|
||||
for entry in manifest['files']:
|
||||
file = root / entry['path']
|
||||
try:
|
||||
if file.is_symlink() or hashlib.sha256(file.read_bytes()).hexdigest() != entry['sha256']: changed.append(entry['path'])
|
||||
except OSError: changed.append(entry['path'])
|
||||
return {'sourceHash': manifest['sourceHash'], 'steps': steps, 'sourceChangedByBuild': changed,
|
||||
'browser': {'status': 'NOT_RUN', 'reason': 'No browser or visual checks executed'},
|
||||
'hasUi': bool(html_files), 'executionScope': 'Separate gVisor review Pod; development workspace was not modified'}
|
||||
@@ -0,0 +1,325 @@
|
||||
"""Restricted OCI runtime controller. This module never builds or runs code on the gateway host."""
|
||||
import hmac
|
||||
import json
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
import html
|
||||
from datetime import datetime, timezone
|
||||
|
||||
from fastapi import APIRouter, Header, HTTPException
|
||||
from kubernetes import client
|
||||
from kubernetes.client.rest import ApiException
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
router = APIRouter()
|
||||
NAMESPACE = os.environ.get("RUNTIME_NAMESPACE", "platform-runtimes")
|
||||
TOKEN = os.environ.get("RUNTIME_TOKEN", "")
|
||||
RUNTIME = os.environ.get("RUNTIME_CLASS", "gvisor")
|
||||
locks = {}
|
||||
guard = threading.Lock()
|
||||
|
||||
|
||||
def auth(authorization):
|
||||
if len(TOKEN) < 32 or not hmac.compare_digest(authorization or "", "Bearer " + TOKEN):
|
||||
raise HTTPException(401, "Unauthorized runtime request")
|
||||
|
||||
|
||||
def identifier(value):
|
||||
try:
|
||||
return uuid.UUID(str(value)).hex
|
||||
except ValueError:
|
||||
raise HTTPException(400, "Invalid resource ID")
|
||||
|
||||
|
||||
class Service(BaseModel):
|
||||
id: uuid.UUID
|
||||
name: str = Field(pattern=r"^[a-z][a-z0-9-]{0,62}$")
|
||||
image: str = Field(pattern=r"^[a-zA-Z0-9][a-zA-Z0-9._/:@-]{0,499}$")
|
||||
command: list[str] = Field(max_length=30)
|
||||
environment: dict[str, str] = Field(max_length=100)
|
||||
port: int = Field(ge=1, le=65535)
|
||||
cpu_millis: int = Field(ge=50, le=64000)
|
||||
memory_mb: int = Field(ge=64, le=262144)
|
||||
replicas: int = Field(ge=1, le=20)
|
||||
health_path: str = Field(pattern=r"^/[^\s]{0,499}$|^$")
|
||||
|
||||
|
||||
class Deployment(BaseModel):
|
||||
projectId: uuid.UUID
|
||||
deploymentId: uuid.UUID
|
||||
services: list[Service] = Field(min_length=1, max_length=100)
|
||||
branding: dict[str, str] = Field(default_factory=dict)
|
||||
|
||||
|
||||
class ProjectState(BaseModel):
|
||||
action: str = Field(pattern=r'^(SUSPEND|RESUME)$')
|
||||
operationId: uuid.UUID
|
||||
|
||||
|
||||
def blocked(core,project):
|
||||
try:
|
||||
return core.read_namespaced_config_map('control-'+project,NAMESPACE).data.get('state')=='SUSPEND'
|
||||
except ApiException as e:
|
||||
if e.status==404:
|
||||
return False
|
||||
raise
|
||||
|
||||
|
||||
def branding_config(port, branding):
|
||||
if branding.get('hostedFooterEnabled') != 'true':
|
||||
return None
|
||||
text = html.escape(branding.get('hostedFooterText', '由 WebUp Cloud 提供服务'), quote=True)
|
||||
url = branding.get('hostedFooterLink', 'https://webup.run')
|
||||
if url and not url.startswith('https://'):
|
||||
raise HTTPException(400, 'Footer link must use HTTPS')
|
||||
label = ('<a href="' + html.escape(url, quote=True) + '" target="_blank" rel="noopener noreferrer" style="color:inherit!important;text-decoration:none!important">' + text + '</a>') if url else text
|
||||
footer = '<div id="webup-powered-by" style="box-sizing:border-box!important;clear:both!important;text-align:center!important;padding:14px 10px!important;font:12px/1.6 system-ui,sans-serif!important;color:#8a96a8!important;background:transparent!important">' + label + '</div>'
|
||||
footer = footer.replace('$','$').replace('\\','\').replace('\n',' ').replace('\r',' ')
|
||||
return '''map $http_upgrade $connection_upgrade { default upgrade; '' close; }
|
||||
server {
|
||||
listen 8081;
|
||||
server_tokens off;
|
||||
client_max_body_size 32m;
|
||||
location / {
|
||||
proxy_pass http://127.0.0.1:PORT;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection $connection_upgrade;
|
||||
proxy_set_header Accept-Encoding "";
|
||||
proxy_read_timeout 3600s;
|
||||
sub_filter_once on;
|
||||
sub_filter '</body>' 'FOOTER</body>';
|
||||
}
|
||||
}
|
||||
'''.replace('PORT',str(port)).replace('FOOTER',footer)
|
||||
|
||||
|
||||
def ensure(method, **kwargs):
|
||||
try:
|
||||
return method(**kwargs)
|
||||
except ApiException as exc:
|
||||
if exc.status != 409:
|
||||
raise
|
||||
|
||||
|
||||
def network_policy(project):
|
||||
return {"apiVersion": "networking.k8s.io/v1", "kind": "NetworkPolicy", "metadata": {"name": "project-" + project},
|
||||
"spec": {"podSelector": {"matchLabels": {"platform/project": project}}, "policyTypes": ["Ingress", "Egress"],
|
||||
"ingress": [{"from": [{"podSelector": {"matchLabels": {"platform/project": project}}},
|
||||
{"namespaceSelector": {"matchLabels": {"kubernetes.io/metadata.name": "ingress-nginx"}},
|
||||
"podSelector": {"matchLabels": {"app.kubernetes.io/name": "ingress-nginx"}}}]}],
|
||||
"egress": [{"to": [{"podSelector": {"matchLabels": {"platform/project": project}}}]},
|
||||
{"to": [{"namespaceSelector": {"matchLabels": {"kubernetes.io/metadata.name": "kube-system"}}}],
|
||||
"ports": [{"port": 53, "protocol": "UDP"}, {"port": 53, "protocol": "TCP"}]},
|
||||
{"to": [{"ipBlock": {"cidr": "0.0.0.0/0", "except": ["0.0.0.0/8", "10.0.0.0/8", "100.64.0.0/10", "127.0.0.0/8", "169.254.0.0/16", "172.16.0.0/12", "192.0.0.0/24", "192.168.0.0/16", "198.18.0.0/15", "224.0.0.0/4", "240.0.0.0/4"]}}],
|
||||
"ports": [{"port": 443, "protocol": "TCP"}, {"port": 80, "protocol": "TCP"}]}]}}
|
||||
|
||||
|
||||
def deployment_manifest(project, deployment, service, branding=None):
|
||||
sid = identifier(service.id)
|
||||
labels = {"platform/project": project, "platform/service": sid, "platform/revision": deployment}
|
||||
name = "app-" + sid[:20] + "-" + deployment[:12]
|
||||
security = {"allowPrivilegeEscalation": False, "readOnlyRootFilesystem": True, "capabilities": {"drop": ["ALL"]}}
|
||||
probe = {"httpGet": {"path": service.health_path, "port": service.port}} if service.health_path else {"tcpSocket": {"port": service.port}}
|
||||
container = {"name": "app", "image": service.image, "ports": [{"containerPort": service.port}], "securityContext": security,
|
||||
"envFrom": [{"secretRef": {"name": name}}], "volumeMounts": [{"name": "tmp", "mountPath": "/tmp"}],
|
||||
"resources": {"requests": {"cpu": str(service.cpu_millis) + "m", "memory": str(service.memory_mb) + "Mi"},
|
||||
"limits": {"cpu": str(service.cpu_millis) + "m", "memory": str(service.memory_mb) + "Mi", "ephemeral-storage": "1Gi"}},
|
||||
"readinessProbe": {**probe, "initialDelaySeconds": 2, "periodSeconds": 3, "timeoutSeconds": 2},
|
||||
"livenessProbe": {**probe, "initialDelaySeconds": 30, "periodSeconds": 10, "timeoutSeconds": 3},
|
||||
"startupProbe": {**probe, "periodSeconds": 3, "failureThreshold": 25}}
|
||||
if service.command:
|
||||
container["command"] = service.command
|
||||
manifest = {"apiVersion": "apps/v1", "kind": "Deployment", "metadata": {"name": name, "labels": labels},
|
||||
"spec": {"replicas": service.replicas, "selector": {"matchLabels": labels},
|
||||
"template": {"metadata": {"labels": labels}, "spec": {"runtimeClassName": RUNTIME,
|
||||
"automountServiceAccountToken": False, "enableServiceLinks": False,
|
||||
"securityContext": {"runAsNonRoot": True, "runAsUser": 10001, "runAsGroup": 10001, "seccompProfile": {"type": "RuntimeDefault"}},
|
||||
"containers": [container], "volumes": [{"name": "tmp", "emptyDir": {"sizeLimit": "512Mi"}}]}}}}
|
||||
if branding_config(service.port, branding or {}) is not None:
|
||||
pod = manifest['spec']['template']['spec']
|
||||
pod['volumes'].extend([{'name':'branding-config','configMap':{'name':name+'-branding'}},{'name':'proxy-tmp','emptyDir':{'sizeLimit':'64Mi'}}])
|
||||
pod['containers'].append({'name':'webup-branding','image':os.environ.get('BRANDING_PROXY_IMAGE','nginxinc/nginx-unprivileged:1.28-alpine'),
|
||||
'ports':[{'containerPort':8081}],'securityContext':security,
|
||||
'resources':{'requests':{'cpu':'10m','memory':'16Mi'},'limits':{'cpu':'100m','memory':'64Mi'}},
|
||||
'volumeMounts':[{'name':'branding-config','mountPath':'/etc/nginx/conf.d','readOnly':True},{'name':'proxy-tmp','mountPath':'/tmp'}],
|
||||
'readinessProbe':{'tcpSocket':{'port':8081},'periodSeconds':3},'livenessProbe':{'tcpSocket':{'port':8081},'periodSeconds':10}})
|
||||
return manifest
|
||||
|
||||
|
||||
@router.post("/runtime/deployments/{deployment}")
|
||||
def deploy(deployment: str, request: Deployment, authorization: str | None = Header(default=None)):
|
||||
auth(authorization)
|
||||
did, project = identifier(deployment), identifier(request.projectId)
|
||||
if did != identifier(request.deploymentId):
|
||||
raise HTTPException(400, "Deployment ID mismatch")
|
||||
with guard:
|
||||
lock = locks.setdefault(project, threading.Lock())
|
||||
if not lock.acquire(timeout=1):
|
||||
raise HTTPException(409, "Project deployment in progress")
|
||||
core, apps, networking = client.CoreV1Api(), client.AppsV1Api(), client.NetworkingV1Api()
|
||||
receipt = "release-" + did
|
||||
old = {}
|
||||
try:
|
||||
if blocked(core,project):
|
||||
raise HTTPException(409,'Project runtime is suspended; deployment denied')
|
||||
try:
|
||||
record = core.read_namespaced_config_map(receipt, NAMESPACE)
|
||||
if record.data.get("project") != project:
|
||||
raise HTTPException(409, "Deployment receipt conflict")
|
||||
if record.data.get("status") == "ACTIVE":
|
||||
return {"ready": True, "deploymentId": deployment, "replayed": True}
|
||||
old = json.loads(record.data["selectors"])
|
||||
except ApiException as exc:
|
||||
if exc.status != 404:
|
||||
raise
|
||||
for service in request.services:
|
||||
stable = "svc-" + identifier(service.id)[:20]
|
||||
try:
|
||||
old[stable] = core.read_namespaced_service(stable, NAMESPACE).spec.selector
|
||||
except ApiException as missing:
|
||||
if missing.status != 404:
|
||||
raise
|
||||
old[stable] = None
|
||||
ensure(core.create_namespaced_config_map, namespace=NAMESPACE, body={"metadata": {"name": receipt}, "data": {"project": project, "status": "PENDING", "selectors": json.dumps(old)}})
|
||||
ensure(networking.create_namespaced_network_policy, namespace=NAMESPACE, body=network_policy(project))
|
||||
names = []
|
||||
for service in request.services:
|
||||
manifest = deployment_manifest(project, did, service, request.branding)
|
||||
name = manifest["metadata"]["name"]
|
||||
ensure(core.create_namespaced_secret, namespace=NAMESPACE, body={"metadata": {"name": name}, "type": "Opaque", "stringData": service.environment})
|
||||
proxy_config = branding_config(service.port,request.branding)
|
||||
if proxy_config is not None:
|
||||
ensure(core.create_namespaced_config_map,namespace=NAMESPACE,body={'metadata':{'name':name+'-branding'},'data':{'default.conf':proxy_config}})
|
||||
ensure(apps.create_namespaced_deployment, namespace=NAMESPACE, body=manifest)
|
||||
stable = "svc-" + identifier(service.id)[:20]
|
||||
target_port = 8081 if proxy_config is not None else service.port
|
||||
ensure(core.create_namespaced_service, namespace=NAMESPACE, body={"metadata": {"name": stable, "labels": {"platform/project": project}}, "spec": {"selector": manifest["metadata"]["labels"], "ports": [{"port": service.port, "targetPort": target_port}]}})
|
||||
names.append((name, stable, manifest["metadata"]["labels"], service.port, service.replicas))
|
||||
deadline = time.monotonic() + 90
|
||||
while time.monotonic() < deadline:
|
||||
if all((apps.read_namespaced_deployment(n, NAMESPACE).status.ready_replicas or 0) >= replicas for n, _, _, _, replicas in names):
|
||||
break
|
||||
time.sleep(2)
|
||||
else:
|
||||
return {"ready": False, "error": "New revision did not become ready; previous traffic selectors retained"}
|
||||
for _, stable, labels, port, _ in names:
|
||||
if blocked(core,project):
|
||||
raise HTTPException(409,'Project was suspended during deployment')
|
||||
core.patch_namespaced_service(stable, NAMESPACE, {"spec": {"selector": labels, "ports": [{"port": port, "targetPort":8081 if request.branding.get('hostedFooterEnabled')=='true' else port}]}})
|
||||
core.patch_namespaced_config_map(receipt, NAMESPACE, {"data": {"status": "ACTIVE"}})
|
||||
cleanup_warnings = []
|
||||
for previous in apps.list_namespaced_deployment(NAMESPACE, label_selector="platform/project=" + project).items:
|
||||
if previous.metadata.labels.get("platform/revision") != did and previous.spec.replicas:
|
||||
try:
|
||||
apps.patch_namespaced_deployment(previous.metadata.name, NAMESPACE, {"spec": {"replicas": 0}})
|
||||
except ApiException:
|
||||
cleanup_warnings.append("Could not scale down retained revision " + previous.metadata.name)
|
||||
return {"ready": True, "deploymentId": deployment, "cleanupWarnings": cleanup_warnings, "services": [{"internalAddress": stable + "." + NAMESPACE + ".svc.cluster.local:" + str(port)} for _, stable, _, port, _ in names]}
|
||||
except ApiException:
|
||||
# If switching a multi-service release fails halfway, restore the previously recorded selectors.
|
||||
for stable, selector in old.items():
|
||||
if selector:
|
||||
try:
|
||||
core.patch_namespaced_service(stable, NAMESPACE, {"spec": {"selector": selector}})
|
||||
except ApiException:
|
||||
pass # The control plane retains a failure event for explicit operator recovery.
|
||||
raise HTTPException(503, "Runtime operation failed; check release receipt before retrying") from None
|
||||
finally:
|
||||
lock.release()
|
||||
|
||||
|
||||
def active(project, service):
|
||||
project, service = identifier(project), identifier(service)
|
||||
core = client.CoreV1Api()
|
||||
try:
|
||||
selected = core.read_namespaced_service("svc-" + service[:20], NAMESPACE).spec.selector
|
||||
if selected.get("platform/project") != project or selected.get("platform/service") != service:
|
||||
raise HTTPException(404, "Service not found")
|
||||
return core, selected
|
||||
except ApiException:
|
||||
raise HTTPException(404, "Service not found") from None
|
||||
|
||||
|
||||
@router.get("/runtime/projects/{project}/services/{service}")
|
||||
def status(project: str, service: str, authorization: str | None = Header(default=None)):
|
||||
auth(authorization)
|
||||
core, selected = active(project, service)
|
||||
selector = ",".join(k + "=" + v for k, v in selected.items())
|
||||
pods = core.list_namespaced_pod(NAMESPACE, label_selector=selector).items
|
||||
return {"pods": [{"name": p.metadata.name, "phase": p.status.phase, "node": p.spec.node_name,
|
||||
"ready": all(c.ready for c in (p.status.container_statuses or [])),
|
||||
"restarts": sum(c.restart_count for c in (p.status.container_statuses or []))} for p in pods],
|
||||
"internalAddress": "svc-" + identifier(service)[:20] + "." + NAMESPACE + ".svc.cluster.local"}
|
||||
|
||||
|
||||
@router.get("/runtime/projects/{project}/services/{service}/logs")
|
||||
def logs(project: str, service: str, authorization: str | None = Header(default=None)):
|
||||
auth(authorization)
|
||||
core, selected = active(project, service)
|
||||
pods = core.list_namespaced_pod(NAMESPACE, label_selector=",".join(k + "=" + v for k, v in selected.items())).items
|
||||
return {"logs": [{"pod": p.metadata.name, "output": core.read_namespaced_pod_log(p.metadata.name, NAMESPACE, container="app", tail_lines=200, limit_bytes=64000)} for p in pods[:20]]}
|
||||
|
||||
|
||||
@router.post("/runtime/projects/{project}/services/{service}/restart")
|
||||
def restart(project: str, service: str, authorization: str | None = Header(default=None)):
|
||||
auth(authorization)
|
||||
_, selected = active(project, service)
|
||||
if blocked(client.CoreV1Api(),identifier(project)):
|
||||
raise HTTPException(409,'Project runtime is suspended')
|
||||
apps = client.AppsV1Api()
|
||||
deployments = apps.list_namespaced_deployment(NAMESPACE, label_selector=",".join(k + "=" + v for k, v in selected.items())).items
|
||||
for d in deployments:
|
||||
apps.patch_namespaced_deployment(d.metadata.name, NAMESPACE, {"spec": {"template": {"metadata": {"annotations": {"platform/restarted-at": datetime.now(timezone.utc).isoformat()}}}}})
|
||||
return {"message": "Restart requested", "deployments": len(deployments)}
|
||||
|
||||
|
||||
@router.post('/runtime/projects/{project}/state')
|
||||
def project_state(project: str,request: ProjectState,authorization: str | None = Header(default=None)):
|
||||
auth(authorization)
|
||||
project=identifier(project)
|
||||
core,apps=client.CoreV1Api(),client.AppsV1Api()
|
||||
name='control-'+project
|
||||
ensure(core.create_namespaced_config_map,namespace=NAMESPACE,body={'metadata':{'name':name},'data':{'state':request.action}})
|
||||
core.patch_namespaced_config_map(name,NAMESPACE,{'data':{'state':request.action,'operation':str(request.operationId)}})
|
||||
changed=0
|
||||
services=core.list_namespaced_service(NAMESPACE,label_selector='platform/project='+project).items
|
||||
for service in services:
|
||||
selector=service.spec.selector or {}
|
||||
if not selector:
|
||||
continue
|
||||
for deployment in apps.list_namespaced_deployment(NAMESPACE,label_selector=','.join(k+'='+v for k,v in selector.items())).items:
|
||||
annotations=deployment.metadata.annotations or {}
|
||||
if request.action=='SUSPEND':
|
||||
desired=deployment.spec.replicas or int(annotations.get('platform/desired-replicas','1'))
|
||||
apps.patch_namespaced_deployment(deployment.metadata.name,NAMESPACE,{'metadata':{'annotations':{'platform/desired-replicas':str(desired)}},'spec':{'replicas':0}})
|
||||
else:
|
||||
apps.patch_namespaced_deployment(deployment.metadata.name,NAMESPACE,{'spec':{'replicas':int(annotations.get('platform/desired-replicas','1'))}})
|
||||
changed+=1
|
||||
return {'state':request.action,'changedDeployments':changed,'operationId':str(request.operationId)}
|
||||
|
||||
|
||||
@router.get('/runtime/nodes')
|
||||
def nodes(authorization: str | None = Header(default=None)):
|
||||
auth(authorization)
|
||||
core=client.CoreV1Api()
|
||||
metrics={}
|
||||
try:
|
||||
for m in client.CustomObjectsApi().list_cluster_custom_object('metrics.k8s.io','v1beta1','nodes').get('items',[]):
|
||||
metrics[m['metadata']['name']]=m.get('usage',{})
|
||||
except ApiException:
|
||||
pass
|
||||
pods=core.list_namespaced_pod(NAMESPACE).items
|
||||
result=[]
|
||||
for n in core.list_node().items:
|
||||
conditions=n.status.conditions or []
|
||||
ready=any(c.type=='Ready' and c.status=='True' for c in conditions)
|
||||
info=n.status.node_info
|
||||
result.append({'name':n.metadata.name,'status':'READY' if ready else 'NOT_READY','cpu':n.status.allocatable.get('cpu',''),
|
||||
'memory':n.status.allocatable.get('memory',''),'internalIp':next((a.address for a in n.status.addresses or [] if a.type=='InternalIP'),''),
|
||||
'kubeletVersion':info.kubelet_version,'os':info.os_image,'runtime':info.container_runtime_version,
|
||||
'unschedulable':bool(n.spec.unschedulable),'managedPods':sum(1 for p in pods if p.spec.node_name==n.metadata.name),'usage':metrics.get(n.metadata.name)})
|
||||
return {'items':result,'metricsAvailable':bool(metrics)}
|
||||
@@ -0,0 +1,86 @@
|
||||
import base64
|
||||
import hashlib
|
||||
import io
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
import zipfile
|
||||
import yaml
|
||||
import app
|
||||
from review_tools import review_export, review_import, review_validate
|
||||
|
||||
|
||||
class CodeReviewTests(unittest.TestCase):
|
||||
def test_snapshot_copy_is_separate_stable_and_excludes_credentials(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
source=Path(directory)/'source';target=Path(directory)/'review';source.mkdir();target.mkdir()
|
||||
(source/'index.html').write_text('<title>Test</title><link rel="stylesheet" href="styles.css">')
|
||||
(source/'styles.css').write_text('body { color: red; }')
|
||||
(source/'.env').write_text('API_KEY=never-copy')
|
||||
(source/'.npmrc').write_text('//registry:token=never-copy')
|
||||
snapshot=review_export(source)
|
||||
self.assertEqual(['index.html','styles.css'],[f['path'] for f in snapshot['files']])
|
||||
self.assertEqual(snapshot['sourceHash'],review_export(source)['sourceHash'])
|
||||
review_import(target,snapshot)
|
||||
self.assertFalse((target/'.env').exists())
|
||||
result=review_validate(target)
|
||||
self.assertEqual('PASSED',result['steps'][0]['status'])
|
||||
self.assertEqual('NOT_RUN',result['browser']['status'])
|
||||
(target/'index.html').write_text('copy was changed')
|
||||
self.assertNotEqual((source/'index.html').read_text(),(target/'index.html').read_text())
|
||||
with self.assertRaises(ValueError):review_import(target,snapshot)
|
||||
|
||||
def test_missing_asset_is_a_real_failure_not_an_ai_claim(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
source=Path(directory)/'source';target=Path(directory)/'target';source.mkdir();target.mkdir()
|
||||
(source/'index.html').write_text('<title>Missing CSS</title><link rel="stylesheet" href="absent.css">')
|
||||
review_import(target,review_export(source))
|
||||
result=review_validate(target)
|
||||
self.assertEqual('FAILED',result['steps'][0]['status'])
|
||||
self.assertIn('absent.css',result['steps'][0]['issues'][0]['message'])
|
||||
|
||||
def test_missing_navigation_target_is_not_accepted(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
source=Path(directory)/'source';target=Path(directory)/'target';source.mkdir();target.mkdir()
|
||||
(source/'index.html').write_text('<title>Incomplete</title><a href="#projects">Projects</a>')
|
||||
review_import(target,review_export(source));result=review_validate(target)
|
||||
self.assertEqual('FAILED',result['steps'][0]['status'])
|
||||
self.assertIn('#projects',result['steps'][0]['issues'][0]['message'])
|
||||
|
||||
def test_archive_tampering_and_zip_slip_are_rejected_before_writes(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
target=Path(directory)/'target';target.mkdir()
|
||||
data=io.BytesIO()
|
||||
with zipfile.ZipFile(data,'w') as archive:archive.writestr('../outside.txt','denied')
|
||||
snapshot={'archive':base64.b64encode(data.getvalue()).decode(),'archiveSha256':hashlib.sha256(data.getvalue()).hexdigest(),'sourceHash':'0'*64}
|
||||
with self.assertRaises(ValueError):review_import(target,snapshot)
|
||||
self.assertEqual([],list(target.iterdir()))
|
||||
self.assertFalse((Path(directory)/'outside.txt').exists())
|
||||
snapshot['archiveSha256']='0'*64
|
||||
with self.assertRaises(ValueError):review_import(target,snapshot)
|
||||
|
||||
def test_review_cleanup_has_no_delete_permission_in_development_namespace(self):
|
||||
root=Path(__file__).parents[1]/'infra/k8s'
|
||||
development=list(yaml.safe_load_all((root/'sandbox-rbac.yaml').read_text()))
|
||||
review=list(yaml.safe_load_all((root/'review-rbac.yaml').read_text()))
|
||||
self.assertFalse(any('delete' in rule['verbs'] for r in development if r['kind']=='Role' for rule in r['rules']))
|
||||
self.assertTrue(any('delete' in rule['verbs'] for r in review if r['kind']=='Role' for rule in r['rules']))
|
||||
self.assertTrue(all(r['metadata']['namespace']=='platform-reviews' for r in review if r['kind']=='Role'))
|
||||
|
||||
def test_large_payload_uses_stdin_instead_of_command_arguments(self):
|
||||
connection=MagicMock();connection.is_open.side_effect=[True,False]
|
||||
connection.peek_stdout.return_value=True;connection.read_stdout.return_value='{"written":192000}'
|
||||
connection.peek_stderr.return_value=False;connection.returncode=0
|
||||
project='00000000-0000-0000-0000-000000000001'
|
||||
with patch.object(app,'TOKEN','x'*32),patch.object(app,'ensure_workspace'),patch.object(app.client,'ApiClient'),patch.object(app,'stream',return_value=connection) as stream:
|
||||
result=app.tool(project,app.Tool(name='write_file',arguments={'path':'big.txt','content':'中'*64000}),'Bearer '+'x'*32)
|
||||
self.assertEqual(192000,result['written'])
|
||||
self.assertLess(len(stream.call_args.kwargs['command'][-1].encode()),128000)
|
||||
encoded=''.join(call.args[0] for call in connection.write_stdin.call_args_list)
|
||||
self.assertEqual('中'*64000,json.loads(base64.b64decode(encoded))['arguments']['content'])
|
||||
|
||||
def test_cleanup_cannot_point_at_development_namespace(self):
|
||||
with patch.object(app,'TOKEN','x'*32),patch.object(app,'REVIEW_NAMESPACE',app.NAMESPACE):
|
||||
with self.assertRaises(app.HTTPException):app.dispose_review('00000000-0000-0000-0000-000000000001','Bearer '+'x'*32)
|
||||
@@ -0,0 +1,15 @@
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
import yaml
|
||||
|
||||
|
||||
class DeploymentPermissionsTests(unittest.TestCase):
|
||||
def test_health_probe_can_list_pods_without_cluster_admin(self):
|
||||
resources=list(yaml.safe_load_all((Path(__file__).parents[1]/'infra/k8s/sandbox-rbac.yaml').read_text()))
|
||||
roles=[item for item in resources if item['kind']=='Role' and item['metadata']['name']=='sandbox-gateway']
|
||||
self.assertEqual(1,len(roles))
|
||||
self.assertTrue(any('pods' in rule['resources'] and 'list' in rule['verbs'] for rule in roles[0]['rules']))
|
||||
self.assertFalse(any('*' in rule['resources'] or '*' in rule['verbs'] for rule in roles[0]['rules']))
|
||||
for item in resources:
|
||||
if item['kind']=='RoleBinding':
|
||||
self.assertEqual('Role',item['roleRef']['kind'])
|
||||
@@ -0,0 +1,75 @@
|
||||
import unittest
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from unittest.mock import Mock, patch
|
||||
from types import SimpleNamespace
|
||||
import uuid
|
||||
import runtime
|
||||
import domain_runtime as routes
|
||||
from fastapi import HTTPException
|
||||
from kubernetes.client.rest import ApiException
|
||||
from domain_runtime import hostname, certificate_ready, owned, upsert, delete_owned
|
||||
|
||||
|
||||
class DomainRuntimeTests(unittest.TestCase):
|
||||
def test_runtime_endpoint_requires_token_before_touching_kubernetes(self):
|
||||
request=routes.DomainRoute(projectId=uuid.uuid4(),serviceId=uuid.uuid4(),hostname='demo.example.com',generation=1)
|
||||
with patch.object(runtime,'TOKEN','x'*32), patch.object(routes.client,'CoreV1Api') as core:
|
||||
with self.assertRaises(HTTPException) as rejected:
|
||||
routes.apply_domain(uuid.uuid4(),request,'Bearer wrong')
|
||||
self.assertEqual(401,rejected.exception.status_code)
|
||||
core.assert_not_called()
|
||||
|
||||
def test_removed_generation_cannot_be_recreated_by_late_request(self):
|
||||
project,domain=uuid.uuid4(),uuid.uuid4()
|
||||
core,network,custom=Mock(),Mock(),Mock()
|
||||
core.read_namespaced_config_map.return_value=SimpleNamespace(data={'project':project.hex,'hostname':'demo.example.com','generation':'4','desired':'REMOVED'})
|
||||
request=routes.DomainRoute(projectId=project,serviceId=uuid.uuid4(),hostname='demo.example.com',generation=3)
|
||||
with patch.object(runtime,'TOKEN','x'*32),patch.object(routes.client,'CoreV1Api',return_value=core),patch.object(routes.client,'NetworkingV1Api',return_value=network),patch.object(routes.client,'CustomObjectsApi',return_value=custom):
|
||||
with self.assertRaises(HTTPException) as rejected:
|
||||
routes.apply_domain(domain,request,'Bearer '+'x'*32)
|
||||
self.assertEqual(409,rejected.exception.status_code)
|
||||
network.create_namespaced_ingress.assert_not_called()
|
||||
custom.create_namespaced_custom_object.assert_not_called()
|
||||
core.patch_namespaced_config_map.assert_not_called()
|
||||
|
||||
def certificate(self):
|
||||
return {'metadata': {'generation': 2}, 'spec': {'dnsNames': ['demo.example.com'], 'secretName': 'domain-tls'}, 'status': {'conditions': [{'type': 'Ready', 'status': 'True', 'observedGeneration': 2}], 'notAfter': (datetime.now(timezone.utc) + timedelta(days=60)).isoformat()}}
|
||||
|
||||
def test_certificate_requires_current_generation_host_and_expiry(self):
|
||||
cert = self.certificate()
|
||||
self.assertTrue(certificate_ready(cert, 'demo.example.com', 'domain-tls'))
|
||||
self.assertFalse(certificate_ready(cert, 'other.example.com', 'domain-tls'))
|
||||
cert['status']['conditions'][0]['observedGeneration'] = 1
|
||||
self.assertFalse(certificate_ready(cert, 'demo.example.com', 'domain-tls'))
|
||||
cert['status']['conditions'][0]['observedGeneration'] = 2
|
||||
cert['status']['notAfter'] = (datetime.now(timezone.utc) - timedelta(seconds=1)).isoformat()
|
||||
self.assertFalse(certificate_ready(cert, 'demo.example.com', 'domain-tls'))
|
||||
|
||||
def test_missing_status_never_means_tls_ready(self):
|
||||
self.assertFalse(certificate_ready({'spec': {'dnsNames': ['demo.example.com'], 'secretName': 'domain-tls'}}, 'demo.example.com', 'domain-tls'))
|
||||
|
||||
def test_foreign_or_newer_generation_resource_cannot_be_changed(self):
|
||||
resource = {'metadata': {'labels': {'platform/domain': 'other'}, 'annotations': {'platform/generation': '1'}}}
|
||||
with self.assertRaises(HTTPException):
|
||||
owned(resource, 'mine', 2)
|
||||
resource['metadata']['labels']['platform/domain'] = 'mine'
|
||||
resource['metadata']['annotations']['platform/generation'] = '3'
|
||||
with self.assertRaises(HTTPException):
|
||||
owned(resource, 'mine', 2)
|
||||
|
||||
def test_already_deleted_resource_is_idempotent(self):
|
||||
read, delete = Mock(side_effect=ApiException(status=404)), Mock()
|
||||
delete_owned(read, delete, 'mine', 1)
|
||||
delete.assert_not_called()
|
||||
|
||||
def test_conflict_does_not_overwrite_foreign_ingress(self):
|
||||
create, patch = Mock(side_effect=ApiException(status=409)), Mock()
|
||||
with self.assertRaises(HTTPException):
|
||||
upsert(create, lambda: {'metadata': {'labels': {'platform/domain': 'other'}}}, patch, {'metadata': {}, 'spec': {}}, 'mine', 1)
|
||||
patch.assert_not_called()
|
||||
|
||||
def test_route_hostname_rejects_injection_and_wildcards(self):
|
||||
self.assertEqual('demo.example.com', hostname('demo.example.com'))
|
||||
for value in ['*.example.com', '127.0.0.1', 'a.example.com;proxy_pass', 'A.example.com', 'https://example.com']:
|
||||
with self.assertRaises(HTTPException):
|
||||
hostname(value)
|
||||
@@ -0,0 +1,32 @@
|
||||
import unittest
|
||||
from fastapi import HTTPException
|
||||
from app import read_exec_output
|
||||
|
||||
|
||||
class Connection:
|
||||
def __init__(self, output, stderr='', code=0):
|
||||
self.output, self.error, self.returncode = output, stderr, code
|
||||
self.open, self.closed = True, False
|
||||
def is_open(self): return self.open
|
||||
def update(self, timeout): self.open = False
|
||||
def peek_stdout(self): return bool(self.output)
|
||||
def read_stdout(self): return self.output
|
||||
def peek_stderr(self): return bool(self.error)
|
||||
def read_stderr(self): return self.error
|
||||
def close(self): self.closed = True
|
||||
|
||||
|
||||
class ExecStreamTests(unittest.TestCase):
|
||||
def test_json_is_preserved_and_stderr_does_not_corrupt_protocol(self):
|
||||
connection=Connection('{"written":30}\n', 'ordinary diagnostic\n')
|
||||
self.assertEqual('{"written":30}\n',read_exec_output(connection,100))
|
||||
self.assertTrue(connection.closed)
|
||||
def test_utf8_output_limit_closes_stream(self):
|
||||
connection=Connection('中'*40)
|
||||
with self.assertRaises(HTTPException):read_exec_output(connection,100)
|
||||
self.assertTrue(connection.closed)
|
||||
def test_nonzero_or_unknown_completion_never_means_success(self):
|
||||
for code in (1,None):
|
||||
connection=Connection('{}',code=code)
|
||||
with self.assertRaises(HTTPException):read_exec_output(connection,100)
|
||||
self.assertTrue(connection.closed)
|
||||
@@ -0,0 +1,91 @@
|
||||
import base64
|
||||
import importlib.util
|
||||
import io
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import types
|
||||
import unittest
|
||||
from unittest.mock import MagicMock, patch
|
||||
import zipfile
|
||||
|
||||
spec = importlib.util.spec_from_file_location("gateway", Path(__file__).with_name("app.py"))
|
||||
gateway = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(gateway)
|
||||
|
||||
|
||||
class GatewayTests(unittest.TestCase):
|
||||
def execute_file_tool(self, root, name, arguments):
|
||||
payload = base64.b64encode(json.dumps({"name": name, "arguments": arguments}).encode()).decode()
|
||||
script = gateway.SCRIPT.replace("pathlib.Path('/workspace')", "pathlib.Path(" + repr(str(root)) + ")")
|
||||
result = subprocess.run([sys.executable, "-X", "utf8", "-c", "PAYLOAD=" + repr(payload) + "\n" + script], capture_output=True, timeout=10)
|
||||
self.assertEqual(0, result.returncode, result.stderr.decode())
|
||||
return json.loads(result.stdout)
|
||||
|
||||
def test_file_roundtrip_and_parent_traversal_rejection(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory) / "workspace"
|
||||
root.mkdir()
|
||||
self.assertIn("written", self.execute_file_tool(root, "write_file", {"path": "src/test.txt", "content": "真实内容"}))
|
||||
self.assertEqual("真实内容", self.execute_file_tool(root, "read_file", {"path": "src/test.txt"})["content"])
|
||||
self.assertIn("error", self.execute_file_tool(root, "write_file", {"path": "../outside.txt", "content": "denied"}))
|
||||
self.assertFalse((Path(directory) / "outside.txt").exists())
|
||||
|
||||
def test_large_file_read_is_rejected(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / "large.txt").write_bytes(b"x" * 64001)
|
||||
self.assertIn("error", self.execute_file_tool(root, "read_file", {"path": "large.txt"}))
|
||||
|
||||
def test_workspace_inspection_requires_nonempty_source_and_excludes_dependencies(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root=Path(directory)
|
||||
(root/'node_modules').mkdir()
|
||||
(root/'node_modules/fake.js').write_text('dependency code')
|
||||
(root/'index.html').write_text('')
|
||||
(root/'.env').write_text('SECRET=must-not-appear')
|
||||
self.assertEqual(0,self.execute_file_tool(root,'workspace_inspect',{})['sourceFiles'])
|
||||
(root/'index.html').write_text('<h1>Actual source</h1>')
|
||||
result=self.execute_file_tool(root,'workspace_inspect',{})
|
||||
self.assertEqual(1,result['sourceFiles'])
|
||||
self.assertTrue(result['complete'])
|
||||
self.assertEqual(['index.html'],[f['path'] for f in result['files']])
|
||||
|
||||
def test_archive_excludes_git_and_dependency_files(self):
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
root = Path(directory)
|
||||
(root / "index.html").write_text("<h1>Test</h1>")
|
||||
(root / "node_modules").mkdir()
|
||||
(root / "node_modules/ignored.js").write_text("ignore")
|
||||
result = self.execute_file_tool(root, "archive_source", {})
|
||||
with zipfile.ZipFile(io.BytesIO(base64.b64decode(result["base64"]))) as archive:
|
||||
self.assertEqual(["index.html"], archive.namelist())
|
||||
|
||||
def test_invalid_identity_and_missing_credentials_fail_closed(self):
|
||||
with self.assertRaises(gateway.HTTPException):
|
||||
gateway.workspace_name("../../host")
|
||||
with patch.object(gateway, "TOKEN", "x" * 32):
|
||||
with self.assertRaises(gateway.HTTPException):
|
||||
gateway.authorize("Bearer wrong")
|
||||
gateway.authorize("Bearer " + "x" * 32)
|
||||
|
||||
def test_created_workspace_has_isolation_and_persistent_data(self):
|
||||
core, net = MagicMock(), MagicMock()
|
||||
core.read_namespaced_pod.return_value = types.SimpleNamespace(status=types.SimpleNamespace(phase="Running", container_statuses=[types.SimpleNamespace(ready=True)]))
|
||||
with patch.object(gateway.app.state, "core", core, create=True), patch.object(gateway.app.state, "net", net, create=True), patch.object(gateway, "IMAGE", "test/isolated-image:local"):
|
||||
gateway.ensure_workspace("00000000-0000-0000-0000-000000000001")
|
||||
pod = core.create_namespaced_pod.call_args.kwargs["body"]["spec"]
|
||||
self.assertEqual("gvisor", pod["runtimeClassName"])
|
||||
self.assertFalse(pod["automountServiceAccountToken"])
|
||||
self.assertTrue(pod["containers"][0]["securityContext"]["readOnlyRootFilesystem"])
|
||||
self.assertEqual(["ALL"], pod["containers"][0]["securityContext"]["capabilities"]["drop"])
|
||||
self.assertIn("persistentVolumeClaim", pod["volumes"][0])
|
||||
policy = net.create_namespaced_network_policy.call_args.kwargs["body"]["spec"]
|
||||
self.assertEqual([], policy["ingress"])
|
||||
self.assertIn("169.254.0.0/16", policy["egress"][1]["to"][0]["ipBlock"]["except"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,25 @@
|
||||
import unittest
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import Mock, patch
|
||||
from fastapi import HTTPException
|
||||
import app as gateway
|
||||
|
||||
|
||||
class ReadinessTests(unittest.TestCase):
|
||||
def test_readiness_requires_auth_and_actual_kubernetes_query(self):
|
||||
core = Mock()
|
||||
with patch.object(gateway, 'TOKEN', 'x' * 32), patch.object(gateway.app.state, 'core', core, create=True):
|
||||
with self.assertRaises(HTTPException) as rejected:
|
||||
gateway.ready('Bearer wrong')
|
||||
self.assertEqual(401, rejected.exception.status_code)
|
||||
core.list_namespaced_pod.assert_not_called()
|
||||
self.assertTrue(gateway.ready('Bearer ' + 'x' * 32)['ready'])
|
||||
core.list_namespaced_pod.assert_called_once()
|
||||
|
||||
def test_kubernetes_failure_is_not_ready(self):
|
||||
core = Mock()
|
||||
core.list_namespaced_pod.side_effect = RuntimeError('fixture')
|
||||
with patch.object(gateway, 'TOKEN', 'x' * 32), patch.object(gateway.app.state, 'core', core, create=True):
|
||||
with self.assertRaises(HTTPException) as failed:
|
||||
gateway.ready('Bearer ' + 'x' * 32)
|
||||
self.assertEqual(503, failed.exception.status_code)
|
||||
@@ -0,0 +1,37 @@
|
||||
import unittest
|
||||
import uuid
|
||||
from runtime import Service, deployment_manifest, network_policy, branding_config
|
||||
|
||||
|
||||
class RuntimeTests(unittest.TestCase):
|
||||
def test_powered_by_footer_only_uses_html_substitution(self):
|
||||
config=branding_config(3000,{'hostedFooterEnabled':'true','hostedFooterText':'由 WebUp Cloud 提供服务','hostedFooterLink':'https://webup.run'})
|
||||
self.assertIn('sub_filter',config)
|
||||
self.assertIn('proxy_pass http://127.0.0.1:3000',config)
|
||||
self.assertIn('proxy_set_header Upgrade',config)
|
||||
self.assertNotIn('sub_filter_types application/json',config)
|
||||
self.assertIsNone(branding_config(3000,{'hostedFooterEnabled':'false'}))
|
||||
|
||||
def test_footer_text_is_escaped(self):
|
||||
config=branding_config(3000,{'hostedFooterEnabled':'true','hostedFooterText':"<script>alert('$x')</script>",'hostedFooterLink':'https://webup.run'})
|
||||
self.assertNotIn('<script>',config)
|
||||
self.assertNotIn('$x',config)
|
||||
|
||||
def test_oci_runtime_is_non_root_and_health_checked(self):
|
||||
service = Service(id=uuid.uuid4(), name="api", image="example.invalid/app:v1", command=[], environment={"KEY": "value"}, port=3000, cpu_millis=250, memory_mb=256, replicas=2, health_path="/health")
|
||||
spec = deployment_manifest(uuid.uuid4().hex, uuid.uuid4().hex, service)["spec"]
|
||||
pod = spec["template"]["spec"]
|
||||
self.assertEqual(2, spec["replicas"])
|
||||
self.assertFalse(pod["automountServiceAccountToken"])
|
||||
self.assertTrue(pod["securityContext"]["runAsNonRoot"])
|
||||
self.assertEqual("gvisor", pod["runtimeClassName"])
|
||||
self.assertIn("startupProbe", pod["containers"][0])
|
||||
self.assertIn("secretRef", pod["containers"][0]["envFrom"][0])
|
||||
self.assertNotIn("hostPath", str(pod))
|
||||
|
||||
def test_project_network_does_not_allow_other_projects(self):
|
||||
project = uuid.uuid4().hex
|
||||
policy = network_policy(project)["spec"]
|
||||
self.assertEqual(project, policy["podSelector"]["matchLabels"]["platform/project"])
|
||||
self.assertEqual(project, policy["ingress"][0]["from"][0]["podSelector"]["matchLabels"]["platform/project"])
|
||||
self.assertIn("169.254.0.0/16", policy["egress"][2]["to"][0]["ipBlock"]["except"])
|
||||
Reference in new issue
Block a user