Skip to content

Commit 3813f0e

Browse files
refactor: hold relay signing keys in EnvironmentSigningKey and CloudMintSigner
The server re-read its environment key pair from the secret store on every proof and re-parsed the PEM per signature; the relay re-parsed the cloud mint key pair on every token it issued or verified. EnvironmentSigningKey loads and parses the environment key once at startup. CloudMintSigner parses the mint keys on first use and keeps them. relayJwt now also accepts parsed keys. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 03c417b commit 3813f0e

19 files changed

Lines changed: 347 additions & 189 deletions

‎apps/server/src/cloud/CloudLink.lifecycle.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { HttpClient, HttpClientResponse, HttpServer, type HttpClientRequest } fr
1313
import * as NetAddress from "effect/net/NetAddress";
1414

1515
import { EnvironmentId } from "@t3tools/contracts";
16+
import * as EnvironmentKeys from "./environmentKeys.ts";
1617
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
1718
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1819
import * as ServerConfigModule from "../config.ts";
@@ -161,7 +162,12 @@ describe("releaseManagedTunnelOnShutdown", () => {
161162
(harness: ReleaseHarness) =>
162163
<A, E, R>(effect: Effect.Effect<A, E, R>) =>
163164
effect.pipe(
164-
Effect.provide(CloudLink.layer.pipe(Layer.provide(ManagedPolicy.layerTest()))),
165+
Effect.provide(
166+
CloudLink.layer.pipe(
167+
Layer.provide(EnvironmentKeys.layer),
168+
Layer.provide(ManagedPolicy.layerTest()),
169+
),
170+
),
165171
Effect.provideService(
166172
ConnectReachability.ConnectReachability,
167173
ConnectReachability.ConnectReachability.of({

‎apps/server/src/cloud/CloudLink.test.ts‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as NetAddress from "effect/net/NetAddress";
1010
import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
1111
import * as NodeServices from "@effect/platform-node/NodeServices";
1212

13+
import * as EnvironmentKeys from "./environmentKeys.ts";
1314
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
1415
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1516
import * as ServerConfig from "../config.ts";
@@ -119,7 +120,13 @@ const withService = <A, E>(
119120
const link = yield* CloudLink.CloudLink;
120121
return yield* body({ preferences: { update: link.updatePreferences }, stored, relayCalls });
121122
}).pipe(
122-
Effect.provide(CloudLink.layer.pipe(Layer.provide(layerDependencies))),
123+
Effect.provide(
124+
CloudLink.layer.pipe(
125+
// These tests never sign proofs; the mocked secret store cannot create a key pair.
126+
Layer.provide(Layer.mock(EnvironmentKeys.EnvironmentSigningKey)({ publicKey: "" })),
127+
Layer.provide(layerDependencies),
128+
),
129+
),
123130
Effect.provideService(FetchHttpClient.Fetch, fetch),
124131
);
125132
});
@@ -301,6 +308,14 @@ it.effect("unlinks on startup and refuses to link when the policy turns Connect
301308
const mintError = yield* link.mintCredential({} as never).pipe(Effect.flip);
302309
assert.equal(applyError.message, "Example Corp's policy does not allow T3 Connect.");
303310
assert.equal(mintError._tag, "ManagedPolicyRefusedError");
304-
}).pipe(Effect.provide(CloudLink.layer.pipe(Layer.provide(layerDependencies))));
311+
}).pipe(
312+
Effect.provide(
313+
CloudLink.layer.pipe(
314+
// These tests never sign proofs; the mocked secret store cannot create a key pair.
315+
Layer.provide(Layer.mock(EnvironmentKeys.EnvironmentSigningKey)({ publicKey: "" })),
316+
Layer.provide(layerDependencies),
317+
),
318+
),
319+
);
305320
}),
306321
);

‎apps/server/src/cloud/CloudLink.ts‎

Lines changed: 52 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
RELAY_MANAGED_TUNNEL_RECOVERY_TYP,
3737
RELAY_MINT_REQUEST_TYP,
3838
RELAY_MINT_RESPONSE_TYP,
39-
signRelayJwt,
4039
verifyRelayJwt,
4140
} from "@t3tools/shared/relayJwt";
4241
import { isSecureRelayUrl } from "@t3tools/shared/relayUrl";
@@ -95,7 +94,7 @@ import {
9594
RELAY_ISSUER_SECRET,
9695
RELAY_URL_SECRET,
9796
} from "./config.ts";
98-
import { getOrCreateEnvironmentKeyPairFromSecretStore } from "./environmentKeys.ts";
97+
import { EnvironmentSigningKey } from "./environmentKeys.ts";
9998
import * as ConnectReachability from "./ConnectReachability.ts";
10099
import * as ManagedEndpointRuntime from "./ManagedEndpointRuntime.ts";
101100
import * as ManagedLimits from "../policy/managedLimits.ts";
@@ -525,6 +524,7 @@ const make = Effect.gen(function* () {
525524
const awarenessRelay = yield* AgentAwarenessRelay.AgentAwarenessRelay;
526525
const random = yield* SecureRandom.SecureRandom;
527526
const signature = yield* Signature.Signature;
527+
const signingKey = yield* EnvironmentSigningKey;
528528
const config = yield* ServerConfig.ServerConfig;
529529
const fileSystem = yield* FileSystem.FileSystem;
530530
const path = yield* Path.Path;
@@ -581,9 +581,6 @@ const make = Effect.gen(function* () {
581581
requestUrl: string,
582582
allowNonLoopbackRequestHost = false,
583583
) {
584-
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets).pipe(
585-
Effect.provideService(Signature.Signature, signature),
586-
);
587584
if (
588585
!isSupportedLinkProviderKind(request) ||
589586
!isAllowedEndpointOrigin({
@@ -608,24 +605,24 @@ const make = Effect.gen(function* () {
608605
challenge: request.challenge,
609606
descriptor,
610607
environmentId: descriptor.environmentId,
611-
environmentPublicKey: normalizePemForSignedPayload(keyPair.publicKey),
608+
environmentPublicKey: normalizePemForSignedPayload(signingKey.publicKey),
612609
endpoint: request.endpoint,
613610
origin: request.origin,
614611
scopes: linkProofScopes(request),
615612
} satisfies RelayEnvironmentLinkProofPayload;
616-
return yield* signRelayJwt({
617-
privateKey: keyPair.privateKey,
618-
typ: RELAY_LINK_PROOF_TYP,
619-
payload,
620-
}).pipe(
621-
Effect.provideService(Signature.Signature, signature),
622-
Effect.mapError(
623-
(cause) =>
624-
new EnvironmentAuth.ServerAuthCloudLinkJwtSigningError({
625-
cause,
626-
}),
627-
),
628-
);
613+
return yield* signingKey
614+
.sign({
615+
typ: RELAY_LINK_PROOF_TYP,
616+
payload,
617+
})
618+
.pipe(
619+
Effect.mapError(
620+
(cause) =>
621+
new EnvironmentAuth.ServerAuthCloudLinkJwtSigningError({
622+
cause,
623+
}),
624+
),
625+
);
629626
});
630627

631628
const linkProof = Effect.fn("environment.cloud.linkProof")(
@@ -925,9 +922,6 @@ const make = Effect.gen(function* () {
925922
const makeManagedTunnelRecoveryProof = Effect.fn(
926923
"environment.cloud.makeManagedTunnelRecoveryProof",
927924
)(function* (input: ManagedTunnelRecoveryProofInput) {
928-
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets).pipe(
929-
Effect.provideService(Signature.Signature, signature),
930-
);
931925
const configuredIssuer = yield* secrets.get(RELAY_ISSUER_SECRET);
932926
const now = yield* DateTime.now;
933927
const issuedAt = Math.floor(now.epochMilliseconds / 1_000);
@@ -953,16 +947,16 @@ const make = Effect.gen(function* () {
953947
}
954948
: { ...claims, action: "recover" as const, origin: input.origin };
955949

956-
return yield* signRelayJwt({
957-
privateKey: keyPair.privateKey,
958-
typ: RELAY_MANAGED_TUNNEL_RECOVERY_TYP,
959-
payload,
960-
}).pipe(
961-
Effect.provideService(Signature.Signature, signature),
962-
Effect.mapError(
963-
(cause) => new CloudLinkInternalError({ operation: "sign-recovery-proof", cause }),
964-
),
965-
);
950+
return yield* signingKey
951+
.sign({
952+
typ: RELAY_MANAGED_TUNNEL_RECOVERY_TYP,
953+
payload,
954+
})
955+
.pipe(
956+
Effect.mapError(
957+
(cause) => new CloudLinkInternalError({ operation: "sign-recovery-proof", cause }),
958+
),
959+
);
966960
});
967961

968962
/** Keep credentials: clock skew can cause 401s. A relink during the request returns false without marking; accepted registration clears the mark. */
@@ -1481,9 +1475,6 @@ const make = Effect.gen(function* () {
14811475
return yield* new CloudLinkProofReplayedError({ request: "health" });
14821476
}
14831477

1484-
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets).pipe(
1485-
Effect.provideService(Signature.Signature, signature),
1486-
);
14871478
const descriptor = yield* environment.getDescriptor;
14881479
const responseExpiresAt = DateTime.add(now, { minutes: 5 });
14891480
const responsePayload = {
@@ -1499,19 +1490,19 @@ const make = Effect.gen(function* () {
14991490
descriptor,
15001491
checkedAt: DateTime.formatIso(now),
15011492
} satisfies RelayEnvironmentHealthResponseProofPayload;
1502-
const responseProof = yield* signRelayJwt({
1503-
privateKey: keyPair.privateKey,
1504-
typ: RELAY_HEALTH_RESPONSE_TYP,
1505-
payload: responsePayload,
1506-
}).pipe(
1507-
Effect.provideService(Signature.Signature, signature),
1508-
Effect.mapError(
1509-
(cause) =>
1510-
new EnvironmentAuth.ServerAuthCloudHealthJwtSigningError({
1511-
cause,
1512-
}),
1513-
),
1514-
);
1493+
const responseProof = yield* signingKey
1494+
.sign({
1495+
typ: RELAY_HEALTH_RESPONSE_TYP,
1496+
payload: responsePayload,
1497+
})
1498+
.pipe(
1499+
Effect.mapError(
1500+
(cause) =>
1501+
new EnvironmentAuth.ServerAuthCloudHealthJwtSigningError({
1502+
cause,
1503+
}),
1504+
),
1505+
);
15151506
return {
15161507
environmentId,
15171508
status: "online",
@@ -1587,9 +1578,6 @@ const make = Effect.gen(function* () {
15871578
return yield* new CloudLinkProofReplayedError({ request: "mint" });
15881579
}
15891580

1590-
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets).pipe(
1591-
Effect.provideService(Signature.Signature, signature),
1592-
);
15931581
const issued = yield* environmentAuth.createPairingLink({
15941582
scopes: AuthStandardClientScopes,
15951583
subject: "cloud-connect",
@@ -1609,19 +1597,19 @@ const make = Effect.gen(function* () {
16091597
requestNonce: proof.nonce,
16101598
credential: issued.credential,
16111599
} satisfies RelayEnvironmentMintResponseProofPayload;
1612-
const responseProof = yield* signRelayJwt({
1613-
privateKey: keyPair.privateKey,
1614-
typ: RELAY_MINT_RESPONSE_TYP,
1615-
payload: responsePayload,
1616-
}).pipe(
1617-
Effect.provideService(Signature.Signature, signature),
1618-
Effect.mapError(
1619-
(cause) =>
1620-
new EnvironmentAuth.ServerAuthCloudMintJwtSigningError({
1621-
cause,
1622-
}),
1623-
),
1624-
);
1600+
const responseProof = yield* signingKey
1601+
.sign({
1602+
typ: RELAY_MINT_RESPONSE_TYP,
1603+
payload: responsePayload,
1604+
})
1605+
.pipe(
1606+
Effect.mapError(
1607+
(cause) =>
1608+
new EnvironmentAuth.ServerAuthCloudMintJwtSigningError({
1609+
cause,
1610+
}),
1611+
),
1612+
);
16251613
return {
16261614
credential: issued.credential,
16271615
expiresAt: DateTime.formatIso(issued.expiresAt),

‎apps/server/src/cloud/environmentKeys.test.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import * as PlatformError from "effect/PlatformError";
88

99
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
1010
import * as ServerConfig from "../config.ts";
11-
import { getOrCreateEnvironmentKeyPairFromSecretStore } from "./environmentKeys.ts";
11+
import {
12+
EnvironmentSigningKey,
13+
getOrCreateEnvironmentKeyPairFromSecretStore,
14+
layer,
15+
} from "./environmentKeys.ts";
1216

1317
const layerServerSecretStore = () =>
1418
ServerSecretStore.layer.pipe(
@@ -48,6 +52,12 @@ it.layer(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer))(
4852
publicKey: "public",
4953
});
5054
assert.isTrue(Option.isSome(yield* secretStore.get("cloud-link-ed25519-key-pair")));
55+
56+
// Unparseable key material must fail signing, not the layer every server starts.
57+
const signingKey = yield* EnvironmentSigningKey.pipe(Effect.provide(layer));
58+
assert.strictEqual(signingKey.publicKey, "public");
59+
const error = yield* Effect.flip(signingKey.sign({ typ: "test+jwt", payload: {} }));
60+
assert.strictEqual(error._tag, "RelayJwtError");
5161
}).pipe(Effect.provide(layerServerSecretStore())),
5262
);
5363

‎apps/server/src/cloud/environmentKeys.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import { importRelayPrivateKey, RelayJwtError, signRelayJwt } from "@t3tools/shared/relayJwt";
2+
import * as Context from "effect/Context";
3+
import * as Signature from "effect/crypto/Signature";
14
import * as Effect from "effect/Effect";
25
import * as Jwk from "effect/jose/Jwk";
6+
import * as Layer from "effect/Layer";
37
import * as Option from "effect/Option";
48
import * as Redacted from "effect/Redacted";
59
import * as Schema from "effect/Schema";
@@ -109,3 +113,42 @@ export const getOrCreateEnvironmentKeyPairFromSecretStore = Effect.fn(function*
109113
);
110114
return yield* persistEnvironmentKeyPair(secrets, keyPair);
111115
});
116+
117+
/** The environment's Ed25519 relay signing key, loaded at startup and parsed on first use. */
118+
export class EnvironmentSigningKey extends Context.Service<
119+
EnvironmentSigningKey,
120+
{
121+
/** SPKI PEM, as relays and linked clients receive it. */
122+
readonly publicKey: string;
123+
readonly sign: (input: {
124+
readonly typ: string;
125+
readonly payload: Readonly<Record<string, unknown>>;
126+
}) => Effect.Effect<string, RelayJwtError>;
127+
}
128+
>()("t3/cloud/environmentKeys/EnvironmentSigningKey") {}
129+
130+
export const layer = Layer.effect(
131+
EnvironmentSigningKey,
132+
Effect.gen(function* () {
133+
const secrets = yield* ServerSecretStore.ServerSecretStore;
134+
const signature = yield* Signature.Signature;
135+
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secrets);
136+
// Parsed lazily so corrupt key material fails Connect operations, not server startup.
137+
let privateKey: Jwk.PrivateKey | undefined;
138+
return EnvironmentSigningKey.of({
139+
publicKey: keyPair.publicKey,
140+
sign: Effect.fn("EnvironmentSigningKey.sign")(function* (input) {
141+
if (privateKey === undefined) {
142+
privateKey = yield* importRelayPrivateKey(keyPair.privateKey).pipe(
143+
Effect.mapError(
144+
(cause) => new RelayJwtError({ operation: "sign", typ: input.typ, cause }),
145+
),
146+
);
147+
}
148+
return yield* signRelayJwt({ ...input, privateKey }).pipe(
149+
Effect.provideService(Signature.Signature, signature),
150+
);
151+
}),
152+
});
153+
}),
154+
);

‎apps/server/src/cloud/http.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as HttpApiBuilder from "effect/http-api/HttpApiBuilder";
2525
import * as NetAddress from "effect/net/NetAddress";
2626
import { decodeRelayJwt, RELAY_LINK_PROOF_TYP, verifyRelayJwt } from "@t3tools/shared/relayJwt";
2727

28+
import * as EnvironmentKeys from "./environmentKeys.ts";
2829
import * as EnvironmentAuth from "../auth/EnvironmentAuth.ts";
2930
import * as AuthHttp from "../auth/http.ts";
3031
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
@@ -78,6 +79,7 @@ const layerLinkProofRoutes = HttpApiBuilder.layer(LinkProofTestApi).pipe(
7879
Layer.provide([AuthHttp.layer, ConnectHttp.layer]),
7980
Layer.provide(AuthHttp.layerAuthenticatedAuth),
8081
Layer.provide(CloudLink.layer),
82+
Layer.provide(EnvironmentKeys.layer),
8183
Layer.provide(
8284
Layer.mock(ConnectReachability.ConnectReachability)({ latest: Effect.succeedNone }),
8385
),

‎apps/server/src/relay/AgentAwarenessRelay.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import * as Stream from "effect/Stream";
2828
import * as TestClock from "effect/testing/TestClock";
2929
import * as FetchHttpClient from "effect/http/FetchHttpClient";
3030

31+
import * as EnvironmentKeys from "../cloud/environmentKeys.ts";
3132
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
3233
import {
3334
PUBLISH_AGENT_ACTIVITY_SECRET,
@@ -259,6 +260,7 @@ const makeTestRelay = Effect.fnUntraced(function* (
259260
{ preconnect: () => {} },
260261
);
261262
const relay = yield* AgentAwarenessRelay.make.pipe(
263+
Effect.provide(EnvironmentKeys.layer),
262264
Effect.provideService(ServerSecretStore.ServerSecretStore, secrets),
263265
Effect.provideService(ThreadManagementService.ThreadManagementService, threads),
264266
Effect.provideService(ServerEnvironment.ServerEnvironment, {

0 commit comments

Comments
 (0)