File size: 4,386 Bytes
d276c3c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | #!/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"}
@clone_app.app.middleware("http")
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,
)
|