From 21bf7ebcee4f9bd898cfafee3baabfff93aefa17 Mon Sep 17 00:00:00 2001 From: Devin Foley Date: Fri, 13 Nov 2026 15:15:43 -0800 Subject: [PATCH] feat(connectors): connect Tailscale networks to SSH environments (#20617) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Thinking Path > - Paperclip manages work for AI agents. > - SSH environments run tasks on computers that users own. > - A Paperclip server needs network access before it can reach a private computer. > - A reusable Tailscale connection belongs in Connectors. > - Connected machines should become choices in the existing SSH environment form. > - This change connects a tailnet and resolves selected machines through a private network client. > - Agents retain their choice of harness and AI account. ## Linked Issues or Issue Description **Subsystem affected** Connectors, environment settings, SSH execution, company secret storage, and runtime packaging. **Problem or motivation** Entering a Tailscale address in the SSH form does not connect the Paperclip server to that tailnet. Requiring a separate device key for every environment also duplicates network setup. **Proposed solution** Add a Tailscale Connector with **Connect Tailscale Network**. Use brokered OAuth consent to enroll one private network client per connection. Let SSH environments select a connected network and stable machine identity. Preserve the enrolled node state through the company vault so a restart does not reuse the one-time token. **Alternatives considered** A separate Tailscale environment type would duplicate SSH behavior and mix network access with harness selection. This change uses the existing `ssh` driver. Related #15648 adds OAuth client credentials and a health check; this change uses user consent and supplies outbound task execution. Related #11743 concerns HTTPS brokering. **Roadmap alignment** This extends the existing Connected Apps, environment, and Personal & Shared AI Accounts capabilities. This PR is stacked on #21248, which provides SSH account selection independently of Tailscale. The hosted OAuth broker is a companion control-plane change. ## What Changed - Preserve Cloud reauthentication in the current browser tab. Explain canceled or failed consent in the connector and offer a retry of the same authorized saved connection. - Record Cloud enrollment starts initiated through the Tailscale connector in the company activity log. - Check install-preparation descendant liveness through the kernel process table in the Runner support test. Keep the direct child’s strict reaping assertion and reject any non-zombie descendant. - Align the application image stage contract with the Tailscale client stage after the upstream Runner-image refactor. Keep the exact stage list and check every application stage, including `tailscale-client`, for prohibited Runner compiler commands. - Group Tailscale consent, discovery, daemon lifecycle, pairing, and deletion under `server/src/services/connectors/tailscale/`, with their service tests. Update existing route, runtime, environment, vault, and company-deletion imports. Keep the existing contracts, authorization, audit, and transaction owners. - Handle early stdin closure in the Kubernetes real-shell test helper. Stop the input stream on `EPIPE`, preserve nonzero child exits, and reject a zero exit when input was lost. Add two real-process regression cases. Production transfer code and confinement checks are unchanged. - Read configured Tailscale executable paths when a client starts, after supported `.env` loading. Keep explicit command overrides available. - Discard pending pairing results after switching to Direct SSH or closing the form. Retain the existing company, connection, machine, and port checks. - Store network credentials with an immutable server-owned purpose from their first vault row. Exclude them from generic catalogs, bindings, environment projection, and value reads. Use a dedicated audited server consumer for pinned connector reads. Block unclassified legacy serialized node state at the same value boundary. Keep SSH private-key binding separate from connector credentials. - Gate network queries and setup on verified local-implicit or instance-admin access. Hide Tailscale catalog rows for other members, show an admin requirement on direct setup navigation, and avoid device discovery before management access is known. - Keep failed-client cleanup in the network manager. A failed request cannot disconnect a newer retry for the same identity; explicit connector disconnect and enrollment retirement still stop their networks. - Check for readable Ed25519, ECDSA, or RSA host public keys before pairing edits `authorized_keys`. Print a known-hosts entry for every available key, or fail before changing authorized keys if none are available. - Return self-hosted Cloud enrollment to the Tailscale connector. Save the unprefixed setup path and let the shared callback add the verified company prefix. Keep external and unrelated destinations rejected. - Hide pairing instructions after an SSH port change. Ignore late key-selection results for the old port. Operators prepare again for matching known-hosts output. - Retire connector references before deleting a company or its vault credential. Stop company daemons after the database commit. Keep failed secret retirement retryable and prevent late node-state writes from restoring deleted connections. Discard retired state without blocking behind the vault deletion lock. - Reload a mismatched credential before replacing a ready daemon. Recheck the active entry and manager generation after the read. An older request cannot stop a newer healthy socket or cross shutdown. - Pin connector credentials to the exact vault version and SHA-256 digest recorded by trusted enrollment and node-state writes. Ignore generic secret rotations. Advance both fields with a compare-and-set guard. Reject unpinned or tampered credentials before network startup. - Save an explicit Direct SSH selection and clear old Tailscale fields after config merging. Preserve the operator's entered direct host. - Route all Tailscale SSH runtime access through the connection service after SSH-key loading. Pin access to the environment's saved connector identity and recheck after device discovery. Remove standalone key-based daemon startup and reject raw enrollment keys or credential-only network configurations. Align the shared config type and SDK pins. - Correct two E2E assumptions exposed during connector qualification. Keep the two-window chat fixture stale until its own mutation. Validate the original failed run and its permitted write-revocation timestamp in the legacy recovery browser test. - Add the Tailscale Connector page, catalog entry, OAuth return flow, connected-network management, device discovery, and disconnect. Document all six routes in OpenAPI and the API reference, including their instance-admin requirement. - Add company-scoped connection storage in `0480_lazy_lethal_legion`, trusted credential version/digest fields in `0481_round_pet_avengers`, and server-owned secret purpose/backfill in `0482_aberrant_rumiko_fujikawa`. Generate all three migrations through the maintained command and retain the five newest snapshots. - Add machine pairing that generates a fresh SSH key per request, stores its private half in the company vault, and uses the generated public half in the install command. Do not trust editable saved metadata. Preserve existing authorized keys, including a file without a final newline. Keep SSH username, workspace, and trusted host keys explicit. - Use the existing SSH driver. Resolve the machine's current address from its stable device ID. Reject disconnected, removed, offline, and wrong-company selections. - Hide Network when Direct is the only option. Select a sole connected tailnet automatically and hide its selector. Remove the requested setup helper copy. Do not restrict the harness to Claude. - Run a private userspace Tailscale daemon with a separate socket. Persist its node state before execution. Restore it after restart, bound startup, serialize reconnect cleanup, and close it during server shutdown. - Guard OAuth completion with a durable attempt marker. Disconnect and newer authorization invalidate older callbacks. Close replaced daemons and remove cancelled enrollment secrets without changing newer connection state. - Preserve SSH host verification and tailnet-specific session identity. Honor the configured Tailscale executable path, including spaces and OpenSSH percent escaping. - Package checksum-pinned Tailscale clients and their license in the standard runtime image. Exercise the client build stage in PR CI and include it in image smoke checks. ## Verification - Merge status: all CI and image checks pass, and current-head Apex is 5/5 with no unresolved comments. GitHub rejected a direct merge because the active master ruleset requires code-owner approval for `.github/workflows/pr-trusted.yml`. The [CODEOWNERS workflow pattern](https://github.com/paperclipai/paperclip/blob/master/.github/CODEOWNERS#L3) applies; eligible maintainers already have review requests. Auto-merge is enabled with the squash method for the verified head. The PR remains open pending that required approval; no branch policy was bypassed. - Final revision: `fdb29b2cc366f4945b7f4c3e2b2d25070329a21b`, including master `999709b0a6dd416af335b5debded13e1de7fcb7a`. [Final CI verification passed](https://github.com/paperclipai/paperclip/actions/runs/43518584814/job/132213508320). All required checks pass, all applicable test/build gates are green, and the branch is mergeable against master. SSH prerequisite #21248 and Cloud broker paperclipai/paperclip-cloud#1314 are already merged. - [Apex reviewed this exact revision at 5/5](https://github.com/paperclipai/paperclip/pull/20617#issuecomment-6776629437). There are no unresolved review threads. The PR is classified as Critical impact. - Final image qualification passes for the [application Runner path](https://github.com/paperclipai/paperclip/actions/runs/43518584522/job/132206784267), [amd64 Runner image](https://github.com/paperclipai/paperclip/actions/runs/43518584544/job/132207296198), and [arm64 Runner image](https://github.com/paperclipai/paperclip/actions/runs/43518584544/job/132207296156). Typechecks, Runner support, native/Rust tests, provider tests, serialized-server suites, browser shards, and the remaining applicable CI checks pass. This is source/image CI qualification, not a production rollout. - The last master conflict affected only Docker stage declarations and their boundary test. Both the checksum-pinned Tailscale stage and upstream `runner-lockfile` guard are retained, including the upstream `deps` inheritance. The Tailscale client stage is byte-identical. All 180 cases across five Docker/image contract suites pass locally, along with server and UI typechecks. The maintained 543-input SDK check has no drift. Runtime and lock pins are unchanged. Secret/PII and whitespace scans pass. - Seven OAuth regression cases failed before the fixes. The current-tab test uses the real handoff helper, checks that the session is stored before reauthentication navigation, mounts the shared return page, resumes consent, and verifies handoff cleanup. Callback tests check trusted saved-connection context, fixed safe outcomes, failure audit metadata, denied membership and expired-state rejection, JSON API errors, and no provider text in browser returns. UI retries require a connection in the current company's catalog. All 80 OAuth route/service/UI/shared-handoff cases pass; server and UI source/test typechecks pass. - [Server shard 4/12 on the prior refresh](https://github.com/paperclipai/paperclip/actions/runs/43510727031/job/132180301436) passed 2,784 cases and skipped 21, then failed a reassignment fixture's `TRUNCATE companies CASCADE` cleanup with PostgreSQL `40P01`. Its automatic retry found the surviving `REA` prefix. The unchanged test passes all three cases locally. A controlled reader/cleanup overlap reproduces the same deadlock, but the exact competing CI query was not logged. Each case now has a distinct company prefix and identifier; the owned database is removed at suite end. The three test bodies and assertions are byte-identical. Eight local reassignment/lock-release cases pass, and the [final server shard 4/12 passes](https://github.com/paperclipai/paperclip/actions/runs/43518584814/job/132207159392). Production reassignment logic, deadlines, and retries are unchanged. - The initial master integration passed 330 cases across 17 suites. Coverage includes connector state, device discovery, network lifecycle, pairing, routes, SSH binding and exports, environment persistence, and connector UI. All three unpublished migrations were regenerated after master's `0479_short_darkstar`: `0480_lazy_lethal_legion`, `0481_round_pet_avengers`, and `0482_aberrant_rumiko_fujikawa`. Each SQL body is byte-identical to its earlier unpublished migration. Generated snapshots preserve existing master tables and add only the intended Tailscale table and secret-purpose column. Exactly five snapshots remain, 0478–0482. Numbering, safety, compatibility, and snapshot-drift checks pass. The later Docker refresh did not alter this migration sequence. - Earlier attempts remain part of the verification record. On `1c020a0`, Rust cache restoration and server shard 5/12 were interrupted by shutdown signals, Runner shard 2/3 lost communication with no retrievable log, and an amd64 provider download failed with npm `ECONNRESET`. Same-head retries recovered, without establishing the cause of the communication loss. Later superseded runs also had cancellations with incomplete logs. These interrupted attempts are not counted as passes. The final revision has fresh successful CI. - Local platform limits remain recorded. The Kubernetes suite on macOS passed 191 cases, failed 12 Linux `/proc/self/fd` transfer cases, and skipped four Linux-only cases. Unchanged source reproduced the same 12 failures. An initial run also lacked provider dependencies until the maintained preparation command installed them. Those local attempts are not green qualification. Final Linux provider CI passes. Local Docker qualification was unavailable; final image checks passed in CI. - Before PR separation, a live tailnet/SSH Claude Code task completed on Ubuntu ARM64 without provider login for the remote user. The selected Paperclip subscription ran it, wrote the expected file, posted completion, and marked the task done. File contents and private runtime cleanup were checked. This is prior live evidence; the final revision was not live-retested. Other providers have automated coverage only. Hosted registration for unrelated tailnets and production concurrency remain qualification limits below. - The Superagent security and supply-chain reports remain neutral and inconclusive. Earlier attempts to restart the failed security scan returned HTTP 404. Supply-chain intelligence lacked coverage for extracted pinned dependencies and the shared workflow reference. These reports do not establish successful security qualification. The required CI and review gates pass; this external coverage gap remains disclosed. ## Risks - The Superagent security and supply-chain reports are inconclusive. The security scan re-run request returned HTTP 404, and the supply-chain scanner lacks coverage for the extracted dependencies and shared workflow reference. These neutral results are not successful security qualification. - The server-owned secret purpose is internal database metadata, not an HTTP-writable field. The generic secret service owns enforcement so direct API reads and runtime projection share the same restriction. Previously copied node state remains valid outside Paperclip until the enrolled device is revoked in Tailscale; a local disconnect cannot stop an external copy. - Tailscale network configurations now require a connected Connector and selected machine. The unreleased key-only path is removed; any saved configuration from that path needs Connector setup. - The connector remains a pilot. The local test used an OAuth app registered in the consenting user's tailnet. Qualify hosted app registration for unrelated customer tailnets before general availability. - This implementation supports one serving process per instance. Concurrent replicas need exclusive ownership of the persisted network identity before production rollout. Preserve the database and company vault master key across restarts. - Each machine-pairing preparation creates a new vault secret. Unused generated keys follow the existing secret deletion lifecycle. A saved SSH key remains available as an explicit choice. - Apply additive migrations `0480_lazy_lethal_legion`, `0481_round_pet_avengers`, and `0482_aberrant_rumiko_fujikawa` before application rollout. The new credential version and digest columns are nullable. Existing unpinned connections require fresh OAuth consent; the migration deliberately does not trust or backfill a generic secret value. Regenerate or renumber migrations if master advances the sequence before merge. - The target needs an SSH server, its chosen harness, and allowed tailnet access. A reachable network does not grant SSH or provider authorization. Pairing reads standard `/etc/ssh` host-public-key files; custom host-key locations require manual known-hosts configuration. - Roll back the application revision to remove new entry points. Existing connection secrets and registered Tailscale nodes retain their separate lifecycle. Do not drop the table or delete node state as part of an application rollback. - Production qualification still needs denied-policy, revocation, cancellation, and deployment-overlap checks. Local Docker and a production deployment were not performed. ## Model Used OpenAI Codex, gpt-6.1-sol, xhigh reasoning, with local code execution and tool use. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used - [x] I have checked ROADMAP.md and this extends existing connector and environment features - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have described the issue in-PR using the feature request fields - [x] I have not referenced internal issues, local URLs, or private machine identifiers - [x] My branch name describes the change - [x] I have run focused tests locally and they pass - [x] I have added tests for new behavior and failure paths - [x] I have updated documentation - [x] I have documented migration and runtime risks - [x] All Paperclip CI gates are green - [ ] Required code-owner approval received for the workflow change - [x] Greptile Apex is 5/5 on the final head with no unresolved comments - [x] I will address CI and review findings before requesting merge --------- Co-authored-by: Paperclip --- .github/workflows/pr-trusted.yml | 3 + Dockerfile | 22 + doc/tailscale-environments.md | 231 +++++++ docker/tailscale.LICENSE | 28 + docs/api/overview.md | 5 + docs/api/secrets.md | 5 + docs/api/tailscale.md | 56 ++ .../src/remote-managed-runtime.ts | 2 + .../adapter-utils/src/ssh-tailscale.test.ts | 54 ++ packages/adapter-utils/src/ssh.ts | 19 +- .../migrations/0480_lazy_lethal_legion.sql | 17 + .../migrations/0481_round_pet_avengers.sql | 2 + .../0482_aberrant_rumiko_fujikawa.sql | 7 + ...{0477_snapshot.json => 0480_snapshot.json} | 531 +++++++++++++++- ...{0476_snapshot.json => 0481_snapshot.json} | 561 ++++++++++++++++- ...{0475_snapshot.json => 0482_snapshot.json} | 589 +++++++++++++++++- packages/db/src/migrations/meta/_journal.json | 21 + packages/db/src/schema/company_secrets.ts | 1 + packages/db/src/schema/index.ts | 1 + .../db/src/schema/tailscale_connections.ts | 21 + .../kubernetes/test/unit/file-sync.test.ts | 47 ++ packages/shared/src/types/environment.ts | 7 + scripts/docker-build-test.sh | 2 + .../__tests__/agent-secrets-routes.test.ts | 41 ++ .../src/__tests__/docker-build-stamp.test.ts | 2 +- .../src/__tests__/environment-config.test.ts | 23 +- .../src/__tests__/environment-routes.test.ts | 19 + .../issue-reassignment-queued-run.test.ts | 12 +- server/src/__tests__/openapi-routes.test.ts | 31 + .../ssh-tailscale-connection.test.ts | 119 ++++ .../tailscale-connection-routes.test.ts | 167 +++++ server/src/app.ts | 2 + server/src/index.ts | 3 +- server/src/routes/openapi.ts | 74 +++ server/src/routes/tailscale-connections.ts | 104 ++++ .../tool-access-connection-intent.test.ts | 17 + server/src/routes/tool-access.ts | 5 +- server/src/services/company-deletion.ts | 7 +- .../connectors/tailscale/connections.test.ts | 565 +++++++++++++++++ .../connectors/tailscale/connections.ts | 178 ++++++ .../services/connectors/tailscale/deletion.ts | 30 + .../connectors/tailscale/devices.test.ts | 21 + .../services/connectors/tailscale/devices.ts | 28 + .../connectors/tailscale/network.test.ts | 291 +++++++++ .../services/connectors/tailscale/network.ts | 171 +++++ .../connectors/tailscale/pairing.test.ts | 115 ++++ .../services/connectors/tailscale/pairing.ts | 54 ++ server/src/services/environment-config.ts | 97 ++- .../services/environment-execution-target.ts | 1 + server/src/services/environment-probe.ts | 4 + ...aperclip-cloud-connector-principal.test.ts | 9 + .../paperclip-cloud-connector-principal.ts | 3 + .../src/services/paperclip-cloud-connector.ts | 8 +- server/src/services/secrets.ts | 30 +- tests/e2e/chat-adapters-ui-messaging.spec.ts | 16 +- tests/e2e/legacy-failure-continuation.spec.ts | 9 +- tests/runner-e2e/chat-installed-lane.test.ts | 5 +- .../runner-e2e/source-sdk-public-inputs.json | 2 +- ui/public/brands/apps/manifest.json | 6 + ui/public/brands/apps/tailscale.png | Bin 0 -> 17645 bytes ui/src/App.tsx | 2 + ui/src/api/tailscale.ts | 12 + .../components/TailscaleSshMachine.test.tsx | 77 +++ ui/src/components/TailscaleSshMachine.tsx | 62 ++ ui/src/hooks/useTailscaleConnections.ts | 19 + ui/src/pages/CompanyEnvironments.test.tsx | 137 ++++ ui/src/pages/CompanyEnvironments.tsx | 66 +- ui/src/pages/CompanySettings.test.tsx | 2 +- ui/src/pages/apps/Browse.test.tsx | 35 ++ ui/src/pages/apps/Browse.tsx | 16 +- ui/src/pages/apps/TailscaleConnector.test.tsx | 169 +++++ ui/src/pages/apps/TailscaleConnector.tsx | 80 +++ 72 files changed, 5105 insertions(+), 73 deletions(-) create mode 100644 doc/tailscale-environments.md create mode 100644 docker/tailscale.LICENSE create mode 100644 docs/api/tailscale.md create mode 100644 packages/adapter-utils/src/ssh-tailscale.test.ts create mode 100644 packages/db/src/migrations/0480_lazy_lethal_legion.sql create mode 100644 packages/db/src/migrations/0481_round_pet_avengers.sql create mode 100644 packages/db/src/migrations/0482_aberrant_rumiko_fujikawa.sql rename packages/db/src/migrations/meta/{0477_snapshot.json => 0480_snapshot.json} (99%) rename packages/db/src/migrations/meta/{0476_snapshot.json => 0481_snapshot.json} (99%) rename packages/db/src/migrations/meta/{0475_snapshot.json => 0482_snapshot.json} (99%) create mode 100644 packages/db/src/schema/tailscale_connections.ts create mode 100644 server/src/__tests__/ssh-tailscale-connection.test.ts create mode 100644 server/src/__tests__/tailscale-connection-routes.test.ts create mode 100644 server/src/routes/tailscale-connections.ts create mode 100644 server/src/services/connectors/tailscale/connections.test.ts create mode 100644 server/src/services/connectors/tailscale/connections.ts create mode 100644 server/src/services/connectors/tailscale/deletion.ts create mode 100644 server/src/services/connectors/tailscale/devices.test.ts create mode 100644 server/src/services/connectors/tailscale/devices.ts create mode 100644 server/src/services/connectors/tailscale/network.test.ts create mode 100644 server/src/services/connectors/tailscale/network.ts create mode 100644 server/src/services/connectors/tailscale/pairing.test.ts create mode 100644 server/src/services/connectors/tailscale/pairing.ts create mode 100644 server/src/services/paperclip-cloud-connector-principal.test.ts create mode 100644 server/src/services/paperclip-cloud-connector-principal.ts create mode 100644 ui/public/brands/apps/tailscale.png create mode 100644 ui/src/api/tailscale.ts create mode 100644 ui/src/components/TailscaleSshMachine.test.tsx create mode 100644 ui/src/components/TailscaleSshMachine.tsx create mode 100644 ui/src/hooks/useTailscaleConnections.ts create mode 100644 ui/src/pages/apps/TailscaleConnector.test.tsx create mode 100644 ui/src/pages/apps/TailscaleConnector.tsx diff --git a/.github/workflows/pr-trusted.yml b/.github/workflows/pr-trusted.yml index 5c8dfb63cd1..513b9bdc52a 100644 --- a/.github/workflows/pr-trusted.yml +++ b/.github/workflows/pr-trusted.yml @@ -653,6 +653,9 @@ jobs: - name: Run generated-file drift checks against the Docker build context run: docker buildx build --file .github/docker-context-checks.Dockerfile . + - name: Verify Tailscale runtime clients + run: docker buildx build --file Dockerfile --target tailscale-client . + - name: Upload CI profile timings continue-on-error: true if: always() && env.PAPERCLIP_CI_PROFILE == 'true' diff --git a/Dockerfile b/Dockerfile index 14bf3c7b5e3..e38ac7bed68 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,24 @@ RUN usermod -u $USER_UID --non-unique node \ && groupmod -g $USER_GID --non-unique node \ && usermod -g $USER_GID -d /paperclip node +FROM base AS tailscale-client +ARG TARGETARCH +WORKDIR /out +RUN set -eu; \ + case "$TARGETARCH" in \ + amd64) digest=720cbbccbfc03d8b55df72afc19e5a72e31b0f65934700aa6fa38074d4ad7bf7 ;; \ + arm64) digest=e900b0e6e25023a1e504b2c237ce2a32ea0535e006f8c1a1097a490886493b6c ;; \ + *) echo "Unsupported Tailscale architecture: $TARGETARCH" >&2; exit 1 ;; \ + esac; \ + curl --fail --silent --show-error --location --retry 2 --connect-timeout 15 --max-time 120 --proto '=https' --proto-redir '=https' \ + "https://pkgs.tailscale.com/stable/tailscale_1.104.5_${TARGETARCH}.tgz" -o tailscale.tgz; \ + printf '%s tailscale.tgz\n' "$digest" > tailscale.sha256; \ + sha256sum -c tailscale.sha256; \ + tar -xzf tailscale.tgz --strip-components=1; \ + gosu node /out/tailscale version; \ + gosu node /out/tailscaled --version +COPY docker/tailscale.LICENSE /out/LICENSE + FROM base AS runner-lockfile WORKDIR /runner-lockfile COPY package.json pnpm-workspace.yaml pnpm-lock.yaml .npmrc ./ @@ -369,6 +387,10 @@ RUN echo "cli-tools-epoch: ${CLI_TOOLS_CACHE_EPOCH}" \ && mkdir -p /paperclip \ && chown node:node /paperclip +COPY --from=tailscale-client /out/tailscale /out/tailscaled /usr/local/bin/ +COPY --from=tailscale-client /out/LICENSE /usr/local/share/licenses/tailscale/LICENSE +RUN gosu node tailscale version && gosu node tailscaled --version + COPY scripts/docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh diff --git a/doc/tailscale-environments.md b/doc/tailscale-environments.md new file mode 100644 index 00000000000..733f4cab51d --- /dev/null +++ b/doc/tailscale-environments.md @@ -0,0 +1,231 @@ +# Use Tailscale machines in SSH environments + +The Tailscale connector gives Paperclip access to a tailnet. Paperclip joins a +separate device to that network, then uses the existing SSH environment driver +to run tasks on selected computers. The connector is managed from **Connectors** +and can serve multiple SSH environments in the same company. The computer is +not replaced or provisioned by Paperclip. + +In **Connectors**, find **Tailscale**, open it and click **Connect Tailscale Network**. +After approving consent, click **Create SSH environment**, or create an SSH +environment and select **Tailscale** under **Network**. Choose the connection +and machine, then configure the SSH user, port, absolute workspace path, private +key, and trusted host keys. The Tailscale network option appears once a tailnet +is connected. On a Mac +with the normal Tailscale application, enable macOS Remote Login. Tailscale SSH +server support on macOS requires the open-source tailscaled variant; ordinary +OpenSSH over Tailscale works with the standard Mac application. + +Cloud reauthentication saves the pending OAuth handoff in the current browser +tab before redirecting. If consent is canceled or completion fails after the +state and company checks, the browser returns to the saved connection with a +fixed explanation and a retry. The return URL contains only the connection ID +and a known outcome, without provider error text, claims, or OAuth state. + +SSH setup hides the Network selector when Direct connection is the only +available option. A sole connected tailnet is selected automatically and its +connection selector is hidden; multiple connected tailnets remain selectable. +Switching an existing environment to **Direct connection** clears its saved +Tailscale machine and credential references and saves the direct host you enter. + +After selecting a machine, **Prepare machine pairing** creates an SSH key in +Paperclip’s vault and displays a command containing only the public key. Each +preparation generates a fresh key pair. The command uses the public key produced +during that request, never mutable metadata from a saved secret. Run it +as the target SSH user. The command preserves existing authorized keys, including +files without a final newline. Before installing the key, it checks for readable +Ed25519, ECDSA, or RSA host public keys in `/etc/ssh` and stops without changing +`authorized_keys` if none are available. It prints that user's name, home +workspace path, and known-hosts entries for every available host key for the +form. You can also select an existing SSH private key secret. + +Pairing output is tied to the selected company, tailnet, machine, and SSH port. +After editing the port, prepare pairing again for matching known-hosts output. +A late response for an earlier port cannot replace the selected private key. +Switching to Direct connection or closing the form also prevents pending pairing +results from changing the selected key. + +Save the environment and test it. Select it as an agent’s environment and choose +any harness supported by SSH environments. Select the agent's AI account in +Paperclip. Runs use that account's subscription or API key, with private +credential directories on the target computer, just as sandbox runs do. The +computer supplies the installed harness and workspace; its SSH user's provider +login is not used for an agent with a configured AI account. Test the chosen +harness separately: SSH access does not prove provider authentication. + +SSH's **Advanced** section includes **Use authentication configured on the remote +machine**. New environments leave it off, requiring the agent's Paperclip AI +account. Turn it on to use the remote user's provider login instead. Existing +SSH environments display it as on; opening or upgrading an installation does +not rewrite their saved configuration. The same option applies to direct SSH +and Tailscale connections. + +The environment keeps the `ssh` driver. Machine selection stores the connector +ID and Tailscale’s stable device ID, and runtime +resolution looks up its current address through that connection’s private +socket. Offline, removed, disconnected, and wrong-company machines fail closed. +The host’s existing Tailscale connection is never a fallback. + +## OAuth and credential custody + +The connection uses the Cloud OAuth broker profile `tailscale.tailnet`, with +`auth_keys:create:once`. It is a network connector on the Connectors surface; +it does not publish agent tools or enter MCP discovery. Management requires an +instance administrator with access to the company. The catalog and setup page +check that access before requesting network data; other company members are +directed to an instance administrator. Existing SSH environment access governs +which agents can execute there. Self-hosted local +instances first complete the existing Paperclip Cloud enrollment and return to +their company's Tailscale connector page to continue network consent. Cloud +stacks use their existing broker identity and handoff. + +Tailscale returns a single-use device enrollment token without a refresh token. +The broker seals it to the originating instance. The instance validates the +user, company, state expiry and single-use callback, stores the token through the +configured company secret provider, and immediately replaces its current value +with the enrolled node state before permitting execution. The connection records +the exact trusted vault version and a SHA-256 digest of the server-written value. +Runtime reads that version and verifies its digest. A generic company secret +rotation cannot change the authorized network. Trusted node-state saves advance +both fields together, with a compare-and-set guard so an older save cannot +replace a newer state. Reads keep using the previous trusted version until the +new version is committed. Tokens, private node state, digests and client secrets +never appear in connection or environment responses. + +Enrollment tokens and node state are server-owned secrets with a durable +`system_purpose` classification. Generic company catalogs omit them. Generic +bindings, environment injection, and agent value reads reject them, including +bindings saved before the restriction. Renaming or rotating a secret cannot +remove this classification. Disconnecting or replacing a connection leaves its +old credential protected. Only the connector's server consumer can resolve its +trusted value; an SSH environment binds its private key, never the node state. +Unclassified legacy node-state values are also rejected by generic reads, +including state left behind by an earlier connection replacement. +Migration `0482_aberrant_rumiko_fujikawa` classifies existing connector-referenced secrets. +Apply it before running the updated server. If node state was copied before +this restriction, revoke that enrolled device in Tailscale; a local disconnect +cannot stop a copy outside Paperclip. + +Connections created before migration `0481_round_pet_avengers` have no trusted version +and digest. They fail closed and require a fresh **Connect Tailscale Network** +consent. The migration leaves these fields empty; it does not trust or backfill a +generic secret's latest value. + +Each connection has a separate userspace daemon and private socket. A stale +credential read reloads the trusted value before replacing a ready daemon and +rechecks which daemon is active after the reload. It preserves a healthy newer +socket and cannot cross a manager shutdown. Failed connection requests leave +cleanup to the network manager's exact client; they cannot disconnect a newer +retry using the same connector identity. The daemon accepts no subnet routes or +DNS changes and blocks incoming connections. Normal shutdown saves +node state and removes temporary files. A restart restores the enrolled device; +it does not reuse the OAuth token or require consent for every task. Disconnect +invalidates pending consent and stops the connection’s local daemon. A callback +already exchanging or saving credentials cannot reconnect after disconnect or +replace a newer authorization. Completion retains the consumed state's digest +as a private attempt marker, with no expiry; disconnect replaces that marker. + +Database writes and credential reloads verify ownership without holding a database +lock across broker or daemon startup. Cancelled attempts close their daemon and +remove their newly created secret without touching a newer connection's state. +Disconnect does not remove the target computer from Tailscale. + +Company deletion removes connector records before vault secrets in the company +transaction. After commit, it stops every daemon owned by those records. A +rolled-back deletion leaves the connector and its daemon intact. Deleting a +connector's vault secret invalidates pending consent, disconnects its network, +and clears the trusted credential reference before removing the secret. If +shutdown fails, the disconnected reference remains available for a deletion +retry. Late daemon state writes cannot restore a retired connector. + +Task runtime resolution obtains its socket from this same guarded connection +service after loading the SSH key. It requires the connection to remain connected +with the environment's saved credential identity during credential reload, +daemon startup, and device discovery. A disconnect or replacement rejects the +run before returning a socket; runtime resolution cannot restart the connection +from a stale saved secret. +Saved network credentials without a connector and machine selection are rejected; +all Tailscale environments use the connector's lifecycle. + +This implementation currently supports one serving process per instance. +Concurrent replicas need a durable connection ownership lease before rollout. + +The enrollment token is consumed once; it is not the runtime credential. +Tailscale identity belongs to the persisted machine key in the saved node state, +not the container ID or hostname. A redeploy must retain both the database's +encrypted node state and the same vault master key. Cloud already materializes +the stable per-stack `PAPERCLIP_SECRETS_MASTER_KEY` across release rolls. Do not +make the connector ephemeral or replace its node state at each start. Losing +that state, device removal, or a tailnet reauthentication policy can require +fresh consent. The app-owned device has the consenting user's tailnet rights. + +## Local development + +Run the Paperclip instance at `http://localhost:3100` and the local Cloud broker +at `http://localhost:3000`. Register the OAuth app callback as +`http://localhost:3000/v1/connector/oauth/tailscale/callback` with scope +`auth_keys:create:once`. The OAuth app must be registered in the authorizing +user’s tailnet under Tailscale’s currently documented restriction. + +Configure the local broker with: + +- `CLOUD_HARNESS_CONNECTOR_TAILSCALE_ENABLED=1` +- `CLOUD_HARNESS_CONNECTOR_TAILSCALE_CLIENT_ID` +- `CLOUD_HARNESS_CONNECTOR_TAILSCALE_CLIENT_SECRET_REF=secret://local/tailscale` +- `CLOUD_HARNESS_CONNECTOR_TAILSCALE_LOCAL_CLIENT_SECRET_FILE`, an owner-only + regular file outside the checkout containing the OAuth app client secret +- `CLOUD_HARNESS_CONNECTOR_TAILSCALE_PILOT_INSTANCE_IDS`, the exact enrolled local + instance ID + +The file credential path is accepted only for a loopback HTTP development broker +using the local fake provider. Hosted credentials continue to use the configured +provider secret store. Never regenerate an existing Cloud `.env` or identity +salt to configure this test. + +The instance needs `tailscale` and `tailscaled`. Its OAuth return path is +`/api/tools/oauth/tailscale/callback`, and completion returns to the Tailscale +connector page. For a separate local userspace +client, install the pinned programs outside the checkout: + +```sh +GOBIN="$HOME/.local/share/paperclip-tailscale/bin" go install tailscale.com/cmd/tailscale@v1.104.5 +GOBIN="$HOME/.local/share/paperclip-tailscale/bin" go install tailscale.com/cmd/tailscaled@v1.104.5 +``` + +Set `PAPERCLIP_TAILSCALE_CLI` and `PAPERCLIP_TAILSCALED` to their absolute paths. +These settings can be set in the supported `.env` files. Each new client reads +them at startup, after environment loading. +Set `PAPERCLIP_CLOUD_CONNECTOR_BASE_URL=http://localhost:3000` and +`PAPERCLIP_CLOUD_CONNECTOR_ENVIRONMENT=development` for local enrollment. + +## Verification and rollout gates + +Offline tests cover broker handoff and sealed claims, company and user binding, +expired and competing callbacks, disconnect during claim exchange, secret +creation, credential reload and daemon startup, superseding authorizations, +failed-enrollment cleanup, denied authorization, device discovery, driver +selection, private subprocess state, shutdown, and restart. These do not prove +live consent or remote execution. + +Before rollout, qualify real consent, machine discovery, a Claude task that +writes a file on the selected computer, result streaming, cancellation, server +restart, machine sleep/reboot, and revocation. A local instance whose public URL +is localhost also needs an API path reachable by the remote agent for tasks that +call back to Paperclip unless the adapter uses the existing remote callback +bridge. Production Cloud uses its public HTTPS stack URL. + +The connector's bundled artwork comes from the vendor's +`https://tailscale.com/favicon.png`, copied without modification (SHA-256 +`680570c9090b604450792f68ceed10beeb33a69c9bccc2a92e398564c7075a5c`). + +The GA announcement describes third-party integrations, but the published OAuth +setup guide still restricts each app to its own tailnet. This profile remains +behind explicit instance pilot admission. A local personal-tailnet test does not +establish that one Paperclip-owned OAuth app can authorize every customer’s +personal tailnet. Verify that provider registration path before general rollout. + +Provider references: + +- [OAuth apps](https://tailscale.com/docs/features/oauth-apps) +- [Device provisioning](https://tailscale.com/docs/features/oauth-apps/device-provisioning) +- [Tailscale SSH](https://tailscale.com/docs/features/tailscale-ssh) diff --git a/docker/tailscale.LICENSE b/docker/tailscale.LICENSE new file mode 100644 index 00000000000..ed6e4bb6d6b --- /dev/null +++ b/docker/tailscale.LICENSE @@ -0,0 +1,28 @@ +BSD 3-Clause License + +Copyright (c) 2020 Tailscale Inc & contributors. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its + contributors may be used to endorse or promote products derived from + this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/docs/api/overview.md b/docs/api/overview.md index ea66c673954..f67e629812e 100644 --- a/docs/api/overview.md +++ b/docs/api/overview.md @@ -62,3 +62,8 @@ List endpoints support standard pagination query parameters when applicable. Res ## Rate Limiting No rate limiting is enforced in local deployments. Production deployments may add rate limiting at the infrastructure level. + +## Network Connectors + +[Tailscale connections](tailscale.md) let instance administrators connect a +company tailnet and select its machines in SSH environments. diff --git a/docs/api/secrets.md b/docs/api/secrets.md index 5300eb8a872..a236553f94a 100644 --- a/docs/api/secrets.md +++ b/docs/api/secrets.md @@ -5,6 +5,11 @@ summary: Secrets CRUD Manage encrypted secrets that agents receive through environment bindings or fetch on demand. +Server-owned connector credentials, including Tailscale enrollment tokens and +node state, are excluded from company secret catalogs. Generic secret bindings, +runtime projection, and agent value reads cannot resolve them. Existing bindings +do not bypass this restriction; those requests return `403`. + ## Agent List and Fetch These routes require the current run-bound agent JWT. They are not available to diff --git a/docs/api/tailscale.md b/docs/api/tailscale.md new file mode 100644 index 00000000000..8bd2a4c79f2 --- /dev/null +++ b/docs/api/tailscale.md @@ -0,0 +1,56 @@ +--- +title: Tailscale Connections +summary: Connect a company tailnet and select its machines in SSH environments +--- + +These endpoints require a board operator with instance-admin rights and access +to the company. Local implicit board access is also accepted. Agent credentials +cannot manage a Tailscale connection. Browser mutations require a trusted Origin. + +| Method | Path | Result | +|--------|------|--------| +| GET | `/api/companies/{companyId}/tailscale-connections` | Connection metadata | +| POST | `/api/companies/{companyId}/tailscale-connections` | Authorization URL | +| GET | `/api/companies/{companyId}/tailscale-connections/{connectionId}/devices` | Available machines | +| POST | `/api/companies/{companyId}/tailscale-connections/{connectionId}/pairing` | Public SSH pairing command | +| DELETE | `/api/companies/{companyId}/tailscale-connections/{connectionId}` | Disconnected connection metadata | + +A connection response contains only `id`, `companyId`, `name`, and `status`. +Status is `pending`, `connecting`, `connected`, or `disconnected`. Responses do +not include enrollment tokens, persisted node state, or OAuth state digests. + +To start consent, POST a JSON object with optional `name` and `connectionId`. +The name defaults to `My tailnet`. An existing connection ID starts a new +authorization for that connection. The `201` response contains `authorizationUrl` +and `expiresAt`. It also contains `connection` when consent can start immediately. +An instance without Cloud enrollment first returns the enrollment URL. Hosted +consent can include a `handoff` with `kind: "paperclip_cloud"` and an opaque +`session`. Use the normal browser connector flow for that signed handoff. + +Machine discovery returns `id`, `name`, `hostname`, `address`, `os`, and `online` +for each peer. Use the stable `id` when configuring a Tailscale-backed SSH +environment. The current address is resolved again for execution. + +An SSH environment with `network: "tailscale"` requires `tailscaleConnectionId` +and `tailscaleDeviceId`. Saving binds the connector's current credential identity. +Raw enrollment keys and credential-only configurations are rejected. Runtime +access checks the connection again after loading the SSH key and after device +discovery; a disconnected or replaced connection requires fresh setup. + +To prepare pairing, POST `{ "deviceId": "", "port": 22 }`. +The port defaults to 22. The response contains `privateKeySecretId`, `publicKey`, +and `command`. Each preparation generates a fresh key pair. Run the command as +the target SSH user and configure the environment with the returned secret ID. +The private key never appears in this response. + +The browser returns from consent to +`GET /api/tools/oauth/tailscale/callback` with required `state` and either +`claim_id` or an authorization `error`. The callback rechecks the initiating +operator's company access and consumes state once. On success it returns a +`303` redirect to the company's connector page with `Cache-Control: no-store` +and `Referrer-Policy: no-referrer`. + +Disconnect invalidates both pending consent and callbacks already completing. +It stops the private network client and keeps the connection record. It does +not remove the target computer from Tailscale. A later authorization can replace +the saved connection; older callbacks cannot replace that newer authorization. diff --git a/packages/adapter-utils/src/remote-managed-runtime.ts b/packages/adapter-utils/src/remote-managed-runtime.ts index c31e911441a..3b9d50ff5ad 100644 --- a/packages/adapter-utils/src/remote-managed-runtime.ts +++ b/packages/adapter-utils/src/remote-managed-runtime.ts @@ -99,6 +99,7 @@ export function buildRemoteExecutionSessionIdentity(spec: SshRemoteExecutionSpec return { transport: "ssh", ...(typeof spec.useHostAuth === "boolean" ? { useHostAuth: spec.useHostAuth } : {}), + ...(spec.tailscaleIdentity ? { tailscaleIdentity: spec.tailscaleIdentity } : {}), host: spec.host, port: spec.port, username: spec.username, @@ -114,6 +115,7 @@ export function remoteExecutionSessionMatches(saved: unknown, current: SshRemote return ( asString(parsedSaved.transport) === currentIdentity.transport && parsedSaved.useHostAuth === currentIdentity.useHostAuth && + asString(parsedSaved.tailscaleIdentity) === asString(currentIdentity.tailscaleIdentity) && asString(parsedSaved.host) === currentIdentity.host && asNumber(parsedSaved.port) === currentIdentity.port && asString(parsedSaved.username) === currentIdentity.username && diff --git a/packages/adapter-utils/src/ssh-tailscale.test.ts b/packages/adapter-utils/src/ssh-tailscale.test.ts new file mode 100644 index 00000000000..578d945c2d8 --- /dev/null +++ b/packages/adapter-utils/src/ssh-tailscale.test.ts @@ -0,0 +1,54 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { buildSshSpawnTarget, parseSshRemoteExecutionSpec } from "./ssh.js"; +import { buildRemoteExecutionSessionIdentity, remoteExecutionSessionMatches } from "./remote-managed-runtime.js"; +import { normalizeAdapterExecutionTargetSessionIdentity } from "./execution-target.js"; + +const spec = { host: "workstation.tailnet.test", port: 22, username: "developer", remoteWorkspacePath: "/workspace", + remoteCwd: "/workspace/task", privateKey: null, knownHosts: "host ssh-ed25519 fixture", strictHostKeyChecking: true }; + +describe("SSH task execution through Tailscale", () => { + afterEach(() => { vi.unstubAllEnvs(); }); + + it.each(["/tmp/Paperclip tools/tailscale", "/tmp/tools%h/tailscale"])("uses the configured Tailscale executable without interpreting its path (%s)", async cli => { + vi.stubEnv("PAPERCLIP_TAILSCALE_CLI", cli); + const target = await buildSshSpawnTarget({ spec: { ...spec, tailscaleSocket: "/tmp/paperclip-ts-test/socket" }, command: "true", args: [], env: {} }); + try { + expect(target.args).toContain(`ProxyCommand='${cli.replaceAll("%", "%%")}' --socket=/tmp/paperclip-ts-test/socket nc %h %p`); + } finally { await target.cleanup(); } + }); + it("uses the isolated tailnet socket for command execution while keeping host verification", async () => { + const target = await buildSshSpawnTarget({ spec: { ...spec, tailscaleSocket: "/tmp/paperclip-ts-test/socket" }, command: "true", args: [], env: {} }); + try { + expect(target.command).toBe("ssh"); + expect(target.args).toContain("ProxyCommand=tailscale --socket=/tmp/paperclip-ts-test/socket nc %h %p"); + expect(target.args).toContain("StrictHostKeyChecking=yes"); + expect(target.args).toContain("BatchMode=yes"); + } finally { await target.cleanup(); } + }); + + it("leaves direct SSH connections independent of Tailscale", async () => { + const target = await buildSshSpawnTarget({ spec, command: "true", args: [], env: {} }); + try { expect(target.args.some(arg => arg.startsWith("ProxyCommand"))).toBe(false); } + finally { await target.cleanup(); } + }); + + it.each(["/tmp/socket;touch bad", "/tmp/$(touch bad)", "relative/socket", "/tmp/socket%h"])("rejects unsafe socket %s", async tailscaleSocket => { + await expect(buildSshSpawnTarget({ spec: { ...spec, tailscaleSocket }, command: "true", args: [], env: {} })).rejects.toThrow("Invalid Tailscale socket"); + expect(parseSshRemoteExecutionSpec({ ...spec, tailscaleSocket })).toBeNull(); + }); + + it.each(["host;touch bad", "$(touch bad)", "-oProxyCommand=bad", "host%h"])("rejects host interpolation %s", async host => { + await expect(buildSshSpawnTarget({ spec: { ...spec, host, tailscaleSocket: "/tmp/socket" }, command: "true", args: [], env: {} })).rejects.toThrow("Invalid Tailscale host"); + }); + + it("preserves the transport through execution-spec parsing and isolates sessions across tailnets", () => { + const tailscale = { ...spec, tailscaleSocket: "/tmp/socket", tailscaleIdentity: "company:connection-one" }; + expect(parseSshRemoteExecutionSpec(tailscale)).toEqual(tailscale); + const saved = buildRemoteExecutionSessionIdentity(tailscale); + expect(normalizeAdapterExecutionTargetSessionIdentity(saved)).toEqual(saved); + expect(saved).not.toHaveProperty("tailscaleSocket"); + expect(remoteExecutionSessionMatches(saved, { ...tailscale, tailscaleSocket: "/tmp/restarted/socket" })).toBe(true); + expect(remoteExecutionSessionMatches(saved, { ...tailscale, tailscaleIdentity: "company:connection-two" })).toBe(false); + expect(remoteExecutionSessionMatches(saved, spec)).toBe(false); + }); +}); diff --git a/packages/adapter-utils/src/ssh.ts b/packages/adapter-utils/src/ssh.ts index 2ed0f81bd0e..7943cd2438a 100644 --- a/packages/adapter-utils/src/ssh.ts +++ b/packages/adapter-utils/src/ssh.ts @@ -30,6 +30,8 @@ const SSH_STDIN_COMMAND = "IFS= read -r paperclip_ssh_command_b64 || exit 125; p export interface SshConnectionConfig { useHostAuth?: boolean; + tailscaleSocket?: string; + tailscaleIdentity?: string; host: string; port: number; username: string; @@ -178,8 +180,14 @@ export function parseSshRemoteExecutionSpec(value: unknown): SshRemoteExecutionS } if (parsed.useHostAuth !== undefined && typeof parsed.useHostAuth !== "boolean") return null; + if (parsed.tailscaleSocket !== undefined && (typeof parsed.tailscaleSocket !== "string" || !/^\/[A-Za-z0-9_./-]+$/.test(parsed.tailscaleSocket))) return null; + + if (parsed.tailscaleIdentity && !parsed.tailscaleSocket) return null; + return { ...(typeof parsed.useHostAuth === "boolean" ? { useHostAuth: parsed.useHostAuth } : {}), + ...(typeof parsed.tailscaleSocket === "string" ? { tailscaleSocket: parsed.tailscaleSocket } : {}), + ...(typeof parsed.tailscaleIdentity === "string" ? { tailscaleIdentity: parsed.tailscaleIdentity } : {}), host, port: portValue, username, @@ -382,7 +390,7 @@ async function withTempFile( } async function createSshAuthArgs( - config: Pick, + config: Pick, ): Promise<{ args: string[]; cleanup: () => Promise }> { const tempFiles: Array<() => Promise> = []; const sshArgs = [ @@ -394,6 +402,15 @@ async function createSshAuthArgs( `StrictHostKeyChecking=${config.strictHostKeyChecking ? "yes" : "no"}`, ]; + if (config.tailscaleIdentity && !config.tailscaleSocket) throw new Error("Tailscale connection is unavailable"); + if (config.tailscaleSocket) { + if (!net.isIP(config.host) && !/^[A-Za-z0-9][A-Za-z0-9_.-]*$/.test(config.host)) throw new Error("Invalid Tailscale host"); + if (!/^\/[A-Za-z0-9_./-]+$/.test(config.tailscaleSocket)) throw new Error("Invalid Tailscale socket path"); + const configuredCli = process.env.PAPERCLIP_TAILSCALE_CLI?.trim(); + const cli = configuredCli ? shellQuote(configuredCli).replaceAll("%", "%%") : "tailscale"; + sshArgs.push("-o", `ProxyCommand=${cli} --socket=${config.tailscaleSocket} nc %h %p`); + } + if (config.strictHostKeyChecking) { if (config.knownHosts) { const knownHosts = await withTempFile("paperclip-ssh-known-hosts-", config.knownHosts, 0o600); diff --git a/packages/db/src/migrations/0480_lazy_lethal_legion.sql b/packages/db/src/migrations/0480_lazy_lethal_legion.sql new file mode 100644 index 00000000000..8e7616511a3 --- /dev/null +++ b/packages/db/src/migrations/0480_lazy_lethal_legion.sql @@ -0,0 +1,17 @@ +CREATE TABLE "tailscale_connections" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "company_id" uuid NOT NULL, + "name" text NOT NULL, + "status" text DEFAULT 'pending' NOT NULL, + "secret_id" uuid, + "created_by_user_id" text NOT NULL, + "oauth_state_hash" text, + "oauth_expires_at" timestamp with time zone, + "created_at" timestamp with time zone DEFAULT now() NOT NULL, + "updated_at" timestamp with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +ALTER TABLE "tailscale_connections" ADD CONSTRAINT "tailscale_connections_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint +ALTER TABLE "tailscale_connections" ADD CONSTRAINT "tailscale_connections_secret_id_company_secrets_id_fk" FOREIGN KEY ("secret_id") REFERENCES "public"."company_secrets"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint +CREATE INDEX "tailscale_connections_company_idx" ON "tailscale_connections" USING btree ("company_id");--> statement-breakpoint +CREATE UNIQUE INDEX "tailscale_connections_oauth_state_idx" ON "tailscale_connections" USING btree ("oauth_state_hash"); \ No newline at end of file diff --git a/packages/db/src/migrations/0481_round_pet_avengers.sql b/packages/db/src/migrations/0481_round_pet_avengers.sql new file mode 100644 index 00000000000..5dfb4ca33d4 --- /dev/null +++ b/packages/db/src/migrations/0481_round_pet_avengers.sql @@ -0,0 +1,2 @@ +ALTER TABLE "tailscale_connections" ADD COLUMN "credential_version" integer;--> statement-breakpoint +ALTER TABLE "tailscale_connections" ADD COLUMN "credential_hash" text; \ No newline at end of file diff --git a/packages/db/src/migrations/0482_aberrant_rumiko_fujikawa.sql b/packages/db/src/migrations/0482_aberrant_rumiko_fujikawa.sql new file mode 100644 index 00000000000..ad0088b4c74 --- /dev/null +++ b/packages/db/src/migrations/0482_aberrant_rumiko_fujikawa.sql @@ -0,0 +1,7 @@ +-- paperclip:migration-compat online_compatible: Nullable metadata ignored by older releases; backfill touches only connector-referenced secret rows through their primary keys. +ALTER TABLE "company_secrets" ADD COLUMN "system_purpose" text; +--> statement-breakpoint +UPDATE "company_secrets" AS secret +SET "system_purpose" = 'tailscale_network' +FROM "tailscale_connections" AS connection +WHERE secret.id = connection.secret_id AND secret.company_id = connection.company_id; diff --git a/packages/db/src/migrations/meta/0477_snapshot.json b/packages/db/src/migrations/meta/0480_snapshot.json similarity index 99% rename from packages/db/src/migrations/meta/0477_snapshot.json rename to packages/db/src/migrations/meta/0480_snapshot.json index c93269cfbfb..b4ce9431fde 100644 --- a/packages/db/src/migrations/meta/0477_snapshot.json +++ b/packages/db/src/migrations/meta/0480_snapshot.json @@ -1,6 +1,6 @@ { - "id": "d5d69127-12de-4281-9816-c20e013bfeb7", - "prevId": "82dcdc57-0796-4162-9451-8ea08a72199e", + "id": "55a8c37c-453c-4be4-b657-b703932836a0", + "prevId": "f5f852bd-0568-4b2d-bc35-0dac5d9c8c5b", "version": "7", "dialect": "postgresql", "tables": { @@ -35261,6 +35261,12 @@ "notNull": true, "default": 1 }, + "native_source_cursors": { + "name": "native_source_cursors", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, "native_phase": { "name": "native_phase", "type": "text", @@ -55947,6 +55953,141 @@ "checkConstraints": {}, "isRLSEnabled": false }, + "public.tailscale_connections": { + "name": "tailscale_connections", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "secret_id": { + "name": "secret_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "oauth_state_hash": { + "name": "oauth_state_hash", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "oauth_expires_at": { + "name": "oauth_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tailscale_connections_company_idx": { + "name": "tailscale_connections_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tailscale_connections_oauth_state_idx": { + "name": "tailscale_connections_oauth_state_idx", + "columns": [ + { + "expression": "oauth_state_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "tailscale_connections_company_id_companies_id_fk": { + "name": "tailscale_connections_company_id_companies_id_fk", + "tableFrom": "tailscale_connections", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tailscale_connections_secret_id_company_secrets_id_fk": { + "name": "tailscale_connections_secret_id_company_secrets_id_fk", + "tableFrom": "tailscale_connections", + "tableTo": "company_secrets", + "columnsFrom": [ + "secret_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, "public.tool_access_audit_events": { "name": "tool_access_audit_events", "schema": "", @@ -60996,6 +61137,392 @@ "checkConstraints": {}, "isRLSEnabled": false }, + "public.tool_mcp_tasks": { + "name": "tool_mcp_tasks", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "invocation_id": { + "name": "invocation_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "connection_id": { + "name": "connection_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "grant_id": { + "name": "grant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "run_id": { + "name": "run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "actor_type": { + "name": "actor_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "actor_id": { + "name": "actor_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "responsible_user_id": { + "name": "responsible_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "identity_context_id": { + "name": "identity_context_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "catalog_tool_name": { + "name": "catalog_tool_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "request_hash": { + "name": "request_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "endpoint_hash": { + "name": "endpoint_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "sealed_caller_headers": { + "name": "sealed_caller_headers", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "protocol_version": { + "name": "protocol_version", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "sealed_provider_task_id": { + "name": "sealed_provider_task_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider_task_id_hash": { + "name": "provider_task_id_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "poll_interval_ms": { + "name": "poll_interval_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "ttl_ms": { + "name": "ttl_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "next_poll_at": { + "name": "next_poll_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_polled_at": { + "name": "last_polled_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "poll_failures": { + "name": "poll_failures", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "lease_expires_at": { + "name": "lease_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "lease_owner": { + "name": "lease_owner", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "arguments_identity": { + "name": "arguments_identity", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "answered_input_keys": { + "name": "answered_input_keys", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "surfaced_input_keys": { + "name": "surfaced_input_keys", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "input_wake_count": { + "name": "input_wake_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "input_wake_at": { + "name": "input_wake_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "error_code": { + "name": "error_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "wake_requested_at": { + "name": "wake_requested_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "delivered_at": { + "name": "delivered_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_invocation_id": { + "name": "last_invocation_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tool_mcp_tasks_provider_task_uq": { + "name": "tool_mcp_tasks_provider_task_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "provider_task_id_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_attach_idx": { + "name": "tool_mcp_tasks_attach_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "request_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_invocation_idx": { + "name": "tool_mcp_tasks_invocation_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "invocation_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_next_poll_idx": { + "name": "tool_mcp_tasks_next_poll_idx", + "columns": [ + { + "expression": "next_poll_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"tool_mcp_tasks\".\"next_poll_at\" is not null", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "tool_mcp_tasks_company_id_companies_id_fk": { + "name": "tool_mcp_tasks_company_id_companies_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tool_mcp_tasks_invocation_id_tool_invocations_id_fk": { + "name": "tool_mcp_tasks_invocation_id_tool_invocations_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "tool_invocations", + "columnsFrom": [ + "invocation_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tool_mcp_tasks_connection_id_tool_connections_id_fk": { + "name": "tool_mcp_tasks_connection_id_tool_connections_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "tool_connections", + "columnsFrom": [ + "connection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, "public.tool_oauth_states": { "name": "tool_oauth_states", "schema": "", diff --git a/packages/db/src/migrations/meta/0476_snapshot.json b/packages/db/src/migrations/meta/0481_snapshot.json similarity index 99% rename from packages/db/src/migrations/meta/0476_snapshot.json rename to packages/db/src/migrations/meta/0481_snapshot.json index 317026f43a0..c4ad44771b7 100644 --- a/packages/db/src/migrations/meta/0476_snapshot.json +++ b/packages/db/src/migrations/meta/0481_snapshot.json @@ -1,6 +1,6 @@ { - "id": "82dcdc57-0796-4162-9451-8ea08a72199e", - "prevId": "ee1f1f66-0cbe-4b18-b364-5e96708a1f0b", + "id": "41eacc6a-6eab-4f01-965a-62aa0cd6778f", + "prevId": "55a8c37c-453c-4be4-b657-b703932836a0", "version": "7", "dialect": "postgresql", "tables": { @@ -35261,6 +35261,12 @@ "notNull": true, "default": 1 }, + "native_source_cursors": { + "name": "native_source_cursors", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, "native_phase": { "name": "native_phase", "type": "text", @@ -55947,6 +55953,153 @@ "checkConstraints": {}, "isRLSEnabled": false }, + "public.tailscale_connections": { + "name": "tailscale_connections", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "secret_id": { + "name": "secret_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "credential_version": { + "name": "credential_version", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "credential_hash": { + "name": "credential_hash", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "oauth_state_hash": { + "name": "oauth_state_hash", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "oauth_expires_at": { + "name": "oauth_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tailscale_connections_company_idx": { + "name": "tailscale_connections_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tailscale_connections_oauth_state_idx": { + "name": "tailscale_connections_oauth_state_idx", + "columns": [ + { + "expression": "oauth_state_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "tailscale_connections_company_id_companies_id_fk": { + "name": "tailscale_connections_company_id_companies_id_fk", + "tableFrom": "tailscale_connections", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tailscale_connections_secret_id_company_secrets_id_fk": { + "name": "tailscale_connections_secret_id_company_secrets_id_fk", + "tableFrom": "tailscale_connections", + "tableTo": "company_secrets", + "columnsFrom": [ + "secret_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, "public.tool_access_audit_events": { "name": "tool_access_audit_events", "schema": "", @@ -57564,6 +57717,12 @@ "primaryKey": false, "notNull": false }, + "icons": { + "name": "icons", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, "risk_level": { "name": "risk_level", "type": "text", @@ -59785,6 +59944,18 @@ "primaryKey": false, "notNull": true }, + "model_context": { + "name": "model_context", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "model_context_updated_at": { + "name": "model_context_updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, "created_at": { "name": "created_at", "type": "timestamp with time zone", @@ -60978,6 +61149,392 @@ "checkConstraints": {}, "isRLSEnabled": false }, + "public.tool_mcp_tasks": { + "name": "tool_mcp_tasks", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "invocation_id": { + "name": "invocation_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "connection_id": { + "name": "connection_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "grant_id": { + "name": "grant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "run_id": { + "name": "run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "actor_type": { + "name": "actor_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "actor_id": { + "name": "actor_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "responsible_user_id": { + "name": "responsible_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "identity_context_id": { + "name": "identity_context_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "catalog_tool_name": { + "name": "catalog_tool_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "request_hash": { + "name": "request_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "endpoint_hash": { + "name": "endpoint_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "sealed_caller_headers": { + "name": "sealed_caller_headers", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "protocol_version": { + "name": "protocol_version", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "sealed_provider_task_id": { + "name": "sealed_provider_task_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider_task_id_hash": { + "name": "provider_task_id_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "poll_interval_ms": { + "name": "poll_interval_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "ttl_ms": { + "name": "ttl_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "next_poll_at": { + "name": "next_poll_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_polled_at": { + "name": "last_polled_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "poll_failures": { + "name": "poll_failures", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "lease_expires_at": { + "name": "lease_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "lease_owner": { + "name": "lease_owner", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "arguments_identity": { + "name": "arguments_identity", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "answered_input_keys": { + "name": "answered_input_keys", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "surfaced_input_keys": { + "name": "surfaced_input_keys", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "input_wake_count": { + "name": "input_wake_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "input_wake_at": { + "name": "input_wake_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "error_code": { + "name": "error_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "wake_requested_at": { + "name": "wake_requested_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "delivered_at": { + "name": "delivered_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_invocation_id": { + "name": "last_invocation_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tool_mcp_tasks_provider_task_uq": { + "name": "tool_mcp_tasks_provider_task_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "provider_task_id_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_attach_idx": { + "name": "tool_mcp_tasks_attach_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "request_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_invocation_idx": { + "name": "tool_mcp_tasks_invocation_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "invocation_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_next_poll_idx": { + "name": "tool_mcp_tasks_next_poll_idx", + "columns": [ + { + "expression": "next_poll_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"tool_mcp_tasks\".\"next_poll_at\" is not null", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "tool_mcp_tasks_company_id_companies_id_fk": { + "name": "tool_mcp_tasks_company_id_companies_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tool_mcp_tasks_invocation_id_tool_invocations_id_fk": { + "name": "tool_mcp_tasks_invocation_id_tool_invocations_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "tool_invocations", + "columnsFrom": [ + "invocation_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tool_mcp_tasks_connection_id_tool_connections_id_fk": { + "name": "tool_mcp_tasks_connection_id_tool_connections_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "tool_connections", + "columnsFrom": [ + "connection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, "public.tool_oauth_states": { "name": "tool_oauth_states", "schema": "", diff --git a/packages/db/src/migrations/meta/0475_snapshot.json b/packages/db/src/migrations/meta/0482_snapshot.json similarity index 99% rename from packages/db/src/migrations/meta/0475_snapshot.json rename to packages/db/src/migrations/meta/0482_snapshot.json index 7b5ecc397be..fe3c626a03e 100644 --- a/packages/db/src/migrations/meta/0475_snapshot.json +++ b/packages/db/src/migrations/meta/0482_snapshot.json @@ -1,6 +1,6 @@ { - "id": "ee1f1f66-0cbe-4b18-b364-5e96708a1f0b", - "prevId": "a7108924-4d75-41e2-9139-f43f9396c02d", + "id": "8b353a38-62df-4834-be55-7e9ebefad5c5", + "prevId": "41eacc6a-6eab-4f01-965a-62aa0cd6778f", "version": "7", "dialect": "postgresql", "tables": { @@ -3954,6 +3954,28 @@ "method": "btree", "with": {} }, + "agent_wakeup_requests_company_run_idx": { + "name": "agent_wakeup_requests_company_run_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "run_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"agent_wakeup_requests\".\"run_id\" IS NOT NULL", + "concurrently": true, + "method": "btree", + "with": {} + }, "agent_wakeup_requests_review_path_recovery_idempotency_uq": { "name": "agent_wakeup_requests_review_path_recovery_idempotency_uq", "columns": [ @@ -18070,6 +18092,12 @@ "notNull": true, "default": "'company'" }, + "system_purpose": { + "name": "system_purpose", + "type": "text", + "primaryKey": false, + "notNull": false + }, "owner_user_id": { "name": "owner_user_id", "type": "text", @@ -35239,6 +35267,12 @@ "notNull": true, "default": 1 }, + "native_source_cursors": { + "name": "native_source_cursors", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, "native_phase": { "name": "native_phase", "type": "text", @@ -55925,6 +55959,153 @@ "checkConstraints": {}, "isRLSEnabled": false }, + "public.tailscale_connections": { + "name": "tailscale_connections", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "secret_id": { + "name": "secret_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "credential_version": { + "name": "credential_version", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "credential_hash": { + "name": "credential_hash", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_by_user_id": { + "name": "created_by_user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "oauth_state_hash": { + "name": "oauth_state_hash", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "oauth_expires_at": { + "name": "oauth_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tailscale_connections_company_idx": { + "name": "tailscale_connections_company_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tailscale_connections_oauth_state_idx": { + "name": "tailscale_connections_oauth_state_idx", + "columns": [ + { + "expression": "oauth_state_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "tailscale_connections_company_id_companies_id_fk": { + "name": "tailscale_connections_company_id_companies_id_fk", + "tableFrom": "tailscale_connections", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tailscale_connections_secret_id_company_secrets_id_fk": { + "name": "tailscale_connections_secret_id_company_secrets_id_fk", + "tableFrom": "tailscale_connections", + "tableTo": "company_secrets", + "columnsFrom": [ + "secret_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, "public.tool_access_audit_events": { "name": "tool_access_audit_events", "schema": "", @@ -57542,6 +57723,12 @@ "primaryKey": false, "notNull": false }, + "icons": { + "name": "icons", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, "risk_level": { "name": "risk_level", "type": "text", @@ -59763,6 +59950,18 @@ "primaryKey": false, "notNull": true }, + "model_context": { + "name": "model_context", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "model_context_updated_at": { + "name": "model_context_updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, "created_at": { "name": "created_at", "type": "timestamp with time zone", @@ -60956,6 +61155,392 @@ "checkConstraints": {}, "isRLSEnabled": false }, + "public.tool_mcp_tasks": { + "name": "tool_mcp_tasks", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "company_id": { + "name": "company_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "invocation_id": { + "name": "invocation_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "connection_id": { + "name": "connection_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "grant_id": { + "name": "grant_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "agent_id": { + "name": "agent_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "issue_id": { + "name": "issue_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "run_id": { + "name": "run_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "actor_type": { + "name": "actor_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "actor_id": { + "name": "actor_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "responsible_user_id": { + "name": "responsible_user_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "identity_context_id": { + "name": "identity_context_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "catalog_tool_name": { + "name": "catalog_tool_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "request_hash": { + "name": "request_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "endpoint_hash": { + "name": "endpoint_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "sealed_caller_headers": { + "name": "sealed_caller_headers", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "protocol_version": { + "name": "protocol_version", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "sealed_provider_task_id": { + "name": "sealed_provider_task_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider_task_id_hash": { + "name": "provider_task_id_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "poll_interval_ms": { + "name": "poll_interval_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "ttl_ms": { + "name": "ttl_ms", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "next_poll_at": { + "name": "next_poll_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_polled_at": { + "name": "last_polled_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "poll_failures": { + "name": "poll_failures", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "lease_expires_at": { + "name": "lease_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "lease_owner": { + "name": "lease_owner", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "arguments_identity": { + "name": "arguments_identity", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "answered_input_keys": { + "name": "answered_input_keys", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "surfaced_input_keys": { + "name": "surfaced_input_keys", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'[]'::jsonb" + }, + "input_wake_count": { + "name": "input_wake_count", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "input_wake_at": { + "name": "input_wake_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "error_code": { + "name": "error_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "wake_requested_at": { + "name": "wake_requested_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "delivered_at": { + "name": "delivered_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "last_invocation_id": { + "name": "last_invocation_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tool_mcp_tasks_provider_task_uq": { + "name": "tool_mcp_tasks_provider_task_uq", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "provider_task_id_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_attach_idx": { + "name": "tool_mcp_tasks_attach_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "request_hash", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_invocation_idx": { + "name": "tool_mcp_tasks_invocation_idx", + "columns": [ + { + "expression": "company_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "invocation_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "tool_mcp_tasks_next_poll_idx": { + "name": "tool_mcp_tasks_next_poll_idx", + "columns": [ + { + "expression": "next_poll_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "\"tool_mcp_tasks\".\"next_poll_at\" is not null", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "tool_mcp_tasks_company_id_companies_id_fk": { + "name": "tool_mcp_tasks_company_id_companies_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "companies", + "columnsFrom": [ + "company_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tool_mcp_tasks_invocation_id_tool_invocations_id_fk": { + "name": "tool_mcp_tasks_invocation_id_tool_invocations_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "tool_invocations", + "columnsFrom": [ + "invocation_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tool_mcp_tasks_connection_id_tool_connections_id_fk": { + "name": "tool_mcp_tasks_connection_id_tool_connections_id_fk", + "tableFrom": "tool_mcp_tasks", + "tableTo": "tool_connections", + "columnsFrom": [ + "connection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, "public.tool_oauth_states": { "name": "tool_oauth_states", "schema": "", diff --git a/packages/db/src/migrations/meta/_journal.json b/packages/db/src/migrations/meta/_journal.json index 29a110630c2..150d999e51d 100644 --- a/packages/db/src/migrations/meta/_journal.json +++ b/packages/db/src/migrations/meta/_journal.json @@ -3347,6 +3347,27 @@ "when": 1794589077537, "tag": "0479_short_darkstar", "breakpoints": true + }, + { + "idx": 480, + "version": "7", + "when": 1794604914931, + "tag": "0480_lazy_lethal_legion", + "breakpoints": true + }, + { + "idx": 481, + "version": "7", + "when": 1794604943265, + "tag": "0481_round_pet_avengers", + "breakpoints": true + }, + { + "idx": 482, + "version": "7", + "when": 1794604948355, + "tag": "0482_aberrant_rumiko_fujikawa", + "breakpoints": true } ] } \ No newline at end of file diff --git a/packages/db/src/schema/company_secrets.ts b/packages/db/src/schema/company_secrets.ts index c5792bd467b..b124143cfee 100644 --- a/packages/db/src/schema/company_secrets.ts +++ b/packages/db/src/schema/company_secrets.ts @@ -13,6 +13,7 @@ export const companySecrets = pgTable( id: uuid("id").primaryKey().defaultRandom(), companyId: uuid("company_id").notNull().references(() => companies.id), scope: text("scope").notNull().default("company"), + systemPurpose: text("system_purpose"), ownerUserId: text("owner_user_id"), ownerAgentId: uuid("owner_agent_id"), ownerProjectId: uuid("owner_project_id"), diff --git a/packages/db/src/schema/index.ts b/packages/db/src/schema/index.ts index d22f870e6f8..66e06586c9a 100644 --- a/packages/db/src/schema/index.ts +++ b/packages/db/src/schema/index.ts @@ -285,4 +285,5 @@ export { issueThreadInteractionIdempotencyKeys } from "./issue_thread_interactio export { projectTaskExecutionPolicies, executionAdmissions, executionAdmissionProjectCharges } from "./execution_admissions.js"; export { budgetEvaluationWork } from "./budget_evaluation_work.js"; +export { tailscaleConnections } from "./tailscale_connections.js"; export { nativeProviderArtifacts, nativeProviderArtifactReferences } from "./native_provider_artifacts.js"; diff --git a/packages/db/src/schema/tailscale_connections.ts b/packages/db/src/schema/tailscale_connections.ts new file mode 100644 index 00000000000..a071a3f201e --- /dev/null +++ b/packages/db/src/schema/tailscale_connections.ts @@ -0,0 +1,21 @@ +import { index, integer, pgTable, text, timestamp, uniqueIndex, uuid } from "drizzle-orm/pg-core"; +import { companies } from "./companies.js"; +import { companySecrets } from "./company_secrets.js"; + +export const tailscaleConnections = pgTable("tailscale_connections", { + id: uuid("id").primaryKey().defaultRandom(), + companyId: uuid("company_id").notNull().references(() => companies.id, { onDelete: "cascade" }), + name: text("name").notNull(), + status: text("status").notNull().default("pending"), + secretId: uuid("secret_id").references(() => companySecrets.id), + credentialVersion: integer("credential_version"), + credentialHash: text("credential_hash"), + createdByUserId: text("created_by_user_id").notNull(), + oauthStateHash: text("oauth_state_hash"), + oauthExpiresAt: timestamp("oauth_expires_at", { withTimezone: true }), + createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), + updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(), +}, table => ({ + companyIdx: index("tailscale_connections_company_idx").on(table.companyId), + stateIdx: uniqueIndex("tailscale_connections_oauth_state_idx").on(table.oauthStateHash), +})); diff --git a/packages/plugins/sandbox-providers/kubernetes/test/unit/file-sync.test.ts b/packages/plugins/sandbox-providers/kubernetes/test/unit/file-sync.test.ts index c0bdfa2aeb0..647c6d7b15c 100644 --- a/packages/plugins/sandbox-providers/kubernetes/test/unit/file-sync.test.ts +++ b/packages/plugins/sandbox-providers/kubernetes/test/unit/file-sync.test.ts @@ -4,6 +4,7 @@ import { promises as fs } from "node:fs"; import os from "node:os"; import path from "node:path"; import { spawn } from "node:child_process"; +import { PassThrough } from "node:stream"; import { assertConfinedSandboxPath, parseTarVerboseListingLine, @@ -39,6 +40,7 @@ function makeRealExec(): { exec: PodStreamExec; calls: RecordedCall[] } { return await new Promise((resolve, reject) => { const child = spawn(command[0], command.slice(1)); let err = ""; + let stdinError: NodeJS.ErrnoException | undefined; child.stderr.on("data", (chunk: Buffer) => { err += chunk.toString("utf-8"); }); @@ -49,6 +51,15 @@ function makeRealExec(): { exec: PodStreamExec; calls: RecordedCall[] } { } else { child.stdout.resume(); } + child.stdin.on("error", (error: NodeJS.ErrnoException) => { + if (error.code !== "EPIPE") { + reject(error); + return; + } + stdinError = error; + io.stdin?.unpipe(child.stdin); + io.stdin?.destroy(); + }); if (io.stdin) { io.stdin.on("error", reject); io.stdin.pipe(child.stdin); @@ -56,6 +67,10 @@ function makeRealExec(): { exec: PodStreamExec; calls: RecordedCall[] } { child.stdin.end(); } child.on("close", (code) => { + if (code === 0 && stdinError) { + reject(stdinError); + return; + } resolve({ exitCode: code ?? 0, stderr: err }); }); }); @@ -96,6 +111,38 @@ afterEach(async () => { await Promise.all(tmpDirs.splice(0).map((dir) => fs.rm(dir, { recursive: true, force: true }))); }); +describe("shell exec stream lifecycle", () => { + it.each([0, 42])("handles an early stdin close while preserving exit status %i", async (exitCode) => { + const stdin = new PassThrough(); + const stdout = new PassThrough(); + stdout.once("data", () => stdin.end(Buffer.alloc(512 * 1024))); + const { exec } = makeRealExec(); + try { + const result = exec([ + process.execPath, + "-e", + ` + require("node:fs").closeSync(0); + process.stdout.write("stdin closed\\n"); + setTimeout(() => { + process.stderr.write("early rejection"); + process.exit(${exitCode}); + }, 100); + `, + ], { stdin, stdout }); + if (exitCode === 0) { + await expect(result).rejects.toMatchObject({ code: "EPIPE" }); + } else { + await expect(result).resolves.toEqual({ exitCode, stderr: "early rejection" }); + } + expect(stdin.destroyed).toBe(true); + } finally { + stdin.destroy(); + stdout.destroy(); + } + }); +}); + describe("kubernetes file-sync path confinement", () => { it("rejects a target path that escapes the workspace remote dir", () => { expect(() => assertConfinedSandboxPath("/workspace", "/workspace/../etc/passwd", "target")).toThrow( diff --git a/packages/shared/src/types/environment.ts b/packages/shared/src/types/environment.ts index 16b9d9598ab..62e62afcb3d 100644 --- a/packages/shared/src/types/environment.ts +++ b/packages/shared/src/types/environment.ts @@ -13,6 +13,13 @@ export interface LocalEnvironmentConfig { export interface SshEnvironmentConfig { useHostAuth?: boolean; + tailscaleConnectionId?: string; + tailscaleDeviceId?: string; + network?: "direct" | "tailscale"; + tailscaleAuthKey?: null; + tailscaleAuthKeySecretRef?: EnvSecretRefBinding | null; + tailscaleSocket?: string; + tailscaleIdentity?: string; host: string; port: number; username: string; diff --git a/scripts/docker-build-test.sh b/scripts/docker-build-test.sh index be1ffa57a6c..692ea63b21b 100755 --- a/scripts/docker-build-test.sh +++ b/scripts/docker-build-test.sh @@ -36,6 +36,8 @@ echo "==> Verifying key binaries in image" set -e node --version git --version + tailscale version + tailscaled --version gh --version rg --version python3 --version diff --git a/server/src/__tests__/agent-secrets-routes.test.ts b/server/src/__tests__/agent-secrets-routes.test.ts index 6e0e94dfeb6..a8dc1af0cf4 100644 --- a/server/src/__tests__/agent-secrets-routes.test.ts +++ b/server/src/__tests__/agent-secrets-routes.test.ts @@ -119,6 +119,47 @@ describeEmbeddedPostgres("agent secret routes", () => { return app; } + it("rejects an unclassified legacy node-state secret even through an existing agent binding", async () => { + const fixture = await seedAgentRun(); + const svc = secretService(db); + const value = "paperclip-tailscale-state-v1:fixture-legacy-node-state"; + const legacy = await svc.create(fixture.companyId, { name: "Legacy network", key: "LEGACY_NETWORK", provider: "local_encrypted", value }); + await svc.createBinding({ companyId: fixture.companyId, secretId: legacy.id, + targetType: "agent", targetId: fixture.agentId, configPath: "env.ANTHROPIC_AUTH_TOKEN" }); + const response = await request(createApp(fixture)).post("/api/agents/me/secrets/legacy_network/value"); + expect(response.status).toBe(403); + expect(JSON.stringify(response.body)).not.toContain(value); + }); + + it("never returns server-owned network state through generic catalog, binding, runtime, or agent API reads", async () => { + const fixture = await seedAgentRun(); + const svc = secretService(db); + const value = "paperclip-tailscale-state-v1:fixture-network-state"; + const secret = await svc.createSystemSecret(fixture.companyId, { + key: "NETWORK_STATE", name: "Network fixture", provider: "local_encrypted", value, + }, "tailscale_network"); + const app = createApp(fixture); + const catalog = await request(app).get(`/api/companies/${fixture.companyId}/secrets/catalog`); + expect(catalog.status).toBe(200); + expect(catalog.body).not.toEqual(expect.arrayContaining([expect.objectContaining({ id: secret.id })])); + await expect(svc.createBinding({ companyId: fixture.companyId, secretId: secret.id, + targetType: "agent", targetId: fixture.agentId, configPath: "env.ANTHROPIC_AUTH_TOKEN" })).rejects.toMatchObject({ status: 403 }); + await expect(svc.resolveSecretValue(fixture.companyId, secret.id, "latest")).rejects.toMatchObject({ status: 403 }); + await expect(svc.resolveAdapterConfigForRuntime(fixture.companyId, { + env: { ANTHROPIC_AUTH_TOKEN: { type: "secret_ref", secretId: secret.id, version: "latest" } }, + })).rejects.toMatchObject({ status: 403 }); + await db.insert(companySecretBindings).values({ companyId: fixture.companyId, secretId: secret.id, + targetType: "agent", targetId: fixture.agentId, configPath: "env.ANTHROPIC_AUTH_TOKEN" }); + const response = await request(app).post("/api/agents/me/secrets/network_state/value"); + expect(response.status).toBe(403); + expect(JSON.stringify(response.body)).not.toContain(value); + await svc.update(secret.id, { name: "Renamed", providerMetadata: {} }); + await svc.rotate(secret.id, { value: "fixture-rotated-network-state" }); + expect((await svc.getById(secret.id))?.systemPurpose).toBe("tailscale_network"); + await expect(svc.resolveSecretValue(fixture.companyId, secret.id, 1)).rejects.toMatchObject({ status: 403 }); + expect(await svc.resolveSystemSecretValue(fixture.companyId, secret.id, "latest", "tailscale_network", "fixture-network")).toBe("fixture-rotated-network-state"); + }); + it("lists metadata only, reads env and access grants, and audits success and failure", async () => { const fixture = await seedAgentRun(); const svc = secretService(db); diff --git a/server/src/__tests__/docker-build-stamp.test.ts b/server/src/__tests__/docker-build-stamp.test.ts index 656ef563837..2f624dbd28b 100644 --- a/server/src/__tests__/docker-build-stamp.test.ts +++ b/server/src/__tests__/docker-build-stamp.test.ts @@ -163,7 +163,7 @@ describe("Application image Runner", () => { it("compiles the Runner only in the runner-artifacts stages", () => { const names = [...dockerfile.matchAll(/^FROM \S+ AS (\S+)$/gm)].map((match) => match[1]); const application = names.filter((name) => !name.startsWith("runner-artifacts")); - expect(application).toEqual(["base", "runner-lockfile", "deps", "build", "runner-provider-pack", "production", "cloud-plugins", "cloud-server-deps", "cloud"]); + expect(application).toEqual(["base", "tailscale-client", "runner-lockfile", "deps", "build", "runner-provider-pack", "production", "cloud-plugins", "cloud-server-deps", "cloud"]); for (const name of application) { expect(stageBody(dockerfile, name), name).not.toMatch(/\b(?:cargo|rustup|rustc|gcc)\b/); } diff --git a/server/src/__tests__/environment-config.test.ts b/server/src/__tests__/environment-config.test.ts index d3573c466cd..bedd936c164 100644 --- a/server/src/__tests__/environment-config.test.ts +++ b/server/src/__tests__/environment-config.test.ts @@ -3,6 +3,17 @@ import { HttpError } from "../errors.js"; import { normalizeEnvironmentConfig, parseEnvironmentDriverConfig } from "../services/environment-config.ts"; describe("environment config helpers", () => { + it("uses a connected tailnet within the SSH driver and requires a complete machine selection", () => { + const config = { host: "100.64.0.1", username: "developer", remoteWorkspacePath: "/Users/developer/Paperclip", network: "tailscale", + tailscaleConnectionId: "11111111-1111-4111-8111-111111111111", tailscaleDeviceId: "stable-device-id" }; + const normalized = normalizeEnvironmentConfig({ driver: "ssh", config }); + expect(normalized).toMatchObject(config); + expect(parseEnvironmentDriverConfig({ driver: "ssh", config: normalized })).toMatchObject({ driver: "ssh", config }); + expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, tailscaleDeviceId: "" } })).toThrow(HttpError); + expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, tailscaleConnectionId: undefined } })).toThrow(HttpError); + expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, network: "direct" } })).toThrow(HttpError); + expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, tailscaleAuthKey: "raw-token" } })).toThrow(HttpError); + }); it.each([undefined, true, false])("preserves SSH authentication compatibility when useHostAuth is %s", useHostAuth => { const config = normalizeEnvironmentConfig({ driver: "ssh", config: { host: "ssh.example.test", username: "fixture", remoteWorkspacePath: "/workspace", @@ -13,7 +24,6 @@ describe("environment config helpers", () => { expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, useHostAuth: "false" } })).toThrow(HttpError); }); - it("normalizes SSH config into its canonical stored shape", () => { const config = normalizeEnvironmentConfig({ driver: "ssh", @@ -47,6 +57,17 @@ describe("environment config helpers", () => { }); }); + it("accepts a secret-backed Tailscale machine and rejects runtime socket injection", () => { + const config = { host: "workstation", username: "developer", remoteWorkspacePath: "/workspace", network: "tailscale", + tailscaleConnectionId: "22222222-2222-4222-8222-222222222222", tailscaleDeviceId: "fixture-device", + tailscaleAuthKeySecretRef: { type: "secret_ref", secretId: "11111111-1111-1111-1111-111111111111", version: "latest" } }; + expect(normalizeEnvironmentConfig({ driver: "ssh", config })).toMatchObject(config); + expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, tailscaleConnectionId: undefined, tailscaleDeviceId: undefined } })).toThrow(HttpError); + expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, tailscaleAuthKey: "tskey-auth-fixture" } })).toThrow(HttpError); + expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, tailscaleSocket: "/tmp/other-company" } })).toThrow(HttpError); + expect(() => normalizeEnvironmentConfig({ driver: "ssh", config: { ...config, tailscaleAuthKeySecretRef: { ...config.tailscaleAuthKeySecretRef, version: 1 } } })).toThrow(HttpError); + }); + it("rejects raw SSH private keys in the stored config shape", () => { expect(() => normalizeEnvironmentConfig({ diff --git a/server/src/__tests__/environment-routes.test.ts b/server/src/__tests__/environment-routes.test.ts index 832e4f90777..bc7011ce244 100644 --- a/server/src/__tests__/environment-routes.test.ts +++ b/server/src/__tests__/environment-routes.test.ts @@ -2151,6 +2151,25 @@ describe("environment routes", () => { expect(mockEnvironmentService.create).not.toHaveBeenCalled(); }); + it("rejects standalone Tailscale enrollment keys before storing an environment secret", async () => { + const res = await request(createApp(localBoardActor())).post("/api/companies/company-1/environments").send({ + name: "My workstation", driver: "ssh", config: { network: "tailscale", host: "workstation", username: "developer", + remoteWorkspacePath: "/workspace", tailscaleAuthKey: "tskey-auth-fixture" }, + }); + expect(res.status).toBe(422); + expect(mockSecretService.create).not.toHaveBeenCalled(); + expect(mockEnvironmentService.create).not.toHaveBeenCalled(); + expect(JSON.stringify(res.body)).not.toContain("tskey-auth-fixture"); + }); + + it("rejects Tailscale environment setup from a non-admin before storing credentials", async () => { + const res = await request(createApp({ type: "board", userId: "member", source: "session", companyIds: ["company-1"] })) + .post("/api/companies/company-1/environments").send({ name: "My workstation", driver: "ssh", config: { + network: "tailscale", host: "workstation", username: "developer", remoteWorkspacePath: "/workspace", tailscaleAuthKey: "tskey-auth-fixture" } }); + expect(res.status).toBe(403); + expect(mockSecretService.create).not.toHaveBeenCalled(); + }); + it("normalizes SSH private keys into secret refs before persistence", async () => { const environment = createSecretBackedSshEnvironment(); mockEnvironmentService.create.mockResolvedValue(environment); diff --git a/server/src/__tests__/issue-reassignment-queued-run.test.ts b/server/src/__tests__/issue-reassignment-queued-run.test.ts index 4a3b3e4bf20..21c42fc43f3 100644 --- a/server/src/__tests__/issue-reassignment-queued-run.test.ts +++ b/server/src/__tests__/issue-reassignment-queued-run.test.ts @@ -2,7 +2,7 @@ import { randomUUID } from "node:crypto"; import express from "express"; import request from "supertest"; import { and, eq, sql } from "drizzle-orm"; -import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest"; +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; import { agentWakeupRequests, agents, @@ -31,16 +31,13 @@ if (!embeddedPostgresSupport.supported) { describeEmbeddedPostgres("reassigning a task whose previous assignee's run has not started", () => { let db!: ReturnType; let tempDb: Awaited> | null = null; + let companySequence = 0; beforeAll(async () => { tempDb = await startEmbeddedPostgresTestDatabase("paperclip-reassign-queued-"); db = createDb(tempDb.connectionString); }, 30_000); - afterEach(async () => { - await db.execute(sql`TRUNCATE TABLE companies CASCADE`); - }); - afterAll(async () => { await db?.$client.end(); await tempDb?.cleanup(); @@ -71,8 +68,9 @@ describeEmbeddedPostgres("reassigning a task whose previous assignee's run has n const morganId = randomUUID(); const issueId = randomUUID(); const queuedRunId = randomUUID(); + const issuePrefix = `REA${++companySequence}`; await db.insert(companies).values({ - id: companyId, name: "Reassign Co", issuePrefix: "REA", requireBoardApprovalForNewAgents: false, + id: companyId, name: "Reassign Co", issuePrefix, requireBoardApprovalForNewAgents: false, }); await db.insert(agents).values([ceoId, morganId].map((id, index) => ({ id, companyId, name: index === 0 ? "CEO" : "Morgan", role: index === 0 ? "ceo" : "engineer", @@ -90,7 +88,7 @@ describeEmbeddedPostgres("reassigning a task whose previous assignee's run has n status: "queued", contextSnapshot: { issueId, wakeReason: "issue_assigned" }, }); await db.insert(issues).values({ - id: issueId, companyId, identifier: "REA-1", title: "Write the announcement", status: "todo", + id: issueId, companyId, identifier: `${issuePrefix}-1`, title: "Write the announcement", status: "todo", priority: "medium", assigneeAgentId: ceoId, executionRunId: options.queuedRunHoldsLock === false ? null : queuedRunId, }); return { companyId, ceoId, morganId, issueId, queuedRunId }; diff --git a/server/src/__tests__/openapi-routes.test.ts b/server/src/__tests__/openapi-routes.test.ts index 6e783970aa6..1241419c306 100644 --- a/server/src/__tests__/openapi-routes.test.ts +++ b/server/src/__tests__/openapi-routes.test.ts @@ -98,6 +98,7 @@ const apiPrefixes: Record = { "sidebar-preferences.ts": "/api", "summary-slots.ts": "/api", "status-cards.ts": "/api", + "tailscale-connections.ts": "/api", "teams-catalog.ts": "/api", "tool-access.ts": "/api", "voice-sessions.ts": "/api", @@ -1250,6 +1251,36 @@ describe("openapi routes", () => { }); }); + it("documents Tailscale management as instance-admin operations without credential responses", () => { + const { spec } = loadSpecRoutes(); + const base = "/api/companies/{companyId}/tailscale-connections"; + for (const [path, method] of [[base, "get"], [base, "post"], [base + "/{connectionId}", "delete"], + [base + "/{connectionId}/devices", "get"], [base + "/{connectionId}/pairing", "post"], ["/api/tools/oauth/tailscale/callback", "get"]]) { + const operation = spec.paths[path]?.[method]; + expect(operation).toBeDefined(); + expect(operation["x-paperclip-authorization"]).toEqual({ actor: "board", instanceAdmin: true }); + expect(operation.security).toEqual([{ BoardSessionAuth: [] }, { BoardApiKeyAuth: [] }]); + expect(operation.responses["403"]).toBeDefined(); + } + const connection = spec.paths[base].get.responses["200"].content["application/json"].schema.items; + expect(Object.keys(connection.properties).sort()).toEqual(["companyId", "id", "name", "status"]); + expect(connection.additionalProperties).toBe(false); + const start = spec.paths[base].post; + expect(start.requestBody.content["application/json"].schema).toMatchObject({ additionalProperties: false, + properties: { name: { minLength: 1, maxLength: 100 }, connectionId: { format: "uuid" } } }); + expect(start.requestBody.content["application/json"].schema.required ?? []).toEqual([]); + const session = start.responses["201"].content["application/json"].schema; + expect(session.required).toEqual(["authorizationUrl", "expiresAt"]); + expect(session.properties.handoff.properties.kind).toEqual({ type: "string", enum: ["paperclip_cloud"] }); + const pairing = spec.paths[base + "/{connectionId}/pairing"].post; + expect(pairing.requestBody.content["application/json"].schema).toMatchObject({ additionalProperties: false, required: ["deviceId"], + properties: { deviceId: { minLength: 1, maxLength: 256 }, port: { minimum: 1, maximum: 65535 } } }); + expect(Object.keys(pairing.responses["200"].content["application/json"].schema.properties).sort()).toEqual(["command", "privateKeySecretId", "publicKey"]); + const callback = spec.paths["/api/tools/oauth/tailscale/callback"].get; + expect(callback.responses["303"].headers).toMatchObject({ Location: { schema: { type: "string" } }, "Cache-Control": { schema: { enum: ["no-store"] } } }); + expect(callback.parameters).toContainEqual(expect.objectContaining({ in: "query", name: "state", required: true, schema: expect.objectContaining({ maxLength: 128 }) })); + }); + it("covers the mounted server routes exactly", () => { const { routes: actualRoutes, diff --git a/server/src/__tests__/ssh-tailscale-connection.test.ts b/server/src/__tests__/ssh-tailscale-connection.test.ts new file mode 100644 index 00000000000..fee17637192 --- /dev/null +++ b/server/src/__tests__/ssh-tailscale-connection.test.ts @@ -0,0 +1,119 @@ +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { normalizeEnvironmentConfigForPersistence, resolveEnvironmentDriverConfigForRuntime } from "../services/environment-config.js"; +import { unprocessable } from "../errors.js"; + +const fixture = vi.hoisted(() => ({ + get: vi.fn(), binding: vi.fn(), connect: vi.fn(), + secrets: { resolveSecretValue: vi.fn(), resolveSecretVersion: vi.fn(), rotate: vi.fn() }, +})); +vi.mock("../services/connectors/tailscale/connections.js", () => ({ tailscaleConnectionService: () => ({ get: fixture.get, environmentBinding: fixture.binding }) })); +vi.mock("../services/connectors/tailscale/network.js", async importOriginal => ({ + ...await importOriginal(), + tailscaleNetworks: { connect: fixture.connect }, +})); +vi.mock("../services/secrets.js", () => ({ secretService: () => fixture.secrets })); + +const companyId = "11111111-1111-4111-8111-111111111111"; +const connectionId = "22222222-2222-4222-8222-222222222222"; +const secretId = "33333333-3333-4333-8333-333333333333"; +const config = { + network: "tailscale", tailscaleConnectionId: connectionId, tailscaleDeviceId: "stable-machine-id", + host: "100.64.0.1", port: 22, username: "developer", remoteWorkspacePath: "/workspace", + tailscaleAuthKeySecretRef: { type: "secret_ref", secretId, version: "latest" }, +}; +const environment = { id: "44444444-4444-4444-8444-444444444444", driver: "ssh" as const, config }; + +describe("SSH environments using the Tailscale connector", () => { + beforeEach(() => { + vi.resetAllMocks(); + fixture.get.mockResolvedValue({ id: connectionId, companyId, secretId, status: "connected" }); + fixture.binding.mockResolvedValue({ host: "100.64.0.2", secretId, socket: "/tmp/private-tailscale/socket" }); + fixture.connect.mockResolvedValue("/tmp/private-tailscale/socket"); + fixture.secrets.resolveSecretValue.mockResolvedValue("fixture-encrypted-node-state"); + fixture.secrets.resolveSecretVersion.mockResolvedValue(2); + }); + + it("uses the connector's company-scoped address and secret instead of client-supplied values", async () => { + const stored = await normalizeEnvironmentConfigForPersistence({ db: {} as never, companyId, environmentName: "Workstation", driver: "ssh", secretProvider: "local_encrypted", + config: { ...config, host: "untrusted.invalid", tailscaleAuthKeySecretRef: { ...config.tailscaleAuthKeySecretRef, secretId: "55555555-5555-4555-8555-555555555555" } } }); + expect(fixture.binding).toHaveBeenCalledWith(companyId, connectionId, "stable-machine-id"); + expect(stored).toMatchObject({ host: "100.64.0.2", tailscaleAuthKeySecretRef: { secretId } }); + }); + + it("refreshes the selected machine address at execution and uses only the connector's private socket", async () => { + const resolved = await resolveEnvironmentDriverConfigForRuntime({} as never, companyId, environment); + expect(fixture.get).toHaveBeenCalledWith(companyId, connectionId); + expect(fixture.binding).toHaveBeenCalledWith(companyId, connectionId, "stable-machine-id", secretId); + expect(resolved).toMatchObject({ driver: "ssh", config: { host: "100.64.0.2", tailscaleSocket: "/tmp/private-tailscale/socket", tailscaleIdentity: `${companyId}:${secretId}` } }); + expect(fixture.connect).not.toHaveBeenCalled(); + expect(fixture.secrets.resolveSecretValue).not.toHaveBeenCalled(); + }); + + it.each(["disconnected", "replaced"])("rejects a connector %s while the SSH key is loading", async state => { + let entered!: () => void, release!: () => void; + const loading = new Promise(resolve => { entered = resolve; }); + const paused = new Promise(resolve => { release = resolve; }); + fixture.secrets.resolveSecretValue.mockImplementationOnce(async () => { entered(); await paused; return "fixture-ssh-key"; }); + const resolving = resolveEnvironmentDriverConfigForRuntime({} as never, companyId, { ...environment, config: { ...config, + privateKeySecretRef: { type: "secret_ref", secretId: "55555555-5555-4555-8555-555555555555", version: "latest" }, + } }); + const outcome = resolving.then(value => ({ value, error: null }), error => ({ value: null, error })); + try { + await Promise.race([loading, resolving.then(() => { throw new Error("Runtime resolved before loading the SSH key"); })]); + fixture.get.mockResolvedValue({ id: connectionId, companyId, secretId: state === "replaced" ? "new-secret" : secretId, status: state === "replaced" ? "connected" : state }); + fixture.binding.mockRejectedValue(unprocessable("Reconnect Tailscale and save this environment again.", { code: "tailscale_reauthorization_required" })); + } finally { release(); } + expect((await outcome).error).toMatchObject({ status: 422, details: { code: "tailscale_reauthorization_required" } }); + expect(fixture.connect).not.toHaveBeenCalled(); + expect(fixture.binding).toHaveBeenCalledWith(companyId, connectionId, "stable-machine-id", secretId); + expect(fixture.binding.mock.invocationCallOrder[0]).toBeGreaterThan(fixture.secrets.resolveSecretValue.mock.invocationCallOrder[0]!); + }); + + it.each(["pending", "connecting", "disconnected"])("rejects a %s connector before reading any runtime credentials", async status => { + fixture.get.mockResolvedValue({ id: connectionId, companyId, secretId, status }); + await expect(resolveEnvironmentDriverConfigForRuntime({} as never, companyId, environment)).rejects.toMatchObject({ status: 422 }); + expect(fixture.secrets.resolveSecretValue).not.toHaveBeenCalled(); + expect(fixture.connect).not.toHaveBeenCalled(); + }); + + it("rejects a saved network credential without its connector instead of starting a standalone daemon", async () => { + const configWithoutConnection = { ...config, tailscaleConnectionId: undefined, tailscaleDeviceId: undefined }; + await expect(resolveEnvironmentDriverConfigForRuntime({} as never, companyId, { ...environment, config: configWithoutConnection })).rejects.toMatchObject({ status: 422 }); + await expect(normalizeEnvironmentConfigForPersistence({ db: {} as never, companyId, environmentName: "Workstation", driver: "ssh", + secretProvider: "local_encrypted", config: configWithoutConnection })).rejects.toMatchObject({ status: 422 }); + expect(fixture.get).not.toHaveBeenCalled(); + expect(fixture.secrets.resolveSecretValue).not.toHaveBeenCalled(); + expect(fixture.connect).not.toHaveBeenCalled(); + }); + + it("clears a saved tailnet when Direct is explicitly merged into an SSH environment", async () => { + const stored = await normalizeEnvironmentConfigForPersistence({ db: {} as never, companyId, environmentName: "Workstation", driver: "ssh", + secretProvider: "local_encrypted", config: { ...config, network: "direct", host: "direct.example.test" } }); + expect(stored).toMatchObject({ network: "direct", host: "direct.example.test" }); + for (const key of ["tailscaleConnectionId", "tailscaleDeviceId", "tailscaleAuthKeySecretRef", "tailscaleAuthKey"]) { + expect(stored).not.toHaveProperty(key); + } + expect(fixture.binding).not.toHaveBeenCalled(); + expect(fixture.connect).not.toHaveBeenCalled(); + }); + + it("requires company and environment context before opening a runtime binding", async () => { + await expect(resolveEnvironmentDriverConfigForRuntime({} as never, null, environment)).rejects.toMatchObject({ status: 422 }); + await expect(resolveEnvironmentDriverConfigForRuntime({} as never, companyId, { ...environment, id: undefined })).rejects.toMatchObject({ status: 422 }); + expect(fixture.binding).not.toHaveBeenCalled(); + expect(fixture.connect).not.toHaveBeenCalled(); + }); + + it("never falls back to a caller's host or the system tailnet when company access or device lookup fails", async () => { + fixture.binding.mockRejectedValue(new Error("machine unavailable in this company")); + await expect(resolveEnvironmentDriverConfigForRuntime({} as never, companyId, environment)).rejects.toThrow("machine unavailable"); + expect(fixture.connect).not.toHaveBeenCalled(); + }); + + it("keeps read-only inspection free of network and daemon activity", async () => { + const resolved = await resolveEnvironmentDriverConfigForRuntime({} as never, companyId, environment, { readOnlyInspection: true }); + expect(resolved.driver).toBe("ssh"); + expect(fixture.binding).not.toHaveBeenCalled(); + expect(fixture.connect).not.toHaveBeenCalled(); + }); +}); diff --git a/server/src/__tests__/tailscale-connection-routes.test.ts b/server/src/__tests__/tailscale-connection-routes.test.ts new file mode 100644 index 00000000000..9da255c2645 --- /dev/null +++ b/server/src/__tests__/tailscale-connection-routes.test.ts @@ -0,0 +1,167 @@ +import express from "express"; +import request from "supertest"; +import { beforeEach, describe, expect, it, vi } from "vitest"; +import { tailscaleConnectionRoutes } from "../routes/tailscale-connections.js"; +import { boardMutationGuard } from "../middleware/board-mutation-guard.js"; +import { errorHandler } from "../middleware/error-handler.js"; +import { badRequest } from "../errors.js"; + +const fixture = vi.hoisted(() => ({ + service: { list: vi.fn(), start: vi.fn(), devices: vi.fn(), complete: vi.fn(), disconnect: vi.fn(), pairing: vi.fn() }, + configured: vi.fn(), enrollment: vi.fn(), activity: vi.fn(), +})); +vi.mock("../services/connectors/tailscale/connections.js", () => ({ tailscaleConnectionService: () => fixture.service })); +vi.mock("../services/paperclip-cloud-connector.js", () => ({ paperclipCloudConnectorConfigFromEnv: fixture.configured })); +vi.mock("../services/paperclip-cloud-connector-enrollment.js", () => ({ startPaperclipCloudConnectorEnrollment: fixture.enrollment })); +vi.mock("../services/activity-log.js", () => ({ logActivity: fixture.activity })); +vi.mock("../config.js", () => ({ loadConfig: () => ({ authPublicBaseUrl: "http://localhost:3100" }) })); + +const companyId = "11111111-1111-4111-8111-111111111111"; +const connectionId = "22222222-2222-4222-8222-222222222222"; +const path = `/api/companies/${companyId}/tailscale-connections`; +const board = { type: "board", source: "session", userId: "fixture-user", isInstanceAdmin: true, companyIds: [companyId] }; +function app(actor = board) { + const result = express(); + result.use(express.json()); + result.use((req, _res, next) => { (req as any).actor = actor; next(); }); + result.use(boardMutationGuard()); + result.use("/api", tailscaleConnectionRoutes({ select: () => ({ from: () => ({ where: async () => [{ prefix: "FIX" }] }) }) } as never)); + result.use(errorHandler); + return result; +} + +describe("Tailscale connector routes", () => { + beforeEach(() => { + vi.clearAllMocks(); + fixture.configured.mockReturnValue({}); + fixture.service.list.mockResolvedValue([{ id: connectionId, name: "Home", status: "connected", companyId }]); + fixture.service.start.mockResolvedValue({ connection: { id: connectionId }, authorizationUrl: "https://login.tailscale.com/a/oauth_authorize" }); + fixture.service.devices.mockResolvedValue([{ id: "stable-device", name: "Home Mac", online: true }]); + fixture.service.complete.mockImplementation(async input => { input.authorizeCompany(companyId, connectionId); return { id: connectionId, companyId }; }); + fixture.service.disconnect.mockResolvedValue({ id: connectionId, companyId, status: "disconnected" }); + fixture.service.pairing.mockResolvedValue({ privateKeySecretId: "fixture-key-ref", publicKey: "fixture-public-key", command: "fixture-public-pairing-command" }); + }); + it("starts consent with the signed-in user, company, and fixed connector callback", async () => { + const response = await request(app()).post(path).set("Origin", "http://localhost:3100").send({ name: "Home" }); + expect(response.status).toBe(201); + expect(fixture.service.start).toHaveBeenCalledWith({ companyId, userId: "fixture-user", name: "Home", returnUri: "http://localhost:3100/api/tools/oauth/tailscale/callback" }); + expect(fixture.activity).toHaveBeenCalledWith(expect.anything(), expect.objectContaining({ action: "tailscale.authorization_started", companyId })); + }); + it("rejects foreign-origin browser mutations before starting consent", async () => { + const response = await request(app()).post(path).set("Origin", "https://untrusted.example.test").send({ name: "Home" }); + expect(response.status).toBe(403); + expect(fixture.service.start).not.toHaveBeenCalled(); + }); + it("rejects agent and non-admin management", async () => { + for (const actor of [{ type: "agent", source: "agent_key", agentId: "fixture-agent", companyId }, { ...board, isInstanceAdmin: false }]) { + expect((await request(app(actor as any)).get(path)).status).toBe(403); + } + expect(fixture.service.list).not.toHaveBeenCalled(); + }); + it("checks company access before listing machines or mutating a connection", async () => { + const denied = app({ ...board, companyIds: [] }); + expect((await request(denied).get(path + `/${connectionId}/devices`)).status).toBe(403); + expect((await request(denied).delete(path + `/${connectionId}`).set("Origin", "http://localhost:3100")).status).toBe(403); + expect(fixture.service.devices).not.toHaveBeenCalled(); + expect(fixture.service.disconnect).not.toHaveBeenCalled(); + }); + it("returns machines only through the selected company connection", async () => { + expect((await request(app()).get(path + `/${connectionId}/devices`)).status).toBe(200); + expect(fixture.service.devices).toHaveBeenCalledWith(companyId, connectionId); + }); + it("returns consent to the connector with only the connection identifier", async () => { + const response = await request(app()).get("/api/tools/oauth/tailscale/callback").query({ state: "s".repeat(43), claim_id: "opaque-claim" }); + expect(response.status).toBe(303); + expect(response.headers.location).toBe(`/FIX/apps/tailscale?tailscale_connection=${connectionId}`); + expect(response.headers["cache-control"]).toBe("no-store"); + }); + it("rechecks current company membership on the callback", async () => { + expect((await request(app({ ...board, companyIds: [] })).get("/api/tools/oauth/tailscale/callback").query({ state: "s".repeat(43), claim_id: "opaque-claim" })).status).toBe(403); + expect(fixture.activity).not.toHaveBeenCalled(); + }); + it.each(["access_denied", "authorization_denied", "untrusted-provider-error