diff --git a/.env.example b/.env.example index e0073b4fb..fb8123bc7 100644 --- a/.env.example +++ b/.env.example @@ -251,7 +251,8 @@ GITHUB_APP_SLUG=openship-io # Set these in the deployed API environment. Both Compose stacks load this # root .env; they do NOT load apps/api/.env.saas (used by dev:saas). # Use the production account's credentials for the production SaaS. -# OBLIEN_API_URL=https://api.oblien.com +# Optional complete API mount override. Unset uses the Oblien SDK default. +# OBLIEN_API_URL=https://your-proxy.example/api/v1 # OBLIEN_CLIENT_ID= # OBLIEN_CLIENT_SECRET= # Optional public base-image pack for NEW managed Cloud spaces only. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7036b3fe6..6c3f05b42 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -118,6 +118,7 @@ jobs: test/e2e/mail-fail2ban-db.e2e.test.ts test/e2e/mail-dovecot-security.e2e.test.ts test/e2e/mail-state-upgrade.e2e.test.ts + test/e2e/mail-backup-roundtrip.e2e.test.ts # Separate API processes and databases exercise both move directions, # recovery, authentication and Desktop relays on every PR. No live # server, provider account or outbound email is used. @@ -149,6 +150,7 @@ jobs: command: >- RUN_DOCKER_E2E=1 bun run --cwd apps/api test:e2e test/e2e/platform-import-data.e2e.test.ts + test/e2e/platform-import-project.e2e.test.ts # Separate independent daemons keep real-Docker coverage within the job # budget. Tests remain serial inside each daemon to preserve isolation. - suite: Compose cutover and rollback diff --git a/.github/workflows/release-gate.yml b/.github/workflows/release-gate.yml index fee238c02..0a225dfb5 100644 --- a/.github/workflows/release-gate.yml +++ b/.github/workflows/release-gate.yml @@ -226,18 +226,24 @@ jobs: # https://github.com/actions/runner-images/issues/15005 runs-on: ${{ matrix.scope == 'fast' && 'ubuntu-22.04' || 'ubuntu-latest' }} # Files remain serial within each disposable daemon. Split the growing - # ordinary suite across two hosts so it fits the gate without relaxing tests. + # ordinary suite across four hosts so it fits the gate without relaxing tests. timeout-minutes: ${{ matrix.scope == 'fast' && 45 || 30 }} strategy: fail-fast: false matrix: include: - scope: fast - label: fast 1/2 - shard: 1/2 + label: fast 1/4 + shard: 1/4 - scope: fast - label: fast 2/2 - shard: 2/2 + label: fast 2/4 + shard: 2/4 + - scope: fast + label: fast 3/4 + shard: 3/4 + - scope: fast + label: fast 4/4 + shard: 4/4 - scope: heavy label: heavy shard: "" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index c6f37c80c..e6dac58bb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1076,7 +1076,7 @@ jobs: fi else gh release create "${TAG}" \ - --title "Openship ${TAG}" \ + --title "OpenShip ${TAG}" \ --notes-file "${NOTES_FILE}" \ ${PRERELEASE_FLAG} \ "${CORE_ASSETS[@]}" diff --git a/.gitignore b/.gitignore index 691f3db59..5c5f64566 100644 --- a/.gitignore +++ b/.gitignore @@ -65,3 +65,6 @@ pnpm-debug.log* .react-router .wrangler next-env.d.ts + +# Local review screenshots and generated previews (not product assets) +/artifacts/ diff --git a/CHANGELOG.md b/CHANGELOG.md index d8e5ee0e3..0d57b1d29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,34 +1,176 @@ # Changelog -All notable changes to Openship. Versions follow [semver](https://semver.org); +All notable changes to OpenShip. Versions follow [semver](https://semver.org); the in-app updater surfaces critical advisories from `release-advisories.json`. Runtime security notices use the signed feed published from `security-advisories.json`. ## Unreleased +### Authorization + +- Require administrative authority for admin operations across HTTP, SDK, native and + MCP access. Ordinary organization membership no longer grants host command execution; + token grants remain bounded by the user's current built-in role (#1783). + +### Cloud billing + +- Open checkout only when its payment URL is ready, with a visible fallback button. + Recover unfinished Cloud purchases when reopening setup instead of creating duplicate + unpaid spaces. Show pending-plan warnings in the Plans & billing sidebar entry. + +### Backups + +- Back up verified existing empty Docker volumes and bind directories without blocking + populated sibling sources. Reject missing sources before capture and preserve failures + for unavailable backing storage (#1767). + +### CLI + +- Run the container-to-host connectivity probe with Node.js, matching the API image, + so `openship up` and `openship doctor` can diagnose the host channel without Bun. + +### Instance relocation + +- Fence event-stream admission while moving the controller so a late subscriber cannot + keep the source draining indefinitely. Restore stream access when the controller resumes, + and link server preparation to its build view while deployment reads remain available. + +## 0.9.12 + +**Critical update** for build-secret protection, database upgrade repairs and +backup/restore reliability. Includes all changes since 0.9.11, with reviewed +source-image update PRs, platform imports, mail improvements and a translated +website. + ### Build security - Pass generated Docker build environments through temporary BuildKit secrets instead of embedding values in Dockerfiles or image history. Mask configured values in streamed logs, error summaries and failure notifications, including split and multiline output. Preserve Cloud worker limits, parallel server builds and cleanup after cancellation. +- Attribute REST audit events to the personal access token used, and audit token + reads through the existing authorization path. +- Preserve private edge-vhost permissions and verify access with unprivileged + workers. Keep tunnel hostnames out of unsupported password-protection flows. -### Image updates +### Runtime and source updates - Keep image services with missing deployment digests visible as unverified. Inspect the running image through the existing deployment runtime to detect moved tags, with bounded reads and no deployment-row writes. Show unavailable update status without an update badge. +- Propose Dockerfile and Compose image changes through reviewed GitHub pull + requests. Check repository access first and guide users to reconnect or update + their GitHub installation when the required permissions are missing. +- Apply security fixes to managed runtime versions instead of repeatedly refreshing + an unchanged image. Distinguish a runtime that is absent from a failed probe. + +### Deployments and infrastructure + +- Allow webhook and CLI deployments to approve reviewed Compose source changes + through the shared approval flow. +- Keep branch and Compose-path controls available after an unsuccessful first + scan, reject blank required build commands, and offer Static Site for folder uploads. +- Keep token-authenticated repository clones on HTTPS even when a server's global + Git configuration rewrites GitHub URLs to SSH. +- Recover orphaned builds through shared worker ownership, preserve application + runtime/log access when adding services, and scope shared image-export paths. +- Scale managed build-cache budgets with available disk and expose server cache + settings. Improve local image exports without sharing another build's output. +- Let edge upgrades finish safely after an SSH disconnect. Preserve owned mounts + in imported Cloud workloads and explain host paths that cannot be imported safely. +- Support hosted Jobs on Manage-connected SSH servers. Match BullMQ repeatable + jobs by name so changing a cron schedule removes the previous schedule. +- Accept bounded YAML aliases in imported GitHub workflows, show the latest run + for every workflow, and avoid duplicate GitHub-owned workflow webhook payloads. +- Recover failed installed-CLI updates and verify release installation and updates + on disposable VPS instances before publishing images and packages. ### Domains +- Configure static HTML, JSON or text responses on a domain through its normal + route editor, including maintenance and verification responses without an app container. - Avoid rewriting unchanged edge route files and repeating remote reads during a registration. Preserve exact file contents and permissions while retaining route validation, reloads, certificate activation and transactional rollback. - **DigitalOcean DNS** — connect a DigitalOcean token in Settings → Connections to preview and apply a custom domain's records and issue DNS-verified HTTPS automatically, as with - Cloudflare. Each record Openship creates is paired with an `_openship-owner` TXT marker, - so removing a domain deletes only Openship's own records. + Cloudflare. Each record OpenShip creates is paired with an `_openship-owner` TXT marker, + so removing a domain deletes only OpenShip's own records. + +### Imports, backups and database upgrades + +- Improve platform imports, including Railway and Vercel setup, source and + destination review, progress reporting and cancellation. +- Keep migration environment fields visible while entering missing secrets, with + a plain section heading instead of a nested disclosure that collapses during editing. +- Preserve source project boundaries during platform imports: one project contains + its native services, private DNS, encrypted service environments and separate volumes. + Keep service environment values scoped correctly during source builds too. +- Carry imported custom domains and aliases through the existing migration routing + flow, pending normal verification. Show DNS cutover guidance and replacement + notices for Railway and Vercel URLs in review and saved migration progress. +- Restore portable instance exports with their server credentials and workspace + ownership. Normalize restored data for the destination schema and keep the + instance import and server selection views consistent. +- Repair missing workspace and security-advisory fields on upgrades, including + databases whose earlier migrations were already marked as applied. +- Serialize backup execution per destination to prevent connection exhaustion. + Recover orphaned Cloud backup activities through the existing ownership checks. +- Complete local mail backups and portable restores through the shared backup + orchestration, including target-server mail configuration and real mail round trips. +- Prevent browser autofill of backup destination credentials and keep destination + menus visible outside their cards. + +### Mail + +- Preserve mail logs and Fail2ban ban history across engine updates, and remove + Dovecot's stale PID file before startup. +- List all signed-in mailboxes and keep account menus usable when they overflow. +- Correct webmail message colors in light/dark themes and when printing, and + refresh security dependencies. +- Protect support intake and add mailbox sender blocking. + +### Assistants + +- Reuse managed agent setup for custom API connections and report the actual + runtime setup state. +- Find agent CLIs installed outside the macOS GUI PATH, restore public BuildKit + pulls, and correct scoped Claude startup. +- Allow reviewed repository patches to delete regular files through the existing + review and validation flow. +- Add OpenShip plugins for Codex and Claude Code. + +### Dashboard, billing and website + +- Keep the shared destination dropdown available with one server or Cloud plan, + with Add server in its menu across deployment and migration flows. +- Refresh deployment state after navigation and browser resume. Unify project + settings and shared environment controls with the existing service views. +- Keep plan overview, usage and payments together under Plans & billing, align + Cloud plan presentation across the website and dashboard, and retain payment + recovery for expired renewals. +- Improve deployment, verification and cancellation emails, and standardize the + Referral experience. +- Translate website content into the existing supported languages, with localized + URLs, navigation and product screenshots. Correct Arabic layout and preserve + English framework names. Documentation remains in English. +- Add migration guidance for teams moving an existing app, group website + navigation, and provide a direct Start on Cloud action. +- Use the BytesMap SDK for unified Cloud telemetry and remove PostHog. + +### Upgrade notes + +- Update the API (including all replicas), dashboard and Desktop app together. + This release includes control-plane database migrations; keep a current + instance/database backup before upgrading. +- Build-secret protection applies to new builds. Rebuild older application images + to use the updated build flow; upgrading does not rewrite existing images. +- Dockerfile and Compose image pins remain controlled by the repository. Review + and merge a source-update PR when the fix requires a source change, then deploy + that revision. Refreshing an unchanged pin does not change its runtime version. +- Update the mail engine as well to receive mail startup, log persistence and + restore fixes. ## 0.9.11 diff --git a/SECURITY.md b/SECURITY.md index 87c2474ab..29b19ad67 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -57,6 +57,12 @@ violation. ## Scope +Organization owners and administrators can perform host administration. Ordinary +members retain read/write access but cannot perform operations requiring `admin`, +including server shell execution. Restricted users require explicit resource grants; +scoped tokens also respect their user's current built-in role. Host commands execute +as the server's configured account, so delegate server administration accordingly. + In scope — all Openship components: - Managed Openship Cloud diff --git a/apps/api/.env.saas.example b/apps/api/.env.saas.example index a4f391f91..0c1556ce6 100644 --- a/apps/api/.env.saas.example +++ b/apps/api/.env.saas.example @@ -31,7 +31,8 @@ BETTER_AUTH_SECRET=change-me-in-production INTERNAL_TOKEN=change-me-32-byte-random-hex # Required for cloud deployments. -OBLIEN_API_URL=https://api.oblien.com +# Leave unset to use the installed SDK's default API version. +# OBLIEN_API_URL=https://your-proxy.example/api/v1 OBLIEN_CLIENT_ID= OBLIEN_CLIENT_SECRET= # Optional: enable only after this software preset is published on your provider. diff --git a/apps/api/assets/geoip/GeoLite2-Country.mmdb b/apps/api/assets/geoip/GeoLite2-Country.mmdb index 53584f011..50f78498c 100644 Binary files a/apps/api/assets/geoip/GeoLite2-Country.mmdb and b/apps/api/assets/geoip/GeoLite2-Country.mmdb differ diff --git a/apps/api/package.json b/apps/api/package.json index 6c8dfc4ef..80b6158d5 100644 --- a/apps/api/package.json +++ b/apps/api/package.json @@ -1,6 +1,6 @@ { "name": "@repo/api", - "version": "0.9.11", + "version": "0.9.12", "license": "Apache-2.0", "private": true, "type": "module", diff --git a/apps/api/scripts/cloud-readiness.ts b/apps/api/scripts/cloud-readiness.ts index 1c2330b94..0a483cae5 100644 --- a/apps/api/scripts/cloud-readiness.ts +++ b/apps/api/scripts/cloud-readiness.ts @@ -1,4 +1,5 @@ /** Read-only release checks. Does not create tokens, checkouts, or resources. */ +import { oblienApiBaseUrl, oblienApiUrl } from "@repo/adapters"; import { runtimeTarget } from "@repo/core"; import { assertOblienEntitlementMatchesSubscription, OblienBillingApi } from "@repo/platform/engine/lib/oblien-billing-api"; import { OBLIEN_WEBHOOK_EVENTS, oblienWebhookUrl } from "@repo/platform/engine/lib/oblien-webhook-config"; @@ -7,13 +8,13 @@ const results: Array<{ check: string; ok: boolean; skipped?: boolean; detail?: s const record = (check: string, ok: boolean, detail?: string) => results.push({ check, ok, ...(detail ? { detail } : {}) }); const clientId = process.env.OBLIEN_CLIENT_ID; const clientSecret = process.env.OBLIEN_CLIENT_SECRET; -const apiBase = process.env.OBLIEN_API_URL ?? "https://api.oblien.com"; +const apiBase = oblienApiBaseUrl(process.env.OBLIEN_API_URL); record("Cloud mode", process.env.CLOUD_MODE === "true"); record("Oblien credentials configured", Boolean(clientId && clientSecret)); record("Webhook secret configured", Boolean(process.env.OBLIEN_WEBHOOK_SECRET)); record("Subscription purchases enabled", process.env.BILLING_ENABLED === "true"); -const billing = new OblienBillingApi({ clientId, clientSecret, baseUrl: apiBase }); +const billing = new OblienBillingApi({ clientId, clientSecret, baseUrl: apiBase.toString() }); const checks = await Promise.allSettled([ (async () => { await billing.assertResellerSupport(); @@ -35,13 +36,13 @@ const checks = await Promise.allSettled([ (async () => { const callback = oblienWebhookUrl(process.env.OBLIEN_WEBHOOK_URL, runtimeTarget.api); if (!clientId || !clientSecret) throw new Error("Oblien credentials are missing"); - const response = await fetch(`${apiBase.replace(/\/+$/, "")}/webhooks`, { + const response = await fetch(oblienApiUrl(apiBase, "/webhooks"), { headers: { "X-Client-ID": clientId, "X-Client-Secret": clientSecret }, redirect: "error", signal: AbortSignal.timeout(15_000), }); if (!response.ok) throw new Error(`Webhook registry HTTP ${response.status}`); const body = await response.json() as { success: boolean; webhooks?: Array<{ url: string; active: boolean; namespace?: string | null; events: string[]; secret?: string | null }> }; - const webhook = body.success && body.webhooks?.find((item) => item.url === callback && !item.namespace && item.active); + const webhook = body.success ? body.webhooks?.find((item) => item.url === callback && !item.namespace && item.active) : undefined; const missing = OBLIEN_WEBHOOK_EVENTS.filter((event) => !webhook || !webhook.events.includes(event)); const maskedSecret = typeof webhook?.secret === "string" && /^[*•*…]+$/u.test(webhook.secret); const matchingSecret = Boolean(webhook?.secret && webhook.secret === process.env.OBLIEN_WEBHOOK_SECRET); @@ -56,7 +57,7 @@ const checks = await Promise.allSettled([ })(), (async () => { if (!clientId || !clientSecret) throw new Error("Oblien credentials are missing"); - const response = await fetch(`${apiBase.replace(/\/+$/, "")}/namespaces?limit=1`, { + const response = await fetch(oblienApiUrl(apiBase, "/namespaces?limit=1"), { headers: { "X-Client-ID": clientId, "X-Client-Secret": clientSecret }, redirect: "error", signal: AbortSignal.timeout(15_000), }); diff --git a/apps/api/src/app.ts b/apps/api/src/app.ts index 264549493..0363ab976 100644 --- a/apps/api/src/app.ts +++ b/apps/api/src/app.ts @@ -23,6 +23,7 @@ import { registerControllerLifecycle, } from "./modules/system/instance/controller-state"; import { resumeControllerSockets } from "./lib/ws"; +import { resumeControllerStreams } from "./lib/sse"; import { trackBackgroundWork } from "@repo/platform/engine/lib/background-work"; import { instanceAuthorityGuard } from "./modules/system/instance/authority-guard"; import { quiesceController } from "./modules/system/instance/quiescence"; @@ -408,6 +409,7 @@ async function startControllerBackground(): Promise { backgroundStarted = true; try { resumeControllerSockets(); + resumeControllerStreams(); // ─── Backup job runner + boot reconcile ───────────────────────────── // // One JobRunner powers all backup work — BullMQ when Redis is diff --git a/apps/api/src/lib/route-permission.ts b/apps/api/src/lib/route-permission.ts index 39337096d..36deb84ae 100644 --- a/apps/api/src/lib/route-permission.ts +++ b/apps/api/src/lib/route-permission.ts @@ -276,6 +276,8 @@ export interface McpRouteMeta { description: string; /** Override the inferred hint for actions such as destructive migration cutover. */ destructive?: boolean; + /** Whether this tool may interact with external systems; defaults to true. */ + openWorld?: boolean; } export interface PermissionSpec { @@ -474,13 +476,10 @@ export function isPublicSpec(spec: RouteSpec): spec is PublicSpec { * Be precise about what that org-wide check buys, because it is easy to misread as * a defense it is not: it is strict only for a RESTRICTED principal (a scoped * token), which needs an all-GitHub grant to pass. For a session-authenticated - * owner/admin/**member** it is satisfied by bare membership — - * `roleAllowsResourceType` (permission.ts) ignores the action level for those - * roles, so `github:admin` is no higher a bar than `github:read`. The per-repo, - * read-vs-write authority for those principals is enforced ONLY by - * `github-access.ts` at the token funnel and at the mutating service helpers. - * Reading this comment as "the route layer already gates writes" is what let - * GHSA-hp2g-hw7g-f3vm sit behind a `github:admin` tag. + * owner/admin it is satisfied by broad membership authority. Ordinary members + * cannot satisfy an admin action. Per-repo read/write authority still belongs + * to `github-access.ts` at the token funnel and mutating service helpers; an + * org-wide tag never substitutes for that repository-specific check. * Paramless GitHub routes (/home, /status, /repos) also stay on the org-wide * path: `filterToolsForPrincipal` already hides them from a scoped token via * `perm.wildcard`, and their handlers narrow results through diff --git a/apps/api/src/lib/sse.ts b/apps/api/src/lib/sse.ts index 210f16549..469f5dde6 100644 --- a/apps/api/src/lib/sse.ts +++ b/apps/api/src/lib/sse.ts @@ -41,7 +41,12 @@ import { SYSTEM } from "@repo/core"; import { trackBackgroundWork } from "@repo/platform/engine/lib/background-work"; const controllerStreams = new Set(); +let accepting = true; +export function resumeControllerStreams(): void { + accepting = true; +} export function closeControllerStreams(): void { + accepting = false; for (const stream of controllerStreams) stream.abort(); } @@ -182,6 +187,16 @@ export function streamSSE( c: Context, cb: (stream: SSEStreamingApi) => Promise, ) { + // An admitted request can finish its permission/metadata read after the HTTP + // fence closed existing streams. Do not give the drain a new live subscriber. + if (!accepting) { + c.header("Retry-After", "5"); + c.header("Cache-Control", "no-store"); + return c.json({ + code: "INSTANCE_CONTROLLER_INACTIVE", + error: "Your instance is moving. Reconnect after the handoff.", + }, 503); + } const context = currentErrorContext(); // Disable reverse-proxy response buffering. nginx/OpenResty buffer proxied // responses by default, which holds SSE events back until the buffer fills — diff --git a/apps/api/src/modules/billing/billing-capacity-catalog.test.ts b/apps/api/src/modules/billing/billing-capacity-catalog.test.ts index e49d13054..eea7004b3 100644 --- a/apps/api/src/modules/billing/billing-capacity-catalog.test.ts +++ b/apps/api/src/modules/billing/billing-capacity-catalog.test.ts @@ -9,6 +9,7 @@ import type { OblienSubscription } from "@repo/platform/engine/lib/oblien-billin import { savedOffer, savedMetadata, savedMonthlyHobby } from "../../../test/helpers/saved-cloud-offer"; import capacityFixture from "../../../test/fixtures/oblien-capacity-catalog.json"; import { oblienCapacityCatalogSchema } from "@repo/platform/engine/lib/oblien-capacity"; +import { cloudBillingContract } from "@repo/adapters"; const savedPro = (): NonNullable => ({ tierId: "reseller", status: "active", billingInterval: "monthly", periodStart: "2026-09-01T00:00:00Z", periodEnd: "2026-10-01T00:00:00Z", cancelAtPeriodEnd: false, canceledAt: null, @@ -17,7 +18,7 @@ const monthlyPro = (): NonNullable => ({ ...savedPro(), offer: subscriptionOffer("pro", "monthly"), metadata: subscriptionMetadata("pro", "org-a", "ns-a") }); describe("funded Cloud offers and isolated capacity", () => { - it.each([undefined, oblienCapacityCatalogSchema.parse(capacityFixture)])( + it.each([undefined, oblienCapacityCatalogSchema.parse(cloudBillingContract(capacityFixture))])( "publishes only subscription plans without prepaid compute offers (with or without provider capacity)", (provider) => { const catalog = presentCloudPlans("en", provider); diff --git a/apps/api/src/modules/billing/billing-checkout.test.ts b/apps/api/src/modules/billing/billing-checkout.test.ts index 9bfae4ea9..58bfb2d97 100644 --- a/apps/api/src/modules/billing/billing-checkout.test.ts +++ b/apps/api/src/modules/billing/billing-checkout.test.ts @@ -37,7 +37,7 @@ vi.mock("@repo/platform/engine/lib/cloud-workspace-host", () => ({ readCloudWorkspaceHost: async () => ({ provider: null }), })); vi.mock("@repo/platform/engine/lib/oblien-client", () => ({ - getOblienClient: () => ({ workspaces: { getQuota: h.quota } }), + getOblienClient: () => ({ vms: { getQuota: h.quota } }), getOblienBillingApi: () => ({ createCheckout: h.checkout, createPortal: h.portal, diff --git a/apps/api/src/modules/billing/billing-reconcile.test.ts b/apps/api/src/modules/billing/billing-reconcile.test.ts index 5d92b3621..3ee761206 100644 --- a/apps/api/src/modules/billing/billing-reconcile.test.ts +++ b/apps/api/src/modules/billing/billing-reconcile.test.ts @@ -116,15 +116,25 @@ describe("Oblien-managed entitlements", () => { h.balance.mockResolvedValue({ ...state.balance, billingMode: "payg" }); await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ code: "CLOUD_BILLING_BLOCKED" }); }); - it("allows billing recovery after an unpaid renewal without granting compute or credits", async () => { + it.each(["active", "past_due"] as const)("allows billing recovery during a %s unpaid renewal without granting compute or credits", async status => { const state = unpaidMonthlyCloudBilling(); + state.subscription.status = status; h.entitlement.mockResolvedValue(state.entitlement); h.subscription.mockResolvedValue({ namespace: state.entitlement.namespace, subscription: state.subscription }); h.balance.mockResolvedValue(state.balance); const result = await syncOblienEntitlement("org_1", { syncResourceLimits: false }); - expect(result).toMatchObject({ tier: "pro", subscription: { status: "past_due" }, entitlement: { status: "canceled", computeCovered: false } }); + expect(result).toMatchObject({ tier: "pro", subscription: { status }, entitlement: { status: "canceled", computeCovered: false } }); await expect(assertNamespaceHasQuota("org_1")).resolves.toBeUndefined(); await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ statusCode: 402, code: "CLOUD_BILLING_BLOCKED" }); + expect(h.mirror).toHaveBeenCalledWith("workspace_1", "org_1", "os-customer", { + planTierId: "pro", subscriptionStatus: "canceled", + currentPeriodStart: new Date(state.entitlement.periodStart!), + currentPeriodEnd: new Date(state.entitlement.periodEnd!), + }); + // A newer balance response alone must not turn old, uncovered capacity into + // paid compute. Both authoritative reads must confirm the funded month. + h.balance.mockResolvedValue({ ...state.balance, blocking: false, computeCovered: true }); + await expect(assertCloudCanSpend("org_1")).rejects.toMatchObject({ statusCode: 402, code: "CLOUD_BILLING_BLOCKED" }); expect(h.limits).not.toHaveBeenCalled(); expect(h.setQuota).not.toHaveBeenCalled(); expect(h.resetQuota).not.toHaveBeenCalled(); diff --git a/apps/api/src/modules/billing/oblien-billing-api.test.ts b/apps/api/src/modules/billing/oblien-billing-api.test.ts index 16fd45c0e..1723cabb3 100644 --- a/apps/api/src/modules/billing/oblien-billing-api.test.ts +++ b/apps/api/src/modules/billing/oblien-billing-api.test.ts @@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, it, expect, vi } from "vitest"; import { OblienBillingApi, oblienOfferSchema, assertOblienEntitlementMatchesSubscription } from "@repo/platform/engine/lib/oblien-billing-api"; import { BillingOperationSchemas } from "@repo/contracts"; import { Value } from "@sinclair/typebox/value"; +import { providerBillingFixture } from "../../../test/helpers/oblien-vm-billing"; import capacityCatalog from "../../../test/fixtures/oblien-capacity-catalog.json"; import { monthlyCloudBilling, unpaidMonthlyCloudBilling } from "../../../test/helpers/monthly-cloud-offer"; @@ -25,7 +26,7 @@ const subscription = { success: true, namespace: "os-one", subscription: { cancelAtPeriodEnd: true, canceledAt: "2026-09-15T00:00:00Z", } }; function setup(body: unknown, status = 200) { - const fetcher = vi.fn(async (_url: Parameters[0], _init?: RequestInit) => Response.json(body, { status })); + const fetcher = vi.fn(async (_url: Parameters[0], _init?: RequestInit) => Response.json(providerBillingFixture(body), { status })); const api = new OblienBillingApi({ clientId: "test-id", clientSecret: "test-secret", fetch: fetcher as unknown as typeof fetch }); return { api, fetcher }; } @@ -47,7 +48,7 @@ describe("Oblien billing SDK and transport contract", () => { await api.createCheckout({ namespace: "os-one", kind: "subscription", billingInterval: "yearly", offer: { ...monthlyCloudBilling().subscription.offer!, unitAmount: 12000 }, metadata, affiliateBuyer, affiliateCode: "AF-0123456789", successUrl: "https://app.openship.io", cancelUrl: "https://app.openship.io", idempotencyKey: "referral-transport" }); - expect(fetcher).toHaveBeenCalledExactlyOnceWith("https://api.oblien.com/billing/checkout", expect.objectContaining({ + expect(fetcher).toHaveBeenCalledExactlyOnceWith("https://api.oblien.com/v1/billing/checkout", expect.objectContaining({ method: "POST", redirect: "error", signal: expect.any(AbortSignal), headers: expect.objectContaining({ "X-Client-ID": "test-id", "X-Client-Secret": "test-secret" }), })); @@ -59,7 +60,7 @@ describe("Oblien billing SDK and transport contract", () => { const live = { success: true, credits_per_dollar: 100, rate_card_id: "live", rates: { cpu_per_min: 1.5, memory_per_gb_min: 0.2, disk_per_gb: 0, network_per_gb: 0.15 } }; const { api, fetcher } = setup({ ...live, compute_rate_cards: [{ chargeEnabled: false, cpu_per_min: 0.001 }] }); expect(await api.getMeteredPricing()).toEqual(live); - expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/pricing/calculator", expect.objectContaining({ method: "GET", redirect: "error", headers: { Accept: "application/json" } })); + expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/pricing/calculator", expect.objectContaining({ method: "GET", redirect: "error", headers: { Accept: "application/json" } })); }); it.each([ { credits_per_dollar: 50, rate_card_id: "new", rates: { cpu_per_min: 1, memory_per_gb_min: 1, disk_per_gb: 0, network_per_gb: 0 } }, @@ -74,14 +75,14 @@ describe("Oblien billing SDK and transport contract", () => { const { api, fetcher } = setup({ success: true, namespace: "os-one", pendingCheckout, wallet: { balance: 12000 } }); expect(await api.getPendingCapacityCheckout("os-one")).toEqual({ success: true, namespace: "os-one", pendingCheckout: { ...pendingCheckout, quote: { id: "quote_pending", namespace: "os-one", paymentSource: "stripe" } } }); - expect(fetcher.mock.calls[0]![0]).toBe("https://api.oblien.com/billing/capacity?namespace=os-one"); + expect(fetcher.mock.calls[0]![0]).toBe("https://api.oblien.com/v1/billing/capacity?namespace=os-one"); await expect(api.getPendingCapacityCheckout("os-other")).rejects.toMatchObject({ code: "OBLIEN_BILLING_NAMESPACE_MISMATCH" }); }); it("cancels a capacity payment with the saved provider quote and retry identity", async () => { const { api, fetcher } = setup({ success: true, namespace: "os-one", pendingCheckout: null }); const input = { quoteId: "quote_pending", idempotencyKey: "cancel-once" }; await expect(api.cancelCapacityCheckout("os-one", input)).resolves.toMatchObject({ pendingCheckout: null }); - expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/billing/capacity/change/cancel", expect.objectContaining({ + expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/billing/capacity/change/cancel", expect.objectContaining({ method: "POST", })); expect(JSON.parse(fetcher.mock.calls[0]![1]!.body as string)).toEqual({ namespace: "os-one", ...input }); @@ -101,7 +102,7 @@ describe("Oblien billing SDK and transport contract", () => { (await api.cancelCheckout("os-one", checkout.id, { idempotencyKey: "cancel-once" })).checkout, ).toEqual(checkout); expect(fetcher).toHaveBeenCalledWith( - "https://api.oblien.com/billing/checkout/bco_original/cancel", + "https://api.oblien.com/v1/billing/checkout/bco_original/cancel", expect.objectContaining({ method: "POST", body: JSON.stringify({ namespace: "os-one", idempotencyKey: "cancel-once" }), @@ -123,7 +124,7 @@ describe("Oblien billing SDK and transport contract", () => { it.each([ { quote: { id: "quote_pending", namespace: "os-other", paymentSource: "stripe" }, checkoutId: "cs_requested", url: null }, { quote: { id: "quote_pending", namespace: "os-one", paymentSource: "stripe" }, checkoutId: "cs_requested", url: "https://evil.example/pay" }, - { quote: { id: "quote_pending", namespace: "os-one", paymentSource: "stripe" }, checkoutId: "cs_requested", url: "https://api.oblien.com/not-a-payment#secret" }, + { quote: { id: "quote_pending", namespace: "os-one", paymentSource: "stripe" }, checkoutId: "cs_requested", url: "https://api.oblien.com/v1/not-a-payment#secret" }, ])("rejects foreign capacity scope and untrusted resume URLs", async pendingCheckout => { await expect(setup({ success: true, namespace: "os-one", pendingCheckout }).api.getPendingCapacityCheckout("os-one")).rejects.toThrow(); }); @@ -135,16 +136,16 @@ describe("Oblien billing SDK and transport contract", () => { }); it("scopes complimentary policy writes to the requested namespace and validates the response", async () => { const policy = { quotaLimit: 3000, overdraft: 60, suspendThreshold: 60, onOverdraftAction: "stop_workspaces" as const }; - const result = { success: true, namespace: "os-one", service: "workspace_vm", ...policy }; + const result = { success: true, namespace: "os-one", service: "vm_compute", ...policy }; const { api, fetcher } = setup(result); expect(await api.setPolicy("os-one", policy)).toEqual(result); - expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/billing/policy/os-one", expect.objectContaining({ method: "PUT", body: JSON.stringify(policy) })); + expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/billing/policy/os-one", expect.objectContaining({ method: "PUT", body: JSON.stringify(providerBillingFixture(policy)) })); await expect(api.setPolicy("os-two", policy)).rejects.toMatchObject({ code: "OBLIEN_BILLING_NAMESPACE_MISMATCH" }); }); it("keeps the same provider reset key on retry and rejects an unscoped reset response", async () => { const { api, fetcher } = setup({ success: true, namespace: "os-one", applied: false }); expect(await api.resetQuota("os-one", "2026-10-25T14:00:00.000Z")).toMatchObject({ applied: false }); - expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/billing/policy/os-one/reset", expect.objectContaining({ method: "POST", body: JSON.stringify({ periodEnd: "2026-10-25T14:00:00.000Z" }) })); + expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/billing/policy/os-one/reset", expect.objectContaining({ method: "POST", body: JSON.stringify({ periodEnd: "2026-10-25T14:00:00.000Z" }) })); await expect(setup({ success: true, applied: true }).api.resetQuota("os-one", "2026-10-25T14:00:00.000Z")) .rejects.toMatchObject({ code: "OBLIEN_BILLING_INVALID_RESPONSE" }); }); @@ -215,7 +216,7 @@ describe("Oblien billing SDK and transport contract", () => { expect(state.checkout).not.toHaveProperty("walletCredits"); expect(state.checkout).not.toHaveProperty("paymentId"); expect(fetcher.mock.calls[0]![0]).toBe( - "https://api.oblien.com/billing/checkout/cs_requested?namespace=os-one", + "https://api.oblien.com/v1/billing/checkout/cs_requested?namespace=os-one", ); await expect(api.getCheckout("os-other", "cs_requested")).rejects.toMatchObject({ code: "OBLIEN_BILLING_NAMESPACE_MISMATCH", @@ -251,7 +252,7 @@ describe("Oblien billing SDK and transport contract", () => { it("authenticates server requests and validates the returned customer namespace", async () => { const { api, fetcher } = setup(entitlement); expect((await api.getEntitlement("os-one")).quota.used).toBe(-50); - expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/billing/entitlement?namespace=os-one", expect.objectContaining({ + expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/billing/entitlement?namespace=os-one", expect.objectContaining({ headers: expect.objectContaining({ "X-Client-ID": "test-id", "X-Client-Secret": "test-secret" }), redirect: "error", })); await expect(api.getEntitlement("os-two")).rejects.toMatchObject({ code: "OBLIEN_BILLING_NAMESPACE_MISMATCH" }); @@ -275,22 +276,24 @@ describe("Oblien billing SDK and transport contract", () => { idempotencyKey: "checkout-123", }; await api.createCheckout(input); - expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/billing/checkout", expect.objectContaining({ method: "POST" })); + expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/billing/checkout", expect.objectContaining({ method: "POST" })); expect(JSON.parse(String(fetcher.mock.calls[0]![1]!.body))).toEqual({ ...input, allowPromotionCodes: true }); }); - it("accepts the exact Oblien promotion checkout and preserves its opaque ID", async () => { + it("keeps the hosted payment page unversioned while sending checkout requests to /v1", async () => { const checkoutId = "bco_24f15202-6a3d-4b27-b7c2-c3abbc401bee"; const url = `https://api.oblien.com/billing/pay#${"a".repeat(43)}`; - const { api } = setup({ success: true, url, checkoutId }); + const { api, fetcher } = setup({ success: true, url, checkoutId }); await expect(api.createCheckout({ namespace: "os-one", kind: "topup", offer, metadata, successUrl: "https://app.openship.io", cancelUrl: "https://app.openship.io", idempotencyKey: "promotion-checkout" })) .resolves.toMatchObject({ url, checkoutId }); + expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/billing/checkout", expect.objectContaining({ method: "POST" })); }); it.each([ "http://api.oblien.com/billing/pay", "https://api.oblien.com.evil.example/billing/pay", - "https://api.oblien.com/billing/pay?return=https://evil.example", "https://api.oblien.com/other", + "https://api.oblien.com/billing/pay?return=https://evil.example", "https://api.oblien.com/v1/other", "https://api.oblien.com/billing/pay", "https://user@api.oblien.com/billing/pay", "https://api.oblien.com:8443/billing/pay", "https://api.oblien.com/billing/pay/", + "https://api.oblien.com/v1/billing/pay", ])("rejects an invalid promotion checkout URL: %s", async (value) => { const url = value + (value === "https://api.oblien.com/billing/pay" ? "#short" : `#${"a".repeat(43)}`); await expect(setup({ success: true, url, checkoutId: "bco_24f15202-6a3d-4b27-b7c2-c3abbc401bee" }).api.createCheckout({ @@ -553,7 +556,7 @@ describe("Oblien billing SDK and transport contract", () => { const { api, fetcher } = setup({ success: true, namespace: "os-one", url: "https://billing.stripe.com/p/session/test" }); const input = { namespace: "os-one", returnUrl: "https://app.openship.io/billing/overview" }; expect((await api.createPortal(input)).url).toBe("https://billing.stripe.com/p/session/test"); - expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/billing/portal", expect.objectContaining({ method: "POST", body: JSON.stringify(input), redirect: "error", signal: expect.any(AbortSignal) })); + expect(fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/billing/portal", expect.objectContaining({ method: "POST", body: JSON.stringify(input), redirect: "error", signal: expect.any(AbortSignal) })); await expect(api.createPortal({ ...input, namespace: "os-two" })).rejects.toMatchObject({ code: "OBLIEN_BILLING_NAMESPACE_MISMATCH" }); }); it("rejects a legacy owner portal even when its hosted URL is otherwise valid", async () => { @@ -572,7 +575,7 @@ describe("Oblien billing SDK and transport contract", () => { ] as const)("uses the SDK %s method and checks the response namespace", async (method, verb, path) => { const { api, fetcher } = setup(subscription); expect(await api[method]("os-one")).toEqual(subscription); - expect(fetcher).toHaveBeenCalledWith(`https://api.oblien.com${path}`, expect.objectContaining({ + expect(fetcher).toHaveBeenCalledWith(`https://api.oblien.com/v1${path}`, expect.objectContaining({ method: verb, ...(verb === "POST" ? { body: JSON.stringify({ namespace: "os-one" }) } : {}), })); await expect(api[method]("os-two")).rejects.toMatchObject({ code: "OBLIEN_BILLING_NAMESPACE_MISMATCH" }); @@ -612,7 +615,7 @@ describe("Oblien monthly capacity contract", () => { const catalog = await api.assertMonthlyCapacitySupport(); expect(catalog.tariff.usage).toEqual({ activeVcpuHourCents: 3, reservedGiBHourCents: 0.8, retainedGiBMonthCents: 5, monthHours: 720 }); const [url, request] = fetcher.mock.calls[0]!; - expect(String(url)).toBe("https://api.oblien.com/billing/capacity/catalog"); + expect(String(url)).toBe("https://api.oblien.com/v1/billing/capacity/catalog"); expect(request?.headers).not.toHaveProperty("X-Client-Secret"); expect(request?.headers).not.toHaveProperty("X-Client-ID"); }); @@ -656,7 +659,7 @@ describe("Oblien monthly capacity contract", () => { .toThrow(/does not match/); } }); - it.each(["past_due", "unpaid", "paused", "canceled"] as const)("keeps billing readable for an expired paid month and a %s renewal", async status => { + it.each(["active", "past_due", "unpaid", "paused", "canceled"] as const)("keeps billing readable for an expired paid month and a %s renewal", async status => { const state = unpaidMonthlyCloudBilling("org-one", "os-one"); state.subscription.status = status; const entitlement = await setup(state.entitlement).api.getEntitlement("os-one"); @@ -665,9 +668,8 @@ describe("Oblien monthly capacity contract", () => { expect(entitlement).toMatchObject({ status: "canceled", computeCovered: false, capacity: { status: "expired", computeCovered: false } }); expect(entitlement.periodEnd).not.toBe(subscription!.periodEnd); }); - it("still rejects funded, overlapping, foreign or inconsistent unpaid renewal records", () => { + it.each(["active", "past_due"] as const)("still rejects funded, overlapping, foreign or inconsistent %s renewal records", status => { for (const mutate of [ - (state: ReturnType) => { state.subscription.status = "active"; }, (state: ReturnType) => { state.subscription.status = "trialing"; }, (state: ReturnType) => { state.entitlement.status = "active"; }, (state: ReturnType) => { state.entitlement.computeCovered = state.entitlement.capacity!.computeCovered = true; }, @@ -678,9 +680,12 @@ describe("Oblien monthly capacity contract", () => { (state: ReturnType) => { state.subscription.periodStart = "2026-10-15T00:00:00Z"; }, (state: ReturnType) => { state.subscription.periodStart = null; }, (state: ReturnType) => { state.subscription.periodEnd = state.subscription.periodStart; }, + (state: ReturnType) => { state.entitlement.periodStart = state.entitlement.capacity!.periodStart = state.entitlement.periodEnd!; }, (state: ReturnType) => { state.entitlement.capacity!.periodEnd = "2026-10-31T00:00:00Z"; }, ]) { - const state = unpaidMonthlyCloudBilling(); mutate(state); + const state = unpaidMonthlyCloudBilling(); + state.subscription.status = status; + mutate(state); expect(() => assertOblienEntitlementMatchesSubscription(state.entitlement, state.subscription)).toThrow(/does not match/); } }); @@ -692,9 +697,9 @@ describe("Oblien monthly capacity contract", () => { offer: subscription.offer!, metadata: subscription.metadata!, idempotencyKey: "saved-monthly-order", successUrl: "https://app.openship.io/billing/overview", cancelUrl: "https://app.openship.io/billing/plans" }); const [url, request] = fetcher.mock.calls[0]!; - expect(String(url)).toBe("https://api.oblien.com/billing/checkout"); + expect(String(url)).toBe("https://api.oblien.com/v1/billing/checkout"); const sent = JSON.parse(String(request?.body)); - expect(sent.offer).toMatchObject({ unitAmount, credits: 0, billingMode: "monthly", capacity: { vcpus: 4, memoryMb: 16384, diskGb: 128, workspaces: 1 } }); + expect(sent.offer).toMatchObject({ unitAmount, credits: 0, billingMode: "monthly", capacity: { vcpus: 4, memoryMb: 16384, diskGb: 128, vms: 1 } }); expect(sent.offer).not.toHaveProperty("policy"); expect(sent.idempotencyKey).toBe("saved-monthly-order"); expect(sent.allowPromotionCodes).toBe(unitAmount > 0); @@ -763,18 +768,18 @@ describe("Oblien subscription changes", () => { it("delegates all four methods to the official SDK and preserves provider cents and fractional credits", async () => { const preview = setup({ success: true, namespace: "os-one", quote }); expect((await preview.api.previewPlanChange("os-one", input)).quote).toEqual(quote); - expect(preview.fetcher).toHaveBeenCalledWith("https://api.oblien.com/billing/subscription/changes/preview", + expect(preview.fetcher).toHaveBeenCalledWith("https://api.oblien.com/v1/billing/subscription/changes/preview", expect.objectContaining({ method: "POST" })); expect(JSON.parse(String(preview.fetcher.mock.calls[0]![1]!.body))).toEqual({ namespace: "os-one", ...input }); const accept = setup({ success: true, namespace: "os-one", change }, 202); expect((await accept.api.changePlan("os-one", { quoteId: quote.id, idempotencyKey: "confirm-request" })).change.status).toBe("payment_pending"); - expect(accept.fetcher).toHaveBeenLastCalledWith("https://api.oblien.com/billing/subscription/changes", + expect(accept.fetcher).toHaveBeenLastCalledWith("https://api.oblien.com/v1/billing/subscription/changes", expect.objectContaining({ method: "POST" })); expect(JSON.parse(String(accept.fetcher.mock.calls.at(-1)![1]!.body))).toEqual({ namespace: "os-one", quoteId: quote.id, idempotencyKey: "confirm-request" }); await accept.api.getPlanChange("os-one", change.id); - expect(accept.fetcher).toHaveBeenLastCalledWith("https://api.oblien.com/billing/subscription/changes/change_one?namespace=os-one", expect.objectContaining({ method: "GET" })); + expect(accept.fetcher).toHaveBeenLastCalledWith("https://api.oblien.com/v1/billing/subscription/changes/change_one?namespace=os-one", expect.objectContaining({ method: "GET" })); await accept.api.cancelPlanChange("os-one", change.id, "cancel-request"); - expect(accept.fetcher).toHaveBeenLastCalledWith("https://api.oblien.com/billing/subscription/changes/change_one/cancel", + expect(accept.fetcher).toHaveBeenLastCalledWith("https://api.oblien.com/v1/billing/subscription/changes/change_one/cancel", expect.objectContaining({ method: "POST" })); expect(JSON.parse(String(accept.fetcher.mock.calls.at(-1)![1]!.body))).toEqual({ namespace: "os-one", idempotencyKey: "cancel-request" }); }); diff --git a/apps/api/src/modules/billing/oblien-managed-transfer.test.ts b/apps/api/src/modules/billing/oblien-managed-transfer.test.ts index fb2ed26a8..c43a1504f 100644 --- a/apps/api/src/modules/billing/oblien-managed-transfer.test.ts +++ b/apps/api/src/modules/billing/oblien-managed-transfer.test.ts @@ -1,11 +1,12 @@ import { describe, expect, it } from "vitest"; import { Value } from "@sinclair/typebox/value"; import { BillingComputeSchema } from "@repo/contracts"; +import { providerBillingFixture } from "../../../test/helpers/oblien-vm-billing"; import { OblienBillingApi, assertOblienEntitlementMatchesSubscription } from "@repo/platform/engine/lib/oblien-billing-api"; import { monthlyCloudBilling } from "../../../test/helpers/monthly-cloud-offer"; import transfer from "../../../test/fixtures/oblien-managed-transfer.json"; -// SDK 2.10's live response shape, with synthetic counters and no customer data. +// Native provider transfer counters with synthetic data. const unlimited = { ...transfer, unlimited: true, includedBytes: null, includedAvailableBytes: null, availableBytes: null }; async function readTransfer(network: unknown, covered = true) { @@ -14,7 +15,7 @@ async function readTransfer(network: unknown, covered = true) { ...entitlement.capacity, computeCovered: covered, network, } }; const api = new OblienBillingApi({ clientId: "test-id", clientSecret: "test-secret", - fetch: (async () => Response.json(payload)) as typeof fetch }); + fetch: (async () => Response.json(providerBillingFixture(payload))) as typeof fetch }); const result = await api.getEntitlement("os-one"); assertOblienEntitlementMatchesSubscription(result, subscription); return result.capacity!; diff --git a/apps/api/src/modules/billing/oblien-webhook-crypto.test.ts b/apps/api/src/modules/billing/oblien-webhook-crypto.test.ts index d92cc82fe..25c707895 100644 --- a/apps/api/src/modules/billing/oblien-webhook-crypto.test.ts +++ b/apps/api/src/modules/billing/oblien-webhook-crypto.test.ts @@ -64,7 +64,7 @@ describe("deriveOblienEventId", () => { const base: OblienEventEnvelope = { event: "credits.usage", timestamp: "2026-07-21T10:00:00Z", - data: { namespace: "os-abc", workspace_id: "ws_1", period_end: "2026-08-01T00:00:00Z" }, + data: { namespace: "os-abc", vm_id: "ws_1", period_end: "2026-08-01T00:00:00Z" }, }; it("is deterministic for the same payload", () => { diff --git a/apps/api/src/modules/billing/oblien-webhook.controller.test.ts b/apps/api/src/modules/billing/oblien-webhook.controller.test.ts index 360375850..2302b9d73 100644 --- a/apps/api/src/modules/billing/oblien-webhook.controller.test.ts +++ b/apps/api/src/modules/billing/oblien-webhook.controller.test.ts @@ -294,7 +294,7 @@ describe("oblienWebhook — dispatch", () => { describe("durable quota alert receipt", () => { it("returns 503 without a checkpoint when notification storage fails, then retries the same event", async () => { - const body = JSON.stringify({ id: "evt-critical", event: "credits.depleted", data: { namespace: "os-abc", service: "workspace_vm" } }); + const body = JSON.stringify({ id: "evt-critical", event: "credits.depleted", data: { namespace: "os-abc", service: "vm_compute" } }); h.enqueue.mockRejectedValueOnce(new Error("database unavailable")); expect(((await oblienWebhook(makeCtx(body, sign(body), "evt-critical"))) as unknown as JsonResult).status).toBe(503); expect(h.processed.size).toBe(0); @@ -305,7 +305,7 @@ describe("durable quota alert receipt", () => { expect(h.processed.size).toBe(1); }); it("leaves the event retryable when exhaustion activity cannot be recorded", async () => { - const body = JSON.stringify({ id: "evt-activity", event: "credits.depleted", data: { namespace: "os-abc", service: "workspace_vm" } }); + const body = JSON.stringify({ id: "evt-activity", event: "credits.depleted", data: { namespace: "os-abc", service: "vm_compute" } }); h.auditRecord.mockRejectedValueOnce(new Error("activity storage unavailable")); expect(((await oblienWebhook(makeCtx(body, sign(body), "evt-activity"))) as unknown as JsonResult).status).toBe(503); expect(h.processed.size).toBe(0); diff --git a/apps/api/src/modules/cloud/cloud-pages.service.ts b/apps/api/src/modules/cloud/cloud-pages.service.ts index 9530a4f99..f2b18387a 100644 --- a/apps/api/src/modules/cloud/cloud-pages.service.ts +++ b/apps/api/src/modules/cloud/cloud-pages.service.ts @@ -5,7 +5,7 @@ import { authorizeCloudRelayWorkspace } from "./cloud-relay-access"; export async function createCloudPage( ctx: RequestContext, - input: { workspace_id: string; path: string; name: string; slug: string; domain?: string }, + input: { vm_id: string; path: string; name: string; slug: string; domain?: string }, ): Promise { const workspaceId = await authorizeCloudRelayWorkspace(ctx); const namespace = await ensureNamespace(ctx.organizationId, workspaceId); diff --git a/apps/api/src/modules/cloud/cloud-resource.controller.ts b/apps/api/src/modules/cloud/cloud-resource.controller.ts index db9cd5e26..34ac7305e 100644 --- a/apps/api/src/modules/cloud/cloud-resource.controller.ts +++ b/apps/api/src/modules/cloud/cloud-resource.controller.ts @@ -10,9 +10,9 @@ import { authorizeCloudRelayWorkspace } from "./cloud-relay-access"; const id = z.string().regex(/^[a-zA-Z0-9_-]{1,128}$/); const path = z.string().min(1).max(2048); const domain = z.string().min(1).max(253); -const source = z.object({ workspace_id: id, path }).strict(); +const source = z.object({ vm_id: id, path }).strict(); const action = z.discriminatedUnion("kind", [ - z.object({ kind: z.literal("proxy"), workspace: id, port: z.number().int().min(1).max(65535), + z.object({ kind: z.literal("proxy"), vm: id, port: z.number().int().min(1).max(65535), stripPrefix: z.boolean().optional(), path: path.optional() }).strict(), z.object({ kind: z.literal("rewrite"), to: path }).strict(), z.object({ kind: z.literal("redirect"), to: path, status: z.union([z.literal(301), z.literal(302), z.literal(307), z.literal(308)]).optional() }).strict(), diff --git a/apps/api/src/modules/cloud/cloud-saas.controller.ts b/apps/api/src/modules/cloud/cloud-saas.controller.ts index 03810ef70..10adfda66 100644 --- a/apps/api/src/modules/cloud/cloud-saas.controller.ts +++ b/apps/api/src/modules/cloud/cloud-saas.controller.ts @@ -635,11 +635,11 @@ export async function checkEdgeVerification(c: Context) { // ─── Pages proxy ──────────────────────────────────────────────────────────── /** - * POST /api/cloud/pages { workspace_id, path, name, slug, domain? } + * POST /api/cloud/pages { vm_id, path, name, slug, domain? } * * Forwards to Oblien with the caller's namespace token. Oblien * enforces: - * - `workspace_id` must belong to the caller's namespace (or 404) + * - `vm_id` must belong to the caller's namespace (or 404) * - `slug` must be free on the shared zone (or 409 SLUG_TAKEN) * * Returns the raw Oblien SDK shape so the caller's infrastructure adapter @@ -648,18 +648,18 @@ export async function checkEdgeVerification(c: Context) { export async function pagesProxy(c: Context) { const ctx = getRequestContext(c); const body = await c.req.json<{ - workspace_id?: string; + vm_id?: string; path?: string; name?: string; slug?: string; domain?: string; }>(); - if (!body.workspace_id || !body.path || !body.name || !body.slug) { - return c.json({ error: "workspace_id, path, name and slug are required" }, 400); + if (!body.vm_id || !body.path || !body.name || !body.slug) { + return c.json({ error: "vm_id, path, name and slug are required" }, 400); } try { const result = await createCloudPage(ctx, { - workspace_id: body.workspace_id, + vm_id: body.vm_id, path: body.path, name: body.name, slug: body.slug, diff --git a/apps/api/src/modules/deployments/build-pipeline.prebuilt-image.test.ts b/apps/api/src/modules/deployments/build-pipeline.prebuilt-image.test.ts index 4e5d91744..f4df5dde0 100644 --- a/apps/api/src/modules/deployments/build-pipeline.prebuilt-image.test.ts +++ b/apps/api/src/modules/deployments/build-pipeline.prebuilt-image.test.ts @@ -92,7 +92,7 @@ vi.mock("@repo/db", () => ({ listByProject: vi.fn(async () => []), remove: vi.fn(async () => undefined), }, - project: { findById: mocks.findProjectById, update: vi.fn(async () => undefined) }, + project: { findById: mocks.findProjectById, update: vi.fn(async () => undefined), getEnvMap: vi.fn(async () => ({})) }, }, })); diff --git a/apps/api/src/modules/deployments/cancel-is-final.test.ts b/apps/api/src/modules/deployments/cancel-is-final.test.ts index 841363250..062a62169 100644 --- a/apps/api/src/modules/deployments/cancel-is-final.test.ts +++ b/apps/api/src/modules/deployments/cancel-is-final.test.ts @@ -43,7 +43,6 @@ vi.mock("@repo/platform/engine/modules/projects/cleanup-keep-set", () => ({ // Terminal-event side channels, each of which drags in auth/github/mail wiring. vi.mock("@repo/platform/engine/lib/notification-dispatcher", () => ({ notification: { emit: mocks.notify } })); vi.mock("../../lib/audit", () => ({ audit: { recordAsync: vi.fn(), record: vi.fn() } })); -vi.mock("@repo/platform/engine/lib/favicon-detector", () => ({ detectAndStoreFavicon: vi.fn(async () => {}) })); vi.mock("@repo/platform/engine/modules/mail/webmail/webmail-install.service", () => ({ onWebmailDeployed: vi.fn(async () => {}), })); diff --git a/apps/api/src/modules/deployments/compose/build-cancel.test.ts b/apps/api/src/modules/deployments/compose/build-cancel.test.ts index 86d366a08..fcb82ccb5 100644 --- a/apps/api/src/modules/deployments/compose/build-cancel.test.ts +++ b/apps/api/src/modules/deployments/compose/build-cancel.test.ts @@ -4,9 +4,11 @@ import { describe, expect, it, vi } from "vitest"; // Same stubs as build.service.test.ts: the DB row read and the SSE broadcast are // side effects this contract doesn't depend on (and importing the real @repo/db // would risk the PGlite single-instance lock). -const { listByProjectMock } = vi.hoisted(() => ({ listByProjectMock: vi.fn() })); +const { listByProjectMock, getEnvMapMock } = vi.hoisted(() => ({ + listByProjectMock: vi.fn(), getEnvMapMock: vi.fn(async () => ({})), +})); vi.mock("@repo/db", () => ({ - repos: { service: { listByProject: listByProjectMock } }, + repos: { project: { getEnvMap: getEnvMapMock }, service: { listByProject: listByProjectMock } }, })); vi.mock("@repo/platform/engine/modules/deployments/session-manager", () => ({ broadcastServiceStatus: vi.fn(), diff --git a/apps/api/src/modules/deployments/compose/build.service.test.ts b/apps/api/src/modules/deployments/compose/build.service.test.ts index f8e2770d9..9023d7848 100644 --- a/apps/api/src/modules/deployments/compose/build.service.test.ts +++ b/apps/api/src/modules/deployments/compose/build.service.test.ts @@ -1,4 +1,5 @@ import { existsSync, readdirSync } from "node:fs"; +import { encrypt } from "@repo/platform/engine/lib/encryption"; import { readFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { isAbsolute, join } from "node:path"; @@ -12,9 +13,11 @@ import { describe, expect, it, vi } from "vitest"; // status. Both are side effects the inline-build materialization contract does not // depend on — stub them so the test exercises ONLY the materialize/COPY path. // (Importing the real @repo/db would also risk the PGlite single-instance lock.) -const { listByProjectMock } = vi.hoisted(() => ({ listByProjectMock: vi.fn() })); +const { listByProjectMock, getEnvMapMock } = vi.hoisted(() => ({ + listByProjectMock: vi.fn(), getEnvMapMock: vi.fn(async () => ({})), +})); vi.mock("@repo/db", () => ({ - repos: { service: { listByProject: listByProjectMock, startServiceDeploymentTiming: vi.fn(async () => {}) } }, + repos: { project: { getEnvMap: getEnvMapMock }, service: { listByProject: listByProjectMock, startServiceDeploymentTiming: vi.fn(async () => {}) } }, })); vi.mock("@repo/platform/engine/modules/deployments/session-manager", () => ({ broadcastServiceStatus: vi.fn(), @@ -567,6 +570,19 @@ describe("buildComposeImages — inline catalog build materialization", () => { * These pin the producer side: which rows get a narrowed context, and which must not. */ describe("buildComposeImages — declared build context", () => { + it("uses each service's encrypted saved environment for its own build", async () => { + getEnvMapMock.mockResolvedValueOnce({ API_SECRET: encrypt("api-only"), PORT: encrypt("3000") }); + getEnvMapMock.mockResolvedValueOnce({ WORKER_SECRET: encrypt("worker-only"), PORT: encrypt("8000") }); + const { captured } = await run([ + repoService({ id: "api", name: "api", build: "." }), + repoService({ id: "worker", name: "worker", build: "." }), + ], undefined, undefined, { SHARED: "shared" }); + expect(captured.map(({ config }) => config.envVars)).toEqual([ + { SHARED: "shared", API_SECRET: "api-only", PORT: "3000" }, + { SHARED: "shared", WORKER_SECRET: "worker-only", PORT: "8000" }, + ]); + }); + it("#795 carries project env into the posted root-Dockerfile setup without duplicate service env", async () => { const projectEnv = { DATABASE_URI: "postgres://db/app", diff --git a/apps/api/src/modules/deployments/route-issues-warning.test.ts b/apps/api/src/modules/deployments/route-issues-warning.test.ts index ef824d981..662d10f40 100644 --- a/apps/api/src/modules/deployments/route-issues-warning.test.ts +++ b/apps/api/src/modules/deployments/route-issues-warning.test.ts @@ -1,12 +1,11 @@ import { describe, expect, it, vi } from "vitest"; // The function under test is pure; everything else this module reaches (DB, SSE, -// notifications, favicon probing) is a side effect the assertion doesn't want. +// notifications) is a side effect the assertion doesn't want. // `@repo/adapters` stays REAL — `isEdgeDownMessage` is the branch being pinned. vi.mock("@repo/db", () => ({ repos: {} })); vi.mock("@repo/platform/engine/lib/notification-dispatcher", () => ({ notification: {} })); vi.mock("../../lib/audit", () => ({ audit: {} })); -vi.mock("@repo/platform/engine/lib/favicon-detector", () => ({ detectAndStoreFavicon: vi.fn() })); vi.mock("@repo/platform/engine/modules/deployments/session-manager", () => ({})); vi.mock("@repo/platform/engine/modules/mail/webmail/webmail-install.service", () => ({ onWebmailDeployed: vi.fn(), diff --git a/apps/api/src/modules/domains/routing-apply.service.test.ts b/apps/api/src/modules/domains/routing-apply.service.test.ts index c3a3b8933..c63228aff 100644 --- a/apps/api/src/modules/domains/routing-apply.service.test.ts +++ b/apps/api/src/modules/domains/routing-apply.service.test.ts @@ -132,7 +132,7 @@ describe("Cloud Docker route tables", () => { } as Record )[`${container}:${port}`]; if (!published) throw new Error("service port is not published"); - return { workspace: "shared-vm", port: published }; + return { vm: "shared-vm", port: published }; }); }); it("repairs an existing single static container route without rebuilding", async () => { @@ -149,7 +149,7 @@ describe("Cloud Docker route tables", () => { }; domainRepo.listByProject.mockResolvedValue([savedDomain]); domainRepo.findByHostname.mockResolvedValue(savedDomain); - target.mockResolvedValue({ workspace: "shared-vm", port: 30000 }); + target.mockResolvedValue({ vm: "shared-vm", port: 30000 }); await applyCloudRouting({ project: { ...base, workloadType: "static", hasServer: false, port: 3000 } as never, deployment: { @@ -186,9 +186,9 @@ describe("Cloud Docker route tables", () => { ["stack-api.opsh.io", 30003, false], ]); expect(publish.mock.calls.map((call) => call[3].routes[0].action)).toEqual([ - { kind: "proxy", workspace: "shared-vm", port: 30001 }, - { kind: "proxy", workspace: "shared-vm", port: 30002 }, - { kind: "proxy", workspace: "shared-vm", port: 30003 }, + { kind: "proxy", vm: "shared-vm", port: 30001 }, + { kind: "proxy", vm: "shared-vm", port: 30002 }, + { kind: "proxy", vm: "shared-vm", port: 30003 }, ]); }); it("resolves project-level domains to their service's exact published port", async () => { @@ -209,7 +209,7 @@ describe("Cloud Docker route tables", () => { expect.objectContaining({ routes: [ expect.objectContaining({ - action: { kind: "proxy", workspace: "shared-vm", port: 30002 }, + action: { kind: "proxy", vm: "shared-vm", port: 30002 }, }), ], }), @@ -235,15 +235,15 @@ describe("Cloud Docker route tables", () => { expect(writes[0]![3].routes).toEqual([ { match: { path: "/api/status", type: "exact" }, - action: { kind: "proxy", workspace: "shared-vm", port: 30001 }, + action: { kind: "proxy", vm: "shared-vm", port: 30001 }, }, { match: { path: "/api/", type: "prefix" }, - action: { kind: "proxy", workspace: "shared-vm", port: 30003 }, + action: { kind: "proxy", vm: "shared-vm", port: 30003 }, }, { match: { path: "/", type: "prefix" }, - action: { kind: "proxy", workspace: "shared-vm", port: 30001 }, + action: { kind: "proxy", vm: "shared-vm", port: 30001 }, }, ]); }); @@ -259,7 +259,7 @@ describe("Cloud Docker route tables", () => { }], }); expect(publish).toHaveBeenCalledWith("api.onvo.me", undefined, true, { - routes: [{ match: { path: "/v3", type: "prefix" }, action: { kind: "proxy", workspace: "shared-vm", port: 30003 } }], + routes: [{ match: { path: "/v3", type: "prefix" }, action: { kind: "proxy", vm: "shared-vm", port: 30003 } }], }); }); it("keeps the old composite table if any path target is missing, while updating other hosts", async () => { @@ -296,11 +296,11 @@ describe("Cloud Docker route tables", () => { expect(rules).toEqual([ { match: { path: "/api/", type: "prefix" }, - action: { kind: "proxy", workspace: "shared-vm", port: 30003 }, + action: { kind: "proxy", vm: "shared-vm", port: 30003 }, }, { match: { path: "/", type: "prefix" }, - action: { kind: "proxy", workspace: "shared-vm", port: 30001 }, + action: { kind: "proxy", vm: "shared-vm", port: 30001 }, }, ]); }); diff --git a/apps/api/src/modules/images/images.service.ts b/apps/api/src/modules/images/images.service.ts index b62749d95..6b24ab6c9 100644 --- a/apps/api/src/modules/images/images.service.ts +++ b/apps/api/src/modules/images/images.service.ts @@ -91,7 +91,7 @@ export async function listImages( if (hit) return hit; const client = await getClientForOrg(organizationId); - const response = await client.workspaces.images.list(params); + const response = await client.vms.images.list(params); const images = normalizeImages(response); await store.set(key, images, CACHE_TTL_S); return images; diff --git a/apps/api/src/modules/mail/admin/backup-plan.ts b/apps/api/src/modules/mail/admin/backup-plan.ts deleted file mode 100644 index 023e583d2..000000000 --- a/apps/api/src/modules/mail/admin/backup-plan.ts +++ /dev/null @@ -1,228 +0,0 @@ -/** - * Mail backup plan — turns the operator's include-checkboxes into the - * `custom_command` producer's payloadConfig (produce/restore shell). Mail - * path + SQL knowledge lives here in apps/api; the backup adapter stays - * generic (it just runs `sh -c ` on the source via the - * bare SSH executor and streams stdout as one artifact). - * - * produce : stage vmail dump (+ optional DKIM/config, + optional - * maildirs) then `tar | zstd` to stdout. - * restore : `zstd -d | tar -x`, load the vmail data (TRUNCATE the four - * account tables then COPY), restore DKIM/config + maildirs - * when present, recount, reload daemons. - * - * Data-only DB restore: rows load into the TARGET install's existing - * `vmail` schema using the target's own daemon creds — we deliberately do - * NOT carry DB roles/passwords (that would desync dovecot-sql.conf). - * `{SSHA512}` password hashes are host-portable, so accounts work as-is. - */ - -import { HOST_STATE_DIR, MAIL_HOST_STATE_DIR, safePipelineFragment } from "@repo/adapters"; -import { - HOST_AMAVIS_CONF_CANDIDATES, - mailDaemonReloadCommand, - mailEngineCommand, - mailPgDumpToStdout, - mailPsqlFromStdin, - type MailEngineFlavor, -} from "@repo/platform/engine/modules/mail/mail-engine"; - -/** The four tables that hold accounts / domains / aliases / admins. */ -const ACCOUNT_TABLES = ["domain", "mailbox", "forwardings", "domain_admins"] as const; - -/** - * Every place a legacy install may keep amavis's editable config, shared with the - * DKIM writer so a backup can't cover a narrower set of hosts than the thing it is - * backing up. Hardcoding `/etc/amavis/conf.d/50-user` here meant a RHEL-family box - * (monolithic `/etc/amavisd.conf`, no `conf.d`) produced a "keys" archive with no - * amavis config in it at all — and said it succeeded. - */ -const AMAVIS_CONF_PATHS = HOST_AMAVIS_CONF_CANDIDATES.map((c) => c.path); - -/** Debian-family location — the fallback when an archive doesn't name one. */ -const AMAVIS_CONF_DEFAULT = HOST_AMAVIS_CONF_CANDIDATES[0].path; - -/** Archive-relative name for the one amavis config we found, plus where it came from. */ -const AMAVIS_IN_ARCHIVE = "keys/amavis-conf"; - -/** Pre-2026-08 archives stored the Debian path under this fixed name. */ -const AMAVIS_IN_ARCHIVE_LEGACY = "keys/amavis-50-user"; - -export interface MailBackupFlags { - /** Include the maildir message store (/var/vmail). Large. */ - messageData: boolean; - /** Include DKIM keys + amavis config + mail-state.json (secrets). */ - keys: boolean; -} - -export interface MailBackupPayload { - payloadKind: "custom_command"; - payloadConfig: { - produceCommand: string; - restoreCommand: string; - artifactName: string; - /** Recorded for the UI + auditing (not used by the shell). */ - mail: { messageData: boolean; keys: boolean }; - }; -} - -/** - * Build the payloadConfig for a mail-server backup policy. `domain` is - * embedded only in the archive's manifest JSON (never in a shell - * position), so it needs no escaping beyond JSON. - */ -export function buildMailBackupPayload( - domain: string, - flags: MailBackupFlags, - flavor: MailEngineFlavor, -): MailBackupPayload { - const tableArgs = ACCOUNT_TABLES.map((t) => `-t ${t}`).join(" "); - const truncateList = ACCOUNT_TABLES.join(", "); - const info = JSON.stringify({ domain, messageData: flags.messageData, keys: flags.keys }); - const vmailHostDir = flavor === "container" ? `${MAIL_HOST_STATE_DIR}/vmail` : "/var/vmail"; - const produceCommand = [ - "set -e", - 'tmp="$(mktemp -d)"', - "trap 'rm -rf \"$tmp\"' EXIT", - // Accounts + auth — always. Plain-SQL data-only dump (COPY blocks). - `${mailPgDumpToStdout(flavor, `--data-only --no-owner --no-privileges ${tableArgs}`)} > "$tmp/vmail.data.sql"`, - // What's inside — read by the UI / hand-restore. - `printf '%s' '${info.replace(/'/g, "'\\''")}' > "$tmp/mail-backup.json"`, - flags.keys - ? [ - 'mkdir -p "$tmp/keys"', - // Every read here is `sudo -n`, and every copy is allowed to FAIL the backup. - // - // These are root-owned paths — `${HOST_STATE_DIR}` sits under /root (mode 0700) - // and is itself created 0700, and /var/lib/dkim holds private keys. On a host - // Openship logs into as a sudoer rather than as root, the old - // `[ -f … ] && cp … || true` could not even stat them: the test failed for lack - // of privilege, `|| true` absorbed it, and the run exited 0 having recorded - // `keys: true` on an archive containing no keys. mail-state.json is the ONLY - // copy of the DKIM state and the encrypted mailbox/admin credentials, so that - // is a disaster-recovery archive which is silently not one. Same shape as the - // amavis bug this file's own header documents. - // - // The `sudo -n` reads below only distinguish "absent" from "unreadable" if sudo - // is known to WORK — otherwise a failing `test` reads as absence and skips, and - // we are back to an archive stamped `keys: true` with no keys in it. - // - // That used to be guaranteed for free: `sudo -u postgres pg_dump` ran first, so - // under `set -e` a box without sudo never reached here. Routing the dump through - // the DB sidecar for containerized engines (GH-563) removed the guarantee on - // exactly the topology most installs now use, so the probe is explicit. It is - // the whole reason dropping `|| true` on the copies is safe. - "sudo -n true", - 'if sudo -n test -d /var/lib/dkim; then sudo -n cp -a /var/lib/dkim "$tmp/keys/dkim"; fi', - // Whichever of the known locations this box actually uses, with the source - // path recorded alongside so the restore puts it back where amavis reads it. - `for c in ${AMAVIS_CONF_PATHS.join(" ")}; do`, - ` sudo -n test -f "$c" || continue`, - ` sudo -n cp -a "$c" "$tmp/${AMAVIS_IN_ARCHIVE}"`, - ` printf '%s' "$c" > "$tmp/${AMAVIS_IN_ARCHIVE}.path"`, - ` break`, - `done`, - `if sudo -n test -f ${HOST_STATE_DIR}/mail-state.json; then`, - ` sudo -n cp -a ${HOST_STATE_DIR}/mail-state.json "$tmp/keys/mail-state.json"`, - `fi`, - ].join("\n") - : "", - // Stream one tar to stdout: the staged dir + (optionally) the maildirs - // read in place (not re-copied), so the source needs no extra disk. - // - // `sudo -n` for the same reason the staging above uses it, and it is not optional - // here: `cp -a` as root stages root-owned 0600 files, so an unprivileged tar could not - // read back what was just collected. /var/vmail is vmail:vmail 0700 and was already in - // that position. - // - // The pipeline goes through the SHARED fragment so tar's status is no longer masked by - // zstd's. The comment here used to describe that masking — "on a non-root login the - // maildirs were being dropped from the artifact just as quietly" — and leave it in - // place; this was the last pipeline in the module still carrying it. The fragment form - // exists because this is one line of a `set -e` script rather than a standalone - // `sh -c`, and it captures each status via `if` for exactly that reason. - safePipelineFragment( - flags.messageData - ? `sudo -n tar -c -C "$tmp" . -C ${vmailHostDir} vmail1` - : 'sudo -n tar -c -C "$tmp" .', - "zstd -c -3", - { left: "tar (staging the mail archive)", right: "the compressor 'zstd'" }, - ), - ] - .filter(Boolean) - .join("\n"); - - const restoreCommand = [ - "set -e", - 'tmp="$(mktemp -d)"', - "trap 'rm -rf \"$tmp\"' EXIT", - // stdin = the tar.zst artifact. Shared fragment again: a `zstd -d` that fails (a - // truncated artifact, a missing binary) must not hide behind tar's status, because the - // lines after this one TRUNCATE the account tables and then load from "$tmp" — so a - // silently-empty extract would wipe the mail database and restore nothing. - safePipelineFragment("zstd -d", 'tar -x -C "$tmp"', { - left: "the decompressor 'zstd -d'", - right: "tar (extracting the mail archive)", - }), - // Data-only restore: wipe the target's account tables, then load. - `printf '%s' 'TRUNCATE ${truncateList} CASCADE;' | ${mailPsqlFromStdin(flavor)}`, - `${mailPsqlFromStdin(flavor)} < "$tmp/vmail.data.sql"`, - // DKIM keys + amavis config (if the archive carried them). - 'if [ -d "$tmp/keys/dkim" ]; then mkdir -p /var/lib/dkim && cp -a "$tmp/keys/dkim/." /var/lib/dkim/ || true; fi', - // Where to put it is decided by THIS box, not by the box the archive came from. - // - // The archive records its source path, and honouring that verbatim restores a Debian - // archive's /etc/amavis/conf.d/50-user onto a RHEL target, which reads a monolithic - // /etc/amavisd.conf and never looks at the file we just wrote — restored, inert, and - // `|| true` covering it. Cross-family is the NORMAL case here: the reason to restore - // is usually a different machine. The recorded path only breaks the tie when the - // target has no amavis at all, and is still matched against the known list rather - // than trusted, because this shell runs as root on the target. - // - // The probe is each candidate's own `test`, which is the family detector the rest of - // the system already uses — the first is keyed on the include DIRECTORY because - // 50-user is ours to CREATE, so testing for the file would misread a Debian box that - // has no override yet and fall through to the RHEL path. Deliberately NOT the - // produce side's expression: collecting asks "is there a file to copy", placing asks - // "which layout is this box". - `dest=""`, - ...HOST_AMAVIS_CONF_CANDIDATES.map( - (c, i) => `${i === 0 ? "if" : "elif"} [ ${c.test} ]; then dest=${c.path}`, - ), - `fi`, - `if [ -f "$tmp/${AMAVIS_IN_ARCHIVE}" ]; then`, - ` saved="$(cat "$tmp/${AMAVIS_IN_ARCHIVE}.path" 2>/dev/null || true)"`, - ` if [ -z "$dest" ]; then`, - ` dest=${AMAVIS_CONF_DEFAULT}`, - ` for c in ${AMAVIS_CONF_PATHS.join(" ")}; do if [ "$saved" = "$c" ]; then dest="$c"; break; fi; done`, - ` fi`, - ` cp -a "$tmp/${AMAVIS_IN_ARCHIVE}" "$dest" || true`, - // Pre-2026-08 archives carried the Debian path under a fixed name, so they name no - // source at all — which makes the target probe the only thing that can place them. - `elif [ -f "$tmp/${AMAVIS_IN_ARCHIVE_LEGACY}" ]; then`, - ` if [ -z "$dest" ]; then dest=${AMAVIS_CONF_DEFAULT}; fi`, - ` cp -a "$tmp/${AMAVIS_IN_ARCHIVE_LEGACY}" "$dest" || true`, - `fi`, - // Maildirs (if included). Ownership must be vmail:vmail for Dovecot. - // /var/vmail is a bind mount, so the COPY is a host operation either way - but the - // ownership has to be applied where the `vmail` user exists, which on a containerized - // box is the engine, not the host (GH-563). No `|| true` on the chown: maildirs left - // root-owned are maildirs Dovecot cannot read, and swallowing that produced a restore - // that reported success and served nothing. - `if [ -d "$tmp/vmail1" ]; then cp -a "$tmp/vmail1" ${vmailHostDir}/ && ${mailEngineCommand(flavor, "chown -R vmail:vmail /var/vmail/vmail1")}; fi`, - // Recompute per-domain counters (app-managed, not DB triggers). - `printf '%s' "UPDATE domain d SET mailboxes=(SELECT count(*) FROM mailbox m WHERE m.domain=d.domain), aliases=(SELECT count(*) FROM forwardings f WHERE f.domain=d.domain AND f.is_alias) WHERE d.domain IS NOT NULL;" | ${mailPsqlFromStdin(flavor)} || true`, - // Reload daemons so the restored data + keys take effect. - `${mailDaemonReloadCommand(flavor)} || true`, - ].join("\n"); - - return { - payloadKind: "custom_command", - payloadConfig: { - produceCommand, - restoreCommand, - artifactName: "mail.tar.zst", - mail: { messageData: flags.messageData, keys: flags.keys }, - }, - }; -} diff --git a/apps/api/src/modules/mail/mail.controller.ts b/apps/api/src/modules/mail/mail.controller.ts index 49bb84608..8f74d97e7 100644 --- a/apps/api/src/modules/mail/mail.controller.ts +++ b/apps/api/src/modules/mail/mail.controller.ts @@ -30,7 +30,7 @@ import type { Context } from "hono"; import type { SSEStreamingApi } from "hono/streaming"; import crypto from "node:crypto"; import { lookup as dnsLookup } from "node:dns/promises"; -import { buildMailBackupPayload } from "./admin/backup-plan"; +import { mailBackupFlags } from "@repo/platform/engine/modules/mail/backup-plan"; // The mail server is one more backup SOURCE in the general system, so its policy // goes through the same cron validation + schedule registration as a project's. import { syncPolicySchedule, validateCronExpression } from "@repo/platform/engine/modules/backups/triggers/cron"; @@ -67,7 +67,6 @@ import { import { checkMailDelivery } from "./mail-delivery.service"; import { checkMailHealth, mailIsServing, MAIL_COMPONENTS } from "@repo/platform/engine/modules/mail/mail-health.service"; import { checkMailPortReachability } from "@repo/platform/engine/modules/mail/mail-port-reachability.service"; -import { resolveMailFlavor } from "@repo/platform/engine/modules/mail/mail-engine"; import { updatePostmasterPassword } from "./mail-credentials.service"; import { reserveMailSetup } from "./mail-setup-lease"; import { preflightMailSetup } from "./mail-setup-preflight"; @@ -1449,20 +1448,25 @@ export async function saveMailBackupPolicy(c: Context) { if (!body.destinationId) { return c.json({ error: "destinationId is required" }, 400); } + if ((body.messageData !== undefined && typeof body.messageData !== "boolean") || + (body.keys !== undefined && typeof body.keys !== "boolean")) { + return c.json({ error: "messageData and keys must be booleans" }, 400); + } const destination = await repos.backupDestination.findById(body.destinationId); if (!destination || destination.organizationId !== ctx.organizationId) { return c.json({ error: "Destination not found" }, 404); } - const messageData = body.messageData === true; - const keys = body.keys !== false; // default: include keys/secrets - // The produce/restore shell is baked HERE and executed later by the generic - // custom_command producer, which has no mail knowledge — so the topology has to be - // resolved at build time. A containerized engine keeps Postgres in a sidecar and the - // `vmail` user only inside the engine, and the old plan issued `sudo -u postgres` and - // `chown vmail` on the host, where neither exists (GH-563). - const flavor = await resolveMailFlavor(serverId); - const payload = buildMailBackupPayload(mailRow.domain, { messageData, keys }, flavor); + const existing = await repos.backupPolicy.findActiveByMailServer(serverId); + const savedFlags = mailBackupFlags(existing?.payloadConfig); + const messageData = body.messageData ?? savedFlags.messageData; + const keys = body.keys ?? savedFlags.keys; + // A mail policy stores intent; the worker resolves the current topology and + // builds the archive commands once per run through the shared mail lifecycle. + const payload = { + payloadKind: "custom_command" as const, + payloadConfig: { mail: { messageData, keys } }, + }; const cronExpression = typeof body.cronExpression === "string" && body.cronExpression.trim() @@ -1478,8 +1482,6 @@ export async function saveMailBackupPolicy(c: Context) { } } - const existing = await repos.backupPolicy.findActiveByMailServer(serverId); - // Omitted vs explicit-null, the same distinction `createPolicy` draws for // project policies (backup.service.ts) — and it has to be drawn here because // `shared` doubles as the update patch, so a blanket default would overwrite diff --git a/apps/api/src/modules/mcp/mcp-tools.ts b/apps/api/src/modules/mcp/mcp-tools.ts index 737e8e1fe..12cf298e5 100644 --- a/apps/api/src/modules/mcp/mcp-tools.ts +++ b/apps/api/src/modules/mcp/mcp-tools.ts @@ -42,7 +42,7 @@ export interface McpToolDef { name: string; description: string; inputSchema: Record; - annotations: { readOnlyHint: boolean; destructiveHint: boolean }; + annotations: { readOnlyHint: boolean; destructiveHint: boolean; openWorldHint: boolean }; // Dispatch metadata (not sent to the client): method: string; path: string; // full path with :params, e.g. /api/projects/:id @@ -76,6 +76,8 @@ export interface McpPrincipal { /** Resolved with the same persisted instance-authority policy as HTTP calls. */ instanceAdmin?: boolean; role: "owner" | "admin" | "member" | "restricted"; + /** For scoped tokens, the current membership in their bound organization. */ + membershipRole?: "owner" | "admin" | "member" | "restricted"; readOnly: boolean; /** Resource types the token holds grants on — only consulted when role === "restricted". */ grantedRootTypes: ReadonlySet; @@ -184,9 +186,12 @@ function inputSchema( function annotationsFor(route: RegisteredRoute): { readOnlyHint: boolean; destructiveHint: boolean; + openWorldHint: boolean; } { const spec = route.spec; - if (isPublicSpec(spec)) return { readOnlyHint: true, destructiveHint: false }; + if (isPublicSpec(spec)) { + return { readOnlyHint: true, destructiveHint: false, openWorldHint: true }; + } const parsed = parsePermissionTag(spec.tag); const readOnlyHint = route.method === "GET" || spec.readOnly === true; const destructiveHint = @@ -195,7 +200,7 @@ function annotationsFor(route: RegisteredRoute): { (route.method === "DELETE" || parsed.action === "admin" || /delete|teardown|destroy|remove|wipe|revoke/i.test(route.path))); - return { readOnlyHint, destructiveHint }; + return { readOnlyHint, destructiveHint, openWorldHint: spec.mcp?.openWorld ?? true }; } let cached: McpToolDef[] | null = null; @@ -350,6 +355,10 @@ export function filterToolsForPrincipal( principal: McpPrincipal, ): McpToolDef[] { return tools.filter((t) => { + const action = assertedAction(t.perm.action); + const role = principal.membershipRole ?? principal.role; + if (role !== "restricted" && + !roleAllowsResourceType(role, t.perm.leaf as CheckedResourceType, action)) return false; // Read-only tokens can only call GET — the runtime gate rejects mutations by // HTTP method, so mirror that exactly rather than guessing from the tag. if (principal.readOnly && t.method !== "GET") return false; @@ -373,9 +382,7 @@ export function filterToolsForPrincipal( if (principal.role === "restricted" && !caps?.has(needed)) return false; } - if (principal.role !== "restricted") { - return roleAllowsResourceType(principal.role, t.perm.leaf as CheckedResourceType); - } + if (principal.role !== "restricted") return true; // "Own projects" scope: the create-capable project wildcard grant reaches the // dedicated create routes (POST /projects, POST /apps) and the project list // (which the caller filters to its self-created projects). Checked before the @@ -401,11 +408,11 @@ export function filterToolsForPrincipal( // A per-id grant still fails here, correctly: `wildcardGrants` holds only // resourceId "*" rows. const held = principal.wildcardGrants.get(t.perm.leaf); - return held ? permitsAction(held, assertedAction(t.perm.action)) : false; + return held ? permitsAction(held, action) : false; } if (principalHasGrantFor(principal.grantedRootTypes, t.perm.grantRoot)) return true; // Same mapping the permission check uses: `domain:admin` ⇒ `domains:delete`. - const capability = capabilityFor(t.perm.leaf, assertedAction(t.perm.action)); + const capability = capabilityFor(t.perm.leaf, action); return !!capability && !!principal.grantedCapabilities?.has(capability); }); } @@ -419,6 +426,7 @@ export function toClientTool(t: McpToolDef) { annotations: { readOnlyHint: t.annotations.readOnlyHint, destructiveHint: t.annotations.destructiveHint, + openWorldHint: t.annotations.openWorldHint, }, }; } diff --git a/apps/api/src/modules/mcp/mcp.routes.ts b/apps/api/src/modules/mcp/mcp.routes.ts index 9a80ecf68..16d90eab1 100644 --- a/apps/api/src/modules/mcp/mcp.routes.ts +++ b/apps/api/src/modules/mcp/mcp.routes.ts @@ -128,6 +128,7 @@ async function resolveMcpCaller(token: string, headers: Headers): Promise ({ pinned: vi.fn(), source: vi.fn(), target: vi.fn(), + list: vi.fn(), + handle: vi.fn(), produce: vi.fn(), restore: vi.fn(), dispose: vi.fn(), @@ -28,7 +30,7 @@ vi.mock("@repo/db", () => ({ activeDeploymentId: "build", }), }, - service: { listByProject: async () => [] }, + service: { listByProject: h.list }, deployment: { findById: async () => ({ containerId: "target-container" }) }, }, })); @@ -52,7 +54,7 @@ vi.mock("@repo/platform/engine/lib/project-runtime-lock", () => ({ withProjectRuntimeLock: async (_id: string, work: () => Promise) => work(), })); vi.mock("@repo/platform/engine/modules/backups/service-handle", () => ({ - serviceHandleFor: vi.fn(), + serviceHandleFor: h.handle, })); vi.mock("@repo/platform/engine/modules/services/service-container", () => ({ containerIdForService: vi.fn(), @@ -107,6 +109,8 @@ let target: BackupExecutor; beforeEach(() => { vi.resetAllMocks(); h.workspaceId = undefined; + h.list.mockResolvedValue([{ id: "db", name: "db", projectId: "project" }]); + h.handle.mockResolvedValue(handle); h.activity.mockImplementation(async (_id: string, work: () => Promise) => work()); h.pinned.mockResolvedValue({ ip: "203.0.113.8" }); target = { @@ -121,12 +125,23 @@ beforeEach(() => { }); }); describe("provider data copy", () => { + it("rejects a missing native target service before opening a source or restoring data", async () => { + h.list.mockResolvedValue([{ id: "other-service", name: "api", projectId: "project" }]); + await expect(copyPlatformData({ + runId: "run", service, projectId: "project", serviceId: "db", deploymentId: "build", + signal: new AbortController().signal, log: vi.fn(), progress: vi.fn(), + })).rejects.toMatchObject({ code: "IMPORT_TARGET_SERVICE_MISSING" }); + expect(h.source).not.toHaveBeenCalled(); + expect(h.restore).not.toHaveBeenCalled(); + expect(h.handle).not.toHaveBeenCalled(); + }); it("leaves a destination changed by the operator untouched", async () => { await expect( copyPlatformData({ runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "replaced-build", signal: new AbortController().signal, log: vi.fn(), @@ -174,6 +189,10 @@ describe("provider data copy", () => { ).rejects.toMatchObject({ code: "IMPORT_DATABASE_EXTENSIONS_MISSING" }); }); it("streams every byte with backpressure, verifies after restore and closes both transports", async () => { + const targetRow = { id: "db", name: "database", projectId: "project" }; + h.list.mockResolvedValue([ + { id: "api", name: "api", projectId: "project" }, targetRow, + ]); const payload = Buffer.alloc(256 * 1024, 0x41); let producerCompleted = false; h.produce.mockImplementation(async function* () { @@ -197,12 +216,14 @@ describe("provider data copy", () => { runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "build", signal: new AbortController().signal, log: vi.fn(), progress, }); expect(restored).toEqual(payload); + expect(h.handle).toHaveBeenCalledWith(targetRow, expect.objectContaining({ projectSlug: "app" })); expect(producerCompleted).toBe(true); expect(h.dispose).toHaveBeenCalledOnce(); expect(h.source).not.toHaveBeenCalled(); @@ -228,6 +249,7 @@ describe("provider data copy", () => { runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "build", signal: new AbortController().signal, log: vi.fn(), @@ -245,6 +267,7 @@ describe("provider data copy", () => { runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "build", signal: new AbortController().signal, log: vi.fn(), @@ -269,6 +292,7 @@ it("holds the owning Cloud workspace activity lock for the whole transfer", asyn runId: "run", service, projectId: "project", + serviceId: "db", deploymentId: "build", signal, log: vi.fn(), diff --git a/apps/api/src/modules/migration/platform-import-plan.test.ts b/apps/api/src/modules/migration/platform-import-plan.test.ts index 910545e3f..2acc649ba 100644 --- a/apps/api/src/modules/migration/platform-import-plan.test.ts +++ b/apps/api/src/modules/migration/platform-import-plan.test.ts @@ -134,7 +134,7 @@ async function review( } describe("reviewed platform imports", () => { - it("keeps secrets server-side and replaces provider database addresses with shared project bindings", async () => { + it("keeps secrets server-side and replaces provider database addresses with native service aliases", async () => { const plan = await review(); expect(plan.ready).toBe(true); expect(plan.requiresWritePause).toBe(true); @@ -147,7 +147,15 @@ describe("reviewed platform imports", () => { valueTemplate: "postgres://app:source-secret@${host}/app", }, ]); - expect(privatePlan.services[1].environment).not.toHaveProperty("DATABASE_URL"); + expect(privatePlan.services[1].environment.DATABASE_URL).toBe( + "postgres://app:source-secret@database/app", + ); + expect(privatePlan.projects[0].create.name).toBe("Project"); + expect(privatePlan.projects[0].create.services?.map((row) => row.name)).toEqual([ + "database", + "website", + ]); + expect(JSON.stringify(privatePlan.projects)).not.toContain("source-secret"); expect(privatePlan.services[0].environment.PGHOST).toBe( privatePlan.services[0].create.internalAlias, ); @@ -175,6 +183,113 @@ describe("reviewed platform imports", () => { code: "IMPORT_SOURCE_CHANGED", }); }); + it("names storage per service inside one destination project", async () => { + inventory.services[1].volumes = [{ id: "files", mountPath: "/data" }]; + const plan = await loadReviewedImport(ctx, (await review()).planId); + expect(plan.projects[0].create.services?.flatMap((service) => service.volumes ?? [])).toEqual([ + "database-data-0:/var/lib/postgresql/data", + "website-data-0:/data", + ]); + expect(plan.projects[0].create.services?.[1].dependsOn).toEqual(["database"]); + }); + it.each(["repository", "commit", "branch"])( + "splits incompatible Git %s without losing revisions or private connections", + async (difference) => { + inventory.services = ["one", "two"].map((id) => + service({ + id, + name: id, + image: undefined, + repository: difference === "repository" ? `owner/${id}` : "owner/repo", + commit: (difference === "commit" && id === "two" ? "b" : "a").repeat(40), + branch: difference === "branch" ? id : "main", + privateDomain: `${id}.railway.internal`, + env: id === "two" ? { API_URL: "http://one.railway.internal:3000/api?secret=keep" } : {}, + }), + ); + const result = await review( + inventory.services.map((service) => ({ + id: service.id, + name: service.name, + data: "empty", + })), + ); + expect(result.ready, JSON.stringify(result)).toBe(true); + expect(result.projects).toEqual([ + { name: "project-one", serviceIds: ["one"] }, + { name: "project-two", serviceIds: ["two"] }, + ]); + const plan = await loadReviewedImport(ctx, result.planId); + expect(plan.projects.map((project) => project.create.gitRepo)).toEqual( + difference === "repository" ? ["one", "two"] : ["repo", "repo"], + ); + expect(plan.services[1].bindings).toEqual([ + { + sourceId: "one", + envKey: "API_URL", + valueTemplate: "http://${host}:3000/api?secret=keep", + }, + ]); + expect(plan.services.map((s) => s.source.commit)).toEqual( + inventory.services.map((s) => s.commit), + ); + expect(h.info.mock.calls.map(([input]) => input.owner)).toEqual(["owner", "owner"]); + expect(h.info.mock.calls.map(([input]) => input.branch)).toEqual( + inventory.services.map((s) => s.commit), + ); + }, + ); + it("keeps compatible source services together and rejects composite cross-project variables during review", async () => { + const git = (id: string, repository: string) => + service({ + id, + name: id, + image: undefined, + repository, + commit: "a".repeat(40), + privateDomain: `${id}.railway.internal`, + }); + inventory.services = [git("one", "owner/repo"), git("two", "owner/repo")]; + const choices = () => + inventory.services.map((s) => ({ id: s.id, name: s.name, data: "empty" as const })); + expect((await review(choices())).projects).toEqual([ + { name: "Project", serviceIds: ["one", "two"] }, + ]); + inventory.services.push(git("three", "owner/other")); + inventory.services[2].env = { HOSTS: "one.railway.internal,two.railway.internal" }; + const result = await review(choices()); + expect(result.ready).toBe(false); + expect(result.services[2].blockers.join(" ")).toContain("separate connection variable"); + }); + it("blocks unavailable private listeners and ambiguous host templates before source downtime", async () => { + inventory.services = [ + service({ + id: "store", + name: "store", + port: undefined, + privateDomain: "store.railway.internal", + }), + ...["one", "two"].map((id) => + service({ + id, + name: id, + image: undefined, + repository: `owner/${id}`, + commit: "a".repeat(40), + env: + id === "one" + ? { HOST: "store.railway.internal" } + : { HOST: "store.railway.internal-${host}" }, + }), + ), + ]; + const result = await review( + inventory.services.map((s) => ({ id: s.id, name: s.name, data: "empty" })), + ); + expect(result.ready).toBe(false); + expect(result.services[1].blockers.join(" ")).toContain("private listener port"); + expect(result.services[2].blockers.join(" ")).toContain("cannot be safely converted"); + }); it("ignores pagination order and changing volume size, but never ignores changed configuration", () => { const next = structuredClone(inventory); next.services.reverse(); @@ -202,16 +317,22 @@ describe("reviewed platform imports", () => { const reviewed = await review(); expect(reviewed.ready).toBe(true); expect( - (await loadReviewedImport(ctx, reviewed.planId)).services[1].bindings[0].valueTemplate, - ).toBe("postgres://app:source-secret@${host}/app"); + (await loadReviewedImport(ctx, reviewed.planId)).services[1].environment.DATABASE_URL, + ).toBe("postgres://app:source-secret@database/app"); }); - it("rejects conflicting project names and enforces destination access before planning", async () => { + it("rejects conflicting service names and enforces destination access before planning", async () => { await expect( review([ { id: "db", name: "Same Name", data: "copy" }, { id: "app", name: "same-name", data: "empty" }, ]), ).rejects.toMatchObject({ code: "IMPORT_NAME_INVALID" }); + await expect( + review([ + { id: "db", name: "api.v2", data: "copy" }, + { id: "app", name: "api v2", data: "empty" }, + ]), + ).rejects.toMatchObject({ code: "IMPORT_NAME_INVALID" }); h.authorize.mockRejectedValue(new Error("forbidden")); await expect(review()).rejects.toThrow("forbidden"); }); @@ -252,7 +373,7 @@ describe("reviewed platform imports", () => { { sourceId: external.id, envKey: "DATABASE_URL" }, ]); expect(privatePlan.services[1].create.services?.[0].volumes).toEqual([ - "data-0:/var/lib/postgresql", + "database-data-0:/var/lib/postgresql", ]); // Supplying secrets does not falsely report provider configuration drift. expect(h.discover).toHaveBeenCalledTimes(2); @@ -281,6 +402,9 @@ it("uses the existing Compose build for a custom Dockerfile and preserves readin readiness: { path: "/health" }, services: [{ build: "apps/api", dockerfile: "docker/production.Dockerfile" }], }); + expect(plan.projects[0].create.services?.[0].advanced).toMatchObject({ + readiness: { path: "/health", port: 3000 }, + }); expect(h.info).not.toHaveBeenCalled(); }); it("blocks an unavailable custom Dockerfile before import", async () => { diff --git a/apps/api/src/modules/migration/platform-import-worker.test.ts b/apps/api/src/modules/migration/platform-import-worker.test.ts index 2f4eaf5e9..446bd3f5c 100644 --- a/apps/api/src/modules/migration/platform-import-worker.test.ts +++ b/apps/api/src/modules/migration/platform-import-worker.test.ts @@ -1,20 +1,26 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; -import { AppError } from "@repo/core"; +import { AppError, ConflictError } from "@repo/core"; import { withProjectRuntimeLock } from "@repo/platform/engine/lib/project-runtime-lock"; import type { ExecutionContext } from "@repo/platform"; import type { PrivateImportPlan, PlannedImportService, } from "@repo/platform/engine/modules/migration/platform-import-plan"; +import { platformImportProjects } from "@repo/platform/engine/modules/migration/platform-import-project"; const h = vi.hoisted(() => ({ ensure: vi.fn(), merge: vi.fn(), - connect: vi.fn(), + list: vi.fn(), + project: vi.fn(), + sourceService: vi.fn(), + rows: [] as Array<{ id: string; projectId: string; name: string }>, view: vi.fn(), + connect: vi.fn(), deploy: vi.fn(), copy: vi.fn(), preflight: vi.fn(), prepareTarget: vi.fn(), + routes: vi.fn(), authorize: vi.fn(), disable: vi.fn(), bind: vi.fn(), @@ -29,7 +35,8 @@ const h = vi.hoisted(() => ({ vi.mock("@repo/db", () => ({ withAdvisoryLock: async (_key: string, fn: () => Promise) => fn(), repos: { - project: { update: h.update, findById: async (id: string) => ({ id, workspaceId: null }) }, + service: { listByProject: h.list }, + project: { update: h.update, findById: h.project }, dockerMigrationRun: { bindImportedProject: h.bind, findById: h.find, @@ -40,11 +47,11 @@ vi.mock("@repo/db", () => ({ vi.mock("@repo/platform/engine/modules/projects/project-crud.service", () => ({ ensureProject: h.ensure, })); -vi.mock("@repo/platform/engine/modules/projects/project-env.service", () => ({ - mergeEnvVars: h.merge, +vi.mock("@repo/platform/engine/modules/services/service-environment-state", () => ({ + mergeServiceEnvVars: h.merge, })); -vi.mock("@repo/platform/engine/modules/projects/project-connection.service", () => ({ - createConnection: h.connect, +vi.mock("@repo/platform/engine/modules/services/service.service", () => ({ + createService: h.sourceService, })); vi.mock("@repo/platform/engine/modules/projects/project-runtime.service", () => ({ disableProject: h.disable, @@ -52,6 +59,9 @@ vi.mock("@repo/platform/engine/modules/projects/project-runtime.service", () => vi.mock("@repo/platform/engine/modules/apps/app-settings.service", () => ({ getAppConnectionView: h.view, })); +vi.mock("@repo/platform/engine/modules/projects/project-connection.service", () => ({ + createConnection: h.connect, +})); vi.mock("@repo/platform/engine/modules/deployments/build.service", () => ({ triggerDeployment: h.deploy, })); @@ -62,6 +72,9 @@ vi.mock("@repo/platform/engine/modules/migration/platform-import-data", () => ({ vi.mock("@repo/platform/engine/modules/migration/migration-runtime", () => ({ prepareMigrationTarget: h.prepareTarget, })); +vi.mock("@repo/platform/engine/modules/migration/migration-routes", () => ({ + saveMigrationRoutes: h.routes, +})); vi.mock("@repo/platform/engine/modules/migration/platform-import-health", () => ({ verifyPlatformDeployment: h.verify, })); @@ -96,7 +109,11 @@ const service = (id: string, kind: "app" | "database" | "worker"): PlannedImport ...(kind === "database" ? { database: "pg_dump" } : { commit: "a".repeat(40), branch: "main" }), }, data: "copy", - create: { name: id }, + create: { + name: id, + internalAlias: id, + services: [{ name: id, image: "image:1", volumes: [`${id}-data:/data`] }], + }, environment: { TOKEN: "secret" }, bindings: [], }); @@ -107,11 +124,19 @@ const planFor = (services: PlannedImportService[]) => userId: "user", targetServerId: "server", services, + projectName: "imported-project", + projects: platformImportProjects("imported-project", "server", services), }) as PrivateImportPlan; let row: any; beforeEach(() => { vi.resetAllMocks(); h.events.length = 0; + h.rows.length = 0; + h.routes.mockResolvedValue([]); + h.project.mockImplementation(async (id) => ({ id, workspaceId: null })); + h.list.mockImplementation(async (projectId) => + h.rows.filter((row) => row.projectId === projectId), + ); row = { id: "run", organizationId: "org", @@ -129,24 +154,43 @@ beforeEach(() => { }); h.ensure.mockImplementation(async (body) => { h.events.push(`create:${body.name}`); + h.rows.push( + ...body.services.map((row: { name: string }) => ({ + ...row, + id: `${body.name}-${row.name}`, + projectId: body.name, + })), + ); return { project_id: body.name }; }); + h.sourceService.mockImplementation(async (_ctx, projectId, body) => { + h.rows.push({ ...body, id: `${projectId}-${body.name}`, projectId }); + }); + let deploymentCount = 0; h.deploy.mockImplementation(async (_ctx, input) => { - h.events.push(`deploy:${input.projectId}`); - return { deployment: { id: `${input.projectId}-build` } }; + h.events.push(`deploy:${input.projectId}:${input.serviceIds.length}`); + return { deployment: { id: `${input.projectId}-build-${++deploymentCount}` } }; }); h.copy.mockImplementation(async (input) => { - h.events.push(`copy:${input.projectId}`); - }); - h.view.mockResolvedValue({ - outputs: [{ id: "database-url", internal: true, value: "postgres://new" }], + h.events.push(`copy:${input.service.source.id}`); }); + h.view.mockImplementation(async () => ({ + outputs: h.rows.map((row) => ({ + id: `${row.id}:url`, + sourceServiceId: row.id, + internal: true, + value: "postgres://new", + })), + })); h.prepareSource.mockImplementation(async () => { h.events.push("pause-project"); }); h.restoreSource.mockImplementation(async (_run, resume) => { h.events.push(resume ? "resume-project" : "restore-config"); }); + h.connect.mockImplementation(async (_ctx, target, input) => { + h.events.push(`connect:${input.sourceProjectId}:${target}`); + }); }); const lifecycle = (signal = new AbortController().signal) => ({ signal, @@ -159,6 +203,149 @@ const lifecycle = (signal = new AbortController().signal) => ({ }), }); describe("provider import through normal project operations", () => { + it("copies all volumes before deploying multiple repositories in dependency order with native links", async () => { + const db = service("db", "database"), + app = service("app", "app"), + worker = service("worker", "worker"); + for (const entry of [app, worker]) + Object.assign(entry.create, { + gitProvider: "github", + gitOwner: "owner", + gitRepo: entry.source.id, + gitBranch: "main", + }); + worker.source.commit = "b".repeat(40); + app.bindings = [ + { sourceId: "db", envKey: "DB_URL", valueTemplate: "postgres://secret@${host}:5432/store" }, + ]; + worker.bindings = [{ sourceId: "app", envKey: "APP_HOST", valueTemplate: "${host}" }]; + app.environment.DB_URL = "postgres://secret@db:5432/store"; + worker.environment.APP_HOST = "app"; + const services = [worker, app, db]; + const plan = { ...planFor(services), sourceControl: "railway-project" as const }; + await executePlatformImport(ctx, "run", plan, lifecycle()); + expect(h.ensure.mock.calls.map(([body]) => body.gitRepo)).toEqual(["worker", "app", undefined]); + expect( + h.ensure.mock.calls.every(([body]) => body.services.every((s: any) => !s.dependsOn.length)), + ).toBe(true); + expect(h.copy.mock.calls.map(([input]) => [input.projectId, input.serviceId])).toEqual([ + ["imported-project-db", "imported-project-db-db"], + ["imported-project-app", "imported-project-app-app"], + ["imported-project-worker", "imported-project-worker-worker"], + ]); + expect(h.events.indexOf("copy:worker")).toBeLessThan( + h.events.indexOf("deploy:imported-project-db:1"), + ); + expect(h.events.indexOf("healthy:imported-project-db-build-1")).toBeLessThan( + h.events.indexOf("connect:imported-project-db:imported-project-app"), + ); + expect(h.events.indexOf("connect:imported-project-db:imported-project-app")).toBeLessThan( + h.events.indexOf("deploy:imported-project-app:1"), + ); + expect(h.connect).toHaveBeenCalledWith( + ctx, + "imported-project-app", + { + sourceProjectId: "imported-project-db", + outputId: "imported-project-db-db:url", + envKey: "DB_URL", + mode: "internal", + valueTemplate: "postgres://secret@${host}:5432/store", + }, + { defer: true }, + ); + expect( + h.merge.mock.calls.flatMap(([, , , input]) => input.upserts.map((v: any) => v.key)), + ).not.toContain("DB_URL"); + expect(h.deploy.mock.calls.map(([, input]) => [input.projectId, input.commitSha])).toEqual([ + ["imported-project-db", undefined], + ["imported-project-app", "a".repeat(40)], + ["imported-project-worker", "b".repeat(40)], + ]); + expect(h.verify).toHaveBeenCalledTimes(3); + expect(row.recovery.providerImport.completed).toEqual(["db", "app", "worker"]); + }); + it("does not start a cross-project consumer if its private connection fails", async () => { + const db = service("db", "database"), + app = service("app", "app"), + worker = service("worker", "worker"); + for (const entry of [app, worker]) + Object.assign(entry.create, { gitOwner: "owner", gitRepo: entry.source.id }); + app.bindings = [{ sourceId: "db", envKey: "DB_URL" }]; + const plan = { ...planFor([app, db, worker]), sourceControl: "railway-project" as const }; + row.recovery.providerImport.planEnc = JSON.stringify(plan); + h.connect.mockRejectedValue(new Error("Private network unavailable")); + await expect(executePlatformImport(ctx, "run", plan, lifecycle())).rejects.toThrow( + "Private network unavailable", + ); + expect(h.deploy).toHaveBeenCalledTimes(1); + expect(row.recovery.providerImport.completed).toEqual([]); + expect(h.restoreSource).not.toHaveBeenCalledWith("run", true, expect.anything()); + await pauseFailedPlatformImport("run"); + expect(h.disable).toHaveBeenCalledExactlyOnceWith( + "imported-project-db", + "org", + "imported-project-db-build-1", + ); + expect(h.restoreSource).toHaveBeenLastCalledWith("run", true); + expect(h.disable.mock.invocationCallOrder[0]).toBeLessThan( + h.restoreSource.mock.invocationCallOrder[1], + ); + }); + it("stages custom domains through native migration routing before pausing the source", async () => { + const web = service("web", "app"); + web.create.name = "storefront"; + web.source.domains = [ + { hostname: "shop.example.com", portable: true }, + { hostname: "www.example.com", portable: true }, + { hostname: "shop.up.railway.app", portable: false }, + ]; + const plan = { ...planFor([web]), sourceControl: "railway-project" as const }; + const flow = lifecycle(); + await executePlatformImport(ctx, "run", plan, flow); + expect(h.routes).toHaveBeenCalledWith( + ctx, + "imported-project", + { + storefront: [ + { domainType: "custom", customDomain: "shop.example.com" }, + { domainType: "custom", customDomain: "www.example.com" }, + ], + }, + flow.log, + ); + expect(h.routes.mock.invocationCallOrder[0]).toBeLessThan( + h.prepareSource.mock.invocationCallOrder[0], + ); + expect(h.routes.mock.invocationCallOrder[0]).toBeLessThan(h.copy.mock.invocationCallOrder[0]); + }); + + it("reports domain conflicts without pausing Railway or deploying the destination", async () => { + const app = service("app", "app"); + app.source.domains = [{ hostname: "taken.example.com", portable: true }]; + h.routes.mockRejectedValueOnce( + new ConflictError("The domain is already connected to another project."), + ); + await expect( + executePlatformImport( + ctx, + "run", + { + ...planFor([app]), + sourceControl: "railway-project", + }, + lifecycle(), + ), + ).rejects.toMatchObject({ + statusCode: 409, + code: "IMPORT_DOMAIN_REVIEW_REQUIRED", + message: "The domain is already connected to another project.", + }); + expect(h.prepareSource).not.toHaveBeenCalled(); + expect(h.copy).not.toHaveBeenCalled(); + expect(h.deploy).not.toHaveBeenCalled(); + }); + it("prepares managed capacity before pausing Railway and does not stop the source if provisioning fails", async () => { const plan = { ...planFor([service("app", "app")]), sourceControl: "railway-project" as const }; const flow = lifecycle(); @@ -194,6 +381,136 @@ describe("provider import through normal project operations", () => { expect(h.events.indexOf("pause-project")).toBeLessThan(h.events.indexOf("copy:db")); expect(h.events.indexOf("restore-config")).toBeLessThan(firstDeployment); expect(h.copy.mock.calls.every(([input]) => input.deploymentId === undefined)).toBe(true); + expect(h.ensure).toHaveBeenCalledTimes(1); + expect(h.deploy).toHaveBeenCalledTimes(1); + expect(new Set(h.copy.mock.calls.map(([input]) => input.projectId))).toEqual( + new Set(["imported-project"]), + ); + expect(new Set(h.copy.mock.calls.map(([input]) => input.serviceId)).size).toBe(3); + expect(new Set(Object.values(row.recovery.providerImport.projects))).toEqual( + new Set(["imported-project"]), + ); + }); + it("imports two source projects as two native projects, keeping identical service names separate", async () => { + for (const name of ["first-project", "second-project"]) { + const services = [service("db", "database"), service("app", "app")]; + services.forEach((service) => (service.transfer = "cold-volume")); + const plan = { ...planFor(services), sourceControl: "railway-project" as const }; + plan.projectName = name; + plan.projects[0].create.name = name; + await executePlatformImport(ctx, name, plan, lifecycle()); + expect(new Set(Object.values(row.recovery.providerImport.projects))).toEqual(new Set([name])); + } + expect(h.ensure.mock.calls.map(([body]) => body.name)).toEqual([ + "first-project", + "second-project", + ]); + expect(h.rows.map((row) => row.projectId)).toEqual([ + "first-project", + "first-project", + "second-project", + "second-project", + ]); + expect(h.copy.mock.calls.map(([input]) => [input.projectId, input.serviceId])).toEqual([ + ["first-project", "first-project-db"], + ["first-project", "first-project-app"], + ["second-project", "second-project-db"], + ["second-project", "second-project-app"], + ]); + }); + it("stores each service's environment separately instead of merging it into project-wide values", async () => { + const app = service("app", "app"), + db = service("db", "database"); + app.environment = { PORT: "3000", TOKEN: "app-secret" }; + db.environment = { PORT: "5432", TOKEN: "database-secret" }; + await executePlatformImport(ctx, "run", planFor([app, db]), lifecycle()); + for (const entry of [app, db]) { + expect(h.merge).toHaveBeenCalledWith( + ctx, + "imported-project", + `imported-project-${entry.source.id}`, + { + environment: "production", + deletes: [], + upserts: Object.entries(entry.environment).map(([key, value]) => ({ + key, + value, + isSecret: true, + sourceId: null, + })), + }, + ); + } + }); + it("adds a source-built app through the native service creator in the same project", async () => { + const app = service("app", "app"); + app.create = { + name: "app", + gitOwner: "owner", + gitRepo: "repo", + gitBranch: "main", + framework: "nextjs", + rootDirectory: "apps/web", + installCommand: "bun install", + startCommand: "bun start", + port: 3000, + }; + await executePlatformImport(ctx, "run", planFor([app, service("db", "database")]), lifecycle()); + expect(h.ensure).toHaveBeenCalledTimes(1); + expect(h.sourceService).toHaveBeenCalledWith( + ctx, + "imported-project", + expect.objectContaining({ + name: "app", + kind: "monorepo", + rootDirectory: "apps/web", + framework: "nextjs", + ports: ["3000"], + exposed: false, + }), + ); + expect(h.deploy).toHaveBeenLastCalledWith( + ctx, + expect.objectContaining({ + projectId: "imported-project", + commitSha: "a".repeat(40), + }), + ); + }); + it("rejects reviews without destination projects before allocating a destination", async () => { + const plan = planFor([service("app", "app")]); + delete (plan as Partial).projects; + await expect(executePlatformImport(ctx, "run", plan, lifecycle())).rejects.toMatchObject({ + code: "IMPORT_REVIEW_EXPIRED", + }); + expect(h.ensure).not.toHaveBeenCalled(); + expect(h.prepareSource).not.toHaveBeenCalled(); + }); + it("pauses a shared destination once before restoring the Railway source", async () => { + const plan = planFor([service("app", "app"), service("db", "database")]); + Object.assign(row.recovery.providerImport, { + planEnc: JSON.stringify(plan), + projects: { app: "shared-project", db: "shared-project" }, + deployments: { app: "shared-build", db: "shared-build" }, + }); + await pauseFailedPlatformImport("run"); + expect(h.disable).toHaveBeenCalledExactlyOnceWith("shared-project", "org", "shared-build"); + expect(h.restoreSource).toHaveBeenCalledTimes(1); + expect(h.disable.mock.invocationCallOrder[0]).toBeLessThan( + h.restoreSource.mock.invocationCallOrder[0], + ); + }); + it("can pause its own database-only predecessor when a complete deployment fails", async () => { + const plan = planFor([service("app", "app"), service("db", "database")]); + Object.assign(row.recovery.providerImport, { + planEnc: JSON.stringify(plan), + projects: { app: "shared", db: "shared" }, + deployments: { app: "failed-build", db: "failed-build" }, + ownedDeploymentIds: ["database-build", "failed-build"], + }); + h.project.mockResolvedValue({ id: "shared", activeDeploymentId: "database-build" }); + await pauseFailedPlatformImport("run"); + expect(h.disable).toHaveBeenCalledExactlyOnceWith("shared", "org", "database-build"); }); it("stops every imported Railway workload before resuming the source on failure", async () => { const plan = { @@ -219,29 +536,43 @@ describe("provider import through normal project operations", () => { const plan = planFor([worker, app, db]); await executePlatformImport(ctx, "run", plan, lifecycle()); expect(h.events).toEqual([ - "create:db", - "create:app", - "create:worker", - "deploy:db", - "healthy:db-build", + "create:imported-project", + "deploy:imported-project:1", + "healthy:imported-project-build-1", "copy:db", "copy:app", - "deploy:app", - "healthy:app-build", "copy:worker", - "deploy:worker", - "healthy:worker-build", + "deploy:imported-project:3", + "healthy:imported-project-build-2", ]); - expect(h.ensure).toHaveBeenCalledWith(app.create, "org", ctx, { mustCreate: true }); - expect(h.connect).toHaveBeenCalledWith( + expect(h.ensure).toHaveBeenCalledExactlyOnceWith(plan.projects[0].create, "org", ctx, { + mustCreate: true, + }); + expect(h.merge).toHaveBeenCalledWith( ctx, - "app", - { sourceProjectId: "db", outputId: "database-url", envKey: "DATABASE_URL", mode: "internal" }, - { defer: true }, + "imported-project", + "imported-project-app", + expect.objectContaining({ + upserts: [{ key: "DATABASE_URL", value: "postgres://new", sourceId: null, isSecret: true }], + }), ); - expect(h.deploy).toHaveBeenCalledWith( + expect(h.deploy).toHaveBeenNthCalledWith( + 1, ctx, - expect.objectContaining({ projectId: "app", commitSha: "a".repeat(40), branch: "main" }), + expect.objectContaining({ + projectId: "imported-project", + serviceIds: ["imported-project-db"], + strictServiceScope: true, + }), + ); + expect(h.deploy).toHaveBeenNthCalledWith( + 2, + ctx, + expect.objectContaining({ + projectId: "imported-project", + serviceIds: ["imported-project-db", "imported-project-app", "imported-project-worker"], + strictServiceScope: true, + }), ); expect(row.recovery.providerImport.completed).toEqual(["db", "app", "worker"]); }); @@ -253,7 +584,13 @@ describe("provider import through normal project operations", () => { executePlatformImport(ctx, "run", planFor([app, service("db", "database")]), lifecycle()), ).rejects.toThrow("restore failed"); expect(h.deploy).toHaveBeenCalledTimes(1); - expect(h.deploy).toHaveBeenCalledWith(ctx, expect.objectContaining({ projectId: "db" })); + expect(h.deploy).toHaveBeenCalledWith( + ctx, + expect.objectContaining({ + projectId: "imported-project", + serviceIds: ["imported-project-db"], + }), + ); }); it("checkpoints the created deployment before observing a cancelled phase", async () => { const flow = lifecycle(); @@ -263,7 +600,7 @@ describe("provider import through normal project operations", () => { await expect( executePlatformImport(ctx, "run", planFor([service("app", "app")]), flow), ).rejects.toThrow("cancelled"); - expect(row.recovery.providerImport.deployments).toEqual({ app: "app-build" }); + expect(row.recovery.providerImport.deployments).toEqual({ app: "imported-project-build-1" }); }); it("rejects dependency cycles rather than starting a partial recursive import", () => { const a = service("a", "app"), @@ -272,7 +609,7 @@ describe("provider import through normal project operations", () => { b.bindings = [{ sourceId: "a", envKey: "URL" }]; expect(() => orderedImports([a, b])).toThrow(/cycle/); }); - it("retains databases and pauses only the import's own app deployment identities on failure", async () => { + it("pauses imported deployment identities before releasing source recovery", async () => { const plan = planFor([service("app", "app"), service("db", "database")]); Object.assign(row.recovery.providerImport, { planEnc: JSON.stringify(plan), @@ -280,7 +617,8 @@ describe("provider import through normal project operations", () => { deployments: { app: "app-build", db: "db-build" }, }); await pauseFailedPlatformImport("run"); - expect(h.disable).toHaveBeenCalledExactlyOnceWith("p-app", "org", "app-build"); + expect(h.disable).toHaveBeenCalledWith("p-app", "org", "app-build"); + expect(h.disable).toHaveBeenCalledWith("p-db", "org", "db-build"); expect(row.recovery.providerImport.cleanupPending).toBe(false); }); it("leaves the cleanup checkpoint pending if the destination cannot be reached", async () => { diff --git a/apps/api/src/modules/migration/railway-wiring.test.ts b/apps/api/src/modules/migration/railway-wiring.test.ts index f0ab87402..d21c42e47 100644 --- a/apps/api/src/modules/migration/railway-wiring.test.ts +++ b/apps/api/src/modules/migration/railway-wiring.test.ts @@ -42,6 +42,27 @@ describe("provider-independent connection values", () => { }); }); describe("Railway project wiring", () => { + it("preserves self references and repeated case-insensitive dependency hosts in one connection", () => { + const db = service("db", {}, 5432); + const app = service("app", { + HOSTS: "DB.railway.internal,db.railway.internal,app.railway.internal", + }); + expect( + resolveRailwayBindings( + app, + new Map([ + ["db", db], + ["app", app], + ]), + ), + ).toEqual([ + { + sourceId: "db", + envKey: "HOSTS", + valueTemplate: "${host},${host},import-app", + }, + ]); + }); it("binds any private service without inferring its type from its image or variable names", () => { const store = service("store"); const app = service("app", { @@ -62,7 +83,9 @@ describe("Railway project wiring", () => { }, ]); expect(store.source.port).toBe(7878); - expect(app.source.env).toEqual({}); + expect(app.source.env).toEqual({ + ARBITRARY_SETTING: "custom://alice:password@import-store:7878/second-db?option=1", + }); }); it("keeps literal shared secrets and rewrites self-hosts without a recursive connection", () => { const store = service("store", { TOKEN: "secret", HOST: "store.railway.internal" }, 7878); @@ -78,4 +101,23 @@ describe("Railway project wiring", () => { expect(app.source.blockers.join(" ")).toMatch(/MISSING/); expect(app.source.blockers.join(" ")).toMatch(/UNRESOLVED/); }); + it("keeps hostname-only and multi-host values on the native project network", () => { + const db = service("db"), + redis = service("redis"); + const app = service("app", { + HOST: "db.railway.internal", + HOSTS: "db.railway.internal,redis.railway.internal", + }); + const bindings = resolveRailwayBindings( + app, + new Map([ + ["db", db], + ["redis", redis], + ["app", app], + ]), + ); + expect(app.source.env).toEqual({ HOST: "import-db", HOSTS: "import-db,import-redis" }); + expect(new Set(bindings.map((binding) => binding.sourceId))).toEqual(new Set(["db", "redis"])); + expect(app.source.blockers).toEqual([]); + }); }); diff --git a/apps/api/src/modules/projects/folder/folder.service.test.ts b/apps/api/src/modules/projects/folder/folder.service.test.ts index 02b956160..ac85c40ac 100644 --- a/apps/api/src/modules/projects/folder/folder.service.test.ts +++ b/apps/api/src/modules/projects/folder/folder.service.test.ts @@ -59,7 +59,7 @@ describe("scanFolderSession", () => { expect(resolveFromLocalMock).toHaveBeenCalledWith(stagingDir, { env: { MY_VERSION: "1.2.3" }, composePath: "deploy/compose.yaml", - }); + }, "app"); expect(result.services).toBe(refreshed); expect(upload.services).toBe(initial); }); diff --git a/apps/api/src/modules/projects/project-cleanup.ref-shape.test.ts b/apps/api/src/modules/projects/project-cleanup.ref-shape.test.ts index f66e072ba..ef4088610 100644 --- a/apps/api/src/modules/projects/project-cleanup.ref-shape.test.ts +++ b/apps/api/src/modules/projects/project-cleanup.ref-shape.test.ts @@ -225,7 +225,7 @@ describe("Cloud Docker project teardown", () => { slug, domain: "opsh.io", namespace: fixture.namespace, - source_workspace_id: fixture.scope.workspaceId, + source_vm_id: fixture.scope.workspaceId, exported_path: `${cloudDockerProjectPaths(owner).routes}/${slug}`, }); return { ...fixture, addPage }; diff --git a/apps/api/src/modules/projects/project-clone.test.ts b/apps/api/src/modules/projects/project-clone.test.ts index 497aa8279..864a94be8 100644 --- a/apps/api/src/modules/projects/project-clone.test.ts +++ b/apps/api/src/modules/projects/project-clone.test.ts @@ -32,8 +32,7 @@ const h = { deletedAt: null, deletionInProgress: false, lifecycleOperation: { kind: "delete", status: "failed", error: "Previous cleanup failure" }, - favicon: "https://clincai.example/favicon.ico", - faviconCheckedAt: new Date(), + iconUrl: "https://clincai.example/icon.png", compositeRoutes: [{ host: "clincai.example" }], workspaceId: null, gitProvider: "github", @@ -194,6 +193,7 @@ describe("the copy direction is COPY-by-default", () => { await clone(); const p = clonedProject(); expect(p.framework).toBe("nextjs"); + expect(p.iconUrl).toBe("https://clincai.example/icon.png"); expect(p.routeStrategy).toBe("loopback"); expect(p.rollbackWindow).toBe(5); // Same for the service rows: a build command exists in no container. @@ -231,7 +231,7 @@ describe("what it deliberately does NOT copy", () => { const p = clonedProject(); // `id`/`groupId` are the insert's, and name/slug/serverId/workspaceId/activeDeploymentId are set // explicitly above — the rest must simply be absent. - for (const field of ["deletedAt", "deletionInProgress", "lifecycleOperation", "disabledAt", "favicon", "faviconCheckedAt", "compositeRoutes", "cloudPromotion", "id", "groupId", "createdAt", "updatedAt"]) { + for (const field of ["deletedAt", "deletionInProgress", "lifecycleOperation", "disabledAt", "compositeRoutes", "cloudPromotion", "id", "groupId", "createdAt", "updatedAt"]) { expect(p, field).not.toHaveProperty(field); } }); diff --git a/apps/api/src/modules/system/setup.controller.ts b/apps/api/src/modules/system/setup.controller.ts index 47a90997f..4770e16f6 100644 --- a/apps/api/src/modules/system/setup.controller.ts +++ b/apps/api/src/modules/system/setup.controller.ts @@ -356,7 +356,7 @@ export async function resetAdminPassword(c: Context) { // multi-user box the reset never lands on an arbitrary member row. const admin = await repos.user.findFoundingAdmin(); if (!admin) { - return c.json({ error: "No admin account exists yet — run `openship` to create one." }, 409); + return c.json({ error: "No admin account exists yet — run `openship` and choose Create first admin, or run `openship up --non-interactive --admin-email ` with OPENSHIP_ADMIN_PASSWORD set." }, 409); } const email = diff --git a/apps/api/test/e2e/catalog-inline-build.e2e.test.ts b/apps/api/test/e2e/catalog-inline-build.e2e.test.ts index a78afccd9..4acac1da7 100644 --- a/apps/api/test/e2e/catalog-inline-build.e2e.test.ts +++ b/apps/api/test/e2e/catalog-inline-build.e2e.test.ts @@ -42,7 +42,10 @@ import { describeDockerE2E, dockerSocketPath, requireDocker } from "../helpers/d // real @repo/db. const { listByProjectMock } = vi.hoisted(() => ({ listByProjectMock: vi.fn() })); vi.mock("@repo/db", () => ({ - repos: { service: { listByProject: listByProjectMock } }, + repos: { + service: { listByProject: listByProjectMock }, + project: { getEnvMap: async () => ({}) }, + }, })); import { buildComposeImages } from "@repo/platform/engine/modules/deployments/compose/build.service"; @@ -136,6 +139,7 @@ describeDockerE2E("catalog inline build through the real build pipeline", () => dep: { id: "d-widget", branch: "main", + environment: "production", commitSha: null, trigger: "deploy", meta: null, diff --git a/apps/api/test/e2e/cloud-shared-workspace.e2e.test.ts b/apps/api/test/e2e/cloud-shared-workspace.e2e.test.ts index e7c29259f..15bc3624e 100644 --- a/apps/api/test/e2e/cloud-shared-workspace.e2e.test.ts +++ b/apps/api/test/e2e/cloud-shared-workspace.e2e.test.ts @@ -152,13 +152,13 @@ describeDockerE2E("multiple projects on one subscribed Docker workspace", () => }; const vm = { id: providerVm, namespace, status: "running" }; const provider = { - workspaces: { + vms: { get: async () => vm, create: () => { throw new Error("Project operation attempted to create a second host"); }, }, - workspace: () => ({ + vm: () => ({ get: async () => vm, runtime: async () => providerRuntime, workloads: { list: async () => [] }, @@ -185,7 +185,7 @@ describeDockerE2E("multiple projects on one subscribed Docker workspace", () => domain: "opsh.io", url: `https://${input.slug}.opsh.io`, namespace, - source_workspace_id: input.workspace_id, + source_vm_id: input.vm_id, exported_path: input.path, }; pages.set(input.slug, page); @@ -359,7 +359,7 @@ describeDockerE2E("multiple projects on one subscribed Docker workspace", () => ); expect((await a.listAllContainers()).map((row) => row.id)).toEqual([aId]); expect(routes.get(`${ids[0]}.opsh.io`).routes[0].action).toMatchObject({ - workspace: providerVm, + vm: providerVm, port: first.hostPortByContainerPort?.[8080], }); }); diff --git a/apps/api/test/e2e/fixtures/instance-api.ts b/apps/api/test/e2e/fixtures/instance-api.ts index 97f198dea..7bf882126 100644 --- a/apps/api/test/e2e/fixtures/instance-api.ts +++ b/apps/api/test/e2e/fixtures/instance-api.ts @@ -135,7 +135,7 @@ export async function startApi(input: { } export async function stopApi(api: RunningApi): Promise { - if (api.child.exitCode === null) { + if (api.child.exitCode === null && api.child.signalCode === null) { api.child.kill("SIGTERM"); const exited = new Promise((resolve) => api.child.once("exit", () => resolve(true))); if (!(await Promise.race([exited, delay(15_000).then(() => false)]))) { diff --git a/apps/api/test/e2e/fixtures/mail-backup/Dockerfile b/apps/api/test/e2e/fixtures/mail-backup/Dockerfile new file mode 100644 index 000000000..71cda3c5f --- /dev/null +++ b/apps/api/test/e2e/fixtures/mail-backup/Dockerfile @@ -0,0 +1,15 @@ +FROM oven/bun:1.4.4 AS bun +FROM debian:12-slim +ENV DEBIAN_FRONTEND=noninteractive container=docker +RUN printf '#!/bin/sh\nexit 101\n' > /usr/sbin/policy-rc.d \ + && chmod +x /usr/sbin/policy-rc.d \ + && apt-get update && apt-get install -y --no-install-recommends \ + systemd systemd-sysv sudo postgresql dovecot-core dovecot-imapd \ + postfix amavisd-new zstd curl ca-certificates openssl netcat-openbsd \ + && rm -rf /var/lib/apt/lists/* \ + && groupadd -g 2000 vmail && useradd -u 2000 -g vmail -d /var/vmail vmail +COPY --from=bun /usr/local/bin/bun /usr/local/bin/bun +RUN apt-get update && apt-get install -y --no-install-recommends openssh-server dovecot-pgsql \ + && rm -rf /var/lib/apt/lists/* +STOPSIGNAL SIGRTMIN+3 +CMD ["/sbin/init"] diff --git a/apps/api/test/e2e/fixtures/mail-backup/Dockerfile.engine b/apps/api/test/e2e/fixtures/mail-backup/Dockerfile.engine new file mode 100644 index 000000000..9ffeba4fe --- /dev/null +++ b/apps/api/test/e2e/fixtures/mail-backup/Dockerfile.engine @@ -0,0 +1,8 @@ +FROM openship-mail-backup-test:1602 +RUN apt-get update && apt-get install -y --no-install-recommends supervisor \ + && rm -rf /var/lib/apt/lists/* +# Different IDs prove that restore uses the destination's service users. +RUN groupmod -g 2101 vmail && usermod -u 2101 -g 2101 vmail \ + && groupmod -g 2301 amavis && usermod -u 2301 -g 2301 amavis +COPY apps/api/test/e2e/fixtures/mail-backup/supervisord.conf /etc/supervisor/supervisord.conf +CMD ["/usr/bin/supervisord", "-n", "-c", "/etc/supervisor/supervisord.conf"] diff --git a/apps/api/test/e2e/fixtures/mail-backup/Dockerfile.host b/apps/api/test/e2e/fixtures/mail-backup/Dockerfile.host new file mode 100644 index 000000000..9b99c8a61 --- /dev/null +++ b/apps/api/test/e2e/fixtures/mail-backup/Dockerfile.host @@ -0,0 +1,6 @@ +FROM docker:28-dind +RUN apk add --no-cache openssh-server sudo tar zstd \ + && ssh-keygen -A && mkdir -p /root/.ssh && chmod 0700 /root/.ssh \ + && passwd -d root +# The test starts sshd after installing an ephemeral public key. +ENV DOCKER_TLS_CERTDIR="" diff --git a/apps/api/test/e2e/fixtures/mail-backup/roundtrip.ts b/apps/api/test/e2e/fixtures/mail-backup/roundtrip.ts new file mode 100644 index 000000000..e43714041 --- /dev/null +++ b/apps/api/test/e2e/fixtures/mail-backup/roundtrip.ts @@ -0,0 +1,501 @@ +/** Runs inside a disposable Linux host, so the local transport is exercised on + * the real PostgreSQL/Postfix/Dovecot filesystem, never the developer's host. */ +import assert from "node:assert/strict"; +import { Readable } from "node:stream"; +import { readFile, readdir, stat } from "node:fs/promises"; +import { join } from "node:path"; +import { createHash, randomBytes } from "node:crypto"; +import { LocalExecutor, initPlatform, resetPlatform, type CommandExecutor } from "@repo/adapters"; +import { shellQuote as sq } from "@repo/core"; +import { repos, closeDb } from "@repo/db"; +import { encrypt, decrypt } from "@repo/platform/engine/lib/encryption"; +import { encryptSecretField } from "@repo/platform/engine/lib/credential-encryption"; +import { resolvePlatformConfig } from "@repo/platform/engine/lib/platform-config"; +import { buildBackgroundContext } from "@repo/platform/engine/lib/background-context"; +import { sshManager } from "@repo/platform/engine/lib/ssh-manager"; +import { + makeFreshState, + readState, + writeState, +} from "@repo/platform/engine/modules/mail/mail-state"; +import { backupOrchestrator } from "@repo/platform/engine/modules/backups/backup.orchestrator"; +import { restoreOrchestrator } from "@repo/platform/engine/modules/backups/restore.orchestrator"; +import { + mailEngineCommand, + mailHostPath, + mailPsqlFromStdin, + pauseMailDaemons, + type MailEngineFlavor, +} from "@repo/platform/engine/modules/mail/mail-engine"; +import { + seedOrg, + seedBackupDestination, + seedBackupPolicy, + seedBackupRun, +} from "../../../helpers/seed"; + +const local = new LocalExecutor(); +const domain = "example.test"; +const user = `alice@${domain}`; +const password = "mail-restore-fixture-password"; +const flavors = new WeakMap(); +const flavorFor = (exec: CommandExecutor) => flavors.get(exec) ?? "host"; +const mail = (exec: CommandExecutor, command: string) => + exec.exec(mailEngineCommand(flavorFor(exec), `sh -ec ${sq(command)}`)); +const mailFile = (exec: CommandExecutor, path: string) => + exec.readFile(mailHostPath(flavorFor(exec), path)); +const sql = (exec: CommandExecutor, statement: string, db = "vmail") => + exec.exec( + `${flavorFor(exec) === "container" ? "docker exec openship-mail-db psql -U postgres" : "sudo -u postgres psql"} -v ON_ERROR_STOP=1 -d ${sq(db)} -Atc ${sq(statement)}`, + ); +const sha = (text: string) => createHash("sha256").update(text).digest("hex"); + +async function setup( + exec: CommandExecutor, + serverId: string, + dbPassword: string, + source: boolean, + flavor: MailEngineFlavor = "host", +) { + flavors.set(exec, flavor); + if (flavor === "host") await exec.exec("systemctl start postgresql"); + await sql( + exec, + `CREATE ROLE vmail LOGIN PASSWORD '${dbPassword}'; CREATE ROLE vmailadmin LOGIN; CREATE ROLE amavisd LOGIN; CREATE ROLE iredapd LOGIN; CREATE ROLE fail2ban LOGIN`, + "postgres", + ); + for (const name of ["vmail", "amavisd", "iredapd", "fail2ban"]) { + if (flavor === "container" && name === "vmail") continue; + await exec.exec( + `${flavor === "container" ? "docker exec openship-mail-db createdb -U postgres" : "sudo -u postgres createdb"} ${name}`, + ); + } + const schema = await readFile("apps/email/engine/samples/iredmail/iredmail.pgsql", "utf8"); + assert.equal( + (await exec.execWithInput!(mailPsqlFromStdin(flavor), Readable.from([schema]))).code, + 0, + ); + await sql( + exec, + `GRANT SELECT ON ALL TABLES IN SCHEMA public TO vmail; + CREATE SCHEMA mail_app; + CREATE TABLE mail_app.notes(id serial PRIMARY KEY, body text NOT NULL); + INSERT INTO mail_app.notes(body) VALUES ('${source ? "source-note" : "target-note"}'); + INSERT INTO domain(domain) VALUES ('example.test'),('second.test'); + INSERT INTO mailbox(username,password,domain,maildir,storagebasedirectory,storagenode) VALUES ('${user}','{PLAIN}${password}','example.test','example.test/alice/','/var/vmail','vmail1'); + INSERT INTO alias(address,domain) VALUES ('team@example.test','example.test'); + INSERT INTO forwardings(address,forwarding,domain) VALUES ('team@example.test','${user}','example.test'); + INSERT INTO alias_domain(alias_domain,target_domain) VALUES ('alias.test','example.test'); + INSERT INTO sender_bcc_user(username,bcc_address,domain) VALUES ('${user}','audit@example.test','example.test');`, + ); + for (const name of ["amavisd", "iredapd", "fail2ban"]) { + await sql( + exec, + `CREATE TABLE restored_rows(id serial PRIMARY KEY, body text); INSERT INTO restored_rows(body) VALUES ('${source ? "source" : "target"}-${name}');`, + name, + ); + } + await mail( + exec, + `sh -ec ${sq(`install -d -o vmail -g vmail /var/vmail/vmail1/example.test/alice/Maildir/{cur,new,tmp} + chown -R vmail:vmail /var/vmail + install -d -m 0700 /var/lib/dkim + openssl genrsa -out /var/lib/dkim/example.test.pem 2048 2>/dev/null + openssl genrsa -out /var/lib/dkim/second.test.pem 2048 2>/dev/null + chown -R amavis:amavis /var/lib/dkim + chmod 0700 /var/lib/dkim + chmod 0600 /var/lib/dkim/*.pem`)}`, + ); + const config = `protocols = imap +listen = 127.0.0.1 +ssl = no +disable_plaintext_auth = no +mail_location = maildir:/var/vmail/vmail1/%d/%n/Maildir +auth_mechanisms = plain login +passdb { + driver = sql + args = /etc/dovecot/backup-test-sql.conf +} +userdb { + driver = static + args = uid=vmail gid=vmail home=/var/vmail/vmail1/%d/%n +} +service auth-worker { + user = root +} +`; + await exec.writeFile(mailHostPath(flavor, "/etc/dovecot/dovecot.conf"), config); + await exec.writeFile( + mailHostPath(flavor, "/etc/dovecot/backup-test-sql.conf"), + `driver = pgsql\nconnect = host=127.0.0.1 dbname=vmail user=vmail password=${dbPassword}\npassword_query = SELECT password FROM mailbox WHERE username = '%u' AND active = 1\n`, + ); + await mail(exec, "chmod 0600 /etc/dovecot/backup-test-sql.conf"); + const amavis = `use strict; +$mydomain = 'example.test'; +$myhostname = 'mail.example.test'; +$daemon_user = 'amavis'; $daemon_group = 'amavis'; +$MYHOME = '/var/lib/amavis'; +$TEMPBASE = "$MYHOME/tmp"; +$inet_socket_bind = '127.0.0.1'; $inet_socket_port = 10024; +@local_domains_maps = (1); +@bypass_virus_checks_maps = (1); @bypass_spam_checks_maps = (1); +$forward_method = undef; $notify_method = undef; +$do_syslog = 0; $max_servers = 1; +dkim_key('example.test', 'dkim', '/var/lib/dkim/example.test.pem'); +dkim_key('second.test', 'dkim', '/var/lib/dkim/second.test.pem'); +@dkim_signature_options_bysender_maps = ( { + '.example.test' => { d => 'example.test' }, + '.second.test' => { d => 'second.test' }, +} ); +1; +`; + await exec.writeFile(mailHostPath(flavor, "/etc/amavis/conf.d/50-user"), amavis); + await mail( + exec, + `sh -ec ${sq(`install -d -o amavis -g amavis /var/lib/amavis/tmp /run/amavis; postfix check; ${flavor === "host" ? "systemctl restart amavis postfix dovecot" : "supervisorctl start amavis postfix dovecot"}`)}`, + ); + const state = makeFreshState(serverId, domain); + state.secrets = { + VMAIL_DB_BIND_PASSWD: dbPassword, + DOMAIN_ADMIN_PASSWD_PLAIN: "postmaster-fixture", + }; + state.platformMailbox = { + email: user, + password: encrypt(password), + smtpHost: "mail.example.test", + smtpPort: 587, + secure: false, + updatedAt: new Date().toISOString(), + }; + state.dnsAcknowledged = true; + state.ptrAcknowledged = true; + state.finishedAt = new Date().toISOString(); + await writeState(exec, state); + await mail( + exec, + `sh -ec ${sq(`install -d -o amavis -g amavis /var/lib/amavis/quarantine /var/lib/amavis/db + printf 'Subject: Retained quarantine\\n\\nQuarantined message\\n' > /var/lib/amavis/quarantine/${source ? "source" : "target"}-marker + printf filter-state > /var/lib/amavis/db/${source ? "source" : "target"}-marker + chown -R amavis:amavis /var/lib/amavis/quarantine /var/lib/amavis/db`)}`, + ); + if (source) + await exec.exec( + "install -d /srv; mv /var/vmail /srv/mail-data; ln -s /srv/mail-data /var/vmail", + ); + if (source) + await exec.exec( + "mkdir -p /var/vmail/link-test; printf preserved-link-data > /var/vmail/link-test/a; ln /var/vmail/link-test/a /var/vmail/link-test/b; ln -s a /var/vmail/link-test/relative", + ); + // Deliver via Dovecot, rather than merely dropping a file into an assumed path. + if (source) { + const body = + "Subject: Preserved attachment\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=fixture\n\n--fixture\nContent-Type: text/plain\n\nPreserve this mail.\n--fixture\nContent-Type: application/octet-stream\nContent-Transfer-Encoding: base64\n\n" + + Buffer.from(Array.from({ length: 256 }, (_, i) => i)).toString("base64") + + "\n--fixture--\n"; + const result = await exec.execWithInput!( + `doveadm save -u ${sq(user)} -m INBOX`, + Readable.from([body]), + ); + assert.equal(result.code, 0, result.stderr); + await exec.exec(`doveadm mailbox create -u ${sq(user)} Archive`); + assert.equal( + ( + await exec.execWithInput!( + `doveadm save -u ${sq(user)} -m Archive`, + Readable.from(["Subject: Saved folder\n\nArchive message\n"]), + ) + ).code, + 0, + ); + await exec.exec( + "printf 'Subject: Queued mail\n\nHold this message\n' | sendmail -f alice@example.test nobody@example.invalid; postsuper -h ALL", + ); + } +} + +async function waitForRestore(id: string, wanted: string) { + const deadline = Date.now() + 120_000; + while (Date.now() < deadline) { + const row = await repos.backupRestore.findById(id); + if (row?.status === wanted) return row; + assert( + !["failed", "cancelled", "succeeded"].includes(row?.status ?? ""), + row?.errorMessage ?? `unexpected ${row?.status}`, + ); + await new Promise((r) => setTimeout(r, 50)); + } + throw new Error(`Restore ${id} did not become ${wanted}`); +} + +try { + await initPlatform(resolvePlatformConfig()); + const org = await seedOrg({ ownsHost: true }); + const ctx = buildBackgroundContext(org); + const source = await repos.server.create({ + organizationId: org.organizationId, + name: "This server", + isLocal: true, + sshHost: "127.0.0.1", + }); + const target = await repos.server.create({ + organizationId: org.organizationId, + name: "Mail destination", + sshHost: "mail-target", + sshUser: "root", + sshAuthMethod: "key", + sshPrivateKey: encryptSecretField(await readFile("/tmp/mail-test-key", "utf8")), + }); + await repos.mailServer.markInstalled(source.id, domain); + await repos.mailServer.markInstalled(target.id, domain); + await setup(local, source.id, "source-db-password", true); + await sshManager.withExecutor(target.id, (exec) => + setup( + exec, + target.id, + "target-db-password", + false, + process.env.MAIL_TEST_TARGET_FLAVOR as MailEngineFlavor, + ), + ); + if (process.env.MAIL_TEST_TARGET_FLAVOR === "container") { + await sshManager.withExecutor(target.id, async (exec) => { + const paused = await pauseMailDaemons(exec, "container"); + // Supervisor's STOPPED verdict alone is insufficient: the old image's + // start-fg wrapper can leave Postfix's master and queue writers alive. + await mail(exec, "if postfix status >/dev/null 2>&1; then exit 1; fi"); + await paused.resume(); + await mail(exec, "postfix status"); + }); + } + + const original = await local.exec(`doveadm fetch -u ${sq(user)} 'mailbox text' all`); + const keys = await local.exec("sha256sum /var/lib/dkim/*.pem"); + const mailboxHash = sha(original); + const destination = await seedBackupDestination(org.organizationId, { + kind: "local", + endpoint: "/var/lib/openship/backups/mail-test", + }); + const payload = { + payloadKind: "custom_command" as const, + payloadConfig: { mail: { messageData: true, keys: true } }, + }; + const policy = await seedBackupPolicy(destination.id, { + sourceKind: "mail_server", + mailServerId: source.id, + payloadKind: payload.payloadKind, + payloadConfig: payload.payloadConfig, + }); + const run = await seedBackupRun(org.organizationId, { + policyId: policy.id, + destinationId: destination.id, + mailServerId: source.id, + sourceKind: "mail_server", + status: "queued", + }); + await backupOrchestrator.execute(run.id); + const captured = await repos.backupRun.findById(run.id); + assert.equal(captured?.status, "succeeded", captured?.errorMessage ?? undefined); + assert((captured?.bytesTransferred ?? 0) > 0); + const artifacts = captured!.artifacts as Array<{ + key: string; + metadata: Record; + }>; + assert.deepEqual(artifacts[0]!.metadata.mail, { + version: 2, + domain, + messageData: true, + keys: true, + }); + assert( + !JSON.stringify(captured).includes(password), + "portable passwords leaked into run metadata", + ); + assert.equal( + (await readdir("/root/.openship")).filter((name) => name.startsWith("mail-backup-")).length, + 0, + ); + await local.exec("systemctl is-active --quiet postfix dovecot amavis"); + + async function restore( + mode: "in_place" | "to_fork", + runId = run.id, + wanted = "succeeded", + forkId = target.id, + ) { + const token = randomBytes(24).toString("base64url"); + const prepared = await restoreOrchestrator.beginPrepare({ + runId, + mode, + ...(mode === "to_fork" ? { forkMailServerId: forkId } : {}), + confirmationToken: token, + trigger: { source: "manual", userId: org.userId }, + }); + await waitForRestore(prepared.restoreId, "prepared"); + await restoreOrchestrator.apply(ctx, prepared.restoreId, prepared.confirmationToken); + return waitForRestore(prepared.restoreId, wanted); + } + await local.exec(`doveadm expunge -u ${sq(user)} mailbox '*' all`); + await sql( + local, + "DELETE FROM alias; DELETE FROM sender_bcc_user; UPDATE mail_app.notes SET body='changed'", + ); + await restore("in_place"); + await local.exec( + 'test -L /var/vmail; test /var/vmail/link-test/a -ef /var/vmail/link-test/b; test "$(readlink /var/vmail/link-test/relative)" = a', + ); + assert.equal( + sha(await local.exec(`doveadm fetch -u ${sq(user)} 'mailbox text' all`)), + mailboxHash, + ); + assert.equal((await sql(local, "SELECT count(*) FROM alias")).trim(), "1"); + assert.equal((await sql(local, "SELECT body FROM mail_app.notes")).trim(), "source-note"); + await restore("to_fork"); + await sshManager.withExecutor(target.id, async (exec) => { + assert.equal( + sha(await mail(exec, `doveadm fetch -u ${sq(user)} 'mailbox text' all`)), + mailboxHash, + ); + assert.equal((await mail(exec, "sha256sum /var/lib/dkim/*.pem")).trim(), keys.trim()); + assert.equal((await sql(exec, "SELECT body FROM mail_app.notes")).trim(), "source-note"); + assert.equal((await sql(exec, "SELECT count(*) FROM sender_bcc_user")).trim(), "1"); + for (const db of ["amavisd", "iredapd", "fail2ban"]) + assert.equal((await sql(exec, "SELECT body FROM restored_rows", db)).trim(), `source-${db}`); + const state = await readState(exec, { strict: true }); + assert.equal(state?.serverId, target.id); + assert.equal(state?.secrets.VMAIL_DB_BIND_PASSWD, "target-db-password"); + assert.equal(decrypt(state!.platformMailbox!.password), password); + assert.equal(state?.dnsAcknowledged, false); + assert(state?.additionalDomains?.["second.test"]?.records.dkim); + assert( + (await mailFile(exec, "/etc/dovecot/backup-test-sql.conf")).includes("target-db-password"), + ); + assert( + (await mail(exec, `doveadm auth test ${sq(user)} ${sq(password)}`)).includes( + "auth succeeded", + ), + ); + assert((await mail(exec, "postqueue -p")).includes("nobody@example.invalid")); + await mail( + exec, + 'test /var/vmail/link-test/a -ef /var/vmail/link-test/b; test "$(readlink /var/vmail/link-test/relative)" = a', + ); + assert( + (await mail(exec, "cat /var/lib/amavis/quarantine/source-marker")).includes( + "Quarantined message", + ), + ); + assert.equal((await mail(exec, "cat /var/lib/amavis/db/source-marker")).trim(), "filter-state"); + await mail(exec, "test ! -e /var/lib/amavis/quarantine/target-marker"); + if (flavorFor(exec) === "host") + await exec.exec("systemctl is-active --quiet postfix dovecot amavis"); + else + assert( + (await mail(exec, "supervisorctl status postfix dovecot amavis")) + .split("\n") + .every((line) => line.includes("RUNNING")), + ); + assert((await repos.mailServer.get(target.id))?.installedAt); + }); + if (process.env.MAIL_TEST_TARGET_FLAVOR === "container") { + const reversePolicy = await seedBackupPolicy(destination.id, { + sourceKind: "mail_server", + mailServerId: target.id, + payloadKind: payload.payloadKind, + payloadConfig: payload.payloadConfig, + }); + const reverseRun = await seedBackupRun(org.organizationId, { + policyId: reversePolicy.id, + destinationId: destination.id, + mailServerId: target.id, + sourceKind: "mail_server", + status: "queued", + }); + await backupOrchestrator.execute(reverseRun.id); + const reverse = await repos.backupRun.findById(reverseRun.id); + assert.equal(reverse?.status, "succeeded", reverse?.errorMessage ?? undefined); + await local.exec(`doveadm expunge -u ${sq(user)} mailbox '*' all`); + await restore("to_fork", reverseRun.id, "succeeded", source.id); + assert.equal( + sha(await local.exec(`doveadm fetch -u ${sq(user)} 'mailbox text' all`)), + mailboxHash, + ); + assert.equal( + (await readState(local, { strict: true }))?.secrets.VMAIL_DB_BIND_PASSWD, + "source-db-password", + ); + } + // A requested secret cannot silently disappear, and failure must undo the + // writer pause even though the archive stream failed before upload completed. + await local.exec("mv /usr/bin/pg_dump /usr/bin/pg_dump-held"); + const failedCapture = await seedBackupRun(org.organizationId, { + policyId: policy.id, + destinationId: destination.id, + mailServerId: source.id, + sourceKind: "mail_server", + status: "queued", + }); + await backupOrchestrator.execute(failedCapture.id); + assert.equal((await repos.backupRun.findById(failedCapture.id))?.status, "failed"); + await local.exec( + "systemctl is-active --quiet postfix dovecot amavis; mv /usr/bin/pg_dump-held /usr/bin/pg_dump", + ); + assert.equal( + (await readdir("/root/.openship")).filter((name) => name.startsWith("mail-backup-")).length, + 0, + ); + + // A syntactically valid archive whose database replay fails must roll back + // that database, report the failed restore, and keep mail stopped for review. + const archive = join(destination.endpoint!, artifacts[0]!.key); + const broken = join(destination.endpoint!, "broken.sql.tar.zst"); + await local.exec(`mkdir -m 0700 /tmp/mail-broken; zstd -dc ${sq(archive)} | tar -x -C /tmp/mail-broken + printf '\\nSELECT nonexistent_restore_column;\\n' >> /tmp/mail-broken/vmail.sql + tar -c -C /tmp/mail-broken . | zstd -c > ${sq(broken)}`); + await sql(local, "UPDATE mail_app.notes SET body='must-survive-failed-restore'"); + const brokenRun = await seedBackupRun(org.organizationId, { + policyId: policy.id, + destinationId: destination.id, + mailServerId: source.id, + sourceKind: "mail_server", + status: "succeeded", + artifacts: [ + { + ...artifacts[0]!, + key: "broken.sql.tar.zst", + sizeBytes: (await stat(broken)).size, + sha256: createHash("sha256") + .update(await readFile(broken)) + .digest("hex"), + }, + ], + }); + const failedRestore = await restore("in_place", brokenRun.id, "failed"); + assert.match(failedRestore.errorMessage ?? "", /nonexistent_restore_column/); + assert.equal( + (await sql(local, "SELECT body FROM mail_app.notes")).trim(), + "must-survive-failed-restore", + ); + assert.equal((failedRestore.meta as Record).serviceLeftStopped, true); + assert.equal((failedRestore.meta as Record).partialWrite, true); + assert.equal((await local.exec("systemctl is-active dovecot || true")).trim(), "inactive"); + assert.equal( + (await readdir("/root/.openship")).filter((name) => name.startsWith("mail-backup-")).length, + 0, + ); + // Explicit retry repairs the stopped partial restore and resumes only services + // that were running when it was requested. + await local.exec("systemctl start postfix dovecot amavis"); + await restore("in_place"); + assert.equal( + sha(await local.exec(`doveadm fetch -u ${sq(user)} 'mailbox text' all`)), + mailboxHash, + ); + console.log( + "MAIL_BACKUP_ROUNDTRIP_OK: local capture, in-place restore, SSH migration, messages/attachments/folders, queue, all mail tables, keys, destination credentials, registration, failed capture recovery and failed SQL rollback", + ); +} finally { + await local.dispose(); + await sshManager.destroy(); + resetPlatform(); + await closeDb(); +} diff --git a/apps/api/test/e2e/fixtures/mail-backup/supervisord.conf b/apps/api/test/e2e/fixtures/mail-backup/supervisord.conf new file mode 100644 index 000000000..adb6f31da --- /dev/null +++ b/apps/api/test/e2e/fixtures/mail-backup/supervisord.conf @@ -0,0 +1,22 @@ +[supervisord] +user=root +logfile=/tmp/supervisord.log +pidfile=/run/supervisord.pid +[unix_http_server] +file=/run/supervisor.sock +[rpcinterface:supervisor] +supervisor.rpcinterface_factory=supervisor.rpcinterface:make_main_rpcinterface +[supervisorctl] +serverurl=unix:///run/supervisor.sock +[program:postfix] +command=/usr/sbin/postfix start-fg +autostart=false +autorestart=true +[program:dovecot] +command=/usr/sbin/dovecot -F +autostart=false +autorestart=true +[program:amavis] +command=/usr/sbin/amavisd foreground +autostart=false +autorestart=true diff --git a/apps/api/test/e2e/instance-handoff.e2e.test.ts b/apps/api/test/e2e/instance-handoff.e2e.test.ts index 0bb185c46..e39c61903 100644 --- a/apps/api/test/e2e/instance-handoff.e2e.test.ts +++ b/apps/api/test/e2e/instance-handoff.e2e.test.ts @@ -5,7 +5,7 @@ import { createRequire } from "node:module"; import { execFile } from "node:child_process"; import { promisify } from "node:util"; import { afterAll, expect, it } from "vitest"; -import { createServer, type Server } from "node:http"; +import { createServer, request as httpRequest, type Server } from "node:http"; import { createHmac } from "node:crypto"; import { freePort, startApi, stopApi, jsonRequest, type RunningApi } from "./fixtures/instance-api"; import type { DataTransferFile } from "../../src/modules/system/data-transfer/types"; @@ -433,6 +433,47 @@ it("resumes after a lost activation acknowledgement without reimporting or start ).toBe(newer.id); }, 120_000); +it.each(["resume", "cancel"] as const)("recovers a persisted quiescing source after restart with %s", async action => { + const { source, target } = await pair(); + const original = await createProject(source, "Before interrupted drain"); + const offer = await post<{ code: string }>(target, "/api/system/instance/offer", { direction: "target" }); + // A real admitted mutation waits for its remaining request body. The move must + // keep waiting for it, rather than treating an incomplete drain as a snapshot. + const held = httpRequest(`${source.baseUrl}/api/projects`, { + method: "POST", headers: { "content-type": "application/json" }, + }); + held.on("error", () => { /* The fixture intentionally interrupts this request. */ }); + held.write('{"name":'); + let restarted: RunningApi | undefined; + try { + await post(source, "/api/system/instance/move", { code: offer.code, confirmReplace: true }); + await expect.poll(async () => (await state(source)).role, { timeout: 15_000 }).toBe("quiescing"); + const exited = new Promise(resolve => source.child.once("exit", () => resolve())); + source.child.kill("SIGKILL"); // simulate a crash after the durable fence + await exited; + held.destroy(); + restarted = await startApi({ dbDir: source.dbDir, port: source.port, secret: source.secret }); + apis.push(restarted); + expect(await state(restarted)).toMatchObject({ role: "quiescing", handoff: { running: false } }); + expect((await fetch(`${restarted.baseUrl}/api/health`)).status).toBe(200); + expect((await fetch(`${restarted.baseUrl}/api/projects`)).status).toBe(503); + await post(restarted, `/api/system/instance/${action}`); + if (action === "resume") { + await waitMove(restarted); + expect((await state(target)).role).toBe("active"); + expect((await jsonRequest<{ data: { id: string } }>(target.baseUrl, `/api/projects/${original.id}`)).data.id).toBe(original.id); + } else { + expect(await state(restarted)).toMatchObject({ role: "active", handoff: { status: "aborted" } }); + expect((await jsonRequest<{ data: { id: string } }>(restarted.baseUrl, `/api/projects/${original.id}`)).data.id).toBe(original.id); + } + } finally { + held.destroy(); + await stopApi(source); + await stopApi(target); + if (restarted) await stopApi(restarted); + } +}, 120_000); + it("cancels a prepared but unactivated receiver and restores its recovery archive", async () => { const { source, target } = await pair(); const old = await createProject(target, "Receiver original"); diff --git a/apps/api/test/e2e/mail-backup-roundtrip.e2e.test.ts b/apps/api/test/e2e/mail-backup-roundtrip.e2e.test.ts new file mode 100644 index 000000000..a7db18b93 --- /dev/null +++ b/apps/api/test/e2e/mail-backup-roundtrip.e2e.test.ts @@ -0,0 +1,243 @@ +import { execFile, spawn } from "node:child_process"; +import { promisify } from "node:util"; +import { pipeline } from "node:stream/promises"; +import { MAIL_CONTAINER_MOUNTS } from "@repo/adapters"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { mkdtemp, readFile, rm } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { beforeAll, afterAll, expect, it } from "vitest"; +import { shellQuote as sq } from "@repo/core"; +import { describeDockerE2E, requireDocker } from "../helpers/docker-e2e"; + +const exec = promisify(execFile); +const root = join(dirname(fileURLToPath(import.meta.url)), "../../../.."); +for (const targetFlavor of ["host", "container"] as const) { + const prefix = `openship-e2e-mail-backup-${targetFlavor}-${process.pid.toString(36)}`; + const source = `${prefix}-source`; + const target = `${prefix}-target`; + const network = `${prefix}-net`; + const image = "openship-mail-backup-test:1602"; + let keys = ""; + async function docker(args: string[]) { + return exec("docker", args, { timeout: 600_000, maxBuffer: 8 * 1024 * 1024 }); + } + + describeDockerE2E(`mail backup and migration to a ${targetFlavor} mail engine`, () => { + beforeAll(async () => { + await requireDocker(); + await docker([ + "build", + "-f", + join(root, "apps/api/test/e2e/fixtures/mail-backup/Dockerfile"), + "-t", + image, + root, + ]); + if (targetFlavor === "container") { + await docker([ + "build", + "-f", + join(root, "apps/api/test/e2e/fixtures/mail-backup/Dockerfile.host"), + "-t", + "openship-mail-backup-host:1602", + root, + ]); + await docker([ + "build", + "-f", + join(root, "apps/api/test/e2e/fixtures/mail-backup/Dockerfile.engine"), + "-t", + "openship-mail-backup-engine:1602", + root, + ]); + await docker(["pull", "postgres:16-alpine"]); + } + keys = await mkdtemp(join(tmpdir(), "openship-mail-key-")); + await exec( + "bun", + [ + "build", + join(root, "apps/api/test/e2e/fixtures/mail-backup/roundtrip.ts"), + "--target=bun", + "--external=cpu-features", + "--external=@electric-sql/pglite", + `--outdir=${keys}`, + ], + { cwd: root }, + ); + await exec("ssh-keygen", ["-q", "-t", "ed25519", "-N", "", "-f", join(keys, "key")]); + // Both hosts are disposable; no outside SMTP/DNS or host Docker socket. + await docker(["network", "create", "--internal", network]); + for (const name of [source, target]) { + await docker([ + "run", + "-d", + "--name", + name, + "--hostname", + "mail.example.test", + "--privileged", + "--cgroupns=private", + "--tmpfs", + "/run", + "--tmpfs", + "/run/lock", + "--network", + network, + ...(name === target ? ["--network-alias", "mail-target"] : []), + ...(name === target && targetFlavor === "container" + ? [ + "-v", + `${prefix}-docker:/var/lib/docker`, + "openship-mail-backup-host:1602", + "--host=unix:///var/run/docker.sock", + "--bridge=none", + "--iptables=false", + "--storage-driver=vfs", + ] + : [image]), + ]); + const deadline = Date.now() + 60_000; + for (;;) { + try { + await docker( + name === target && targetFlavor === "container" + ? ["exec", name, "docker", "info"] + : ["exec", name, "systemctl", "is-active", "--quiet", "multi-user.target"], + ); + break; + } catch (error) { + if (Date.now() >= deadline) throw error; + await new Promise((r) => setTimeout(r, 200)); + } + } + } + if (targetFlavor === "container") { + const save = spawn("docker", [ + "save", + "openship-mail-backup-engine:1602", + "postgres:16-alpine", + ]); + const load = spawn("docker", ["exec", "-i", target, "docker", "load"]); + const errors: Buffer[] = []; + save.stderr.on("data", (chunk) => errors.push(chunk)); + load.stderr.on("data", (chunk) => errors.push(chunk)); + load.stdout.resume(); + const closed = [save, load].map( + (child) => + new Promise((resolve, reject) => { + child.once("error", reject); + child.once("close", (code) => + code === 0 ? resolve() : reject(new Error(Buffer.concat(errors).toString())), + ); + }), + ); + await Promise.all([pipeline(save.stdout, load.stdin), ...closed]); + const command = [ + ...MAIL_CONTAINER_MOUNTS.map((mount) => `mkdir -p ${sq(mount.host)}`), + "docker create --name mail-seed openship-mail-backup-engine:1602", + ...MAIL_CONTAINER_MOUNTS.filter( + (mount) => mount.seed && mount.container.startsWith("/etc/"), + ).map((mount) => `docker cp mail-seed:${mount.container}/. ${sq(mount.host)}`), + "docker rm mail-seed", + "docker run -d --network host --name openship-mail-db -e POSTGRES_DB=vmail -e POSTGRES_PASSWORD=mail-fixture-db-root postgres:16-alpine", + `docker run -d --network host --hostname mail.example.test --name openship-mail ${MAIL_CONTAINER_MOUNTS.map((mount) => `-v ${sq(`${mount.host}:${mount.container}${mount.readonly ? ":ro" : ""}`)}`).join(" ")} openship-mail-backup-engine:1602`, + "for attempt in $(seq 1 100); do if docker exec openship-mail-db pg_isready -U postgres -d vmail >/dev/null 2>&1 && docker exec openship-mail test -S /run/supervisor.sock; then exit 0; fi; sleep 0.1; done; docker logs openship-mail >&2; docker logs openship-mail-db >&2; df -h /var/lib/docker >&2; exit 1", + ].join("\n"); + await docker(["exec", target, "sh", "-ec", command]); + } + // Copy the small bundle and its portable database assets. Bind mounts would + // require the daemon to share the checkout's filesystem (remote/Colima do not). + await docker([ + "exec", + source, + "mkdir", + "-p", + "/workspace/node_modules/@electric-sql/pglite", + "/workspace/apps/email/engine/samples/iredmail", + ]); + await docker(["cp", join(keys, "roundtrip.js"), `${source}:/workspace/roundtrip.js`]); + await docker([ + "cp", + `${join(root, "node_modules/@electric-sql/pglite")}/.`, + `${source}:/workspace/node_modules/@electric-sql/pglite`, + ]); + await docker(["cp", join(root, "packages/db/drizzle"), `${source}:/workspace/drizzle`]); + await docker([ + "cp", + join(root, "apps/email/engine/samples/iredmail/iredmail.pgsql"), + `${source}:/workspace/apps/email/engine/samples/iredmail/iredmail.pgsql`, + ]); + await docker(["cp", join(keys, "key"), `${source}:/tmp/mail-test-key`]); + const publicKey = await readFile(join(keys, "key.pub"), "utf8"); + await docker([ + "exec", + target, + "sh", + "-ec", + `install -d -m 0700 /root/.ssh; printf %s ${sq(publicKey)} > /root/.ssh/authorized_keys; chmod 0600 /root/.ssh/authorized_keys; ${targetFlavor === "container" ? "/usr/sbin/sshd" : "systemctl start ssh"}`, + ]); + // The fixture models a bare host: PID 1 is real systemd in a private + // cgroup namespace. Remove only this fixture's Docker marker so the normal + // host resolver selects LocalExecutor, exactly as on the reported install. + await docker(["exec", source, "rm", "-f", "/.dockerenv"]); + }, 900_000); + + afterAll(async () => { + for (const name of [source, target]) await docker(["rm", "-f", name]).catch(() => {}); + await docker(["network", "rm", network]).catch(() => {}); + if (targetFlavor === "container") + await docker(["volume", "rm", `${prefix}-docker`]).catch(() => {}); + if (keys) await rm(keys, { recursive: true, force: true }); + }); + + it("restores attachments, folders, mail databases, DKIM and target registration through the shared backup workers", async () => { + let result; + try { + result = await docker([ + "exec", + "-w", + "/workspace", + "-e", + "NODE_ENV=test", + "-e", + "DEPLOY_MODE=bare", + "-e", + "CLOUD_MODE=false", + "-e", + "INTERNAL_TOKEN=mail-fixture-internal-token-00000000000000000000", + "-e", + "BETTER_AUTH_SECRET=mail-fixture-controller-secret-00000000000000000000", + "-e", + "BACKUP_ALLOW_LOCAL_DESTINATION=true", + "-e", + "BACKUP_LOCAL_ROOT=/var/lib/openship/backups", + "-e", + "OPENSHIP_MIGRATIONS_DIR=/workspace/drizzle", + "-e", + `MAIL_TEST_TARGET_FLAVOR=${targetFlavor}`, + source, + "bun", + "roundtrip.js", + ]); + } catch (error) { + const failure = error as Error & { stdout?: string; stderr?: string }; + const inspect = targetFlavor === "container" + ? "docker exec openship-mail sh -c 'supervisorctl status; cat /tmp/supervisord.log /tmp/postfix*.log; postfix status; postfix check; ps aux'" + : "systemctl status postfix dovecot amavis; journalctl --no-pager -n 60 -u postfix -u dovecot -u amavis"; + const diagnostics = await docker(["exec", target, "sh", "-c", inspect]).catch( + (diagnosticError: Error & { stdout?: string; stderr?: string }) => diagnosticError, + ); + throw new Error([ + failure.message, + failure.stdout, + failure.stderr, + diagnostics.stdout, + diagnostics.stderr, + ].filter(Boolean).join("\n"), { cause: error }); + } + expect(result.stdout, result.stderr).toContain("MAIL_BACKUP_ROUNDTRIP_OK"); + }, 300_000); + }); +} diff --git a/apps/api/test/e2e/platform-import-data.e2e.test.ts b/apps/api/test/e2e/platform-import-data.e2e.test.ts index ef24f936c..ac15215ea 100644 --- a/apps/api/test/e2e/platform-import-data.e2e.test.ts +++ b/apps/api/test/e2e/platform-import-data.e2e.test.ts @@ -23,14 +23,15 @@ import { describeDockerE2E, requireDocker, dockerSocketPath } from "../helpers/d const h = vi.hoisted(() => ({ project: {} as any, source: undefined as unknown, + service: undefined as any, runtime: undefined as unknown, workspaceId: undefined as string | undefined, exportOnly: vi.fn(), })); vi.mock("@repo/db", () => ({ repos: { - project: { findById: async () => h.project }, - service: { listByProject: async () => [] }, + project: { findById: async () => h.project, getEnvMap: async () => ({}) }, + service: { listByProject: async () => h.service ? [h.service] : [] }, }, })); vi.mock("@repo/platform/engine/modules/migration/migration-access", () => ({ @@ -378,10 +379,16 @@ describeDockerE2E("generic stopped-volume provider import", () => { environment: {}, bindings: [], } as PlannedImportService; + h.service = { + id: `${projectId}-service`, projectId, name: fixture.name, + image: fixture.image, environment: {}, + volumes: service.create.volumes, namespaceVolumes: true, + }; const args = { runId: tag, service, projectId, + serviceId: h.service.id, signal: new AbortController().signal, log: () => {}, progress: vi.fn(), diff --git a/apps/api/test/e2e/platform-import-project.e2e.test.ts b/apps/api/test/e2e/platform-import-project.e2e.test.ts new file mode 100644 index 000000000..b8de6a4ca --- /dev/null +++ b/apps/api/test/e2e/platform-import-project.e2e.test.ts @@ -0,0 +1,569 @@ +import { afterAll, beforeAll, expect, it, vi } from "vitest"; +import { randomUUID } from "node:crypto"; +import { spawn } from "node:child_process"; +import { createConnection } from "node:net"; +import { once } from "node:events"; +import { mkdtemp, rm, writeFile } from "node:fs/promises"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { + BuildLogger, + CommandSourceBackupExecutor, + CloudDockerRuntime, + DockerRuntime, + type CommandExecutor, +} from "@repo/adapters"; +import { CloudServerConnection } from "../../../../packages/adapters/src/runtime/cloud/server-connection"; +import { CloudWorkspaceExecutor } from "../../../../packages/adapters/src/runtime/cloud/workspace-executor"; +import { db, repos, schema } from "@repo/db"; +import type { ExecutionContext } from "@repo/platform"; +import type { ProviderInventory } from "@repo/platform/engine/modules/migration/providers/types"; +import { encrypt, decryptEnvMap } from "@repo/platform/engine/lib/encryption"; +import { deployComposeServices } from "@repo/platform/engine/modules/deployments/compose/deploy.service"; +import { projectServicesToDeployableServices } from "@repo/platform/engine/modules/deployments/compose/project-services"; +import { seedDeployment, seedOrg, setActive } from "../helpers/seed"; +import { describeDockerE2E, requireDocker, dockerSocketPath } from "../helpers/docker-e2e"; +import { refreshConnectionEnv } from "@repo/platform/engine/modules/projects/project-connection.service"; +import { + sharedServiceAlias, + sharedServiceNetwork, +} from "@repo/platform/engine/modules/projects/shared-service-network"; + +// Substitute Railway's control/SSH boundary, Git checkout, destination resolution +// and the asynchronous build queue. All runtime operations use an isolated daemon. +// Review, project creation, encrypted environments, migration checkpoints, +// volume transfer and the ordinary Compose deployment engine remain real. +const h = vi.hoisted(() => ({ + discover: vi.fn(), + prepare: vi.fn(), + assertStopped: vi.fn(), + openSource: vi.fn(), + runtime: vi.fn(), + deploy: vi.fn(), +})); +vi.mock("@repo/platform/engine/modules/credentials/connection-authorization", () => ({ + getProviderAccess: async () => ({ provider: "railway", publicFields: {}, secrets: {} }), +})); +vi.mock("@repo/platform/engine/modules/migration/providers/railway", () => ({ + railwayMigrationProvider: { discover: h.discover }, +})); +vi.mock("@repo/platform/engine/modules/migration/providers/railway-project-transfer", () => ({ + prepareRailwayProjectTransfer: h.prepare, + assertRailwayExportOnly: h.assertStopped, + restoreRailwayProjectTransfer: async () => {}, +})); +vi.mock("@repo/platform/engine/modules/migration/providers/railway-data", () => ({ + openRailwayDataSource: h.openSource, +})); +vi.mock("@repo/platform/engine/modules/migration/migration-runtime", async (original) => ({ + ...(await original()), + createMigrationDockerRuntime: h.runtime, +})); +vi.mock("@repo/platform/engine/modules/services/service-container", async (original) => ({ + ...(await original()), + // Resolve private-link operations onto the same isolated daemon as the import. + // Connection authorization, persistence, DNS and network attachment stay real. + resolveServicePlatform: async () => ({ platform: { runtime: await h.runtime() } }), +})); +vi.mock("@repo/platform/engine/modules/deployments/build.service", () => ({ + triggerDeployment: h.deploy, +})); +vi.mock("@repo/platform/engine/modules/deployments/project-reader", () => ({ + createGitHubReader: () => ({ readText: async () => "FROM redis:7-alpine\n" }), +})); +import { + discoverPlatform, + reviewPlatform, + loadReviewedImport, +} from "@repo/platform/engine/modules/migration/platform-import-plan"; +import { executePlatformImport } from "@repo/platform/engine/modules/migration/platform-import-worker"; + +describeDockerE2E("Railway projects retain native service boundaries", () => { + const tag = `import-groups-${randomUUID().slice(0, 8)}`; + const containers = new Set(); + const volumes = new Set(); + const images = new Set(); + const sharedNetworks = new Set(); + const projects: Array<{ id: string; slug: string }> = []; + const sources = new Map(); + let runtime: DockerRuntime; + const exec = async (id: string, command: string) => + (await (await runtime.inContainerExecutor(id)).exec(command)).trim(); + + beforeAll(async () => { + await requireDocker(); + runtime = await DockerRuntime.create({ transport: "socket" }); + await runtime.pullImage("redis:7-alpine"); + await runtime.pullImage("alpine:3"); + const host = await runtime.docker.createContainer({ + Image: "alpine:3", + Cmd: ["sleep", "infinity"], + Labels: { "openship.test": tag }, + HostConfig: { NetworkMode: "host" }, + }); + containers.add(host.id); + await host.start(); + vi.spyOn(CloudServerConnection.prototype, "connectDocker").mockImplementation(async () => { + const socket = createConnection(dockerSocketPath); + await once(socket, "connect"); + return socket; + }); + vi.spyOn(CloudServerConnection.prototype, "ensureDocker").mockResolvedValue(); + vi.spyOn(CloudServerConnection.prototype, "publishedPorts").mockResolvedValue([]); + vi.spyOn(CloudWorkspaceExecutor.prototype, "exec").mockImplementation((command) => + exec(host.id, command), + ); + }); + afterAll(async () => { + if (!runtime) return; + for (const project of projects) { + for (const id of await runtime.listProjectContainerIds(project.id)) containers.add(id); + } + for (const id of containers) { + const container = runtime.docker.getContainer(id); + for (const mount of (await container.inspect()).Mounts) { + if (mount.Type === "volume" && mount.Name) volumes.add(mount.Name); + } + await container.remove({ force: true }); + } + for (const project of projects) await runtime.removeNetwork(project.slug); + for (const name of sharedNetworks) await runtime.docker.getNetwork(name).remove(); + for (const volume of (await runtime.docker.listVolumes()).Volumes ?? []) { + if (projects.some((project) => volume.Labels?.["openship.project"] === project.id)) + volumes.add(volume.Name); + } + for (const name of volumes) await runtime.docker.getVolume(name).remove(); + for (const image of images) await runtime.removeImage(image); + await runtime.dispose(); + vi.restoreAllMocks(); + }); + + async function start(image: string, volume: string, command: string[]) { + const container = await runtime.docker.createContainer({ + Image: image, + Cmd: command, + Labels: { "openship.test": tag }, + HostConfig: { Binds: [`${volume}:/data`] }, + }); + containers.add(container.id); + await container.start(); + return container.id; + } + + it.each([ + { transport: "socket", split: false }, + { transport: "cloud", split: false }, + { transport: "socket", split: true }, + { transport: "cloud", split: true }, + ] as const)( + "imports through $transport (multiple repositories: $split), isolating DNS, env and persisted data", + async ({ transport, split }) => { + const owner = await seedOrg({ ownsHost: true }); + const ctx = { + ...owner, + role: "owner", + user: { id: owner.userId, name: "Test", email: "test@example.test" }, + sessionKind: "cookie", + } as ExecutionContext; + const serverId = `${tag}-${transport}-${split}`; + await db.insert(schema.servers).values({ + id: serverId, + organizationId: owner.organizationId, + isLocal: true, + sshHost: "localhost", + sshUser: "root", + sshAuthMethod: "key", + }); + const openRuntime = () => + DockerRuntime.create( + transport === "socket" + ? { transport: "socket" } + : { + transport: "cloud", + // The real managed Docker transport, with only its provider tunnel + // replaced. Host execution must never be used for copying data. + cloudConnection: async () => { + const socket = createConnection(dockerSocketPath); + await once(socket, "connect"); + return socket; + }, + executor: { + exec: async () => { + throw new Error("Unexpected API-host execution"); + }, + dispose: async () => {}, + } as unknown as CommandExecutor, + }, + ); + h.runtime.mockImplementation(openRuntime); + h.prepare.mockImplementation(async (_run, plan) => { + for (const service of plan.services) { + const source = sources.get(service.source.id); + if (source) await runtime.docker.getContainer(source.container).stop({ t: 10 }); + } + }); + h.assertStopped.mockImplementation(async () => { + for (const source of sources.values()) { + expect( + (await runtime.docker.getContainer(source.container).inspect()).State.Running, + ).toBe(false); + } + }); + h.openSource.mockImplementation(async (_run, service) => { + const source = sources.get(service.id)!; + const channel = { + rawExec: async (command: string) => { + const child = spawn("docker", ["exec", source.exporter, "sh", "-c", command]); + return { + stdout: child.stdout, + stderr: child.stderr, + kill: () => { + child.kill("SIGKILL"); + }, + onClose: new Promise((resolve, reject) => { + child.once("error", reject); + child.once("close", (code) => resolve(code ?? 1)); + }), + }; + }, + } as CommandExecutor; + return { + executor: new CommandSourceBackupExecutor(channel, ["/data"]), + dispose: async () => {}, + }; + }); + h.deploy.mockImplementation(async (_ctx, input) => { + // This isolated daemon uses its private project networks; no edge or + // published host ports are needed for this container-IP routing fixture. + await repos.project.update(input.projectId, { routeStrategy: "container-ip" }); + const project = (await repos.project.findById(input.projectId))!; + const services = await repos.service.listByProject(project.id); + await refreshConnectionEnv(ctx, project.id); + const preparedLocalImages = new Map(); + const builtImages = new Map(); + for (const service of services) { + if (service.image) preparedLocalImages.set(service.id, service.image); + else { + // Stand in for the immutable Git checkout with a distinct build per + // repository/revision. The image build and deployment stay real. + const directory = await mkdtemp(join(tmpdir(), "openship-import-build-")); + const image = `${tag}:${service.id}`; + images.add(image); + try { + await writeFile( + join(directory, "Dockerfile"), + `FROM redis:7-alpine\nRUN echo '${project.gitRepo}:${input.commitSha}' > /source-revision\n`, + ); + const stream = await runtime.docker.buildImage( + { context: directory, src: ["Dockerfile"] }, + { t: image, networkmode: "none", labels: { "openship.project": project.id } }, + ); + await new Promise((resolve, reject) => + runtime.docker.modem.followProgress(stream, (error, output) => { + const failure = output?.find((event: { error?: string }) => event.error)?.error; + if (error || failure) reject(error ?? new Error(failure)); + else resolve(); + }), + ); + preparedLocalImages.set(service.id, image); + builtImages.set(service.id, image); + } finally { + await rm(directory, { recursive: true, force: true }); + } + } + } + const deployment = await seedDeployment(project, { + status: "deploying", + trigger: "manual", + imageRef: "compose", + containerId: "compose", + meta: { + runtimeMode: "docker", + deployTarget: "local", + composeServices: projectServicesToDeployableServices(services), + }, + }); + const target = + transport === "socket" + ? await openRuntime() + : await CloudDockerRuntime.forWorkspace( + { + vms: { + get: async () => ({ + id: serverId, + namespace: owner.organizationId, + status: "running", + }), + }, + } as unknown as Parameters[0], + { + workspaceId: serverId, + ownerWorkspaceId: serverId, + projectId: project.id, + namespace: owner.organizationId, + provisionLock: { run: (work) => work() }, + resolveRegistryAuth: async () => undefined, + }, + ); + try { + const result = await deployComposeServices( + project, + deployment, + target, + new BuildLogger(() => {}), + { + executor: null, + localHost: false, + preparedLocalImages, + builtImages, + targetServiceIds: new Set(input.serviceIds), + }, + ); + expect(result.status, JSON.stringify(result)).toBe("ready"); + await repos.deployment.updateStatus(deployment.id, "ready"); + await setActive(project.id, deployment.id); + await result.cutover?.commit(); + } finally { + await target.dispose(); + } + return { deployment }; + }); + + const imported: Array<{ id: string; marker: string }> = []; + for (const projectNumber of [1, 2]) { + const marker = `${tag}-${transport}-${split}-${projectNumber}`; + const services: ProviderInventory["services"] = []; + for (const name of ["db", "cache"]) { + const id = `${marker}-${name}`; + const volume = `${id}-source`; + await runtime.docker.createVolume({ Name: volume }); + volumes.add(volume); + const container = await start("redis:7-alpine", volume, [ + "redis-server", + "--appendonly", + "yes", + ]); + await expect + .poll(() => exec(container, "redis-cli PING"), { timeout: 10_000 }) + .toBe("PONG"); + await exec(container, `redis-cli SET marker '${marker}-${name}'`); + const exporter = await start("alpine:3", volume, ["sleep", "infinity"]); + sources.set(id, { container, exporter }); + services.push({ + id, + name, + kind: "database", + database: "redis_rdb", + image: "redis:7-alpine", + instanceId: container, + privateDomain: `${name}.railway.internal`, + port: 6379, + env: { SERVICE_SECRET: `${marker}-${name}` }, + rawEnv: {}, + variables: [], + volumes: [{ id: volume, mountPath: "/data" }], + domains: [], + warnings: [], + blockers: [], + startCommand: "redis-server --appendonly yes", + }); + } + services.push({ + id: `${marker}-worker`, + name: "worker", + kind: "worker", + image: "redis:7-alpine", + instanceId: "source-worker", + port: 8080, + env: { + SERVICE_SECRET: `${marker}-worker`, + PRIMARY_HOST: "db.railway.internal", + CACHE_HOST: "cache.railway.internal", + BOTH_HOSTS: "db.railway.internal,cache.railway.internal", + }, + rawEnv: {}, + variables: [], + volumes: [], + domains: [ + { hostname: `${marker}.example.com`, portable: true }, + { hostname: `www.${marker}.example.com`, portable: true }, + { hostname: `${marker}.up.railway.app`, portable: false }, + ], + warnings: [], + blockers: [], + startCommand: + "sh -c 'while true; do printf \"HTTP/1.1 200 OK\\r\\nContent-Length: 6\\r\\nConnection: close\\r\\n\\r\\nready\\n\" | nc -l -p 8080; done'", + }); + if (split) { + // Two distinct Git sources plus one image-backed database. All volumes + // must move before either destination Git service is allowed to start. + for (const name of ["cache", "worker"]) { + const source = services.find((service) => service.name === name)!; + source.image = undefined; + source.repository = `fixture/${name}`; + source.commit = (name === "cache" ? "a" : "b").repeat(40); + source.branch = "main"; + source.dockerfile = "Dockerfile"; + } + delete services.find((service) => service.name === "worker")!.env.BOTH_HOSTS; + } + const inventory: ProviderInventory = { + provider: "railway", + source: { connectionId: "fixture", projectId: marker, environmentId: "production" }, + projectName: marker, + environmentName: "Production", + services, + warnings: [], + }; + h.discover.mockImplementation(async () => structuredClone(inventory)); + const discovery = await discoverPlatform(ctx, inventory.source); + const review = await reviewPlatform(ctx, { + draftId: discovery.draftId, + targetServerId: serverId, + services: discovery.services.map((s) => ({ id: s.id, name: s.name, data: "copy" })), + }); + expect(review.ready, JSON.stringify(review)).toBe(true); + const plan = await loadReviewedImport(ctx, review.planId); + const runId = `${marker}-run`; + await repos.dockerMigrationRun.create({ + id: runId, + organizationId: owner.organizationId, + targetServerId: serverId, + projectName: marker, + mode: "provider_import", + status: "adopting", + inputSnapshot: { provider: "railway", source: inventory.source }, + recovery: { + providerImport: { + planId: plan.id, + planEnc: encrypt(JSON.stringify(plan)), + connectionId: "fixture", + projects: {}, + completed: [], + cleanupPending: true, + }, + }, + }); + try { + await executePlatformImport(ctx, runId, plan, { + signal: new AbortController().signal, + log: () => {}, + progress: () => {}, + phase: async () => {}, + wait: async (id) => (await repos.deployment.findById(id))?.status === "ready", + }); + } finally { + const saved = await repos.project.listByOrganization(owner.organizationId); + for (const p of saved.rows) { + if (!projects.some((known) => known.id === p.id)) projects.push(p); + for (const link of await repos.projectConnection.listByTarget(p.id)) + if (link.sourceServiceId) + sharedNetworks.add(sharedServiceNetwork(link.sourceServiceId)); + } + } + const checkpoint = (await repos.dockerMigrationRun.findById(runId))!.recovery + .providerImport!; + const ids = [...new Set(Object.values(checkpoint.projects))]; + expect(ids).toHaveLength(split ? 3 : 1); + expect(checkpoint.completed).toHaveLength(3); + expect((await repos.project.listByOrganization(owner.organizationId)).total).toBe( + projectNumber * (split ? 3 : 1), + ); + for (const id of ids) { + expect(await repos.service.listByProject(id)).toHaveLength(split ? 1 : 3); + if (!split) expect(await repos.project.getEnvMap(id, "production", null)).toEqual({}); + imported.push({ id, marker }); + } + } + + // Check both projects AFTER the second import: duplicate service names + // must not redirect the first project's DNS or overwrite its storage. + for (const { id, marker } of imported) { + const project = (await repos.project.findById(id))!; + const rows = await repos.service.listByProject(id); + const deployed = await repos.service.listByDeployment(project.activeDeploymentId!); + for (const service of rows) { + const env = decryptEnvMap(await repos.project.getEnvMap(id, "production", service.id)); + expect(env.SERVICE_SECRET).toBe(`${marker}-${service.name}`); + const container = deployed.find((r) => r.serviceId === service.id)!.containerId!; + expect(await exec(container, 'printf %s "$SERVICE_SECRET"')).toBe(env.SERVICE_SECRET); + if (split && project.gitRepo) { + const revision = (service.name === "cache" ? "a" : "b").repeat(40); + expect(await exec(container, "cat /source-revision")).toBe( + `${project.gitRepo}:${revision}`, + ); + } + if (service.name === "worker") { + expect(service.exposed).toBe(true); + expect(service.publicEndpoints).toEqual([ + expect.objectContaining({ port: 8080, customDomain: `${marker}.example.com` }), + ]); + const domains = await repos.domain.listByProject(id); + expect(domains.map((domain) => domain.hostname).sort()).toEqual( + [`${marker}.example.com`, `www.${marker}.example.com`].sort(), + ); + expect( + domains.every( + (domain) => + domain.serviceId === service.id && domain.targetPort === 8080 && !domain.verified, + ), + ).toBe(true); + expect(await exec(container, "wget -qO- http://127.0.0.1:8080")).toBe("ready"); + if (!split) expect(await exec(container, 'printf %s "$BOTH_HOSTS"')).toBe("db,cache"); + else { + const links = await repos.projectConnection.listByTarget(id); + expect(links).toHaveLength(2); + expect( + links.every( + (link) => + link.organizationId === owner.organizationId && + link.mode === "internal" && + link.usesPrivateNetwork, + ), + ).toBe(true); + for (const link of links) { + expect(env[link.envKey]).toBeUndefined(); // Service env must not shadow its native link. + expect(await exec(container, `printenv ${link.envKey}`)).toBe( + sharedServiceAlias(link.sourceServiceId!), + ); + } + } + expect(await exec(container, 'redis-cli -h "$PRIMARY_HOST" GET marker')).toBe( + `${marker}-db`, + ); + expect(await exec(container, 'redis-cli -h "$CACHE_HOST" GET marker')).toBe( + `${marker}-cache`, + ); + if (split) { + // An ordinary later deployment refreshes the same saved links and + // rejoins the private networks without the import worker involved. + await h.deploy(ctx, { + projectId: id, + serviceIds: [service.id], + commitSha: "b".repeat(40), + }); + const current = (await repos.project.findById(id))!; + const next = (await repos.service.listByDeployment(current.activeDeploymentId!)).find( + (row) => row.serviceId === service.id, + )!; + expect(await exec(next.containerId!, 'redis-cli -h "$PRIMARY_HOST" GET marker')).toBe( + `${marker}-db`, + ); + expect(await exec(next.containerId!, 'redis-cli -h "$CACHE_HOST" GET marker')).toBe( + `${marker}-cache`, + ); + } + } else { + expect(await exec(container, "redis-cli GET marker")).toBe(`${marker}-${service.name}`); + await exec(container, "redis-cli SET new-write survives"); + await runtime.docker.getContainer(container).restart({ t: 10 }); + await expect + .poll(() => exec(container, "redis-cli GET marker"), { timeout: 10_000 }) + .toBe(`${marker}-${service.name}`); + expect(await exec(container, "redis-cli GET new-write")).toBe("survives"); + } + } + } + }, + ); +}); diff --git a/apps/api/test/e2e/route-protection.e2e.test.ts b/apps/api/test/e2e/route-protection.e2e.test.ts index c00322877..819e043a6 100644 --- a/apps/api/test/e2e/route-protection.e2e.test.ts +++ b/apps/api/test/e2e/route-protection.e2e.test.ts @@ -6,6 +6,7 @@ import { PassThrough } from "node:stream"; import { readFile, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; +import { normalizeEdgeResponse } from "@repo/core"; import { NginxProvider, OPENRESTY_DEFAULT_PATHS, @@ -203,6 +204,8 @@ describeDockerE2E("password-protected routes through the real edge", () => { `${paths.sitesDir}/_test_upstream.conf`, `server { listen 127.0.0.1:39482; + # Test-only bridge to the loopback management listener. + location = /api/test-rules { proxy_pass http://127.0.0.1:9145/rules; } location / { add_header Cache-Control "public, max-age=3600"; content_by_lua_block { ngx.print(ngx.var.http_cookie or "none") } @@ -352,6 +355,21 @@ describeDockerE2E("password-protected routes through the real edge", () => { } }); + it("protects literal edge responses and prevents them from making private content cacheable", async () => { + const payload = JSON.stringify({ host: "private.test", rules: [{ pathPrefix: "/virtual", spec: { response: normalizeEdgeResponse({ body: "private response", headers: { "Cache-Control": "public, max-age=3600" } }) } }] }); + try { + const applied = await ask("/api/test-rules", { cookie, method: "POST", body: payload, headers: { "Content-Type": "application/json" } }); + expect(applied.status).toBe(200); + expect(JSON.parse(applied.body)).toMatchObject({ ok: true, staticResponses: true }); + expect((await ask("/virtual")).status).toBe(401); + const response = await ask("/virtual", { cookie }); + expect(response).toMatchObject({ status: 200, body: "private response" }); + expect(response.headers["cache-control"]).toBe("private, no-store"); + } finally { + expect((await ask("/api/test-rules?host=private.test", { cookie, method: "DELETE" })).status).toBe(200); + } + }); + it("rejects cross-origin sign-in and unsafe return locations", async () => { const csrf = (await ask("/")).body.match(/name="csrf" value="([^"]+)"/)![1]; for (const origin of ["https://attacker.test", "null"]) { diff --git a/apps/api/test/e2e/routing-rules.e2e.test.ts b/apps/api/test/e2e/routing-rules.e2e.test.ts index d7d7585b8..5bb132807 100644 --- a/apps/api/test/e2e/routing-rules.e2e.test.ts +++ b/apps/api/test/e2e/routing-rules.e2e.test.ts @@ -69,13 +69,14 @@ import { type RootChecked, type RouteConfig, } from "@repo/adapters"; -import type { RoutingConfig } from "@repo/core"; +import { normalizeEdgeResponse, type RoutingConfig } from "@repo/core"; import { compileProjectRoutingFields } from "@repo/platform/engine/lib/project-routing-fields"; import { buildDomainFanoutRegistrations } from "@repo/platform/engine/modules/deployments/compose/composite-route"; import type Dockerode from "dockerode"; import { describeDockerE2E, requireDocker } from "../helpers/docker-e2e"; import { freePort } from "../helpers/free-port"; import { EMBEDDED_LUA } from "../../../../packages/adapters/src/infra/lua-embedded"; +import { EDGE_CLIENT_MAX_BODY_SIZE } from "../../../../packages/adapters/src/infra/openresty-lua"; import type { HostRuleEntry } from "@repo/platform/engine/modules/route-rules/route-rule.service"; const REPO_ROOT = join(dirname(fileURLToPath(import.meta.url)), "../../../.."); @@ -268,16 +269,94 @@ events { worker_connections 128; } http { include ${OPENRESTY_DEFAULT_PATHS.confDir}/mime.types; default_type application/octet-stream; + client_max_body_size ${EDGE_CLIENT_MAX_BODY_SIZE}; access_log off; lua_package_path "/usr/local/openresty/site/lualib/?.lua;;"; ${EDGE_SHARED_DICTS.filter(({ name }) => name === "rules" || name === "rl_counters") .map(({ name, size }) => `lua_shared_dict ${name} ${size};`) .join("\n ")} + # Deterministic hot-path instrumentation, isolated to this fixture worker. + init_worker_by_lua_block { + local counts = { decodes=0, pathDecodes=0, bodyReads=0, bodyBytes=0, hashes=0 } + package.loaded.response_test_costs = counts + local json = require "cjson.safe" + local decode = json.decode + json.decode = function(...) + counts.decodes = counts.decodes + 1 + return decode(...) + end + local unescape = ngx.unescape_uri + ngx.unescape_uri = function(...) + counts.pathDecodes = counts.pathDecodes + 1 + return unescape(...) + end + local dict = ngx.shared.rules + local get = dict.get + dict.get = function(self, key, ...) + local value = get(self, key, ...) + if string.sub(key, 1, 10) == "responses:" then + counts.bodyReads = counts.bodyReads + 1 + counts.bodyBytes = counts.bodyBytes + (value and #value or 0) + end + return value + end + local lib = require "openship.rules_lib" + local digest = lib.response_revision + lib.response_revision = function(...) + counts.hashes = counts.hashes + 1 + return digest(...) + end + } + # Test-only management vhost, on the same loopback-published test port. server { listen 80; server_name management.test; + location = /response-legacy-worker { + content_by_lua_block { + local lib = require "openship.rules_lib" + if ngx.req.get_method() == "POST" then + local previous = {} + for _, key in ipairs({ "static_responses", "valid_responses", "response_revision", "response_for" }) do + previous[key], lib[key] = lib[key], nil + end + package.loaded.response_test_previous = previous + else + for key, value in pairs(package.loaded.response_test_previous or {}) do lib[key] = value end + end + ngx.say("ok") + } + } + location = /response-costs { + content_by_lua_block { + local counts = package.loaded.response_test_costs + if ngx.req.get_method() == "POST" then + for key in pairs(counts) do counts[key] = 0 end + end + ngx.say(require("cjson.safe").encode(counts)) + } + } + location = /response-memory { + content_by_lua_block { + local dict = ngx.shared.rules + if ngx.req.get_method() == "POST" then + local i = 0 + while dict:safe_set("test-fill:" .. i, string.rep("x", 65536)) do i = i + 1 end + ngx.say(i) + else + for _, key in ipairs(dict:get_keys(0)) do + if string.sub(key, 1, 10) == "test-fill:" then dict:delete(key) end + end + ngx.say("cleared") + end + } + } + location = /policy-cache-check { + content_by_lua_block { + ngx.say(ngx.shared.rules:get("rules.test") or "[]") + } + } location = /country-cache-check { content_by_lua_block { ngx.say(assert(loadfile("/tmp/visitor-country.lua"))()) @@ -934,8 +1013,9 @@ http { pathPrefix?: string | null; } >, + targetPort = port, ): Promise { - const response = await ask(port, "/rules", "management.test", { + const response = await ask(targetPort, "/rules", "management.test", { method: "POST", body: JSON.stringify({ host, rules }), }); @@ -943,6 +1023,370 @@ http { expect(JSON.parse(response.body)).toMatchObject({ ok: true, host, count: rules.length }); } + it("serves exact literal responses with headers, query strings, Unicode and HEAD", async () => { + const body = '{"message":"مرحبا","literal":"$host; return 500;"}'; + await pushRules("rules.test", [ + { + pathPrefix: "/virtual", + spec: { + response: normalizeEdgeResponse({ + status: 201, + contentType: "application/json", + body, + headers: { "X-Example": "literal-$host" }, + }), + }, + }, + ]); + const result = await ask(port, "/virtual?source=test", "rules.test"); + expect(result).toMatchObject({ status: 201, body }); + expect(result.headers["content-type"]).toBe("application/json"); + expect(result.headers["x-example"]).toBe("literal-$host"); + expect(result.headers["content-length"]).toBe(String(Buffer.byteLength(body))); + const head = await ask(port, "/virtual", "rules.test", { method: "HEAD" }); + expect(head).toMatchObject({ status: 201, body: "" }); + expect(head.headers["content-length"]).toBe(String(Buffer.byteLength(body))); + for (const path of ["/virtual/child", "/virtual-other"]) { + expect((await ask(port, path, "rules.test")).body).not.toBe(body); + } + expect((await ask(port, "/virtual", "other-rules.test")).body).not.toBe(body); + expect((await ask(port, "/virtual", "rules.test", { method: "POST" })).status).not.toBe(201); + }); + + it("keeps responses out of the legacy policy cache during edge upgrades", async () => { + const capability = await ask(port, "/rules/capabilities", "management.test", { + method: "POST", + body: "{}", + }); + expect(JSON.parse(capability.body)).toMatchObject({ ok: true, staticResponses: true }); + const policy = { pathPrefix: null, spec: { access: { methods: ["GET"] } } }; + await pushRules("rules.test", [ + policy, + { pathPrefix: "/virtual", spec: { response: normalizeEdgeResponse({ body: "ready" }) } }, + ]); + const legacy = await ask(port, "/policy-cache-check", "management.test"); + expect(JSON.parse(legacy.body)).toEqual([policy]); + const stored = await ask(port, "/rules?host=rules.test", "management.test"); + expect(JSON.parse(stored.body).rules).toHaveLength(2); + await ask(port, "/rules?host=rules.test", "management.test", { method: "DELETE" }); + expect((await ask(port, "/virtual", "rules.test")).body).not.toBe("ready"); + }); + + it("still publishes policies while a worker has the previous rules library loaded", async () => { + // Earlier route-update tests gracefully reload the shared edge. Their old + // and new workers can briefly overlap, so a worker-local library mutation + // must use a fresh edge whose single worker is stable across these requests. + const legacyPort = await bootAll(); + await pushRules("rules.test", [ + { pathPrefix: null, spec: { access: { methods: ["GET"] } } }, + { + pathPrefix: "/virtual", + spec: { response: normalizeEdgeResponse({ body: "new worker response" }) }, + }, + ], legacyPort); + await ask(legacyPort, "/response-legacy-worker", "management.test", { method: "POST" }); + try { + // A new worker may already have published a response into shared memory. + // This worker must still enforce its policies and serve ordinary traffic. + const ordinary = await ask(legacyPort, "/virtual", "rules.test"); + expect(ordinary.status).toBe(200); + expect(ordinary.body).not.toBe("new worker response"); + expect((await ask(legacyPort, "/virtual", "rules.test", { method: "POST" })).status).toBe(403); + await pushRules("rules.test", [{ pathPrefix: null, spec: { access: { methods: ["GET"] } } }], legacyPort); + const capability = await ask(legacyPort, "/rules/capabilities", "management.test"); + expect(JSON.parse(capability.body).staticResponses).toBe(false); + const response = await ask(legacyPort, "/rules", "management.test", { + method: "POST", + body: JSON.stringify({ + host: "rules.test", + rules: [ + { + pathPrefix: "/virtual", + spec: { response: normalizeEdgeResponse({ body: "not yet" }) }, + }, + ], + }), + }); + expect(response.status).toBe(409); + expect((await ask(legacyPort, "/virtual", "rules.test", { method: "POST" })).status).toBe(403); + } finally { + await ask(legacyPort, "/response-legacy-worker", "management.test", { method: "DELETE" }); + } + }); + + it("retains domain access, bans and rate limits in front of a static response", async () => { + const response = { + pathPrefix: "/virtual", + spec: { response: normalizeEdgeResponse({ body: "answer", methods: [] }) }, + }; + for (const policy of [ + { access: { methods: ["POST"] } }, + { ban: { userAgents: ["blocked-client"] } }, + { hotlink: { allowReferers: ["allowed.test"], allowEmpty: false } }, + ]) { + await pushRules("rules.test", [response, { pathPrefix: null, spec: policy }]); + expect( + (await ask(port, "/virtual", "rules.test", { headers: { "User-Agent": "blocked-client" } })) + .status, + ).toBe(403); + } + await pushRules("rules.test", [ + response, + { pathPrefix: "/virtual", spec: { rateLimit: { rps: 1, burst: 0 } } }, + ]); + const statuses = await Promise.all( + Array.from({ length: 8 }, () => ask(port, "/virtual", "rules.test").then((r) => r.status)), + ); + expect(statuses).toContain(429); + }); + + it("matches prefixes at a path boundary and gives exact responses precedence", async () => { + const rules = [ + { + pathPrefix: "/virtual", + spec: { response: normalizeEdgeResponse({ match: "prefix", body: "prefix" }) }, + }, + { pathPrefix: "/virtual", spec: { response: normalizeEdgeResponse({ body: "exact" }) } }, + ]; + for (const ordered of [rules, [...rules].reverse()]) { + await pushRules("rules.test", ordered); + expect((await ask(port, "/virtual", "rules.test")).body).toBe("exact"); + expect((await ask(port, "/virtual/child", "rules.test")).body).toBe("prefix"); + expect((await ask(port, "/virtual-other", "rules.test")).body).not.toBe("prefix"); + } + await pushRules("rules.test", [ + { + pathPrefix: "/index.html", + spec: { response: normalizeEdgeResponse({ body: "only-index" }) }, + }, + ]); + expect((await ask(port, "/unmatched-spa-path", "rules.test")).body).not.toBe("only-index"); + }); + + it("keeps custom error content and empty responses exact, including bodyless statuses", async () => { + for (const [status, body] of [ + [404, "Custom missing page"], + [503, ""], + [204, ""], + [205, ""], + [304, ""], + ] as const) { + await pushRules("rules.test", [ + { pathPrefix: "/virtual", spec: { response: normalizeEdgeResponse({ status, body }) } }, + ]); + const answer = await ask(port, "/virtual", "rules.test"); + expect(answer).toMatchObject({ status, body }); + if (status === 204 || status === 304) + expect(answer.headers["content-length"]).toBeUndefined(); + } + }); + + it("applies large bodies, updates and removal without a reload", async () => { + const body = "ع".repeat(32000); + await pushRules("rules.test", [ + { pathPrefix: "/virtual", spec: { response: normalizeEdgeResponse({ body }) } }, + ]); + expect((await ask(port, "/virtual", "rules.test")).body).toBe(body); + await pushRules("rules.test", [ + { pathPrefix: "/virtual", spec: { response: normalizeEdgeResponse({ body: "updated" }) } }, + ]); + expect((await ask(port, "/virtual", "rules.test")).body).toBe("updated"); + await pushRules("rules.test", []); + expect((await ask(port, "/virtual", "rules.test")).body).not.toBe("updated"); + }); + + it("rejects unsafe or malformed responses before changing active rules", async () => { + const original = normalizeEdgeResponse({ body: "original response" }); + await pushRules("rules.test", [ + { pathPrefix: null, spec: { access: { methods: ["GET"] } } }, + { pathPrefix: "/virtual", spec: { response: original } }, + ]); + const invalid = [ + { headers: { "X-Test": "ok\r\nInjected: yes" } }, + { headers: { "X-Test": "nul\0value" } }, + { headers: { "Bad Header": "value" } }, + { headers: { "X-Test": "one", "x-test": "two" } }, + ...[ + "Content-Length", + "Transfer-Encoding", + "Content-Encoding", + "Connection", + "X-Accel-Redirect", + ].map((name) => ({ headers: { [name]: "unsafe" } })), + { headers: { "X-Test": "a".repeat(2049) } }, + { headers: Object.fromEntries(Array.from({ length: 33 }, (_, i) => [`X-${i}`, "value"])) }, + { + headers: Object.fromEntries( + Array.from({ length: 5 }, (_, i) => [`X-${i}`, "a".repeat(2048)]), + ), + }, + { body: "ع".repeat(32769) }, + { status: 101 }, + { status: 204 }, + { status: 200.5 }, + { contentType: "text/plain\r\nInjected: yes" }, + { contentType: "bad-type" }, + { methods: "GET" }, + { methods: ["TRACE"] }, + { methods: { GET: true } }, + { match: "regex" }, + ]; + for (const override of invalid) { + const rejected = await ask(port, "/rules", "management.test", { + method: "POST", + body: JSON.stringify({ + host: "rules.test", + rules: [{ pathPrefix: "/virtual", spec: { response: { ...original, ...override } } }], + }), + }); + expect(rejected.status, JSON.stringify(override).slice(0, 100)).toBe(400); + const answer = await ask(port, "/virtual", "rules.test"); + expect(answer.body).toBe(original.body); + expect(answer.headers["injected"]).toBeUndefined(); + expect((await ask(port, "/virtual", "rules.test", { method: "POST" })).status).toBe(403); + } + for (const pathPrefix of [ + "/_openship/hooks/probe", + "/.openship/auth", + "/.well-known/acme-challenge/probe", + "/../virtual", + "/bad//path", + "/bad%2Fpath", + "/" + "x".repeat(512), + ]) { + const rejected = await ask(port, "/rules", "management.test", { + method: "POST", + body: JSON.stringify({ + host: "rules.test", + rules: [{ pathPrefix, spec: { response: original } }], + }), + }); + expect(rejected.status, pathPrefix).toBe(400); + } + }); + + it("bounds route counts and preserves access policies when response storage is full", async () => { + const responses = Array.from({ length: 64 }, (_, i) => ({ + pathPrefix: `/bounded/${i}`, + spec: { response: normalizeEdgeResponse({ body: `response-${i}` }) }, + })); + await pushRules("rules.test", responses); + expect((await ask(port, "/bounded/63", "rules.test")).body).toBe("response-63"); + for (const extra of [responses[0], { ...responses[0], pathPrefix: "/overflow" }]) { + const rejected = await ask(port, "/rules", "management.test", { + method: "POST", + body: JSON.stringify({ host: "rules.test", rules: [...responses, extra] }), + }); + expect(rejected.status).toBe(400); + expect((await ask(port, "/bounded/63", "rules.test")).body).toBe("response-63"); + } + await pushRules("rules.test", [{ pathPrefix: null, spec: { access: { methods: ["GET"] } } }]); + try { + expect( + (await ask(port, "/response-memory", "management.test", { method: "POST" })).status, + ).toBe(200); + const failed = await ask(port, "/rules", "management.test", { + method: "POST", + body: JSON.stringify({ + host: "other-rules.test", + rules: responses.map((entry) => ({ + ...entry, + spec: { response: normalizeEdgeResponse({ body: "x".repeat(65536) }) }, + })), + }), + }); + expect(failed.status, failed.body).toBe(503); + expect((await ask(port, "/virtual", "rules.test", { method: "POST" })).status).toBe(403); + } finally { + await ask(port, "/response-memory", "management.test", { method: "DELETE" }); + } + }); + + it("keeps unconfigured traffic cheap and reuses compiled responses without copying bodies", async () => { + const reset = () => ask(port, "/response-costs", "management.test", { method: "POST" }); + const costs = async () => + JSON.parse((await ask(port, "/response-costs", "management.test")).body); + for (const policies of [[], [{ pathPrefix: null, spec: { access: { methods: ["GET"] } } }]]) { + await pushRules("rules.test", policies); + await ask(port, "/", "rules.test"); // warm existing policy/GeoIP caches + await reset(); + for (let i = 0; i < 16; i++) await ask(port, "/", "rules.test"); + expect(await costs()).toEqual({ + decodes: 0, + pathDecodes: 0, + bodyReads: 0, + bodyBytes: 0, + hashes: 0, + }); + } + const entries = (body: string) => [ + { pathPrefix: "/virtual", spec: { response: normalizeEdgeResponse({ body }) } }, + ]; + const body = "x".repeat(65536); + await pushRules("rules.test", entries(body)); + await reset(); + expect((await ask(port, "/virtual", "rules.test")).body).toBe(body); + expect(await costs()).toMatchObject({ decodes: 1, pathDecodes: 1, bodyReads: 1, hashes: 1 }); + await reset(); + for (let i = 0; i < 16; i++) + expect((await ask(port, `/virtual?request=${i}`, "rules.test")).body).toBe(body); + expect(await costs()).toEqual({ + decodes: 0, + pathDecodes: 16, + bodyReads: 0, + bodyBytes: 0, + hashes: 0, + }); + await pushRules("rules.test", entries("refreshed")); + await reset(); + expect((await ask(port, "/virtual", "rules.test")).body).toBe("refreshed"); + expect(await costs()).toMatchObject({ decodes: 1, bodyReads: 1, hashes: 1 }); + await pushRules("rules.test", []); + await reset(); + expect((await ask(port, "/virtual", "rules.test")).body).not.toBe("refreshed"); + expect(await costs()).toEqual({ + decodes: 0, + pathDecodes: 0, + bodyReads: 0, + bodyBytes: 0, + hashes: 0, + }); + }); + + it("supports an OPTIONS response and leaves platform paths alone under a catch-all", async () => { + await pushRules("rules.test", [ + { + pathPrefix: "/virtual", + spec: { + response: normalizeEdgeResponse({ + status: 204, + methods: ["OPTIONS"], + headers: { "Access-Control-Allow-Origin": "https://client.test" }, + }), + }, + }, + ]); + const options = await ask(port, "/virtual", "rules.test", { method: "OPTIONS" }); + expect(options).toMatchObject({ status: 204, body: "" }); + expect(options.headers["access-control-allow-origin"]).toBe("https://client.test"); + await pushRules("rules.test", [ + { + pathPrefix: null, + spec: { response: normalizeEdgeResponse({ match: "prefix", body: "catch-all" }) }, + }, + ]); + expect((await ask(port, "/ordinary", "rules.test")).body).toBe("catch-all"); + for (const path of [ + "/_openship/hooks/probe", + "/.openship/auth", + "/.well-known/acme-challenge/probe", + "/ignored/../.openship/auth", + "//.openship/auth", + ]) { + expect((await ask(port, path, "rules.test")).body).not.toBe("catch-all"); + } + }); + it.each([ ["JSON null", null], ["omitted", undefined], diff --git a/apps/api/test/e2e/scaling-databases.e2e.test.ts b/apps/api/test/e2e/scaling-databases.e2e.test.ts index fe5b603f9..b86e1fcfd 100644 --- a/apps/api/test/e2e/scaling-databases.e2e.test.ts +++ b/apps/api/test/e2e/scaling-databases.e2e.test.ts @@ -15,6 +15,7 @@ import { type KubernetesObject, } from "@repo/adapters"; import type { ClusterRuntimePlan, ClusterDatabaseConfig } from "@repo/core"; +import { SecretValues } from "@repo/core/diagnostics"; import type { ClusterDatabase } from "@repo/contracts"; import { DATABASE_IMAGES, @@ -441,8 +442,32 @@ done afterEach(async (context) => { console.info(`[database-e2e] ${context.task.result?.state}: ${context.task.name}`); - if (context.task.result?.state === "fail") console.error(await lab.diagnostics()); - }); + if (context.task.result?.state !== "fail") return; + console.error(await lab.diagnostics()); + // Pod readiness alone cannot explain an operator stalled during a resize. + // Keep its status and reconciliation errors, with the lab's secrets masked. + try { + const secrets = await lab.api.request<{ items: KubernetesObject[] }>( + "GET", + `/api/v1/secrets?labelSelector=${encodeURIComponent(`openship.io/runtime=${lab.runtimeId}`)}`, + ); + const redactor = new SecretValues( + secrets.items.flatMap((secret) => + Object.values(secret.data ?? {}).map((value) => + Buffer.from(String(value), "base64").toString(), + ), + ), + ); + for (const args of [ + ["get", "redisclusters", "-A", "-o", "yaml"], + ["logs", "-n", "ot-operators", "deployment/redis-operator", "--tail=200"], + ]) { + console.error(redactor.redact(await lab.nodeExec(0, ["kubectl", ...args]))); + } + } catch (error) { + console.error("Database operator diagnostics unavailable:", String(error)); + } + }, 180000); afterAll(async () => { for (const index of paused) await resume(index).catch(() => {}); const { stopNetworkSetups } = diff --git a/apps/api/test/fixtures/oblien-capacity-catalog.json b/apps/api/test/fixtures/oblien-capacity-catalog.json index 1e937f0cf..78cf91b65 100644 --- a/apps/api/test/fixtures/oblien-capacity-catalog.json +++ b/apps/api/test/fixtures/oblien-capacity-catalog.json @@ -56,12 +56,12 @@ "vcpus": 0.5, "memoryMb": 512, "diskGb": 10, - "workspaces": 4 + "vms": 4 }, "monthlyAmount": 500, "paygCapAmount": 625, "resourceLimits": { - "max_workspaces": 4, + "max_vms": 4, "max_vcpus": 1, "max_ram_mb": 512, "max_disk_gb": 10, @@ -76,12 +76,12 @@ "vcpus": 1, "memoryMb": 1024, "diskGb": 20, - "workspaces": 8 + "vms": 8 }, "monthlyAmount": 800, "paygCapAmount": 1000, "resourceLimits": { - "max_workspaces": 8, + "max_vms": 8, "max_vcpus": 1, "max_ram_mb": 1024, "max_disk_gb": 20, @@ -96,12 +96,12 @@ "vcpus": 2, "memoryMb": 4096, "diskGb": 40, - "workspaces": 12 + "vms": 12 }, "monthlyAmount": 1800, "paygCapAmount": 2250, "resourceLimits": { - "max_workspaces": 12, + "max_vms": 12, "max_vcpus": 2, "max_ram_mb": 4096, "max_disk_gb": 40, @@ -116,12 +116,12 @@ "vcpus": 4, "memoryMb": 8192, "diskGb": 80, - "workspaces": 24 + "vms": 24 }, "monthlyAmount": 3600, "paygCapAmount": 4500, "resourceLimits": { - "max_workspaces": 24, + "max_vms": 24, "max_vcpus": 4, "max_ram_mb": 8192, "max_disk_gb": 80, diff --git a/apps/api/test/fixtures/upgrade/compose-0.9.11.yml b/apps/api/test/fixtures/upgrade/compose-0.9.11.yml new file mode 100644 index 000000000..d9da40163 --- /dev/null +++ b/apps/api/test/fixtures/upgrade/compose-0.9.11.yml @@ -0,0 +1,153 @@ +# Frozen CLI template from v0.9.11:apps/cli/src/lib/compose.ts. +# Only test isolation (paths, images and container name) is applied at runtime. +# Managed by `openship up` — do not edit; re-run `openship up` to regenerate. +x-logging: &default-logging + driver: "json-file" + options: + max-size: "20m" + max-file: "3" + +services: + postgres: + image: postgres:16-alpine + restart: unless-stopped + logging: *default-logging + environment: + # Keep the data dir in a subdirectory of the volume so a fresh install never + # runs initdb against a bare mount root (which fails on quirky host + # filesystems with EPERM — #350). OPENSHIP_PGDATA is decided ONCE at install + # by the CLI (fresh → subdir, pre-existing volume → root) and preserved in + # .env, so this never moves an existing database. + PGDATA: ${OPENSHIP_PGDATA:-/var/lib/postgresql/data/pgdata} + POSTGRES_USER: ${POSTGRES_USER:-openship} + POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?missing from .env — re-run openship up to regenerate it} + POSTGRES_DB: ${POSTGRES_DB:-openship} + expose: ["5432"] + volumes: [postgres_data:/var/lib/postgresql/data] + healthcheck: + test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER:-openship} -d ${POSTGRES_DB:-openship}"] + interval: 5s + timeout: 3s + retries: 12 + + redis: + image: redis:7-alpine + restart: unless-stopped + logging: *default-logging + command: ["redis-server", "--appendonly", "yes"] + expose: ["6379"] + volumes: [redis_data:/data] + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 12 + + api: + image: ${OPENSHIP_IMAGE_REGISTRY:-ghcr.io/oblien}/openship-api:${OPENSHIP_VERSION:-latest} + restart: unless-stopped + logging: *default-logging + # Loopback by default — the host-net edge reaches it over loopback, so nothing + # sits on a public interface. OPENSHIP_BIND_ADDR opts into a public/LAN interface + # (set by `openship up` when a public URL is configured for off-box access). + ports: ["${OPENSHIP_BIND_ADDR:-127.0.0.1}:${API_PORT:-4000}:${API_PORT:-4000}"] + volumes: + # The daemon socket, at the path the api's own default expects INSIDE the + # container. The HOST side is a variable because it MOVES: a rootless daemon + # keeps its socket under the invoking user's runtime dir, and Docker creates a + # missing bind source as an empty DIRECTORY instead of failing — so the rootful + # path there brings the stack up and then fails every container operation in + # the transport, naming neither the path nor the cause (#482). `openship up` + # writes OPENSHIP_DOCKER_SOCKET when the detected path isn't this default. + - ${OPENSHIP_DOCKER_SOCKET:-/var/run/docker.sock}:/var/run/docker.sock + # Registry credentials for private-image pulls. The API talks to the daemon over + # the socket above via dockerode, which — unlike the docker CLI — reads no + # config.json of its own, so a private pull went out anonymous on a logged-in + # host (#581). Mounted READ-ONLY and only into this service; absent entirely when + # the host has no config, so nothing is invented. Substituted at write time (the + # path depends on DOCKER_CONFIG and on the file existing), hence the marker. + # Routing state shared with the edge, as HOST BIND MOUNTS (generated from + # EDGE_CONTAINER_MOUNTS): the vhost tree, /etc/letsencrypt, the ACME webroot + # and the static doc-roots the API writes and the edge serves. Named volumes + # hid all of it from every host-side reader (migrate's proxy scan, cert carry, + # cert reuse, mail cert symlinks), each of which then silently saw "nothing". + - /private/var/lib/openship/edge/sites-enabled:/usr/local/openresty/nginx/conf/sites-enabled:z + - /private/etc/letsencrypt:/etc/letsencrypt:z + - /private/var/lib/openship/edge/acme:/var/www/acme:z + - /opt/openship/static:/opt/openship/static:z + # Host-op SSH key (createHostExecutor → host.docker.internal). /dev/null + # when the host channel isn't provisioned → OPENSHIP_HOST_SSH_HOST stays + # unset, and host operations REFUSE (they'd otherwise run against this + # container's filesystem, #509) while ordinary deploys keep working over + # the socket below. + - ${OPENSHIP_HOST_KEY_PATH:-/dev/null}:/run/secrets/openship_host_key:ro + # `host-gateway` is resolved by the daemon and is right on a stock rootful + # install. It is NOT universally right: under rootless Docker it lands in + # RootlessKit's namespace rather than the host's, so the host's sshd isn't + # there (#482). OPENSHIP_HOST_GATEWAY re-points the alias (e.g. at the box's + # LAN address) without patching this file, which `openship up` rewrites. + extra_hosts: ["host.docker.internal:${OPENSHIP_HOST_GATEWAY:-host-gateway}"] + env_file: [.env] + environment: + NODE_ENV: production + PORT: "${API_PORT:-4000}" + DATABASE_URL: postgresql://${POSTGRES_USER:-openship}:${POSTGRES_PASSWORD:?missing from .env — re-run openship up to regenerate it}@postgres:5432/${POSTGRES_DB:-openship} + REDIS_URL: redis://redis:6379 + OPENSHIP_EDGE_MODE: docker + OPENSHIP_EDGE_CONTAINER: openship-edge + depends_on: + postgres: { condition: service_healthy } + redis: { condition: service_healthy } + healthcheck: + test: ["CMD-SHELL", "node -e \"fetch('http://127.0.0.1:${API_PORT:-4000}/api/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))\""] + interval: 10s + timeout: 5s + retries: 12 + start_period: 40s + + dashboard: + image: ${OPENSHIP_IMAGE_REGISTRY:-ghcr.io/oblien}/openship-dashboard:${OPENSHIP_VERSION:-latest} + restart: unless-stopped + logging: *default-logging + # Loopback by default (see api note); OPENSHIP_BIND_ADDR opts into a public interface. + ports: ["${OPENSHIP_BIND_ADDR:-127.0.0.1}:${DASHBOARD_PORT:-3001}:${DASHBOARD_PORT:-3001}"] + env_file: [.env] + environment: + NODE_ENV: production + PORT: "${DASHBOARD_PORT:-3001}" + HOSTNAME: "0.0.0.0" + INTERNAL_API_URL: http://api:${API_PORT:-4000} + depends_on: + api: { condition: service_healthy } + healthcheck: + test: ["CMD", "node", "-e", "fetch('http://127.0.0.1:'+process.env.PORT+'/',{redirect:'manual'}).then(r=>process.exit(r.status<500?0:1)).catch(()=>process.exit(1))"] + interval: 5s + timeout: 5s + retries: 24 + start_period: 20s + + edge: + image: ${OPENSHIP_IMAGE_REGISTRY:-ghcr.io/oblien}/openship-edge:${OPENSHIP_VERSION:-latest} + # PINNED, and it must stay pinned: the api reaches the edge by NAME through + # DockerEdgeExecutor (OPENSHIP_EDGE_CONTAINER above), and "ours" edge + # detection greps `docker ps --filter name=openship-edge`. Without this, + # compose derives `-edge-1` from the directory and every + # `docker exec` into the edge fails with "No such container: openship-edge" — + # which silently migrated 0 sites after the operator's proxy was stopped. + # Safe here: the edge is a singleton (host networking, one per box). + container_name: openship-edge + restart: unless-stopped + logging: *default-logging + network_mode: host + depends_on: + api: { condition: service_healthy } + dashboard: { condition: service_healthy } + volumes: + - /private/var/lib/openship/edge/sites-enabled:/usr/local/openresty/nginx/conf/sites-enabled:z + - /private/etc/letsencrypt:/etc/letsencrypt:z + - /private/var/lib/openship/edge/acme:/var/www/acme:z + - /opt/openship/static:/opt/openship/static:z + +volumes: + postgres_data: + redis_data: diff --git a/apps/api/test/helpers/monthly-cloud-offer.ts b/apps/api/test/helpers/monthly-cloud-offer.ts index b95c4cb7c..e55e44596 100644 --- a/apps/api/test/helpers/monthly-cloud-offer.ts +++ b/apps/api/test/helpers/monthly-cloud-offer.ts @@ -1,6 +1,6 @@ import type { OblienEntitlement, OblienSubscription } from "@repo/platform/engine/lib/oblien-billing-api"; -/** Saved v9 terms and the SDK 2.8 capacity shape, independent of today's catalog. */ +/** OpenShip saved v9 terms, independent of today's catalog and provider wire names. */ export function monthlyCloudBilling(organizationId = "org_1", namespace = "os-customer") { const periodStart = "2026-10-01T00:00:00Z"; const periodEnd = "2026-11-01T00:00:00Z"; diff --git a/apps/api/test/helpers/oblien-vm-billing.ts b/apps/api/test/helpers/oblien-vm-billing.ts new file mode 100644 index 000000000..9f8ea75f9 --- /dev/null +++ b/apps/api/test/helpers/oblien-vm-billing.ts @@ -0,0 +1,40 @@ +/** Build provider HTTP fixtures from OpenShip's saved billing model. This helper + * is only for fixture construction; production must read native SDK 3 payloads. + * Customer metadata and other opaque dictionaries are deliberately untouched. */ +export function providerBillingFixture(value: unknown): unknown { + if (!value || typeof value !== "object" || Array.isArray(value)) return value; + const source = value as Record; + const native = { ...source }; + if ("vcpus" in source && "memoryMb" in source && "diskGb" in source && "workspaces" in source) { + native.vms = source.workspaces; + delete native.workspaces; + } + if (source.resourceLimits && typeof source.resourceLimits === "object") { + const limits = { ...(source.resourceLimits as Record) }; + if ("max_workspaces" in limits) { + limits.max_vms = limits.max_workspaces; + delete limits.max_workspaces; + } + native.resourceLimits = limits; + } + if (source.onOverdraftAction === "stop_workspaces") native.onOverdraftAction = "stop_vms"; + if (source.service === "workspace_vm") native.service = "vm_compute"; + for (const key of [ + "data", + "capacity", + "quote", + "current", + "next", + "subscription", + "offer", + "policy", + "pendingChange", + "change", + "pendingCheckout", + "checkout", + ]) + if (source[key] && typeof source[key] === "object") + native[key] = providerBillingFixture(source[key]); + if (Array.isArray(source.presets)) native.presets = source.presets.map(providerBillingFixture); + return native; +} diff --git a/apps/api/test/lib/cloud-capacity.test.ts b/apps/api/test/lib/cloud-capacity.test.ts index 4be698883..621c3145c 100644 --- a/apps/api/test/lib/cloud-capacity.test.ts +++ b/apps/api/test/lib/cloud-capacity.test.ts @@ -20,7 +20,7 @@ vi.mock("@repo/db", () => ({ })); vi.mock("@repo/platform/engine/lib/oblien-client", () => ({ getOblienClient: () => ({ - workspaces: { get: h.workspace }, + vms: { get: h.workspace }, namespaces: { get: h.namespace }, }), })); diff --git a/apps/api/test/lib/cloud-docker-workspace.test.ts b/apps/api/test/lib/cloud-docker-workspace.test.ts index b2c2cf0cf..9e4d1f18b 100644 --- a/apps/api/test/lib/cloud-docker-workspace.test.ts +++ b/apps/api/test/lib/cloud-docker-workspace.test.ts @@ -30,8 +30,8 @@ vi.mock("@repo/adapters", async (original) => ({ ...await original(), Oblien: class { constructor(credentials: unknown) { h.credentials.push(credentials); } - workspaces = { create: h.create, retryCreation: h.retry, get: h.get, list: h.list }; - workspace = (id: string) => ({ id, get: h.get, start: h.start, resume: h.resume, + vms = { create: h.create, retryCreation: h.retry, get: h.get, list: h.list }; + vm = (id: string) => ({ id, get: h.get, start: h.start, resume: h.resume, lifecycle: { makePermanent: h.permanent }, resources: { update: h.resize }, runtime: async () => ({ exec: { run: h.exec } }), invalidateRuntime: h.invalidate }); }, @@ -86,7 +86,7 @@ beforeEach(() => { return { success: true, relaunched: true }; }); h.inFlight.mockResolvedValue([]); - h.list.mockResolvedValue({ workspaces: [], total: 0, limit: 100, page: 1 }); + h.list.mockResolvedValue({ vms: [], total: 0, limit: 100, page: 1 }); h.discard.mockImplementation(async () => { binding = undefined; }); @@ -190,7 +190,7 @@ describe("Subscribed Cloud server provisioning and retry", () => { it("adopts an earlier workspace discovered after a quota refusal instead of creating another", async () => { binding = { ...input, namespace: "namespace-a", provisionKey: "legacy-key", workspaceId: null, state: "provisioning" }; h.create.mockRejectedValue(Object.assign(new Error("namespace workspace limit"), { status: 409, code: "NAMESPACE_LIMIT_REACHED" })); - h.list.mockResolvedValue({ workspaces: [await h.get()], total: 1, limit: 100, page: 1 }); + h.list.mockResolvedValue({ vms: [await h.get()], total: 1, limit: 100, page: 1 }); await expect(ensureCloudDockerWorkspace(input)).resolves.toEqual({ projectId: project.id, workspaceId: "workspace-a", ownerWorkspaceId: "owner-a" }); expect(h.create).toHaveBeenCalledOnce(); expect(h.discard).not.toHaveBeenCalled(); @@ -209,8 +209,8 @@ describe("Subscribed Cloud server provisioning and retry", () => { binding = { ...input, namespace: "namespace-a", provisionKey: "legacy-key", workspaceId: null, state: "provisioning" }; h.create.mockRejectedValue(Object.assign(new Error("namespace workspace limit"), { status: 409, code: "NAMESPACE_LIMIT_REACHED" })); const workspace = await h.get(); - h.list.mockResolvedValueOnce({ workspaces: [{ ...workspace, namespace: "other-namespace", id: "foreign-vm" }], total: 2, limit: 1, page: 1 }); - h.list.mockResolvedValueOnce({ workspaces: [workspace], total: 2, limit: 1, page: 2 }); + h.list.mockResolvedValueOnce({ vms: [{ ...workspace, namespace: "other-namespace", id: "foreign-vm" }], total: 2, limit: 1, page: 1 }); + h.list.mockResolvedValueOnce({ vms: [workspace], total: 2, limit: 1, page: 2 }); await expect(ensureCloudDockerWorkspace(input)).resolves.toMatchObject({ workspaceId: "workspace-a" }); expect(h.list).toHaveBeenNthCalledWith(2, { page: 2, limit: 100 }); expect(h.attach).toHaveBeenCalledExactlyOnceWith(ownerRef, project.organizationId, "namespace-a", "workspace-a"); @@ -219,7 +219,7 @@ describe("Subscribed Cloud server provisioning and retry", () => { binding = { ...input, namespace: "namespace-a", provisionKey: "legacy-key", workspaceId: null, state: "provisioning" }; h.create.mockRejectedValue(Object.assign(new Error("namespace workspace limit"), { status: 409, code: "NAMESPACE_LIMIT_REACHED" })); const workspace = await h.get(); - h.list.mockResolvedValue({ workspaces: [workspace, { ...workspace, id: "duplicate-vm" }], total: 2, limit: 100, page: 1 }); + h.list.mockResolvedValue({ vms: [workspace, { ...workspace, id: "duplicate-vm" }], total: 2, limit: 100, page: 1 }); await expect(ensureCloudDockerWorkspace(input)).rejects.toThrow("Multiple Cloud servers"); expect(h.attach).not.toHaveBeenCalled(); expect(h.discard).not.toHaveBeenCalled(); @@ -288,7 +288,7 @@ describe("Subscribed Cloud server provisioning and retry", () => { const error = Object.assign(new Error("initial provisioning failed"), { status: 422, code: "CREATE_FAILED", - details: { workspace_id: "workspace-a" }, + details: { vm_id: "workspace-a" }, }); h.create.mockRejectedValueOnce(error); await expect(ensureCloudDockerWorkspace(input)).rejects.toBe(error); @@ -328,7 +328,7 @@ describe("Subscribed Cloud server provisioning and retry", () => { h.create.mockRejectedValueOnce( Object.assign(new Error("creation failed"), { status: 422, - details: { workspace_id: "workspace-a" }, + details: { vm_id: "workspace-a" }, }), ); const created = await h.get(); diff --git a/apps/api/test/lib/cloud-namespace-token-reuse.test.ts b/apps/api/test/lib/cloud-namespace-token-reuse.test.ts index c27c36e3b..f5623dd04 100644 --- a/apps/api/test/lib/cloud-namespace-token-reuse.test.ts +++ b/apps/api/test/lib/cloud-namespace-token-reuse.test.ts @@ -98,7 +98,7 @@ it("renews expiring credentials once across concurrent clients", async () => { ]); vi.setSystemTime(Date.now() + 1741_000); await Promise.all( - clients.map(({ client }) => client._http.request({ method: "GET", path: "/workspace" })), + clients.map(({ client }) => client._http.request({ method: "GET", path: "/vms" })), ); expect(h.mint).toHaveBeenCalledTimes(2); expect(fetcher.mock.calls).toHaveLength(2); @@ -111,12 +111,12 @@ it("evicts rejected credentials without replaying a mutation", async () => { .mockImplementation(async () => Response.json({ success: true })); vi.stubGlobal("fetch", fetcher); const { client } = await getNamespaceClient("org", "space"); - await expect(client._http.request({ method: "POST", path: "/workspace" })).rejects.toMatchObject({ + await expect(client._http.request({ method: "POST", path: "/vms" })).rejects.toMatchObject({ status: 401, }); expect(fetcher).toHaveBeenCalledTimes(1); const next = await getNamespaceClient("org", "space"); - await next.client._http.request({ method: "GET", path: "/workspace" }); + await next.client._http.request({ method: "GET", path: "/vms" }); expect(h.mint).toHaveBeenCalledTimes(2); expect(new Headers(fetcher.mock.calls[1]![1].headers).get("authorization")).toBe( "Bearer token-2", @@ -171,9 +171,9 @@ it("does not evict a renewed token when an older request returns a late 401", as .mockImplementation(async () => Response.json({ success: true })); vi.stubGlobal("fetch", fetcher); const { client } = await getNamespaceClient("org", "space"); - const old = client._http.request({ method: "POST", path: "/workspace" }).catch((error) => error); + const old = client._http.request({ method: "POST", path: "/vms" }).catch((error) => error); vi.setSystemTime(Date.now() + 1741_000); - await client._http.request({ method: "GET", path: "/workspace" }); + await client._http.request({ method: "GET", path: "/vms" }); rejectOld(Response.json({ code: "token_revoked" }, { status: 401 })); expect(await old).toMatchObject({ status: 401 }); await getNamespaceClient("org", "space"); diff --git a/apps/api/test/lib/cloud-preflight.test.ts b/apps/api/test/lib/cloud-preflight.test.ts index e419c3712..2051f666a 100644 --- a/apps/api/test/lib/cloud-preflight.test.ts +++ b/apps/api/test/lib/cloud-preflight.test.ts @@ -1,7 +1,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; const h = vi.hoisted(() => ({ token: vi.fn(), routes: vi.fn(), checkSlug: vi.fn(), verify: vi.fn(), scopedVerify: vi.fn(), spend: vi.fn(), createPlatform: vi.fn(), quota: vi.fn() })); -vi.mock("@repo/adapters", () => ({ createPlatform: h.createPlatform })); +vi.mock("@repo/adapters", async original => ({ ...await original(), createPlatform: h.createPlatform })); vi.mock("@repo/platform/engine/config/env", () => ({ env: { OBLIEN_API_URL: "https://api.oblien.com" } })); vi.mock("@repo/platform/engine/lib/routing-domains", () => ({ getRoutingBaseDomain: () => "opsh.io" })); vi.mock("@repo/platform/engine/lib/openship-cloud", () => ({ diff --git a/apps/api/test/lib/cloud-resource-limits.test.ts b/apps/api/test/lib/cloud-resource-limits.test.ts index a25adc3c9..69433a654 100644 --- a/apps/api/test/lib/cloud-resource-limits.test.ts +++ b/apps/api/test/lib/cloud-resource-limits.test.ts @@ -2,8 +2,9 @@ import { beforeEach, describe, expect, it, vi } from "vitest"; const h = vi.hoisted(() => ({ quota: vi.fn(), get: vi.fn(), update: vi.fn() })); vi.mock("@repo/platform/engine/lib/oblien-client", () => ({ - getOblienClient: () => ({ workspaces: { getQuota: h.quota }, namespaces: { get: h.get, update: h.update } }), + getOblienClient: () => ({ vms: { getQuota: h.quota }, namespaces: { get: h.get, update: h.update } }), })); +import { toOblienVmLimits } from "@repo/adapters"; import { cloudNamespaceLimits, initialCloudNamespaceLimits, readCloudCapacity, readCloudCapacityPool, syncCloudResourceLimits } from "@repo/platform/engine/lib/cloud-resource-limits"; beforeEach(() => { @@ -11,15 +12,15 @@ beforeEach(() => { h.quota.mockImplementation(() => { throw new Error("Openship must not calculate provider capacity"); }); h.get.mockResolvedValue({ data: { id: "ns-a", slug: "tenant-a", resource_limits: null } }); h.update.mockImplementation(async (_id, input) => ({ data: { id: "ns-a", slug: "tenant-a", ...input, - effective_resource_limits: { max_workspaces: 52, max_vcpus: 32, max_ram_mb: 65536, max_disk_gb: 1024 }, + effective_resource_limits: { max_vms: 52, max_vcpus: 32, max_ram_mb: 65536, max_disk_gb: 1024 }, } })); }); describe("Oblien-owned namespace capacity", () => { it("displays provider allocations in their real units without using credit balances or owner limits", async () => { h.get.mockResolvedValue({ success: true, data: { slug: "tenant-a", - effective_resource_limits: cloudNamespaceLimits("pro"), - allocated_resource_usage: { workspaces: 3, vcpus: 2.5, ram_mb: 4096, disk_gb: 48.5, pending_updates: 1 }, + effective_resource_limits: toOblienVmLimits(cloudNamespaceLimits("pro")), + allocated_resource_usage: { vms: 3, vcpus: 2.5, ram_mb: 4096, disk_gb: 48.5, pending_updates: 1 }, } }); expect(await readCloudCapacity("tenant-a")).toEqual({ workspaces: { used: 3, max: 1 }, vcpus: { used: 2.5, max: 4 }, ramMb: { used: 4096, max: 16384 }, diskGb: { used: 48.5, max: 250 } }); @@ -36,8 +37,8 @@ describe("Oblien-owned namespace capacity", () => { }); it("converts disk to MB for admission without confusing zero and unlimited", async () => { h.get.mockResolvedValue({ success: true, data: { slug: "tenant-a", - effective_resource_limits: { max_workspaces: null, max_total_vcpus: 0, max_total_ram_mb: 8192, max_total_disk_gb: 16 }, - allocated_resource_usage: { workspaces: 0, vcpus: 0, ram_mb: 0, disk_gb: 1.25, pending_updates: 0 }, + effective_resource_limits: { max_vms: null, max_total_vcpus: 0, max_total_ram_mb: 8192, max_total_disk_gb: 16 }, + allocated_resource_usage: { vms: 0, vcpus: 0, ram_mb: 0, disk_gb: 1.25, pending_updates: 0 }, } }); expect(await readCloudCapacityPool("tenant-a")).toEqual({ workspaces: { used: 0, max: null }, cpuCores: { used: 0, max: 0 }, memoryMb: { used: 0, max: 8192 }, diskMb: { used: 1280, max: 16384 } }); @@ -50,37 +51,37 @@ describe("Oblien-owned namespace capacity", () => { }); it("declares finite retail VM and aggregate caps without reading enterprise owner capacity", async () => { expect(cloudNamespaceLimits("team")).toEqual({ max_workspaces: 1, max_vcpus: 8, max_ram_mb: 32768, max_disk_gb: 600, max_total_vcpus: 8, max_total_ram_mb: 32768, max_total_disk_gb: 600 }); - await expect(initialCloudNamespaceLimits()).resolves.toEqual({ max_workspaces: 0, max_vcpus: 1, max_ram_mb: 1024, max_disk_gb: 8, max_total_vcpus: 0, max_total_ram_mb: 0, max_total_disk_gb: 0 }); + await expect(initialCloudNamespaceLimits()).resolves.toEqual({ max_vms: 0, max_vcpus: 1, max_ram_mb: 1024, max_disk_gb: 8, max_total_vcpus: 0, max_total_ram_mb: 0, max_total_disk_gb: 0 }); await syncCloudResourceLimits("tenant-a", "team"); - expect(h.update).toHaveBeenCalledWith("ns-a", { resource_limits: cloudNamespaceLimits("team") }); + expect(h.update).toHaveBeenCalledWith("ns-a", { resource_limits: toOblienVmLimits(cloudNamespaceLimits("team")) }); expect(h.quota).not.toHaveBeenCalled(); }); - it("sends an older saved offer unchanged and accepts Oblien's lower effective ceiling", async () => { + it("preserves an older saved offer when sending VM limits and accepts Oblien's lower effective ceiling", async () => { const saved = Object.freeze({ max_workspaces: 52, max_vcpus: 200, max_ram_mb: 417792, max_disk_gb: 64, max_total_vcpus: 8, max_total_ram_mb: 16384, max_total_disk_gb: 256 }); await syncCloudResourceLimits("tenant-a", "team", saved); - expect(h.update).toHaveBeenCalledWith("ns-a", { resource_limits: saved }); + expect(h.update).toHaveBeenCalledWith("ns-a", { resource_limits: toOblienVmLimits(saved) }); expect(saved.max_vcpus).toBe(200); expect(h.quota).not.toHaveBeenCalled(); }); it("preserves explicit customer restrictions even when the owner's capacity is unlimited", async () => { const saved = Object.freeze({ max_workspaces: 7, max_vcpus: 2, max_ram_mb: 4096, max_disk_gb: 12, max_total_vcpus: 3, max_total_ram_mb: 6144, max_total_disk_gb: 24 }); await syncCloudResourceLimits("tenant-a", "team", saved); - expect(h.update).toHaveBeenCalledWith("ns-a", { resource_limits: saved }); + expect(h.update).toHaveBeenCalledWith("ns-a", { resource_limits: toOblienVmLimits(saved) }); expect(h.quota).not.toHaveBeenCalled(); }); it("does not rewrite declared policy when effective capacity differs", async () => { const declared = { max_workspaces: 52, max_vcpus: 200, max_ram_mb: 417792, max_disk_gb: 64, max_total_vcpus: 8, max_total_ram_mb: 16384, max_total_disk_gb: 256 }; - h.get.mockResolvedValue({ data: { id: "ns-a", slug: "tenant-a", resource_limits: declared, - effective_resource_limits: { ...declared, max_vcpus: 32, max_ram_mb: 65536 }, + h.get.mockResolvedValue({ data: { id: "ns-a", slug: "tenant-a", resource_limits: toOblienVmLimits(declared), + effective_resource_limits: { ...toOblienVmLimits(declared), max_vcpus: 32, max_ram_mb: 65536 }, } }); await syncCloudResourceLimits("tenant-a", "team", declared); expect(h.update).not.toHaveBeenCalled(); expect(h.quota).not.toHaveBeenCalled(); }); it("clears old declared caps only after a verified enterprise entitlement", async () => { - h.get.mockResolvedValue({ data: { id: "ns-a", slug: "tenant-a", resource_limits: { max_workspaces: 5 } } }); + h.get.mockResolvedValue({ data: { id: "ns-a", slug: "tenant-a", resource_limits: { max_vms: 5 } } }); await syncCloudResourceLimits("tenant-a", "enterprise"); - expect(h.update).toHaveBeenCalledWith("ns-a", { resource_limits: { max_workspaces: null, max_vcpus: null, max_ram_mb: null, max_disk_gb: null, max_total_vcpus: null, max_total_ram_mb: null, max_total_disk_gb: null } }); + expect(h.update).toHaveBeenCalledWith("ns-a", { resource_limits: { max_vms: null, max_vcpus: null, max_ram_mb: null, max_disk_gb: null, max_total_vcpus: null, max_total_ram_mb: null, max_total_disk_gb: null } }); }); it("cannot change policy when the provider returns another namespace", async () => { h.get.mockResolvedValue({ data: { id: "ns-b", slug: "tenant-b" } }); diff --git a/apps/api/test/lib/cloud-resource-proxy.test.ts b/apps/api/test/lib/cloud-resource-proxy.test.ts index f41b50777..0c6168e0a 100644 --- a/apps/api/test/lib/cloud-resource-proxy.test.ts +++ b/apps/api/test/lib/cloud-resource-proxy.test.ts @@ -12,7 +12,7 @@ vi.mock("../../src/lib/request-context", () => ({ getRequestContext: () => ({ or vi.mock("../../src/modules/cloud/cloud-relay-access", () => ({ authorizeCloudRelayWorkspace: async () => null })); vi.mock("@repo/platform/engine/lib/openship-cloud", () => ({ ensureNamespace: async () => "ns-a" })); vi.mock("@repo/platform/engine/lib/oblien-client", () => ({ getOblienClient: () => ({ - workspaces: { get: h.workspaceGet }, + vms: { get: h.workspaceGet }, pages: { list: h.pageList, get: h.pageGet, create: h.pageCreate, deploy: h.pageDeploy, delete: h.pageDelete }, domain: { routes: h.routes }, routes: { set: h.setRoutes }, }) })); @@ -46,18 +46,18 @@ describe("desktop-to-SaaS Cloud resource delegation", () => { it("round-trips page reads and redeploys through the SaaS ownership checks", async () => { const proxy = createRemoteCloudAdmin("org-a"); expect(await proxy.pages!.get("site-a")).toMatchObject({ page: { namespace: "ns-a" } }); - await proxy.pages!.deploy("site-a", { workspace_id: "ws-a", path: "/app/dist" }); - expect(h.pageDeploy).toHaveBeenCalledWith("site-a", { workspace_id: "ws-a", path: "/app/dist" }); + await proxy.pages!.deploy("site-a", { vm_id: "ws-a", path: "/app/dist" }); + expect(h.pageDeploy).toHaveBeenCalledWith("site-a", { vm_id: "ws-a", path: "/app/dist" }); expect(h.spend).toHaveBeenCalledWith("org-a", null); }); it("ignores the client's namespace and pins a create to its server identity", async () => { - await createRemoteCloudAdmin("org-a").pages!.create({ workspace_id: "ws-a", path: "/app/dist", name: "Site", slug: "site-a", namespace: "ns-b" }); + await createRemoteCloudAdmin("org-a").pages!.create({ vm_id: "ws-a", path: "/app/dist", name: "Site", slug: "site-a", namespace: "ns-b" }); expect(h.pageCreate).toHaveBeenCalledWith(expect.objectContaining({ namespace: "ns-a" })); }); it("refuses both foreign page reads and foreign source exports", async () => { const pages = createRemoteCloudAdmin("org-a").pages!; await expect(pages.get("site-b")).rejects.toMatchObject({ status: 404 }); - await expect(pages.create({ workspace_id: "ws-b", path: "/app/dist", name: "Site", slug: "site-a" })).rejects.toMatchObject({ status: 404 }); + await expect(pages.create({ vm_id: "ws-b", path: "/app/dist", name: "Site", slug: "site-a" })).rejects.toMatchObject({ status: 404 }); expect(h.pageCreate).not.toHaveBeenCalled(); }); it("preserves provider 404 so the runtime can create a missing page", async () => { diff --git a/apps/api/test/lib/cloud-tenant-admin.test.ts b/apps/api/test/lib/cloud-tenant-admin.test.ts index fe6313b97..b6e113560 100644 --- a/apps/api/test/lib/cloud-tenant-admin.test.ts +++ b/apps/api/test/lib/cloud-tenant-admin.test.ts @@ -5,7 +5,7 @@ const h = vi.hoisted(() => ({ workspaceGet: vi.fn(), routes: vi.fn(), setRoutes: vi.fn(), spend: vi.fn(), pageList: vi.fn(), })); vi.mock("@repo/platform/engine/lib/oblien-client", () => ({ getOblienClient: () => ({ - workspaces: { get: h.workspaceGet }, + vms: { get: h.workspaceGet }, pages: { list: h.pageList, get: h.pageGet, create: h.pageCreate, delete: h.pageDelete, disable: h.pageDisable, deploy: h.pageDeploy }, domain: { routes: h.routes }, routes: { set: h.setRoutes }, }) })); @@ -32,12 +32,12 @@ describe("admin-only cloud delegation", () => { }); it("rejects another customer's workspace before exporting a page", async () => { const proxy = createTenantCloudAdmin("org-one", "ns-own"); - await expect(proxy.pages!.create({ workspace_id: "ws-other", path: "/app/dist", name: "test", slug: "test" })).rejects.toMatchObject({ statusCode: 404 }); + await expect(proxy.pages!.create({ vm_id: "ws-other", path: "/app/dist", name: "test", slug: "test" })).rejects.toMatchObject({ statusCode: 404 }); expect(h.pageCreate).not.toHaveBeenCalled(); }); it("pins page creation to the authenticated namespace", async () => { const proxy = createTenantCloudAdmin("org-one", "ns-own"); - await proxy.pages!.create({ workspace_id: "ws-own", path: "/app/dist", name: "test", slug: "test", namespace: "ns-other" }); + await proxy.pages!.create({ vm_id: "ws-own", path: "/app/dist", name: "test", slug: "test", namespace: "ns-other" }); expect(h.pageCreate).toHaveBeenCalledWith(expect.objectContaining({ namespace: "ns-own" })); expect(h.spend).toHaveBeenCalledWith("org-one", undefined); }); @@ -45,7 +45,7 @@ describe("admin-only cloud delegation", () => { const pages = createTenantCloudAdmin("org-one", "ns-own").pages!; await expect(pages.get("other-page")).rejects.toMatchObject({ statusCode: 404 }); await expect(pages.delete("other-page")).rejects.toMatchObject({ statusCode: 404 }); - await expect(pages.deploy("other-page", { workspace_id: "ws-own", path: "/app/dist" })).rejects.toMatchObject({ statusCode: 404 }); + await expect(pages.deploy("other-page", { vm_id: "ws-own", path: "/app/dist" })).rejects.toMatchObject({ statusCode: 404 }); expect(h.pageDelete).not.toHaveBeenCalled(); expect(h.pageDeploy).not.toHaveBeenCalled(); }); @@ -59,11 +59,11 @@ describe("admin-only cloud delegation", () => { }); it("validates both hostname and every route target before an admin mutation", async () => { const proxy = createTenantCloudAdmin("org-one", "ns-own"); - const routes = [{ match: { path: "/" }, action: { kind: "proxy" as const, workspace: "ws-other", port: 8080 } }]; + const routes = [{ match: { path: "/" }, action: { kind: "proxy" as const, vm: "ws-other", port: 8080 } }]; await expect(proxy.setRoutes!("own.opsh.io", { routes })).rejects.toMatchObject({ statusCode: 404 }); await expect(proxy.setRoutes!("other.opsh.io", { routes: [] })).rejects.toMatchObject({ statusCode: 404 }); expect(h.setRoutes).not.toHaveBeenCalled(); - await proxy.setRoutes!("own.opsh.io", { routes: [{ ...routes[0]!, action: { kind: "proxy", workspace: "ws-own", port: 8080 } }] }); + await proxy.setRoutes!("own.opsh.io", { routes: [{ ...routes[0]!, action: { kind: "proxy", vm: "ws-own", port: 8080 } }] }); expect(h.setRoutes).toHaveBeenCalledOnce(); }); it("does not use reseller authority for unverified external proxy origins", async () => { diff --git a/apps/api/test/lib/controller-sse-drain.test.ts b/apps/api/test/lib/controller-sse-drain.test.ts new file mode 100644 index 000000000..fd2e2ffcc --- /dev/null +++ b/apps/api/test/lib/controller-sse-drain.test.ts @@ -0,0 +1,71 @@ +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { Hono } from "hono"; +import type { SSEStreamingApi } from "hono/streaming"; +import { closeControllerStreams, resumeControllerStreams, streamSSE } from "../../src/lib/sse"; +import { drainBackgroundWork } from "@repo/platform/engine/lib/background-work"; + +beforeEach(() => resumeControllerStreams()); +afterEach(async () => { + closeControllerStreams(); + await drainBackgroundWork(); + resumeControllerStreams(); +}); + +it("refuses a stream admitted before the move but initialized after streams were closed", async () => { + let enter!: () => void; + let proceed!: () => void; + const entered = new Promise(resolve => { enter = resolve; }); + const admitted = new Promise(resolve => { proceed = resolve; }); + const callback = vi.fn(async (stream: SSEStreamingApi) => { + await new Promise(resolve => stream.onAbort(resolve)); + }); + const app = new Hono(); + app.get("/events", async c => { + enter(); + await admitted; // permission/metadata read already admitted by the HTTP guard + return streamSSE(c, callback); + }); + const pending = app.request("/events"); + await entered; + closeControllerStreams(); + proceed(); + const response = await pending; + try { + expect(response.status).toBe(503); + expect(await response.json()).toMatchObject({ code: "INSTANCE_CONTROLLER_INACTIVE" }); + expect(callback).not.toHaveBeenCalled(); + } finally { + closeControllerStreams(); + if (!response.bodyUsed) await response.body?.cancel(); + await drainBackgroundWork(); + } +}); + +it("aborts an existing subscriber so the controller can finish draining", async () => { + const aborted = vi.fn(); + const app = new Hono(); + app.get("/events", c => streamSSE(c, async stream => { + await new Promise(resolve => stream.onAbort(() => { aborted(); resolve(); })); + })); + const response = await app.request("/events"); + expect(response.status).toBe(200); + closeControllerStreams(); + await drainBackgroundWork(); + expect(aborted).toHaveBeenCalledOnce(); + await response.body?.cancel(); +}); + +it("accepts streams again when the active controller is resumed after cancellation", async () => { + const callback = vi.fn(async () => {}); + const app = new Hono(); + app.get("/events", c => streamSSE(c, callback)); + closeControllerStreams(); + const refused = await app.request("/events"); + expect(refused.status).toBe(503); + expect(callback).not.toHaveBeenCalled(); + resumeControllerStreams(); + const response = await app.request("/events"); + expect(response.status).toBe(200); + expect(callback).toHaveBeenCalledOnce(); + await response.body?.cancel(); +}); diff --git a/apps/api/test/lib/edge-mgmt-large-body.test.ts b/apps/api/test/lib/edge-mgmt-large-body.test.ts new file mode 100644 index 000000000..64d0515cb --- /dev/null +++ b/apps/api/test/lib/edge-mgmt-large-body.test.ts @@ -0,0 +1,172 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { exec as shellExec } from "node:child_process"; +import { access, mkdtemp, readFile, rm, stat, writeFile } from "node:fs/promises"; +import { createServer, type Server } from "node:http"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { promisify } from "node:util"; +import { LocalExecutor } from "@repo/adapters"; + +const { acquire } = vi.hoisted(() => ({ acquire: vi.fn() })); +vi.mock("@repo/platform/engine/lib/ssh-manager", () => ({ sshManager: { acquire } })); +vi.mock("@repo/platform/engine/lib/ssh-tunnel", () => ({ + tunnelRequest: vi.fn(async () => null), + tunnelStream: vi.fn(), +})); +vi.mock("@repo/adapters", async (original) => ({ + ...(await original()), + resolveOurEdgeContainer: async () => "fixture-edge", +})); + +const { postEdgeMgmt } = await import("@repo/platform/engine/lib/edge-management"); +const exec = promisify(shellExec); +let server: Server | undefined; +let directory: string | undefined; +const files: string[] = []; +let local: LocalExecutor; +beforeEach(() => { + vi.stubEnv("CLOUD_MODE", "false"); + local = new LocalExecutor(); +}); +afterEach(async () => { + await new Promise((resolve) => (server ? server.close(() => resolve()) : resolve())); + server = undefined; + for (const file of files.splice(0)) await rm(file, { force: true }); + if (directory) await rm(directory, { recursive: true, force: true }); + directory = undefined; + await local.dispose(); + vi.unstubAllEnvs(); +}); + +describe("large edge management requests", () => { + it.each([ + { transport: "stdin", bytes: 64000 }, + { transport: "file", bytes: 64000 }, + { transport: "tunnel fallback", bytes: 64000 }, + { transport: "stdin", bytes: 0 }, + { transport: "file", bytes: 0 }, + ])( + "delivers literal JSON via $transport ($bytes padding bytes) and preserves large replies", + async ({ transport, bytes }) => { + directory = await mkdtemp(join(tmpdir(), "openship-response-transport-")); + const marker = join(directory, "must-not-execute"); + const literal = `'; touch ${marker}; $(touch ${marker}) \`touch ${marker}\`\nمرحبا\u0000`; + const payload = { + host: "response.test", + rules: Array.from({ length: 4 }, (_, i) => ({ + pathPrefix: `/response/${i}`, + spec: { response: { body: literal + "x".repeat(bytes) } }, + })), + }; + const reply = { ok: true, count: 4, collected: "مرحبا".repeat(10000) }; + let received = ""; + server = createServer(async (request, response) => { + const chunks: Buffer[] = []; + for await (const chunk of request) chunks.push(Buffer.from(chunk)); + received = Buffer.concat(chunks).toString("utf8"); + response.setHeader("Content-Type", "application/json"); + response.end(JSON.stringify(reply)); + }); + await new Promise((resolve) => server!.listen(0, "127.0.0.1", resolve)); + const address = server.address(); + if (!address || typeof address === "string") throw new Error("Missing fixture port"); + const stdin = transport !== "file"; + const hostWrite = vi.fn(async (path: string, body: string, options: { mode: number }) => { + files.push(path); + await writeFile(path, body, options); + expect((await stat(path)).mode & 0o777).toBe(0o600); + }); + const hostRemove = vi.fn((path: string) => rm(path, { force: true })); + const toLocal = (command: string, input = false) => { + if (Buffer.byteLength(command) >= 128 * 1024) throw new Error("E2BIG"); + const prefix = `docker exec ${input ? "-i " : ""}'fixture-edge' `; + if (!command.startsWith(prefix)) throw new Error("Unexpected fixture command"); + return command.slice(prefix.length).replace("127.0.0.1:9145", `127.0.0.1:${address.port}`); + }; + acquire.mockResolvedValue({ + ...(transport === "tunnel fallback" ? { forwardPort: vi.fn() } : {}), + ...(stdin + ? { + execWithInput: async (command: string, body: import("node:stream").Readable) => { + const path = command.match(/\/tmp\/openship-edge-request-[\w-]+\.json/)?.[0]; + if (!path) throw new Error("Missing private upload path"); + files.push(path); + const result = await local.execWithInput(toLocal(command, true), body); + expect((await stat(path)).mode & 0o777).toBe(0o600); + return result; + }, + } + : {}), + writeFile: hostWrite, + rm: hostRemove, + exec: async (command: string) => { + // Only Docker isolation is elided: stdin, curl, shell, files and HTTP + // are real. The fixture shares the container's file with the host. + if (command.startsWith("docker cp ")) { + if (stdin) throw new Error("stdin-capable executors must not stage a host copy"); + return ""; + } + return (await exec(toLocal(command))).stdout; + }, + }); + await expect(postEdgeMgmt("fixture-server", "/rules", payload)).resolves.toEqual(reply); + expect(JSON.parse(received)).toEqual(payload); + await expect(access(marker)).rejects.toMatchObject({ code: "ENOENT" }); + expect(files).toHaveLength(1); + await expect(access(files[0]!)).rejects.toMatchObject({ code: "ENOENT" }); + if (stdin) { + expect(hostWrite).not.toHaveBeenCalled(); + expect(hostRemove).not.toHaveBeenCalled(); + } + }, + ); + + it("cleans up a failed stdin upload without publishing or retrying through a host copy", async () => { + const hostWrite = vi.fn(); + const commands: string[] = []; + acquire.mockResolvedValue({ + execWithInput: async (command: string) => { + const path = command.match(/\/tmp\/openship-edge-request-[\w-]+\.json/)?.[0]; + if (!path) throw new Error("Missing upload path"); + files.push(path); + await writeFile(path, "partial", { mode: 0o600 }); + return { code: 1, stdout: "", stderr: "connection interrupted" }; + }, + exec: async (command: string) => { + commands.push(command); + if (command.includes("curl") || command.startsWith("docker cp ")) + throw new Error("Failed upload must not publish"); + return (await exec(command.slice("docker exec 'fixture-edge' ".length))).stdout; + }, + writeFile: hostWrite, + }); + await expect( + postEdgeMgmt("fixture-server", "/rules", { body: "x".repeat(65536) }), + ).resolves.toBeNull(); + expect(hostWrite).not.toHaveBeenCalled(); + expect( + commands.some((command) => command.includes("curl") || command.startsWith("docker cp ")), + ).toBe(false); + expect(files).toHaveLength(1); + await expect(access(files[0]!)).rejects.toMatchObject({ code: "ENOENT" }); + }); + + it("cleans up private request files when copying to the edge fails", async () => { + acquire.mockResolvedValue({ + writeFile: async (path: string, body: string, options: { mode: number }) => { + files.push(path); + await writeFile(path, body, options); + }, + exec: async (command: string) => { + if (command.startsWith("docker cp ")) throw new Error("edge unavailable"); + return ""; + }, + rm: (path: string) => rm(path, { force: true }), + }); + await expect( + postEdgeMgmt("fixture-server", "/rules", { body: "x".repeat(65536) }), + ).resolves.toBeNull(); + expect(files).toHaveLength(1); + await expect(readFile(files[0]!)).rejects.toMatchObject({ code: "ENOENT" }); + }); +}); diff --git a/apps/api/test/lib/email-templates.test.ts b/apps/api/test/lib/email-templates.test.ts index 00a84f123..be313f41d 100644 --- a/apps/api/test/lib/email-templates.test.ts +++ b/apps/api/test/lib/email-templates.test.ts @@ -5,7 +5,11 @@ */ import { describe, expect, it } from "vitest"; -import { organizationInviteEmail, resetPasswordOtpEmail } from "@repo/platform/engine/lib/email-templates"; +import { + existingAccountEmail, + organizationInviteEmail, + resetPasswordOtpEmail, +} from "@repo/platform/engine/lib/email-templates"; describe("email-templates — HTML injection is neutralized", () => { it("escapes attacker-controlled org name and inviter name in the invite HTML", () => { @@ -64,4 +68,15 @@ describe("email-templates — HTML injection is neutralized", () => { expect(email.html).not.toContain(" { + const email = existingAccountEmail( + { name: '', email: "customer@example.test" }, + "https://dashboard.example.test", + ); + expect(email.html).not.toContain(" { + vi.unstubAllEnvs(); + vi.resetModules(); +}); + +it.each([ + [undefined, "https://vm.oblien.com"], + ["https://runtime.staging.example", "https://runtime.staging.example"], +])("configures the native VM credential endpoint with override %s", async (override, expected) => { + vi.resetModules(); + vi.stubEnv("NODE_ENV", "test"); + vi.stubEnv("BILLING_ENABLED", "false"); + vi.stubEnv("OBLIEN_RUNTIME_URL", override); + const { env } = await import("@repo/platform/engine/config/env"); + expect(env.OBLIEN_RUNTIME_URL).toBe(expected); +}); diff --git a/apps/api/test/lib/permission-grant-satisfiability.test.ts b/apps/api/test/lib/permission-grant-satisfiability.test.ts index f4baf51a2..bc4816867 100644 --- a/apps/api/test/lib/permission-grant-satisfiability.test.ts +++ b/apps/api/test/lib/permission-grant-satisfiability.test.ts @@ -24,9 +24,10 @@ import type { RequestContext } from "../../src/lib/request-context"; * the grantable surface without proving it authorizes something → this fails. */ +// Exercise grant algebra under an owner's ceiling; members cannot delegate admin. vi.mock("@repo/db", () => ({ repos: { - member: { find: vi.fn(async () => ({ id: "m1", role: "member" })) }, + member: { find: vi.fn(async () => ({ id: "m1", role: "owner" })) }, project: { findById: vi.fn(async (id: string) => (id === "*" ? null : { id, organizationId: "org1" })), findEnvVarById: vi.fn(async () => null), diff --git a/apps/api/test/lib/permission-own-projects.test.ts b/apps/api/test/lib/permission-own-projects.test.ts index d1b2a52b9..0f3a3edf0 100644 --- a/apps/api/test/lib/permission-own-projects.test.ts +++ b/apps/api/test/lib/permission-own-projects.test.ts @@ -3,9 +3,10 @@ import { describe, it, expect, vi } from "vitest"; // checkPermission touches only member.find (membership gate) and, for a // concrete project id, project.findById (org resolution). We pass the grant // source explicitly via opts, so resourceGrant is never consulted. +// The owner can delegate project admin; the token must still satisfy each grant. vi.mock("@repo/db", () => ({ repos: { - member: { find: vi.fn(async () => ({ id: "m1", role: "member" })) }, + member: { find: vi.fn(async () => ({ id: "m1", role: "owner" })) }, project: { findById: vi.fn(async (id: string) => id === "*" ? null : { id, organizationId: "org1" }, diff --git a/apps/api/test/lib/permission-wildcard-list-arm.test.ts b/apps/api/test/lib/permission-wildcard-list-arm.test.ts index d92e3788a..0620fa0ff 100644 --- a/apps/api/test/lib/permission-wildcard-list-arm.test.ts +++ b/apps/api/test/lib/permission-wildcard-list-arm.test.ts @@ -19,9 +19,10 @@ import { describe, it, expect, vi } from "vitest"; +// The owner's role ceiling permits delegation; the tests enforce grant widths. vi.mock("@repo/db", () => ({ repos: { - member: { find: vi.fn(async () => ({ id: "m1", role: "member" })) }, + member: { find: vi.fn(async () => ({ id: "m1", role: "owner" })) }, project: { findById: vi.fn(async (id: string) => (id === "*" ? null : { id, organizationId: "org1" })), findEnvVarById: vi.fn(async () => null), @@ -198,7 +199,7 @@ describe("a full project wildcard now lists and creates (deliberate widening)", }); describe("non-restricted roles are untouched by this arm", () => { - it("a member is authorized by role, without consulting grants", async () => { + it("an owner is authorized by role, without consulting grants", async () => { const boom = { findForResource: async () => { throw new Error("the role arm must return before any grant lookup"); diff --git a/apps/api/test/lib/post-edge-mgmt.test.ts b/apps/api/test/lib/post-edge-mgmt.test.ts index 9031b4275..8a41c016b 100644 --- a/apps/api/test/lib/post-edge-mgmt.test.ts +++ b/apps/api/test/lib/post-edge-mgmt.test.ts @@ -31,7 +31,6 @@ vi.mock("@repo/platform/engine/lib/ssh-tunnel", () => ({ vi.mock("@repo/platform/engine/lib/ssh-manager", () => ({ sshManager: { retain: vi.fn(), release: vi.fn(), acquire: vi.fn() }, })); -vi.mock("@repo/platform/engine/lib/platform-mode", () => ({ isOblienBackedDeployment: async () => false })); const { findLocal } = vi.hoisted(() => ({ findLocal: vi.fn() })); vi.mock("@repo/db", () => ({ repos: { @@ -39,7 +38,7 @@ vi.mock("@repo/db", () => ({ }, })); -const { postEdgeMgmt } = await import("@repo/platform/engine/lib/project-analytics"); +const { postEdgeMgmt } = await import("@repo/platform/engine/lib/edge-management"); beforeEach(() => { vi.clearAllMocks(); @@ -123,13 +122,13 @@ describe("failure is best-effort by contract", () => { // would abort a routing reconcile over a metrics setting. await expect( postEdgeMgmt(null, "/analytics/config", { hosts: [] }, "org1"), - ).resolves.toBeUndefined(); + ).resolves.toBeNull(); }); it("resolves when the tunnel fails", async () => { tunnelRequest.mockRejectedValue(new Error("tunnel closed")); await expect( postEdgeMgmt("srv1", "/analytics/config", { host: "a.com", paths: true }), - ).resolves.toBeUndefined(); + ).resolves.toBeNull(); }); }); diff --git a/apps/api/test/lib/profile-avatar.test.ts b/apps/api/test/lib/profile-avatar.test.ts new file mode 100644 index 000000000..31d4e4604 --- /dev/null +++ b/apps/api/test/lib/profile-avatar.test.ts @@ -0,0 +1,55 @@ +import { expect, it } from "vitest"; +import { auth } from "@repo/platform/engine/lib/auth"; +import { provisionUser } from "@repo/platform/engine/lib/provision-user"; +import { mintSession } from "../../src/lib/cloud-auth-proxy"; +import { db, schema, eq } from "@repo/db"; +import { randomUUID } from "node:crypto"; + +it("updates only the signed-in user's avatar through the existing auth endpoint and rejects unsafe URLs", async () => { + const id = randomUUID(); + await provisionUser({ + id, + email: `${id}@example.test`, + name: "Avatar owner", + emailVerified: true, + }); + const session = await mintSession({ purpose: "local-cookie", userId: id }); + const update = (image: unknown, token: string | null = session.token, extra = {}) => + auth.handler( + new Request("http://localhost:4000/api/auth/update-user", { + method: "POST", + headers: { + "Content-Type": "application/json", + ...(token ? { Authorization: `Bearer ${token}` } : {}), + }, + body: JSON.stringify({ image, ...extra }), + }), + ); + expect((await update("https://cdn.example.test/avatar.png", null)).status).toBe(401); + const result = await update("https://cdn.example.test/avatar.png"); + expect(result.status, await result.clone().text()).toBe(200); + expect(await db.query.user.findFirst({ where: eq(schema.user.id, id) })).toMatchObject({ + image: "https://cdn.example.test/avatar.png", + role: "user", + }); + expect( + await auth.api.getSession({ + headers: new Headers({ Authorization: `Bearer ${session.token}` }), + }), + ).toMatchObject({ user: { image: "https://cdn.example.test/avatar.png" } }); + expect( + (await update("https://cdn.example.test/avatar.png", session.token, { role: "admin" })).status, + ).toBe(400); + for (const image of [ + "javascript:alert(1)", + "data:image/svg+xml,", + "https://user:secret@example.test/a.png", + "/avatar.png", + 123, + ]) + expect((await update(image)).status).toBe(400); + expect((await update(null)).status).toBe(200); + expect(await db.query.user.findFirst({ where: eq(schema.user.id, id) })).toMatchObject({ + image: null, + }); +}); diff --git a/apps/api/test/lib/server-log-stream-framing.test.ts b/apps/api/test/lib/server-log-stream-framing.test.ts index 877b4219b..e641ab402 100644 --- a/apps/api/test/lib/server-log-stream-framing.test.ts +++ b/apps/api/test/lib/server-log-stream-framing.test.ts @@ -1,6 +1,6 @@ import { describe, it, expect } from "vitest"; -import { streamChunkBytes } from "@repo/platform/engine/lib/project-analytics"; +import { streamChunkBytes } from "@repo/platform/engine/lib/edge-management"; /** * The server-logs live tail relays the EDGE's SSE frames. `pipe_stream.lua` emits diff --git a/apps/api/test/modules/actions/billing-webhook.test.ts b/apps/api/test/modules/actions/billing-webhook.test.ts index 8e547c7b2..1eb01e4dc 100644 --- a/apps/api/test/modules/actions/billing-webhook.test.ts +++ b/apps/api/test/modules/actions/billing-webhook.test.ts @@ -450,7 +450,7 @@ describe("Actions namespace preparation before checkout", () => { name: "Openship Actions", slug: f.namespace, resource_limits: { - max_workspaces: 0, + max_vms: 0, max_vcpus: 0, max_ram_mb: 0, max_disk_gb: 0, diff --git a/apps/api/test/modules/analytics/analytics-config-push.test.ts b/apps/api/test/modules/analytics/analytics-config-push.test.ts index bef7892dc..4ec55da96 100644 --- a/apps/api/test/modules/analytics/analytics-config-push.test.ts +++ b/apps/api/test/modules/analytics/analytics-config-push.test.ts @@ -32,10 +32,12 @@ vi.mock("@repo/db", () => ({ })); vi.mock("@repo/platform/engine/lib/project-analytics", () => ({ + resolveProjectTrafficSources: vi.fn(async (id: string) => h.sourcesByProject[id] ?? []), +})); +vi.mock("@repo/platform/engine/lib/edge-management", () => ({ postEdgeMgmt: vi.fn(async (serverId: string | null, path: string, body: unknown) => { h.pushes.push({ serverId, path, body }); }), - resolveProjectTrafficSources: vi.fn(async (id: string) => h.sourcesByProject[id] ?? []), })); const { pushProjectAnalyticsConfig, reconcileAnalyticsConfig } = await import( diff --git a/apps/api/test/modules/analytics/geo-rollup.test.ts b/apps/api/test/modules/analytics/geo-rollup.test.ts index d195752ba..84a37d471 100644 --- a/apps/api/test/modules/analytics/geo-rollup.test.ts +++ b/apps/api/test/modules/analytics/geo-rollup.test.ts @@ -34,6 +34,8 @@ vi.mock("@repo/db", () => ({ vi.mock("@repo/platform/engine/lib/project-analytics", () => ({ resolveProjectTrafficSources: vi.fn(async () => h.sources), +})); +vi.mock("@repo/platform/engine/lib/edge-management", () => ({ fetchMgmt: vi.fn(async (_serverId: string, path: string) => { for (const [prefix, value] of h.mgmt) { if (path.startsWith(prefix)) return value; diff --git a/apps/api/test/modules/analytics/overview-traffic.test.ts b/apps/api/test/modules/analytics/overview-traffic.test.ts index 6efa816cf..e88094fc4 100644 --- a/apps/api/test/modules/analytics/overview-traffic.test.ts +++ b/apps/api/test/modules/analytics/overview-traffic.test.ts @@ -28,6 +28,8 @@ vi.mock("@repo/db", () => ({ repos: { vi.mock("@repo/platform/engine/lib/project-analytics", () => ({ resolveProjectTrafficSources: async (_id: string, options: { domain?: string }) => h.sources.filter((source) => !options.domain || source.domain === options.domain), +})); +vi.mock("@repo/platform/engine/lib/edge-management", () => ({ fetchMgmt: h.readLive, })); vi.mock("@repo/platform/engine/modules/system/analytics-scraper", () => ({ scrapeServerIfStale: h.scrape })); diff --git a/apps/api/test/modules/analytics/scrape-to-db.test.ts b/apps/api/test/modules/analytics/scrape-to-db.test.ts index d8c9fde24..4ac9d46f4 100644 --- a/apps/api/test/modules/analytics/scrape-to-db.test.ts +++ b/apps/api/test/modules/analytics/scrape-to-db.test.ts @@ -87,7 +87,7 @@ vi.mock("@repo/db", () => ({ }, })); -vi.mock("@repo/platform/engine/lib/project-analytics", () => ({ +vi.mock("@repo/platform/engine/lib/edge-management", () => ({ fetchMgmt: vi.fn(async (_serverId: string, path: string) => path.startsWith("/analytics/totals") ? h.totals : null, ), @@ -209,7 +209,7 @@ describe("retained minute history", () => { const now = Math.floor(Date.now() / 60_000); h.lastMinute = lastOffset === null ? null : now + lastOffset; const edge = new Map([[now - 300, 40], [now - 2, 8], [now, 3]]); - const { postMgmtJson } = await import("@repo/platform/engine/lib/project-analytics"); + const { postMgmtJson } = await import("@repo/platform/engine/lib/edge-management"); const collect = async (_server: string, _path: string, body: unknown) => { const request = body as { domains: Array<{ domain: string; from: number; to: number }> }; const { from, to } = request.domains[0]; diff --git a/apps/api/test/modules/apps/app-install-routing.test.ts b/apps/api/test/modules/apps/app-install-routing.test.ts index 992ecf782..980fffc58 100644 --- a/apps/api/test/modules/apps/app-install-routing.test.ts +++ b/apps/api/test/modules/apps/app-install-routing.test.ts @@ -703,3 +703,41 @@ it("deduplicates an interpolated secret also declared as a generated config fiel vars.filter((v: { key: string }) => v.key === "OPENSHIP_ADMIN_PASSWORD"), ).toHaveLength(1); }); + +function templateWithConfigPorts() { + const template = structuredClone(getAppTemplate("convex")!); + template.id = "config-ports-test"; + template.configFields = [ + { key: "HOST_PORT", service: "backend", label: "Host port", default: "8181" }, + { key: "HTTP_PORT", service: "backend", label: "Container port", default: "8080" }, + ]; + template.services!.find(service => service.name === "backend")!.ports = ["{{config:HOST_PORT}}:{{config:HTTP_PORT}}/tcp"]; + customTemplateMock.mockResolvedValue({ template }); + return template; +} + +it("substitutes port defaults before validating routes and preserves the cached template", async () => { + const template = templateWithConfigPorts(); + await installApp(ctx, { templateId: template.id, routes: [{ service: "backend", port: 8080, mode: "port" }] }); + expect(payloadFor("backend")?.ports).toEqual(["0.0.0.0:8181:8080/tcp"]); + expect(template.services![0]!.ports).toEqual(["{{config:HOST_PORT}}:{{config:HTTP_PORT}}/tcp"]); +}); + +it("uses supplied port values for both route validation and service creation", async () => { + const template = templateWithConfigPorts(); + await installApp(ctx, { + templateId: template.id, config: { HOST_PORT: "9191", HTTP_PORT: "9090" }, + routes: [{ service: "backend", port: 9090, mode: "port" }], + }); + expect(payloadFor("backend")?.ports).toEqual(["0.0.0.0:9191:9090/tcp"]); +}); + +it("uses resolved authored ports when undoing port-only access on an adopted draft", async () => { + const template = templateWithConfigPorts(); + draftMock.mockResolvedValue({ id: "p1", slug: "draft", name: "Draft" }); + listServicesMock.mockResolvedValue(template.services!.map(service => ({ id: `svc-${service.name}`, name: service.name, ports: service.name === "backend" ? ["0.0.0.0:8181:8080/tcp"] : [] }))); + await installApp(ctx, { templateId: template.id, routes: [{ service: "backend", port: 8080, mode: "custom", customDomain: "ports.example.com" }] }); + expect(updateServiceMock).toHaveBeenCalledWith(ctx, "p1", "svc-backend", expect.objectContaining({ ports: ["8181:8080/tcp"] })); + expect(createServiceMock).not.toHaveBeenCalled(); + expect(setEnvMock).not.toHaveBeenCalled(); +}); diff --git a/apps/api/test/modules/apps/cloud-install-cleanup.integration.test.ts b/apps/api/test/modules/apps/cloud-install-cleanup.integration.test.ts index f92314c2a..383127280 100644 --- a/apps/api/test/modules/apps/cloud-install-cleanup.integration.test.ts +++ b/apps/api/test/modules/apps/cloud-install-cleanup.integration.test.ts @@ -105,7 +105,7 @@ beforeEach(async () => { } const method = init?.method ?? "GET"; requests.push(`${method} ${url.pathname}`); - if (url.pathname === "/workspace" && method === "POST") { + if (url.pathname === "/vms" && method === "POST") { if (createMode === "quota" || createMode === "legacy-quota") { return Response.json( { success: false, code: "NAMESPACE_LIMIT_REACHED" }, @@ -133,27 +133,27 @@ beforeEach(async () => { return Response.json( { code: "CREATE_FAILED", - details: { workspace_id: workspace.id }, + details: { vm_id: workspace.id }, }, { status: 422 }, ); } - if (url.pathname === "/workspace" && method === "GET") { + if (url.pathname === "/vms" && method === "GET") { return Response.json({ success: true, - workspaces: [...workspaces.values()], + vms: [...workspaces.values()], total: workspaces.size, limit: 100, page: 1, }); } - const workspaceId = url.pathname.match(/^\/workspace\/([^/]+)$/)?.[1]; - const retryId = url.pathname.match(/^\/workspace\/([^/]+)\/provisioning\/retry$/)?.[1]; + const workspaceId = url.pathname.match(/^\/vms\/([^/]+)$/)?.[1]; + const retryId = url.pathname.match(/^\/vms\/([^/]+)\/provisioning\/retry$/)?.[1]; if (retryId && method === "POST") { const workspace = workspaces.get(retryId)!; workspace.status = "running"; workspace.provisioning.state = "ready"; - return Response.json({ success: true, workspace }); + return Response.json({ success: true, vm: workspace }); } if (url.pathname.endsWith("/lifecycle/permanent") && method === "POST") return Response.json({ success: true }); @@ -162,7 +162,7 @@ beforeEach(async () => { if (deleting.has(workspaceId)) workspaces.delete(workspaceId); const workspace = workspaces.get(workspaceId); return workspace - ? Response.json({ success: true, workspace }) + ? Response.json({ success: true, vm: workspace }) : Response.json({ code: "NOT_FOUND" }, { status: 404 }); } if (workspaceId && method === "DELETE") { diff --git a/apps/api/test/modules/backups/backup-run-durability.test.ts b/apps/api/test/modules/backups/backup-run-durability.test.ts index 34f6aeb22..f087a5b35 100644 --- a/apps/api/test/modules/backups/backup-run-durability.test.ts +++ b/apps/api/test/modules/backups/backup-run-durability.test.ts @@ -155,6 +155,7 @@ describe("backupRun.transition — status must not ride a payload that can fail" // ─── Layer 2: the orchestrator ─────────────────────────────────────────────── const h = vi.hoisted(() => ({ + recoveryFailure: false, run: null as Record | null, policy: null as Record | null, services: [] as Array>, @@ -225,6 +226,14 @@ vi.mock("@repo/db", () => ({ // `node:stream` is imported INSIDE the factory: vi.mock factories are hoisted // above the file's own imports, so closing over a top-level binding throws. +vi.mock("@repo/platform/engine/modules/mail/backup-lifecycle", () => ({ + prepareMailBackup: async () => ({ + payload: { payloadConfig: {} }, + metadata: { version: 2, domain: "example.test", messageData: false, keys: false }, + cleanup: async () => { if (h.recoveryFailure) throw new Error("mail restart refused"); }, + }), +})); + vi.mock("@repo/adapters", async () => { const { Readable, PassThrough } = await import("node:stream"); // Pulled from the real (side-effect-free) leaf module rather than restated, so a @@ -245,6 +254,7 @@ vi.mock("@repo/adapters", async () => { runPrefix: () => "pfx/", buildManifest: () => ({ version: 1 }), resolveDestination: () => ({ + deleteMany: async (keys: string[]) => ({ deleted: keys, failed: [] }), preflight: async () => ({ ok: true }), put: async (_key: string, body: AsyncIterable) => { let bytesWritten = 0; @@ -270,7 +280,11 @@ vi.mock("@repo/adapters", async () => { }; }, }), - resolveProducer: () => ({ async *produce() {} }), + resolveProducer: () => ({ + async *produce() { + yield { name: "vmail.tar.zst", stream: Readable.from([Buffer.from("bytes")]), sizeHint: 5, payloadKind: "custom_command", metadata: h.artifactMetadata }; + }, + }), }; }); @@ -312,6 +326,7 @@ import { boundedStorableText } from "@repo/platform/engine/modules/deployments/b /** Wire the orchestrator to the REAL run repo over the Postgres-shaped fake, so * a rejected write fails exactly where it fails in production. */ function wireRun(): PgFake { + h.policy = { ...h.policy, payloadKind: "custom_command" }; const pg = makePgFake({ id: "bkr_live", status: "queued" }); const repo = createBackupRunRepo(pg.db as never); h.run = { @@ -330,6 +345,7 @@ function wireRun(): PgFake { } beforeEach(() => { + h.recoveryFailure = false; h.executionRow = null; h.policy = { id: "pol_1", @@ -509,6 +525,15 @@ describe("a terminal status is final — one owner per verdict", () => { }); describe("BackupOrchestrator.execute — hook output cannot fail the backup", () => { + it("records source recovery failures before the terminal verdict and still acknowledges execution", async () => { + const pg = wireRun(); + h.recoveryFailure = true; + await new BackupOrchestrator().execute("bkr_live"); + expect(pg.row.status).toBe("failed"); + expect(pg.row.errorMessage).toContain("Mail recovery needs attention: mail restart refused"); + expect(h.acknowledgements).toEqual([{ runId: "bkr_live", status: "failed" }]); + }); + it("delivers failure references when policy and destination lookup are unavailable", async () => { const pg = wireRun(); h.policy = null; diff --git a/apps/api/test/modules/backups/custom-command-config.test.ts b/apps/api/test/modules/backups/custom-command-config.test.ts index c3978c506..868e91003 100644 --- a/apps/api/test/modules/backups/custom-command-config.test.ts +++ b/apps/api/test/modules/backups/custom-command-config.test.ts @@ -21,7 +21,7 @@ import { describe, expect, it, beforeEach, vi } from "vitest"; import { PassThrough, Readable } from "node:stream"; import { createHash } from "node:crypto"; -import { buildMailBackupPayload } from "../../../src/modules/mail/admin/backup-plan"; +import { buildMailBackupPayload } from "@repo/platform/engine/modules/mail/backup-plan"; const h = vi.hoisted(() => ({ /** payloadConfig the policy under test carries. */ @@ -142,6 +142,15 @@ vi.mock("@repo/platform/engine/lib/deployment-runtime", () => ({ resolveDeploymentPlatform: async () => ({ platform: { runtime: { name: "bare" } } }), })); vi.mock("@repo/platform/engine/lib/encryption", () => ({ decryptEnvMap: (v: unknown) => v })); +// This suite verifies the generic producer/manifest boundary. The mail lifecycle +// has its own real-host round trip; keep its private staging out of this fixture. +vi.mock("@repo/platform/engine/modules/mail/backup-lifecycle", () => ({ + prepareMailBackup: async () => ({ + payload: { payloadConfig: h.payloadConfig }, + metadata: { version: 2, domain: "mail.example.com", messageData: true, keys: true }, + cleanup: async () => {}, + }), +})); vi.mock("@repo/platform/engine/lib/notification-dispatcher", () => ({ notification: { emit: () => {} }, })); diff --git a/apps/api/test/modules/billing/billing-sdk-parity.test.ts b/apps/api/test/modules/billing/billing-sdk-parity.test.ts index 4dea70ffd..e7de42cd1 100644 --- a/apps/api/test/modules/billing/billing-sdk-parity.test.ts +++ b/apps/api/test/modules/billing/billing-sdk-parity.test.ts @@ -1,4 +1,5 @@ import { savedOffer as subscriptionOffer, savedMetadata as subscriptionMetadata } from "../../helpers/saved-cloud-offer"; +import { providerBillingFixture } from "../../helpers/oblien-vm-billing"; import capacityCatalog from "../../fixtures/oblien-capacity-catalog.json"; import { monthlyCloudBilling } from "../../helpers/monthly-cloud-offer"; import managedTransfer from "../../fixtures/oblien-managed-transfer.json"; @@ -60,7 +61,7 @@ vi.mock("@repo/platform/engine/lib/oblien-client", () => ({ }), }), getOblienClient: () => ({ - workspaces: { getQuota: provider.quota, get: provider.workspace }, + vms: { getQuota: provider.quota, get: provider.workspace }, namespaces: { ensure: provider.namespaces, get: provider.resourceRead, @@ -192,8 +193,8 @@ describe("billing through the same SDK and HTTP application operations", () => { // Exercise the actual SDK/validator before the shared application operation; // a mock entitlement alone missed the provider's newly included transfer. const billing = new OblienBillingApi({ clientId: "test-id", clientSecret: "test-secret", - fetch: (async url => Response.json(new URL(String(url)).pathname === "/billing/balance" - ? monthly.balance : monthly.entitlement)) as typeof fetch }); + fetch: (async url => Response.json(providerBillingFixture(new URL(String(url)).pathname === "/v1/billing/balance" + ? monthly.balance : monthly.entitlement))) as typeof fetch }); provider.subscriptions.set(selected.namespace!, monthly.subscription); provider.entitlement.mockImplementation(async namespace => namespace === selected.namespace ? billing.getEntitlement(namespace) : { success: true, namespace, tierId: null, status: "credit_exhausted", @@ -267,13 +268,13 @@ describe("billing through the same SDK and HTTP application operations", () => { data: { slug, effective_resource_limits: { - max_workspaces: 12, + max_vms: 12, max_total_vcpus: 8, max_total_ram_mb: 16384, max_total_disk_gb: 256, }, allocated_resource_usage: { - workspaces: 0, + vms: 0, vcpus: 0, ram_mb: 0, disk_gb: 0, diff --git a/apps/api/test/modules/billing/credit-alert.test.ts b/apps/api/test/modules/billing/credit-alert.test.ts index d02ca881d..437b90dae 100644 --- a/apps/api/test/modules/billing/credit-alert.test.ts +++ b/apps/api/test/modules/billing/credit-alert.test.ts @@ -23,13 +23,19 @@ const input = { eventType: "namespace.quota.threshold", eventId: "evt1", timestamp: "2026-09-28T00:00:00Z", - data: { namespace: "tenant", service: "workspace_vm", threshold: 95, alert: { limit: 4000 } }, + data: { namespace: "tenant", service: "vm_compute", threshold: 95, alert: { limit: 4000 } }, organizationId: "org-a", dashboardUrl: "https://app.openship.io", entitlement: state(), }; describe("provider credit alerts", () => { + it("accepts a pre-cutover delivery without changing its idempotency key", () => { + const result = creditAlertNotification({ + ...input, data: { ...input.data, service: "workspace_vm" }, + }); + expect(result).toEqual(creditAlertNotification(input)); + }); it("uses current provider numbers and an organization-bound billing link", () => { const result = creditAlertNotification(input)!; expect(result.payload.message).toContain("95%"); diff --git a/apps/api/test/modules/cloud-execution-access.test.ts b/apps/api/test/modules/cloud-execution-access.test.ts index 3402dfe1e..1812cf15b 100644 --- a/apps/api/test/modules/cloud-execution-access.test.ts +++ b/apps/api/test/modules/cloud-execution-access.test.ts @@ -120,7 +120,7 @@ describe("Cloud relays cannot substitute Cloud feature access for host administr expect(await response.json()).toMatchObject({ data: { token: "provider-secret" } }); }); - it.each(["owner", "admin", "member"])( + it.each(["owner", "admin"])( "preserves existing full host access for the %s role", async (role) => { await db.update(schema.member).set({ role }).where(eq(schema.member.id, memberId)); @@ -129,6 +129,17 @@ describe("Cloud relays cannot substitute Cloud feature access for host administr }, ); + it("denies members host credentials through both Cloud relay selectors", async () => { + await db.update(schema.member).set({ role: "member" }).where(eq(schema.member.id, memberId)); + for (const path of ["token", `token?serverId=${serverId}`, "resource-proxy", `resource-proxy?serverId=${serverId}`]) { + const response = await post(path, { operation: "list" }); + expect(response.status, await response.clone().text()).toBe(404); + } + expect(h.token).not.toHaveBeenCalled(); + expect(h.namespace).not.toHaveBeenCalled(); + expect(h.pages).not.toHaveBeenCalled(); + }); + it("does not bootstrap a server or credentials for an empty organization without host authority", async () => { actor = await seedOwner(); const membership = (await repos.member.find(actor.orgId, actor.userId))!; @@ -211,7 +222,7 @@ describe("Cloud relays cannot substitute Cloud feature access for host administr it.each([ ["preflight", {}], - ["pages", { workspace_id: "vm-own", path: "/private", name: "export", slug: "export" }], + ["pages", { vm_id: "vm-own", path: "/private", name: "export", slug: "export" }], ["pages/enable", { slug: "own-page" }], ["pages/disable", { slug: "own-page" }], ["pages/delete", { slug: "own-page" }], diff --git a/apps/api/test/modules/cloud-workspaces/lifecycle.integration.test.ts b/apps/api/test/modules/cloud-workspaces/lifecycle.integration.test.ts index 44dfb01c6..34e133682 100644 --- a/apps/api/test/modules/cloud-workspaces/lifecycle.integration.test.ts +++ b/apps/api/test/modules/cloud-workspaces/lifecycle.integration.test.ts @@ -218,7 +218,7 @@ beforeEach(async () => { expiresAt: "2030-01-01T00:00:00Z", })), }, - workspaces: { + vms: { create: vi.fn(async (input: any) => { const existing = [...vms.values()].find((vm) => vm.slug === input.slug); if (existing) return existing; @@ -242,7 +242,7 @@ beforeEach(async () => { return vm; }), list: vi.fn(async () => ({ - workspaces: [...vms.values()], + vms: [...vms.values()], total: vms.size, page: 1, limit: 100, @@ -260,9 +260,9 @@ beforeEach(async () => { vms.delete(id); return { success: true }; }), - workspace: (id: string) => ({ + vm: (id: string) => ({ id, - get: () => h.client.workspaces.get(id), + get: () => h.client.vms.get(id), lifecycle: { makePermanent: async () => {} }, resources: { update: (input: any) => h.client.resize(id, input) }, delete: () => h.client.delete(id), @@ -433,7 +433,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { expect((await request("POST", `/${workspace.id}/ensure`, undefined, stranger)).status).toBe( 404, ); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); const invalid = await request("POST", "", { name: "Broken", mode: "shared", @@ -468,7 +468,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { }); expect(observed.isError).toBe(false); expect(JSON.parse(observed.content[0].text).managed.operation.status).toBe("succeeded"); - expect(h.client.workspaces.create).toHaveBeenCalledTimes(1); + expect(h.client.vms.create).toHaveBeenCalledTimes(1); const outsider = await seedOwner(); expect( @@ -492,7 +492,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { }) ).isError, ).toBe(true); - expect(h.client.workspaces.create).toHaveBeenCalledTimes(1); + expect(h.client.vms.create).toHaveBeenCalledTimes(1); }); it("provisions on paid reconciliation and reuses one host across simultaneous project deployments", async () => { await syncOblienEntitlement(owner.orgId, { workspaceId: workspace.id }); @@ -512,7 +512,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { ); expect(targets[0]?.workspaceId).toBe(targets[1]?.workspaceId); expect(targets.map((target) => target.ownerWorkspaceId)).toEqual([workspace.id, workspace.id]); - expect(h.client.workspaces.create).toHaveBeenCalledTimes(1); + expect(h.client.vms.create).toHaveBeenCalledTimes(1); expect(h.client.resize).not.toHaveBeenCalled(); expect(vms.get(targets[0]!.workspaceId).resources).toMatchObject({ cpus: 1, @@ -544,7 +544,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { await expect( resolveSnapshotTarget(saved, { serverId: otherServer.id, deployTarget: "server" }), ).rejects.toMatchObject({ code: "CLOUD_WORKSPACE_TARGET_CONFLICT" }); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("places bare and Docker projects on managed servers without provisioning a second host", async () => { const sdk = await nativeShip(); @@ -557,7 +557,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { const docker = await sdk.projects.create({ name: "Docker app", serverId: selected.serverId }); for (const project of [bare, docker]) expect(await repos.project.findById(project.id)).toMatchObject({ serverId: selected.serverId, workspaceId: selected.id }); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("installs and previews catalog apps on the selected managed server without creating a project VM", async () => { subscribe("starter"); @@ -583,7 +583,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { await expect( stranger.apps.install({ templateId: "redis", serverId: server.id }), ).rejects.toMatchObject({ statusCode: 404 }); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("carries one managed server through source staging, scan and project creation", async () => { const sdk = await nativeShip(); @@ -614,15 +614,15 @@ describe("subscription-owned Cloud workspace lifecycle", () => { await expect(stranger.sources.scan(staged.sessionId)).rejects.toMatchObject({ statusCode: 404, }); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); } finally { const session = deleteFolderSession(staged.sessionId); if (session?.stagingDir) await rm(session.stagingDir, { recursive: true, force: true }); } }); it("recovers a lost provider create response with the same request and disk", async () => { - const create = h.client.workspaces.create.getMockImplementation()!; - h.client.workspaces.create.mockImplementationOnce(async (input: any) => { + const create = h.client.vms.create.getMockImplementation()!; + h.client.vms.create.mockImplementationOnce(async (input: any) => { await create(input); throw new Error("create response lost"); }); @@ -642,7 +642,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { ); expect(vms.size).toBe(1); expect( - new Set(h.client.workspaces.create.mock.calls.map(([input]: any[]) => input.idempotency_key)) + new Set(h.client.vms.create.mock.calls.map(([input]: any[]) => input.idempotency_key)) .size, ).toBe(1); expect( @@ -651,7 +651,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { ).toBe([...vms.keys()][0]); }); it("stops after three provisioning failures and durably escalates once without retrying the host", async () => { - h.client.workspaces.create.mockRejectedValue(new AppError("Hosts are temporarily at capacity", 503, "CLOUD_CAPACITY_UNAVAILABLE")); + h.client.vms.create.mockRejectedValue(new AppError("Hosts are temporarily at capacity", 503, "CLOUD_CAPACITY_UNAVAILABLE")); expect((await request("POST", `/${workspace.id}/ensure`)).status).toBe(202); await drainBackgroundWork(); const createTicket = vi.spyOn(repos.cloudSupport, "create").mockRejectedValueOnce(new Error("support database temporarily unavailable")); @@ -673,7 +673,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { expect(await repos.cloudSupport.find(ticketId)).toMatchObject({ status: "open", ownerUserId: owner.userId }); expect(await repos.cloudSupport.messages(ticketId)).toHaveLength(2); await processWorkspaceOperation(workspace.id); - expect(h.client.workspaces.create).toHaveBeenCalledTimes(3); + expect(h.client.vms.create).toHaveBeenCalledTimes(3); expect(await repos.cloudSupport.messages(ticketId)).toHaveLength(2); expect((await repos.cloudWorkspace.listPendingOperations()).map(row => row.id)).not.toContain(workspace.id); createTicket.mockRestore(); @@ -707,7 +707,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { }, }); expect((await deliver("bad-signature")).status).toBe(401); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); expect(await db.select().from(schema.cloudCustomerEmail).where(eq(schema.cloudCustomerEmail.organizationId, owner.orgId))).toEqual([]); h.billing.getCheckout.mockImplementation(async () => ({ checkout: { @@ -731,7 +731,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { .map(mail => mail.kind).sort()).toEqual(["invoice", "purchase"]); expect(result[2]).toMatchObject({ fulfilled: true, creditsGranted: 0 }); await drainBackgroundWork(); - expect(h.client.workspaces.create).toHaveBeenCalledTimes(1); + expect(h.client.vms.create).toHaveBeenCalledTimes(1); expect((await repos.cloudWorkspace.findById(workspace.id))?.operation?.status).toBe( "succeeded", ); @@ -764,7 +764,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { await expect(client.resize(server.id, { revision: "a".repeat(64), confirmRestart: true, idempotencyKey: randomUUID() })) .rejects.toMatchObject({ code: "TOKEN_READ_ONLY" }); } - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("recovers one draft and its server across concurrent native/HTTP creates and scopes retries by organization", async () => { @@ -779,7 +779,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { const stranger = await seedOwner(); const other = await (await clients(stranger))[0]!.createManaged(input); expect(other.id).not.toBe(created[0]!.id); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("never recreates a deleted purchase identity or its old provider namespace", async () => { @@ -791,14 +791,14 @@ describe("subscription-owned Cloud workspace lifecycle", () => { await expect(native!.createManaged(input)).rejects.toMatchObject({ code: "CLOUD_WORKSPACE_PURCHASE_REMOVED" }); await expect(remote!.createManaged(input)).rejects.toMatchObject({ code: "CLOUD_WORKSPACE_PURCHASE_REMOVED" }); expect(await repos.cloudWorkspace.findById(created.id)).toBeUndefined(); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("does not provision, stop or change the entitlement mirror when provider billing is unavailable", async () => { await syncOblienEntitlement(owner.orgId, { workspaceId: workspace.id }); h.billing.getEntitlement.mockRejectedValue(new Error("Billing temporarily unavailable")); const response = await request("POST", `/${workspace.id}/ensure`); expect(response.status).toBe(500); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); expect(h.client.delete).not.toHaveBeenCalled(); expect((await repos.cloudWorkspace.findById(workspace.id))?.subscriptionStatus).toBe("active"); }); @@ -917,7 +917,7 @@ describe("subscription-owned Cloud workspace lifecycle", () => { expect((await request("POST", `/${workspace.id}/ensure`)).body.code).toBe( "CLOUD_SERVER_LINK_REQUIRED", ); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); }); @@ -1757,7 +1757,7 @@ describe("managed server free-domain scope", () => { expect((await repos.domain.listByProject(id)).map(domain => domain.hostname)).toContain(`routed-${entry}.opsh.io`); expect(h.billing.getEntitlement).toHaveBeenCalled(); expect(h.billing.getEntitlement.mock.calls.every(([namespace]: [string]) => namespace === workspace.namespace)).toBe(true); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("uses the selected metered server's entitlement and allowance", async () => { subscribe("starter"); @@ -1780,7 +1780,7 @@ describe("managed server free-domain scope", () => { expect((await repos.service.listByProject(project.id))[0]?.domain).toBe("metered-service"); expect(h.billing.getEntitlement).toHaveBeenCalled(); expect(h.billing.getEntitlement.mock.calls.every(([namespace]: [string]) => namespace === workspace.namespace)).toBe(true); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it.each(["ensure", "update"] as const)("refuses excess free domains before %s changes an existing project's fields", async entry => { const sdk = await nativeShip(); @@ -1823,7 +1823,7 @@ describe("managed server free-domain scope", () => { envVars: {}, })).rejects.toMatchObject({ code: "PLAN_UPGRADE_REQUIRED", reason: "free-subdomain-limit" }); expect((await repos.deployment.listByProject(project.id)).rows).toEqual([]); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("does not count dormant routes outside an exclusive service deployment", async () => { const project = await addProject("Exclusive routes"); @@ -1839,7 +1839,7 @@ describe("managed server free-domain scope", () => { envVars: {}, serviceIds: [selected.id], strictServiceScope: true, }); expect(queued).toMatchObject({ status: "queued" }); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("keeps existing free routes editable when the server's allowance is full", async () => { const sdk = await nativeShip(); @@ -1875,7 +1875,7 @@ describe("managed server free-domain scope", () => { expect(await repos.service.findById(service.id)).toMatchObject({ domain: "routed-console-updated" }); expect(h.billing.getEntitlement).toHaveBeenCalled(); expect(h.billing.getEntitlement.mock.calls.every(([namespace]: [string]) => namespace === workspace.namespace)).toBe(true); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); it("does not use a paid sibling's allowance or write a project for an unauthorized server", async () => { const sdk = await nativeShip(); @@ -1890,6 +1890,6 @@ describe("managed server free-domain scope", () => { await expect(sdk.projects.create({ ...input, serverId: foreignServer.id })).rejects.toMatchObject({ statusCode: 404 }); expect((await repos.projectGroup.listByOrganization(owner.orgId, { page: 1, perPage: 1 })).total).toBe(0); expect(h.billing.getEntitlement).not.toHaveBeenCalled(); - expect(h.client.workspaces.create).not.toHaveBeenCalled(); + expect(h.client.vms.create).not.toHaveBeenCalled(); }); }); diff --git a/apps/api/test/modules/customer-email/signup.integration.test.ts b/apps/api/test/modules/customer-email/signup.integration.test.ts index b5dddde33..c18513ec7 100644 --- a/apps/api/test/modules/customer-email/signup.integration.test.ts +++ b/apps/api/test/modules/customer-email/signup.integration.test.ts @@ -52,6 +52,9 @@ it("welcomes a real SaaS email signup only after the real OTP verification flow" password: "valid-test-password-1234!", }); expect(signup.status, await signup.clone().text()).toBe(200); + expect(h.send).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ to: email })); + const initialMail = h.send.mock.calls.at(-1)![0] as { subject: string }; + const initialCode = initialMail.subject.match(/\b\d{6}\b/)![0]; const [user] = await db.select().from(schema.user).where(eq(schema.user.email, email)); expect(user!.emailVerified).toBe(false); expect( @@ -63,14 +66,7 @@ it("welcomes a real SaaS email signup only after the real OTP verification flow" h.send.mockClear(); expect(await service().flush()).toMatchObject({ sent: 0 }); expect(h.send).not.toHaveBeenCalled(); - const otp = await request("/email-otp/send-verification-otp", { - email, - type: "email-verification", - }); - expect(otp.status, await otp.clone().text()).toBe(200); - const mail = h.send.mock.calls.at(-1)![0] as { subject: string }; - const code = mail.subject.match(/\b\d{6}\b/)![0]; - const verified = await request("/email-otp/verify-email", { email, otp: code }); + const verified = await request("/email-otp/verify-email", { email, otp: initialCode }); expect(verified.status, await verified.clone().text()).toBe(200); h.send.mockClear(); await service().flush(); @@ -170,3 +166,100 @@ it("welcomes a verified Google signup through the same outbox without repeating fetch.mockRestore(); } }, 30_000); + +it("keeps one OAuth account on repeat signup and privately explains how to sign in", async () => { + const email = `existing-google-${randomUUID()}@example.com`; + const id = randomUUID(); + await db + .insert(schema.user) + .values({ id, email, name: "Existing Google customer", emailVerified: true }); + await db + .insert(schema.account) + .values({ id: randomUUID(), accountId: randomUUID(), providerId: "google", userId: id }); + h.send.mockClear(); + for (const attempt of [1, 2]) { + const signup = await request("/sign-up/email", { + name: "Signup retry", + email, + password: `attempt-${attempt}-not-an-account-password!`, + }); + expect(signup.status, await signup.clone().text()).toBe(200); + expect((await signup.json()).token).toBeNull(); + } + expect(await db.select().from(schema.user).where(eq(schema.user.email, email))).toHaveLength(1); + expect(await db.select().from(schema.account).where(eq(schema.account.userId, id))).toMatchObject( + [{ providerId: "google", password: null }], + ); + expect(h.send).toHaveBeenCalledWith( + expect.objectContaining({ to: email, subject: "Sign in to your OpenShip account" }), + ); + const notice = h.send.mock.calls.at(-1)![0] as { text: string }; + expect(notice.text).toContain("already have"); + expect(notice.text).toContain("Google"); + const login = await request("/sign-in/email", { + email, + password: "attempt-2-not-an-account-password!", + }); + expect(login.status).toBe(401); + // A provider-only customer can add a password only after proving mailbox + // ownership through the existing recovery flow, never from duplicate signup. + h.send.mockClear(); + const reset = await request("/email-otp/request-password-reset", { email }); + expect(reset.status, await reset.clone().text()).toBe(200); + const resetMail = h.send.mock.calls.at(-1)![0] as { subject: string }; + const otp = resetMail.subject.match(/\b\d{6}\b/)![0]; + const password = "verified-mailbox-owner-new-password-1234!"; + const changed = await request("/email-otp/reset-password", { email, otp, password }); + expect(changed.status, await changed.clone().text()).toBe(200); + expect((await request("/sign-in/email", { email, password })).status).toBe(200); + expect(await db.select().from(schema.account).where(eq(schema.account.userId, id))).toHaveLength( + 2, + ); +}, 30_000); + +it("resends verification on an unverified signup retry without replacing its password", async () => { + const email = `retry-signup-${randomUUID()}@example.com`; + const password = "first-signup-password-1234!"; + const first = await request("/sign-up/email", { email, name: "Signup customer", password }); + expect(first.status).toBe(200); + h.send.mockClear(); + const retry = await request("/sign-up/email", { + email, + name: "Second attempt", + password: "different-retry-password-1234!", + }); + expect(retry.status).toBe(200); + expect(h.send).toHaveBeenCalledWith(expect.objectContaining({ to: email })); + const mail = h.send.mock.calls.at(-1)![0] as { subject: string }; + const otp = mail.subject.match(/\b\d{6}\b/)![0]; + const verified = await request("/email-otp/verify-email", { email, otp }); + expect(verified.status, await verified.clone().text()).toBe(200); + expect((await request("/sign-in/email", { email, password })).status).toBe(200); + expect( + (await request("/sign-in/email", { email, password: "different-retry-password-1234!" })).status, + ).toBe(401); + expect(await db.select().from(schema.user).where(eq(schema.user.email, email))).toHaveLength(1); +}, 30_000); + +it("recovers an initial delivery failure by retrying signup with the same account", async () => { + const email = `smtp-retry-${randomUUID()}@example.com`; + const password = "original-signup-password-1234!"; + h.send.mockClear(); + h.send.mockResolvedValueOnce(false); + const first = await request("/sign-up/email", { email, name: "SMTP retry", password }); + // Better Auth logs background delivery failures while keeping its generic response. + expect(first.status).toBe(200); + h.send.mockClear(); + const retry = await request("/sign-up/email", { + email, + name: "SMTP retry", + password: "a-different-retry-password-1234!", + }); + expect(retry.status, await retry.clone().text()).toBe(200); + expect(h.send).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ to: email })); + const mail = h.send.mock.calls.at(-1)![0] as { subject: string }; + const otp = mail.subject.match(/\b\d{6}\b/)![0]; + expect((await request("/email-otp/verify-email", { email, otp })).status).toBe(200); + expect((await request("/sign-in/email", { email, password })).status).toBe(200); + expect(await db.select().from(schema.user).where(eq(schema.user.email, email))).toHaveLength(1); +}, 30_000); diff --git a/apps/api/test/modules/deployments/build-duration.test.ts b/apps/api/test/modules/deployments/build-duration.test.ts index 026f4db36..8d1e2cecb 100644 --- a/apps/api/test/modules/deployments/build-duration.test.ts +++ b/apps/api/test/modules/deployments/build-duration.test.ts @@ -13,7 +13,6 @@ vi.mock("@repo/platform/engine/lib/notification-dispatcher", () => ({ notification: { emit: vi.fn() }, })); vi.mock("@repo/platform/engine/lib/audit-emitter", () => ({ audit: { recordAsync: vi.fn() } })); -vi.mock("@repo/platform/engine/lib/favicon-detector", () => ({ detectAndStoreFavicon: vi.fn() })); vi.mock("@repo/platform/engine/modules/mail/webmail/webmail-install.service", () => ({ onWebmailDeployed: vi.fn(), })); diff --git a/apps/api/test/modules/deployments/compose-host-channel-notice.test.ts b/apps/api/test/modules/deployments/compose-host-channel-notice.test.ts index 58861d410..83322bdd5 100644 --- a/apps/api/test/modules/deployments/compose-host-channel-notice.test.ts +++ b/apps/api/test/modules/deployments/compose-host-channel-notice.test.ts @@ -250,7 +250,7 @@ it("deploys Cloud Compose endpoints, internal paths, generated files and contain const configurations: Array> = []; const publishRoute = vi.fn(async () => undefined); const pullImage = vi.fn(async () => undefined); - const resolveRoutingTarget = vi.fn(async (id: string, port: number) => ({ workspace: "shared-vm", + const resolveRoutingTarget = vi.fn(async (id: string, port: number) => ({ vm: "shared-vm", port: id === "web-container" ? (port === 8080 ? 30001 : 30002) : 30003 })); const runtime = Object.assign(Object.create(CloudDockerRuntime.prototype), { name: "docker", workspaceId: "shared-vm", projectId: "p1", unsupportedComposeKeys: new Set(), @@ -314,8 +314,8 @@ it("deploys Cloud Compose endpoints, internal paths, generated files and contain serviceId: "svc-api", allocatedResources: { containerId: "api-container", cpuCores: 2, memoryMb: 2048 }, })); expect(publishRoute).toHaveBeenCalledWith("app.opsh.io", 30001, false, expect.objectContaining({ routes: [ - { match: { path: "/api/", type: "prefix" }, action: { kind: "proxy", workspace: "shared-vm", port: 30003 } }, - { match: { path: "/", type: "prefix" }, action: { kind: "proxy", workspace: "shared-vm", port: 30001 } }, + { match: { path: "/api/", type: "prefix" }, action: { kind: "proxy", vm: "shared-vm", port: 30003 } }, + { match: { path: "/", type: "prefix" }, action: { kind: "proxy", vm: "shared-vm", port: 30001 } }, ] })); }); @@ -1599,7 +1599,8 @@ describe("compose deploy — host channel unavailable", () => { expect(base.deployServiceWorkload).not.toHaveBeenCalled(); }); - it("refuses ambiguous duplicate containers before repairing or activating anything", async () => { + it.each([null, "server-original"])("refuses ambiguous duplicate containers on %s before repairing or activating anything", async (serverId) => { + h.previousDeployment.meta = { serverId }; const base = startingRuntime(); const liveContainer = (id: string, name: string) => ({ id, @@ -1665,11 +1666,18 @@ describe("compose deploy — host channel unavailable", () => { { executor: {} as CommandExecutor, hostPortTarget: localHostPortTarget, + serverId: serverId ?? undefined, }, ), - ).rejects.toThrow( - /more than one container matching active service "api".*no service activation/i, - ); + ).rejects.toMatchObject({ + code: "SERVICE_CONTAINER_CONFLICT", + message: expect.stringMatching(/more than one container matching active service "api".*no service activation/i), + details: { + projectId: "p1", serviceId: "svc-api", serviceName: "api", + containerNames: ["openship-app-api-a", "openship-app-api-b"], + serverId, workspaceId: null, + }, + }); expect(h.updateServiceDeployment).not.toHaveBeenCalled(); expect(h.allocateAndReservePinnedHostPort).not.toHaveBeenCalled(); diff --git a/apps/api/test/modules/deployments/compose-noop-settle.test.ts b/apps/api/test/modules/deployments/compose-noop-settle.test.ts index 100da4d26..7a522fa77 100644 --- a/apps/api/test/modules/deployments/compose-noop-settle.test.ts +++ b/apps/api/test/modules/deployments/compose-noop-settle.test.ts @@ -94,7 +94,6 @@ vi.mock("@repo/platform/engine/lib/notification-dispatcher", () => ({ vi.mock("../../../src/lib/audit", () => ({ audit: { recordAsync: (_c: unknown, e: { eventType: string }) => h.audits.push(e.eventType) }, })); -vi.mock("@repo/platform/engine/lib/favicon-detector", () => ({ detectAndStoreFavicon: async () => {} })); vi.mock("@repo/platform/engine/modules/mail/webmail/webmail-install.service", () => ({ onWebmailDeployed: async () => {}, })); diff --git a/apps/api/test/modules/deployments/deploy-outcome-vs-logs.test.ts b/apps/api/test/modules/deployments/deploy-outcome-vs-logs.test.ts index e847bd792..52ffd3b75 100644 --- a/apps/api/test/modules/deployments/deploy-outcome-vs-logs.test.ts +++ b/apps/api/test/modules/deployments/deploy-outcome-vs-logs.test.ts @@ -113,7 +113,6 @@ vi.mock("@repo/platform/engine/lib/notification-dispatcher", () => ({ }, })); vi.mock("../../../src/lib/audit", () => ({ audit: { recordAsync: () => {} } })); -vi.mock("@repo/platform/engine/lib/favicon-detector", () => ({ detectAndStoreFavicon: async () => {} })); vi.mock("@repo/platform/engine/modules/mail/webmail/webmail-install.service", () => ({ onWebmailDeployed: async () => {}, })); diff --git a/apps/api/test/modules/deployments/deployment-meta-redaction.test.ts b/apps/api/test/modules/deployments/deployment-meta-redaction.test.ts index 3fd2f63b7..ee521b560 100644 --- a/apps/api/test/modules/deployments/deployment-meta-redaction.test.ts +++ b/apps/api/test/modules/deployments/deployment-meta-redaction.test.ts @@ -13,7 +13,6 @@ vi.mock("@repo/platform/engine/lib/notification-dispatcher", () => ({ notification: { emit: vi.fn() }, })); vi.mock("@repo/platform/engine/lib/audit-emitter", () => ({ audit: { recordAsync: vi.fn() } })); -vi.mock("@repo/platform/engine/lib/favicon-detector", () => ({ detectAndStoreFavicon: vi.fn() })); vi.mock("@repo/platform/engine/modules/mail/webmail/webmail-install.service", () => ({ onWebmailDeployed: vi.fn(), })); diff --git a/apps/api/test/modules/deployments/native-sdk-parity.test.ts b/apps/api/test/modules/deployments/native-sdk-parity.test.ts index a247e0c27..4b0e0ec0d 100644 --- a/apps/api/test/modules/deployments/native-sdk-parity.test.ts +++ b/apps/api/test/modules/deployments/native-sdk-parity.test.ts @@ -6,7 +6,7 @@ import { join } from "node:path"; import { createShip } from "@repo/sdk/native"; import { OpenshipClient } from "@repo/sdk/client"; import type { VerifiedIdentity } from "@repo/platform"; -import { ENV_MASK, type AppTemplate } from "@repo/core"; +import { ENV_MASK, normalizeEdgeResponse, type AppTemplate } from "@repo/core"; import type { BackupDestination as StoredBackupDestination } from "@repo/db"; import type { ProjectInfo } from "@repo/platform/engine/modules/deployments/prepare.service"; import { alice, storedDeployment } from "../../../../../packages/platform/test/fixtures"; @@ -1897,8 +1897,8 @@ describe("domain and DNS HTTP/native parity", () => { const local = await native(); h.members.set("org-a:alice", { id: "member-a", role: "member" }); for (const dns of [remote().dns, local.dns]) { - await expect(dns.addCredential(input)).rejects.toMatchObject({ code: "INSUFFICIENT_ROLE" }); - await expect(dns.removeCredential("dns-a")).rejects.toMatchObject({ code: "INSUFFICIENT_ROLE" }); + await expect(dns.addCredential(input)).rejects.toMatchObject({ code: "NOT_FOUND" }); + await expect(dns.removeCredential("dns-a")).rejects.toMatchObject({ code: "NOT_FOUND" }); } expect(h.dnsAdd).not.toHaveBeenCalled(); expect(h.dnsRemove).not.toHaveBeenCalled(); @@ -1910,6 +1910,9 @@ describe("domain and DNS HTTP/native parity", () => { }); describe("project routing HTTP/native parity", () => { + beforeEach(() => { + for (const id of ["project-a", "project-b", "project-sibling"]) h.projects.set(id, { ...h.projects.get(id)!, id }); + }); const remote = () => new OpenshipClient({ baseUrl: "http://openship.test", organizationId: "org-a", fetch: ((url, init) => app.request(url as string, init)) as typeof fetch }); const rule = { id: "rule-a", organizationId: "org-a", projectId: "project-a", domainId: "domain-a", pathPrefix: "/api", spec: { access: { methods: ["GET"] } }, enabled: true, createdAt: "2026-09-12T00:00:00.000Z", updatedAt: "2026-09-12T00:00:00.000Z" }; it("retains the sanitizer, project/domain checks, edge push, and one audit per facade", async () => { @@ -1936,6 +1939,41 @@ describe("project routing HTTP/native parity", () => { expect(h.ruleUpdate).not.toHaveBeenCalled(); for (const args of h.ruleRemove.mock.calls) expect(args).toEqual(["project-sibling", rule.id]); }); + it("validates domain responses in both facades and distinguishes persistence from edge application", async () => { + const local = await native(); + h.ruleCreate.mockImplementation(async data => ({ ...rule, ...data })); + const input = { domainId: "domain-a", pathPrefix: "/status", spec: { response: normalizeEdgeResponse({ body: "ready", headers: { "Cache-Control": "no-store" } }) } }; + for (const projects of [remote().projects, local.projects]) { + h.rulePush.mockResolvedValue(true); + expect(await projects.createRouteRule("project-a", input)).toMatchObject({ domainId: "domain-a", pathPrefix: "/status", edgeApplied: true, spec: { response: { match: "exact", status: 200, body: "ready", headers: { "cache-control": "no-store" } } } }); + h.rulePush.mockResolvedValue(false); + expect(await projects.createRouteRule("project-a", input)).toMatchObject({ edgeApplied: false }); + for (const invalid of [ + { ...input, domainId: null }, + { ...input, domainId: "domain-b" }, + { ...input, pathPrefix: "/_openship/auth" }, + { ...input, spec: { response: { ...input.spec.response, headers: { "X-Test": "ok\r\nInjected: yes" } } } }, + { ...input, spec: { ...input.spec, access: { methods: ["GET"] } } }, + ]) await expect(projects.createRouteRule("project-a", invalid)).rejects.toMatchObject({ statusCode: 400 }); + } + expect(h.ruleCreate).toHaveBeenCalledTimes(4); + }); + it("refuses unsupported edge targets and prevents changing a response to a foreign domain", async () => { + const local = await native(); + const input = { domainId: "domain-a", spec: { response: normalizeEdgeResponse({ body: "ready" }) } }; + for (const projects of [remote().projects, local.projects]) { + h.domains.set("domain-a", { projectId: "project-a", externalIngress: true }); + await expect(projects.createRouteRule("project-a", input)).rejects.toMatchObject({ code: "EDGE_RESPONSE_UNSUPPORTED" }); + h.domains.set("domain-a", { projectId: "project-a" }); + h.projects.set("project-a", { id: "project-a", organizationId: "org-a", workspaceId: "workspace" }); + await expect(projects.createRouteRule("project-a", input)).rejects.toMatchObject({ code: "EDGE_RESPONSE_UNSUPPORTED" }); + h.projects.set("project-a", { id: "project-a", organizationId: "org-a" }); + h.ruleGet.mockResolvedValue({ ...rule, spec: input.spec }); + await expect(projects.updateRouteRule("project-a", { ruleId: rule.id, domainId: "domain-b" })).rejects.toMatchObject({ code: "INVALID_DOMAIN" }); + } + expect(h.ruleCreate).not.toHaveBeenCalled(); + expect(h.ruleUpdate).not.toHaveBeenCalled(); + }); it("connects a primary domain through the same domain operation and retains legacy errors", async () => { h.domainCreate.mockResolvedValue({ domain: domainFixture(), records: { mode: "external", records: [] }, wwwError: "sibling is already owned" }); const local = await native(); @@ -2007,10 +2045,10 @@ describe("provider credential HTTP/native parity", () => { const local = await native(); h.members.set("org-a:alice", { id: "member-a", role: "member" }); for (const credentials of [local.credentials, remote().credentials]) { - await expect(credentials.create({ provider: "docker-registry", name: "Registry", values: { secret: "value" } })).rejects.toMatchObject({ code: "INSUFFICIENT_ROLE" }); - await expect(credentials.update("credential-a", { name: "Renamed" })).rejects.toMatchObject({ code: "INSUFFICIENT_ROLE" }); - await expect(credentials.remove("credential-a")).rejects.toMatchObject({ code: "INSUFFICIENT_ROLE" }); - await expect(credentials.verify("credential-a")).rejects.toMatchObject({ code: "INSUFFICIENT_ROLE" }); + await expect(credentials.create({ provider: "docker-registry", name: "Registry", values: { secret: "value" } })).rejects.toMatchObject({ code: "NOT_FOUND" }); + await expect(credentials.update("credential-a", { name: "Renamed" })).rejects.toMatchObject({ code: "NOT_FOUND" }); + await expect(credentials.remove("credential-a")).rejects.toMatchObject({ code: "NOT_FOUND" }); + await expect(credentials.verify("credential-a")).rejects.toMatchObject({ code: "NOT_FOUND" }); } expect(h.credentialCreate).not.toHaveBeenCalled(); expect(h.credentialUpdate).not.toHaveBeenCalled(); @@ -2454,6 +2492,20 @@ describe("server HTTP/native parity", () => { expect(h.serverRemove).not.toHaveBeenCalled(); }); + it("denies ordinary members host execution through native, SDK and raw HTTP operations", async () => { + h.members.set("org-a:alice", { id: "member-a", role: "member" }); + const local = await native(); + for (const servers of [local.servers, remote().servers]) { + await expect(servers.exec("server-a", { command: "whoami" })).rejects.toMatchObject({ code: "NOT_FOUND" }); + } + const response = await app.request("/api/system/servers/server-a/exec", { + method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ command: "whoami" }), + }); + expect(response.status).toBe(404); + expect(h.serverWithExecutor).not.toHaveBeenCalled(); + expect(h.audit.mock.calls.filter(([event]) => event.eventType === "server.exec")).toHaveLength(0); + }); + it("returns the same partial teardown result and keeps HTTP 409", async () => { h.serverWorkloads.mockResolvedValue([{ id: "project-a", name: "Application", appTemplateId: null }]); h.projectTeardown.mockResolvedValue({ rowDeleted: false, orphaned: [], unrecoverable: [{ error: "volume busy" }] }); @@ -2546,11 +2598,11 @@ describe("HTTP/native system parity", () => { for (const system of [local.system, remote().system]) { await expect(system.updateSettings({ productMode: "mail" })).rejects.toMatchObject({ statusCode: 403 }); await expect(system.updateEmailSettings({ host: null })).rejects.toMatchObject({ statusCode: 403 }); - await expect(system.resetSettings()).rejects.toMatchObject({ statusCode: 403 }); + await expect(system.resetSettings()).rejects.toMatchObject({ statusCode: role === "member" ? 404 : 403 }); await expect(system.health()).rejects.toMatchObject({ statusCode: 403 }); await expect(system.browse({ path: "/" })).rejects.toMatchObject({ statusCode: 403 }); await expect(system.listUntrackedEdgeSites()).rejects.toMatchObject({ statusCode: 403 }); - await expect(system.removeUntrackedEdgeSite({ hostname: "orphan.example.test" })).rejects.toMatchObject({ statusCode: 403 }); + await expect(system.removeUntrackedEdgeSite({ hostname: "orphan.example.test" })).rejects.toMatchObject({ statusCode: role === "member" ? 404 : 403 }); } } expect(h.settingsUpsert).not.toHaveBeenCalled(); diff --git a/apps/api/test/modules/deployments/resource-guidance.test.ts b/apps/api/test/modules/deployments/resource-guidance.test.ts index 3e0533151..68b7f89ee 100644 --- a/apps/api/test/modules/deployments/resource-guidance.test.ts +++ b/apps/api/test/modules/deployments/resource-guidance.test.ts @@ -237,3 +237,21 @@ it("retains resource guidance for a published partial rollout without turning it errorDetails: { resourceIssue: "storage-full" }, }); }); + + +it.each([true, false])("restores a container conflict on a Cloud=%s attempt without relabeling it as capacity", async (cloud) => { + const ctx = await attempt(cloud); + const details = { + projectId: ctx.project.id, + serviceId: "app-service", + containerNames: ["app-current", "app-duplicate"], + serverId: ctx.project.serverId ?? "ssh-original", + workspaceId: ctx.project.workspaceId, + }; + await onFailure(ctx, "More than one container matches the active service", undefined, { + errorCode: "SERVICE_CONTAINER_CONFLICT", errorDetails: details, + }); + expect(await getBuildSessionStatus(ctx.dep.id)).toMatchObject({ + status: "failed", errorCode: "SERVICE_CONTAINER_CONFLICT", errorDetails: details, + }); +}); diff --git a/apps/api/test/modules/deployments/reused-artifact-not-reclaimed.test.ts b/apps/api/test/modules/deployments/reused-artifact-not-reclaimed.test.ts index 5ab30154e..5d07a4e0f 100644 --- a/apps/api/test/modules/deployments/reused-artifact-not-reclaimed.test.ts +++ b/apps/api/test/modules/deployments/reused-artifact-not-reclaimed.test.ts @@ -32,9 +32,6 @@ vi.mock("@repo/platform/engine/lib/notification-dispatcher", () => ({ notification: { emit: vi.fn() }, })); vi.mock("../../../src/lib/audit", () => ({ audit: { recordAsync: vi.fn(), record: vi.fn() } })); -vi.mock("@repo/platform/engine/lib/favicon-detector", () => ({ - detectAndStoreFavicon: vi.fn(async () => {}), -})); vi.mock("@repo/platform/engine/modules/projects/cleanup-keep-set", () => ({ computeCleanupKeepSet: vi.fn(async () => ({ containers: new Set() })), })); diff --git a/apps/api/test/modules/dns/managed-ingress.test.ts b/apps/api/test/modules/dns/managed-ingress.test.ts index 9bc116e4c..9593efea9 100644 --- a/apps/api/test/modules/dns/managed-ingress.test.ts +++ b/apps/api/test/modules/dns/managed-ingress.test.ts @@ -94,6 +94,6 @@ describe("managed ingress through authenticated HTTP and the public SDK", () => headers, body: JSON.stringify({ credentialId: "no-secret" }), }); - expect(denied.status).toBe(403); + expect(denied.status).toBe(404); }); }); diff --git a/apps/api/test/modules/mail/backup-script.ts b/apps/api/test/modules/mail/backup-script.ts new file mode 100644 index 000000000..4a2132172 --- /dev/null +++ b/apps/api/test/modules/mail/backup-script.ts @@ -0,0 +1,11 @@ +import { execFileSync } from "node:child_process"; +import { expect } from "vitest"; + +/** Decode the one root shell argument without executing its contents. Tests + * inspect the actual script, independent of its outer shell-quoting layer. */ +export function backupScript(command: string): string { + expect(command).toMatch(/^sudo -n sh -c /); + return execFileSync("sh", ["-c", command.replace(/^sudo -n sh -c /, "printf '%s' ")], { + encoding: "utf8", + }); +} diff --git a/apps/api/test/modules/mail/mail-backup-flavor.test.ts b/apps/api/test/modules/mail/mail-backup-flavor.test.ts index 8635a38f2..961210557 100644 --- a/apps/api/test/modules/mail/mail-backup-flavor.test.ts +++ b/apps/api/test/modules/mail/mail-backup-flavor.test.ts @@ -1,81 +1,40 @@ -/** - * The backup shell has to be written for the topology it will run on (GH-563). - * - * The produce/restore commands are BAKED here and executed later by the generic - * custom_command producer over a bare SSH executor, which knows nothing about mail. So a - * containerized engine cannot be discovered at run time - the plan has to already say - * `docker exec`. It did not: it issued `sudo -u postgres pg_dump` and - * `chown -R vmail:vmail` on the host, where on a container box there is no `postgres` - * user, no `pg_dump`, and no `vmail`. The dump aborted the whole backup under `set -e`, - * and on restore the chown was masked by `|| true`, leaving maildirs root-owned and - * unreadable by Dovecot while the run reported success. - */ +import "./_setup-env"; import { describe, expect, it } from "vitest"; -import { buildMailBackupPayload } from "../../../src/modules/mail/admin/backup-plan"; +import { buildMailBackupPayload } from "@repo/platform/engine/modules/mail/backup-plan"; +import { MAIL_CONTAINER_MOUNTS } from "@repo/adapters"; +import { backupScript } from "./backup-script"; -const FLAGS = { messageData: true, keys: true }; - -describe("mail backup plan targets the right topology", () => { - it("[container] runs Postgres in the sidecar and chown in the engine", () => { - const { payloadConfig } = buildMailBackupPayload("example.com", FLAGS, "container"); - const { produceCommand: produce, restoreCommand: restore } = payloadConfig; - - // Nothing may assume a host-side postgres or vmail user. - expect(produce).not.toContain("sudo -u postgres"); - expect(restore).not.toContain("sudo -u postgres"); - expect(produce).toContain("docker exec openship-mail-db pg_dump -U postgres"); - - // The dump file is staged in the producer's $tmp ON THE HOST, which the sidecar - // cannot see - so the replay must arrive over stdin, and docker exec needs -i. +const flags = { messageData: true, keys: true }; +describe("mail archive topology", () => { + it("runs database tools in the sidecar and repairs mail ownership in the engine", () => { + const plan = buildMailBackupPayload("example.test", flags, "container").payloadConfig; + const capture = backupScript(plan.produceCommand); + const restore = backupScript(plan.restoreCommand); + expect(capture).toContain("docker exec openship-mail-db pg_dump -U postgres"); expect(restore).toContain("docker exec -i openship-mail-db psql -U postgres"); - expect(restore).not.toMatch(/psql[^\n]*-f "\$tmp/); - - // Ownership must be applied where the vmail user exists: the ENGINE, not the sidecar, - // and not the host. - expect(restore).toContain("docker exec openship-mail chown -R vmail:vmail /var/vmail/vmail1"); - // ...and a failure there must not be swallowed - root-owned maildirs are unreadable. - expect(restore).not.toMatch(/chown -R vmail:vmail [^\n]*\|\| true/); - - // Daemons re-read via supervisord in the container; systemctl does not exist there. - expect(restore).toContain("supervisorctl restart postfix dovecot amavis"); - expect(restore).not.toContain("systemctl reload"); + expect(restore).toContain("--single-transaction"); + expect(restore).toContain("docker exec openship-mail chown -R -H vmail:vmail /var/vmail"); + expect(restore).not.toContain("sudo -u postgres"); + expect(restore).not.toMatch(/chown[^\n]*\|\| true/); }); - - it("[host] keeps the legacy sudo/systemctl form", () => { - const { payloadConfig } = buildMailBackupPayload("example.com", FLAGS, "host"); - const { produceCommand: produce, restoreCommand: restore } = payloadConfig; - - expect(produce).toContain("sudo -u postgres pg_dump -d vmail"); - expect(restore).toContain("sudo -u postgres psql"); - expect(restore).toContain("chown -R vmail:vmail /var/vmail/vmail1"); - expect(restore).toContain("systemctl reload postfix dovecot"); - expect(produce).not.toContain("docker exec"); - expect(restore).not.toContain("docker exec"); + it("uses native database tools on bare hosts", () => { + const plan = buildMailBackupPayload("example.test", flags, "host").payloadConfig; + expect(backupScript(plan.produceCommand)).toContain("sudo -u postgres pg_dump"); + expect(backupScript(plan.restoreCommand)).toContain("sudo -u postgres psql"); + expect(backupScript(plan.restoreCommand)).not.toContain("docker exec"); }); - - it("proves sudo works before relying on it to tell absent from unreadable", () => { - // The `sudo -n test`/`sudo -n cp` reads below only distinguish those two cases if sudo - // is known to work. That used to be guaranteed by `sudo -u postgres pg_dump` running - // first under `set -e`; routing the dump through the sidecar removed the guarantee on - // container boxes, so the probe has to be explicit - otherwise a failing `test` reads - // as absence and the archive is stamped `keys: true` with no keys in it. - for (const flavor of ["container", "host"] as const) { - const { payloadConfig } = buildMailBackupPayload("example.com", FLAGS, flavor); - const produce = payloadConfig.produceCommand; - expect(produce).toContain("sudo -n true"); - expect(produce.indexOf("sudo -n true")).toBeLessThan(produce.indexOf("sudo -n test -d /var/lib/dkim")); + it("captures complete mail roots through the installation's canonical mount map", () => { + const plan = buildMailBackupPayload("example.test", flags, "container").payloadConfig; + const capture = backupScript(plan.produceCommand); + const restore = backupScript(plan.restoreCommand); + for (const path of ["/var/vmail", "/var/lib/dkim"]) { + const mount = MAIL_CONTAINER_MOUNTS.find((entry) => entry.container === path)!; + expect(mount).toBeDefined(); + // tar addresses a root through -C parent + basename; restore uses its full path. + expect(restore).toContain(mount.host); } - }); - - it("keeps the maildir tar on the host, where the bind mount is", () => { - // /var/vmail is a bind mount on container setups (${MAIL_HOST_STATE_DIR}/vmail) and - // a direct path on host setups. - const { payloadConfig: container } = buildMailBackupPayload("example.com", FLAGS, "container"); - expect(container.produceCommand).toContain('sudo -n tar -c -C "$tmp" . -C /var/lib/openship/mail/vmail vmail1'); - expect(container.restoreCommand).toContain('if [ -d "$tmp/vmail1" ]; then cp -a "$tmp/vmail1" /var/lib/openship/mail/vmail/'); - - const { payloadConfig: host } = buildMailBackupPayload("example.com", FLAGS, "host"); - expect(host.produceCommand).toContain('sudo -n tar -c -C "$tmp" . -C /var/vmail vmail1'); - expect(host.restoreCommand).toContain('if [ -d "$tmp/vmail1" ]; then cp -a "$tmp/vmail1" /var/vmail/'); + expect(capture).not.toContain("vmail1"); // Other stores/sieve/quarantine must survive too. + expect(capture).toContain("--clean --if-exists --no-owner"); + expect(capture).not.toContain("--table="); }); }); diff --git a/apps/api/test/modules/mail/mail-backup-lifecycle.test.ts b/apps/api/test/modules/mail/mail-backup-lifecycle.test.ts new file mode 100644 index 000000000..e538ca33d --- /dev/null +++ b/apps/api/test/modules/mail/mail-backup-lifecycle.test.ts @@ -0,0 +1,151 @@ +import "./_setup-env"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +const mocks = vi.hoisted(() => ({ + writeFile: vi.fn(), + readState: vi.fn(), + writeState: vi.fn(), + readFile: vi.fn(), + removeFile: vi.fn(), + target: vi.fn(), + installed: vi.fn(), + pause: vi.fn(), + resume: vi.fn(), + dkim: vi.fn(), + dns: vi.fn(), + rows: vi.fn(), + relay: vi.fn(), + disableRelay: vi.fn(), +})); +vi.mock("@repo/db", async (original) => ({ + ...(await original()), + repos: { mailServer: { get: mocks.target, markInstalled: mocks.installed } }, +})); +vi.mock("@repo/platform/engine/lib/openship-server-store", () => ({ + OPENSHIP_DIR: "/root/.openship", + HOST_STATE_DIR: "/root/.openship", + readOpenshipFile: mocks.readFile, + removeOpenshipFile: mocks.removeFile, + writeOpenshipFile: mocks.writeFile, +})); +vi.mock("@repo/platform/engine/modules/mail/mail-state", async (original) => ({ + ...(await original()), + readState: mocks.readState, + writeState: mocks.writeState, +})); +vi.mock("@repo/platform/engine/modules/mail/mail-engine", async (original) => ({ + ...(await original()), + resolveMailFlavor: async () => "host", + pauseMailDaemons: mocks.pause, +})); +vi.mock("@repo/platform/engine/modules/mail/mail.service", () => ({ + provisionDomainDkim: mocks.dkim, + stepDkimKeys: mocks.dns, +})); +vi.mock("@repo/platform/engine/modules/mail/admin/psql-runner", () => ({ queryRows: mocks.rows })); +vi.mock("@repo/platform/engine/modules/mail/admin/domain-dns.service", () => ({ + buildDomainDnsRecords: (_primary: string, domain: string, dkim: string) => ({ + mx: { value: domain }, + dkim: { value: dkim }, + }), +})); +vi.mock("@repo/platform/engine/modules/mail/admin/outbound-relay.service", () => ({ + configureOutboundRelay: mocks.relay, + disableOutboundRelay: mocks.disableRelay, +})); +import { BareRuntime, LocalExecutor } from "@repo/adapters"; +import { makeFreshState } from "@repo/platform/engine/modules/mail/mail-state"; +import { exportMailState } from "@repo/platform/engine/modules/mail/backup-state"; +import { + prepareMailRestore, + prepareMailBackup, + readMailBackupMetadata, +} from "@repo/platform/engine/modules/mail/backup-lifecycle"; +import { backupScript } from "./backup-script"; + +const metadata = { version: 2 as const, domain: "example.test", messageData: true, keys: true }; +const runtime = () => new BareRuntime({ executor: new LocalExecutor() }); +beforeEach(() => { + vi.clearAllMocks(); + mocks.target.mockResolvedValue({ domain: metadata.domain }); + mocks.readState.mockResolvedValue(makeFreshState("target", metadata.domain)); + mocks.readFile.mockResolvedValue( + JSON.stringify(exportMailState(makeFreshState("source", metadata.domain))), + ); + mocks.pause.mockResolvedValue({ resume: mocks.resume, stopped: true }); + mocks.rows.mockResolvedValue([{ domain: metadata.domain }, { domain: "second.test" }]); + mocks.dkim.mockImplementation(async (_exec, domain) => `public-key:${domain}`); + mocks.dns.mockResolvedValue({ + stepId: 6, + success: true, + message: "DNS", + data: { dnsRecords: { a: { value: "192.0.2.2" } } }, + }); +}); +describe("mail restore uses the destination's normal configuration paths", () => { + it("reconciles DKIM through normal setup, retains target identity and registers completion", async () => { + const operation = await prepareMailRestore(runtime(), "target", "restore-1", metadata); + const script = backupScript(operation.restoreCommand); + // Source amavis DB passwords/paths must not replace the destination config. + expect(script).not.toContain('cp -a "$tmp/keys/amavis'); + await operation.finalize(); + expect(mocks.dkim).toHaveBeenCalledWith(expect.anything(), "second.test", { reload: false }); + const state = mocks.writeState.mock.calls.at(-1)![1]; + expect(state.serverId).toBe("target"); + expect(state.dnsAcknowledged).toBe(false); + expect(state.additionalDomains["second.test"].records.dkim.value).toBe( + "public-key:second.test", + ); + expect(mocks.resume).toHaveBeenCalledOnce(); + expect(mocks.installed).toHaveBeenCalledWith("target", metadata.domain); + expect(mocks.disableRelay).toHaveBeenCalledWith(expect.anything(), { reload: false }); + }); + it("rejects a different target domain before pausing or modifying it", async () => { + mocks.target.mockResolvedValue({ domain: "unrelated.test" }); + await expect(prepareMailRestore(runtime(), "target", "restore-2", metadata)).rejects.toThrow( + "primary domain", + ); + expect(mocks.pause).not.toHaveBeenCalled(); + expect(mocks.writeState).not.toHaveBeenCalled(); + }); + + it("binds restored on-server state to the destination's current registration", async () => { + mocks.readState.mockResolvedValue(makeFreshState("previous-controller-id", metadata.domain)); + const operation = await prepareMailRestore(runtime(), "target", "restore-adopted", metadata); + await operation.finalize(); + expect(mocks.writeState.mock.calls.at(-1)![1].serverId).toBe("target"); + }); + it("leaves mail stopped and does not report completion when configuration recovery fails", async () => { + const operation = await prepareMailRestore(runtime(), "target", "restore-3", metadata); + mocks.dkim.mockRejectedValue(new Error("DKIM key unreadable")); + await expect(operation.finalize()).rejects.toThrow("DKIM key unreadable"); + expect(mocks.resume).not.toHaveBeenCalled(); + expect(mocks.installed).not.toHaveBeenCalled(); + await operation.cleanup(); + expect(mocks.removeFile).toHaveBeenCalledWith(expect.anything(), "mail-backup-restore-3.json"); + }); + it("uses recorded commands for legacy archives and refuses an unknown format", () => { + expect(readMailBackupMetadata({ restoreCommand: "recorded legacy command" })).toBeNull(); + expect(() => readMailBackupMetadata({ mail: { ...metadata, version: 99 } })).toThrow( + "unsupported", + ); + }); +}); + +describe("mail capture preflight", () => { + it("does not pause delivery when a required key/configuration check fails", async () => { + mocks.rows.mockResolvedValue([{ storagebasedirectory: "/var/vmail" }]); + const exec = vi.fn().mockRejectedValue(new Error("primary key missing")); + const host = new BareRuntime({ executor: { exec } as never }); + await expect(prepareMailBackup(host, "capture-1", {})).rejects.toThrow("primary key missing"); + expect(mocks.pause).not.toHaveBeenCalled(); + expect(mocks.removeFile).toHaveBeenCalledWith(expect.anything(), "mail-backup-capture-1.json"); + }); + it("refuses to claim a full backup of a custom mailbox root it would not capture", async () => { + mocks.rows.mockResolvedValue([{ storagebasedirectory: "/srv/external-mail" }]); + await expect(prepareMailBackup(runtime(), "capture-2", {})).rejects.toThrow( + "outside /var/vmail", + ); + expect(mocks.pause).not.toHaveBeenCalled(); + expect(mocks.writeFile).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/api/test/modules/mail/mail-backup-policy.test.ts b/apps/api/test/modules/mail/mail-backup-policy.test.ts new file mode 100644 index 000000000..0fbcb7b19 --- /dev/null +++ b/apps/api/test/modules/mail/mail-backup-policy.test.ts @@ -0,0 +1,89 @@ +import "./_setup-env"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +const mocks = vi.hoisted(() => ({ + existing: null as unknown, + destinationOrg: "org", + saved: vi.fn(), + flavor: vi.fn(), +})); +vi.mock("@repo/db", async (original) => ({ + ...(await original()), + repos: { + mailServer: { get: async () => ({ domain: "example.test" }) }, + backupDestination: { findById: async () => ({ organizationId: mocks.destinationOrg }) }, + backupPolicy: { + findActiveByMailServer: async () => mocks.existing, + create: async (data: unknown) => { + mocks.saved(data); + return data; + }, + update: async (_id: string, data: unknown) => { + mocks.saved(data); + return data; + }, + }, + }, +})); +vi.mock("../../../src/lib/permission", () => ({ permission: { assert: async () => {} } })); +vi.mock("@repo/platform/engine/lib/resource-access", () => ({ isServerInOrg: async () => true })); +vi.mock("../../../src/lib/request-context", () => ({ + getRequestContext: () => ({ userId: "owner", organizationId: "org" }), +})); +vi.mock("@repo/platform/engine/modules/backups/triggers/cron", () => ({ + syncPolicySchedule: async () => {}, + validateCronExpression: () => ({ valid: true }), +})); +vi.mock("@repo/platform/engine/modules/mail/mail-engine", async (original) => ({ + ...(await original()), + resolveMailFlavor: mocks.flavor, +})); +import { saveMailBackupPolicy } from "../../../src/modules/mail/mail.controller"; +async function save(body: Record) { + const json = vi.fn((body: unknown, status = 200) => ({ body, status })); + const ctx = { + req: { + param: () => "mail-server", + json: async () => ({ destinationId: "destination", ...body }), + }, + json, + }; + return (await saveMailBackupPolicy(ctx as never)) as unknown as { body: unknown; status: number }; +} +beforeEach(() => { + vi.clearAllMocks(); + mocks.existing = null; + mocks.destinationOrg = "org"; + mocks.flavor.mockResolvedValue("host"); +}); +describe("mail backup policy defaults and scope", () => { + it("includes messages and keys in a new policy", async () => { + expect((await save({})).status).toBe(200); + expect(mocks.saved.mock.calls[0]![0].payloadConfig.mail).toEqual({ + messageData: true, + keys: true, + }); + }); + it("retains explicit exclusions on an existing policy", async () => { + mocks.existing = { id: "policy", payloadConfig: { mail: { messageData: false, keys: false } } }; + expect((await save({})).status).toBe(200); + expect(mocks.saved.mock.calls[0]![0].payloadConfig.mail).toEqual({ + messageData: false, + keys: false, + }); + }); + it("can explicitly upgrade an old policy to a full backup", async () => { + mocks.existing = { id: "policy", payloadConfig: { mail: { messageData: false, keys: false } } }; + expect((await save({ messageData: true, keys: true })).status).toBe(200); + expect(mocks.saved.mock.calls[0]![0].payloadConfig.mail.messageData).toBe(true); + }); + it("rejects a foreign destination before touching the mail host", async () => { + mocks.destinationOrg = "foreign"; + expect((await save({})).status).toBe(404); + expect(mocks.flavor).not.toHaveBeenCalled(); + expect(mocks.saved).not.toHaveBeenCalled(); + }); + it("rejects ambiguous non-boolean capture flags", async () => { + expect((await save({ messageData: "false" })).status).toBe(400); + expect(mocks.saved).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/api/test/modules/mail/mail-backup-secrets.test.ts b/apps/api/test/modules/mail/mail-backup-secrets.test.ts index a5914863b..1aa35f35b 100644 --- a/apps/api/test/modules/mail/mail-backup-secrets.test.ts +++ b/apps/api/test/modules/mail/mail-backup-secrets.test.ts @@ -1,99 +1,46 @@ +import "./_setup-env"; import { describe, expect, it } from "vitest"; +import { buildMailBackupPayload } from "@repo/platform/engine/modules/mail/backup-plan"; +import { backupScript } from "./backup-script"; -import { buildMailBackupPayload } from "../../../src/modules/mail/admin/backup-plan"; - -/** - * A `keys: true` backup must not be able to succeed without the keys. - * - * Every secret this archive exists to carry lives on a root-owned path — `/root/.openship` - * is mode 0700, `/var/lib/dkim` holds private keys, and `/var/vmail` is vmail:vmail 0700 — - * while `produceCommand` runs through the BARE ssh executor, unelevated. The reads were - * written as `[ -f … ] && cp … || true`, which on a host Openship logs into as a sudoer - * rather than as root cannot even stat the path: the test fails for lack of privilege, - * `|| true` absorbs it, and the run exits 0 recording `keys: true` on an archive with no - * keys in it. mail-state.json is the only copy of the DKIM state and the encrypted - * mailbox/admin credentials, so the artifact is a disaster-recovery archive that isn't one. - * - * These assert the two halves of the fix — read as root, and don't swallow the failure — - * against the emitted script, which is the only observable: the shell runs on a mail host. - */ - -const SECRET_READS = [ - { what: "DKIM private keys", path: "/var/lib/dkim" }, - { what: "mail-state.json (DKIM state + encrypted credentials)", path: "/root/.openship/mail-state.json" }, -]; - -describe("mail backup cannot silently omit the secrets it was asked for", () => { - const produce = (keys: boolean) => - buildMailBackupPayload("example.com", { messageData: false, keys }).payloadConfig - .produceCommand; - - it.each(SECRET_READS)("reads $what with sudo, not as the login user", ({ path }) => { - const script = produce(true); - const line = script.split("\n").find((l) => l.includes(path)); - - expect(line, `no line reads ${path}`).toBeDefined(); - expect(line).toContain("sudo -n"); - }); - - it.each(SECRET_READS)("does not absorb a failed read of $what", ({ path }) => { - // `|| true` on these is the whole defect: it converts "could not read the secret" into - // "the host does not have one", and the two are not the same answer. - const line = produce(true) - .split("\n") - .find((l) => l.includes(path) && l.includes("cp ")); - - expect(line, `no copy of ${path}`).toBeDefined(); - expect(line).not.toContain("|| true"); - }); - - it("distinguishes an absent secret from an unreadable one", () => { - const script = produce(true); - // Absence must still skip — a box with no amavis config is a gap in the archive, not a - // failed backup. The `test` is what expresses that, so it has to be there AND be - // privileged; a `[ -f ]` under the login user answers "absent" for both cases. - expect(script).toContain("sudo -n test -f /root/.openship/mail-state.json"); - expect(script).toContain("sudo -n test -d /var/lib/dkim"); - expect(script).not.toMatch(/^\s*\[ -[fd] \/(root|var\/lib\/dkim)/m); - }); - - it("tars as root, so it can read back what it staged as root", () => { - // Not cosmetic: `sudo -n cp -a` stages root-owned 0600 files, and an unprivileged tar - // would fail to read them. Same reason /var/vmail needs it. - // - // Asserted as "EVERY `tar -c` is privileged" rather than "the tar line starts with - // sudo": the pipeline is now built by the shared `safePipelineFragment`, so the - // invocation sits inside an `if` and no longer begins the line. The property is the - // privilege, not the column it appears in — and the old prefix match would also have - // gone green if a second, unprivileged tar were added below it. - for (const messageData of [false, true]) { - const script = buildMailBackupPayload("example.com", { messageData, keys: true }) - .payloadConfig.produceCommand; - const invocations = [...script.matchAll(/(\S*\s*\S*\s*)?tar -c/g)].map((m) => m[0]); - expect(invocations.length, `no tar -c (messageData: ${messageData})`).toBeGreaterThan(0); - for (const found of invocations) { - expect(found, `unprivileged tar -c (messageData: ${messageData})`).toContain("sudo -n"); - } - } - }); - - it("no longer lets zstd mask tar's exit status", () => { - // The masking this file's comments described for as long as they existed: a shell - // pipeline reports its LAST command's status, so an unprivileged tar that dropped every - // root-owned secret still exited 0 behind zstd. This was the last pipeline in the - // backup module still carrying it. - const script = buildMailBackupPayload("example.com", { messageData: true, keys: true }) +describe("mail backup secret handling", () => { + it("runs the entire capture as root, with private staging and mandatory requested secrets", () => { + const command = buildMailBackupPayload("example.test", { messageData: true, keys: true }) .payloadConfig.produceCommand; + const script = backupScript(command); + expect(script).toContain("umask 077"); + expect(script).toContain("test -d '/var/lib/dkim'"); + expect(script).toContain("test -s '/root/.openship/mail-state.json'"); + expect(script).toContain("cp -a '/var/lib/dkim'"); + expect(script).toContain("cp -a '/root/.openship/mail-state.json'"); + expect(script).not.toContain("|| true"); + expect(script).toContain("trap 'rm -rf \"$tmp\"' EXIT"); + }); + it("uses the per-run portable file, without embedding its contents in commands", () => { + const command = buildMailBackupPayload( + "example.test", + { messageData: false, keys: true }, + "host", + { statePath: "/root/.openship/mail-backup-run.json" }, + ).payloadConfig.produceCommand; + expect(backupScript(command)).toContain("cp -a '/root/.openship/mail-backup-run.json'"); + expect(backupScript(command)).not.toContain("cp -a '/root/.openship/mail-state.json'"); + }); + it("propagates both tar and compressor failures", () => { + const script = backupScript( + buildMailBackupPayload("example.test", { messageData: true, keys: true }).payloadConfig + .produceCommand, + ); expect(script).toContain("openship: tar (staging the mail archive) failed with status"); expect(script).toContain("openship: the compressor 'zstd' failed with status"); - // And the raw masking shape is gone. - expect(script).not.toMatch(/tar -c[^\n]*\| zstd -c -3\s*$/m); }); - - it("stages nothing privileged when keys were not requested", () => { - // The contrast case: without it, a fix that sudo'd unconditionally would pass the - // assertions above while changing what a keys:false backup touches. - const script = produce(false); - for (const { path } of SECRET_READS) expect(script).not.toContain(path); + it("does not read private configuration when explicitly excluded", () => { + const script = backupScript( + buildMailBackupPayload("example.test", { messageData: false, keys: false }).payloadConfig + .produceCommand, + ); + expect(script).not.toContain("/var/lib/dkim"); + expect(script).not.toContain("/root/.openship/mail-state.json"); + expect(script).not.toContain("-C '/var' vmail"); }); }); diff --git a/apps/api/test/modules/mail/mail-backup-state.test.ts b/apps/api/test/modules/mail/mail-backup-state.test.ts new file mode 100644 index 000000000..ffdc66488 --- /dev/null +++ b/apps/api/test/modules/mail/mail-backup-state.test.ts @@ -0,0 +1,97 @@ +import "./_setup-env"; +import { describe, expect, it } from "vitest"; +import { createEncryption } from "@repo/db/encryption"; +import { makeFreshState } from "@repo/platform/engine/modules/mail/mail-state"; +import { exportMailState, importMailState } from "@repo/platform/engine/modules/mail/backup-state"; + +describe("mail configuration transferred between instances", () => { + it("re-encrypts mailbox and relay credentials while retaining the destination's database credentials", () => { + const sourceCipher = createEncryption("source-instance-key"); + const targetCipher = createEncryption("different-target-instance-key"); + const source = makeFreshState("source", "example.test"); + source.secrets = { + VMAIL_DB_BIND_PASSWD: "source-db", + DOMAIN_ADMIN_PASSWD_PLAIN: "source-postmaster", + }; + source.dnsAcknowledged = true; + source.ptrAcknowledged = true; + source.platformMailbox = { + email: "openship@example.test", + password: sourceCipher.encrypt("mail-password"), + smtpHost: "mail.example.test", + smtpPort: 587, + secure: false, + updatedAt: "2026-01-01", + }; + source.testMailboxes = { "example.test": { ...source.platformMailbox } }; + source.outboundRelay = { + enabled: true, + provider: "custom", + host: "relay.example.test", + port: 587, + username: "sender", + passwordEncrypted: sourceCipher.encrypt("relay-password"), + updatedAt: "2026-01-01", + }; + const target = makeFreshState("destination", "example.test"); + target.secrets = { VMAIL_DB_BIND_PASSWD: "target-db", PGSQL_ROOT_PASSWD: "target-root" }; + target.engineImage = "target-engine"; + const portable = exportMailState(source, sourceCipher); + const restored = importMailState(portable, target, targetCipher); + + expect(portable.state.platformMailbox?.password).toBe("mail-password"); + expect(JSON.stringify(portable)).not.toContain("source-db"); + expect(targetCipher.decrypt(restored.platformMailbox!.password)).toBe("mail-password"); + expect(targetCipher.decrypt(restored.testMailboxes!["example.test"]!.password)).toBe( + "mail-password", + ); + expect(targetCipher.decrypt(restored.outboundRelay!.passwordEncrypted)).toBe("relay-password"); + expect(() => sourceCipher.decrypt(restored.platformMailbox!.password)).toThrow(); + expect(restored.secrets).toEqual({ + VMAIL_DB_BIND_PASSWD: "target-db", + PGSQL_ROOT_PASSWD: "target-root", + DOMAIN_ADMIN_PASSWD_PLAIN: "source-postmaster", + }); + expect(restored.serverId).toBe("destination"); + expect(restored.engineImage).toBe("target-engine"); + expect(restored.dnsAcknowledged).toBe(false); + expect(restored.ptrAcknowledged).toBe(false); + expect(sourceCipher.decrypt(source.platformMailbox!.password)).toBe("mail-password"); + sourceCipher.close(); + targetCipher.close(); + }); + + it("refuses an undecryptable credential instead of making an unrestorable archive", () => { + const source = makeFreshState("source", "example.test"); + const wrong = createEncryption("wrong-key"); + const original = createEncryption("original-key"); + source.platformMailbox = { + email: "openship@example.test", + password: original.encrypt("secret"), + smtpHost: "mail.example.test", + smtpPort: 587, + secure: false, + updatedAt: "2026-01-01", + }; + expect(() => exportMailState(source, wrong)).toThrow(); + original.close(); + wrong.close(); + }); + + it("refuses restoring a different primary domain", () => { + const source = makeFreshState("source", "example.test"); + const target = makeFreshState("target", "unrelated.test"); + expect(() => importMailState(exportMailState(source), target)).toThrow("primary domain"); + }); + + it("does not retain a destination password for a postmaster account replaced by the backup", () => { + const source = makeFreshState("source", "example.test"); + const target = makeFreshState("target", "example.test"); + target.secrets = { + VMAIL_DB_BIND_PASSWD: "target-db", + DOMAIN_ADMIN_PASSWD_PLAIN: "old-account-password", + }; + const restored = importMailState(exportMailState(source), target); + expect(restored.secrets).toEqual({ VMAIL_DB_BIND_PASSWD: "target-db" }); + }); +}); diff --git a/apps/api/test/modules/mail/mail-dkim-domain-validation.test.ts b/apps/api/test/modules/mail/mail-dkim-domain-validation.test.ts new file mode 100644 index 000000000..9a358f06f --- /dev/null +++ b/apps/api/test/modules/mail/mail-dkim-domain-validation.test.ts @@ -0,0 +1,12 @@ +import "./_setup-env"; +import { expect, it, vi } from "vitest"; +import { provisionDomainDkim } from "@repo/platform/engine/modules/mail/mail.service"; + +it.each(["x;touch /tmp/executed.test", "../example.test", "x'example.test", "x$(id).test"])( + "refuses unsafe restored domain %s before filesystem or shell access", + async (domain) => { + const exec = vi.fn(); + await expect(provisionDomainDkim({ exec } as never, domain)).rejects.toThrow("Invalid domain"); + expect(exec).not.toHaveBeenCalled(); + }, +); diff --git a/apps/api/test/modules/mail/mail-restore-amavis-path.test.ts b/apps/api/test/modules/mail/mail-restore-amavis-path.test.ts deleted file mode 100644 index e7fb719e1..000000000 --- a/apps/api/test/modules/mail/mail-restore-amavis-path.test.ts +++ /dev/null @@ -1,140 +0,0 @@ -import { execFileSync } from "node:child_process"; -import { existsSync, mkdirSync, mkdtempSync, readFileSync, writeFileSync } from "node:fs"; -import { tmpdir } from "node:os"; -import { join } from "node:path"; - -import { describe, expect, it } from "vitest"; - -import { buildMailBackupPayload } from "../../../src/modules/mail/admin/backup-plan"; - -/** - * A restored amavis config has to land where THIS box's amavis reads it. - * - * Restoring is normally cross-machine — that is the point of a backup — so source and - * target routinely disagree about where amavis keeps its editable config: Debian has an - * include dir (`/etc/amavis/conf.d/50-user`), the RHEL family a monolithic - * `/etc/amavisd.conf`. The archive records the path it came from, and honouring that - * verbatim writes a file the target's amavis never opens — `cp -a … || true` succeeds, the - * restore reports success, and the config silently does nothing. - * - * These run the REAL emitted shell rather than asserting on its text, with the candidate - * paths rebased under a temp root: the rule is a chain of `test`s, and a string assertion - * cannot tell a correct chain from a plausible one. It also pins the direction that is easy - * to get wrong — on Debian the destination FILE legitimately does not exist yet (50-user is - * ours to create), so a probe keyed on the file instead of the include DIRECTORY reads a - * Debian box as "no amavis here" and falls through to the RHEL path. - */ - -const DEBIAN = "/etc/amavis/conf.d/50-user"; -const DEBIAN_DIR = "/etc/amavis/conf.d"; -const RHEL = "/etc/amavisd.conf"; -const CONTENT = "$final_spam_destiny = D_DISCARD;\n"; - -/** - * The amavis placement block of the real restore script, rebased under `root`. - * - * Sliced between two stable markers rather than by line number, so an edit elsewhere in the - * restore cannot silently shrink what is under test — a missing marker fails loudly here - * instead of passing vacuously. - */ -function placementScript(root: string, tmp: string): string { - const script = buildMailBackupPayload("example.com", { - messageData: false, - keys: true, - }).payloadConfig.restoreCommand; - - const start = script.indexOf('dest=""'); - const end = script.indexOf('if [ -d "$tmp/vmail1" ]'); - expect(start, "restore script no longer resolves an amavis `dest`").toBeGreaterThan(-1); - expect(end, "restore script has no maildir step to slice against").toBeGreaterThan(start); - - const block = script.slice(start, end); - expect(block).toContain("cp -a"); - // Every absolute path in this block is an /etc one, and nothing else in it is. - return `set -e\ntmp=${JSON.stringify(tmp)}\n${block.replaceAll("/etc/", `${root}/etc/`)}`; -} - -/** Stage an archive + a target layout, run the placement, report where the file went. */ -function place(opts: { - /** Pre-existing amavis config on the TARGET, or none at all. */ - target: "debian" | "rhel" | "none"; - /** Path the ARCHIVE says it came from; null for a legacy archive that records none. */ - archiveFrom: string | null; - legacyName?: boolean; -}): { at: (p: string) => boolean; read: (p: string) => string } { - const root = mkdtempSync(join(tmpdir(), "amavis-target-")); - const tmp = mkdtempSync(join(tmpdir(), "amavis-archive-")); - - mkdirSync(join(root, "etc"), { recursive: true }); - if (opts.target === "debian") mkdirSync(join(root, DEBIAN_DIR), { recursive: true }); - if (opts.target === "rhel") writeFileSync(join(root, RHEL), "# the target's own\n"); - - mkdirSync(join(tmp, "keys"), { recursive: true }); - writeFileSync(join(tmp, opts.legacyName ? "keys/amavis-50-user" : "keys/amavis-conf"), CONTENT); - if (opts.archiveFrom) { - // A real archive names a path in the SOURCE box's namespace, and the candidate list the - // script matches it against is rebased — so a known path has to be rebased too, or the - // fallback could never match its own list and every case would test the same branch. - // An unknown path is left alone: not matching is the whole point of it. - const recorded = opts.archiveFrom.startsWith("/etc/") - ? join(root, opts.archiveFrom) - : opts.archiveFrom; - writeFileSync(join(tmp, "keys/amavis-conf.path"), recorded); - } - - execFileSync("sh", ["-c", placementScript(root, tmp)], { stdio: "pipe" }); - - return { - at: (p) => existsSync(join(root, p)), - read: (p) => readFileSync(join(root, p), "utf8"), - }; -} - -describe("a restored amavis config lands where the TARGET reads it", () => { - it("writes the RHEL path on a RHEL target, even from a Debian archive", () => { - const r = place({ target: "rhel", archiveFrom: DEBIAN }); - - expect(r.read(RHEL)).toBe(CONTENT); - // The source's path must not be recreated: a file there is one this box's amavis will - // never open, and its presence is what made the failure look like a success. - expect(r.at(DEBIAN)).toBe(false); - }); - - it("writes the include file on a Debian target, even from a RHEL archive", () => { - // The direction a file-existence probe gets wrong: 50-user is ours to CREATE, so the - // only evidence a Debian box offers is the include DIRECTORY. - const r = place({ target: "debian", archiveFrom: RHEL }); - - expect(r.read(DEBIAN)).toBe(CONTENT); - expect(r.at(RHEL)).toBe(false); - }); - - it("falls back to the archive's recorded path when the target has no amavis", () => { - // Nothing to probe, so the source is the best evidence left. - const r = place({ target: "none", archiveFrom: RHEL }); - - expect(r.read(RHEL)).toBe(CONTENT); - expect(r.at(DEBIAN)).toBe(false); - }); - - it("never writes a path the archive invented", () => { - // This shell runs as root on the target, so the recorded path is matched against the - // known candidates rather than used — otherwise `cp -a` is an arbitrary root write. - // With no amavis to probe and no usable recorded path, nothing is placed at all: the - // default's include dir doesn't exist on such a box and `|| true` absorbs the miss. - const r = place({ target: "none", archiveFrom: "/tmp/openship-amavis-escape/authorized_keys" }); - - expect(existsSync("/tmp/openship-amavis-escape/authorized_keys")).toBe(false); - expect(r.at(RHEL)).toBe(false); - expect(r.at(DEBIAN)).toBe(false); - }); - - it("places a legacy archive by the target's layout, since it records no source", () => { - // Pre-2026-08 archives carried the Debian path under a fixed name with no `.path` - // sidecar, so the target probe is the only thing that can place them at all. - const r = place({ target: "rhel", archiveFrom: null, legacyName: true }); - - expect(r.read(RHEL)).toBe(CONTENT); - expect(r.at(DEBIAN)).toBe(false); - }); -}); diff --git a/apps/api/test/modules/mcp/mcp-tools-filter.test.ts b/apps/api/test/modules/mcp/mcp-tools-filter.test.ts index 719dad2d2..be9f00362 100644 --- a/apps/api/test/modules/mcp/mcp-tools-filter.test.ts +++ b/apps/api/test/modules/mcp/mcp-tools-filter.test.ts @@ -135,6 +135,7 @@ function principal( const granted = new Set([...(p.grantedRootTypes ?? []), ...Object.keys(wildcards)]); return { role: p.role ?? "restricted", + membershipRole: p.membershipRole, readOnly: p.readOnly ?? false, grantedRootTypes: granted, wildcardGrants: p.wildcardGrants ?? new Map(Object.entries(wildcards)), @@ -147,6 +148,16 @@ function principal( const RWA: Permission[] = ["read", "write", "admin"]; describe("filterToolsForPrincipal", () => { + it("hides server administration from members and their existing scoped tokens", () => { + const read = tool({ name: "server_read", method: "GET", perm: { root: "server", leaf: "server", grantRoot: "server", action: "read", wildcard: false } }); + const exec = tool({ name: "server_exec", method: "POST", perm: { root: "server", leaf: "server", grantRoot: "server", action: "admin", wildcard: false } }); + for (const caller of [ + principal({ role: "member" }), + principal({ role: "restricted", membershipRole: "member", grantedRootTypes: new Set(["server"]), wildcards: { server: RWA } }), + ]) expect(names(filterToolsForPrincipal([read, exec], caller))).toEqual(["server_read"]); + expect(names(filterToolsForPrincipal([read, exec], principal({ role: "admin" })))).toEqual(["server_exec", "server_read"]); + }); + it("owner sees everything", () => { const out = filterToolsForPrincipal(ALL, principal({ role: "owner" })); expect(out.length).toBe(ALL.length); diff --git a/apps/api/test/modules/mcp/mcp-workspaces.test.ts b/apps/api/test/modules/mcp/mcp-workspaces.test.ts index 80dbb98b3..8179cc373 100644 --- a/apps/api/test/modules/mcp/mcp-workspaces.test.ts +++ b/apps/api/test/modules/mcp/mcp-workspaces.test.ts @@ -138,6 +138,22 @@ describe("MCP workspace discovery", () => { expect(denied.isError).toBe(true); }); + it.each([false, true])("advertises administration in another workspace only for an unbound credential (bound=%s)", async bound => { + const owner = await seedOwner({ bound }); + await secondWorkspace(owner, "admin"); + await db.update(schema.member).set({ role: "member" }).where(and(eq(schema.member.userId, owner.userId), eq(schema.member.organizationId, owner.orgId))); + const listed = await (await rpc(owner, "tools/list")).json() as { result: { tools: { name: string }[] } }; + expect(listed.result.tools.some(tool => tool.name === "delete_projects_by_id")).toBe(!bound); + if (!bound) { + const group = await repos.projectGroup.create({ organizationId: owner.orgId, name: "Member project", slug: "member-project" }); + const project = await repos.project.create({ organizationId: owner.orgId, groupId: group.id, name: "Member project", slug: "member-project" }); + const denied = await call(owner, "delete_projects_by_id", { id: project.id, organizationId: owner.orgId }); + expect(denied).toMatchObject({ isError: true, data: { code: "NOT_FOUND" } }); + expect(forwarding.fetch).toHaveBeenCalled(); + expect(await repos.project.findById(project.id)).toMatchObject({ id: project.id }); + } + }); + it("shares the discovery contract with SDKs and respects fixed native scopes", async () => { const owner = await seedOwner({ bound: false }); const target = await secondWorkspace(owner); diff --git a/apps/api/test/modules/migration/platform-import-domains.test.ts b/apps/api/test/modules/migration/platform-import-domains.test.ts new file mode 100644 index 000000000..26c5800b8 --- /dev/null +++ b/apps/api/test/modules/migration/platform-import-domains.test.ts @@ -0,0 +1,185 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { db, eq, repos, schema, type Service } from "@repo/db"; +import type { ExecutionContext } from "@repo/platform"; +import type { PlannedImportService } from "@repo/platform/engine/modules/migration/platform-import-plan"; +import { platformImportRoutes } from "@repo/platform/engine/modules/migration/platform-import-project"; +import { saveMigrationRoutes } from "@repo/platform/engine/modules/migration/migration-routes"; +import { seedOrg, seedProject, seedService } from "../../helpers/seed"; + +const live = vi.hoisted(() => ({ reconcile: vi.fn() })); +vi.mock("@repo/platform/engine/lib/route-apply.service", async (original) => ({ + ...(await original()), + reconcileProjectRoutes: live.reconcile, +})); + +function recipe( + service: Service, + domains: PlannedImportService["source"]["domains"], + data: PlannedImportService["data"] = "empty", +): PlannedImportService { + return { + source: { + id: `source-${service.id}`, + name: "Provider name", + kind: "app", + domains, + env: {}, + rawEnv: {}, + variables: [], + volumes: [], + warnings: [], + blockers: [], + }, + data, + create: { name: service.name }, + environment: {}, + bindings: [], + }; +} + +describe("provider imports share native pending-domain persistence", () => { + const organizations: string[] = []; + let ctx: ExecutionContext; + beforeEach(async () => { + vi.clearAllMocks(); + ctx = (await seedOrg()) as ExecutionContext; + organizations.push(ctx.organizationId); + }); + afterEach(async () => { + for (const id of organizations.splice(0)) { + await db.delete(schema.project).where(eq(schema.project.organizationId, id)); + await db.delete(schema.servers).where(eq(schema.servers.organizationId, id)); + await db.delete(schema.cloudWorkspace).where(eq(schema.cloudWorkspace.organizationId, id)); + await db.delete(schema.organization).where(eq(schema.organization.id, id)); + } + }); + + it.each(["server", "cloud"])( + "saves custom aliases on their services without verifying or applying live routes (%s)", + async (target) => { + const managed = + target === "cloud" + ? await repos.cloudWorkspace.create({ + organizationId: ctx.organizationId, + name: "Import destination", + }) + : null; + const server = managed + ? await repos.server.findByWorkspace(managed.id, ctx.organizationId) + : null; + const project = await seedProject(ctx.organizationId, { + framework: "docker-compose", + serverId: server?.id, + }); + const web = await seedService(project.id, { + name: "storefront", + ports: ["8080"], + exposed: false, + }); + const api = await seedService(project.id, { + name: "api", + kind: "monorepo", + ports: ["3000"], + exposed: false, + }); + const internal = await seedService(project.id, { + name: "worker", + ports: ["9000"], + exposed: false, + }); + const plans = [ + recipe(web, [ + { hostname: "shop.example.com", portable: true }, + { hostname: "www.example.com", portable: true }, + { hostname: "shop.up.railway.app", portable: false }, + ]), + recipe(api, [ + { hostname: "api.example.com", portable: true }, + { hostname: "api.vercel.app", portable: false }, + ]), + recipe(internal, [{ hostname: "worker.up.railway.app", portable: false }]), + recipe(internal, [{ hostname: "external.example.com", portable: true }], "external"), + ]; + const routes = platformImportRoutes(plans); + await saveMigrationRoutes(ctx, project.id, routes, () => {}); + // Re-saving uses the existing rows, including verification tokens and aliases. + const original = await repos.domain.listByProject(project.id); + await saveMigrationRoutes(ctx, project.id, routes, () => {}); + const domains = await repos.domain.listByProject(project.id); + expect(domains.map((domain) => domain.id).sort()).toEqual( + original.map((domain) => domain.id).sort(), + ); + expect(domains).toHaveLength(3); + for (const [hostname, serviceId, targetPort] of [ + ["shop.example.com", web.id, 8080], + ["www.example.com", web.id, 8080], + ["api.example.com", api.id, 3000], + ] as const) { + const domain = domains.find((item) => item.hostname === hostname)!; + expect(domain).toMatchObject({ + projectId: project.id, + serviceId, + targetPort, + verified: false, + status: "pending", + }); + expect(domain.verificationToken).toBeTruthy(); + expect(domain.verificationToken).toBe( + original.find((item) => item.id === domain.id)?.verificationToken, + ); + } + expect((await repos.service.findById(web.id))?.publicEndpoints).toEqual([ + expect.objectContaining({ port: 8080, customDomain: "shop.example.com" }), + ]); + expect((await repos.project.findById(project.id))?.compositeRoutes).toEqual([ + expect.objectContaining({ + hostname: "www.example.com", + rootServiceId: web.id, + rootPort: 8080, + }), + ]); + expect((await repos.service.findById(internal.id))?.exposed).toBe(false); + expect(live.reconcile).not.toHaveBeenCalled(); + }, + ); + + it("refuses a cross-organization domain before saving any imported route", async () => { + const foreign = (await seedOrg()) as ExecutionContext; + organizations.push(foreign.organizationId); + const existingProject = await seedProject(foreign.organizationId, { + framework: "docker-compose", + }); + const existingService = await seedService(existingProject.id, { + name: "owner", + ports: ["8080"], + }); + await saveMigrationRoutes( + foreign, + existingProject.id, + platformImportRoutes([ + recipe(existingService, [{ hostname: "taken.example.com", portable: true }]), + ]), + () => {}, + ); + const original = await repos.domain.listByProject(existingProject.id); + const project = await seedProject(ctx.organizationId, { framework: "docker-compose" }); + const web = await seedService(project.id, { name: "web", ports: ["8080"], exposed: false }); + await expect( + saveMigrationRoutes( + ctx, + project.id, + platformImportRoutes([ + recipe(web, [ + { hostname: "available.example.com", portable: true }, + { hostname: "taken.example.com", portable: true }, + ]), + ]), + () => {}, + ), + ).rejects.toThrow("already connected to another project"); + expect(await repos.domain.listByProject(project.id)).toEqual([]); + expect((await repos.service.findById(web.id))?.exposed).toBe(false); + expect(await repos.domain.listByProject(existingProject.id)).toEqual(original); + expect(live.reconcile).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/api/test/modules/notifications/notifications-sdk-parity.test.ts b/apps/api/test/modules/notifications/notifications-sdk-parity.test.ts index 0f616742c..06ab6469c 100644 --- a/apps/api/test/modules/notifications/notifications-sdk-parity.test.ts +++ b/apps/api/test/modules/notifications/notifications-sdk-parity.test.ts @@ -133,7 +133,7 @@ describe("notifications shared SDK/HTTP operations", () => { expect(await c.native.listDefaults()).toEqual([value]); expect(await c.native.categories()).toEqual(await c.remote.categories()); await db.update(schema.member).set({ role: "member" }).where(eq(schema.member.userId, owner.userId)); - for (const client of [c.native, c.remote]) await expect(client.upsertDefault({ category: "deploy.failed", defaultEnabled: false })).rejects.toMatchObject({ code: "FORBIDDEN" }); + for (const client of [c.native, c.remote]) await expect(client.upsertDefault({ category: "deploy.failed", defaultEnabled: false })).rejects.toMatchObject({ code: "NOT_FOUND" }); }); it("does not verify a replacement configuration or hide normal delivery failures", async () => { diff --git a/apps/api/test/modules/projects/cloud-promotion-recovery.test.ts b/apps/api/test/modules/projects/cloud-promotion-recovery.test.ts index a6858b068..10a53e784 100644 --- a/apps/api/test/modules/projects/cloud-promotion-recovery.test.ts +++ b/apps/api/test/modules/projects/cloud-promotion-recovery.test.ts @@ -465,8 +465,9 @@ describe("project promotion recovery", () => { const dump = await dumpSubgraph({ kind: "project", projectId }); const changed = structuredClone(dump); changed.tables.resource_usage.push({ id: "another-sample", projectId, cpu: 42 }); - changed.tables.project[0].favicon = "new favicon"; expect(projectPromotionDigest(changed)).toBe(projectPromotionDigest(dump)); + changed.tables.project[0].iconUrl = "https://cdn.example.test/new-icon.png"; + expect(projectPromotionDigest(changed)).not.toBe(projectPromotionDigest(dump)); changed.tables.project[0].localPath = "/another-source"; expect(projectPromotionDigest(changed)).not.toBe(projectPromotionDigest(dump)); }); diff --git a/apps/api/test/modules/projects/cloud-routing-regression.test.ts b/apps/api/test/modules/projects/cloud-routing-regression.test.ts index a08e2a3ed..a047ea544 100644 --- a/apps/api/test/modules/projects/cloud-routing-regression.test.ts +++ b/apps/api/test/modules/projects/cloud-routing-regression.test.ts @@ -113,7 +113,7 @@ describe("managed Cloud route port edits and repair", () => { await editRoutes(["app.opsh.io"]); expect(h.fixture.pages.create).toHaveBeenCalledOnce(); expect(h.fixture.routes.set).toHaveBeenLastCalledWith("app.opsh.io", expect.objectContaining({ - routes: [{ match: { path: "/", type: "prefix" }, action: { kind: "proxy", workspace: h.fixture.workspaceId, port: 32001 } }], + routes: [{ match: { path: "/", type: "prefix" }, action: { kind: "proxy", vm: h.fixture.workspaceId, port: 32001 } }], })); expect(h.syncManagedEdge).not.toHaveBeenCalled(); }); @@ -124,7 +124,7 @@ describe("managed Cloud route port edits and repair", () => { expect(h.fixture.pages.connectDomain).toHaveBeenCalledWith(expect.any(String), { domain: "app.example.com" }); expect(h.fixture.workspace.network.update).toHaveBeenCalledWith(expect.objectContaining({ ingress_ports: [443, 32000] })); expect(h.fixture.routes.set).toHaveBeenCalledWith("app.example.com", expect.objectContaining({ - routes: expect.arrayContaining([expect.objectContaining({ action: { kind: "proxy", workspace: h.fixture.workspaceId, port: 32000 } })]), + routes: expect.arrayContaining([expect.objectContaining({ action: { kind: "proxy", vm: h.fixture.workspaceId, port: 32000 } })]), })); }); @@ -165,8 +165,8 @@ describe("managed Cloud route port edits and repair", () => { h.domains.push({ ...h.domains[0]!, id: "domain-b", hostname: "console.opsh.io", targetPort: 9000, isPrimary: false }); expect(await retryProjectRouting(h.project.id, h.project.organizationId)).toEqual({ ok: true }); expect(h.fixture.routes.set.mock.calls.map(([host, table]) => [host, table.routes[0]?.action])).toEqual([ - ["app.opsh.io", { kind: "proxy", workspace: h.fixture.workspaceId, port: 32000 }], - ["console.opsh.io", { kind: "proxy", workspace: h.fixture.workspaceId, port: 32001 }], + ["app.opsh.io", { kind: "proxy", vm: h.fixture.workspaceId, port: 32000 }], + ["console.opsh.io", { kind: "proxy", vm: h.fixture.workspaceId, port: 32001 }], ]); }); @@ -196,8 +196,8 @@ describe("managed Cloud route port edits and repair", () => { })); expect(await retryProjectRouting(h.project.id, h.project.organizationId)).toEqual({ ok: true }); expect(h.fixture.routes.set.mock.calls.map(([host, table]) => [host, table.routes[0]?.action])).toEqual([ - ["app.example.com", { kind: "proxy", workspace: h.fixture.workspaceId, port: 32000 }], - ["alias.example.com", { kind: "proxy", workspace: h.fixture.workspaceId, port: 32000 }], + ["app.example.com", { kind: "proxy", vm: h.fixture.workspaceId, port: 32000 }], + ["alias.example.com", { kind: "proxy", vm: h.fixture.workspaceId, port: 32000 }], ]); expect(h.fixture.pages.delete).not.toHaveBeenCalled(); }); diff --git a/apps/api/test/modules/projects/folder-upload.test.ts b/apps/api/test/modules/projects/folder-upload.test.ts index 4360dc34f..9cc62e6a2 100644 --- a/apps/api/test/modules/projects/folder-upload.test.ts +++ b/apps/api/test/modules/projects/folder-upload.test.ts @@ -80,11 +80,11 @@ afterAll(async () => { if (root) await rm(root, { recursive: true, force: true }); }); -async function open() { +async function open(name?: string) { const response = await fetch(`${origin}/api/projects/folder/session`, { method: "POST", headers: { ...owner.auth, "Content-Type": "application/json" }, - body: JSON.stringify(env.CLOUD_MODE ? { serverId: managedServerId } : {}), + body: JSON.stringify({ ...(env.CLOUD_MODE ? { serverId: managedServerId } : {}), ...(name ? { name } : {}) }), }); expect(response.status).toBe(200); const target = (await response.json()) as FolderSessionResult; @@ -117,6 +117,33 @@ function upload(target: FolderSessionResult, bytes = archive, chunked = false) { describe.each([false, true])("authenticated folder upload over HTTP (Cloud: %s)", (cloudMode) => { beforeEach(() => { env.CLOUD_MODE = cloudMode; }); + it.each([undefined, "my-folder-app"])("uses a client name or stable fallback instead of the staging path: %s", async (name) => { + const target = await open(name); + const uploaded = await upload(target); + expect(uploaded.status).toBe(200); + const response = await fetch(`${origin}/api/projects/folder/scan/${target.sessionId}`, { + method: "POST", headers: { ...owner.auth, "Content-Type": "application/json" }, body: "{}", + }); + expect(response.status, await response.clone().text()).toBe(200); + expect(await response.json()).toMatchObject({ name: name ?? "app" }); + }); + + it("returns the safe Compose refusal through the actual folder scan route", async () => { + const source = await mkdtemp(join(root, "host-network-")); + await writeFile(join(source, "compose.yaml"), "services:\n web:\n image: nginx:alpine\n network_mode: host\n"); + const packed = await archiveSourceDirectory(source, { temporaryRoot: join(root, "archives") }); + try { + const target = await open(); + expect((await upload(target, await readFile(packed.path))).status).toBe(200); + const response = await fetch(`${origin}/api/projects/folder/scan/${target.sessionId}`, { + method: "POST", headers: { ...owner.auth, "Content-Type": "application/json" }, body: "{}", + }); + expect(response.status).toBe(400); + expect(await response.json()).toMatchObject({ + code: "COMPOSE_CONFIGURATION_INVALID", error: expect.stringContaining("network_mode"), + }); + } finally { await packed.dispose(); } + }); it.each([false, true])("preserves every byte above 10 MiB (chunked: %s)", async (chunked) => { const target = await open(); const response = await upload(target, archive, chunked); @@ -133,7 +160,8 @@ describe.each([false, true])("authenticated folder upload over HTTP (Cloud: %s)" it("rejects a truncated gzip, cleans temporary files, and allows a complete retry", async () => { const target = await open(); const response = await upload(target, archive.subarray(0, 10 * 1024 * 1024)); - expect(response.status, await response.text()).toBe(400); + expect(response.status).toBe(400); + expect(await response.json()).toMatchObject({ error: expect.stringMatching(/incomplete|truncated/i) }); const session = getFolderSession(target.sessionId)!; expect(session.uploaded).toBe(false); expect(session.uploading).toBe(false); diff --git a/apps/api/test/modules/projects/project-icon.test.ts b/apps/api/test/modules/projects/project-icon.test.ts new file mode 100644 index 000000000..4cbd56c71 --- /dev/null +++ b/apps/api/test/modules/projects/project-icon.test.ts @@ -0,0 +1,58 @@ +import "../jobs/_env"; +import { expect, it } from "vitest"; +import { Hono } from "hono"; +import { seedOwner, repos } from "../jobs/_harness"; +import { seedProject, seedDeployment } from "../../helpers/seed"; +import { listDeployments } from "@repo/platform/engine/modules/deployments/deployment.service"; +import { projectRoutes } from "../../../src/modules/projects/project.routes"; +import { handleApiError } from "../../../src/middleware/error-handler"; + +const app = new Hono().onError(handleApiError).route("/api/projects", projectRoutes); + +it("uses one icon URL across authenticated settings, reads and deployment history", async () => { + const owner = await seedOwner(); + const project = await seedProject(owner.orgId); + const custom = "https://cdn.example.test/project.svg"; + const update = (iconUrl: unknown, auth = owner.auth) => + app.request(`/api/projects/${project.id}`, { + method: "PATCH", + headers: { ...auth, "Content-Type": "application/json" }, + body: JSON.stringify({ iconUrl }), + }); + const assertIcon = (data: unknown, expected: string | null) => { + expect(data).toMatchObject({ iconUrl: expected }); + expect(data).not.toHaveProperty("favicon"); + expect(data).not.toHaveProperty("faviconCheckedAt"); + }; + const saved = await update(` ${custom} `); + expect(saved.status, await saved.clone().text()).toBe(200); + assertIcon((await saved.json()).data, custom); + assertIcon(await repos.project.findById(project.id), custom); + const read = await app.request(`/api/projects/${project.id}`, { headers: owner.auth }); + expect(read.status, await read.clone().text()).toBe(200); + assertIcon((await read.json()).data, custom); + await seedDeployment(project); + for (const options of [{ projectId: project.id }, {}]) { + const history = await listDeployments(owner.orgId, options); + assertIcon(history.rows[0], custom); + } + const stranger = await seedOwner(); + expect((await update("https://stranger.example.test/icon.png", stranger.auth)).status).toBe(404); + for (const unsafe of [ + "javascript:alert(1)", + "data:image/png;base64,AAA", + "https://user:password@example.test/i.png", + "/relative.png", + ]) + expect((await update(unsafe)).status).toBe(400); + assertIcon(await repos.project.findById(project.id), custom); + for (const reset of [null, ""]) { + expect((await update(custom)).status).toBe(200); + const cleared = await update(reset); + expect(cleared.status).toBe(200); + assertIcon((await cleared.json()).data, null); + assertIcon(await repos.project.findById(project.id), null); + const history = await listDeployments(owner.orgId, { projectId: project.id }); + assertIcon(history.rows[0], null); + } +}); diff --git a/apps/api/test/modules/projects/project-overview-logs.test.ts b/apps/api/test/modules/projects/project-overview-logs.test.ts index 05434455e..555e7ddf9 100644 --- a/apps/api/test/modules/projects/project-overview-logs.test.ts +++ b/apps/api/test/modules/projects/project-overview-logs.test.ts @@ -19,7 +19,6 @@ vi.mock("@repo/db", () => ({ repos: { vi.mock("@repo/platform/engine/modules/projects/project.service", () => ({ listProjects: h.list, enrichProjectsBatch: h.enrich, deploymentIsBlocked: () => false })); vi.mock("@repo/platform/engine/lib/authorization", () => ({ authorization: { checkPermissionOnResource: h.canRead, authorize: async (ctx: ExecutionContext) => ctx } })); vi.mock("@repo/platform/engine/lib/cloud/projects", () => ({ fetchOrgCloudProjects: h.cloudProjects })); -vi.mock("@repo/platform/engine/lib/favicon-detector", () => ({ refreshProjectFaviconIfStale: vi.fn() })); vi.mock("@repo/platform/engine/config/index", () => ({ env: h.env })); vi.mock("@repo/platform/engine/lib/oblien-user-client", () => ({ getAdminOblienClient: () => h.env.CLOUD_MODE ? { analytics: { streamToken: h.streamToken, requests: h.requests } } : null })); vi.mock("@repo/platform/engine/lib/cloud/client", () => ({ cloudClient: () => ({ analytics: { streamToken: h.streamToken, requests: h.requests } }) })); @@ -27,6 +26,8 @@ vi.mock("@repo/platform/engine/lib/ssh-manager", () => ({ sshManager: {} })); vi.mock("@repo/platform/engine/lib/openresty-paths", () => ({ getOpenRestyPaths: vi.fn() })); vi.mock("@repo/platform/engine/lib/project-analytics", () => ({ resolveProjectTrafficSource: h.source, resolveProjectTrafficSources: h.sources, +})); +vi.mock("@repo/platform/engine/lib/edge-management", () => ({ fetchMgmt: h.fetchMgmt, mgmtStream: vi.fn(), probeMgmt: vi.fn(), })); import { getProjectHome } from "@repo/platform/engine/modules/projects/project-home.operations"; diff --git a/apps/api/test/modules/projects/shared-service-connections.test.ts b/apps/api/test/modules/projects/shared-service-connections.test.ts index fc4d00f0b..1df8a5fb8 100644 --- a/apps/api/test/modules/projects/shared-service-connections.test.ts +++ b/apps/api/test/modules/projects/shared-service-connections.test.ts @@ -66,9 +66,10 @@ beforeEach(() => { describe("connections to a service inside a project", () => { it("offers all eligible projects while excluding the current project and read-only access", async () => { - h.candidates = ["source", "read-only", ...Array.from({ length: 120 }, (_, i) => `consumer-${i}`)].map(id => ({ id, name: id, environmentName: "production", appTemplateId: null })); + h.candidates = ["source", "read-only", ...Array.from({ length: 120 }, (_, i) => `consumer-${i}`)].map(id => ({ id, name: id, environmentName: "production", appTemplateId: null, iconUrl: "https://cdn.example.test/icon.svg", framework: "nextjs" })); const choices = await listConnectionCandidates(ctx, "source"); expect(choices).toHaveLength(120); + expect(choices[0]).toMatchObject({ iconUrl: "https://cdn.example.test/icon.svg", framework: "nextjs" }); expect(choices.some(choice => ["source", "read-only"].includes(choice.id))).toBe(false); expect(h.authorize).toHaveBeenCalledWith(ctx, { resourceType: "project", resourceId: "source", action: "write" }); }); diff --git a/apps/api/test/modules/route-rules/route-rule-push.test.ts b/apps/api/test/modules/route-rules/route-rule-push.test.ts index 947253175..0b1ec36ee 100644 --- a/apps/api/test/modules/route-rules/route-rule-push.test.ts +++ b/apps/api/test/modules/route-rules/route-rule-push.test.ts @@ -1,6 +1,6 @@ import { diagnostics as errorDiagnostics } from "@repo/core/diagnostics"; import { describe, expect, it, vi, beforeEach } from "vitest"; -import type { RouteRuleSpec } from "@repo/core"; +import { normalizeEdgeResponse, type RouteRuleSpec } from "@repo/core"; /** * The DB is the source of truth for route rules; the edge only knows what was last @@ -41,8 +41,8 @@ vi.mock("@repo/db", async (importOriginal) => ({ }, })); -vi.mock("@repo/platform/engine/lib/project-analytics", async (importOriginal) => ({ - ...(await importOriginal()), +vi.mock("@repo/platform/engine/lib/edge-management", async (importOriginal) => ({ + ...(await importOriginal()), postEdgeMgmt, })); @@ -88,7 +88,7 @@ function capturedPushes() { beforeEach(() => { vi.clearAllMocks(); - postEdgeMgmt.mockResolvedValue(undefined); + postEdgeMgmt.mockResolvedValue({ ok: true, staticResponses: true }); // pushProjectRules now loads the project too, for the organizationId that resolves a // null serverId to the "This Server" row. The real repo always returns a promise; an // unset mock returns undefined and blows up on `.catch`. @@ -171,6 +171,38 @@ describe("serializeProjectRules", () => { }); describe("pushProjectRules", () => { + it("checks response support before publishing and keeps the response bound to its domain", async () => { + const response = { response: normalizeEdgeResponse({ body: "ready" }) }; + listByProject.mockResolvedValue([rule({ domainId: "d1", pathPrefix: "/health", spec: response })]); + listDomains.mockResolvedValue([domain("d1", "a.com"), domain("d2", "b.com")]); + await expect(pushProjectRules("p1", "srv1")).resolves.toBe(true); + const calls = capturedPushes(); + expect(calls.findIndex(call => call.path === "/rules/capabilities")).toBeLessThan( + calls.findIndex(call => call.path === "/rules" && call.body.host === "a.com")); + expect(calls.find(call => call.body.host === "a.com")?.body.rules).toEqual([{ pathPrefix: "/health", spec: response }]); + expect(calls.find(call => call.body.host === "b.com")?.body.rules).toEqual([]); + }); + + it.each([null, { ok: true }, { ok: true, staticResponses: false }])("does not send a response to an older edge: %j", async (capability) => { + listByProject.mockResolvedValue([rule(), rule({ id: "response", domainId: "d1", spec: { response: normalizeEdgeResponse({ body: "ready" }) } })]); + listDomains.mockResolvedValue([domain("d1", "a.com")]); + postEdgeMgmt.mockImplementation(async (_server, path) => path === "/rules/capabilities" ? capability : { ok: true }); + const warn = vi.spyOn(errorDiagnostics, "warn").mockImplementation(() => {}); + try { + await expect(pushProjectRules("p1", "srv1")).resolves.toBe(false); + expect(capturedPushes().find(call => call.path === "/rules")?.body.rules).toEqual([{ pathPrefix: null, spec: RL }]); + } finally { warn.mockRestore(); } + }); + + it("reports an unacknowledged push as pending", async () => { + listByProject.mockResolvedValue([]); + listDomains.mockResolvedValue([domain("d1", "a.com")]); + postEdgeMgmt.mockResolvedValue(null); + const warn = vi.spyOn(errorDiagnostics, "warn").mockImplementation(() => {}); + try { await expect(pushProjectRules("p1", "srv1")).resolves.toBe(false); } + finally { warn.mockRestore(); } + }); + it("pushes every current host, even the ones with no rules", async () => { // An empty ruleset is how a host is CLEARED. Pushing only hosts that still have // rules is exactly the bug where a deleted rule keeps enforcing. @@ -212,7 +244,7 @@ describe("pushProjectRules", () => { postEdgeMgmt.mockRejectedValueOnce(new Error("tunnel closed")).mockResolvedValue({ ok: true }); const warn = vi.spyOn(errorDiagnostics, "warn").mockImplementation(() => {}); - await expect(pushProjectRules("p1", "srv1")).resolves.toBeUndefined(); + await expect(pushProjectRules("p1", "srv1")).resolves.toBe(false); expect(postEdgeMgmt).toHaveBeenCalledTimes(2); expect(warn).toHaveBeenCalled(); warn.mockRestore(); diff --git a/apps/api/test/modules/route-rules/rules-lua-contract.test.ts b/apps/api/test/modules/route-rules/rules-lua-contract.test.ts index 23c058eb6..4fc4e0ea7 100644 --- a/apps/api/test/modules/route-rules/rules-lua-contract.test.ts +++ b/apps/api/test/modules/route-rules/rules-lua-contract.test.ts @@ -202,7 +202,7 @@ describe("rules_guard.lua keeps the properties its comments promise", () => { it("returns early when the host has no rules (the common case)", () => { const head = guard.slice(0, guard.indexOf("local lib =")); - expect(head).toContain("if not raw then return end"); + expect(head).toContain("if not raw and not response_revision then return end"); }); it("picks the longest matching path prefix", () => { @@ -231,10 +231,8 @@ describe("rules_guard.lua keeps the properties its comments promise", () => { expect(guard).not.toContain("http_x_forwarded_for"); }); - it("never writes a rule value into a response header", () => { - const headerWrites = [...guard.matchAll(/ngx\.header\[[^\]]+\]\s*=\s*(.+)/g)].map((m) => m[1]!); - expect(headerWrites).toEqual(['"1"']); // Retry-After only, a literal - }); + // Literal response headers now have an explicit validator. Header injection + // and access-policy preservation are exercised by routing-rules.e2e.test.ts. it("applies access before ban before hotlink before rate limit", () => { // After resolving the country header, keep the cheaper access/ban checks diff --git a/apps/api/test/modules/services/application-identity.test.ts b/apps/api/test/modules/services/application-identity.test.ts new file mode 100644 index 000000000..5cef0a7f7 --- /dev/null +++ b/apps/api/test/modules/services/application-identity.test.ts @@ -0,0 +1,101 @@ +import { beforeEach, expect, it, vi } from "vitest"; +const mocks = vi.hoisted(() => ({ project: vi.fn(), services: vi.fn(), env: vi.fn() })); +vi.mock("@repo/db", async (original) => { + const actual = await original(); + return { + ...actual, + repos: { + ...actual.repos, + project: { ...actual.repos.project, findById: mocks.project, listEnvVars: mocks.env }, + service: { ...actual.repos.service, listByProject: mocks.services }, + }, + }; +}); +import { listServices } from "@repo/platform/engine/modules/services/service.service"; +const project = { + id: "p", + organizationId: "org", + slug: "website", + framework: "nextjs", + rootDirectory: ".", + buildCommand: "bun run build", + port: 3000, + hasServer: true, + workloadType: "web", +}; +const app = { + id: "app", + projectId: "p", + name: "website", + kind: "monorepo", + enabled: true, + sortOrder: -1, + rootDirectory: ".", + environment: {}, + volumes: [], + advanced: {}, + buildArgs: {}, +}; +const database = { + id: "db", + projectId: "p", + name: "postgres", + kind: "compose", + image: "postgres:17", + enabled: true, + sortOrder: 0, + exposed: false, + environment: { PASSWORD: "private-value" }, +}; +const context = { organizationId: "org" } as never; +beforeEach(() => { + mocks.project.mockResolvedValue(project); + mocks.services.mockResolvedValue([app, database]); + mocks.env.mockResolvedValue([]); +}); +it("exposes one stable project application when the first database is added", async () => { + const rows = await listServices(context, "p"); + expect(rows.map((s) => [s.id, s.applicationMode])).toEqual([ + ["app", "project"], + ["db", undefined], + ]); + expect(mocks.env).toHaveBeenCalledWith("p", undefined, "app"); + expect(JSON.stringify(rows)).not.toContain("private-value"); +}); +it("preserves the application identity after removing the last companion", async () => { + mocks.services.mockResolvedValue([app]); + expect((await listServices(context, "p"))[0].applicationMode).toBe("project"); +}); +it.each([ + ["saved runtime override", app, [{ key: "CUSTOM", value: "keep" }]], + ["Compose value", { ...app, environment: { CUSTOM: "keep" } }, []], + ["custom build", { ...app, buildCommand: "custom-build" }, []], + ["renamed app", { ...app, name: "renamed" }, []], + ["disabled app", { ...app, enabled: false }, []], +])("retains service ownership for %s", async (_name, row, env) => { + mocks.services.mockResolvedValue([row, database]); + mocks.env.mockResolvedValue(env); + expect((await listServices(context, "p"))[0].applicationMode).toBe("service"); +}); +it.each([ + { ...project, framework: "docker-compose" }, + { ...project, composePath: "compose.yml" }, +])("does not collapse a real Compose app into the project", async (shape) => { + mocks.project.mockResolvedValue(shape); + expect((await listServices(context, "p")).every((s) => s.applicationMode === undefined)).toBe( + true, + ); +}); +it("does not invent an identity for a regular monorepo application", async () => { + mocks.services.mockResolvedValue([{ ...app, sortOrder: 0 }, database]); + expect((await listServices(context, "p"))[0].applicationMode).toBeUndefined(); +}); + +it.each([ + { workspaceId: "cloud-workspace" }, + { clusterId: "cluster" }, + { workloadType: "static", hasServer: false }, +])("keeps service controls for a runtime outside the primary app pipeline", async (shape) => { + mocks.project.mockResolvedValue({ ...project, ...shape }); + expect((await listServices(context, "p"))[0].applicationMode).toBe("service"); +}); diff --git a/apps/api/test/modules/system/analytics-scrape-sweep.test.ts b/apps/api/test/modules/system/analytics-scrape-sweep.test.ts index ef0d25e3e..40eb4a913 100644 --- a/apps/api/test/modules/system/analytics-scrape-sweep.test.ts +++ b/apps/api/test/modules/system/analytics-scrape-sweep.test.ts @@ -51,14 +51,14 @@ vi.mock("@repo/platform/engine/lib/system-debug", () => ({ formatDuration: () => "1ms", })); -vi.mock("@repo/platform/engine/lib/project-analytics", () => ({ +vi.mock("@repo/platform/engine/lib/edge-management", () => ({ probeMgmt: vi.fn(async (serverId: string) => { h.scraped.push(serverId); if (h.failing.has(serverId)) throw new Error("ssh refused"); return true; }), fetchMgmt: vi.fn(async () => h.totals), - postMgmt: vi.fn(async () => ({ buckets: [], flushed: 0 })), + postMgmtJson: vi.fn(async () => ({ buckets: [], flushed: 0 })), })); const { runAnalyticsScrapeSweep } = await import("@repo/platform/engine/modules/system/analytics-scraper"); @@ -98,7 +98,7 @@ describe("runAnalyticsScrapeSweep", () => { it("goes one server at a time — a 50-box control plane must not connect to all at once", async () => { let concurrent = 0; let peak = 0; - const { probeMgmt } = await import("@repo/platform/engine/lib/project-analytics"); + const { probeMgmt } = await import("@repo/platform/engine/lib/edge-management"); (probeMgmt as unknown as { mockImplementation: (f: unknown) => void }).mockImplementation( async (serverId: string) => { concurrent += 1; diff --git a/apps/api/test/modules/system/server-exec-authorization.test.ts b/apps/api/test/modules/system/server-exec-authorization.test.ts new file mode 100644 index 000000000..754a3fcb9 --- /dev/null +++ b/apps/api/test/modules/system/server-exec-authorization.test.ts @@ -0,0 +1,48 @@ +import "../jobs/_env"; +import { beforeEach, expect, it, vi } from "vitest"; +import { Hono } from "hono"; +import { and, eq } from "@repo/db"; + +const host = vi.hoisted(() => ({ execute: vi.fn(), command: vi.fn() })); +vi.mock("@repo/platform/engine/lib/server-execution", async original => ({ + ...await original(), + withServerExecution: host.execute, +})); +vi.mock("@repo/platform/engine/lib/agent-exec", () => ({ execOnHost: host.command })); + +import { db, schema, seedOwner } from "../jobs/_harness"; +import { serverResourceRoutes } from "../../../src/modules/system/server-resource.routes"; +import { handleApiError } from "../../../src/middleware/error-handler"; + +const app = new Hono().onError(handleApiError).route("/api/system", serverResourceRoutes); +beforeEach(() => { + vi.clearAllMocks(); + host.command.mockResolvedValue({ output: "probe", exitCode: 0, timedOut: false, truncated: false, durationMs: 1 }); + host.execute.mockImplementation(async (_org, _id, work) => work({})); +}); + +it("rejects an authenticated member before host dispatch and preserves administrator execution", async () => { + const actor = await seedOwner(); + const id = `server-${actor.userId}`; + await db.insert(schema.servers).values({ id, organizationId: actor.orgId, name: "Fixture host", sshHost: "203.0.113.10", sshUser: "root" }); + const membership = and(eq(schema.member.userId, actor.userId), eq(schema.member.organizationId, actor.orgId)); + const execute = (serverId = id) => app.request(`/api/system/servers/${serverId}/exec`, { + method: "POST", headers: { ...actor.auth, "Content-Type": "application/json" }, body: JSON.stringify({ command: "id" }), + }); + await db.update(schema.member).set({ role: "member" }).where(membership); + expect((await execute()).status).toBe(404); + expect(host.execute).not.toHaveBeenCalled(); + expect(host.command).not.toHaveBeenCalled(); + await db.update(schema.member).set({ role: "admin" }).where(membership); + expect((await execute()).status).toBe(200); + expect(host.execute).toHaveBeenCalledOnce(); + expect(host.command).toHaveBeenCalledOnce(); + const other = await seedOwner(); + const otherServer = `server-${other.userId}`; + await db.insert(schema.servers).values({ id: otherServer, organizationId: other.orgId, name: "Other organization", sshHost: "203.0.113.11" }); + expect((await execute(otherServer)).status).toBe(404); + expect(host.command).toHaveBeenCalledOnce(); + await db.update(schema.member).set({ role: "member" }).where(membership); + expect((await execute()).status).toBe(404); + expect(host.command).toHaveBeenCalledOnce(); +}); diff --git a/apps/api/test/modules/system/server-managed-controls.test.ts b/apps/api/test/modules/system/server-managed-controls.test.ts index aeea020f2..364e1abd0 100644 --- a/apps/api/test/modules/system/server-managed-controls.test.ts +++ b/apps/api/test/modules/system/server-managed-controls.test.ts @@ -72,11 +72,11 @@ let workloads: Map; let vm: any; let handle: any; -const jwt = (id = vmId, type = "workspace") => +const jwt = (id = vmId, type = "vm") => `e30.${Buffer.from( JSON.stringify({ type, - workspace_id: id, + vm_id: id, token: runtimeToken, exp: Math.floor(Date.now() / 1000) + 3600, }), @@ -261,8 +261,8 @@ beforeEach(async () => { { ip: "8.8.4.4", countryCode: "US" }, { ip: "1.1.1.1", countryCode: "AU" }, ]), - workspaces: { get: vi.fn(async () => vm), invalidateRuntime: vi.fn() }, - workspace: vi.fn(() => handle), + vms: { get: vi.fn(async () => vm), invalidateRuntime: vi.fn() }, + vm: vi.fn(() => handle), }; h.namespaceClient.mockImplementation(async () => ({ client: h.client, namespace })); h.canWork.mockResolvedValue(undefined); @@ -402,7 +402,7 @@ describe("managed server controls through real HTTP, policy and SQL ownership", ).toBe(400); expect(handle.ssh.setPassword).not.toHaveBeenCalled(); }); - it("reveals only a workspace gateway token and fences repeat rotations", async () => { + it("reveals only a VM gateway token and fences repeat rotations", async () => { const revealed = await request("managed/runtime-api/credential", "POST", { confirm: true }); expect(revealed.status, JSON.stringify(revealed.body)).toBe(200); expect(revealed.body.endpoint).toBe("https://runtime.example.test"); @@ -414,12 +414,19 @@ describe("managed server controls through real HTTP, policy and SQL ownership", "MANAGED_RUNTIME_TOKEN_CHANGED", ); expect(handle.apiAccess.rotateToken).toHaveBeenCalledTimes(1); - expect(h.client.workspaces.invalidateRuntime).toHaveBeenCalledWith(vmId); + expect(h.client.vms.invalidateRuntime).toHaveBeenCalledWith(vmId); handle.apiAccess.getToken.mockResolvedValue({ token: jwt("another-vm") }); expect( (await request("managed/runtime-api/credential", "POST", { confirm: true })).status, ).toBe(502); }); + it.each(["workspace", "namespace", "account"])("rejects %s credentials even with the selected VM ID", async (type) => { + handle.apiAccess.getToken.mockResolvedValue({ token: jwt(vmId, type) }); + const result = await request("managed/runtime-api/credential", "POST", { confirm: true }); + expect(result.status).toBe(502); + expect(result.body.code).toBe("MANAGED_CREDENTIAL_UNAVAILABLE"); + expect(JSON.stringify(result.body)).not.toContain(runtimeToken); + }); it("keeps the original network response for clients that do not request diagnostics", async () => { const legacy = { internetAccess: true, ingressPorts: [80, 9990] }; expect((await request("network-settings")).body).toEqual(legacy); @@ -482,7 +489,7 @@ describe("managed server controls through real HTTP, policy and SQL ownership", "MANAGED_RUNTIME_TOKEN_CHANGED", ); expect(handle.apiAccess.rotateToken).toHaveBeenCalledTimes(1); - expect(h.client.workspaces.invalidateRuntime).toHaveBeenCalledWith(vmId); + expect(h.client.vms.invalidateRuntime).toHaveBeenCalledWith(vmId); const recovered = await request("managed/runtime-api/credential", "POST", { confirm: true }); expect(recovered.status).toBe(200); expect(recovered.body.revision).not.toBe(input.expectedRevision); diff --git a/apps/api/test/modules/tokens/mint-org-binding.test.ts b/apps/api/test/modules/tokens/mint-org-binding.test.ts index 88e421d30..b0998efd1 100644 --- a/apps/api/test/modules/tokens/mint-org-binding.test.ts +++ b/apps/api/test/modules/tokens/mint-org-binding.test.ts @@ -188,6 +188,16 @@ beforeEach(() => { }); describe("finding 1: grant width is checked in the org the binding is written to", () => { + it("refuses server administration grants for an ordinary member", async () => { + const grants = [{ resourceType: "server", resourceId: "*", permissions: ["admin"] }]; + const refused = await authorize(OWNER_ORG, { clientId: "cli", organizationId: VICTIM_ORG, grants }); + expect(refused.status).toBe(403); + expect(refused.body.code).toBe("GRANT_EXCEEDS_ACCESS"); + expect(upsertOAuthBindingWithGrants).not.toHaveBeenCalled(); + const allowed = await authorize(OWNER_ORG, { clientId: "cli", organizationId: OWNER_ORG, grants }); + expect(allowed.status).toBe(200); + }); + /** * `billing` and `audit` are the two types `roleAllowsResourceType` denies a * member, and both are grantable — so they isolate "authority comes from the diff --git a/apps/api/tsconfig.json b/apps/api/tsconfig.json index 7c483898a..94a494d7e 100644 --- a/apps/api/tsconfig.json +++ b/apps/api/tsconfig.json @@ -15,6 +15,6 @@ "declaration": false, "declarationMap": false }, - "include": ["src/**/*.ts"], + "include": ["src/**/*.ts", "scripts/cloud-readiness.ts"], "exclude": ["node_modules", "dist"] } diff --git a/apps/cli/package.json b/apps/cli/package.json index a13d32459..f21acbce9 100644 --- a/apps/cli/package.json +++ b/apps/cli/package.json @@ -1,6 +1,6 @@ { "name": "@repo/cli", - "version": "0.9.11", + "version": "0.9.12", "description": "Openship CLI - deploy, manage, and open your Openship platform from the terminal", "license": "Apache-2.0", "repository": { diff --git a/apps/cli/src/commands/domain.ts b/apps/cli/src/commands/domain.ts index 5661d5b14..9b6011a8c 100644 --- a/apps/cli/src/commands/domain.ts +++ b/apps/cli/src/commands/domain.ts @@ -9,7 +9,7 @@ import { exitCommand, rethrowCommandExit } from "../lib/command-exit"; import { Command, Option } from "commander"; import { UploadCertBody, parseInput } from "@repo/contracts"; import { readFileSync } from "node:fs"; -import { attachCustomDomainEndpoint } from "../lib/custom-domain-endpoint"; +import { prepareCustomDomainEndpoint } from "../lib/custom-domain-endpoint"; import { getShipClient } from "../lib/ship-client"; import { spin, fail, confirmOrExit, printResult } from "../lib/cmd-helpers"; import type { Domain, DomainRecords, DomainSsl } from "@repo/sdk/client"; @@ -71,14 +71,17 @@ const addCmd = new Command("add") .argument("", "Domain hostname (e.g. app.example.com)") .requiredOption("-p, --project ", "Project ID to attach the domain to") .option("--primary", "Mark this domain as the project's primary", false) + .option("--service ", "Target service within the selected project") + .option("--port ", "Container port to route (required for ambiguous service ports)", positiveInteger) .action(async (hostname: string, opts) => { const sp = spin(`Adding ${hostname}…`); try { const ship = getShipClient(); + const attach = await prepareCustomDomainEndpoint(ship, opts.project, { service: opts.service, port: opts.port }); const res = await ship.domains.create(opts.project, { hostname, isPrimary: !!opts.primary }); - const routed = await attachCustomDomainEndpoint(ship, opts.project, res.domain.hostname); + await attach(res.domain.hostname); sp?.succeed(`Added ${res.domain.hostname}`); - if (routed) info(` ${res.domain.hostname} is configured to be routed on the next deploy.`); + info(` ${res.domain.hostname} is configured to be routed on the next deploy.`); if (isJsonMode()) { printJson(res); return; diff --git a/apps/cli/src/commands/login.ts b/apps/cli/src/commands/login.ts index e8da11cd3..fb5e18403 100644 --- a/apps/cli/src/commands/login.ts +++ b/apps/cli/src/commands/login.ts @@ -18,6 +18,7 @@ import { fetchCaps } from "../lib/caps"; import { err, info, isJsonMode, ok, printJson } from "../lib/output"; import { exitCommand } from "../lib/command-exit"; import { readSecret } from "../lib/command-input"; +import { discoverPublicInstanceApi } from "../lib/public-instance-api"; export interface LoginOptions { token?: string; @@ -80,7 +81,7 @@ export async function runLogin(options: LoginOptions = {}): Promise { const existing = getContext(contextName); const local = !opts.cloud && !opts.apiUrl && !existing.apiUrl ? localConnectionEndpoints() : undefined; - const apiUrl = opts.cloud ? CLOUD_API_URL : opts.apiUrl || existing.apiUrl || local!.apiUrl; + let apiUrl = opts.cloud ? CLOUD_API_URL : opts.apiUrl || existing.apiUrl || local!.apiUrl; const normalizedApi = new HttpClient({ baseUrl: apiUrl }).apiUrl; const sameEndpoint = existing.apiUrl && new HttpClient({ baseUrl: existing.apiUrl }).apiUrl === normalizedApi; @@ -89,7 +90,7 @@ export async function runLogin(options: LoginOptions = {}): Promise { : opts.dashboardUrl || (sameEndpoint ? existing.dashboardUrl : undefined) || local?.dashboardUrl || - normalizedApi.slice(0, -4); + normalizedApi.slice(0, -4).replace(/\/api\/proxy$/, ""); let token: string | undefined = opts.tokenFile ? readSecret(opts.tokenFile) : opts.token; @@ -132,13 +133,23 @@ export async function runLogin(options: LoginOptions = {}): Promise { // Validate the token against an authenticated endpoint before storing. // 200 → valid; 403 → valid but lacks settings:read scope (still usable). let scoped = false; - try { - const client = new OpenshipClient({ baseUrl: apiUrl, token, timeoutMs: 8000 }); - await client.tokens.list(); - } catch (error) { - if (error instanceof ApiError && error.status === 403) { - scoped = true; - } else { + for (let attempt = 0; ; attempt++) { + try { + const client = new OpenshipClient({ baseUrl: apiUrl, token, timeoutMs: 8000 }); + await client.tokens.list(); + break; + } catch (error) { + if (error instanceof ApiError && error.status === 403) { + scoped = true; + break; + } + // A missing/non-JSON route can mean the supplied URL is the dashboard. + // An actual authentication refusal must never switch endpoints. + if (attempt === 0 && !opts.cloud && error instanceof ApiError && + (error.status === 404 || error.message === "Invalid JSON response")) { + const discovered = await discoverPublicInstanceApi(apiUrl); + if (discovered) { apiUrl = discovered; continue; } + } err( error instanceof ApiError && error.status === 401 ? "Token rejected by the API. Check that it is valid and not revoked." diff --git a/apps/cli/src/commands/service.ts b/apps/cli/src/commands/service.ts index 9f830de06..5243bdc7a 100644 --- a/apps/cli/src/commands/service.ts +++ b/apps/cli/src/commands/service.ts @@ -25,6 +25,7 @@ import { getShipClient, ApiError, hasShipCredentials } from "../lib/ship-client" import { isJsonMode, printJson, printTable, ok, err, info } from "../lib/output"; import { collect, parsePairs, readJsonInput } from "../lib/command-input"; import { fail, confirmOrExit, reportResult, reportExecResult } from "../lib/cmd-helpers"; +import { resolveServiceRef } from "../lib/service-ref"; // ─── Shared helpers ────────────────────────────────────────────────────────── @@ -86,19 +87,7 @@ async function listServices(projectId: string): Promise { /** Resolve a service ref (name or svc_ id) to its row within the stack. */ async function resolveService(projectId: string, ref: string): Promise { - if (/^svc_/.test(ref)) return getShipClient().services.get(projectId, ref); - const services = await listServices(projectId); - const byId = services.find((s) => s.id === ref); - if (byId) return byId; - const byName = services.filter((s) => s.name === ref); - if (byName.length === 1) return byName[0]; - if (byName.length > 1) { - err(` "${ref}" matches ${byName.length} services — pass the service id (svc_…).`); - exitCommand(1); - } - const known = services.map((s) => s.name).join(", ") || "(none)"; - err(` No service "${ref}" in this stack. Services: ${known}`); - exitCommand(1); + return resolveServiceRef(getShipClient(), projectId, ref); } // ─── list / get ─────────────────────────────────────────────────────────────── diff --git a/apps/cli/src/commands/up.ts b/apps/cli/src/commands/up.ts index 51864365e..5af9fdc1a 100644 --- a/apps/cli/src/commands/up.ts +++ b/apps/cli/src/commands/up.ts @@ -11,7 +11,7 @@ import { dirname, join } from "node:path"; import { fileURLToPath } from "node:url"; import { ensureDashboard } from "../lib/dashboard"; -import { installAndStart } from "../lib/service"; +import { installAndStart, serviceHeldPorts } from "../lib/service"; import { composeUp, composePrefetch, @@ -44,6 +44,7 @@ import { LocalExecutor } from "@repo/adapters"; import { resolveInstallInputs, headlessProvision, + provisionInstanceDomain, HeadlessInputError, } from "../lib/instance-provision"; import { mintBareInternalToken } from "../lib/internal-token"; @@ -284,6 +285,26 @@ export const upCommand = new Command("up") if (opts.fromSource || opts.source) return runFromSource(opts); if (opts.foreground) return runForeground(opts); const headless = !!(opts.nonInteractive || opts.yes); + // Validate requested provisioning before pulling images or stopping services. + if (headless || opts.managedEdge) { + try { + if (opts.managedEdge) { + opts.publicUrl = effectivePublicUrl(opts.publicUrl); + if (!opts.publicUrl || new URL(opts.publicUrl).protocol !== "https:") + throw new HeadlessInputError("--managed-edge requires --public-url https://your-domain (or an existing saved HTTPS URL)."); + if (opts.domainKind && opts.domainKind !== "custom") + throw new HeadlessInputError("--managed-edge requires --domain-kind custom."); + if (opts.hostname && opts.hostname !== new URL(opts.publicUrl).hostname) + throw new HeadlessInputError("--hostname must match --public-url when using --managed-edge."); + opts.trustProxy = true; + } + if (headless) upInstallInputs(opts); + } catch (error) { + reportCliMessage("error", (error as Error).message); + process.exit(1); + } + } + // Install method: Compose is the default when it can actually work (Docker on // Linux — the edge container needs host networking); else bare. `planMethod` // owns the decision so `--dry-run` predicts the same one. @@ -319,9 +340,10 @@ export const upCommand = new Command("up") if (!token) { reportCliMessage("warn", chalk.yellow( - "\n Couldn't read the stack's internal token (compose/.env) — create the admin from the dashboard.\n", + "\n Couldn't read the stack's internal token (compose/.env). Run this command as the installation owner (or sudo) to finish admin setup.\n", ), ); + process.exit(1); } else { await runHeadlessProvision( opts, @@ -329,6 +351,18 @@ export const upCommand = new Command("up") { token, method: "compose" }, ); } + } else if (opts.managedEdge) { + const result = await provisionInstanceDomain({ + port: started.apiPort, dashPort: started.dashPort, token: composeInternalToken() ?? undefined, + domain: { kind: "custom", hostname: new URL(opts.publicUrl!).hostname, acmeEmail: opts.acmeEmail, + edge: opts.edge === "takeover" || opts.edge === "migrate" ? opts.edge : "cancel" }, + onLog: message => console.log(chalk.dim(` ${message}`)), + }); + for (const warning of result.warnings) reportCliMessage("warn", warning); + if (!result.domainRegistered) { + reportCliMessage("error", "Managed domain registration did not complete. If this is a new install, run `openship` and choose Create first admin, then retry this command."); + process.exit(1); + } } return; } @@ -336,6 +370,21 @@ export const upCommand = new Command("up") if (headless) await runHeadlessProvision(opts, started, { method: "bare" }); }); +/** Flags and preflight share one provisioning interpretation. */ +function upInstallInputs(opts: UpOpts) { + return resolveInstallInputs({ + adminName: opts.adminName, + adminEmail: opts.adminEmail, + adminPassword: opts.adminPassword, + domainKind: opts.domainKind ?? (opts.managedEdge ? "custom" : undefined), + hostname: opts.hostname, + slug: opts.slug, + publicUrl: opts.publicUrl, + acmeEmail: opts.acmeEmail, + edge: opts.edge, + }); +} + /** * Headless install (bare service): after `startService` installs + supervises * the process, create the admin + register the domain from the flags, so a box @@ -349,17 +398,7 @@ export async function runHeadlessProvision( ): Promise { let inputs; try { - inputs = resolveInstallInputs({ - adminName: opts.adminName, - adminEmail: opts.adminEmail, - adminPassword: opts.adminPassword, - domainKind: opts.domainKind, - hostname: opts.hostname, - slug: opts.slug, - publicUrl: opts.publicUrl, - acmeEmail: opts.acmeEmail, - edge: opts.edge, - }); + inputs = upInstallInputs(opts); } catch (err) { if (err instanceof HeadlessInputError) { reportCliMessage("error", chalk.red(`\n ${err.message}\n`)); @@ -377,8 +416,10 @@ export async function runHeadlessProvision( method: extra?.method, onLog: (m) => console.log(chalk.dim(` ${m}`)), }); - console.log(chalk.green(`\n ✓ Openship provisioned${result.liveUrl ? `: ${result.liveUrl}` : "."}`)); for (const w of result.warnings) reportCliMessage("warn", chalk.yellow(` ⚠ ${w}`)); + if (opts.managedEdge && !result.domainRegistered) + throw new Error("Managed domain registration did not complete. Retry after resolving the domain warning above."); + console.log(chalk.green(`\n ✓ Openship provisioned${result.liveUrl ? `: ${result.liveUrl}` : "."}`)); } catch (err) { observeCaughtError(err, "cli/commands/up"); reportCliMessage("error", chalk.red(`\n Headless provisioning failed: ${(err as Error).message}\n`)); @@ -641,7 +682,7 @@ async function runCompose(opts: UpOpts & { yes?: boolean }): Promise<{ apiPort: chalk.dim(" Images: api + dashboard + edge (OpenResty on :80/:443)\n") + chalk.dim(" Manage: openship stop · openship update · openship status\n") + // In headless mode the admin is bootstrapped below — don't tell the user to do it by hand. - (headless ? "" : chalk.dim(" Create an admin: open the dashboard and register the first account.\n")), + (headless ? "" : chalk.dim(" Admin setup: run `openship` → Create first admin, or use `openship up --non-interactive --admin-email ` with OPENSHIP_ADMIN_PASSWORD set.\n")), ); return { apiPort: res.apiPort, dashPort: res.dashPort }; } @@ -688,9 +729,13 @@ export async function startService( // No permanent port: switch off any occupied default / flag / remembered port // BEFORE writing the service unit, so the chosen ports are baked into its args. + // A re-run finds our own running service on its ports. installAndStart replaces + // that instance, so they're reclaimable rather than busy; otherwise every re-run + // waited out waitPortFree and then moved the install to new ports (#1570). const resolved = await resolvePorts({ api: opts.port ? Number(opts.port) : undefined, dashboard: opts.dashboardPort ? Number(opts.dashboardPort) : undefined, + reclaimable: await serviceHeldPorts(), }); const port = String(resolved.api); const dashPort = String(resolved.dashboard); diff --git a/apps/cli/src/commands/wizard.ts b/apps/cli/src/commands/wizard.ts index 814fe0f28..ba716acb0 100644 --- a/apps/cli/src/commands/wizard.ts +++ b/apps/cli/src/commands/wizard.ts @@ -1231,7 +1231,7 @@ export async function runControl(): Promise { `${chalk.dim("Dashboard".padEnd(11))}${dashUrl}\n` + (ports.api ? `${chalk.dim("API".padEnd(11))}http://localhost:${ports.api}\n` : "") + `${chalk.dim("Manager".padEnd(11))}${managerLabel}`, - "Openship is already set up", + "Openship is installed", ); // Crash-looping on a corrupt DB is the one case where "Start" won't help — @@ -1279,6 +1279,7 @@ export async function runControl(): Promise { : { value: "start", label: "Start the service" }, { value: "stop", label: "Stop the service", hint: "won't restart on boot" }, ...(corrupted ? [] : [{ value: "repair", label: "Repair database", hint: "backup → heal a corrupt DB" }]), + { value: "create-admin", label: "Create first admin", hint: "finish a new install; existing accounts stay intact" }, { value: "reset", label: "Reset admin password", hint: "sets a local email + password login" }, { value: "reconfigure", label: "Re-run setup", hint: "reconfigure domain / cloud / admin" }, { value: "quit", label: "Quit" }, @@ -1287,6 +1288,16 @@ export async function runControl(): Promise { ); switch (action) { + case "create-admin": { + const admin = await promptLocalAdmin(); + const result = await bootstrapAdmin(apiPort, admin); + outro(result.ok + ? result.message === "already-exists" + ? "An admin already exists. Use Reset admin password if you need to recover access." + : `Admin created. Sign in at ${primaryUrl}.` + : `Could not create the admin: ${result.message}`); + return; + } case "repair": { const res = await runRepair(); outro(res.healed ? chalk.green(res.detail) : chalk.yellow(res.detail)); diff --git a/apps/cli/src/lib/custom-domain-endpoint.ts b/apps/cli/src/lib/custom-domain-endpoint.ts index 1b1c1a8cf..b979265ff 100644 --- a/apps/cli/src/lib/custom-domain-endpoint.ts +++ b/apps/cli/src/lib/custom-domain-endpoint.ts @@ -1,71 +1,79 @@ -import { safeErrorMessage } from "@repo/core"; +import { firstServicePort, resolveRedirectStatus, safeErrorMessage } from "@repo/core"; import type { UpdateProjectInput } from "@repo/contracts"; import type { CliShipClient } from "./ship-client"; -import { reportCliMessage } from "./output"; +import { resolveServiceRef } from "./service-ref"; type PublicEndpoint = NonNullable[number]; -/** Attach the routing endpoint after claiming the domain, as the dashboard does. */ +export interface CustomDomainTarget { service?: string; port?: number } + +/** Validate the target before claiming a hostname, then save through the same + * project/service API used by the dashboard. DNS verification and deployment + * remain explicit subsequent steps. */ +export async function prepareCustomDomainEndpoint( + ship: Pick, + projectId: string, + options: CustomDomainTarget = {}, +): Promise<(hostname: string) => Promise> { + const { project } = await ship.projects.getInfo(projectId); + if (!options.service && project.projectType !== "app") + throw new Error("This is a multi-service project. Pass --service and --port to select its route."); + if (!options.service && project.options.workloadType === "worker") + throw new Error("This worker has no public route. Configure a web workload before adding a domain."); + const service = options.service ? await resolveServiceRef(ship, projectId, options.service) : undefined; + const hasServer = !!service || project.options.hasServer; + const endpoints: PublicEndpoint[] = service?.publicEndpoints?.map(endpoint => ({ ...endpoint, redirectStatus: endpoint.redirectStatus !== undefined ? resolveRedirectStatus(endpoint.redirectStatus) : undefined })) ?? + (service ? [] : (project as typeof project & { publicEndpoints?: PublicEndpoint[] }).publicEndpoints ?? []); + const ports = service ? [...new Set([ + ...(service.ports ?? []).map(spec => firstServicePort([spec])), + ...(service.publicEndpoints ?? []).map(endpoint => endpoint.port), + Number(service.exposedPort), + ].filter((value): value is number => Number.isInteger(value) && Number(value) > 0 && Number(value) <= 65535))] : []; + const port = options.port ?? (service + ? ports.length === 1 ? ports[0] : undefined + : Number(project.options.productionPort || project.port)); + if (hasServer && (!Number.isInteger(port) || !port || port < 1 || port > 65535)) + throw new Error("Choose a valid container port with --port <1-65535>; services with multiple ports require an explicit target."); + if (!hasServer && options.port !== undefined) + throw new Error("A static project routes its output directory; --port requires a web service."); + + return async hostname => { + const host = hostname.trim().toLowerCase(); + try { + // Project info adds UI metadata. Send only the accepted save fields, keeping + // every existing target, hostname, redirect, and endpoint position intact. + const publicEndpoints: PublicEndpoint[] = endpoints.map((endpoint) => ({ + ...(endpoint.port !== undefined ? { port: endpoint.port } : {}), + ...(endpoint.targetPath !== undefined ? { targetPath: endpoint.targetPath } : {}), + ...(endpoint.domain !== undefined ? { domain: endpoint.domain } : {}), + ...(endpoint.customDomain !== undefined ? { customDomain: endpoint.customDomain } : {}), + ...(endpoint.domainType !== undefined ? { domainType: endpoint.domainType } : {}), + ...(endpoint.redirectTo !== undefined ? { redirectTo: endpoint.redirectTo } : {}), + ...(endpoint.redirectStatus !== undefined ? { redirectStatus: endpoint.redirectStatus } : {}), + })); + const endpoint = { domainType: "custom" as const, customDomain: host, ...(hasServer ? { port } : { targetPath: "/" }) }; + const index = publicEndpoints.findIndex(value => value.customDomain?.toLowerCase() === host); + if (index >= 0) { + if (options.port === undefined || publicEndpoints[index]!.port === port) return; + publicEndpoints[index] = { ...publicEndpoints[index], ...endpoint }; + } else publicEndpoints.push(endpoint); + if (service) await ship.services.update(projectId, service.id, { exposed: true, publicEndpoints }); + else await ship.projects.update(projectId, { publicEndpoints }); + } catch (error) { + throw new Error( + `Domain ${host} row still exists, but attaching its route failed: ${safeErrorMessage(error)}. Retry in the dashboard project ${projectId} Domains tab: configure this domain's target and save.`, + { cause: error }, + ); + } + }; +} + +/** Existing callers claim a domain before attaching its endpoint. */ export async function attachCustomDomainEndpoint( - ship: Pick, + ship: Pick, projectId: string, hostname: string, ): Promise { - const host = hostname.trim().toLowerCase(); - try { - const { project } = await ship.projects.getInfo(projectId); - const endpoints = - (project as typeof project & { publicEndpoints?: PublicEndpoint[] }).publicEndpoints ?? []; - if (endpoints.some((endpoint) => endpoint.customDomain?.toLowerCase() === host)) return true; - - if (project.options.workloadType === "worker") { - reportCliMessage( - "warn", - `Domain ${host} was added, but this worker has no public route. Configure a web workload and port before attaching it.`, - ); - return false; - } - - if (project.projectType !== "app") { - reportCliMessage( - "warn", - `Domain ${host} was added, but ${project.name || projectId} is a multi-service project, so routes belong to a service. Attach it to a service in the dashboard.`, - ); - return false; - } - - const hasServer = project.options.hasServer; - const port = Number(project.options.productionPort || project.port); - if (hasServer && (!Number.isInteger(port) || port < 1 || port > 65535)) { - reportCliMessage( - "warn", - `Domain ${host} was added, but the project has no valid server port and no public route. Configure a port in the dashboard Domains tab.`, - ); - return false; - } - - // Project info adds UI metadata. Send only the accepted save fields, keeping - // every existing target, hostname, redirect, and endpoint position intact. - const publicEndpoints: PublicEndpoint[] = endpoints.map((endpoint) => ({ - ...(endpoint.port !== undefined ? { port: endpoint.port } : {}), - ...(endpoint.targetPath !== undefined ? { targetPath: endpoint.targetPath } : {}), - ...(endpoint.domain !== undefined ? { domain: endpoint.domain } : {}), - ...(endpoint.customDomain !== undefined ? { customDomain: endpoint.customDomain } : {}), - ...(endpoint.domainType !== undefined ? { domainType: endpoint.domainType } : {}), - ...(endpoint.redirectTo !== undefined ? { redirectTo: endpoint.redirectTo } : {}), - ...(endpoint.redirectStatus !== undefined ? { redirectStatus: endpoint.redirectStatus } : {}), - })); - publicEndpoints.push({ - domainType: "custom", - customDomain: host, - ...(hasServer ? { port } : { targetPath: "/" }), - }); - await ship.projects.update(projectId, { publicEndpoints }); - return true; - } catch (error) { - throw new Error( - `Domain ${host} row still exists, but attaching its route failed: ${safeErrorMessage(error)}. Retry in the dashboard project ${projectId} Domains tab: configure this domain's target and save.`, - { cause: error }, - ); - } + await (await prepareCustomDomainEndpoint(ship, projectId))(hostname); + return true; } diff --git a/apps/cli/src/lib/host-channel-preflight.ts b/apps/cli/src/lib/host-channel-preflight.ts index b1ae9370e..dd88c8dbf 100644 --- a/apps/cli/src/lib/host-channel-preflight.ts +++ b/apps/cli/src/lib/host-channel-preflight.ts @@ -188,7 +188,7 @@ async function realProbe(host: string, port: number): Promise { "-e", `OPENSHIP_PROBE_PORT=${port}`, "api", - "bun", + "node", "-e", PROBE_SCRIPT, ], diff --git a/apps/cli/src/lib/instance-provision.ts b/apps/cli/src/lib/instance-provision.ts index b9801ebc7..33b13c440 100644 --- a/apps/cli/src/lib/instance-provision.ts +++ b/apps/cli/src/lib/instance-provision.ts @@ -268,8 +268,6 @@ export async function headlessProvision(opts: { }): Promise { const { port, inputs, token } = opts; const log = opts.onLog ?? (() => {}); - const warnings: string[] = []; - log("Waiting for the API to become healthy…"); if (!(await waitHealthy(port))) { throw new HeadlessInputError("The API did not become healthy in time — check `openship logs`."); @@ -279,8 +277,24 @@ export async function headlessProvision(opts: { const admin = await bootstrapOrReset(port, inputs.admin, token); if (!admin.ok) throw new HeadlessInputError(`Could not create the admin account: ${admin.message}`); + return { adminReady: true, ...await provisionInstanceDomain({ ...opts, domain: inputs.domain }) }; +} + +/** Register routing independently of credentials, including managed-edge + * re-runs on an already initialized Compose install. */ +export async function provisionInstanceDomain(opts: { + port: string; + dashPort?: string; + domain: InstallInputs["domain"]; + token?: string; + onLog?: (msg: string) => void; + onSlugTaken?: (taken: string) => Promise; +}): Promise> { + const { port, token } = opts; + const log = opts.onLog ?? (() => {}); + const warnings: string[] = []; const dashPort = opts.dashPort ? Number(opts.dashPort) : undefined; - const d = inputs.domain; + const d = opts.domain; let liveUrl: string | undefined; let domainRegistered = false; @@ -392,5 +406,5 @@ export async function headlessProvision(opts: { } } - return { adminReady: true, domainRegistered, liveUrl, warnings }; + return { domainRegistered, liveUrl, warnings }; } diff --git a/apps/cli/src/lib/public-instance-api.ts b/apps/cli/src/lib/public-instance-api.ts new file mode 100644 index 000000000..6df441097 --- /dev/null +++ b/apps/cli/src/lib/public-instance-api.ts @@ -0,0 +1,21 @@ +import { SDK_CAPABILITIES } from "@repo/contracts"; +import { HttpClient } from "@repo/sdk/client"; + +/** A public dashboard may mount the API beneath /api/proxy. Probe without + * credentials and never follow redirects or infer another origin. */ +export async function discoverPublicInstanceApi(baseUrl: string): Promise { + const base = new HttpClient({ baseUrl }).apiUrl.slice(0, -4); + if (/\/(?:api|api\/proxy)$/.test(new URL(baseUrl).pathname.replace(/\/+$/, ""))) return null; + const candidate = `${base}/api/proxy`; + try { + const response = await fetch(`${candidate}/api/health`, { + headers: { Accept: "application/json" }, redirect: "error", signal: AbortSignal.timeout(3_000), + }); + if (!response.ok) return null; + const health = await response.json() as { status?: string; sdk?: { protocol?: number } }; + return health?.status === "ok" && health.sdk?.protocol === SDK_CAPABILITIES.protocol ? candidate : null; + } catch { + // diagnostics-ignore: Optional credential-free discovery; keep the original login failure when this probe cannot identify an API. + return null; + } +} diff --git a/apps/cli/src/lib/service-ref.ts b/apps/cli/src/lib/service-ref.ts new file mode 100644 index 000000000..619ebbc17 --- /dev/null +++ b/apps/cli/src/lib/service-ref.ts @@ -0,0 +1,13 @@ +import type { CliShipClient } from "./ship-client"; + +/** Resolve only within the selected project; never fall back to another stack. */ +export async function resolveServiceRef(ship: Pick, projectId: string, ref: string) { + if (/^svc_/.test(ref)) return ship.services.get(projectId, ref); + const services = await ship.services.list(projectId); + const byId = services.find(service => service.id === ref); + if (byId) return byId; + const matches = services.filter(service => service.name === ref); + if (matches.length === 1) return matches[0]!; + if (matches.length > 1) throw new Error(`"${ref}" matches ${matches.length} services — pass the service id (svc_…).`); + throw new Error(`No service "${ref}" in this stack. Services: ${services.map(service => service.name).join(", ") || "(none)"}`); +} diff --git a/apps/cli/src/lib/service.ts b/apps/cli/src/lib/service.ts index b807497ba..254594a1c 100644 --- a/apps/cli/src/lib/service.ts +++ b/apps/cli/src/lib/service.ts @@ -15,6 +15,7 @@ import { spawnSync } from "node:child_process"; import { existsSync, mkdirSync, writeFileSync, rmSync, readFileSync } from "node:fs"; import { homedir } from "node:os"; import { join, resolve } from "node:path"; +import { LocalExecutor, probeListeningPortOwners } from "@repo/adapters"; import { readCliInstall } from "./cli-install"; import { IS_ALT_HOME, LOG_DIR, OS_DIR } from "./paths"; @@ -390,6 +391,50 @@ export function serviceStatus(): { kind: ServiceKind; installed: boolean; runnin return { kind, installed: false, running: false }; } +/** Only reclaim remembered listeners owned by the installed service's process tree. + * A saved port alone proves no ownership (for example, after `up --no-ui`). + * Unknown service PIDs or incomplete listener ownership use normal port resolution. */ +export async function serviceHeldPorts(): Promise { + const kind = detectKind(); + let pid: number | null = null; + if (kind === "launchd") pid = launchdPid(); + else if (kind === "systemd-user" || kind === "systemd-system") { + const args = kind === "systemd-user" ? ["--user"] : []; + const result = run("systemctl", [...args, "show", SYSTEMD_NAME, "--property=MainPID", "--value"]); + if (result.ok && /^\d+$/.test(result.out)) pid = Number(result.out); + } + // Scheduled Tasks do not expose an authoritative process ID through schtasks. + if (!pid || !Number.isSafeInteger(pid) || pid <= 1) return []; + const processes = run("ps", ["-axo", "pid=,ppid="]); + if (!processes.ok) return []; + const parents = new Map(); + for (const line of processes.out.split("\n")) { + const match = line.match(/^\s*(\d+)\s+(\d+)\s*$/); + if (match) parents.set(Number(match[1]), Number(match[2])); + } + if (!parents.has(pid)) return []; + const belongsToService = (owner: number | null): boolean => { + const seen = new Set(); + while (owner != null && owner > 1 && !seen.has(owner)) { + if (owner === pid) return true; + seen.add(owner); + owner = parents.get(owner) ?? null; + } + return false; + }; + const stored = readStoredPorts(); + const ports = [...new Set([stored?.api, stored?.dashboard])].filter( + (port): port is number => typeof port === "number" && Number.isInteger(port) && port > 0 && port <= 65535, + ); + const executor = new LocalExecutor(); + const held = await Promise.all(ports.map(async (port) => { + const owners = await probeListeningPortOwners(executor, port); + return owners.checked && owners.ownershipComplete && owners.occupants.length > 0 && + owners.occupants.every((owner) => belongsToService(owner.pid)) ? port : null; + })); + return held.filter((port): port is number => port !== null); +} + /** Stop AND disable the service — it won't restart or return on reboot. */ /** * Best-effort reaper for processes still holding our canonical API/dashboard diff --git a/apps/cli/src/lib/up-plan.ts b/apps/cli/src/lib/up-plan.ts index ee9d9f6c6..119d75a95 100644 --- a/apps/cli/src/lib/up-plan.ts +++ b/apps/cli/src/lib/up-plan.ts @@ -30,7 +30,7 @@ import { previewHostEdge, type EdgePreview } from "./edge-preflight"; import { DEFAULT_REPO, SOURCE_CHECKOUT_DIR, SOURCE_DIST_DIR } from "./from-source"; import { AUTH_SECRET_FILE, DATA_DIR, INTERNAL_TOKEN_FILE, LOG_DIR } from "./paths"; import { PORTS_FILE, resolvePorts, type ResolvedPorts } from "./ports"; -import { installStepFor, preview as previewService, type ServiceKind } from "./service"; +import { installStepFor, preview as previewService, serviceHeldPorts, type ServiceKind } from "./service"; /** How `up` would run: install a supervised service, or stay attached. */ export type UpMode = "service" | "foreground" | "from-source"; @@ -136,6 +136,7 @@ export async function planUp(opts: UpPlanOpts): Promise { { api: opts.port ? Number(opts.port) : undefined, dashboard: opts.dashboardPort ? Number(opts.dashboardPort) : undefined, + reclaimable: mode === "service" ? await serviceHeldPorts() : [], }, { persist: false }, ), diff --git a/apps/cli/test/e2e/domain.test.ts b/apps/cli/test/e2e/domain.test.ts index e1d2405cf..cae63fd24 100644 --- a/apps/cli/test/e2e/domain.test.ts +++ b/apps/cli/test/e2e/domain.test.ts @@ -86,9 +86,9 @@ describe("openship domain add", () => { : { status: 201, json: ADDED }); const { err, code } = await runCommand(domainCommand, ["add", "app.example.com", "-p", "prj1"]); expect(code).toBe(0); - expect(fetchStub.calls[0].method).toBe("POST"); - expect(fetchStub.calls[0].url).toBe(`${API}/domains`); - expect(fetchStub.calls[0].body).toEqual({ projectId: "prj1", hostname: "app.example.com", isPrimary: false }); + const creation = fetchStub.calls.find(call => call.method === "POST")!; + expect(creation.url).toBe(`${API}/domains`); + expect(creation.body).toEqual({ projectId: "prj1", hostname: "app.example.com", isPrimary: false }); expect(err).toContain("openship domain verify d9"); // next-step hint names the new id }); @@ -99,7 +99,7 @@ describe("openship domain add", () => { ? { json: { data: projectInfo.project } } : { status: 201, json: ADDED }); await runCommand(domainCommand, ["add", "app.example.com", "-p", "prj1", "--primary"]); - expect(fetchStub.calls[0].body).toMatchObject({ isPrimary: true }); + expect(fetchStub.calls.find(call => call.method === "POST")!.body).toMatchObject({ isPrimary: true }); }); it("emits the domain and its DNS records as JSON in json mode", async () => { diff --git a/apps/cli/test/e2e/host-channel-container.test.ts b/apps/cli/test/e2e/host-channel-container.test.ts new file mode 100644 index 000000000..0a202d7f3 --- /dev/null +++ b/apps/cli/test/e2e/host-channel-container.test.ts @@ -0,0 +1,19 @@ +import { describe, expect, it } from "vitest"; +import { checkHostChannel } from "../../src/lib/host-channel-preflight"; + +// Requires an installed Compose stack with a configured, reachable host SSH channel. +// Run with OPENSHIP_TEST_HOST_CHANNEL=1; never change the host firewall. +describe.skipIf(process.env.OPENSHIP_TEST_HOST_CHANNEL !== "1")("host channel in the API image", () => { + it("reaches the configured host from the actual API container", async () => { + const result = await checkHostChannel(); + expect(result.configured).toBe(true); + expect(result.code).toBe("open"); + }); + + it("distinguishes a refused connection from an unavailable probe", async () => { + const result = await checkHostChannel({ + channel: () => ({ host: "127.0.0.1", port: 1, user: "unused" }), + }); + expect(result.code).toBe("refused"); + }); +}); diff --git a/apps/cli/test/e2e/login.test.ts b/apps/cli/test/e2e/login.test.ts index b869db092..1116501fd 100644 --- a/apps/cli/test/e2e/login.test.ts +++ b/apps/cli/test/e2e/login.test.ts @@ -48,6 +48,7 @@ vi.mock("../../src/lib/local-connection", () => ({ import { loginCommand } from "../../src/commands/login"; import { CLOUD_API_URL, CLOUD_DASHBOARD_URL, LOCAL_DASHBOARD_URL } from "@repo/core"; +import { SDK_CAPABILITIES } from "@repo/contracts"; import { runCommand, stubFetch, type FetchStub } from "../helpers/harness"; import { setJsonMode } from "../../src/lib/output"; @@ -67,6 +68,38 @@ afterEach(() => { }); describe("openship login endpoint preservation", () => { + it.each([404, 200])("discovers the same-origin API after a dashboard response (%s) and saves it", async (status) => { + fetchStub.restore(); + fetchStub = stubFetch(request => { + if (request.url === "https://ship.example.com/api/tokens") return { status, text: "Dashboard" }; + if (request.url.endsWith("/health")) { + expect(request.headers.authorization).toBeUndefined(); + return { json: { status: "ok", sdk: SDK_CAPABILITIES } }; + } + expect(request.url).toBe("https://ship.example.com/api/proxy/api/tokens"); + expect(request.headers.authorization).toBe("Bearer opsh_pat_test"); + return { json: { data: [] } }; + }); + const result = await runCommand(loginCommand, ["--api-url", "https://ship.example.com", "--token", "opsh_pat_test"]); + expect(result.code).toBe(0); + expect(h.added.at(-1)?.opts).toMatchObject({ apiUrl: "https://ship.example.com/api/proxy", dashboardUrl: "https://ship.example.com" }); + expect(fetchStub.calls).toHaveLength(3); + }); + + it("does not send a token to a guessed endpoint without a valid API health response", async () => { + fetchStub.restore(); + fetchStub = stubFetch(() => ({ status: 404, json: { error: "Not found" } })); + const result = await runCommand(loginCommand, ["--api-url", "https://ship.example.com", "--token", "opsh_pat_test"]); + expect(result.code).toBe(1); + expect(h.added).toEqual([]); + expect(fetchStub.calls.map(call => call.url)).toEqual(["https://ship.example.com/api/tokens", "https://ship.example.com/api/proxy/api/health"]); + expect(fetchStub.calls[1]!.headers.authorization).toBeUndefined(); + }); + + it("derives the dashboard correctly for an explicitly supplied proxy API", async () => { + await runCommand(loginCommand, ["--api-url", "https://ship.example.com/api/proxy", "--token", "opsh_pat_test"]); + expect(h.added.at(-1)?.opts.dashboardUrl).toBe("https://ship.example.com"); + }); it("guides a fresh interactive login to Cloud and opens its token page", async () => { h.interactive = true; const result = await runCommand(loginCommand, []); diff --git a/apps/cli/test/e2e/up.test.ts b/apps/cli/test/e2e/up.test.ts index 8ffcf15c7..d1716f857 100644 --- a/apps/cli/test/e2e/up.test.ts +++ b/apps/cli/test/e2e/up.test.ts @@ -207,6 +207,8 @@ beforeEach(() => { (upCommand as any).setOptionValue?.("edge", undefined); (upCommand as any).setOptionValue?.("compose", undefined); (upCommand as any).setOptionValue?.("resetSecrets", undefined); + for (const key of ["managedEdge", "publicUrl", "trustProxy", "nonInteractive", "yes", "adminEmail", "adminPassword", "domainKind", "hostname"]) + upCommand.setOptionValue(key, undefined); con = captureConsole(); }); @@ -218,6 +220,68 @@ afterEach(() => { }); describe("openship up --compose (edge chain)", () => { + it("gives a usable first-admin instruction after an ordinary install", async () => { + const result = await runCommand(upCommand, ["--compose"]); + expect(result.code).toBe(0); + expect(con.text()).toContain("Create first admin"); + expect(con.text()).not.toContain("register the first account"); + }); + + it("refuses incomplete non-interactive setup before changing the stack", async () => { + const result = await runCommand(upCommand, ["--compose", "--non-interactive"]); + expect(result.code).toBe(1); + expect(h.prefetchCalls).toBe(0); + expect(h.composeUpCalls).toBe(0); + expect(con.text()).toContain("--admin-email"); + }); + + it("requires an HTTPS hostname before starting managed-edge installation", async () => { + const result = await runCommand(upCommand, ["--compose", "--managed-edge", "--public-url", "http://ops.example.com"]); + expect(result.code).toBe(1); + expect(h.composeUpCalls).toBe(0); + expect(con.text()).toContain("https://"); + }); + + it("honors managed-edge in Compose without changing admin credentials", async () => { + fetchStub = stubFetch(request => { + expect(request.url).toContain("/api/system/self-register"); + expect(request.body).toMatchObject({ domainType: "custom", hostname: "ops.example.com", dashPort: 3001 }); + return { json: { url: "https://ops.example.com" } }; + }); + const result = await runCommand(upCommand, ["--compose", "--managed-edge", "--public-url", "https://ops.example.com"]); + expect(result.code, result.err).toBe(0); + expect(fetchStub.calls).toHaveLength(1); + expect(fetchStub.calls[0]!.headers["x-internal-token"]).toBe("tok"); + }); + + it("provisions the requested managed domain during headless Compose setup", async () => { + fetchStub = stubFetch(request => { + if (request.url.endsWith("/api/health")) return { json: { status: "ok" } }; + if (request.url.endsWith("/api/system/bootstrap-admin")) return { json: { success: true } }; + expect(request.url).toContain("/api/system/self-register"); + expect(request.body).toMatchObject({ domainType: "custom", hostname: "ops.example.com" }); + return { json: { url: "https://ops.example.com" } }; + }); + const result = await runCommand(upCommand, ["--compose", "--managed-edge", "--public-url", "https://ops.example.com", + "--non-interactive", "--admin-email", "owner@example.com", "--admin-password", "strong-initial-password"]); + expect(result.code, con.text()).toBe(0); + expect(fetchStub.calls.map(call => new URL(call.url).pathname)).toEqual([ + "/api/health", "/api/system/bootstrap-admin", "/api/system/self-register", + ]); + }); + + it("does not report managed-edge success when registration fails", async () => { + fetchStub = stubFetch(request => { + if (request.url.endsWith("/api/health")) return { json: { status: "ok" } }; + if (request.url.endsWith("/api/system/bootstrap-admin")) return { json: { success: true } }; + return { status: 503, json: { error: "Edge unavailable" } }; + }); + const result = await runCommand(upCommand, ["--compose", "--managed-edge", "--public-url", "https://ops.example.com", + "--non-interactive", "--admin-email", "owner@example.com", "--admin-password", "strong-initial-password"]); + expect(result.code).toBe(1); + expect(con.text()).toContain("Managed domain registration did not complete"); + expect(con.text()).not.toContain("✓ Openship provisioned"); + }); it("exits before the edge preflight when docker/compose is missing", async () => { h.hasDocker = false; const r = await runCommand(upCommand, ["--compose"]); diff --git a/apps/cli/test/unit/compose-env-preserve.test.ts b/apps/cli/test/unit/compose-env-preserve.test.ts index 1ca62a9cd..7ea232daa 100644 --- a/apps/cli/test/unit/compose-env-preserve.test.ts +++ b/apps/cli/test/unit/compose-env-preserve.test.ts @@ -1753,10 +1753,13 @@ describe("composeUp — the host channel is provisioned out loud (#509)", () => seedHostKey(); h.channelAuth = () => ({ status: 0, stdout: "openship-sudo=refused\n", stderr: "" }); - await composeUp({}); + // Select a non-root session explicitly: the test runner may itself be root, + // whose SSH login already has privilege and needs no sudo warning. + await composeUp({ hostSshUser: "ops" }); // A working channel, so it IS written — just one with no route to root. expect(writtenEnv().OPENSHIP_HOST_SSH_HOST).toBe("host.docker.internal"); + expect(writtenEnv().OPENSHIP_HOST_SSH_USER).toBe("ops"); expect(warned.join("\n")).toContain("no route to it"); }); diff --git a/apps/cli/test/unit/custom-domain-endpoint.test.ts b/apps/cli/test/unit/custom-domain-endpoint.test.ts index 39d360423..45f0154c9 100644 --- a/apps/cli/test/unit/custom-domain-endpoint.test.ts +++ b/apps/cli/test/unit/custom-domain-endpoint.test.ts @@ -4,6 +4,8 @@ const h = vi.hoisted(() => ({ project: {} as Record, rows: [] as unknown[], updates: [] as unknown[], + services: [] as Array>, + serviceUpdates: [] as unknown[], updateError: false, })); vi.mock("../../src/lib/ship-client", async (importOriginal) => { @@ -30,6 +32,18 @@ vi.mock("../../src/lib/ship-client", async (importOriginal) => { h.updates.push({ id, input }); }, }, + services: { + list: async () => h.services, + get: async (_project: string, id: string) => { + const service = h.services.find(service => service.id === id); + if (!service) throw new Error("Service not found in project"); + return service; + }, + update: async (project: string, id: string, input: unknown) => { + if (h.updateError) throw new Error("update unavailable"); + h.serviceUpdates.push({ project, id, input }); + }, + }, domains: { verify: async () => { throw new Error("verification unavailable"); @@ -59,6 +73,8 @@ beforeEach(() => { h.rows = []; h.updates = []; h.updateError = false; + h.services = []; + h.serviceUpdates = []; }); afterEach(() => vi.restoreAllMocks()); @@ -122,28 +138,48 @@ describe("CLI custom domain routing", () => { expect(h.updates).toEqual([]); }); - it("warns without attaching a portless worker or failing the command", async () => { + it("refuses a portless worker before claiming a domain", async () => { h.project = { ...h.project, hasServer: false, workloadType: "worker", port: null }; - const warning = vi.spyOn(console, "warn").mockImplementation(() => {}); const result = await add(); - expect(result.code).toBe(0); - expect(h.rows).toHaveLength(1); + expect(result.code).toBe(1); + expect(h.rows).toEqual([]); expect(h.updates).toEqual([]); - expect(warning.mock.calls.flat().join(" ")).toMatch(/worker.*no public route/i); - expect(result.err).not.toContain("routed on the next deploy"); + expect(result.err).toContain("worker has no public route"); }); - it.each(["services", "monorepo"])("warns without attaching a %s project", async (projectType) => { - h.project = { ...h.project, name: "My project", projectType }; - const warning = vi.spyOn(console, "warn").mockImplementation(() => {}); + it.each(["services", "monorepo"])("requires an explicit target before claiming a domain for %s", async projectType => { + h.project = { ...h.project, projectType }; const result = await add(); - expect(result.code).toBe(0); - expect(h.rows).toHaveLength(1); + expect(result.code).toBe(1); + expect(h.rows).toEqual([]); + expect(result.err).toContain("--service"); + }); + + it.each(["web", "svc_web"])("attaches to the project-scoped service selected by %s", async ref => { + h.project.projectType = "services"; + const existing = [{ domainType: "free", domain: "existing", port: 8080 }]; + h.services = [{ id: "svc_web", name: "web", ports: ["8080:3000"], publicEndpoints: existing }]; + const result = await runCommand(domainCommand, ["add", "app.example.com", "-p", "project-1", "--service", ref, "--port", "3000"]); + expect(result.code, result.err).toBe(0); expect(h.updates).toEqual([]); - expect(warning).toHaveBeenCalledWith( - "Domain app.example.com was added, but My project is a multi-service project, so routes belong to a service. Attach it to a service in the dashboard.", - ); - expect(result.err).not.toContain("routed on the next deploy"); + expect(h.serviceUpdates).toEqual([{ project: "project-1", id: "svc_web", input: { exposed: true, publicEndpoints: [...existing, { domainType: "custom", customDomain: "app.example.com", port: 3000 }] } }]); + }); + + it.each(["unknown", "svc_foreign"])("rejects an unknown or foreign service %s before claiming the hostname", async ref => { + h.project.projectType = "services"; + const result = await runCommand(domainCommand, ["add", "app.example.com", "-p", "project-1", "--service", ref, "--port", "3000"]); + expect(result.code).toBe(1); + expect(h.rows).toEqual([]); + expect(h.serviceUpdates).toEqual([]); + }); + + it("requires a port when the selected service has several possible targets", async () => { + h.project.projectType = "services"; + h.services = [{ id: "svc_web", name: "web", ports: ["3000", "3001"] }]; + const result = await runCommand(domainCommand, ["add", "app.example.com", "-p", "project-1", "--service", "web"]); + expect(result.code).toBe(1); + expect(h.rows).toEqual([]); + expect(result.err).toContain("--port"); }); it.each(["domain", "edge"])( diff --git a/apps/cli/test/unit/headless-provision-domain.test.ts b/apps/cli/test/unit/headless-provision-domain.test.ts index dd0e2f7f7..435f4132f 100644 --- a/apps/cli/test/unit/headless-provision-domain.test.ts +++ b/apps/cli/test/unit/headless-provision-domain.test.ts @@ -14,6 +14,7 @@ import { describe, it, expect, beforeEach, vi } from "vitest"; const h = vi.hoisted(() => ({ posts: [] as Array<{ path: string; body: Record }>, + bootstrap: vi.fn(async () => ({ ok: true, message: "created" })), })); vi.mock("../../src/lib/loopback-api", () => ({ @@ -22,13 +23,13 @@ vi.mock("../../src/lib/loopback-api", () => ({ return { ok: true, data: { url: "https://ops.example.com" } }; }, waitHealthy: async () => true, - bootstrapAdmin: async () => ({ ok: true, message: "created" }), + bootstrapAdmin: h.bootstrap, // The provision-stream reader goes through this; unreachable here (the stubbed // self-register returns no sessionId), but the module imports it. internalFetch: async () => ({ kind: "unreachable", detail: "not stubbed" }), })); -import { headlessProvision } from "../../src/lib/instance-provision"; +import { headlessProvision, provisionInstanceDomain } from "../../src/lib/instance-provision"; const inputs = { admin: { email: "a@b.com", name: "a", password: "supersecret" }, @@ -39,9 +40,16 @@ const selfRegister = () => h.posts.find((p) => p.path === "/api/system/self-regi beforeEach(() => { h.posts = []; + h.bootstrap.mockClear(); }); describe("headlessProvision — self-app custom domain", () => { + it("can register a managed domain without creating or resetting an existing admin", async () => { + const result = await provisionInstanceDomain({ port: "4000", dashPort: "3001", domain: inputs.domain }); + expect(result.domainRegistered).toBe(true); + expect(h.bootstrap).not.toHaveBeenCalled(); + expect(h.posts.map(post => post.path)).toEqual(["/api/system/self-register"]); + }); it("requests the custom domain on COMPOSE (container edge issues the cert)", async () => { const res = await headlessProvision({ port: "4000", dashPort: "3001", inputs, method: "compose" }); diff --git a/apps/cli/test/unit/public-instance-api.test.ts b/apps/cli/test/unit/public-instance-api.test.ts new file mode 100644 index 000000000..89ac6d914 --- /dev/null +++ b/apps/cli/test/unit/public-instance-api.test.ts @@ -0,0 +1,20 @@ +import { afterEach, expect, it, vi } from "vitest"; +import { SDK_CAPABILITIES } from "@repo/contracts"; +import { discoverPublicInstanceApi } from "../../src/lib/public-instance-api"; +afterEach(() => vi.unstubAllGlobals()); + +it("confines credential-free discovery to the supplied origin and refuses redirects", async () => { + const fetcher = vi.fn(async () => Response.json({ status: "ok", sdk: SDK_CAPABILITIES })); + vi.stubGlobal("fetch", fetcher); + expect(await discoverPublicInstanceApi("https://ship.test/prefix/")).toBe("https://ship.test/prefix/api/proxy"); + expect(fetcher).toHaveBeenCalledWith("https://ship.test/prefix/api/proxy/api/health", expect.objectContaining({ redirect: "error", headers: { Accept: "application/json" } })); +}); +it.each(["https://ship.test/api", "https://ship.test/api/proxy", "https://ship.test/prefix/api/proxy/"])("does not reinterpret an explicit API: %s", async url => { + const fetcher = vi.fn(); vi.stubGlobal("fetch", fetcher); + expect(await discoverPublicInstanceApi(url)).toBeNull(); + expect(fetcher).not.toHaveBeenCalled(); +}); +it.each([{ status: "ok" }, { sdk: SDK_CAPABILITIES }, { status: "ok", sdk: { protocol: -1 } }])("ignores an unrelated health response: %j", async body => { + vi.stubGlobal("fetch", vi.fn(async () => Response.json(body))); + expect(await discoverPublicInstanceApi("https://ship.test")).toBeNull(); +}); diff --git a/apps/cli/test/unit/service-held-ports.test.ts b/apps/cli/test/unit/service-held-ports.test.ts new file mode 100644 index 000000000..8c90f5dc9 --- /dev/null +++ b/apps/cli/test/unit/service-held-ports.test.ts @@ -0,0 +1,111 @@ +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const state = vi.hoisted(() => { + const home = `${process.env.TMPDIR || "/tmp"}/openship-service-owners-${process.pid}`; + process.env.OPENSHIP_HOME = home; + return { home, pid: 4200, processes: "4200 1\n4201 4200\n4202 4201\n9000 1", probe: vi.fn() }; +}); +vi.mock("@repo/adapters", () => ({ + LocalExecutor: class {}, + probeListeningPortOwners: state.probe, +})); +vi.mock("../../src/lib/this-host", () => ({ + thisHost: () => ({ profile: { serviceManager: "systemd" } }), +})); +const commands = vi.hoisted(() => vi.fn()); +vi.mock("node:child_process", async (original) => ({ + ...(await original()), + spawnSync: commands, +})); +const { serviceHeldPorts } = await import("../../src/lib/service"); +const platform = process.platform; +beforeEach(() => { + Object.defineProperty(process, "platform", { value: "darwin", configurable: true }); + mkdirSync(state.home, { recursive: true }); + writeFileSync(join(state.home, "ports.json"), JSON.stringify({ api: 7100, dashboard: 7200 })); + state.pid = 4200; + state.processes = "4200 1\n4201 4200\n4202 4201\n9000 1"; + state.probe.mockReset().mockResolvedValue({ + checked: true, + ownershipComplete: true, + occupants: [{ pid: 4202 }], + }); + commands.mockReset().mockImplementation((command, args) => { + const stdout = + command === "launchctl" + ? `"PID" = ${state.pid};` + : command === "systemctl" + ? String(state.pid) + : command === "ps" + ? state.processes + : ""; + return { status: 0, stdout, stderr: "" }; + }); +}); +afterEach(() => + Object.defineProperty(process, "platform", { value: platform, configurable: true }), +); +afterAll(() => rmSync(state.home, { recursive: true, force: true })); + +describe("service-held listener ownership", () => { + it("accepts listeners belonging to the service and nested children", async () => { + expect(await serviceHeldPorts()).toEqual([7100, 7200]); + }); + it("rejects a foreign dashboard listener even while the API service runs", async () => { + state.probe.mockImplementation(async (_, port) => ({ + checked: true, + ownershipComplete: true, + occupants: [{ pid: port === 7100 ? 4201 : 9000 }], + })); + expect(await serviceHeldPorts()).toEqual([7100]); + }); + it("rejects mixed owners, hidden owners and incomplete ownership", async () => { + for (const result of [ + { checked: true, ownershipComplete: true, occupants: [{ pid: 4201 }, { pid: 9000 }] }, + { checked: true, ownershipComplete: false, occupants: [{ pid: 4201 }] }, + { checked: true, ownershipComplete: true, occupants: [{ pid: null }] }, + { checked: false, ownershipComplete: false, occupants: [] }, + ]) { + state.probe.mockResolvedValue(result); + expect(await serviceHeldPorts()).toEqual([]); + } + }); + it("does not trust a saved port when its owner is outside the live service tree", async () => { + state.probe.mockResolvedValue({ + checked: true, + ownershipComplete: true, + occupants: [{ pid: 9000 }], + }); + expect(await serviceHeldPorts()).toEqual([]); + }); + it("falls back when the service is stopped or its process tree is unavailable", async () => { + state.pid = 0; + expect(await serviceHeldPorts()).toEqual([]); + expect(state.probe).not.toHaveBeenCalled(); + state.pid = 4200; + state.processes = "9000 1"; + expect(await serviceHeldPorts()).toEqual([]); + expect(state.probe).not.toHaveBeenCalled(); + }); + it("uses the live systemd MainPID for Linux services", async () => { + Object.defineProperty(process, "platform", { value: "linux", configurable: true }); + expect(await serviceHeldPorts()).toEqual([7100, 7200]); + expect( + commands.mock.calls.some( + ([command, args]) => command === "systemctl" && args.includes("--property=MainPID"), + ), + ).toBe(true); + }); + it("does not guess ownership for a scheduled task without a PID", async () => { + Object.defineProperty(process, "platform", { value: "win32", configurable: true }); + expect(await serviceHeldPorts()).toEqual([]); + expect(state.probe).not.toHaveBeenCalled(); + }); + it("ignores malformed stored ports without invoking an ownership probe", async () => { + writeFileSync(join(state.home, "ports.json"), JSON.stringify({ api: "7100", dashboard: -1 })); + expect(await serviceHeldPorts()).toEqual([]); + expect(state.probe).not.toHaveBeenCalled(); + }); +}); diff --git a/apps/cli/test/unit/up-rerun-ports.test.ts b/apps/cli/test/unit/up-rerun-ports.test.ts new file mode 100644 index 000000000..51dad16e0 --- /dev/null +++ b/apps/cli/test/unit/up-rerun-ports.test.ts @@ -0,0 +1,153 @@ +import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { createServer, type Server } from "node:net"; +import { join } from "node:path"; +import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +/** + * Re-running `openship up` while its own service is running (#1570). + * + * The running service holds the ports in ports.json, so they probe busy. They are + * still ours: installAndStart replaces that instance before the new one binds. + * Treating them as taken moved the install to new ports on every re-run + * (7100 -> 7101), rewrote ports.json, and first sat out waitPortFree on each port. + */ + +// lib/paths reads OPENSHIP_HOME at import time, so set it before anything loads. +const HOME = vi.hoisted(() => { + const dir = `${process.env.TMPDIR?.replace(/\/$/, "") || "/tmp"}/openship-up-rerun-${process.pid}`; + process.env.OPENSHIP_HOME = dir; + return dir; +}); + +const svc = vi.hoisted(() => ({ + running: false, + heldPorts: [] as number[], + installed: [] as Array<{ port: string; dashboardPort: string }>, +})); + +// Only the service manager is faked. The port resolver and ports.json stay real. +vi.mock("../../src/lib/service", async (importOriginal) => ({ + ...(await importOriginal()), + serviceHeldPorts: async () => (svc.running ? svc.heldPorts : []), + installAndStart: (flags: { port: string; dashboardPort: string }) => { + svc.installed.push(flags); + return { kind: "launchd", detail: "test" }; + }, +})); + +const { startService } = await import("../../src/commands/up"); +const { planUp } = await import("../../src/lib/up-plan"); +const { getFreePort, isPortFree } = await import("../../src/lib/ports"); + +const PORTS_FILE = join(HOME, "ports.json"); +const held: Server[] = []; + +/** Bind a port the way a running process would. */ +function hold(port: number): Promise { + return new Promise((resolve, reject) => { + const srv = createServer(); + srv.once("error", reject); + srv.listen(port, "127.0.0.1", () => { + held.push(srv); + resolve(); + }); + }); +} + +/** A free port whose neighbour is free too, so a move to it would be visible. */ +async function freeWithNeighbour(from?: number): Promise { + for (let i = 0; i < 50; i++) { + const port = from === undefined ? await getFreePort() : from + i * 3; + if ((await isPortFree(port)) && (await isPortFree(port + 1))) return port; + } + throw new Error("no free port with a free neighbour"); +} + +/** API and dashboard ports far enough apart that neither move lands on the other. */ +async function freePorts(): Promise<{ api: number; dashboard: number }> { + const api = await freeWithNeighbour(); + return { api, dashboard: await freeWithNeighbour(api + 10) }; +} + +beforeEach(() => { + mkdirSync(HOME, { recursive: true }); + rmSync(PORTS_FILE, { force: true }); + svc.running = false; + svc.heldPorts = []; + svc.installed = []; +}); + +afterEach(async () => { + await Promise.all(held.splice(0).map((s) => new Promise((r) => s.close(r)))); +}); + +afterAll(() => { + rmSync(HOME, { recursive: true, force: true }); +}); + +describe("startService on a re-run", () => { + it("keeps the ports its own running service holds", async () => { + const { api, dashboard } = await freePorts(); + writeFileSync(PORTS_FILE, JSON.stringify({ api, dashboard })); + await hold(api); + await hold(dashboard); + svc.running = true; + svc.heldPorts = [api, dashboard]; + + const r = await startService({}, { quiet: true }); + + expect({ api: r.port, dashboard: r.dashPort }).toEqual({ + api: String(api), + dashboard: String(dashboard), + }); + expect(svc.installed[0]).toMatchObject({ port: String(api), dashboardPort: String(dashboard) }); + expect(JSON.parse(readFileSync(PORTS_FILE, "utf8"))).toEqual({ api, dashboard }); + }, 30_000); + + it("still moves off a remembered port another program holds when the service is not running", async () => { + const { api, dashboard } = await freePorts(); + writeFileSync(PORTS_FILE, JSON.stringify({ api, dashboard })); + await hold(api); + + const r = await startService({}, { quiet: true }); + + expect(r.port).not.toBe(String(api)); + expect(r.dashPort).toBe(String(dashboard)); + }, 30_000); + + it("moves off an unrelated dashboard listener when the running service owns only the API", async () => { + const { api, dashboard } = await freePorts(); + writeFileSync(PORTS_FILE, JSON.stringify({ api, dashboard })); + await hold(api); + await hold(dashboard); + svc.running = true; + svc.heldPorts = [api]; + const r = await startService({}, { quiet: true }); + expect(r.port).toBe(String(api)); + expect(r.dashPort).not.toBe(String(dashboard)); + }, 30_000); + + it("uses the same owned ports for a dry-run and an actual service rerun", async () => { + const { api, dashboard } = await freePorts(); + writeFileSync(PORTS_FILE, JSON.stringify({ api, dashboard })); + await hold(api); + await hold(dashboard); + svc.running = true; + svc.heldPorts = [api, dashboard]; + const plan = await planUp({ bare: true }); + const r = await startService({}, { quiet: true }); + expect(plan.ports.api).toBe(Number(r.port)); + expect(plan.ports.dashboard).toBe(Number(r.dashPort)); + }, 30_000); + + it("does not reclaim service ports for an attached foreground preview", async () => { + const { api, dashboard } = await freePorts(); + writeFileSync(PORTS_FILE, JSON.stringify({ api, dashboard })); + await hold(api); + svc.running = true; + svc.heldPorts = [api]; + const plan = await planUp({ bare: true, foreground: true }); + expect(plan.ports.api).not.toBe(api); + expect(plan.ports.dashboard).toBe(dashboard); + }, 30_000); +}); diff --git a/apps/dashboard/scripts/check-i18n.mjs b/apps/dashboard/scripts/check-i18n.mjs index 8eadfab65..7a3f6d701 100644 --- a/apps/dashboard/scripts/check-i18n.mjs +++ b/apps/dashboard/scripts/check-i18n.mjs @@ -14,6 +14,15 @@ import { fileURLToPath } from "node:url"; const SOURCE_LOCALE = "en"; +// These display names intentionally stay English in every locale. +const BRAND_NAMES = new Set([ + "OpenShip", + "OpenShip Cloud", + "OpenShip Desktop", + "OpenShip Manage", + "Ruby on Rails", +]); + /** Absolute path to the locales dir, resolved relative to this script. */ export function defaultLocalesDir() { return fileURLToPath(new URL("../src/i18n/locales", import.meta.url)); @@ -56,7 +65,12 @@ function readJson(file) { * English sentence, so the UI silently renders English and every test stays green. */ function looksTranslatable(value) { - return typeof value === "string" && value.trim().length > 3 && /\s/.test(value.trim()); + return ( + typeof value === "string" && + value.trim().length > 3 && + /\s/.test(value.trim()) && + !BRAND_NAMES.has(value.trim()) + ); } /** @@ -98,7 +112,8 @@ export function checkI18nParity(localesDir = defaultLocalesDir()) { if (fs.existsSync(file)) localeEntries = leafEntries(readJson(file)); const localeKeys = new Set(Object.keys(localeEntries)); for (const k of base) if (!localeKeys.has(k)) missing.push({ locale, namespace: ns, key: k }); - for (const k of localeKeys) if (!baseSet.has(k)) extra.push({ locale, namespace: ns, key: k }); + for (const k of localeKeys) + if (!baseSet.has(k)) extra.push({ locale, namespace: ns, key: k }); // Present, but still the English sentence — invisible to the two checks above. for (const k of localeKeys) { if (!baseSet.has(k)) continue; @@ -189,12 +204,14 @@ if (isMain()) { } if (r.missing.length) { console.log("MISSING (key → locales that lack it):"); - for (const [id, locs] of group(r.missing)) console.log(` ${id} [${[...locs].sort().join(",")}]`); + for (const [id, locs] of group(r.missing)) + console.log(` ${id} [${[...locs].sort().join(",")}]`); console.log(""); } if (r.extra.length) { console.log("EXTRA (stale locale keys not in en):"); - for (const [id, locs] of group(r.extra)) console.log(` ${id} [${[...locs].sort().join(",")}]`); + for (const [id, locs] of group(r.extra)) + console.log(` ${id} [${[...locs].sort().join(",")}]`); console.log(""); } } else { diff --git a/apps/dashboard/src/app/(auth)/login/page.tsx b/apps/dashboard/src/app/(auth)/login/page.tsx index 2cacb7df7..26e819723 100644 --- a/apps/dashboard/src/app/(auth)/login/page.tsx +++ b/apps/dashboard/src/app/(auth)/login/page.tsx @@ -236,7 +236,7 @@ function LoginPageInner() { ); } - /* ── Cloud mode (desktop): redirect to Openship Cloud for all auth ── */ + /* ── Cloud mode (desktop): redirect to OpenShip Cloud for all auth ── */ if (authMode === "cloud") { const apiUrl = getApiOrigin(typeof window !== "undefined" ? window.location.origin : undefined); const callbackUrl = `${apiUrl}/api/auth/cloud-callback`; @@ -251,7 +251,7 @@ function LoginPageInner() { {t.auth.login.title}

- Sign in with your Openship account to continue. + Sign in with your OpenShip account to continue.

@@ -272,7 +272,7 @@ function LoginPageInner() { onClick={() => { void handleCloudSignIn(callbackUrl); }} > {loading ? : } - {loading ? "Opening Openship Cloud..." : "Sign in with Openship"} + {loading ? "Opening OpenShip Cloud..." : "Sign in with OpenShip"} ); diff --git a/apps/dashboard/src/app/(auth)/register/signup-next.test.ts b/apps/dashboard/src/app/(auth)/register/signup-next.test.ts index e5e61f44d..2c7ece3a7 100644 --- a/apps/dashboard/src/app/(auth)/register/signup-next.test.ts +++ b/apps/dashboard/src/app/(auth)/register/signup-next.test.ts @@ -15,7 +15,7 @@ describe("signUpNext", () => { it("goes to verification when sign-up succeeds WITHOUT a session", () => { // The exact better-auth shape for requireEmailVerification. const next = signUpNext({ data: { token: null } }, opts); - expect(next).toEqual({ kind: "verify", href: "/verify-email?email=a%40b.co" }); + expect(next).toEqual({ kind: "verify", href: "/verify-email?email=a%40b.co&signup=1" }); }); it("treats a missing token field the same as an explicit null", () => { @@ -54,3 +54,16 @@ describe("signUpNext", () => { expect(next.kind === "verify" && next.href).toContain("a%2Btag%40b.co"); }); }); + +it("preserves the Cloud consent and PKCE continuation through signup verification", () => { + const returnTo = "/cloud-authorize?mode=device&state=test-state&code_challenge=test-challenge"; + const next = signUpNext( + { data: { token: null } }, + { email: "existing@example.test", authParams: new URLSearchParams({ returnTo }) }, + ); + expect(next.kind).toBe("verify"); + if (next.kind !== "verify") throw new Error("Expected email continuation"); + const params = new URL(next.href, "https://app.example.test").searchParams; + expect(params.get("signup")).toBe("1"); + expect(params.get("returnTo")).toBe(returnTo); +}); diff --git a/apps/dashboard/src/app/(auth)/register/signup-next.ts b/apps/dashboard/src/app/(auth)/register/signup-next.ts index 1d77fe8d2..f7ee49d02 100644 --- a/apps/dashboard/src/app/(auth)/register/signup-next.ts +++ b/apps/dashboard/src/app/(auth)/register/signup-next.ts @@ -23,7 +23,7 @@ export type SignUpResultShape = { export type SignUpNext = /** Show the message; stay on the form. */ | { kind: "error"; message: string | null } - /** Account exists, no session yet — the code screen, reachable unauthenticated. */ + /** No session: privately continue by verification code or sign-in instructions. */ | { kind: "verify"; href: string } /** A real session was issued; follow the post-login destination. */ | { kind: "session"; href: string }; @@ -36,7 +36,9 @@ export function signUpNext( authParams?: Parameters[1]; }, ): SignUpNext { - const verifyHref = emailVerificationHref(opts.email, opts.authParams ?? new URLSearchParams()); + // The same response also protects an existing address from enumeration. Do + // not promise account creation or a code when the email contains sign-in help. + const verifyHref = `${emailVerificationHref(opts.email, opts.authParams ?? new URLSearchParams())}&signup=1`; if (result.error) { // Some deployments surface the requirement as an error instead of a null diff --git a/apps/dashboard/src/app/(auth)/verify-email/page.tsx b/apps/dashboard/src/app/(auth)/verify-email/page.tsx index 78944d7c5..fd3996605 100644 --- a/apps/dashboard/src/app/(auth)/verify-email/page.tsx +++ b/apps/dashboard/src/app/(auth)/verify-email/page.tsx @@ -33,9 +33,11 @@ function VerifyEmailContent() { const searchParams = useSearchParams(); const token = searchParams.get("token"); const emailParam = searchParams.get("email") ?? ""; + const fromSignup = searchParams.get("signup") === "1"; const { toast } = useToast(); const { t } = useI18n(); const loginHref = buildAuthPageHref("/login", searchParams); + const resetHref = buildAuthPageHref("/forgot-password", searchParams); const [status, setStatus] = useState<"pending" | "verifying" | "verified" | "error">( token ? "verifying" : "pending", @@ -221,11 +223,14 @@ function VerifyEmailContent() {

- {t.auth.verifyEmail.pendingTitle} + {fromSignup ? t.auth.verifyEmail.signupTitle : t.auth.verifyEmail.pendingTitle}

{emailParam - ? interpolate(t.auth.verifyEmail.pendingSentTo, { email: emailParam }) + ? interpolate( + fromSignup ? t.auth.verifyEmail.signupSentTo : t.auth.verifyEmail.pendingSentTo, + { email: emailParam }, + ) : t.auth.verifyEmail.pendingGeneric}

@@ -264,10 +269,17 @@ function VerifyEmailContent() {

+ {t.auth.register.hasAccount}{" "} - {t.auth.verifyEmail.backToSignIn} + {t.auth.register.signIn}

+ + {t.auth.forgotPassword.title} + ); diff --git a/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/CloudWaitlistModal.tsx b/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/CloudWaitlistModal.tsx index 44e443d2b..797e21a01 100644 --- a/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/CloudWaitlistModal.tsx +++ b/apps/dashboard/src/app/(dashboard)/(deployment)/deploy/[slug]/components/CloudWaitlistModal.tsx @@ -5,12 +5,12 @@ import { Icon as UiIcon } from "@repo/ui/icons"; // TODO: removed — temporary SaaS "Cloud is almost here" waitlist gate. // Delete this file, the /api/cloud-waitlist route, and the gate in Sidebar's -// handleDeploy when Openship Cloud opens for real deploys. +// handleDeploy when OpenShip Cloud opens for real deploys. import { useState } from "react"; /** - * SaaS-only "Openship Cloud is almost here" gate. Shown instead of running a + * SaaS-only "OpenShip Cloud is almost here" gate. Shown instead of running a * deploy while the managed cloud isn't open yet — captures an email for the * notify-me waitlist. "Notify me" POSTs to `/api/cloud-waitlist`, a thin Next * route handler that forwards to `process.env.MARKETING_API_URL` (no DB / Hono @@ -53,7 +53,7 @@ export function CloudWaitlistModal({ onClose }: { onClose: () => void }) {
-

Openship Cloud is almost here

+

OpenShip Cloud is almost here

Thanks for your interest! We're building a fast, scalable, and efficient managed cloud — one-click deploys with zero infrastructure to run. Launching soon. @@ -64,7 +64,7 @@ export function CloudWaitlistModal({ onClose }: { onClose: () => void }) { {status === "done" ? (

- Thank you — we'll email you the moment Openship Cloud is ready. + Thank you — we'll email you the moment OpenShip Cloud is ready.
) : (
diff --git a/apps/dashboard/src/app/(dashboard)/DashboardHomeClient.tsx b/apps/dashboard/src/app/(dashboard)/DashboardHomeClient.tsx index 138d19258..b9513fcc3 100644 --- a/apps/dashboard/src/app/(dashboard)/DashboardHomeClient.tsx +++ b/apps/dashboard/src/app/(dashboard)/DashboardHomeClient.tsx @@ -143,7 +143,7 @@ export default function DashboardHomeClient({ ) : (
{sourceProjects.slice(0, 5).map((p) => ( - removeProject(p.id)} /> + removeProject(p.id)} /> ))}
)} diff --git a/apps/dashboard/src/app/(dashboard)/apps/page.tsx b/apps/dashboard/src/app/(dashboard)/apps/page.tsx index 4ba8cd845..377e5f5f2 100644 --- a/apps/dashboard/src/app/(dashboard)/apps/page.tsx +++ b/apps/dashboard/src/app/(dashboard)/apps/page.tsx @@ -184,7 +184,7 @@ export default function AppsPage() {
{filteredApps.length > 0 ? filteredApps.map(app => ( - removeProject(app.id)} /> + removeProject(app.id)} /> )) : (

{copy.catalogNoResults}

)} diff --git a/apps/dashboard/src/app/(dashboard)/backups/backups-pages.test.tsx b/apps/dashboard/src/app/(dashboard)/backups/backups-pages.test.tsx index 1bcfca29d..e710363c4 100644 --- a/apps/dashboard/src/app/(dashboard)/backups/backups-pages.test.tsx +++ b/apps/dashboard/src/app/(dashboard)/backups/backups-pages.test.tsx @@ -64,7 +64,7 @@ const row = (id: string, serviceName: string, bytes: number): BackupDestinationR destinationId: "storage", destinationName: "Production backups", projectId: "project", - projectName: "Openship", + projectName: "OpenShip", serviceId: serviceName, serviceName, mailServerId: null, diff --git a/apps/dashboard/src/app/(dashboard)/billing/_components/billing-overview-layout.render.test.tsx b/apps/dashboard/src/app/(dashboard)/billing/_components/billing-overview-layout.render.test.tsx index efc890889..733e1b53d 100644 --- a/apps/dashboard/src/app/(dashboard)/billing/_components/billing-overview-layout.render.test.tsx +++ b/apps/dashboard/src/app/(dashboard)/billing/_components/billing-overview-layout.render.test.tsx @@ -48,7 +48,7 @@ describe("billing sidebar", () => { it("keeps the no-plan promotion visible when the provider has no free product", () => { const out = render(); - expect(text(out)).toContain("Launch on Openship Cloud"); + expect(text(out)).toContain("Launch on OpenShip Cloud"); expect(out).toContain("/billing/plans"); expect(text(out)).not.toContain("Unlimited"); }); diff --git a/apps/dashboard/src/app/(dashboard)/billing/manage/page.test.tsx b/apps/dashboard/src/app/(dashboard)/billing/manage/page.test.tsx index 31f875750..569a0f6de 100644 --- a/apps/dashboard/src/app/(dashboard)/billing/manage/page.test.tsx +++ b/apps/dashboard/src/app/(dashboard)/billing/manage/page.test.tsx @@ -227,7 +227,7 @@ it("adds slots without a plan picker and confirms the exact quoted price only on return state; }); await render(); - expect(host.querySelector("h1")?.textContent).toBe("Openship Manage"); + expect(host.querySelector("h1")?.textContent).toBe("OpenShip Manage"); expect(host.querySelector('a[href="/servers/external"]')?.textContent).toContain( "Production server", ); @@ -270,7 +270,7 @@ it("adds slots without a plan picker and confirms the exact quoted price only on it("keeps an empty paid plan on its detail page and offers the normal server connection form", async () => { activateManagement(0); await render(); - expect(host.querySelector("h1")?.textContent).toBe("Openship Manage"); + expect(host.querySelector("h1")?.textContent).toBe("OpenShip Manage"); expect(host.textContent).toContain(baseDictionary.billing.managementDetail.empty); expect(host.textContent).toContain("0 / 2"); expect(host.querySelector('a[href="/servers/new"]')?.textContent).toContain( @@ -417,7 +417,7 @@ it("keeps the Manage payment controls in its own payments tab", async () => { it("shows an unfinished Manage purchase as a named pending plan with recovery actions", async () => { state.pendingCheckout = true; await render(); - expect(host.querySelector("h1")?.textContent).toBe("Openship Manage"); + expect(host.querySelector("h1")?.textContent).toBe("OpenShip Manage"); expect(host.textContent).toContain(baseDictionary.billing.hub.paymentRequired); expect(button("Resume checkout")).toBeDefined(); expect(host.querySelector('[role="radiogroup"]')).toBeNull(); diff --git a/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentCard.tsx b/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentCard.tsx index 105bd759e..92eaf168a 100644 --- a/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentCard.tsx +++ b/apps/dashboard/src/app/(dashboard)/deployments/components/DeploymentCard.tsx @@ -8,10 +8,8 @@ import { DeploymentMenu } from "./DeploymentMenu"; import { CommitDetailsModal } from "./CommitDetailsModal"; import type { Deployment } from "../types"; import { formatDistanceToNow, formatBuildTime, getStatusConfig } from "../utils"; -import { ProjectStackIcon } from "@/components/shared/ProjectStackIcon"; -import { AppLogo } from "@/components/AppLogo"; +import { ProjectIcon } from "@/components/shared/ProjectIcon"; import { useI18n, interpolate } from "@/components/i18n-provider"; -import { useImageFallback } from "@/hooks/useImageFallback"; type ServiceStatusLabels = { deployed: string; @@ -122,7 +120,6 @@ export const DeploymentCard: React.FC = ({ const { t } = useI18n(); const [isCommitModalOpen, setIsCommitModalOpen] = useState(false); const [isMenuOpen, setIsMenuOpen] = useState(false); - const favicon = useImageFallback(deployment.favicon); const statusConfig = getStatusConfig(deployment.status); const statusLabelMap: Record = { @@ -151,21 +148,9 @@ export const DeploymentCard: React.FC = ({ aria-label={deployment.projectName || t.deployments.card.unknownProject} className="absolute inset-0 z-0" /> - {/* App logo (catalog apps) → project favicon → framework icon → initials */} + {/* Project icon → catalog logo → framework or service icon */}
- {appTemplateId ? ( - - ) : favicon.showImage ? ( - - ) : ( - - )} +
{/* Main info */} diff --git a/apps/dashboard/src/app/(dashboard)/deployments/types.ts b/apps/dashboard/src/app/(dashboard)/deployments/types.ts index 213fb5e83..a49f53391 100644 --- a/apps/dashboard/src/app/(dashboard)/deployments/types.ts +++ b/apps/dashboard/src/app/(dashboard)/deployments/types.ts @@ -56,9 +56,8 @@ export interface Deployment { branch?: string; projectId?: string; projectName?: string; - /** Auto-detected favicon of the deployed site, cached on the project. - * When present it replaces the framework/Docker glyph as the row logo. */ - favicon?: string | null; + /** Project icon shared with project lists and settings. */ + iconUrl?: string | null; failureReason?: string; /** Rollback state — populated by the orchestrator-aware listing endpoint. * `artifactRetainedAt` non-null = artifact is archived, rollback-eligible. diff --git a/apps/dashboard/src/app/(dashboard)/deployments/utils.ts b/apps/dashboard/src/app/(dashboard)/deployments/utils.ts index f2bc27c23..711c32894 100644 --- a/apps/dashboard/src/app/(dashboard)/deployments/utils.ts +++ b/apps/dashboard/src/app/(dashboard)/deployments/utils.ts @@ -41,7 +41,7 @@ export const mapRowToDeployment = (row: any): Deployment => { branch: row.branch ?? undefined, projectId: row.projectId, projectName: row.projectName, - favicon: row.favicon ?? null, + iconUrl: row.iconUrl ?? null, failureReason: row.errorMessage ?? undefined, /* Rollback state — flows from the listing endpoint, which enriches * each row with isActive and surfaces the orchestrator-owned diff --git a/apps/dashboard/src/app/(dashboard)/emails/_components/admin/backup-tab.tsx b/apps/dashboard/src/app/(dashboard)/emails/_components/admin/backup-tab.tsx index 85e95c71a..a5587e9f8 100644 --- a/apps/dashboard/src/app/(dashboard)/emails/_components/admin/backup-tab.tsx +++ b/apps/dashboard/src/app/(dashboard)/emails/_components/admin/backup-tab.tsx @@ -89,7 +89,7 @@ export function BackupTab({ serverId, domain }: { serverId: string; domain: stri // Form state const [destinationId, setDestinationId] = useState(""); - const [messageData, setMessageData] = useState(false); + const [messageData, setMessageData] = useState(true); const [keys, setKeys] = useState(true); const [schedule, setSchedule] = useState(DEFAULT_SCHEDULE_PARTS); // Kept as strings: an empty box is "no limit", which a number state can't hold diff --git a/apps/dashboard/src/app/(dashboard)/emails/_components/admin/mail-restore-modal.tsx b/apps/dashboard/src/app/(dashboard)/emails/_components/admin/mail-restore-modal.tsx index 78b4e7be8..f1c55e36d 100644 --- a/apps/dashboard/src/app/(dashboard)/emails/_components/admin/mail-restore-modal.tsx +++ b/apps/dashboard/src/app/(dashboard)/emails/_components/admin/mail-restore-modal.tsx @@ -12,7 +12,7 @@ import { Icon as UiIcon } from "@repo/ui/icons"; * mode "in_place" → restore the backup onto THIS mail server. * mode "to_fork" → migrate: restore onto a DIFFERENT mail server. * - * Both are destructive on the target: its accounts are TRUNCATED before the + * Both are destructive on the target: the database and mail stores are replaced as the * backup's data loads. */ @@ -92,14 +92,14 @@ function MailRestoreContent({ .then(({ servers }) => setTargets( servers - .filter((s) => s.id !== sourceServerId && s.completed) + .filter((s) => s.id !== sourceServerId && s.completed && s.domain === domain) .map((s) => ({ id: s.id, name: s.name, host: s.host, domain: s.domain, completed: s.completed })), ), ) .catch((diagnosticFailure) => { observeCaughtError(diagnosticFailure, "dashboard/app/(dashboard)/emails/_components/admin/mail-restore-modal"); }); - }, [mode, sourceServerId]); + }, [mode, sourceServerId, domain]); const canStart = confirmText.trim() === domain && (mode === "in_place" || !!targetId); diff --git a/apps/dashboard/src/app/(dashboard)/emails/_components/setup-error-banner.render.test.tsx b/apps/dashboard/src/app/(dashboard)/emails/_components/setup-error-banner.render.test.tsx index 1f3f83c5a..e4d1a0a25 100644 --- a/apps/dashboard/src/app/(dashboard)/emails/_components/setup-error-banner.render.test.tsx +++ b/apps/dashboard/src/app/(dashboard)/emails/_components/setup-error-banner.render.test.tsx @@ -33,7 +33,7 @@ describe("SetupErrorBanner", () => { render({ title: "Setup couldn't start", message: - "Can't start mail setup: Permission denied (publickey,password). Openship could not run a single command on this server.", + "Can't start mail setup: Permission denied (publickey,password). OpenShip could not run a single command on this server.", }), ); expect(out).toContain("Setup couldn't start"); diff --git a/apps/dashboard/src/app/(dashboard)/layout.tsx b/apps/dashboard/src/app/(dashboard)/layout.tsx index abb88ac76..cc8c81f0c 100644 --- a/apps/dashboard/src/app/(dashboard)/layout.tsx +++ b/apps/dashboard/src/app/(dashboard)/layout.tsx @@ -173,10 +173,9 @@ export default async function DashboardLayout({ children }: { children: React.Re hostDomain={deploymentInfo.hostDomain} >
- {/* Update + platform-status surface — full app width, ABOVE the sidebar. - Renders nothing unless there's an advisory / platform notice (SaaS: - partial outage, maintenance) / available update / what's-new, so it - adds no chrome when idle. */} + {/* Full-width update surface. Managed Cloud shows workload security; + Desktop and self-hosted also show installation updates, operator + notices and what's new. Empty state adds no chrome. */} {children}
diff --git a/apps/dashboard/src/app/(dashboard)/library/LibraryContent.tsx b/apps/dashboard/src/app/(dashboard)/library/LibraryContent.tsx new file mode 100644 index 000000000..a7d345baa --- /dev/null +++ b/apps/dashboard/src/app/(dashboard)/library/LibraryContent.tsx @@ -0,0 +1,299 @@ +"use client"; + +import { Icon as UiIcon, type IconName } from "@repo/ui/icons"; + +import React, { useState, useEffect, useCallback, useRef } from "react"; +import { usePathname, useRouter, useSearchParams } from "next/navigation"; +import { migrationHref, parseMigrationSource, type MigrationSourceKind } from "@repo/core"; +import { useGitHub } from "@/context/GitHubContext"; +import { usePlatform } from "@/context/PlatformContext"; +import { useCloud } from "@/context/CloudContext"; +import { ConnectPrompt } from "./components/ConnectPrompt"; +import { LoadingSkeleton } from "./components/LoadingSkeleton"; +import { RepositoryList } from "./components/RepositoryList"; +import { useLibraryRepos } from "./useLibraryRepos"; +import { GhCliConsent } from "./components/GhCliConsent"; +import { LocalProjects } from "./components/LocalProjects"; +import { FolderUpload } from "./components/FolderUpload"; +import { LibrarySidebar } from "./components/LibrarySidebar"; +import { UrlImport } from "./components/UrlImport"; +import { RepositoryAccounts } from "./components/RepositoryAccounts"; +import { PageContainer } from "@/components/ui/PageContainer"; +import { HelpMenu } from "@/components/HelpMenu"; +import { MigrationWizard } from "@/components/migration/MigrationWizard"; +import { useI18n } from "@/components/i18n-provider"; +import { AppCatalog } from "@/components/apps/AppCatalog"; + +type Tab = "folder" | "repositories" | "url" | "server" | "apps"; + +/** One-time gh-CLI repo-read consent flag (per browser — desktop is single-user). */ +const GH_CLI_CONSENT_KEY = "openship.gh-cli-consent"; + +interface TabItem { + key: Tab; + label: string; + icon: IconName; +} + +export function LibraryContent() { + const { t } = useI18n(); + const router = useRouter(); + const pathname = usePathname(); + const searchParams = useSearchParams(); + const migrating = pathname === migrationHref(); + const requestedSource = parseMigrationSource(searchParams.get("from")); + const { + state, + connected, + connecting, + loading, + capabilities, + connect, + cliAction, + accounts, + selectedOwner, + setSelectedOwner, + refresh, + installUrl, + } = useGitHub(); + // Server-paginated repo list for the Library (own hook so the shared + // GitHubContext.repos — used by the GitSettings + migration pickers — stays a + // full-set, client-side list). Fetches a page at a time + authoritative counts. + const libRepos = useLibraryRepos(selectedOwner, connected); + const { selfHosted, deployMode } = usePlatform(); + // Only the desktop app can read the user's folder off disk (native picker + + // co-located API). A remote self-hosted browser uploads it instead. + const isDesktop = deployMode === "desktop"; + const { connected: cloudConnected } = useCloud(); + + // Cloud has a dedicated URL tab. Self-hosted instances keep its shortcut in + // the account row; both use the same selection and public import flow. + const [selectedTab, setActiveTab] = useState("repositories"); + const activeTab = migrating ? "server" : selectedTab; + const appsTabRef = useRef(null); + const importingUrl = activeTab === "url"; + const [showMigrate, setShowMigrate] = useState(migrating); + const [migrationSource, setMigrationSource] = useState( + requestedSource, + ); + // The shared layout keeps an import draft/progress mounted while browsing + // other Library tabs. Browser navigation still selects the source from its URL. + useEffect(() => { + if (!migrating) return; + setShowMigrate(true); + setMigrationSource(requestedSource); + }, [migrating, requestedSource]); + + const importUrl = () => setActiveTab("url"); + const selectOwner = (login: string) => { + setActiveTab("repositories"); + if (login) setSelectedOwner(login); + }; + const addAccount = () => { + setActiveTab("repositories"); + const app = capabilities?.methods.find((method) => method.kind === "app"); + const available = app + ? app.available && (!app.requiresCloud || cloudConnected) + : !selfHosted || !!installUrl; + if (available) void connect("oauth"); + else router.push("/settings?tab=git"); + }; + const sourceHeader = ( +
+ +
+ ); + + // First-run consent before the gh-CLI source lists repos. The gh path runs + // entirely on this machine (nothing to the cloud), but we ask once so the + // Library doesn't silently enumerate the user's repos on first open. + const [ghCliConsent, setGhCliConsent] = useState(true); // optimistic until localStorage reads + useEffect(() => { + setGhCliConsent(localStorage.getItem(GH_CLI_CONSENT_KEY) === "1"); + }, []); + const allowGhCli = useCallback(() => { + localStorage.setItem(GH_CLI_CONSENT_KEY, "1"); + setGhCliConsent(true); + }, []); + // Gate ONLY a credential we found on the host by ourselves. A device sign-in or + // a pasted token was handed over by the operator inside Openship — asking them + // to consent again to the thing they just did is a dead end that made a fresh + // token look broken until the prompt was noticed and accepted. + const needsGhCliConsent = + state.primary === "gh-cli" && + (state.sources.ghCli.method ?? "host-cli") === "host-cli" && + !ghCliConsent; + + // Folder import is available on self-hosted instances. Cloud offers a Git URL tab. + const tabs: TabItem[] = [ + { key: "apps", label: t.dashboard.pages.apps.title, icon: "grid" }, + { key: "repositories", label: t.library.page.tabs.github, icon: "github" }, + ...(selfHosted + ? [{ key: "folder" as const, label: t.library.page.tabs.folder, icon: "folder-out" as const }] + : [{ key: "url" as const, label: t.library.page.tabs.gitUrl, icon: "link" as const }]), + { key: "server", label: t.migration.entry.tab, icon: "migration" }, + ]; + + return ( + + {/* ── Header ───────────────────────────────────────────── */} + {/* No primary action here (the tabs below are the action), so the shared ⋮ + help menu sits alone at the title's trailing edge — level with the + heading, matching the Projects / Apps headers. */} +
+
+

+ {t.library.page.title} +

+

{t.library.page.subtitle}

+
+ +
+ + {/* ── Tabs ─────────────────────────────────────────────── */} +
+ {tabs.map((tab) => { + const Icon = tab.icon; + const selected = + activeTab === tab.key || (selfHosted && importingUrl && tab.key === "repositories"); + return ( + + ); + })} +
+ + {/* ── Main Grid ──────────────────────────────────────────── */} +
+ {/* ── LEFT COLUMN ────────────────────────────────────────── */} +
+ {activeTab === "apps" ? ( + + ) : selfHosted && activeTab === "folder" ? ( + // Desktop reads the folder off disk; remote self-hosted browsers upload it. + isDesktop ? ( + + ) : ( + + ) + ) : importingUrl ? ( + + ) : loading ? ( + + ) : !connected ? ( + { + setActiveTab("apps"); + appsTabRef.current?.focus(); + }} + selfHosted={selfHosted} + /> + ) : needsGhCliConsent ? ( + + ) : ( + + )} +
+ + {/* ── RIGHT COLUMN ───────────────────────────────────────── */} + +
+ + {(migrating || showMigrate) && ( +
+ router.push(migrationHref(source), { scroll: false })} + onClose={() => { + setShowMigrate(false); + setMigrationSource(null); + setActiveTab("repositories"); + router.push("/library", { scroll: false }); + }} + /> +
+ )} +
+ ); +} diff --git a/apps/dashboard/src/app/(dashboard)/library/layout.tsx b/apps/dashboard/src/app/(dashboard)/library/layout.tsx new file mode 100644 index 000000000..c0ceb5ea5 --- /dev/null +++ b/apps/dashboard/src/app/(dashboard)/library/layout.tsx @@ -0,0 +1,13 @@ +import { Suspense, type ReactNode } from "react"; +import { LibraryContent } from "./LibraryContent"; + +export default function LibraryLayout({ children }: { children: ReactNode }) { + return ( + <> + + + + {children} + + ); +} diff --git a/apps/dashboard/src/app/(dashboard)/library/migrate/page.tsx b/apps/dashboard/src/app/(dashboard)/library/migrate/page.tsx new file mode 100644 index 000000000..063704560 --- /dev/null +++ b/apps/dashboard/src/app/(dashboard)/library/migrate/page.tsx @@ -0,0 +1,4 @@ +// A direct entry into the same Library flow, including a source selected by ?from=. +export default function LibraryMigrationPage() { + return null; +} diff --git a/apps/dashboard/src/app/(dashboard)/library/page.test.tsx b/apps/dashboard/src/app/(dashboard)/library/page.test.tsx index 63204a84e..0b0ded45a 100644 --- a/apps/dashboard/src/app/(dashboard)/library/page.test.tsx +++ b/apps/dashboard/src/app/(dashboard)/library/page.test.tsx @@ -2,7 +2,7 @@ import { act } from "react"; import { createRoot, type Root } from "react-dom/client"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; -import LibraryPage from "./page"; +import { LibraryContent } from "./LibraryContent"; import { decodeSlug } from "@/utils/repoSlug"; const h = vi.hoisted(() => ({ @@ -16,8 +16,13 @@ const h = vi.hoisted(() => ({ appAvailable: true, requiresCloud: false, cloudConnected: true, + href: "/library", +})); +vi.mock("next/navigation", () => ({ + useRouter: () => ({ push: h.push }), + usePathname: () => new URL(h.href, "https://app.test").pathname, + useSearchParams: () => new URL(h.href, "https://app.test").searchParams, })); -vi.mock("next/navigation", () => ({ useRouter: () => ({ push: h.push }) })); vi.mock("@/lib/auth-client", () => ({ useSession: () => ({ data: { user: { id: "user" }, session: { activeOrganizationId: "org" } }, @@ -74,6 +79,14 @@ vi.mock("./components/LibrarySidebar", () => ({ LibrarySidebar: () => null })); vi.mock("./components/LocalProjects", () => ({ LocalProjects: () => null })); vi.mock("./components/FolderUpload", () => ({ FolderUpload: () => null })); vi.mock("@/components/apps/AppCatalog", () => ({ AppCatalog: () =>
App catalog
})); +vi.mock("@/components/migration/PlatformMigrationWizard", () => ({ + PlatformMigrationWizard: ({ provider, onBack }: { provider: string; onBack: () => void }) => ( +
+ + +
+ ), +})); vi.mock("@/components/migration/ServerMigrationWizard", () => ({ ServerMigrationWizard: ({ variant, onClose }: { variant: string; onClose: () => void }) => (
@@ -95,6 +108,13 @@ beforeEach(() => { appAvailable: true, requiresCloud: false, cloudConnected: true, + href: "/library", + }); + h.push.mockImplementation((href: string) => { + if (href.split("?")[0] === "/library" || href.split("?")[0] === "/library/migrate") { + h.href = href; + root.render(); + } }); localStorage.clear(); vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); @@ -107,7 +127,7 @@ afterEach(async () => { container.remove(); vi.unstubAllGlobals(); }); -const render = () => act(async () => root.render()); +const render = () => act(async () => root.render()); function button(label: string) { const result = Array.from(container.querySelectorAll("button")).find( (node) => (node.getAttribute("aria-label") ?? node.textContent?.trim()) === label, @@ -225,20 +245,73 @@ describe("New Project source actions", () => { h.selfHosted = true; await render(); await click("Import existing project"); + expect(h.href).toBe("/library/migrate"); expect(container.textContent).toContain("Where are you moving from?"); const serverSource = Array.from(container.querySelectorAll("button")).find((node) => node.textContent?.includes("Server or Docker"), ); expect(serverSource).toBeDefined(); await act(async () => serverSource!.click()); + expect(h.href).toBe("/library/migrate?from=server"); const wizard = container.querySelector('[aria-label="Migration"]'); expect(wizard?.getAttribute("data-variant")).toBe("tab"); expect(container.querySelector('[role="dialog"]')).toBeNull(); await click("Apps"); + expect(h.href).toBe("/library"); await click("Import existing project"); + expect(h.href).toBe("/library/migrate?from=server"); expect(container.querySelector('[aria-label="Migration"]')).toBe(wizard); await click("Close migration"); + expect(h.href).toBe("/library"); expect(container.querySelector('[aria-label="Migration"]')).toBeNull(); expect(button("GitHub").getAttribute("aria-pressed")).toBe("true"); }); + + it.each(["railway", "vercel"])("opens a direct %s link without GitHub setup", async (provider) => { + h.connected = false; + h.loading = true; + h.href = `/library/migrate?from=${provider}`; + await render(); + expect(button("Import existing project").getAttribute("aria-pressed")).toBe("true"); + expect(container.querySelector(`[aria-label="${provider} import"]`)).not.toBeNull(); + expect(h.connect).not.toHaveBeenCalled(); + const draft = container.querySelector('[aria-label="Connection name"]')!; + draft.value = "My connection"; + await click("Apps"); + await click("Import existing project"); + expect(container.querySelector('[aria-label="Connection name"]')).toBe(draft); + expect(draft.value).toBe("My connection"); + await click("Choose another source"); + expect(h.href).toBe("/library/migrate"); + expect(container.textContent).toContain("Where are you moving from?"); + }); + + it("follows browser source navigation and resets provider-specific drafts", async () => { + h.href = "/library/migrate?from=railway"; + await render(); + const railwayDraft = container.querySelector('[aria-label="Connection name"]')!; + railwayDraft.value = "Railway connection"; + h.href = "/library/migrate?from=vercel"; + await render(); + expect(container.querySelector('[aria-label="railway import"]')).toBeNull(); + expect(container.querySelector('[aria-label="vercel import"]')).not.toBeNull(); + expect(container.querySelector('[aria-label="Connection name"]')!.value).toBe(""); + h.href = "/library/migrate?from=railway"; + await render(); + expect(container.querySelector('[aria-label="railway import"]')).not.toBeNull(); + h.href = "/library/migrate"; + await render(); + expect(container.textContent).toContain("Where are you moving from?"); + }); + + it.each(["", "?from=unknown", "?from=https%3A%2F%2Fevil.test", "?from=Railway"])( + "shows the source chooser for an absent or unsupported hint (%s)", async (query) => { + h.href = `/library/migrate${query}`; + await render(); + expect(container.textContent).toContain("Where are you moving from?"); + expect(container.querySelector('[aria-label="Migration"]')).toBeNull(); + expect(container.querySelector('[aria-label="railway import"]')).toBeNull(); + expect(h.push).not.toHaveBeenCalled(); + }, + ); }); diff --git a/apps/dashboard/src/app/(dashboard)/library/page.tsx b/apps/dashboard/src/app/(dashboard)/library/page.tsx index 3f82d5fc4..9ab8912f8 100644 --- a/apps/dashboard/src/app/(dashboard)/library/page.tsx +++ b/apps/dashboard/src/app/(dashboard)/library/page.tsx @@ -1,262 +1,4 @@ -"use client"; - -import { Icon as UiIcon, type IconName } from "@repo/ui/icons"; - -import React, { useState, useEffect, useCallback, useRef } from "react"; -import { useRouter } from "next/navigation"; -import { useGitHub } from "@/context/GitHubContext"; -import { usePlatform } from "@/context/PlatformContext"; -import { useCloud } from "@/context/CloudContext"; -import { ConnectPrompt } from "./components/ConnectPrompt"; -import { LoadingSkeleton } from "./components/LoadingSkeleton"; -import { RepositoryList } from "./components/RepositoryList"; -import { useLibraryRepos } from "./useLibraryRepos"; -import { GhCliConsent } from "./components/GhCliConsent"; -import { LocalProjects } from "./components/LocalProjects"; -import { FolderUpload } from "./components/FolderUpload"; -import { LibrarySidebar } from "./components/LibrarySidebar"; -import { UrlImport } from "./components/UrlImport"; -import { RepositoryAccounts } from "./components/RepositoryAccounts"; -import { PageContainer } from "@/components/ui/PageContainer"; -import { HelpMenu } from "@/components/HelpMenu"; -import { MigrationWizard } from "@/components/migration/MigrationWizard"; -import { useI18n } from "@/components/i18n-provider"; -import { AppCatalog } from "@/components/apps/AppCatalog"; - -type Tab = "folder" | "repositories" | "url" | "server" | "apps"; - -/** One-time gh-CLI repo-read consent flag (per browser — desktop is single-user). */ -const GH_CLI_CONSENT_KEY = "openship.gh-cli-consent"; - -interface TabItem { - key: Tab; - label: string; - icon: IconName; -} - +// The shared layout owns the Library and retains import progress across its routes. export default function LibraryPage() { - const { t } = useI18n(); - const router = useRouter(); - const { - state, - connected, - connecting, - loading, - capabilities, - connect, - cliAction, - accounts, - selectedOwner, - setSelectedOwner, - refresh, - installUrl, - } = useGitHub(); - // Server-paginated repo list for the Library (own hook so the shared - // GitHubContext.repos — used by the GitSettings + migration pickers — stays a - // full-set, client-side list). Fetches a page at a time + authoritative counts. - const libRepos = useLibraryRepos(selectedOwner, connected); - const { selfHosted, deployMode } = usePlatform(); - // Only the desktop app can read the user's folder off disk (native picker + - // co-located API). A remote self-hosted browser uploads it instead. - const isDesktop = deployMode === "desktop"; - const { connected: cloudConnected } = useCloud(); - - // Cloud has a dedicated URL tab. Self-hosted instances keep its shortcut in - // the account row; both use the same selection and public import flow. - const [activeTab, setActiveTab] = useState("repositories"); - const appsTabRef = useRef(null); - const importingUrl = activeTab === "url"; - const [showMigrate, setShowMigrate] = useState(false); - - const importUrl = () => setActiveTab("url"); - const selectOwner = (login: string) => { - setActiveTab("repositories"); - if (login) setSelectedOwner(login); - }; - const addAccount = () => { - setActiveTab("repositories"); - const app = capabilities?.methods.find((method) => method.kind === "app"); - const available = app - ? app.available && (!app.requiresCloud || cloudConnected) - : !selfHosted || !!installUrl; - if (available) void connect("oauth"); - else router.push("/settings?tab=git"); - }; - const sourceHeader = ( -
- -
- ); - - // First-run consent before the gh-CLI source lists repos. The gh path runs - // entirely on this machine (nothing to the cloud), but we ask once so the - // Library doesn't silently enumerate the user's repos on first open. - const [ghCliConsent, setGhCliConsent] = useState(true); // optimistic until localStorage reads - useEffect(() => { - setGhCliConsent(localStorage.getItem(GH_CLI_CONSENT_KEY) === "1"); - }, []); - const allowGhCli = useCallback(() => { - localStorage.setItem(GH_CLI_CONSENT_KEY, "1"); - setGhCliConsent(true); - }, []); - // Gate ONLY a credential we found on the host by ourselves. A device sign-in or - // a pasted token was handed over by the operator inside Openship — asking them - // to consent again to the thing they just did is a dead end that made a fresh - // token look broken until the prompt was noticed and accepted. - const needsGhCliConsent = - state.primary === "gh-cli" && - (state.sources.ghCli.method ?? "host-cli") === "host-cli" && - !ghCliConsent; - - // Folder import is available on self-hosted instances. Cloud offers a Git URL tab. - const tabs: TabItem[] = [ - { key: "apps", label: t.dashboard.pages.apps.title, icon: "grid" }, - { key: "repositories", label: t.library.page.tabs.github, icon: "github" }, - ...(selfHosted - ? [{ key: "folder" as const, label: t.library.page.tabs.folder, icon: "folder-out" as const }] - : [{ key: "url" as const, label: t.library.page.tabs.gitUrl, icon: "link" as const }]), - { key: "server", label: t.migration.entry.tab, icon: "migration" }, - ]; - - return ( - - {/* ── Header ───────────────────────────────────────────── */} - {/* No primary action here (the tabs below are the action), so the shared ⋮ - help menu sits alone at the title's trailing edge — level with the - heading, matching the Projects / Apps headers. */} -
-
-

- {t.library.page.title} -

-

{t.library.page.subtitle}

-
- -
- - {/* ── Tabs ─────────────────────────────────────────────── */} -
- {tabs.map((tab) => { - const Icon = tab.icon; - const selected = - activeTab === tab.key || (selfHosted && importingUrl && tab.key === "repositories"); - return ( - - ); - })} -
- - {/* ── Main Grid ──────────────────────────────────────────── */} -
- {/* ── LEFT COLUMN ────────────────────────────────────────── */} -
- {activeTab === "apps" ? ( - - ) : selfHosted && activeTab === "folder" ? ( - // Desktop reads the folder off disk; remote self-hosted browsers upload it. - isDesktop ? ( - - ) : ( - - ) - ) : importingUrl ? ( - - ) : loading ? ( - - ) : !connected ? ( - { - setActiveTab("apps"); - appsTabRef.current?.focus(); - }} - selfHosted={selfHosted} - /> - ) : needsGhCliConsent ? ( - - ) : ( - - )} -
- - {/* ── RIGHT COLUMN ───────────────────────────────────────── */} - -
- - {showMigrate && ( -
- { - setShowMigrate(false); - setActiveTab("repositories"); - }} - /> -
- )} -
- ); + return null; } diff --git a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/AdvancedSettings.tsx b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/AdvancedSettings.tsx index fa09be27a..96b71d21c 100644 --- a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/AdvancedSettings.tsx +++ b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/AdvancedSettings.tsx @@ -27,6 +27,7 @@ import { isSchemaAppTemplate } from "@/components/app-settings/AppSettingsForm"; import { AppConfiguration } from "./AppConfiguration"; import { BuildSettings } from "./BuildSettings"; import { CloudStaticHosting } from "@/components/project-settings/CloudStaticHosting"; +import { ProjectIconSetting } from "@/components/project-settings/ProjectIconSetting"; interface Props { onDeleteProject: (wipeVolumes?: boolean, recordOnly?: boolean) => void; @@ -357,6 +358,7 @@ export const AdvancedSettings = ({ onDeleteProject }: Props) => { and collided — and since this card has no `overflow-hidden` (the ⋮ menu needs to escape it), the collision spilled into the neighbouring card. */}
+
-
-
+
+
@@ -65,192 +58,82 @@ function SectionCard({ ); } -function Row({ - label, - value, - mono, -}: { - label: string; - value?: React.ReactNode; - mono?: boolean; -}) { - const empty = value === undefined || value === null || value === ""; - return ( -
- {label} - - {empty ? — : value} - -
- ); -} - +/** Project-owned settings stay here regardless of the number of workloads. */ export const BuildSettings = () => { - const { buildData, projectData, servicesData, id, updateBuild, refreshEnvironment } = useProjectSettings(); + const { buildData, projectData, servicesData, id } = useProjectSettings(); const { t } = useI18n(); - const router = useRouter(); + const application = projectApplication(projectData, servicesData.services); + const hasRepo = Boolean(projectData.gitOwner && projectData.gitRepo); + const composeSource = + Boolean(projectData.composePath) || isServicesFramework(projectData.framework); - const isWebmail = projectData?.framework === "webmail"; - const services = servicesData.services; - // SERVICE-FIRST = the project itself is a set of services (a compose-stack - // project) or a monorepo of sub-apps — its config genuinely lives per-service. - // A single/static APP that merely had a sidecar service ADDED is NOT - // service-first: it keeps its own primary-app Configuration below. Keyed on - // the project's framework, never on "a service row exists" (which conflates - // the two — the whole point of this fix). - const isServiceFirst = - services.some((service) => service.kind === "monorepo") || - isServicesFramework(projectData?.framework); - - // Edit = the deploy wizard, rehydrated from this project. The single place - // config is editable; this tab never mutates it. - const hasRepo = Boolean(projectData?.gitOwner && projectData?.gitRepo); - const editSlug = hasRepo - ? encodeRepoSlug(projectData!.gitOwner!, projectData!.gitRepo!) - : projectData?.localPath - ? encodeLocalSlug(projectData.localPath) - : null; - const openWizard = () => { - // mode=config → the wizard SAVES config (no deploy); see Sidebar handleSave. - if (editSlug) router.push(`/deploy/${editSlug}?projectId=${id}&mode=config`); - }; - - const EditButton = () => - editSlug ? ( - - ) : null; - - // ── Webmail: fully managed, nothing to show or edit. ────────────────── - if (isWebmail) { + if (projectData.framework === "webmail") { return ( -
- -
+ ); } - // ── Service-first project: config lives per-service in the Services tab. - // (A single/static app with an added sidecar service falls through to the - // single-app config below — it is NOT service-first.) ── - if (isServiceFirst) { - return ( -
- {hasRepo && } - - - -
- ); - } - - // ── Single-app: read-only configuration summary. ────────────────────── - // A static site has no runtime process to isolate — the edge serves its files - // from the shared static volume. Its `runtimeMode` is still "docker" because it - // BUILT in a Docker sandbox, so reporting "Sandboxed (container)" here mislabels a - // build detail as a runtime one and contradicts the Start command row's "Static - // (no server)". Say plainly there is no runtime instead. - // A worker shares hasServer=false with a static site but DOES run a process - // (sandboxed like a web app), so classify via the resolved workload — only a - // static site has "no runtime" (#538). - const workload = workloadOf(buildData); - const runtimeModeLabel = - workload === "static" - ? projectData?.deployTarget === "cloud" - ? t.projectSettings.cloudStaticHosting[projectData.cloudStaticHosting ?? "pages"] - : t.projectSettings.build.runtime.modeStatic - : projectData?.runtimeMode === "docker" - ? t.projectSettings.build.runtime.modeSandboxed - : projectData?.runtimeMode === "bare" - ? t.projectSettings.build.runtime.modeDirect - : t.projectSettings.build.runtime.modeDefault; - return (
- { updateBuild(commands); await refreshEnvironment(); }} /> - - {hasRepo && (projectData?.buildKind === "dockerfile" || projectData?.framework === "docker") && } - -
} - > -
- - - - {/* Resources moved to the editable ResourceSettings card below — one - source, and it covers self-hosted (where limits are now a real - choice) instead of being cloud-only and read-only. */} - - - - {getRuntimeVersionPolicy(projectData?.framework) && } - {projectData?.runtimeImage && } - - - {/* Only when pinned — a blank row would be noise for the vast majority - of projects, whose compose file (if any) sits at the root. */} - {buildData.composePath && ( - - )} - -
-
- + {application && ( + + {t.projectDetail.services.detail.tabs.settings} + + + } + /> + )} + {hasRepo && composeSource && ( + + )} - - {/* Project-wide limits also apply to companion services on a static site. */} - {(workload !== "static" || services.length > 0) && } - - {/* Storage — persistent paths + object storage. Editable in place (see the - component's own note on why it doesn't route through the wizard). Only - meaningful for a project with a running container. */} - {workload !== "static" && } +
); }; -/** Shared project inputs remain editable for single apps, Compose and monorepos. */ +/** One shared editor, accessible here and through workload shortcuts. */ export function ProjectEnvironmentSettings() { - const { id } = useProjectSettings(); + const { id, projectData, servicesData } = useProjectSettings(); const { t } = useI18n(); const [envOpen, setEnvOpen] = useState(false); + const application = projectApplication(projectData, servicesData.services); + const opensApplication = application?.mode === "project" && + !servicesData.services.some((service) => !service.applicationMode); return ( <> - {/* Environment variables — edited in place via a safe per-variable editor - (diff-merge; untouched secrets are never re-sent), NOT the wizard. */} + {t.projectDetail.services.detail.tabs.env} + + + ) : ( - } + )} /> - - setEnvOpen(false)} /> + {!opensApplication && ( + setEnvOpen(false)} /> + )} ); } diff --git a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/DomainSettings.tsx b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/DomainSettings.tsx index df5460400..49a48108e 100644 --- a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/DomainSettings.tsx +++ b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/DomainSettings.tsx @@ -11,6 +11,7 @@ import { useRouter } from "next/navigation"; import { useProjectSettings } from "@/context/ProjectSettingsContext"; import { RoutingConfigCard } from "./RoutingConfigCard"; import { RouteRules } from "./RouteRules"; +import { DomainResponseRoutes } from "@/components/routing/DomainResponseRoutes"; import { RoutingUnsyncedCallout } from "./RoutingUnsyncedCallout"; import { localizeDiagnosisMessage } from "@/lib/i18n/localize-issues"; import { invalidateProjectCaches } from "@/hooks/useProjectEndpoints"; @@ -338,12 +339,14 @@ function resolveDomainSsl(hostname: string, domain: any, baseDomain: string, t: } interface DomainSettingsProps { + /** Primary app routes only, including before it has a backing service row. */ + applicationScope?: boolean; /** Keep domain management inside a service, with an optional port selected. */ serviceScope?: { serviceId: string; port?: number; add?: boolean }; onRoutesChanged?: () => void | Promise; } -export const DomainSettings = ({ serviceScope, onRoutesChanged }: DomainSettingsProps = {}) => { +export const DomainSettings = ({ applicationScope = false, serviceScope, onRoutesChanged }: DomainSettingsProps = {}) => { const { domainsData, updateDomains, @@ -673,7 +676,7 @@ export const DomainSettings = ({ serviceScope, onRoutesChanged }: DomainSettings const projectHasServices = Number(projectData.serviceCount ?? 0) > 0 || services.length > 0; const hasProjectLevelRouting = - !serviceScope && ((Array.isArray(projectData.publicEndpoints) && projectData.publicEndpoints.length > 0) || + !serviceScope && (applicationScope || (Array.isArray(projectData.publicEndpoints) && projectData.publicEndpoints.length > 0) || !projectHasServices); const draftPublicEndpoints = useMemo( () => @@ -717,6 +720,7 @@ export const DomainSettings = ({ serviceScope, onRoutesChanged }: DomainSettings (typeof endpoint?.id === "string" ? domainById.get(endpoint.id) : undefined) || domainByHostname.get(hostname) || null; + if (applicationScope && !serviceScope && domain?.serviceId) return null; const mappedPort = endpoint?.port !== undefined && endpoint?.port !== null ? String(endpoint.port) : projectRuntimePort; @@ -754,6 +758,8 @@ export const DomainSettings = ({ serviceScope, onRoutesChanged }: DomainSettings }) .filter((domain): domain is DomainSummaryItem => domain !== null); }, [ + applicationScope, + serviceScope?.serviceId, projectData.publicEndpoints, projectData.activeDeploymentId, projectData.awaitingDecision, @@ -1575,7 +1581,7 @@ export const DomainSettings = ({ serviceScope, onRoutesChanged }: DomainSettings // Per-service domains have no endpoint order to reorder — primary is the // domain row's isPrimary flag. Flip it via the API, then reflect locally // (exactly one primary per project). getPrimaryByProject picks this up for - // the project's canonical URL / favicon / analytics, and it survives + // the project's canonical URL / analytics, and it survives // redeploys (service route registration preserves an existing isPrimary). const handleSetPrimaryServiceDomain = async (summary: DomainSummaryItem) => { if (!summary.domainId || summary.isPrimary) return; @@ -1856,9 +1862,9 @@ export const DomainSettings = ({ serviceScope, onRoutesChanged }: DomainSettings /** Cards actually rendered — gates "Set as primary", which needs a choice. */ const totalRouteCards = serviceRouteCards.length + orphanDomainCards.length; - const inServiceScope = (summary: DomainSummaryItem) => !serviceScope || ( - summary.serviceId === serviceScope.serviceId && (!portFilter || summary.mappedPort === portFilter) - ); + const inServiceScope = (summary: DomainSummaryItem) => serviceScope + ? summary.serviceId === serviceScope.serviceId && (!portFilter || summary.mappedPort === portFilter) + : !applicationScope || !summary.serviceId; const visibleServiceRoutes = serviceRouteCards.filter(({ summary }) => inServiceScope(summary)); const visibleOrphanRoutes = orphanDomainCards.filter(inServiceScope); @@ -2003,6 +2009,11 @@ export const DomainSettings = ({ serviceScope, onRoutesChanged }: DomainSettings + ) : undefined} protectionControl={} menuActions={menuActions} @@ -2718,7 +2729,7 @@ export const DomainSettings = ({ serviceScope, onRoutesChanged }: DomainSettings {/* Routing (rewrites/redirects/headers) — advanced, sits AFTER the domain/route cards so the primary domain list leads the page. */} - {!serviceScope && } + {!serviceScope && !applicationScope && !isCloudProject && } {editingRouteService && editingRouteTarget && routeDraft && (
) : null}
+ {responseRoutes}
); } diff --git a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/EnvVarsEditor.tsx b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/EnvVarsEditor.tsx index 4a7ce8568..de37857c6 100644 --- a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/EnvVarsEditor.tsx +++ b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/EnvVarsEditor.tsx @@ -1,46 +1,12 @@ -import { useComposeReview } from "@/hooks/useComposeReview"; -import { reportCaughtError as observeCaughtError } from "@repo/core/diagnostics"; -import { Icon as UiIcon } from "@repo/ui/icons"; -import React, { useCallback, useEffect, useState } from "react"; -import { useRouter } from "next/navigation"; +"use client"; + +import { useState } from "react"; import { Modal } from "@/components/ui/Modal"; -import { AppLogo } from "@/components/AppLogo"; -import { projectsApi, deployApi } from "@/lib/api"; -import { connectionsApi, type ProjectConnection } from "@/lib/api/connections"; -import { getApiErrorMessage } from "@/lib/api/client"; -import { useToast } from "@/context/ToastContext"; -import { useCloudDeployPricing } from "@/hooks/useCloudDeployPricing"; -import { useI18n, interpolate } from "@/components/i18n-provider"; -import { useProjectSettings } from "@/context/ProjectSettingsContext"; -import type { EnvironmentVariable } from "@/components/import-project/types"; -import { EnvironmentTextEditor } from "@/components/import-project/EnvironmentTextEditor"; -import { canRefreshProjectEnvironment } from "@/lib/project-application"; -import { openTriggeredBuild } from "@/lib/deploy-nav"; -import { - computeProjectEnvDiff, - createProjectEnvEditState, - type PersistedProjectEnv, -} from "@/lib/project-env-diff"; - -/** - * Per-variable production env editor (modal). Safe by design: - * - reads via GET /:id/env (secret VALUES come back masked); - * - saves a DIFF via PATCH /:id/env (merge) — only added/changed/deleted keys - * are touched. A secret the user didn't re-enter is never re-sent, so masked - * secrets can't be overwritten and untouched vars are never wiped. - */ - -const ENVIRONMENT = "production"; - -interface Row extends EnvironmentVariable { - /** Stable local id for React keys. */ - uid: string; - isSecret: boolean; -} - -let uidCounter = 0; -const nextUid = () => `row-${uidCounter++}`; +import { Button } from "@/components/ui/button"; +import { useI18n } from "@/components/i18n-provider"; +import { ProjectEnvironmentPanel } from "@/components/project-settings/ProjectEnvironmentPanel"; +/** Settings opens the same project environment panel as Apps & Services. */ export function EnvVarsEditor({ projectId, isOpen, @@ -50,425 +16,25 @@ export function EnvVarsEditor({ isOpen: boolean; onClose: () => void; }) { - const { showToast } = useToast(); const { t } = useI18n(); - const { projectData, servicesData } = useProjectSettings(); - const router = useRouter(); - const canRefresh = canRefreshProjectEnvironment(projectData, servicesData.services); - const showCloudPricing = useCloudDeployPricing(projectData.workspaceId); - const showComposeReview = useComposeReview(projectId); - const hasActiveDeployment = Boolean(projectData?.activeDeploymentId); - const [rows, setRows] = useState([]); - const [baseline, setBaseline] = useState(null); - const [loading, setLoading] = useState(false); - const [saving, setSaving] = useState(false); - const [reveal, setReveal] = useState>({}); - // Saving persists the values; applying explicitly updates the active deployment. - const [pendingApply, setPendingApply] = useState(false); - const [applying, setApplying] = useState(false); - // Env keys wired IN by a service connection (keyed by envKey). These render as - // read-only "linked integration" rows — click opens the integration detail. - const [linked, setLinked] = useState>({}); - const [linkDetail, setLinkDetail] = useState(null); - const [unlinking, setUnlinking] = useState(false); - - const load = useCallback(async () => { - setLoading(true); - setRows([]); - setBaseline(null); - setLinked({}); - try { - const res = await projectsApi.getEnv(projectId); - const envState = createProjectEnvEditState(res?.data ?? [], ENVIRONMENT); - const loaded: Row[] = envState.rows.map((row) => ({ - ...row, - uid: nextUid(), - isSecret: row.isSecret ?? false, - })); - setRows(loaded); - setBaseline(envState.baseline); - // Best-effort: which of these keys are managed by a service connection? - const conns = await connectionsApi.list(projectId).catch((diagnosticFailure) => { observeCaughtError(diagnosticFailure, "dashboard/app/(dashboard)/projects/[id]/components/EnvVarsEditor"); return ({ data: [] }); }); - const map: Record = {}; - for (const cn of conns?.data ?? []) map[cn.envKey] = cn; - setLinked(map); - } catch (err) { - showToast(getApiErrorMessage(err, t.projectSettings.envVars.toast.loadFailed), "error", t.projectSettings.envVars.toast.loadFailedTitle); - } finally { - setLoading(false); - } - }, [projectId, showToast, t]); - - useEffect(() => { - if (isOpen) { - setReveal({}); - setPendingApply(false); - void load(); - } - }, [isOpen, load]); - - const update = (uid: string, patch: Partial) => - setRows((prev) => prev.map((r) => (r.uid === uid ? { ...r, ...patch } : r))); - - const addRow = (atTop = false) => - setRows((prev) => { - const row = { uid: nextUid(), key: "", value: "", visible: true, isSecret: false }; - return atTop ? [row, ...prev] : [...prev, row]; - }); - - const removeRow = (uid: string) => setRows((prev) => prev.filter((r) => r.uid !== uid)); - - const handleSave = async () => { - if (baseline === null) return; - const result = computeProjectEnvDiff(rows, baseline); - if (!result.ok) { - showToast(result.error, "error", t.projectSettings.envVars.toast.validationTitle); - return; - } - const { upserts, deletes } = result.diff; - - if (upserts.length === 0 && deletes.length === 0) { - // Say so, rather than just closing. An untouched masked secret is deliberately - // absent from the diff (we never hold its value), so re-entering the SAME `.env` - // over a set of saved secrets produces an empty diff — and a dialog that shut - // itself with no word read as "Save did nothing", so operators retried and - // concluded env saving was broken (#587). - showToast( - t.projectSettings.envVars.toast.noChanges, - "info", - t.projectSettings.envVars.toast.noChangesTitle, - ); - onClose(); - return; - } - - setSaving(true); - try { - await projectsApi.mergeEnv(projectId, { environment: ENVIRONMENT, upserts, deletes }); - showToast(t.projectSettings.envVars.toast.saved, "success", t.projectSettings.envVars.toast.savedTitle); - // A persisted env change only reaches the RUNNING service on a deploy. - // Offer the deployment mode this project supports; otherwise the saved - // values will take effect on its first deployment. - if (hasActiveDeployment) { - await load(); // re-sync so the diff resets to the saved state - setPendingApply(true); - } else { - onClose(); - } - } catch (err) { - showToast(getApiErrorMessage(err, t.projectSettings.envVars.toast.saveFailed), "error", t.projectSettings.envVars.toast.saveFailedTitle); - } finally { - setSaving(false); - } - }; - - const handleApply = async () => { - setApplying(true); - try { - const res = await deployApi.trigger( - canRefresh ? { projectId, refresh: true } : { projectId, smartRoute: true }, - ); - if (canRefresh) { - showToast(t.projectSettings.envVars.toast.applying, "success", t.projectSettings.envVars.toast.applyingTitle); - } else { - openTriggeredBuild(router, res, projectId); - } - onClose(); - } catch (err) { - if (!showComposeReview(err) && !showCloudPricing(err)) { - showToast( - getApiErrorMessage( - err, - canRefresh - ? t.projectSettings.envVars.toast.applyFailed - : t.projects.redeploy.couldNotRedeploy, - ), - "error", - canRefresh ? t.projectSettings.envVars.toast.applyFailedTitle : t.projects.redeploy.errorTitle, - ); - } - } finally { - setApplying(false); - } - }; - - // Remove a linked connection: drops the injected env var + the link, then - // re-syncs. The connection API already requests a redeploy for a live target. - const handleUnlink = async () => { - if (!linkDetail) return; - setUnlinking(true); - try { - await connectionsApi.remove(projectId, linkDetail.id); - showToast(t.projects.connections.removed, "success"); - setLinkDetail(null); - await load(); - } catch (err) { - showToast(getApiErrorMessage(err, t.projects.connections.failed), "error"); - } finally { - setUnlinking(false); - } - }; - + const [busy, setBusy] = useState(false); return ( - <> - - {pendingApply ? ( - - ) : ( - - )} +
+
} > -
- {pendingApply && ( -
- -
-

- {canRefresh - ? t.projectSettings.envVars.applyTitle - : t.projectSettings.envVars.rebuildTitle} -

-

- {canRefresh ? t.projectSettings.envVars.applyText : t.projectSettings.envVars.rebuildText} -

-
-
- )} -
-
- -
-
-

{t.projectSettings.envVars.title}

-

- {t.projectSettings.envVars.description} -

-
-
- - {loading ? ( -
- -
- ) : ( -
-
- - row.originalKey && linked[row.originalKey]) - .map((row) => row.key)} - disabled={saving || applying || baseline === null} - className="inline-flex h-8 items-center gap-1.5 rounded-lg bg-muted/60 px-3 text-xs font-medium text-foreground hover:bg-muted disabled:opacity-50" - onChange={(next) => - setRows(next.map((row) => ({ - ...row, - uid: rows.find((previous) => previous.key === row.key)?.uid ?? nextUid(), - isSecret: row.isSecret ?? false, - }))) - } - /> -
- {rows.length === 0 ? ( -

- {t.projectSettings.envVars.empty} -

- ) : ( - rows.map((r) => { - // A connection-managed key renders as a read-only integration - // chip — not editable here; click opens the integration detail. - const conn = r.originalKey ? linked[r.originalKey] : undefined; - if (conn) { - return ( - - ); - } - const showValue = reveal[r.uid] || (!r.isSecret && !r.preserveValue); - return ( -
- update(r.uid, { key: e.target.value })} - placeholder={t.projectSettings.envVars.keyPlaceholder} - spellCheck={false} - className="h-9 w-2/5 rounded-lg border border-border/50 bg-muted/20 px-3 font-mono text-[13px] text-foreground outline-none transition-colors focus:border-primary/40" - /> -
- update(r.uid, { value: e.target.value, preserveValue: false })} - placeholder={r.preserveValue ? t.projectSettings.envVars.secretPlaceholder : t.projectSettings.envVars.valuePlaceholder} - spellCheck={false} - className="h-9 w-full rounded-lg border border-border/50 bg-muted/20 px-3 pe-9 font-mono text-[13px] text-foreground outline-none transition-colors focus:border-primary/40" - /> - -
- - -
- ); - }) - )} - - -
- )} -
+ {isOpen && }
- - {/* Linked-integration detail — opens above the env editor when a linked row - is clicked. Shows the source app + reach mode; the value lives in the - connection, so the only action here is removing the link. */} - {linkDetail && ( - setLinkDetail(null)} maxWidth="460px" width="92vw" zIndex={10010} showCloseButton> -
-
-
- -
-
-

- {t.projectSettings.envVars.linkedTitle} -

-

{linkDetail.sourceName}

-
-
- -
-
- {t.projectSettings.envVars.title} - {linkDetail.envKey} -
-
- {t.projects.connections.modeInternalShort}/{t.projects.connections.modePublicShort} - - {linkDetail.mode === "internal" ? : } - {linkDetail.mode === "internal" - ? t.projects.connections.modeInternalShort - : t.projects.connections.modePublicShort} - -
-
- -

- {interpolate(t.projectSettings.envVars.linkedBody, { app: linkDetail.sourceName })} -

- -
- - -
-
-
- )} - ); } diff --git a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/LogsSettings.tsx b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/LogsSettings.tsx index 1aebaf000..380b2814a 100644 --- a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/LogsSettings.tsx +++ b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/LogsSettings.tsx @@ -22,7 +22,7 @@ import { Button } from "@/components/ui/button"; type LogsTab = "terminal" | "server"; -export const LogsSettings = () => { +export const LogsSettings = ({ applicationOnly = false }: { applicationOnly?: boolean }) => { const { projectData, buildData, @@ -52,7 +52,7 @@ export const LogsSettings = () => { }) !== "static"; const searchParams = useSearchParams(); const router = useRouter(); - const serviceIdFromUrl = searchParams.get("service"); + const serviceIdFromUrl = applicationOnly ? null : searchParams.get("service"); const roleFromUrl = searchParams.get("role"); const roleReplicaFromUrl = Number(searchParams.get("replica") ?? 1); const roleDeploymentFromUrl = searchParams.get("deploymentId"); @@ -92,15 +92,15 @@ export const LogsSettings = () => { }, [requestedTarget, urlSelectionChanged]); // Public (exposed) services lead the target picker — the ones users browse to. const services = useMemo( - () => sortServicesByPublicFirst(servicesData.services), - [servicesData.services], + () => applicationOnly ? [] : sortServicesByPublicFirst(servicesData.services), + [servicesData.services, applicationOnly], ); const servicesLoading = servicesData.isLoading; const servicesLoaded = !servicesData.isLoading; const hasServices = services.length > 0; // The shared application model distinguishes companion services from a // Compose/monorepo app whose primary runtime already has a service row. - const hasProjectRuntime = effectiveHasServer && hasSeparateApplication(projectData, services); + const hasProjectRuntime = effectiveHasServer && (applicationOnly || hasSeparateApplication(projectData, services)); // Request logs come from the EDGE, not from a runtime process — so anything // served through an edge has them, whether or not it has a container to stream // stdout from. That's every project with a domain: cloud (Oblien's edge proxy) @@ -188,6 +188,7 @@ export const LogsSettings = () => { // filter is a real, shareable, refresh-safe query param (null = project // runtime → no param). Guarded so we only replace when it actually changed. useEffect(() => { + if (applicationOnly) return; if (servicesLoading || urlSelectionChanged) return; if (roleFromUrl && !urlDeepLinkAppliedRef.current) return; const role = roleTargets.find((target) => target.value === selectedTarget); @@ -210,6 +211,7 @@ export const LogsSettings = () => { roleTargets, roleFromUrl, urlSelectionChanged, + applicationOnly, ]); const switchTab = useCallback( diff --git a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/RouteRules.tsx b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/RouteRules.tsx index e10f0b21b..845559b21 100644 --- a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/RouteRules.tsx +++ b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/RouteRules.tsx @@ -69,7 +69,7 @@ export function RouteRules() { setLoading(true); try { const res = await projectsApi.listRouteRules(projectData.id); - const list = res?.rules ?? []; + const list = (res?.rules ?? []).filter(rule => !rule.spec.response); setRules(list); if (!autoOpened.current && list.length > 0) { setOpen(true); diff --git a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/ServicesTab.tsx b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/ServicesTab.tsx index 147194d9b..4c3f35dec 100644 --- a/apps/dashboard/src/app/(dashboard)/projects/[id]/components/ServicesTab.tsx +++ b/apps/dashboard/src/app/(dashboard)/projects/[id]/components/ServicesTab.tsx @@ -2,14 +2,25 @@ import { serviceRepositorySource } from "./services/repository-source"; -import { observedAllSettled, reportCaughtError as observeCaughtError } from "@repo/core/diagnostics"; +import { + observedAllSettled, + reportCaughtError as observeCaughtError, +} from "@repo/core/diagnostics"; import { Icon as UiIcon } from "@repo/ui/icons"; import React, { useEffect, useState, useCallback, useMemo, useRef } from "react"; import Link from "next/link"; import { useProjectSettings } from "@/context/ProjectSettingsContext"; import { usePlatform } from "@/context/PlatformContext"; -import { serviceKind, serviceCanStartWithoutBuild, servicesApi, sortServicesByPublicFirst, type Service, type ServiceContainer, type ServiceInput } from "@/lib/api/services"; +import { + serviceKind, + serviceCanStartWithoutBuild, + servicesApi, + sortServicesByPublicFirst, + type Service, + type ServiceContainer, + type ServiceInput, +} from "@/lib/api/services"; import { getServiceStatus } from "@/components/services/ServiceStatusBadge"; import { Button } from "@/components/ui/button"; import { ConnectionNotice } from "@/components/shared/ConnectionNotice"; @@ -24,10 +35,17 @@ import { useCloudDeployPricing } from "@/hooks/useCloudDeployPricing"; import { AddServiceModal } from "./services/AddServiceModal"; import { LinkedAppsCard } from "./services/LinkedAppsCard"; import { ServiceListItem } from "./services/ServiceListItem"; -import { hasSeparateApplication } from "@/lib/project-application"; +import { + hasSeparateApplication, + projectApplication, + applicationServiceRoute, +} from "@/lib/project-application"; +import { ServiceIcon } from "@/components/services/ServiceIcon"; +import { canConfigureRuntimeRoles } from "@/lib/runtime-roles"; import { RuntimeRoleConfigurationControl } from "@/components/services/RuntimeRoleConfiguration"; import { RuntimeRolesCard } from "@/components/services/RuntimeRoles"; -import { ProjectApplicationCard } from "./services/ProjectApplicationCard"; +import { ProjectApplicationListItem } from "./services/ProjectApplicationListItem"; +import { ProjectApplicationPanel } from "./services/ProjectApplicationPanel"; /** Render a drift diff value (arrays → csv, objects → keys, scalars → string). */ const fmtDriftVal = (v: unknown): string => { @@ -74,7 +92,8 @@ export const ServicesTab = () => { // the full-tab skeleton on every action and tab switch (#666) — rows render // fine while their runtime status is checked separately. const loading = - servicesData.isLoading || (containersLoading && services.length === 0); + servicesData.isLoading || + (containersLoading && services.length === 0 && !hasSeparateApplication(projectData, services)); const projectSlugBase = projectData.slug || projectData.name || "project"; const selectedId = slug?.[1] ?? null; const hasProjectId = Boolean(id && id !== "undefined"); @@ -98,10 +117,10 @@ export const ServicesTab = () => { // surfaces as a bare runtime error overlay instead of this component's // error state. The container read is also the one that can time out // (it reflects live runtime state), so it must not take the tab down. - const [, containersResult] = await observedAllSettled([ - refreshServices(), - servicesApi.containers(id), - ], "dashboard/app/(dashboard)/projects/[id]/components/ServicesTab"); + const [, containersResult] = await observedAllSettled( + [refreshServices(), servicesApi.containers(id)], + "dashboard/app/(dashboard)/projects/[id]/components/ServicesTab", + ); if (request !== runtimeRequest.current) return; if (containersResult.status === "rejected") throw containersResult.reason; const ctRes = containersResult.value; @@ -121,9 +140,11 @@ export const ServicesTab = () => { projectId: id, // Keep identity/port details, but never present a stale health reading // as current after a failed request. - containers: previous.projectId === id - ? previous.containers?.map((container) => ({ ...container, status: "unknown" })) ?? null - : null, + containers: + previous.projectId === id + ? (previous.containers?.map((container) => ({ ...container, status: "unknown" })) ?? + null) + : null, loading: false, error: !isAbortError(e) && e instanceof Error ? e.message : t.projects.services.failedLoad, })); @@ -139,7 +160,35 @@ export const ServicesTab = () => { const containerFor = (serviceId: string) => containers?.find((c) => c.serviceId === serviceId); - const selectedService = services.find((s) => s.id === selectedId); + const application = + servicesData.error && services.length === 0 ? null : projectApplication(projectData, services); + const hasApplication = application?.mode === "project"; + const selectedApplication = + !!application && (selectedId === "application" || selectedId === application.service?.id); + const selectedService = + selectedApplication && application?.mode === "service" + ? application.service + : services.find((s) => s.id === selectedId); + const listedServices = hasApplication + ? services.filter((s) => s.id !== application?.service?.id) + : services; + const entryCount = listedServices.length + (hasApplication ? 1 : 0); + const choices = [ + ...(hasApplication + ? [ + { + id: application?.service?.id ?? "application", + name: projectData.name, + icon: , + }, + ] + : []), + ...listedServices.map((s) => ({ + id: s.id, + name: s.name, + icon: , + })), + ]; // Null for a service with no route — it is reachable on its port, and linking // to a derived `-` host sent people to a name nobody created. @@ -158,7 +207,7 @@ export const ServicesTab = () => { const handleCreateService = async (data: ServiceInput) => { if (!hasProjectId) return; - const result = await servicesApi.create(id, data).catch(error => { + const result = await servicesApi.create(id, data).catch((error) => { showCloudPricing(error); throw error; }); @@ -181,14 +230,22 @@ export const ServicesTab = () => { if (!shouldLaunch) { // Nothing to launch against yet — keep the row and land on its detail so // the user can deploy/start it when ready. - showToast(interpolate(t.projects.services.toastSavedDeploy, { name: data.name }), "success", t.projects.services.toastServiceTitle); + showToast( + interpolate(t.projects.services.toastSavedDeploy, { name: data.name }), + "success", + t.projects.services.toastServiceTitle, + ); router.push(`/projects/${id}/services/${newServiceId}`); return; } // Start this service on the project's selected server. Keep its saved // configuration on failure so the user can inspect and retry the operation. - showToast(interpolate(t.projects.services.toastAddedDeploying, { name: data.name }), "success", t.projects.services.toastServiceTitle); + showToast( + interpolate(t.projects.services.toastAddedDeploying, { name: data.name }), + "success", + t.projects.services.toastServiceTitle, + ); const showStartFailure = async (message: string) => { await fetchData(); showToast(message, "error", data.name); @@ -201,17 +258,26 @@ export const ServicesTab = () => { await showStartFailure(res?.error || t.projects.services.toastDeployFailed); return; } - showToast(interpolate(t.projects.services.toastStarting, { name: data.name }), "success", t.projects.services.toastServiceTitle); + showToast( + interpolate(t.projects.services.toastStarting, { name: data.name }), + "success", + t.projects.services.toastServiceTitle, + ); await fetchData(); router.push(`/projects/${id}/services/${newServiceId}`); }) .catch(async (err) => { - if (showCloudPricing(err, async () => { - const result = await servicesApi.start(id, newServiceId); - if (result?.success === false) throw new Error(t.projects.services.toastDeployFailed); + if ( + showCloudPricing(err, async () => { + const result = await servicesApi.start(id, newServiceId); + if (result?.success === false) throw new Error(t.projects.services.toastDeployFailed); + await fetchData(); + router.push(`/projects/${id}/services/${newServiceId}`); + }) + ) { await fetchData(); - router.push(`/projects/${id}/services/${newServiceId}`); - })) { await fetchData(); return; } + return; + } await showStartFailure(getApiErrorMessage(err, t.projects.services.toastDeployFailed)); }); }; @@ -236,7 +302,11 @@ export const ServicesTab = () => { await fetchData(); } catch (e) { observeCaughtError(e, "dashboard/app/(dashboard)/projects/[id]/components/ServicesTab"); - showToast(e instanceof Error ? e.message : t.projects.services.failedResolveDrift, "error", name); + showToast( + e instanceof Error ? e.message : t.projects.services.failedResolveDrift, + "error", + name, + ); } finally { setDriftBusy(null); } @@ -245,7 +315,12 @@ export const ServicesTab = () => { ); const driftedServices = services.filter((s) => s.drift && s.drift.changes.length > 0); - const hasApplication = !servicesData.error && hasSeparateApplication(projectData, services); + + // Application variables are control-plane data. Keep them reachable even if + // the live container listing is slow or unavailable (including bare runtimes). + if (selectedApplication && hasApplication && !servicesData.isLoading) { + return ; + } /* ── Loading state ─────────────────────────────────────────────── */ if (loading) { @@ -255,7 +330,10 @@ export const ServicesTab = () => {
-
)} {service.domains.length > 0 && (
diff --git a/apps/dashboard/src/components/migration/PlatformMigrationWizard.tsx b/apps/dashboard/src/components/migration/PlatformMigrationWizard.tsx index 86a83d11f..d71e270d4 100644 --- a/apps/dashboard/src/components/migration/PlatformMigrationWizard.tsx +++ b/apps/dashboard/src/components/migration/PlatformMigrationWizard.tsx @@ -63,6 +63,7 @@ export function PlatformMigrationWizard({ const [projectId, setProjectId] = useState(""); const [environmentId, setEnvironmentId] = useState(""); const [discovery, setDiscovery] = useState(); + const [projectName, setProjectName] = useState(""); const [choices, setChoices] = useState>({}); const [targetId, setTargetId] = useState(initialTargetId ?? ""); const [targetName, setTargetName] = useState(""); @@ -263,6 +264,7 @@ export function PlatformMigrationWizard({ const data = await platformMigrationApi.discover({ connectionId, projectId, environmentId }); if (!alive.current) return; setDiscovery(data); + setProjectName(data.projectName); setChoices( Object.fromEntries( data.services.map((service) => [ @@ -289,6 +291,7 @@ export function PlatformMigrationWizard({ const result = await platformMigrationApi.review({ draftId: discovery!.draftId, targetServerId: targetId, + projectName, services: Object.values(choices) .filter((choice) => choice.selected) .map(({ selected: _selected, ...choice }) => choice), @@ -461,6 +464,11 @@ export function PlatformMigrationWizard({ busy={busy} targetId={targetId} targetReady={targetReady} + projectName={projectName} + onProjectName={(name) => { + setProjectName(name); + invalidateReview(); + }} onChoose={(id, patch) => { setChoices((current) => ({ ...current, [id]: { ...current[id], ...patch } })); invalidateReview(); diff --git a/apps/dashboard/src/components/migration/platform-import-flow.test.tsx b/apps/dashboard/src/components/migration/platform-import-flow.test.tsx index ce806260a..4646d0d7a 100644 --- a/apps/dashboard/src/components/migration/platform-import-flow.test.tsx +++ b/apps/dashboard/src/components/migration/platform-import-flow.test.tsx @@ -186,6 +186,170 @@ afterEach(async () => { }); describe("platform import flow", () => { + it("reviews custom domains and provider URLs before starting the import", async () => { + h.review.mockResolvedValue({ + ...plan, + services: plan.services.map((service) => ({ + ...service, + domains: + service.id === "web" + ? [ + { hostname: "shop.example.com", portable: true }, + { hostname: "shop.up.railway.app", portable: false }, + ] + : [], + })), + }); + await render(); + await selectServices(); + await click("Check import"); + const rows = [...host.querySelectorAll("li")]; + expect( + rows.find((row) => row.textContent?.includes("shop.example.com"))?.textContent, + ).toContain("Update DNS"); + expect( + rows.find((row) => row.textContent?.includes("shop.up.railway.app"))?.textContent, + ).toContain("Replace URL"); + expect(host.textContent).toContain("Importing does not change DNS or move traffic"); + expect(host.textContent).toContain("Provider-owned URLs cannot move to OpenShip"); + expect(host.textContent).toContain("OAuth callbacks and webhooks"); + expect(h.start).not.toHaveBeenCalled(); + expect(host.querySelector('a[href$="/domains"]')).toBeNull(); + }); + + it.each([ + ["railway", "shop.up.railway.app"], + ["vercel", "shop.vercel.app"], + ] as const)( + "restores %s domain guidance and links each imported project once", + async (provider, providerDomain) => { + h.getMigration.mockResolvedValue({ + run: { + id: "run", + status: "awaiting_cutover", + projectName: "Commerce", + importedProjects: { web: "commerce", db: "commerce", api: "legacy-api" }, + logs: "", + confirmationToken: "confirmation", + inputSnapshot: { + services: [ + { + id: "web", + name: "Storefront", + domains: [ + { hostname: "shop.example.com", portable: true }, + { hostname: providerDomain, portable: false }, + ], + }, + { id: "db", name: "PostgreSQL", domains: [] }, + { + id: "api", + name: "API", + domains: [{ hostname: "api.example.com", portable: true }], + }, + { + id: "external", + name: "Retained service", + domains: [{ hostname: "external.example.com", portable: true }], + }, + ], + }, + }, + }); + await render(provider, "run"); + const rows = [...host.querySelectorAll("li")]; + const custom = rows.find((row) => row.textContent?.includes("shop.example.com")); + expect(custom?.textContent).toContain("Storefront"); + expect(custom?.textContent).toContain("Update DNS"); + expect(rows.find((row) => row.textContent?.includes(providerDomain))?.textContent).toContain( + "Replace URL", + ); + expect( + rows.find((row) => row.textContent?.includes("api.example.com"))?.textContent, + ).toContain("API"); + expect(host.textContent).not.toContain("external.example.com"); + expect(host.querySelectorAll('a[href="/projects/commerce/domains"]')).toHaveLength(1); + expect(host.querySelectorAll('a[href="/projects/legacy-api/domains"]')).toHaveLength(1); + expect(host.textContent).toContain("apply the exact DNS records shown"); + expect(host.textContent).toContain("Verify DNS and HTTPS"); + expect(h.discover).not.toHaveBeenCalled(); + expect(h.confirmCutover).not.toHaveBeenCalled(); + await click("Confirm cutover complete"); + expect(h.confirmCutover).toHaveBeenCalledWith("run", "confirmation", false, false); + }, + ); + + it("does not request custom DNS changes when only a provider URL needs replacing", async () => { + h.getMigration.mockResolvedValue({ + run: { + id: "run", + status: "awaiting_cutover", + projectName: "Website", + logs: "", + importedProjects: { web: "website" }, + inputSnapshot: { + services: [ + { + id: "web", + name: "Web", + domains: [{ hostname: "website.up.railway.app", portable: false }], + }, + ], + }, + }, + }); + await render("railway", "run"); + expect(host.textContent).toContain("Replace URL"); + expect(host.textContent).toContain("Choose a new address"); + expect(host.textContent).not.toContain("Update DNS"); + expect(host.textContent).not.toContain("Add your custom domains"); + expect(button("Confirm cutover complete").disabled).toBe(true); + }); + + it("keeps internal services private and handles saved runs without domain inventory", async () => { + h.getMigration.mockResolvedValue({ + run: { + id: "run", + status: "awaiting_cutover", + projectName: "Jobs", + logs: "", + importedProjects: { worker: "jobs", db: "jobs" }, + inputSnapshot: { + services: [ + { id: "worker", name: "Worker", domains: [] }, + { id: "db", name: "Database" }, + ], + }, + }, + }); + await render("railway", "run"); + expect(host.textContent).toContain("Keep internal services private"); + expect(host.textContent).not.toContain("Add your custom domains"); + expect(host.querySelectorAll('a[href="/projects/jobs/domains"]')).toHaveLength(1); + }); + + it("shows one imported project when several source services share it", async () => { + h.getMigration.mockResolvedValue({ + run: { + id: "run", + status: "succeeded", + projectName: "Commerce", + importedProjects: { app: "commerce", db: "commerce", redis: "commerce" }, + logs: "", + inputSnapshot: { + services: [ + { id: "app", name: "API" }, + { id: "db", name: "PostgreSQL" }, + ], + }, + }, + }); + await render("railway", "run"); + expect(host.querySelectorAll('a[href="/projects/commerce"]')).toHaveLength(1); + expect(host.textContent).toContain("Imported project"); + expect(host.textContent).toContain("Commerce"); + }); + it("keeps an accepted stop visible through stale polls until the worker finishes", async () => { vi.useFakeTimers(); let current = { @@ -342,22 +506,49 @@ describe("platform import flow", () => { expect(button("Choose services").disabled).toBe(false); }); + it("keeps missing-secret fields visible and focused while typing or clearing a value", async () => { + await render(); + await click("Choose services"); + const secret = host.querySelector('input[type="password"]')!; + await act(async () => secret.focus()); + expect(button("Check import").disabled).toBe(true); + for (const value of ["f", "fixture-secret", "", "replacement-secret"]) { + await fill(secret, value); + expect(secret.closest("[hidden], details:not([open])")).toBeNull(); + expect(document.activeElement).toBe(secret); + expect(secret.value).toBe(value); + expect(button("Check import").disabled).toBe(!value); + } + await click("Check import"); + expect(h.review).toHaveBeenCalledWith( + expect.objectContaining({ + services: expect.arrayContaining([ + expect.objectContaining({ id: "web", env: { API_TOKEN: "replacement-secret" } }), + ]), + }), + ); + }); + it("requires missing secrets, a separate review, and writer pause before starting exactly once", async () => { await render(); await click("Choose services"); expect(button("Check import").disabled).toBe(true); + expect(host.textContent).toContain( + "Services with different Git sources import into separate projects", + ); await fill(host.querySelector('input[type="password"]')!, "fixture-secret"); await click("Check import"); expect(h.review).toHaveBeenCalledWith( expect.objectContaining({ targetServerId: "server-a", + projectName: "Production", services: expect.arrayContaining([ expect.objectContaining({ id: "web", env: { API_TOKEN: "fixture-secret" } }), ]), }), ); expect(h.start).not.toHaveBeenCalled(); - expect(button("Import 2 projects").disabled).toBe(true); + expect(button("Import project").disabled).toBe(true); await act(async () => host.querySelector('[role="checkbox"]')!.click()); let resolve!: (value: unknown) => void; h.start.mockImplementation( @@ -366,7 +557,7 @@ describe("platform import flow", () => { resolve = done; }), ); - const start = button("Import 2 projects"); + const start = button("Import project"); await act(async () => { start.click(); start.click(); @@ -381,6 +572,25 @@ describe("platform import flow", () => { expect(h.push).toHaveBeenCalledWith("/migrations/run"); }); + it("shows separate destination projects before approving a multi-repository import", async () => { + h.review.mockResolvedValueOnce({ + ...plan, + projects: [ + { name: "production-database", serviceIds: ["db"] }, + { name: "production-website", serviceIds: ["web"] }, + ], + }); + await render(); + await selectServices(); + await click("Check import"); + expect(host.textContent).toContain("2 projects · 2 services"); + expect(host.textContent).toContain("Project: production-database"); + expect(host.textContent).toContain("Project: production-website"); + expect(host.textContent).toContain("Dependencies use private connections"); + expect(button("Import project").disabled).toBe(true); + expect(h.start).not.toHaveBeenCalled(); + }); + it("keeps blocked reviews in the editor and requires a fresh review after changing destination", async () => { h.review.mockResolvedValueOnce({ ...plan, @@ -404,7 +614,7 @@ describe("platform import flow", () => { expect(h.review).toHaveBeenLastCalledWith( expect.objectContaining({ targetServerId: "server-b" }), ); - expect(button("Import 2 projects").disabled).toBe(true); + expect(button("Import project").disabled).toBe(true); }); it("returns to editable services when a review request fails", async () => { diff --git a/apps/dashboard/src/components/migration/platform-import-model.ts b/apps/dashboard/src/components/migration/platform-import-model.ts index b49b3dfc1..a1c3d7e66 100644 --- a/apps/dashboard/src/components/migration/platform-import-model.ts +++ b/apps/dashboard/src/components/migration/platform-import-model.ts @@ -26,7 +26,7 @@ export const PLATFORM_IMPORT_HELP = { tokenUrl: "https://railway.com/account/tokens", docsUrl: "https://docs.railway.com/integrations/api", access: "Use a workspace token and its workspace ID, or a project token for one environment.", - data: "Openship pauses the project and copies its persistent volumes together. Allow downtime and keep Railway deployments paused until cutover.", + data: "OpenShip pauses the project and copies its persistent volumes together. Allow downtime and keep Railway deployments paused until cutover.", }, vercel: { name: "Vercel", diff --git a/apps/dashboard/src/components/migration/project-migration-card.render.test.tsx b/apps/dashboard/src/components/migration/project-migration-card.render.test.tsx index a5cfedc43..2b8cc1e01 100644 --- a/apps/dashboard/src/components/migration/project-migration-card.render.test.tsx +++ b/apps/dashboard/src/components/migration/project-migration-card.render.test.tsx @@ -59,7 +59,7 @@ describe("ProjectMigrationCard first paint", () => { it("says Cloud is out of scope instead of silently offering only servers", () => { // Otherwise a missing Cloud option reads as a bug rather than a boundary. - expect(text(render())).toContain("Openship Cloud"); + expect(text(render())).toContain("OpenShip Cloud"); }); it("renders no confirm dialog until a plan has been fetched", () => { diff --git a/apps/dashboard/src/components/migration/server-migration-entry.test.tsx b/apps/dashboard/src/components/migration/server-migration-entry.test.tsx index a4ddc5189..813dd5271 100644 --- a/apps/dashboard/src/components/migration/server-migration-entry.test.tsx +++ b/apps/dashboard/src/components/migration/server-migration-entry.test.tsx @@ -914,7 +914,7 @@ it("offers recovered projects inside discovery and keeps recovery separate from await pickSource("First server"); await act(async () => button("Scan server").click()); expect(container.querySelector('[role="tab"][aria-selected="true"]')?.textContent).toBe("Cards"); - expect(container.textContent).not.toContain("Openship projects found"); + expect(container.textContent).not.toContain("OpenShip projects found"); await act(async () => container.querySelector('[aria-label="Recover recovered"]')!.click(), ); diff --git a/apps/dashboard/src/components/network-error-handler.test.tsx b/apps/dashboard/src/components/network-error-handler.test.tsx index 963810d7c..5e92f113d 100644 --- a/apps/dashboard/src/components/network-error-handler.test.tsx +++ b/apps/dashboard/src/components/network-error-handler.test.tsx @@ -42,7 +42,7 @@ it("shows one connection notice for concurrent request failures and clears it af await vi.advanceTimersByTimeAsync(1_500); }); expect(node.querySelectorAll('[role="status"]')).toHaveLength(1); - expect(node.textContent).toContain("Can’t reach Openship"); + expect(node.textContent).toContain("Can’t reach OpenShip"); expect(node.textContent).toContain("Status updates may be delayed"); // A workload failure returned by a reachable API is not a browser connection failure. diff --git a/apps/dashboard/src/components/overview/AttentionCards.test.tsx b/apps/dashboard/src/components/overview/AttentionCards.test.tsx index 45cd891ac..a5a4fc8d1 100644 --- a/apps/dashboard/src/components/overview/AttentionCards.test.tsx +++ b/apps/dashboard/src/components/overview/AttentionCards.test.tsx @@ -49,9 +49,9 @@ describe("IssuesCard", () => { it("presents unavailable monitoring as a connection notice with no repair action", () => { const issue: SystemIssue = { id: "platform:monitoring-offline", kind: "monitoring_offline", severity: "action_required", - scope: "platform", source: "component", title: "Openship", message: "Current health is unknown", + scope: "platform", source: "component", title: "OpenShip", message: "Current health is unknown", details: { affectedServers: [{ name: "Mail", reason: "connect ENETUNREACH 192.0.2.1:22" }] }, - target: { id: "observer", name: "Openship", scope: "platform", href: "/monitoring" }, resolveWith: [], + target: { id: "observer", name: "OpenShip", scope: "platform", href: "/monitoring" }, resolveWith: [], }; const html = issues([issue]); expect(html).toContain("Connection interrupted"); diff --git a/apps/dashboard/src/components/overview/HomeAppsCard.tsx b/apps/dashboard/src/components/overview/HomeAppsCard.tsx index aa0d4fe73..ac1024970 100644 --- a/apps/dashboard/src/components/overview/HomeAppsCard.tsx +++ b/apps/dashboard/src/components/overview/HomeAppsCard.tsx @@ -4,7 +4,7 @@ import { useId } from "react"; import Link from "next/link"; import { Icon } from "@repo/ui/icons"; import type { Project } from "@/constants/mock"; -import { AppLogo } from "@/components/AppLogo"; +import { ProjectIcon } from "@/components/shared/ProjectIcon"; import { useI18n } from "@/components/i18n-provider"; import { ProjectStatusBadge } from "@/components/shared/ProjectStatusBadge"; import { Button } from "@/components/ui/button"; @@ -66,7 +66,7 @@ export default function HomeAppsCard({ className="absolute inset-0 rounded-xl focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring" />
- +

["projectData"]; + +/** Only fields owned by the shared build form. Environment, routes and source identity + * have their own owners and must never be serialized by a configuration save. */ +function optionsOf(config: Config): ProjectOptionsBody { + const options = config.options ?? {}; + return { + installCommand: options.installCommand ?? "", + buildCommand: options.buildCommand ?? "", + startCommand: options.startCommand ?? "", + outputDirectory: options.outputDirectory ?? "", + productionPaths: options.productionPaths ?? "", + rootDirectory: options.rootDirectory ?? "", + productionPort: options.productionPort ? Number(options.productionPort) : undefined, + hasBuild: options.hasBuild, + hasServer: options.hasServer, + workloadType: options.workloadType, + buildImage: config.buildImage ?? "", + runtimeVersion: config.runtimeVersion ?? null, + runtimeImage: config.runtimeImage ?? null, + }; +} + +export function ApplicationSettingsForm({ + onBusyChange, +}: { + onBusyChange?: (busy: boolean) => void; +}) { + const { id, projectData, buildData, updateProjectData, updateBuild, refreshEnvironment } = + useProjectSettings(); + const { t } = useI18n(); + const copy = t.projectSettings.build.runtime; + const { showToast } = useToast(); + const persisted = useMemo( + () => ({ ...projectData, options: { ...projectData.options, ...buildData } }), + [projectData, buildData], + ); + const [baseline, setBaseline] = useState(persisted); + const [draft, setDraft] = useState(persisted); + const [saving, setSaving] = useState(false); + const [error, setError] = useState(null); + const mutation = useRef(false); + const alive = useRef(true); + const before = optionsOf(baseline); + const after = optionsOf(draft); + const patch = Object.fromEntries( + Object.entries(after).filter( + ([key, value]) => value !== before[key as keyof ProjectOptionsBody], + ), + ) as ProjectOptionsBody; + const dirty = Object.keys(patch).length > 0; + + useEffect(() => { + alive.current = true; + return () => { + alive.current = false; + }; + }, []); + useEffect(() => { + onBusyChange?.(saving); + }, [saving, onBusyChange]); + useEffect(() => { + // A status refresh or an added companion must not erase an unsaved app draft. + if (!dirty && !mutation.current) { + setBaseline(persisted); + setDraft(persisted); + } + }, [persisted, dirty]); + useEffect(() => { + if (!dirty) return; + const prevent = (event: BeforeUnloadEvent) => { + event.preventDefault(); + event.returnValue = ""; + }; + window.addEventListener("beforeunload", prevent); + return () => window.removeEventListener("beforeunload", prevent); + }, [dirty]); + + const save = async () => { + if (mutation.current || !dirty || buildData.isLoading || buildData.error) return; + if ( + Object.hasOwn(patch, "productionPort") && + (!Number.isInteger(patch.productionPort) || + patch.productionPort! < 1 || + patch.productionPort! > 65535) + ) { + setError(copy.invalidPort); + return; + } + mutation.current = true; + setSaving(true); + setError(null); + try { + const result = await projectsApi.setOptions(id, patch); + if (result.success === false) throw new Error(copy.saveFailed); + if (!alive.current) return; + // setOptions returns the flat Project contract; the page uses the nested + // ProjectDetails projection. Map only this form's options, retaining the + // server's normalized workload, images and commands. + const saved = result.data as Config; + if (!saved || saved.id !== id) throw new Error(copy.saveFailed); + const workload = resolveWorkload(saved.workloadType, saved.hasServer); + const options = Object.fromEntries( + Object.entries(after) + .filter(([key]) => !["buildImage", "runtimeVersion", "runtimeImage"].includes(key)) + .map(([key, fallback]) => [ + key, + key === "productionPort" + ? workload === "web" + ? String(saved.port ?? 3000) + : "" + : key === "workloadType" + ? workload + : Object.hasOwn(saved, key) + ? (saved[key] ?? (typeof fallback === "string" ? "" : fallback)) + : fallback, + ]), + ); + const normalized = { ...draft, ...saved, options: { ...draft.options, ...options } }; + updateProjectData(normalized); + setBaseline(normalized); + setDraft(normalized); + invalidateProjectCaches(id); + showToast(copy.saved, "success"); + } catch (cause) { + if (alive.current) setError(getApiErrorMessage(cause, copy.saveFailed)); + } finally { + mutation.current = false; + if (alive.current) setSaving(false); + } + }; + + return ( +

+ {projectData.gitOwner && + projectData.gitRepo && + (projectData.buildKind === "dockerfile" || projectData.framework === "docker") && ( + + )} + {!dirty && ( + { + updateBuild(commands); + await refreshEnvironment(); + }} + /> + )} +
{ + event.preventDefault(); + void save(); + }} + > +
+ setDraft((previous) => ({ ...previous, ...updates }))} + updateOptions={(updates) => + setDraft((previous) => ({ + ...previous, + options: { ...previous.options, ...updates }, + })) + } + /> + {(error || buildData.error) && ( +

+ {error || buildData.error} +

+ )} +
+ + +
+
+
+

+ {copy.projectSettingsHint}{" "} + + {t.projects.sidebar.tabs.settings} + +

+
+ ); +} diff --git a/apps/dashboard/src/components/project-settings/EnvironmentPanel.tsx b/apps/dashboard/src/components/project-settings/EnvironmentPanel.tsx new file mode 100644 index 000000000..cba94ac4e --- /dev/null +++ b/apps/dashboard/src/components/project-settings/EnvironmentPanel.tsx @@ -0,0 +1,70 @@ +"use client"; + +import { useId, useState, type ComponentProps, type ReactNode } from "react"; +import { Icon } from "@repo/ui/icons"; +import EnvironmentVariables from "@/components/import-project/EnvironmentVariables"; +import { Button } from "@/components/ui/button"; +import { useI18n } from "@/components/i18n-provider"; + +/** Shared saved-environment surface. Hosts supply scope, persistence and runtime actions. */ +export function EnvironmentPanel({ + actions, + description, + children, + editor, +}: { + actions: ReactNode; + description: string; + children?: ReactNode; + editor?: ComponentProps; +}) { + const { t } = useI18n(); + const helpId = useId(); + const [helpOpen, setHelpOpen] = useState(false); + const helpTitle = t.projectDetail.services.detail.environmentState.helpTitle; + return ( +
+
+
+

+ {t.importProject.environmentVariables.title} +

+ +
+
+ {actions} +
+
+
+ + {children} +
+ {editor && ( +
+ +
+ )} +
+ ); +} diff --git a/apps/dashboard/src/components/project-settings/ProjectEnvironmentPanel.tsx b/apps/dashboard/src/components/project-settings/ProjectEnvironmentPanel.tsx new file mode 100644 index 000000000..190530cb0 --- /dev/null +++ b/apps/dashboard/src/components/project-settings/ProjectEnvironmentPanel.tsx @@ -0,0 +1,405 @@ +"use client"; + +import { useCallback, useEffect, useRef, useState } from "react"; +import Link from "next/link"; +import { useRouter } from "next/navigation"; +import { Icon } from "@repo/ui/icons"; +import { EnvironmentPanel } from "./EnvironmentPanel"; +import { RuntimeRedeployMenu } from "./RuntimeRedeployMenu"; +import { RuntimeImageUpdateNotice } from "./RuntimeImageUpdateNotice"; +import { AppLogo } from "@/components/AppLogo"; +import { Button } from "@/components/ui/button"; +import { Modal } from "@/components/ui/Modal"; +import { useI18n, interpolate } from "@/components/i18n-provider"; +import { useProjectSettings } from "@/context/ProjectSettingsContext"; +import { useToast } from "@/context/ToastContext"; +import { useComposeReview } from "@/hooks/useComposeReview"; +import { useCloudDeployPricing } from "@/hooks/useCloudDeployPricing"; +import { useRuntimeUpdate } from "@/hooks/useRuntimeUpdate"; +import { projectsApi, deployApi } from "@/lib/api"; +import { connectionsApi, type ProjectConnection } from "@/lib/api/connections"; +import { getApiErrorMessage } from "@/lib/api/client"; +import { canRefreshProjectEnvironment } from "@/lib/project-application"; +import { openTriggeredBuild, triggeredDeploymentId } from "@/lib/deploy-nav"; +import { + computeProjectEnvDiff, + createProjectEnvEditState, + type PersistedProjectEnv, +} from "@/lib/project-env-diff"; +import type { EnvironmentVariableRow } from "@/components/import-project/EnvironmentVariables"; + +/** Project-scoped adapter for the same panel used by service environments. + * A single application continues to own project variables; it gets no shadow service or env store. + */ +export function ProjectEnvironmentPanel({ + onBusyChange, + onDirtyChange, +}: { + onBusyChange?: (busy: boolean) => void; + onDirtyChange?: (dirty: boolean) => void; +}) { + const { id: projectId, projectData, servicesData } = useProjectSettings(); + const { t } = useI18n(); + const copy = t.projectSettings.envVars; + const serviceCopy = t.projectDetail.services.detail; + const { showToast } = useToast(); + const router = useRouter(); + const showComposeReview = useComposeReview(projectId); + const showCloudPricing = useCloudDeployPricing(projectData.workspaceId); + const runtime = useRuntimeUpdate( + projectId, + undefined, + projectData.workspaceId, + projectData.activeDeploymentId, + ); + const canRefresh = canRefreshProjectEnvironment(projectData, servicesData.services); + const shared = servicesData.services.some(service => !service.applicationMode); + const [rows, setRows] = useState([]); + const [baseline, setBaseline] = useState(null); + const [connections, setConnections] = useState([]); + const [selectedConnection, setSelectedConnection] = useState(null); + const [loading, setLoading] = useState(true); + const [error, setError] = useState(null); + const [operation, setOperation] = useState<"save" | "apply" | "build" | "unlink" | null>(null); + const [updateHref, setUpdateHref] = useState(null); + const generation = useRef(0); + const mutation = useRef(false); + const alive = useRef(true); + const busy = operation !== null || runtime.updating || runtime.checking; + const diff = baseline ? computeProjectEnvDiff(rows, baseline) : null; + const dirty = + !!diff && (!diff.ok || diff.diff.upserts.length > 0 || diff.diff.deletes.length > 0); + const linkedKeys = new Set(connections.map((connection) => connection.envKey)); + const managedRows = rows.filter((row) => linkedKeys.has(row.originalKey ?? row.key)); + const editableRows = rows.filter((row) => !linkedKeys.has(row.originalKey ?? row.key)); + + useEffect(() => { + onBusyChange?.(busy); + }, [busy, onBusyChange]); + useEffect(() => { + onDirtyChange?.(dirty); + }, [dirty, onDirtyChange]); + useEffect(() => { + if (!dirty) return; + const prevent = (event: BeforeUnloadEvent) => { + event.preventDefault(); + event.returnValue = ""; + }; + window.addEventListener("beforeunload", prevent); + return () => window.removeEventListener("beforeunload", prevent); + }, [dirty]); + + const load = useCallback(async () => { + const token = ++generation.current; + setLoading(true); + setError(null); + try { + // Resolve ownership before enabling editing. A failed connection read must + // not turn integration-managed variables into ordinary editable inputs. + const [result, linked] = await Promise.all([ + projectsApi.getEnv(projectId), + connectionsApi.list(projectId), + ]); + if (!alive.current || token !== generation.current) return; + const state = createProjectEnvEditState(result.data ?? [], "production"); + setRows(state.rows); + setBaseline(state.baseline); + setConnections(linked.data ?? []); + } catch (cause) { + if (alive.current && token === generation.current) + setError(getApiErrorMessage(cause, copy.toast.loadFailed)); + } finally { + if (alive.current && token === generation.current) setLoading(false); + } + }, [projectId, copy.toast.loadFailed]); + + useEffect(() => { + alive.current = true; + void load(); + return () => { + alive.current = false; + generation.current++; + }; + }, [load]); + + const save = async () => { + if (mutation.current || busy || loading || error || !diff || !dirty) return; + if (!diff.ok) { + showToast(diff.error, "error", copy.toast.validationTitle); + return; + } + // Pasting a managed key cannot create an editable replacement for it. + if (diff.diff.upserts.some((row) => linkedKeys.has(row.key))) { + showToast(copy.linkedTitle, "error"); + return; + } + mutation.current = true; + setOperation("save"); + try { + const result = await projectsApi.mergeEnv(projectId, { + environment: "production", + ...diff.diff, + }); + if (!alive.current) return; + showToast(copy.toast.saved, "success", copy.toast.savedTitle); + for (const warning of result.warnings ?? []) showToast(warning, "info"); + setUpdateHref(null); + await load(); + } catch (cause) { + if (alive.current) + showToast( + getApiErrorMessage(cause, copy.toast.saveFailed), + "error", + copy.toast.saveFailedTitle, + ); + } finally { + mutation.current = false; + if (alive.current) setOperation(null); + } + }; + + const deploy = async (refresh: boolean) => { + if (mutation.current || busy || loading || error || dirty || !baseline) return; + mutation.current = true; + setOperation(refresh ? "apply" : "build"); + try { + const result = await deployApi.trigger( + refresh ? { projectId, refresh: true } : { projectId, smartRoute: true }, + ); + if (!alive.current) return; + if (refresh) { + // This API queues a deployment. Show its progress, never claim the + // running environment already matches just because the request succeeded. + const deploymentId = triggeredDeploymentId(result); + setUpdateHref( + deploymentId ? `/build/${deploymentId}` : `/projects/${projectId}/deployments`, + ); + showToast(copy.updateStarted, "info"); + } else openTriggeredBuild(router, result, projectId); + } catch (cause) { + if (alive.current && !showComposeReview(cause) && !showCloudPricing(cause)) { + showToast( + getApiErrorMessage( + cause, + refresh ? copy.toast.applyFailed : t.projects.redeploy.couldNotRedeploy, + ), + "error", + refresh ? copy.toast.applyFailedTitle : t.projects.redeploy.errorTitle, + ); + } + } finally { + mutation.current = false; + if (alive.current) setOperation(null); + } + }; + + const unlink = async () => { + if (!selectedConnection || mutation.current || busy || dirty) return; + mutation.current = true; + setOperation("unlink"); + try { + await connectionsApi.remove(projectId, selectedConnection.id); + if (!alive.current) return; + setSelectedConnection(null); + showToast(t.projects.connections.removed, "success"); + await load(); + } catch (cause) { + if (alive.current) + showToast(getApiErrorMessage(cause, t.projects.connections.failed), "error"); + } finally { + mutation.current = false; + if (alive.current) setOperation(null); + } + }; + + const actionDisabled = + loading || + !!error || + busy || + dirty || + !baseline || + servicesData.isLoading || + !!servicesData.error; + return ( + <> + + + + {projectData.activeDeploymentId && ( + <> + + {(projectData.runtimeMode !== "bare" || shared) && ( + + )} + {canRefresh && ( + + )} + + )} + + } + editor={ + baseline && !loading && !error + ? { + envVars: editableRows, + onEnvVarsChange: (next) => { + if (!mutation.current && !busy) setRows([...managedRows, ...next]); + }, + isEditingMode: !busy, + setIsEditingMode: () => {}, + isSaving: busy, + } + : undefined + } + > + {shared &&

{copy.sharedHint}

} + {loading ? ( +
+ {[0, 1, 2].map((index) => ( +
+ ))} +
+ ) : error ? ( +
+

{error}

+ +
+ ) : ( + <> +

+ {!projectData.activeDeploymentId + ? copy.firstDeployHint + : canRefresh + ? copy.runtimeHint + : copy.buildHint} +

+ + {updateHref && ( +
+ {copy.updateStarted} + + {t.projects.sidebar.tabs.deployments} + +
+ )} + {managedRows.map((row) => { + const connection = connections.find((item) => item.envKey === row.key)!; + return ( + + ); + })} + + )} + + {selectedConnection && ( + { + if (!busy) setSelectedConnection(null); + }} + closable={!busy} + maxWidth="460px" + width="92vw" + zIndex={10010} + > +
+

{copy.linkedTitle}

+ {selectedConnection.envKey} +

+ {interpolate(copy.linkedBody, { app: selectedConnection.sourceName })} +

+
+ + +
+
+
+ )} + + ); +} diff --git a/apps/dashboard/src/components/project-settings/ProjectIconSetting.tsx b/apps/dashboard/src/components/project-settings/ProjectIconSetting.tsx new file mode 100644 index 000000000..766ad83cd --- /dev/null +++ b/apps/dashboard/src/components/project-settings/ProjectIconSetting.tsx @@ -0,0 +1,38 @@ +"use client"; + +import { ProjectIcon } from "@/components/shared/ProjectIcon"; +import { useProjectSettings } from "@/context/ProjectSettingsContext"; +import { useI18n } from "@/components/i18n-provider"; +import { ImageUrlEditor } from "@/components/settings/ImageUrlEditor"; +import { projectsApi } from "@/lib/api"; +import { invalidateProjectCaches } from "@/hooks/useProjectEndpoints"; + +export function ProjectIconSetting() { + const { projectData, updateProjectData } = useProjectSettings(); + const { t } = useI18n(); + const c = t.projectSettings.icon; + return ( + + } + onSave={async (iconUrl) => { + const response = await projectsApi.update(projectData.id, { iconUrl }); + updateProjectData({ iconUrl: response.data.iconUrl }); + invalidateProjectCaches(projectData.id); + }} + /> + ); +} diff --git a/apps/dashboard/src/components/referrals/ReferralLanding.test.tsx b/apps/dashboard/src/components/referrals/ReferralLanding.test.tsx index 046883982..2f768970a 100644 --- a/apps/dashboard/src/components/referrals/ReferralLanding.test.tsx +++ b/apps/dashboard/src/components/referrals/ReferralLanding.test.tsx @@ -74,10 +74,10 @@ afterEach(async () => { vi.unstubAllGlobals(); }); -it("shows the live Cloud cards, defaults to annual and continues the selected plan with its referral", async () => { +it("shows the live Cloud cards, defaults to monthly and continues the selected plan with its referral", async () => { await render(); expect(titles()).toEqual(["Hobby", "Starter", "Pro", "Scale"]); - expect(button("Yearly").getAttribute("aria-pressed")).toBe("true"); + expect(button("Monthly").getAttribute("aria-pressed")).toBe("true"); expect(host.textContent).toContain("Save 5% for 12 months"); expect(h.push).not.toHaveBeenCalled(); await click("Choose Hobby"); @@ -85,23 +85,23 @@ it("shows the live Cloud cards, defaults to annual and continues the selected pl expect(Object.fromEntries(target().searchParams)).toEqual({ newServer: "1", plan: "hobby", - interval: "annual", + interval: "monthly", startCheckout: "1", ref: code, }); expect(validateReturnTo(`${target().pathname}${target().search}`)).not.toBeNull(); }); -it("preserves monthly selection and keeps generic sign-in browse-only", async () => { +it("preserves yearly selection and keeps generic sign-in browse-only", async () => { await render(); const signIn = host.querySelector('a[href^="/login?"]')!; const destination = new URL(signIn.href).searchParams.get("returnTo")!; expect(validateReturnTo(destination)).toBe(destination); expect(destination).toBe(`/billing/plans?newServer=1&ref=${code}`); - await click("Monthly"); + await click("Yearly"); expect(h.push).not.toHaveBeenCalled(); await click("Choose Starter"); - expect(target().searchParams.get("interval")).toBe("monthly"); + expect(target().searchParams.get("interval")).toBe("annual"); expect(target().searchParams.get("ref")).toBe(code); }); @@ -186,18 +186,19 @@ it("limits public cards to the selected plan and billing combinations and choose it.each([ - ["annual", 1000, 12000, 1000, "$9", "$10", "$108 billed yearly"], + ["annual", 1000, 12000, 1000, "$108", "$120", "Billed yearly"], ["monthly", 1200, 12000, 1000, "$10.80", "$12", "billed monthly"], ["monthly", 1231, 12000, 500, "$11.69", "$12.31", "billed monthly"], - ["annual", 1000, 12000, 0, "$10", null, "$120 billed yearly"], + ["annual", 1000, 12000, 0, "$120", null, "Billed yearly"], ["monthly", 1200, 12000, 10000, "Free", "$12", "billed monthly"], ] as const)("shows %s referral prices for %i/%i cents at %i basis points", async (interval, monthly, annual, discountBps, price, original, billing) => { h.offer.mockResolvedValue({ ...offer, discountBps }); catalog([{ ...plans[0]!, price: { monthly, annual }, listPrice: { monthly }, effectivePrice: { monthly } }]); await render(); - if (interval === "monthly") await click("Monthly"); + if (interval === "annual") await click("Yearly"); const card = host.querySelector('article[aria-label="Hobby"]')!; - expect(card.textContent).toContain(price); + expect(card.querySelector(".text-2xl")?.textContent).toBe(price); + if (price !== "Free") expect(card.textContent).toContain(interval === "annual" ? pricingUi("en").perYear : pricingUi("en").perMonth); expect(card.querySelector("s")?.textContent ?? null).toBe(original); expect(card.textContent).toContain(billing); if (discountBps) expect(card.textContent).toContain(`Save ${discountBps / 100}% for 12 months`); diff --git a/apps/dashboard/src/components/referrals/ReferralLanding.tsx b/apps/dashboard/src/components/referrals/ReferralLanding.tsx index fdcb14b6c..92408efad 100644 --- a/apps/dashboard/src/components/referrals/ReferralLanding.tsx +++ b/apps/dashboard/src/components/referrals/ReferralLanding.tsx @@ -22,7 +22,7 @@ export function ReferralLanding({ code }: { code: string }) { copy = t.referrals; const router = useRouter(); const catalog = useCloudPlans(); - const [selectedInterval, setInterval] = useState<"monthly" | "annual">("annual"); + const [selectedInterval, setInterval] = useState<"monthly" | "annual">("monthly"); const [state, setState] = useState<{ offer?: ReferralPublicOffer; error?: string }>({}); const load = useCallback(async () => { try { @@ -140,6 +140,7 @@ export function ReferralLanding({ code }: { code: string }) { ui={catalog.payload.ui} currentPlan={null} interval={interval} + annualSavings={catalog.payload.annual.monthsFree > 0 ? catalog.payload.ui.monthsFree : undefined} referralOffer={state.offer} workspaceScoped onSelectPlan={(planId) => { diff --git a/apps/dashboard/src/components/routing/DomainResponseRoutes.test.tsx b/apps/dashboard/src/components/routing/DomainResponseRoutes.test.tsx new file mode 100644 index 000000000..e9b7f1f68 --- /dev/null +++ b/apps/dashboard/src/components/routing/DomainResponseRoutes.test.tsx @@ -0,0 +1,193 @@ +// @vitest-environment happy-dom +import { act } from "react"; +import { createRoot, type Root } from "react-dom/client"; +import { afterEach, beforeEach, expect, it, vi } from "vitest"; +import { normalizeEdgeResponse } from "@repo/core"; +import { DomainResponseRoutes } from "./DomainResponseRoutes"; + +const api = vi.hoisted(() => ({ + listRouteRules: vi.fn(), + createRouteRule: vi.fn(), + updateRouteRule: vi.fn(), + deleteRouteRule: vi.fn(), +})); +vi.mock("@/lib/api", () => ({ + projectsApi: api, + getApiErrorMessage: (error: unknown, fallback: string) => + error instanceof Error ? error.message : fallback, +})); +let root: Root; +let container: HTMLDivElement; +const rule = (overrides = {}) => ({ + id: "response-a", + projectId: "project", + domainId: "domain-a", + pathPrefix: "/x", + enabled: true, + spec: { + response: normalizeEdgeResponse({ body: "original", headers: { "cache-control": "no-store" } }), + }, + ...overrides, +}); +beforeEach(() => { + vi.clearAllMocks(); + vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true); + api.listRouteRules.mockResolvedValue({ rules: [] }); + api.createRouteRule.mockImplementation(async (_project, input) => ({ + rule: rule({ ...input, edgeApplied: true }), + })); + api.updateRouteRule.mockImplementation(async (_project, _id, input) => ({ + rule: rule({ ...input, edgeApplied: true }), + })); + api.deleteRouteRule.mockResolvedValue({ success: true, edgeApplied: true }); + container = document.createElement("div"); + document.body.append(container); + root = createRoot(container); +}); +afterEach(async () => { + await act(async () => root.unmount()); + container.remove(); + vi.unstubAllGlobals(); +}); +async function render() { + await act(async () => + root.render( + , + ), + ); +} +async function click(label: string) { + const button = [...document.querySelectorAll("button")].find( + (element) => + element.getAttribute("aria-label") === label || element.textContent?.trim() === label, + ); + expect(button, label).toBeTruthy(); + await act(async () => button!.click()); +} +async function input(selector: string, value: string) { + const element = document.querySelector(selector)!; + expect(element, selector).toBeTruthy(); + await act(async () => { + Object.getOwnPropertyDescriptor( + element.tagName === "TEXTAREA" ? HTMLTextAreaElement.prototype : HTMLInputElement.prototype, + "value", + )!.set!.call(element, value); + element.dispatchEvent(new Event("input", { bubbles: true })); + }); +} +async function submit() { + await act(async () => { + document + .querySelector("form")! + .dispatchEvent(new Event("submit", { bubbles: true, cancelable: true })); + }); +} + +it("creates a response for this domain, keeps preview HTML inert, and reports edge acknowledgement", async () => { + await render(); + await click("Add route"); + await input('input[placeholder="/health"]', "/status"); + await input("textarea", ""); + await click("Add header"); + await input('input[aria-label="Header name 1"]', "Cache-Control"); + await input('input[aria-label="Header value 1"]', "no-store"); + await click("Response preview"); + expect(document.querySelector("pre")?.textContent).toContain("", + headers: { "cache-control": "no-store" }, + match: "exact", + }), + }, + }), + ); + expect(document.body.textContent).toContain("Saved and applied to the edge."); + expect(document.querySelector('[role="dialog"]')).toBeNull(); +}); + +it("retains a failed draft, avoids double submit and distinguishes saved from applied", async () => { + await render(); + await click("Add route"); + await input('input[placeholder="/health"]', "/x"); + await input("textarea", "keep this content"); + let reject!: (error: Error) => void; + api.createRouteRule.mockImplementationOnce( + () => + new Promise((_, fail) => { + reject = fail; + }), + ); + await submit(); + await submit(); + expect(api.createRouteRule).toHaveBeenCalledTimes(1); + await act(async () => reject(new Error("offline"))); + expect(document.querySelector("textarea")?.value).toBe("keep this content"); + expect(document.body.textContent).toContain("offline"); + api.createRouteRule.mockResolvedValue({ rule: rule({ edgeApplied: false }) }); + await submit(); + expect(document.body.textContent).toContain("Edge application is pending"); + expect(document.body.textContent).not.toContain("Saved and applied"); +}); + +it("rejects duplicate or injected headers before sending a mutation", async () => { + await render(); + await click("Add route"); + await input('input[placeholder="/health"]', "/headers"); + for (const [index, name] of ["X-Test", "x-test"].entries()) { + await click("Add header"); + await input(`input[aria-label="Header name ${index + 1}"]`, name); + } + await submit(); + expect(api.createRouteRule).not.toHaveBeenCalled(); + expect(document.body.textContent).toContain("must be unique"); +}); + +it("edits only the selected domain response and preserves its disabled state", async () => { + api.listRouteRules.mockResolvedValue({ + rules: [ + rule({ enabled: false }), + rule({ id: "other", domainId: "domain-b", pathPrefix: "/other-domain" }), + rule({ id: "policy", spec: { rateLimit: { rps: 5, burst: 0 } } }), + ], + }); + await render(); + expect(container.textContent).not.toContain("/other-domain"); + await click("Edit route /x"); + expect(document.querySelector("textarea")?.value).toBe("original"); + await input("textarea", "changed"); + await submit(); + const patch = api.updateRouteRule.mock.calls[0]![2]; + expect(patch).not.toHaveProperty("enabled"); + expect(patch.spec.response).toMatchObject({ + body: "changed", + headers: { "cache-control": "no-store" }, + }); + expect(api.createRouteRule).not.toHaveBeenCalled(); +}); + +it("keeps failed loading distinct from an empty list and requires confirmation to remove", async () => { + api.listRouteRules.mockRejectedValueOnce(new Error("Cannot reach API")); + await render(); + expect(container.textContent).toContain("Cannot reach API"); + expect( + [...container.querySelectorAll("button")].find((button) => + button.textContent?.includes("Add route"), + )?.disabled, + ).toBe(true); + api.listRouteRules.mockResolvedValue({ rules: [rule()] }); + await click("Retry"); + await click("Remove /x"); + expect(api.deleteRouteRule).not.toHaveBeenCalled(); + await click("Remove"); + expect(api.deleteRouteRule).toHaveBeenCalledWith("project", "response-a"); + expect(container.textContent).not.toContain("/x"); +}); diff --git a/apps/dashboard/src/components/routing/DomainResponseRoutes.tsx b/apps/dashboard/src/components/routing/DomainResponseRoutes.tsx new file mode 100644 index 000000000..cb59ad690 --- /dev/null +++ b/apps/dashboard/src/components/routing/DomainResponseRoutes.tsx @@ -0,0 +1,625 @@ +"use client"; + +import { useCallback, useEffect, useId, useRef, useState } from "react"; +import { Icon } from "@repo/ui/icons"; +import { normalizeEdgeResponse, normalizeResponsePath, EDGE_RESPONSE_MAX_BYTES } from "@repo/core"; +import { useI18n } from "@/components/i18n-provider"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Textarea } from "@/components/ui/textarea"; +import { CustomSelect } from "@/components/ui/CustomSelect"; +import { Modal } from "@/components/ui/Modal"; +import { useDialogFocus } from "@/hooks/useDialogFocus"; +import { projectsApi, getApiErrorMessage, type RouteRuleRow } from "@/lib/api"; + +type Draft = { + id?: string; + path: string; + match: "exact" | "prefix"; + status: string; + contentType: string; + body: string; + methods: string; + headers: Array<{ name: string; value: string }>; +}; +const blank = (): Draft => ({ + path: "", + match: "exact", + status: "200", + contentType: "text/plain; charset=utf-8", + body: "", + methods: "GET, HEAD", + headers: [], +}); +const fromRule = (rule: RouteRuleRow): Draft => ({ + id: rule.id, + path: rule.pathPrefix || "/", + match: rule.spec.response!.match, + status: String(rule.spec.response!.status), + contentType: rule.spec.response!.contentType, + body: rule.spec.response!.body, + methods: rule.spec.response!.methods.join(", "), + headers: Object.entries(rule.spec.response!.headers).map(([name, value]) => ({ name, value })), +}); + +/** The same domain editor is used by single-app, service and project route cards. */ +export function DomainResponseRoutes({ + projectId, + domainId, + hostname, +}: { + projectId: string; + domainId: string; + hostname: string; +}) { + const { t, locale } = useI18n(); + const w = t.projectSettings.responseRoutes; + const labelId = useId(); + const [rules, setRules] = useState([]); + const [loading, setLoading] = useState(true); + const [loadError, setLoadError] = useState(""); + const [error, setError] = useState(""); + const [notice, setNotice] = useState(""); + const [draft, setDraft] = useState(null); + const [open, setOpen] = useState(false); + const [preview, setPreview] = useState(false); + const [busy, setBusy] = useState(false); + const [removeId, setRemoveId] = useState(null); + const mutation = useRef(false); + const alive = useRef(true); + useEffect(() => { + alive.current = true; + return () => { + alive.current = false; + }; + }, []); + const close = () => { + if (!mutation.current) setOpen(false); + }; + const { dialog, onKeyDown } = useDialogFocus(close, open); + const load = useCallback(async () => { + setLoading(true); + setLoadError(""); + try { + const result = await projectsApi.listRouteRules(projectId); + if (alive.current) + setRules(result.rules.filter((rule) => rule.domainId === domainId && rule.spec.response)); + } catch (cause) { + if (alive.current) setLoadError(getApiErrorMessage(cause, w.loadFailed)); + } finally { + if (alive.current) setLoading(false); + } + }, [projectId, domainId, w.loadFailed]); + useEffect(() => { + void load(); + }, [load]); + + const update = (patch: Partial) => + setDraft((current) => current && { ...current, ...patch }); + const edit = (rule?: RouteRuleRow) => { + setDraft((current) => + current && current.id === rule?.id ? current : rule ? fromRule(rule) : blank(), + ); + setError(""); + setPreview(false); + setOpen(true); + }; + const run = async (operation: () => Promise) => { + if (mutation.current) return; + mutation.current = true; + setBusy(true); + setError(""); + setNotice(""); + try { + await operation(); + } catch (cause) { + if (alive.current) setError(getApiErrorMessage(cause, w.saveFailed)); + } finally { + mutation.current = false; + if (alive.current) setBusy(false); + } + }; + const save = () => + run(async () => { + if (!draft) return; + const path = normalizeResponsePath(draft.path); + const headers: Record = {}; + for (const row of draft.headers) { + if (!row.name && !row.value) continue; + const name = row.name.trim().toLowerCase(); + if (Object.hasOwn(headers, name)) throw new Error(w.duplicateHeader); + Object.defineProperty(headers, name, { value: row.value, enumerable: true }); + } + const response = normalizeEdgeResponse({ + match: draft.match, + status: Number(draft.status), + contentType: draft.contentType.trim(), + body: draft.body, + headers, + methods: draft.methods + .split(/[\s,]+/) + .filter(Boolean) + .map((value) => value.toUpperCase()), + }); + const input = { domainId, pathPrefix: path, spec: { response } }; + const result = draft.id + ? await projectsApi.updateRouteRule(projectId, draft.id, input) + : await projectsApi.createRouteRule(projectId, { ...input, enabled: true }); + if (!alive.current) return; + setRules((current) => [...current.filter((rule) => rule.id !== result.rule.id), result.rule]); + setDraft(null); + setOpen(false); + setNotice(result.rule.edgeApplied === true ? w.saved : w.pending); + }); + const toggle = (rule: RouteRuleRow) => + run(async () => { + const result = await projectsApi.updateRouteRule(projectId, rule.id, { + enabled: !rule.enabled, + }); + if (alive.current) { + setRules((current) => current.map((row) => (row.id === rule.id ? result.rule : row))); + setNotice(result.rule.edgeApplied === true ? w.saved : w.pending); + } + }); + const remove = (rule: RouteRuleRow) => + run(async () => { + const result = await projectsApi.deleteRouteRule(projectId, rule.id); + if (!alive.current) return; + setRules((current) => current.filter((row) => row.id !== rule.id)); + setRemoveId(null); + setNotice(result.edgeApplied === true ? w.saved : w.pending); + if (draft?.id === rule.id) setDraft(null); + }); + const contentTypes = [ + { value: "text/plain; charset=utf-8", label: w.plainText }, + { value: "application/json", label: "JSON" }, + { value: "text/html; charset=utf-8", label: "HTML" }, + { value: "application/xml", label: "XML" }, + ]; + const format = + draft && contentTypes.some((type) => type.value === draft.contentType) + ? draft.contentType + : "custom"; + const contentBytes = new TextEncoder().encode(draft?.body ?? "").length; + const fieldClass = "bg-muted/60 ring-1 ring-inset ring-border/50 focus:bg-muted/80"; + const bodyless = draft && [204, 205, 304].includes(Number(draft.status)); + + return ( +
+
+

{w.title}

+ +
+

{w.description}

+ {loading && ( +
+ )} + {loadError && ( +
+

+ {loadError} +

+ +
+ )} + {!open && error && ( +

+ {error} +

+ )} + {notice && ( +

+ {notice} +

+ )} +
+ {rules.map((rule) => { + const path = rule.pathPrefix || "/"; + const response = rule.spec.response!; + return ( +
+
+ + + +
+ {removeId === rule.id && ( +
+

{w.removeHint}

+ + +
+ )} +
+ ); + })} +
+ +
+
{ + event.preventDefault(); + void save(); + }} + className="flex min-h-0 flex-1 flex-col" + > +
+
+ +
+
+

+ {draft?.id ? w.edit : w.add} +

+

+ {hostname} +

+
+ + {w.staticResponse} + + +
+ {draft && ( +
+
+
+
+
+ +
+ {w.match} + update({ match })} + /> +
+
+

+ {draft.match === "exact" ? w.exactHint : w.prefixHint} +

+
+
+ +
+ {w.contentType} + + update({ contentType: value === "custom" ? "" : value }) + } + /> +
+
+ {format === "custom" && ( + + )} +
+
+
+ {[ + { value: false, label: w.body, icon: "code" as const }, + { value: true, label: w.preview, icon: "eye" as const }, + ].map((view) => ( + + ))} +
+ {preview ? ( +
{`HTTP/1.1 ${draft.status}\nContent-Type: ${draft.contentType}\n${draft.headers
+                            .filter((row) => row.name)
+                            .map((row) => `${row.name}: ${row.value}\n`)
+                            .join("")}\n${draft.body}`}
+ ) : ( +