fix(relay): harden cloud lifecycle security
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
231de30606
commit
47de59eed3
8 files changed
+332
-71
No files matched your search
@@ -0,0 +1,87 @@
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { expect, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as PlatformError from "effect/PlatformError";
|
||||
|
||||
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
|
||||
import { ServerConfig } from "../config.ts";
|
||||
import { getOrCreateEnvironmentKeyPairFromSecretStore } from "./environmentKeys.ts";
|
||||
|
||||
const makeServerSecretStoreLayer = () =>
|
||||
ServerSecretStore.layer.pipe(
|
||||
Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-environment-keys-test-" })),
|
||||
);
|
||||
|
||||
const unusedSecretStoreOperation = () => Effect.die("unused secret-store operation");
|
||||
|
||||
it.layer(NodeServices.layer)("getOrCreateEnvironmentKeyPairFromSecretStore", (it) => {
|
||||
it.effect("persists one atomic keypair secret and reuses it", () =>
|
||||
Effect.gen(function* () {
|
||||
const secretStore = yield* ServerSecretStore.ServerSecretStore;
|
||||
|
||||
const first = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secretStore);
|
||||
const second = yield* getOrCreateEnvironmentKeyPairFromSecretStore(secretStore);
|
||||
|
||||
expect(second).toEqual(first);
|
||||
expect(yield* secretStore.get("cloud-link-ed25519-key-pair")).not.toBeNull();
|
||||
expect(yield* secretStore.get("cloud-link-ed25519-private-key")).toBeNull();
|
||||
expect(yield* secretStore.get("cloud-link-ed25519-public-key")).toBeNull();
|
||||
}).pipe(Effect.provide(makeServerSecretStoreLayer())),
|
||||
);
|
||||
|
||||
it.effect("migrates a legacy keypair into the atomic secret", () =>
|
||||
Effect.gen(function* () {
|
||||
const secretStore = yield* ServerSecretStore.ServerSecretStore;
|
||||
yield* secretStore.set("cloud-link-ed25519-private-key", new TextEncoder().encode("private"));
|
||||
yield* secretStore.set("cloud-link-ed25519-public-key", new TextEncoder().encode("public"));
|
||||
|
||||
expect(yield* getOrCreateEnvironmentKeyPairFromSecretStore(secretStore)).toEqual({
|
||||
privateKey: "private",
|
||||
publicKey: "public",
|
||||
});
|
||||
expect(yield* secretStore.get("cloud-link-ed25519-key-pair")).not.toBeNull();
|
||||
}).pipe(Effect.provide(makeServerSecretStoreLayer())),
|
||||
);
|
||||
|
||||
it.effect("uses the persisted keypair when a concurrent creator wins", () =>
|
||||
Effect.gen(function* () {
|
||||
const winner = new TextEncoder().encode(
|
||||
'{"privateKey":"winner-private","publicKey":"winner-public"}',
|
||||
);
|
||||
let createAttempted = false;
|
||||
const secretStore = {
|
||||
get: (name) =>
|
||||
Effect.sync(() =>
|
||||
name === "cloud-link-ed25519-key-pair" && createAttempted ? winner : null,
|
||||
),
|
||||
set: unusedSecretStoreOperation,
|
||||
create: () =>
|
||||
Effect.sync(() => {
|
||||
createAttempted = true;
|
||||
}).pipe(
|
||||
Effect.flatMap(() =>
|
||||
Effect.fail(
|
||||
new ServerSecretStore.SecretStoreError({
|
||||
message: "Concurrent keypair creation won.",
|
||||
cause: PlatformError.systemError({
|
||||
_tag: "AlreadyExists",
|
||||
module: "FileSystem",
|
||||
method: "open",
|
||||
pathOrDescriptor: "cloud-link-ed25519-key-pair.bin",
|
||||
}),
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
getOrCreateRandom: unusedSecretStoreOperation,
|
||||
remove: unusedSecretStoreOperation,
|
||||
} satisfies ServerSecretStore.ServerSecretStoreShape;
|
||||
|
||||
expect(yield* getOrCreateEnvironmentKeyPairFromSecretStore(secretStore)).toEqual({
|
||||
privateKey: "winner-private",
|
||||
publicKey: "winner-public",
|
||||
});
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -1,11 +1,23 @@
|
||||
import * as NodeCrypto from "node:crypto";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Schema from "effect/Schema";
|
||||
|
||||
import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
|
||||
|
||||
const CLOUD_LINK_KEY_PAIR = "cloud-link-ed25519-key-pair";
|
||||
const CLOUD_LINK_PRIVATE_KEY = "cloud-link-ed25519-private-key";
|
||||
const CLOUD_LINK_PUBLIC_KEY = "cloud-link-ed25519-public-key";
|
||||
|
||||
const EnvironmentKeyPair = Schema.Struct({
|
||||
privateKey: Schema.String,
|
||||
publicKey: Schema.String,
|
||||
});
|
||||
type EnvironmentKeyPair = typeof EnvironmentKeyPair.Type;
|
||||
|
||||
const EnvironmentKeyPairJson = Schema.fromJsonString(EnvironmentKeyPair);
|
||||
const decodeEnvironmentKeyPair = Schema.decodeUnknownEffect(EnvironmentKeyPairJson);
|
||||
const encodeEnvironmentKeyPair = Schema.encodeEffect(EnvironmentKeyPairJson);
|
||||
|
||||
function bytesToString(bytes: Uint8Array): string {
|
||||
return new TextDecoder().decode(bytes);
|
||||
}
|
||||
@@ -14,26 +26,75 @@ function stringToBytes(value: string): Uint8Array {
|
||||
return new TextEncoder().encode(value);
|
||||
}
|
||||
|
||||
const keyPairPersistenceError = (message: string, cause?: unknown) =>
|
||||
new ServerSecretStore.SecretStoreError({ message, cause });
|
||||
|
||||
const readEnvironmentKeyPair = Effect.fn("readEnvironmentKeyPair")(function* (
|
||||
secrets: ServerSecretStore.ServerSecretStoreShape,
|
||||
) {
|
||||
const encoded = yield* secrets.get(CLOUD_LINK_KEY_PAIR);
|
||||
if (encoded === null) {
|
||||
return null;
|
||||
}
|
||||
return yield* decodeEnvironmentKeyPair(bytesToString(encoded)).pipe(
|
||||
Effect.mapError((cause) =>
|
||||
keyPairPersistenceError("Failed to decode environment signing key pair.", cause),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
const persistEnvironmentKeyPair = Effect.fn("persistEnvironmentKeyPair")(function* (
|
||||
secrets: ServerSecretStore.ServerSecretStoreShape,
|
||||
keyPair: EnvironmentKeyPair,
|
||||
) {
|
||||
const encoded = yield* encodeEnvironmentKeyPair(keyPair).pipe(
|
||||
Effect.mapError((cause) =>
|
||||
keyPairPersistenceError("Failed to encode environment signing key pair.", cause),
|
||||
),
|
||||
);
|
||||
return yield* secrets.create(CLOUD_LINK_KEY_PAIR, stringToBytes(encoded)).pipe(
|
||||
Effect.as(keyPair),
|
||||
Effect.catchTag("SecretStoreError", (error) =>
|
||||
ServerSecretStore.isSecretAlreadyExistsError(error)
|
||||
? readEnvironmentKeyPair(secrets).pipe(
|
||||
Effect.flatMap((existing) =>
|
||||
existing !== null
|
||||
? Effect.succeed(existing)
|
||||
: Effect.fail(
|
||||
keyPairPersistenceError(
|
||||
"Failed to read environment signing key pair after concurrent creation.",
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
: Effect.fail(error),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
export const getOrCreateEnvironmentKeyPairFromSecretStore = Effect.fn(function* (
|
||||
secrets: ServerSecretStore.ServerSecretStoreShape,
|
||||
) {
|
||||
const existing = yield* readEnvironmentKeyPair(secrets);
|
||||
if (existing !== null) {
|
||||
return existing;
|
||||
}
|
||||
|
||||
const existingPrivate = yield* secrets.get(CLOUD_LINK_PRIVATE_KEY);
|
||||
const existingPublic = yield* secrets.get(CLOUD_LINK_PUBLIC_KEY);
|
||||
if (existingPrivate && existingPublic) {
|
||||
return {
|
||||
return yield* persistEnvironmentKeyPair(secrets, {
|
||||
privateKey: bytesToString(existingPrivate),
|
||||
publicKey: bytesToString(existingPublic),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
const keyPair = NodeCrypto.generateKeyPairSync("ed25519", {
|
||||
privateKeyEncoding: { format: "pem", type: "pkcs8" },
|
||||
publicKeyEncoding: { format: "pem", type: "spki" },
|
||||
});
|
||||
yield* secrets.set(CLOUD_LINK_PRIVATE_KEY, stringToBytes(keyPair.privateKey));
|
||||
yield* secrets.set(CLOUD_LINK_PUBLIC_KEY, stringToBytes(keyPair.publicKey));
|
||||
return {
|
||||
return yield* persistEnvironmentKeyPair(secrets, {
|
||||
privateKey: keyPair.privateKey,
|
||||
publicKey: keyPair.publicKey,
|
||||
};
|
||||
});
|
||||
});
|
||||
@@ -526,7 +526,6 @@ const cloudPreferencesHandler = Effect.fn("environment.cloud.preferences")(
|
||||
|
||||
const cloudEnvironmentHealthHandler = Effect.fn("environment.cloud.health")(
|
||||
function* (dependencies: CloudHttpDependencies, request: RelayCloudEnvironmentHealthRequest) {
|
||||
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(dependencies.secrets);
|
||||
const cloudMintPublicKey = yield* dependencies.secrets.get(CLOUD_MINT_PUBLIC_KEY).pipe(
|
||||
Effect.flatMap((bytes) =>
|
||||
bytes
|
||||
@@ -593,6 +592,7 @@ const cloudEnvironmentHealthHandler = Effect.fn("environment.cloud.health")(
|
||||
});
|
||||
}
|
||||
|
||||
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(dependencies.secrets);
|
||||
const descriptor = yield* dependencies.environment.getDescriptor;
|
||||
const responseExpiresAt = DateTime.add(now, { minutes: 5 });
|
||||
const responsePayload = {
|
||||
@@ -643,7 +643,6 @@ const cloudEnvironmentHealthHandler = Effect.fn("environment.cloud.health")(
|
||||
|
||||
const cloudMintCredentialHandler = Effect.fn("environment.cloud.mintCredential")(
|
||||
function* (dependencies: CloudHttpDependencies, request: RelayCloudMintCredentialRequest) {
|
||||
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(dependencies.secrets);
|
||||
const cloudMintPublicKey = yield* dependencies.secrets.get(CLOUD_MINT_PUBLIC_KEY).pipe(
|
||||
Effect.flatMap((bytes) =>
|
||||
bytes
|
||||
@@ -711,6 +710,7 @@ const cloudMintCredentialHandler = Effect.fn("environment.cloud.mintCredential")
|
||||
});
|
||||
}
|
||||
|
||||
const keyPair = yield* getOrCreateEnvironmentKeyPairFromSecretStore(dependencies.secrets);
|
||||
const issued = yield* dependencies.environmentAuth.createPairingLink({
|
||||
scopes: AuthStandardClientScopes,
|
||||
subject: "cloud-connect",
|
||||
|
||||
@@ -192,6 +192,25 @@ describe.sequential("signRelayAgentActivityPublishProof", () => {
|
||||
expect(AgentAwarenessRelay.isAgentActivityPublishingEnabled("true")).toBe(true);
|
||||
});
|
||||
|
||||
it("redacts failed activity details and caps other relay detail", () => {
|
||||
expect(
|
||||
AgentAwarenessRelay.sanitizeRelayAgentActivityState({
|
||||
...state,
|
||||
phase: "failed",
|
||||
detail: "Provider process exited with secret token.",
|
||||
}),
|
||||
).toMatchObject({
|
||||
phase: "failed",
|
||||
detail: "The agent run failed.",
|
||||
});
|
||||
expect(
|
||||
AgentAwarenessRelay.sanitizeRelayAgentActivityState({
|
||||
...state,
|
||||
detail: "x".repeat(200),
|
||||
})?.detail,
|
||||
).toHaveLength(160);
|
||||
});
|
||||
|
||||
it("resolves a null publish state when a thread or project snapshot disappeared", () => {
|
||||
const environmentId = "env-1" as EnvironmentId;
|
||||
const threadId = "thread-1" as ThreadId;
|
||||
|
||||
@@ -99,6 +99,23 @@ export function isAgentActivityPublishingEnabled(value: string | null): boolean
|
||||
return value === "true";
|
||||
}
|
||||
|
||||
const RELAY_AGENT_ACTIVITY_DETAIL_MAX_LENGTH = 160;
|
||||
const REDACTED_RELAY_AGENT_FAILURE_DETAIL = "The agent run failed.";
|
||||
|
||||
export function sanitizeRelayAgentActivityState(
|
||||
state: RelayAgentActivityState | null,
|
||||
): RelayAgentActivityState | null {
|
||||
if (state === null) {
|
||||
return null;
|
||||
}
|
||||
const { detail: _detail, ...rest } = state;
|
||||
const detail = (state.phase === "failed" ? REDACTED_RELAY_AGENT_FAILURE_DETAIL : state.detail)
|
||||
?.trim()
|
||||
.slice(0, RELAY_AGENT_ACTIVITY_DETAIL_MAX_LENGTH)
|
||||
.trim();
|
||||
return detail ? { ...rest, detail } : rest;
|
||||
}
|
||||
|
||||
function relayEnvironmentClient(token: string) {
|
||||
return HttpClient.mapRequest(HttpClientRequest.setHeader("authorization", `Bearer ${token}`));
|
||||
}
|
||||
@@ -203,11 +220,13 @@ export function resolveAgentAwarenessRelayPublishSnapshot(input: {
|
||||
}
|
||||
return {
|
||||
projectId: input.thread.value.projectId,
|
||||
state: projectThreadAwareness({
|
||||
environmentId: input.environmentId,
|
||||
project: input.project.value,
|
||||
thread: input.thread.value,
|
||||
}),
|
||||
state: sanitizeRelayAgentActivityState(
|
||||
projectThreadAwareness({
|
||||
environmentId: input.environmentId,
|
||||
project: input.project.value,
|
||||
thread: input.thread.value,
|
||||
}),
|
||||
),
|
||||
reason: "snapshot",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import * as NodeCryptoLayer from "@effect/platform-node/NodeCrypto";
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import { PgDialect } from "drizzle-orm/pg-core";
|
||||
import { PgDialect, QueryBuilder } from "drizzle-orm/pg-core";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Layer from "effect/Layer";
|
||||
|
||||
@@ -98,6 +98,7 @@ describe("EnvironmentCredentials", () => {
|
||||
const updateValues: Array<Record<string, unknown>> = [];
|
||||
const whereConditions: Array<unknown> = [];
|
||||
const fakeDb = {
|
||||
select: (fields: Parameters<QueryBuilder["select"]>[0]) => new QueryBuilder().select(fields),
|
||||
update: (table: unknown) => {
|
||||
expect(table).toBe(relayEnvironmentCredentials);
|
||||
return {
|
||||
@@ -135,7 +136,16 @@ describe("EnvironmentCredentials", () => {
|
||||
expect(query.sql).toContain('"relay_environment_credentials"."environment_id" = $1');
|
||||
expect(query.sql).toContain('"relay_environment_credentials"."environment_public_key" = $2');
|
||||
expect(query.sql).toContain('"relay_environment_credentials"."revoked_at" is null');
|
||||
expect(query.params).toEqual(["env_test", "environment-public-key"]);
|
||||
expect(query.sql).toContain("not exists");
|
||||
expect(query.sql).toContain('"relay_environment_links"."environment_id" = $3');
|
||||
expect(query.sql).toContain('"relay_environment_links"."environment_public_key" = $4');
|
||||
expect(query.sql).toContain('"relay_environment_links"."revoked_at" is null');
|
||||
expect(query.params).toEqual([
|
||||
"env_test",
|
||||
"environment-public-key",
|
||||
"env_test",
|
||||
"environment-public-key",
|
||||
]);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
EnvironmentCredentials.layer.pipe(
|
||||
|
||||
@@ -6,10 +6,10 @@ import * as Effect from "effect/Effect";
|
||||
import * as Encoding from "effect/Encoding";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import { and, eq, isNull, ne } from "drizzle-orm";
|
||||
import { and, eq, isNull, ne, notExists } from "drizzle-orm";
|
||||
|
||||
import { RelayDb } from "../db.ts";
|
||||
import { relayEnvironmentCredentials } from "../persistence/schema.ts";
|
||||
import { relayEnvironmentCredentials, relayEnvironmentLinks } from "../persistence/schema.ts";
|
||||
|
||||
export class EnvironmentCredentialCreatePersistenceError extends Data.TaggedError(
|
||||
"EnvironmentCredentialCreatePersistenceError",
|
||||
@@ -161,6 +161,18 @@ const make = Effect.gen(function* () {
|
||||
eq(relayEnvironmentCredentials.environmentId, input.environmentId),
|
||||
eq(relayEnvironmentCredentials.environmentPublicKey, input.environmentPublicKey),
|
||||
isNull(relayEnvironmentCredentials.revokedAt),
|
||||
notExists(
|
||||
db
|
||||
.select({ userId: relayEnvironmentLinks.userId })
|
||||
.from(relayEnvironmentLinks)
|
||||
.where(
|
||||
and(
|
||||
eq(relayEnvironmentLinks.environmentId, input.environmentId),
|
||||
eq(relayEnvironmentLinks.environmentPublicKey, input.environmentPublicKey),
|
||||
isNull(relayEnvironmentLinks.revokedAt),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
.returning({
|
||||
|
||||
+107
-54
@@ -188,9 +188,13 @@ export type RelayAgentActivityPublishProofPayload =
|
||||
export type RelayAgentActivityPublishProof = string;
|
||||
|
||||
export const RelayAgentActivityPublishRequest = Schema.Struct({
|
||||
state: Schema.NullOr(RelayAgentActivityState),
|
||||
proof: TrimmedNonEmptyString,
|
||||
});
|
||||
state: Schema.NullOr(RelayAgentActivityState).annotate({
|
||||
description: "Current agent-awareness state, or null to remove the published state.",
|
||||
}),
|
||||
proof: TrimmedNonEmptyString.annotate({
|
||||
description: "Environment-signed JWT covering this published activity state.",
|
||||
}),
|
||||
}).annotate({ description: "Publishes a signed agent-awareness update from an environment." });
|
||||
export type RelayAgentActivityPublishRequest = typeof RelayAgentActivityPublishRequest.Type;
|
||||
|
||||
export const RelayEnvironmentLinkScope = Schema.Literals([
|
||||
@@ -215,10 +219,16 @@ export const RelayEnvironmentLinkProof = TrimmedNonEmptyString;
|
||||
export type RelayEnvironmentLinkProof = typeof RelayEnvironmentLinkProof.Type;
|
||||
|
||||
export const RelayEnvironmentLinkChallengeRequest = Schema.Struct({
|
||||
notificationsEnabled: Schema.Boolean,
|
||||
liveActivitiesEnabled: Schema.Boolean,
|
||||
managedTunnelsEnabled: Schema.Boolean,
|
||||
});
|
||||
notificationsEnabled: Schema.Boolean.annotate({
|
||||
description: "Whether this link may deliver push notifications.",
|
||||
}),
|
||||
liveActivitiesEnabled: Schema.Boolean.annotate({
|
||||
description: "Whether this link may update Live Activities.",
|
||||
}),
|
||||
managedTunnelsEnabled: Schema.Boolean.annotate({
|
||||
description: "Whether the relay should provision a managed tunnel for this environment.",
|
||||
}),
|
||||
}).annotate({ description: "Requested capabilities for a new environment-link challenge." });
|
||||
export type RelayEnvironmentLinkChallengeRequest = typeof RelayEnvironmentLinkChallengeRequest.Type;
|
||||
|
||||
export const RelayEnvironmentLinkChallengeResponse = Schema.Struct({
|
||||
@@ -229,12 +239,18 @@ export type RelayEnvironmentLinkChallengeResponse =
|
||||
typeof RelayEnvironmentLinkChallengeResponse.Type;
|
||||
|
||||
export const RelayEnvironmentLinkRequest = Schema.Struct({
|
||||
deviceId: Schema.optional(TrimmedNonEmptyString),
|
||||
proof: RelayEnvironmentLinkProof,
|
||||
deviceId: Schema.optional(
|
||||
TrimmedNonEmptyString.annotate({
|
||||
description: "Optional client device identifier associated with this link.",
|
||||
}),
|
||||
),
|
||||
proof: RelayEnvironmentLinkProof.annotate({
|
||||
description: "Environment-signed proof bound to a previously issued link challenge.",
|
||||
}),
|
||||
notificationsEnabled: Schema.Boolean,
|
||||
liveActivitiesEnabled: Schema.Boolean,
|
||||
managedTunnelsEnabled: Schema.Boolean,
|
||||
});
|
||||
}).annotate({ description: "Links an authenticated cloud user to a T3 environment." });
|
||||
export type RelayEnvironmentLinkRequest = typeof RelayEnvironmentLinkRequest.Type;
|
||||
|
||||
export const RelayEnvironmentLinkResponse = Schema.Struct({
|
||||
@@ -519,10 +535,22 @@ export const RelayListEnvironmentsResponse = Schema.Struct({
|
||||
export type RelayListEnvironmentsResponse = typeof RelayListEnvironmentsResponse.Type;
|
||||
|
||||
export const RelayEnvironmentConnectRequest = Schema.Struct({
|
||||
deviceId: Schema.optional(TrimmedNonEmptyString),
|
||||
clientKeyThumbprint: Schema.optional(TrimmedNonEmptyString),
|
||||
clientProofKeyThumbprint: Schema.optional(TrimmedNonEmptyString),
|
||||
});
|
||||
deviceId: Schema.optional(
|
||||
TrimmedNonEmptyString.annotate({
|
||||
description: "Optional client device identifier requesting the connection.",
|
||||
}),
|
||||
),
|
||||
clientKeyThumbprint: Schema.optional(
|
||||
TrimmedNonEmptyString.annotate({
|
||||
description: "Deprecated alias for clientProofKeyThumbprint.",
|
||||
}),
|
||||
),
|
||||
clientProofKeyThumbprint: Schema.optional(
|
||||
TrimmedNonEmptyString.annotate({
|
||||
description: "JWK thumbprint that the minted environment credential must be bound to.",
|
||||
}),
|
||||
),
|
||||
}).annotate({ description: "Requests a short-lived credential for connecting to an environment." });
|
||||
export type RelayEnvironmentConnectRequest = typeof RelayEnvironmentConnectRequest.Type;
|
||||
|
||||
export const RelayEnvironmentConnectScope = "environment:connect" as const;
|
||||
@@ -546,13 +574,21 @@ export const RelayWebClientId = "t3-web" as const;
|
||||
|
||||
export const RelayDpopAccessTokenRequest = Schema.Struct({
|
||||
grant_type: Schema.Literal(RelayDpopTokenExchangeGrantType),
|
||||
subject_token: TrimmedNonEmptyString,
|
||||
subject_token: TrimmedNonEmptyString.annotate({
|
||||
description: "Clerk bearer token for the signed-in cloud user.",
|
||||
}),
|
||||
subject_token_type: Schema.Literal(RelayJwtSubjectTokenType),
|
||||
requested_token_type: Schema.Literal(RelayAccessTokenType),
|
||||
resource: TrimmedNonEmptyString,
|
||||
scope: TrimmedNonEmptyString,
|
||||
resource: TrimmedNonEmptyString.annotate({
|
||||
description: "Relay issuer URL that will receive the DPoP-bound access token.",
|
||||
}),
|
||||
scope: TrimmedNonEmptyString.annotate({
|
||||
description: "Space-separated relay scopes requested by the client.",
|
||||
}),
|
||||
client_id: RelayPublicClientId,
|
||||
}).pipe(HttpApiSchema.asFormUrlEncoded());
|
||||
})
|
||||
.annotate({ description: "OAuth token exchange request for a DPoP-bound relay access token." })
|
||||
.pipe(HttpApiSchema.asFormUrlEncoded());
|
||||
export type RelayDpopAccessTokenRequest = typeof RelayDpopAccessTokenRequest.Type;
|
||||
|
||||
export const RelayDpopAccessTokenResponse = Schema.Struct({
|
||||
@@ -731,21 +767,25 @@ export const RelayHealthResponse = Schema.Struct({
|
||||
});
|
||||
export type RelayHealthResponse = typeof RelayHealthResponse.Type;
|
||||
|
||||
export const RelayHealthGroup = HttpApiGroup.make("health").add(
|
||||
HttpApiEndpoint.get("health", "/health", {
|
||||
success: RelayHealthResponse,
|
||||
error: RelayInternalError,
|
||||
}),
|
||||
);
|
||||
export const RelayHealthGroup = HttpApiGroup.make("health")
|
||||
.add(
|
||||
HttpApiEndpoint.get("health", "/health", {
|
||||
success: RelayHealthResponse,
|
||||
error: RelayInternalError,
|
||||
}).annotate(OpenApi.Summary, "Check relay health"),
|
||||
)
|
||||
.annotate(OpenApi.Description, "Service health and readiness.");
|
||||
|
||||
export const RelayMetadataGroup = HttpApiGroup.make("metadata").add(
|
||||
HttpApiEndpoint.get("authorizationServer", "/.well-known/oauth-authorization-server", {
|
||||
success: RelayAuthorizationServerMetadata,
|
||||
}),
|
||||
HttpApiEndpoint.get("protectedResource", "/.well-known/oauth-protected-resource", {
|
||||
success: RelayProtectedResourceMetadata,
|
||||
}),
|
||||
);
|
||||
export const RelayMetadataGroup = HttpApiGroup.make("metadata")
|
||||
.add(
|
||||
HttpApiEndpoint.get("authorizationServer", "/.well-known/oauth-authorization-server", {
|
||||
success: RelayAuthorizationServerMetadata,
|
||||
}).annotate(OpenApi.Summary, "Read OAuth authorization-server metadata"),
|
||||
HttpApiEndpoint.get("protectedResource", "/.well-known/oauth-protected-resource", {
|
||||
success: RelayProtectedResourceMetadata,
|
||||
}).annotate(OpenApi.Summary, "Read OAuth protected-resource metadata"),
|
||||
)
|
||||
.annotate(OpenApi.Description, "OAuth and DPoP discovery metadata.");
|
||||
|
||||
export const RelayRegisterDeviceEndpoint = HttpApiEndpoint.post(
|
||||
"registerDevice",
|
||||
@@ -756,7 +796,7 @@ export const RelayRegisterDeviceEndpoint = HttpApiEndpoint.post(
|
||||
success: RelayOkResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
},
|
||||
);
|
||||
).annotate(OpenApi.Summary, "Register or update a mobile device");
|
||||
|
||||
export const RelayRegisterLiveActivityEndpoint = HttpApiEndpoint.post(
|
||||
"registerLiveActivity",
|
||||
@@ -767,7 +807,7 @@ export const RelayRegisterLiveActivityEndpoint = HttpApiEndpoint.post(
|
||||
success: RelayOkResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
},
|
||||
);
|
||||
).annotate(OpenApi.Summary, "Register a Live Activity push token");
|
||||
|
||||
export const RelayUnregisterDeviceEndpoint = HttpApiEndpoint.delete(
|
||||
"unregisterDevice",
|
||||
@@ -778,7 +818,7 @@ export const RelayUnregisterDeviceEndpoint = HttpApiEndpoint.delete(
|
||||
success: RelayOkResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
},
|
||||
);
|
||||
).annotate(OpenApi.Summary, "Unregister a mobile device");
|
||||
|
||||
export const RelayMobileGroup = HttpApiGroup.make("mobile")
|
||||
.add(
|
||||
@@ -786,6 +826,7 @@ export const RelayMobileGroup = HttpApiGroup.make("mobile")
|
||||
RelayRegisterLiveActivityEndpoint,
|
||||
RelayUnregisterDeviceEndpoint,
|
||||
)
|
||||
.annotate(OpenApi.Description, "Mobile push-notification and Live Activity registration.")
|
||||
.middleware(RelayDpopClientAuth);
|
||||
|
||||
export const RelayClientGroup = HttpApiGroup.make("client")
|
||||
@@ -794,18 +835,18 @@ export const RelayClientGroup = HttpApiGroup.make("client")
|
||||
headers: RelayBearerRequestHeaders,
|
||||
success: RelayListEnvironmentsResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
}),
|
||||
}).annotate(OpenApi.Summary, "List linked environments"),
|
||||
HttpApiEndpoint.get("listDevices", "/v1/client/devices", {
|
||||
headers: RelayBearerRequestHeaders,
|
||||
success: RelayListDevicesResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
}),
|
||||
}).annotate(OpenApi.Summary, "List registered mobile devices"),
|
||||
HttpApiEndpoint.post("linkEnvironment", "/v1/client/environment-links", {
|
||||
headers: RelayBearerRequestHeaders,
|
||||
payload: RelayEnvironmentLinkRequest,
|
||||
success: RelayEnvironmentLinkResponse,
|
||||
error: RelayEnvironmentLinkErrors,
|
||||
}),
|
||||
}).annotate(OpenApi.Summary, "Link an environment"),
|
||||
HttpApiEndpoint.post(
|
||||
"createEnvironmentLinkChallenge",
|
||||
"/v1/client/environment-link-challenges",
|
||||
@@ -815,14 +856,15 @@ export const RelayClientGroup = HttpApiGroup.make("client")
|
||||
success: RelayEnvironmentLinkChallengeResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
},
|
||||
),
|
||||
).annotate(OpenApi.Summary, "Create an environment-link challenge"),
|
||||
HttpApiEndpoint.delete("unlinkEnvironment", "/v1/client/environment-links/:environmentId", {
|
||||
headers: RelayBearerRequestHeaders,
|
||||
params: RelayEnvironmentUnlinkParams,
|
||||
success: RelayOkResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
}),
|
||||
}).annotate(OpenApi.Summary, "Unlink an environment"),
|
||||
)
|
||||
.annotate(OpenApi.Description, "Cloud-user environment links and registered devices.")
|
||||
.middleware(RelayClientAuth);
|
||||
|
||||
export const RelayExchangeDpopAccessTokenEndpoint = HttpApiEndpoint.post(
|
||||
@@ -834,9 +876,11 @@ export const RelayExchangeDpopAccessTokenEndpoint = HttpApiEndpoint.post(
|
||||
success: RelayDpopAccessTokenResponse,
|
||||
error: RelayAuthAndInternalErrors,
|
||||
},
|
||||
);
|
||||
).annotate(OpenApi.Summary, "Exchange a Clerk token for a DPoP access token");
|
||||
|
||||
export const RelayTokenGroup = HttpApiGroup.make("token").add(RelayExchangeDpopAccessTokenEndpoint);
|
||||
export const RelayTokenGroup = HttpApiGroup.make("token")
|
||||
.add(RelayExchangeDpopAccessTokenEndpoint)
|
||||
.annotate(OpenApi.Description, "OAuth token exchange for DPoP-bound client access.");
|
||||
|
||||
export const RelayConnectEnvironmentEndpoint = HttpApiEndpoint.post(
|
||||
"connectEnvironment",
|
||||
@@ -850,7 +894,7 @@ export const RelayConnectEnvironmentEndpoint = HttpApiEndpoint.post(
|
||||
success: RelayEnvironmentConnectResponse,
|
||||
error: RelayEnvironmentConnectErrors,
|
||||
},
|
||||
);
|
||||
).annotate(OpenApi.Summary, "Connect to an environment");
|
||||
|
||||
export const RelayGetEnvironmentStatusEndpoint = HttpApiEndpoint.post(
|
||||
"getEnvironmentStatus",
|
||||
@@ -863,10 +907,11 @@ export const RelayGetEnvironmentStatusEndpoint = HttpApiEndpoint.post(
|
||||
success: RelayEnvironmentStatusResponse,
|
||||
error: RelayEnvironmentConnectErrors,
|
||||
},
|
||||
);
|
||||
).annotate(OpenApi.Summary, "Check environment status");
|
||||
|
||||
export const RelayDpopClientGroup = HttpApiGroup.make("dpopClient")
|
||||
.add(RelayConnectEnvironmentEndpoint, RelayGetEnvironmentStatusEndpoint)
|
||||
.annotate(OpenApi.Description, "DPoP-authenticated client access to linked environments.")
|
||||
.middleware(RelayDpopClientAuth);
|
||||
|
||||
export const RelayServerGroup = HttpApiGroup.make("server")
|
||||
@@ -883,17 +928,25 @@ export const RelayServerGroup = HttpApiGroup.make("server")
|
||||
success: RelayPublishResponse,
|
||||
error: RelayAgentActivityPublishErrors,
|
||||
},
|
||||
),
|
||||
).annotate(OpenApi.Summary, "Publish agent activity"),
|
||||
)
|
||||
.annotate(OpenApi.Description, "Environment-authenticated activity publication.")
|
||||
.middleware(RelayEnvironmentAuth);
|
||||
|
||||
export const RelayApi = HttpApi.make("RelayApi").add(
|
||||
RelayHealthGroup,
|
||||
RelayMetadataGroup,
|
||||
RelayMobileGroup,
|
||||
RelayClientGroup,
|
||||
RelayTokenGroup,
|
||||
RelayDpopClientGroup,
|
||||
RelayServerGroup,
|
||||
);
|
||||
export const RelayApi = HttpApi.make("RelayApi")
|
||||
.add(
|
||||
RelayHealthGroup,
|
||||
RelayMetadataGroup,
|
||||
RelayMobileGroup,
|
||||
RelayClientGroup,
|
||||
RelayTokenGroup,
|
||||
RelayDpopClientGroup,
|
||||
RelayServerGroup,
|
||||
)
|
||||
.annotate(OpenApi.Title, "T3 Code Relay API")
|
||||
.annotate(OpenApi.Version, "1.0.0")
|
||||
.annotate(
|
||||
OpenApi.Description,
|
||||
"Control-plane API for linking T3 environments, connecting authorized clients, and publishing agent activity.",
|
||||
);
|
||||
export type RelayApi = typeof RelayApi;
|
||||
Reference in new issue
Block a user