-
Notifications
You must be signed in to change notification settings - Fork 537
Add production build123d backend #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zachdive
wants to merge
1
commit into
master
Choose a base branch
from
dog-ocicat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| __pycache__/ | ||
| *.pyc | ||
| .pytest_cache/ | ||
| .venv/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| FROM python:3.11-slim | ||
|
|
||
| ENV PYTHONDONTWRITEBYTECODE=1 \ | ||
| PYTHONUNBUFFERED=1 | ||
|
|
||
| RUN apt-get update \ | ||
| && apt-get install -y --no-install-recommends \ | ||
| libgl1 \ | ||
| libglib2.0-0 \ | ||
| libxrender1 \ | ||
| tini \ | ||
| && rm -rf /var/lib/apt/lists/* | ||
|
|
||
| WORKDIR /app | ||
| COPY requirements.txt . | ||
| RUN pip install --no-cache-dir --upgrade pip \ | ||
| && pip install --no-cache-dir -r requirements.txt | ||
|
|
||
| COPY app.py . | ||
|
|
||
| RUN useradd --create-home --shell /usr/sbin/nologin exporter | ||
| USER exporter | ||
|
|
||
| EXPOSE 8080 | ||
| ENTRYPOINT ["/usr/bin/tini", "--"] | ||
| CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8080"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| import base64 | ||
| import json | ||
| import os | ||
| import pathlib | ||
| import resource | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import textwrap | ||
| from typing import Literal | ||
|
|
||
| from fastapi import FastAPI, Header, HTTPException, Response | ||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| ExportFormat = Literal["stl", "step", "brep", "preview"] | ||
|
|
||
| MAX_CODE_LENGTH = int(os.getenv("BUILD123D_MAX_CODE_LENGTH", "200000")) | ||
| DEFAULT_TIMEOUT_MS = int(os.getenv("BUILD123D_EXPORT_TIMEOUT_MS", "45000")) | ||
| DEFAULT_MAX_OUTPUT_BYTES = int( | ||
| os.getenv("BUILD123D_MAX_OUTPUT_BYTES", str(25 * 1024 * 1024)) | ||
| ) | ||
| DEFAULT_MAX_PREVIEW_PARTS = int(os.getenv("BUILD123D_MAX_PREVIEW_PARTS", "64")) | ||
| MEMORY_LIMIT_MB = int(os.getenv("BUILD123D_MEMORY_LIMIT_MB", "2048")) | ||
| TOKEN = os.getenv("BUILD123D_EXPORT_TOKEN", "") | ||
|
|
||
|
|
||
| class ExportRequest(BaseModel): | ||
| code: str = Field(min_length=1, max_length=MAX_CODE_LENGTH) | ||
| format: ExportFormat | ||
| timeoutMs: int = Field(default=DEFAULT_TIMEOUT_MS, ge=1000, le=120000) | ||
| maxOutputBytes: int = Field( | ||
| default=DEFAULT_MAX_OUTPUT_BYTES, | ||
| ge=1024, | ||
| le=100 * 1024 * 1024, | ||
| ) | ||
| maxPreviewParts: int = Field(default=DEFAULT_MAX_PREVIEW_PARTS, ge=1, le=256) | ||
|
|
||
|
|
||
| app = FastAPI(title="CADAM build123d exporter") | ||
|
|
||
|
|
||
| def _require_token(authorization: str | None) -> None: | ||
| if not TOKEN: | ||
| return | ||
| expected = f"Bearer {TOKEN}" | ||
| if authorization != expected: | ||
| raise HTTPException(status_code=401, detail="Unauthorized") | ||
|
|
||
|
|
||
| def _extension(format: ExportFormat) -> str: | ||
| return "step" if format == "step" else format | ||
|
|
||
|
|
||
| def _mime_type(format: ExportFormat) -> str: | ||
| if format == "preview": | ||
| return "application/json" | ||
| if format == "stl": | ||
| return "model/stl" | ||
| if format == "step": | ||
| return "model/step" | ||
| return "application/octet-stream" | ||
|
|
||
|
|
||
| def _runner_source( | ||
| source_path: pathlib.Path, | ||
| output_path: pathlib.Path, | ||
| format: ExportFormat, | ||
| max_preview_parts: int, | ||
| ) -> str: | ||
| return textwrap.dedent( | ||
| f""" | ||
| import base64 | ||
| import importlib.util | ||
| import json | ||
| import pathlib | ||
| import socket | ||
| import sys | ||
| import traceback | ||
|
|
||
| try: | ||
| import build123d | ||
|
|
||
| def _blocked_network(*args, **kwargs): | ||
| raise RuntimeError("Network access is disabled during build123d export") | ||
|
|
||
| socket.create_connection = _blocked_network | ||
| socket.socket = _blocked_network | ||
|
|
||
| source_path = pathlib.Path({str(source_path)!r}) | ||
| output_path = pathlib.Path({str(output_path)!r}) | ||
| spec = importlib.util.spec_from_file_location("adam_build123d_model", source_path) | ||
| if spec is None or spec.loader is None: | ||
| raise RuntimeError("Could not load build123d source") | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| gen_step = getattr(module, "gen_step", None) | ||
| if not callable(gen_step): | ||
| raise RuntimeError("build123d source must define gen_step()") | ||
| shape = gen_step() | ||
| if shape is None: | ||
| raise RuntimeError("gen_step() returned None") | ||
|
|
||
| export_format = {format!r} | ||
| if export_format == "preview": | ||
| def color_tuple(value): | ||
| if value is None: | ||
| return None | ||
| if hasattr(value, "to_tuple"): | ||
| return [float(v) for v in value.to_tuple()] | ||
| try: | ||
| return [float(v) for v in value] | ||
| except TypeError: | ||
| return None | ||
|
|
||
| def collect_leaves(node, inherited_color=None, inherited_label="part"): | ||
| color = getattr(node, "color", None) or inherited_color | ||
| label = getattr(node, "label", None) or inherited_label | ||
| children = tuple(getattr(node, "children", ()) or ()) | ||
| if children: | ||
| leaves = [] | ||
| for index, child in enumerate(children): | ||
| child_label = getattr(child, "label", None) or f"{{label}}_{{index + 1}}" | ||
| leaves.extend(collect_leaves(child, color, child_label)) | ||
| return leaves | ||
| return [(node, color, label)] | ||
|
|
||
| root_stl_path = output_path.with_suffix(".root.stl") | ||
| build123d.export_stl(shape, root_stl_path) | ||
| parts = [] | ||
| for index, (part, part_color, part_label) in enumerate(collect_leaves(shape)[:{max_preview_parts}]): | ||
| part_path = output_path.with_suffix(f".part-{{index}}.stl") | ||
| build123d.export_stl(part, part_path) | ||
| parts.append({{ | ||
| "label": str(part_label or f"part_{{index + 1}}"), | ||
| "color": color_tuple(part_color), | ||
| "stl": base64.b64encode(part_path.read_bytes()).decode("ascii"), | ||
| }}) | ||
| output_path.write_text(json.dumps({{ | ||
| "rootStl": base64.b64encode(root_stl_path.read_bytes()).decode("ascii"), | ||
| "parts": parts, | ||
| }}), encoding="utf8") | ||
| elif export_format == "step": | ||
| build123d.export_step(shape, output_path) | ||
| elif export_format == "brep": | ||
| build123d.export_brep(shape, output_path) | ||
| else: | ||
| build123d.export_stl(shape, output_path) | ||
| except Exception: | ||
| traceback.print_exc() | ||
| sys.exit(1) | ||
| """ | ||
| ) | ||
|
|
||
|
|
||
| def _limit_child_resources(max_output_bytes: int, timeout_ms: int): | ||
| def set_limit(kind: int, limits: tuple[int, int]) -> None: | ||
| try: | ||
| resource.setrlimit(kind, limits) | ||
| except (OSError, ValueError): | ||
| pass | ||
|
|
||
| def apply_limits() -> None: | ||
| memory_bytes = MEMORY_LIMIT_MB * 1024 * 1024 | ||
| cpu_seconds = max(1, int(timeout_ms / 1000) + 2) | ||
| set_limit(resource.RLIMIT_AS, (memory_bytes, memory_bytes)) | ||
| set_limit(resource.RLIMIT_CPU, (cpu_seconds, cpu_seconds)) | ||
| set_limit( | ||
| resource.RLIMIT_FSIZE, | ||
| (max_output_bytes * 2, max_output_bytes * 2), | ||
| ) | ||
|
|
||
| return apply_limits | ||
|
|
||
|
|
||
| @app.get("/healthz") | ||
| def healthz() -> dict[str, str]: | ||
| return {"status": "ok"} | ||
|
|
||
|
|
||
| @app.post("/export") | ||
| def export_model( | ||
| request: ExportRequest, | ||
| authorization: str | None = Header(default=None), | ||
| ) -> Response: | ||
| _require_token(authorization) | ||
|
|
||
| temp_dir = pathlib.Path(tempfile.mkdtemp(prefix="adam-build123d-")) | ||
| source_path = temp_dir / "model.py" | ||
| output_path = temp_dir / f"model.{_extension(request.format)}" | ||
| runner_path = temp_dir / "export_model.py" | ||
|
|
||
| try: | ||
| source_path.write_text(request.code, encoding="utf8") | ||
| runner_path.write_text( | ||
| _runner_source( | ||
| source_path, | ||
| output_path, | ||
| request.format, | ||
| request.maxPreviewParts, | ||
| ), | ||
| encoding="utf8", | ||
| ) | ||
|
|
||
| result = subprocess.run( | ||
| [sys.executable, str(runner_path)], | ||
| cwd=temp_dir, | ||
| stdin=subprocess.DEVNULL, | ||
| stdout=subprocess.DEVNULL, | ||
| stderr=subprocess.PIPE, | ||
| text=True, | ||
| timeout=request.timeoutMs / 1000, | ||
| env={"PATH": os.getenv("PATH", "")}, | ||
| preexec_fn=_limit_child_resources( | ||
| request.maxOutputBytes, | ||
| request.timeoutMs, | ||
| ), | ||
| ) | ||
| if result.returncode != 0: | ||
| message = result.stderr[-8000:].strip() or "build123d export failed" | ||
| raise HTTPException(status_code=422, detail=message) | ||
|
|
||
| output = output_path.read_bytes() | ||
| if len(output) > request.maxOutputBytes: | ||
| raise HTTPException( | ||
| status_code=413, | ||
| detail="build123d export exceeded maximum output size", | ||
| ) | ||
|
|
||
| return Response( | ||
| content=output, | ||
| media_type=_mime_type(request.format), | ||
| headers={"Cache-Control": "no-store"}, | ||
| ) | ||
| except subprocess.TimeoutExpired: | ||
| raise HTTPException(status_code=422, detail="build123d export timed out") | ||
| finally: | ||
| shutil.rmtree(temp_dir, ignore_errors=True) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| build123d==0.10.0 | ||
| fastapi==0.115.6 | ||
| uvicorn[standard]==0.32.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
BUILD123D_EXPORT_TOKENis unset or empty,_require_tokenreturns immediately with no auth check — any caller can execute arbitrary Python code. The service executes LLM-generated code in a subprocess, so an unauthenticated endpoint is a high-impact gap. Additionally, the!=string comparison is not constant-time, making the token enumerable via timing side-channel when the service is reachable. Both issues compound: a misconfigured deployment (empty token) gives full access, and a correctly-configured one leaks timing information. Usehmac.compare_digestfor the comparison, and raise a startup error rather than silently skipping auth when no token is configured.