Download tasks/blueapron/environment/reference/harbor_entry.py from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 4.39 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/blueapron/environment/reference/harbor_entry.py
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/blueapron/environment/reference/harbor_entry.py
-
curl -L -o harbor_entry.py https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/blueapron/environment/reference/harbor_entry.py
4.39 kB
| #!/usr/bin/env python3 | |
| from __future__ import annotations | |
| import os | |
| import random | |
| import sys | |
| import time | |
| from pathlib import Path | |
| from typing import Any | |
| ROOT = Path(__file__).resolve().parent | |
| sys.path.insert(0, str(ROOT / "vendor")) | |
| import uvicorn # noqa: E402 | |
| SITE_ID = "blueapron" | |
| DATABASE_NAME = "blueapron.sqlite3" | |
| # The session identity this adapter runs with. Harbor reads the cookie off the | |
| # wire; this names the same contract in the executable and fails the runtime | |
| # closed if the projected backend ever stops scoping the session to this site | |
| # or stops hardening it. | |
| blueapron_session_cookie = "websitebench-blueapron-session" | |
| blueapron_session_policy = dict(httponly=True, samesite="lax", path="/") | |
| PAYMENT_ADAPTER = "disabled" | |
| payment_profile = "no-payment-external-effects" | |
| FORBIDDEN_PAYMENT_ADAPTERS = ("local-sandbox", "stripe-test", "live") | |
| MAIL_TRANSPORT = "disabled-public-dom-verification" | |
| migration_version = 1 | |
| status_code = 200 | |
| required_invariants = ( | |
| "blueapron.offline-network-closure", | |
| "blueapron.server-authoritative-plan-and-total", | |
| ) | |
| if PAYMENT_ADAPTER in FORBIDDEN_PAYMENT_ADAPTERS: | |
| raise SystemExit("Blue Apron payments must remain disabled") | |
| contract_headers = { | |
| "X-WebsiteBench-Site": SITE_ID, | |
| "X-WebsiteBench-Database": DATABASE_NAME, | |
| "X-WebsiteBench-Migration": str(migration_version), | |
| "X-WebsiteBench-Payment-Adapter": PAYMENT_ADAPTER, | |
| "X-WebsiteBench-Mail-Transport": MAIL_TRANSPORT, | |
| } | |
| for name in ("HOST", "PORT", "DATA_DIR", "SEED", "TZ"): | |
| if not os.environ.get(name): | |
| raise SystemExit(f"{name} is required") | |
| data_dir = Path(os.environ["DATA_DIR"]) | |
| if not data_dir.is_absolute(): | |
| raise SystemExit("DATA_DIR must be absolute") | |
| data_dir.mkdir(parents=True, exist_ok=True) | |
| try: | |
| random.seed(int(os.environ["SEED"])) | |
| except ValueError: | |
| raise SystemExit("SEED must be an integer") from None | |
| if hasattr(time, "tzset"): | |
| time.tzset() | |
| os.environ["WEBSITEBENCH_SITE_BACKEND_DATABASE"] = str(data_dir / DATABASE_NAME) | |
| os.environ["WEBSITEBENCH_SITE_BACKEND_RUNTIME"] = str(ROOT / "backend/runtime.json") | |
| os.environ["WEBSITEBENCH_LOCAL_HTTP_COOKIE"] = "1" | |
| os.environ["WEBSITEBENCH_DEPLOYMENT_PROFILE"] = "offline-harbor" | |
| os.environ["WEBSITEBENCH_PAYMENT_PROFILE"] = PAYMENT_ADAPTER | |
| os.environ["WEBSITEBENCH_DATA_DIR"] = str(data_dir) | |
| os.environ["PYTHONDONTWRITEBYTECODE"] = "1" | |
| for name in ( | |
| "STRIPE_SECRET_KEY", | |
| "STRIPE_WEBHOOK_SECRET", | |
| "STRIPE_TEST_SECRET_KEY", | |
| "STRIPE_TEST_WEBHOOK_SECRET", | |
| ): | |
| os.environ.pop(name, None) | |
| sys.dont_write_bytecode = True | |
| sys.path.insert(0, str(ROOT / "clone")) | |
| from websitebench.site_backend.runtime import load_runtime # noqa: E402 | |
| _session_contract = load_runtime(ROOT / "backend" / "runtime.json") | |
| if _session_contract.cookie_name.removeprefix("__Host-") != blueapron_session_cookie: | |
| raise SystemExit("Blue Apron session cookie is not scoped to this site") | |
| _session_options = _session_contract.cookie_options | |
| if not _session_options["httponly"] or str( | |
| _session_options["samesite"] | |
| ).lower() != blueapron_session_policy["samesite"]: | |
| raise SystemExit("Blue Apron session cookie is not hardened") | |
| if _session_options["path"] != blueapron_session_policy["path"]: | |
| raise SystemExit("Blue Apron session cookie is not host-scoped to the root path") | |
| import app as clone_app # noqa: E402 | |
| from starlette.responses import JSONResponse # noqa: E402 | |
| HEALTH_PATHS = {"/healthz", "/__websitebench/health"} | |
| async def harbor_contract(request: Any, call_next: Any): | |
| response = ( | |
| JSONResponse({"status": "ok"}) | |
| if request.url.path in HEALTH_PATHS | |
| else await call_next(request) | |
| ) | |
| for name, value in contract_headers.items(): | |
| response.headers[name] = value | |
| if request.url.path not in HEALTH_PATHS: | |
| response.headers["X-WebsiteBench-Invariants"] = ",".join(required_invariants) | |
| configured_status = int( | |
| os.environ.get("WEBSITEBENCH_HARBOR_STATUS_CODE", str(status_code)) | |
| ) | |
| if configured_status != 200 and request.url.path not in HEALTH_PATHS: | |
| response.status_code = configured_status | |
| return response | |
| if __name__ == "__main__": | |
| uvicorn.run( | |
| clone_app.app, | |
| host=os.environ["HOST"], | |
| port=int(os.environ["PORT"]), | |
| log_level="warning", | |
| timeout_graceful_shutdown=2, | |
| ) | |