Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/python/src/lightspeed_ai/platform_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Principal(TypedDict):


class Credential(TypedDict):
kind: Literal['session', 'cliSession', 'personalKey', 'serviceUserKey']
kind: Literal['session', 'cliSession', 'personalKey', 'serviceUserKey', 'federated']
id: str


Expand Down
6 changes: 4 additions & 2 deletions clients/typescript/schema/platform.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
"session",
"cliSession",
"personalKey",
"serviceUserKey"
"serviceUserKey",
"federated"
]
},
"id": {
Expand Down Expand Up @@ -186,7 +187,8 @@
"session",
"cliSession",
"personalKey",
"serviceUserKey"
"serviceUserKey",
"federated"
]
},
"id": {
Expand Down
4 changes: 2 additions & 2 deletions clients/typescript/src/generated/platform-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export interface PlatformConnectionDetails {
platformAdmin: boolean;
};
credential: {
kind: "session" | "cliSession" | "personalKey" | "serviceUserKey";
kind: "session" | "cliSession" | "personalKey" | "serviceUserKey" | "federated";
id: string;
};
organizations: {
Expand Down Expand Up @@ -62,7 +62,7 @@ export interface PlatformSessionReadResult {
email: string | null;
};
credential: {
kind: "session" | "cliSession" | "personalKey" | "serviceUserKey";
kind: "session" | "cliSession" | "personalKey" | "serviceUserKey" | "federated";
id: string;
};
session: {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions platform/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"jose": "6.2.12",
"pg": "^8.23.1",
"tsx": "^4.23.15",
"yaml": "2.9.1",
"zod": "^4.6.7"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion platform/backend/src/auth-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ export type Principal = {
export interface AuthContext {
principal: Principal;
credential: {
kind: "session" | "cliSession" | "personalKey" | "serviceUserKey";
kind: "session" | "cliSession" | "personalKey" | "serviceUserKey" | "federated";
id: string;
restrictions: CredentialRestrictions;
workload?: { issuer: string; subject: string };
};
actorId: string;
session?: { id: string; createdAt: Date; expiresAt: Date };
Expand Down
11 changes: 10 additions & 1 deletion platform/backend/src/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { compactJws, MAX_WORKLOAD_TOKEN_LENGTH } from "./federation.js";
import { resolveFederatedBinding } from "./federated-binding.js";
import { eq } from "drizzle-orm";
import { schema } from "@lightspeed-ai/platform-db";
import { credentialRestrictionsSchema } from "@lightspeed-ai/platform-shared";
Expand All @@ -24,8 +26,15 @@ export async function authenticateRequest(ctx: AppContext, request: Request): Pr
if (authorization !== null) {
// An explicitly supplied invalid credential must never fall back to a broader cookie.
const match = /^Bearer ([^\s,]+)$/i.exec(authorization);
if (!match || match[1]!.length > 4096) return null;
if (!match) return null;
const secret = match[1]!;
if (ctx.env.federation && secret.length <= MAX_WORKLOAD_TOKEN_LENGTH && compactJws.test(secret)) {
const identity = await ctx.env.federation.verify(secret);
if (!identity) return null;
const binding = await ctx.db.transaction(tx => resolveFederatedBinding(tx, identity));
return binding?.auth ? { auth: binding.auth } : null;
}
if (secret.length > 4096) return null;
const configId = secret.startsWith(PERSONAL_KEY_PREFIX) ? PERSONAL_KEY_CONFIG : secret.startsWith(SERVICE_USER_KEY_PREFIX) ? SERVICE_USER_KEY_CONFIG : null;
if (!configId) {
// Ignore cookies completely when a bearer is explicit. Better Auth owns
Expand Down
5 changes: 5 additions & 0 deletions platform/backend/src/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { WorkloadFederation } from "./federation.js";
import { readFederationConfig } from "./federation-config.js";
import { readFileSync } from "node:fs";
import { databaseWorkloadIdentityFromEnv, type DatabaseWorkloadIdentity } from "@lightspeed-ai/platform-db";
import {
Expand Down Expand Up @@ -47,6 +49,7 @@ export interface ServerEnv {
apiKeyMaxExpiryDays?: number;
github: { clientId: string; clientSecret: string } | null;
oidc?: OidcConfig | null;
federation?: WorkloadFederation;
passwordSignIn?: "local" | "break-glass" | "off";
/// Authenticated Lightspeed runtime HTTP API endpoint.
lightspeedApiUrl: string | null;
Expand Down Expand Up @@ -115,6 +118,8 @@ export function loadEnv(): ServerEnv {
apiKeyMaxExpiryDays: optionalIntegerEnv("LIGHTSPEED_PLATFORM_API_KEY_MAX_EXPIRY_DAYS", 1, 3650),
github,
oidc,
federation: process.env.LIGHTSPEED_PLATFORM_FEDERATION_FILE
? new WorkloadFederation(readFederationConfig(process.env.LIGHTSPEED_PLATFORM_FEDERATION_FILE)) : undefined,
passwordSignIn: passwordSignIn as ServerEnv["passwordSignIn"],
lightspeedApiUrl: process.env.LIGHTSPEED_API_URL ?? null,
lightspeedApiToken,
Expand Down
39 changes: 39 additions & 0 deletions platform/backend/src/federated-binding.integration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { afterAll, beforeAll, expect, it } from "vitest";
import { eq } from "drizzle-orm";
import { schema } from "@lightspeed-ai/platform-db";
import { identityEnv, identityFixture } from "../test/identity-fixture.js";
import { workloadIssuer } from "../test/workload-issuer.js";

let source: Awaited<ReturnType<typeof workloadIssuer>>;
let fixture: Awaited<ReturnType<typeof identityFixture>>;
beforeAll(async () => {
source = await workloadIssuer();
fixture = await identityFixture({ ...identityEnv, federation: source.federation });
}, 30_000);
afterAll(async () => { await fixture?.postgres.close(); await source?.close(); });

it("admits a bound service user with current restrictions and enforces disablement, UID pinning and deletion on the next request", async () => {
const token = await source.mint({ "kubernetes.io": { serviceaccount: { uid: "uid-1" } } });
const request = (path: string, bearer = token, method = "GET", body?: object) =>
fixture.app.request(path, { method, headers: { authorization: `Bearer ${bearer}`, "content-type": "application/json" }, body: JSON.stringify(body) });
const session = "/api/v1/platform/session";
expect((await request(session)).status).toBe(401);
const organizationId = crypto.randomUUID();
await fixture.db.insert(schema.organization).values({ id: organizationId, slug: "federated", name: "Federated", createdAt: new Date() });
const [user] = await fixture.db.insert(schema.serviceUsers).values({ organizationId, name: "CI", role: "admin" }).returning();
const [binding] = await fixture.db.insert(schema.federatedCredentials).values({
serviceUserId: user!.id, issuer: source.issuer, subject: source.subject, serviceAccountUid: "uid-1", restrictions: { readOnly: true },
}).returning();
expect(await (await request(session)).json()).toMatchObject({ principal: { kind: "serviceUser", id: user!.id }, credential: { kind: "federated", id: binding!.id } });
expect((await request("/api/ui/me")).status).toBe(401);
expect((await request(`/api/v1/platform/organizations/${organizationId}`, token, "PATCH", { name: "New" })).status).toBe(403);
expect((await request(session, await source.mint())).status).toBe(401);
const audit = await fixture.db.select().from(schema.identityAudit).where(eq(schema.identityAudit.action, "serviceUser.authenticate"));
expect(audit[0]?.details).toMatchObject({ credentialKind: "federated", issuer: source.issuer, subject: source.subject });
expect(JSON.stringify(audit)).not.toContain(token);
await fixture.db.update(schema.serviceUsers).set({ enabled: false }).where(eq(schema.serviceUsers.id, user!.id));
expect((await request(session)).status).toBe(401);
await fixture.db.update(schema.serviceUsers).set({ enabled: true }).where(eq(schema.serviceUsers.id, user!.id));
await fixture.db.delete(schema.federatedCredentials).where(eq(schema.federatedCredentials.id, binding!.id));
expect((await request(session)).status).toBe(401);
});
32 changes: 32 additions & 0 deletions platform/backend/src/federated-binding.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { and, eq } from "drizzle-orm";
import { schema, type Db } from "@lightspeed-ai/platform-db";
import { credentialRestrictionsSchema } from "@lightspeed-ai/platform-shared";
import { auditAsSubject } from "./identity-audit.js";
import type { AuthContext } from "./auth-context.js";
import type { WorkloadIdentity } from "./federation.js";

/** An unusable binding still owns its subject and must never be eligible for enrollment. */
export async function resolveFederatedBinding(db: Pick<Db, "select" | "insert" | "update">, identity: WorkloadIdentity) {
const { federatedCredentials, serviceUsers, organization } = schema;
const [row] = await db.select({ credential: federatedCredentials, serviceUser: serviceUsers, handle: organization.slug })
.from(federatedCredentials)
.innerJoin(serviceUsers, eq(serviceUsers.id, federatedCredentials.serviceUserId))
.innerJoin(organization, eq(organization.id, serviceUsers.organizationId))
.where(and(eq(federatedCredentials.issuer, identity.issuer), eq(federatedCredentials.subject, identity.subject)));
if (!row) return null;
const limits = credentialRestrictionsSchema.safeParse(row.credential.restrictions);
if (!limits.success || !row.serviceUser.enabled ||
(row.credential.serviceAccountUid !== null && row.credential.serviceAccountUid !== identity.serviceAccountUid)) {
return { auth: null };
}
const { issuer, subject } = identity;
const auth: AuthContext = {
principal: { kind: "serviceUser", id: row.serviceUser.id, serviceUser: row.serviceUser,
organizationId: row.serviceUser.organizationId, platformAdmin: false },
credential: { kind: "federated", id: row.credential.id, restrictions: limits.data, workload: { issuer, subject } },
actorId: `serviceUser:${row.serviceUser.id}`,
};
await db.update(federatedCredentials).set({ lastUsedAt: new Date() }).where(eq(federatedCredentials.id, row.credential.id));
await auditAsSubject(db, auth, { action: "serviceUser.authenticate", targetId: row.serviceUser.id });
return { auth, handle: row.handle, organizationId: row.serviceUser.organizationId };
}
52 changes: 52 additions & 0 deletions platform/backend/src/federation-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { readFileSync } from "node:fs";
import { parse } from "yaml";
import { z } from "zod";
import { organizationCreateSchema, serviceUserCreateSchema } from "@lightspeed-ai/platform-shared";

const nonempty = z.string().min(1);
export const keySourceUrl = z.string().url().refine(value => {
const url = new URL(value);
return !url.username && !url.password && !url.hash && !url.search &&
(url.protocol === "https:" || (url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)));
}, "Use HTTPS, or HTTP on loopback");

const sources = z.union([
z.strictObject({ jwksFile: nonempty }),
z.strictObject({
discoveryUrl: keySourceUrl.optional(), jwksUrl: keySourceUrl.optional(),
caFile: nonempty.optional(), bearerTokenFile: nonempty.optional(),
}),
]);
const issuer = z.strictObject({
name: nonempty, issuer: keySourceUrl, audiences: z.array(nonempty).min(1),
algorithms: z.array(z.enum(["RS256", "RS384", "RS512", "PS256", "PS384", "PS512", "ES256", "ES384", "ES512", "EdDSA", "Ed25519"])).min(1).default(["RS256"]),
keys: sources.default({}), maxTokenLifetimeSeconds: z.int().positive().optional(),
clockSkewSeconds: z.int().min(0).max(300).default(60),
});
const rule = z.strictObject({
issuer: nonempty, subjectPattern: nonempty.max(512), maxOrganizations: z.int().nonnegative(),
handlePattern: nonempty.optional().transform(value => value === undefined ? undefined : new RegExp(value, "u")),
organization: organizationCreateSchema.pick({ universeQuota: true }).default({}),
serviceUser: z.strictObject({ name: serviceUserCreateSchema.shape.name, role: z.literal("admin") })
.default({ name: "deployer", role: "admin" }),
});
export type TrustedIssuer = z.infer<typeof issuer>;
export type EnrollmentRule = z.infer<typeof rule>;
export const federationConfig = z.strictObject({
issuers: z.array(issuer).min(1), enrollment: z.array(rule).default([]),
}).superRefine(({ issuers, enrollment }, ctx) => {
const names = new Set(issuers.map(i => i.name));
if (names.size !== issuers.length || new Set(issuers.map(i => i.issuer)).size !== issuers.length)
ctx.addIssue({ code: "custom", message: "Trusted issuer names and identifiers must be unique" });
const patterns = new Set<string>();
for (const r of enrollment) {
const key = JSON.stringify([r.issuer, r.subjectPattern]);
if (!names.has(r.issuer) || patterns.has(key))
ctx.addIssue({ code: "custom", message: "Enrollment rules must identify a trusted issuer and a unique subject pattern" });
patterns.add(key);
}
});

export function readFederationConfig(path: string) {
return federationConfig.parse(parse(readFileSync(path, "utf8")));
}
70 changes: 70 additions & 0 deletions platform/backend/src/federation-keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { readFile } from "node:fs/promises";
import http from "node:http";
import https from "node:https";
import { createLocalJWKSet, type JSONWebKeySet } from "jose";
import { keySourceUrl, type TrustedIssuer } from "./federation-config.js";

const REFRESH_INTERVAL = 30_000;
const CACHE_TTL = 60 * 60_000;
const MAX_CACHE_AGE = 24 * CACHE_TTL;

async function getJson(url: URL, ca?: Buffer, bearer?: string): Promise<unknown> {
const get = url.protocol === "https:" ? https.get : http.get;
const response = await new Promise<http.IncomingMessage>((resolve, reject) => {
get(url, { ca, headers: bearer ? { authorization: `Bearer ${bearer}` } : {},
signal: AbortSignal.timeout(5000) }, resolve).on("error", reject);
});
try {
if (response.statusCode !== 200) throw new Error("Key source unavailable");
const chunks: Buffer[] = [];
let size = 0;
for await (const chunk of response) {
size += chunk.length;
if (size > 1024 * 1024) throw new Error("Oversized key source");
chunks.push(chunk);
}
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
} finally { response.destroy(); }
}

async function loadKeys(config: TrustedIssuer) {
const source = config.keys;
let keys: unknown;
if ("jwksFile" in source) {
keys = JSON.parse(await readFile(source.jwksFile, "utf8"));
} else {
const discoveryUrl = new URL(source.discoveryUrl ?? config.issuer.replace(/\/$/, "") + "/.well-known/openid-configuration");
const ca = source.caFile ? await readFile(source.caFile) : undefined;
const bearer = source.bearerTokenFile ? (await readFile(source.bearerTokenFile, "utf8")).trim() : undefined;
const discovery = await getJson(discoveryUrl, ca, bearer) as { issuer?: unknown; jwks_uri?: unknown } | null;
if (discovery?.issuer !== config.issuer) throw new Error("Discovery issuer mismatch");
const url = new URL(source.jwksUrl ?? keySourceUrl.parse(discovery.jwks_uri));
// Only explicitly configured origins may receive the operator's credential.
keys = await getJson(url, ca, source.jwksUrl || url.origin === discoveryUrl.origin ? bearer : undefined);
}
const resolve = createLocalJWKSet(keys as JSONWebKeySet);
return { resolve, kids: new Set((keys as JSONWebKeySet).keys.map(key => key.kid)), loaded: Date.now() };
}

/** One cache per configured issuer, shared by ordinary admission and enrollment. */
export class IssuerKeys {
private cached?: Awaited<ReturnType<typeof loadKeys>>;
private pending?: Promise<void>;
private attempted = -Infinity;
constructor(private readonly issuer: TrustedIssuer) {}

async resolve(kid: string) {
const now = Date.now();
if (!this.cached || now - this.cached.loaded >= CACHE_TTL || !this.cached.kids.has(kid)) {
if (!this.pending && now - this.attempted >= REFRESH_INTERVAL) {
this.attempted = now;
this.pending = loadKeys(this.issuer)
.then(cached => { this.cached = cached; })
.finally(() => { this.pending = undefined; });
}
try { await this.pending; } catch { /* Serve the last good set within its maximum age. */ }
}
if (!this.cached || Date.now() - this.cached.loaded >= MAX_CACHE_AGE) throw new Error("No current issuer keys");
return this.cached.resolve;
}
}
Loading