Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

t3-connect.md

127 lines7.5 KB

1# T3 Connect
2​
3T3 Connect uses Clerk for cloud identity. The relay manages environment links,
4credentials for reaching environments, and managed tunnel allocations. After
5bootstrap, clients send application traffic through the environment's tunnel
6hostname; the relay Worker does not proxy their HTTP or WebSocket sessions.
7​
8Clerk, deployment, and native authentication setup live in the
9[Connect setup runbook](../operations/connect-setup.md).
10​
11## The relay is a trusted broker
12​
13An authenticated cloud user still needs an active environment link. The relay
14asks that environment to mint a one-time bootstrap credential bound to the
15client's DPoP key. The client exchanges it directly with the environment for an
16[environment session](./environment-auth.md). The relay never receives that
17session token, and possessing the bootstrap credential alone does not permit
18redeeming it without the client's private key.
19​
20Both sides authenticate this exchange. The environment accepts only bounded,
21replay-guarded relay proofs for its own identity, linked user, and requested
22operation. Signed environment responses bind the result to the request nonce;
23mint responses also bind the credential to the client proof key. The relay
24verifies those bindings before returning a credential. This prevents a different
25process behind the tunnel from impersonating the linked environment. The checks
26meet in the
27[environment cloud handlers](../../apps/server/src/cloud/http.ts) and
28[relay connector](../../infra/relay/src/environments/EnvironmentConnector.ts).
29​
30The relay holds the signing authority for mint requests. DPoP protects an honest
31exchange from credential reuse; it does not make a compromised relay signing
32key harmless. Keep that trust assumption explicit when changing the protocol.
33​
34Managed tunnels expose only a validated loopback HTTP origin. Link proof checks
35reject forwarded authority headers, and the relay resolves endpoints from its
36own managed allocations rather than a caller-supplied URL. Health and mint
37requests must not follow redirects. These restrictions keep endpoint discovery
38from turning into arbitrary relay egress or exposing another service on the
39environment host.
40​
41## A link outlives a connector process
42​
43CLI authorization, desired exposure, and a running connector have different
44lifetimes. Linking can record intent while the server is stopped. Startup
45reconciles that intent. CLI logout removes the stored cloud credential and
46disables exposure without uninstalling the environment's background service.
47​
48Managed allocations belong to a user/environment pair. Provisioning checkpoints
49external tunnel and DNS resources so retries can reconcile partial work. A
50normal shutdown of a CLI-managed link releases its tunnel to avoid paying for
51an idle resource, retaining the hostname reservation for the next startup.
52It also retains the allocation record so the environment remains "offline"
53rather than becoming "not authorized".
54​
55Two cases must retain the tunnel across shutdown. A link installed through a
56client has no startup provisioning path and depends on its stored connector
57token. An update handoff immediately starts a replacement server, and replacing
58the tunnel would add routing propagation delay to every update. These exceptions
59belong to [shutdown handling](../../apps/server/src/cloud/http.ts).
60​
61Release and unlink claim the allocation generation before deleting external
62resources. A delayed cleanup must not delete a tunnel reused by a concurrent
63restart or relink. Unlink commits authorization revocation before external
64teardown, because a database failure must leave the active link usable. Failed
65teardown retains enough state to retry. See the
66[managed endpoint lifecycle](../../infra/relay/src/environments/ManagedEndpointProvider.ts).
67​
68## Idle tunnels are reclaimed and recovered
69​
70Cloudflare bills a tunnel whether or not a connector is attached, so a laptop
71that sleeps with a linked environment leaves a paid tunnel behind. The relay's
72five-minute maintenance job can reclaim those tunnels. `RELAY_TUNNEL_CLEANUP_MODE`
73selects `off`, `dry-run`, or `enabled`, with `off` as the default. The mode is
74read at deploy time, so changing it means a relay deploy, not a variable flip.
75A candidate is a same-stage tunnel that Cloudflare reports down for at least
76five minutes, or one that never connected and is at least an hour old. The
77longer grace for never-connected tunnels covers a pairing still in progress.
78​
79Cleanup deletes only tunnels whose host has registered recovery. Allocations
80without recovery registration belong to hosts that cannot replace a deleted
81tunnel and are left alone. Allocations with no recorded tunnel ID, or a
82different tunnel ID, are skipped because a provision may own them. A tunnel with
83no allocation row at all is counted as `skippedOrphan` and never deleted: there
84is no row to lock, so a relink that adopts it by name could race the delete.
85Clear those by hand. Each sweep is bounded: at most ten list requests, 100 deletions, a
86two-minute deadline, and an early stop on a Cloudflare rate limit. Each sweep
87starts one budget further along the candidate list, so a block of deletes that
88keep failing cannot starve the tunnels listed after them. See the
89[reaper](../../infra/relay/src/environments/ManagedEndpointReaper.ts).
90​
91A host registers recovery at startup by sending its tunnel ID and loopback
92origin with a short-lived signature from the environment key. Registration
93touches Cloudflare only when the local host or port changed, and once per
94existing allocation on the first registration after the upgrade because the
95stored origin is empty. First registrations are jittered so an auto-update wave
96does not hit the relay at once. The host stores a confirmed-origin marker with
97the connector config, and a later boot starts the connector before registration
98only when that marker matches the current config and port. If registration
99cannot reach the relay for ten minutes, the host starts its stored config anyway
100and keeps registering in the background until it can reconcile the origin.
101If the connector exits, or `cloudflared` reports repeated tunnel
102rejections, the host asks the relay for a replacement, at most once every two
103minutes. The relay
104provisions under the same allocation, so the hostname and DNS record survive
105and clients keep their bindings. Every mutation on an allocation bumps its
106`generation`, and deletion locks the row at the generation it claimed, so a
107host that reconnects mid-sweep wins.
108​
109## OAuth traps
110​
111Interactive clients and the headless CLI use the same Clerk application but
112different credentials. The relay accepts both session-template JWTs and CLI
113OAuth tokens; requiring a JWT template for the CLI would reject valid logins.
114The CLI is a public OAuth client using PKCE and stores no client secret.
115​
116Loopback CLI authorization starts on the hosted `/connect` page so sign-in
117completes before entering Clerk's authorize endpoint. Sending a signed-out
118browser straight to that endpoint loses the authorize parameters during the
119sign-in redirect. The [shared flow](../../packages/shared/src/connectAuth.ts)
120preserves PKCE and state for the loopback callback.
121​
122SSH and headless sessions use Clerk's OAuth device authorization grant because
123the browser cannot ordinarily reach a listener on the remote machine. The CLI
124polls Clerk's token endpoint directly while the user approves a short code on
125Clerk's hosted device page; the hosted app plays no part and there is no
126redirect URI or PKCE. The grant must be enabled on the CLI OAuth application
127or the device endpoint returns an error before any prompt is shown.