mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-20 09:50:48 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - Agents use outside services through connections, and the control plane holds each connection's credentials > - Companies want agents to "do things on AWS" with exactly the permissions they choose, without Paperclip storing long-lived AWS keys or rebuilding the AWS API > - AWS hosts an MCP server that runs any AWS API call in its own sandbox, and it accepts a one-hour bearer token that can be minted from IAM credentials > - This pull request adds an AWS connection that mints that token for each connection at call time. The role method proves the connection with the company's workload identity issuer (#22083), so no AWS key is stored > - The benefit is that each AWS connection carries its own role and permissions, AWS enforces them, and agents never see any AWS credential ## Linked Issues or Issue Description Refs #22083 (workload identity issuer) and #22191 (spike findings). **Subsystem affected** Connections (tool access), tool gateway, shared app definitions, instance settings. **Problem or motivation** Paperclip has no way to connect AWS. AWS's hosted MCP server needs a bearer token from AWS Sign-in, minted from IAM credentials. No existing credential path mints a header at call time from a role or key. **Proposed solution** Methods can declare `credentialMinter: "aws_mcp"`. The gateway and connection health checks mint the `Authorization` header at call time, and they share one in-memory cache per connection and grant (about 55 minutes). There are two methods: - Role (`AssumeRoleWithWebIdentity` with a Paperclip-signed connection token). - Access key (the key directly, `GetFederationToken`, or `AssumeRole`). Read-only connections add `ReadOnlyAccess` and `AWSMCPSignInOAuthAccessPolicy` as session policies, so AWS refuses changes. The feature is behind the `enableAwsConnector` experimental setting. **Alternatives considered** - Run AWS's `mcp-proxy-for-aws` process on the control plane. Rejected: it adds a Python runtime and processes, and gives nothing the token exchange does not. - Put credentials in the agent sandbox and use the AWS CLI. Deferred: there is no per-call governance, and every sandbox would need the CLI. - Add a new `credentialSource`. Rejected: it needs a migration, and the `vercel_connect` database rules do not fit stored access keys. ## What Changed - `packages/shared`: - `credentialMinter` on connection methods. The validator requires `mcp_remote` and `api_key`, and does not allow `keyPlacement` or another credential source. - The `aws` app definition (two methods, reviewed tools, `mcpSessionRequired`, experimental key). - The `enableAwsConnector` setting in the catalog, types and validators. - `aws` added to the connectable apps. - `server/src/services/connectors/aws/mcp-credentials.ts`: - Settings parsing and checks. The URL must be AWS's endpoint for the saved region, and the role ARN must be valid. - The minter: role or key session, read-only session policies, Sign-in exchange, cache keyed on settings, key references and status, in-flight de-duplication, eviction. - A plain-language error mapping that never shows AWS's own text, because that text can contain account IDs and ARNs. - Gateway (`resolveCredentialHeadersUnrecorded`), credential access (`resolveCredentialHeaders`), catalog discovery and health-failure mapping: - The AWS branch. - After a 401, the gateway evicts the token and mints again, then requires an explicit retry (no replay). Discovery lists again. - Setup: - Minted methods may save a draft without contacting the provider. The role must trust a connection that already exists. - Saving an AWS role draft publishes the company's issuer, so AWS can validate it when the customer registers it. - `workload-identity.ts`: a draft connection can get a token, so setup can test it. Disabled, archived or switched-off connections still get none. - `server/package.json`: `@aws-sdk/client-sts` and `@aws-sdk/client-signin`. The lockfile is left to the refresh workflow. - Artwork: AWS light and dark marks from LobeHub's MIT icon set (already licensed in the repo). The manifest entry passes the brand checks. - `scripts/ingest-app-definitions.mjs`: minted methods are exempt from the `keyPlacement` requirement. The override source is `scripts/app-definition-overrides/aws.json`. Permission reviews are added. - UI: the experimental settings toggle, in alphabetical position. - Docs: `doc/connections/AWS.md`, the connections index, the issuer API doc (draft rule), and the plan's phase table. ## Verification - `server/src/services/connectors/aws/mcp-credentials.test.ts` (7 tests) covers these items: - Both methods and the read-only policies. - Caching, revision, forced refresh, expiry and eviction. - Concurrent de-duplication. - The error mapping, with no AWS text leaking. - Settings validation, including the URL and region check and the rule that an unknown access level means read only. - `server/src/__tests__/aws-mcp-connector.test.ts` (3 tests, real setup, catalog and gateway code with a fake AWS and a session-requiring fake MCP server) covers these items: - The setting gates setup. - The access key is never sent as a header or stored in config. - A cached token is reused. - After a 401, a fresh token is minted and the call is not replayed. - Role draft-then-resume publishes the issuer. - The signed token has the right `iss`, `sub` and `aud`. - An AWS refusal shows a plain health message without the account ID. - Red/green: - Removing the gateway hook fails the access-key test. - Turning off `mcpSessionRequired` fails both integration tests. - Removing draft support for minted methods fails both integration tests. - Restoring each change passes again. - Nearby suites: `tool-gateway-service`, `tool-access-service`, `remote-mcp-connectors`, `instance-settings-service`, `workload-identity`, plus the new files. 775 of 775 pass. - Shared `app-definitions.test.ts` passes (42), including new `credentialMinter` schema tests. UI `InstanceExperimentalSettings` (63) and the brand asset tests pass. `check-app-brand-assets` validates 112 identities. - Typecheck is clean for `packages/shared`, `server` and `ui`. The only UI errors are in Storybook's generated catalog files, which this worktree has not built. - **Real AWS** (Paperclip dev account, integrated code path, issuer behind a temporary public tunnel): - Access key, read only: 8 tools listed. `run_script` `ListBuckets` succeeded in 8.9 s on a cold sandbox. AWS refused `CreateBucket` ("no session policy allows"). - Role: the draft was saved without contacting AWS, and the issuer was published. After an IAM provider and a role trusting only that connection were created, resuming setup listed 8 tools. `ListBuckets` succeeded in 12.8 s. The role's own permissions refused `CreateBucket`. - This run found two bugs the fakes did not: AWS needs MCP sessions for calls, and the role method needs draft-first setup with the issuer published. Both are fixed and both now have tests. - All IAM users, keys, roles, providers and buckets were deleted, and a listing confirmed it. - Not run: Paperclip Cloud staging (the setting is off by default), and `AssumeRole` from an access key with a role ARN against real AWS (unit tests only). ## Risks - The feature is off by default, behind `enableAwsConnector`. No migration. - New dependencies `@aws-sdk/client-sts` and `@aws-sdk/client-signin` are in the existing `@aws-sdk` family (the server already uses `client-s3`). - Minted tokens are held only in process memory. A restart mints again. A cached token can outlive a deactivated key by up to an hour, unless AWS returns 401, which evicts it. - `aws___run_script` is classified as a write, so the normal approval and ask-first policy applies. Read-only connections rely on AWS session policies. - The guided setup screen and the CloudFormation template are not in this PR. Setup uses the generic form and the documented draft and resume steps. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, AWS SDK for JavaScript against the Paperclip dev AWS account, a Cloudflare quick tunnel, an exploration subagent). ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change (e.g. `docs/...`, `fix/...`) and contains no internal Paperclip ticket id or instance-derived details - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Paperclip <noreply@paperclip.ing>
360 lines
19 KiB
TypeScript
360 lines
19 KiB
TypeScript
import { createPublicKey, randomUUID, verify } from "node:crypto";
|
|
import { mkdtemp, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import { join } from "node:path";
|
|
import express from "express";
|
|
import request from "supertest";
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { and, eq } from "drizzle-orm";
|
|
import {
|
|
activityLog,
|
|
closeRegisteredClients,
|
|
companies,
|
|
companyWorkloadIdentityKeys,
|
|
createDb,
|
|
toolAccessAuditEvents,
|
|
toolApplications,
|
|
toolConnections,
|
|
type Db,
|
|
} from "@paperclipai/db";
|
|
import { generateClientKeyPair } from "../services/tool-access/oauth/client-credentials.js";
|
|
import { localEncryptedProvider } from "../secrets/local-encrypted-provider.js";
|
|
import { startEmbeddedPostgresTestDatabase } from "./helpers/embedded-postgres.js";
|
|
import { resolveExternalTestDatabaseUrl } from "./helpers/external-test-database.js";
|
|
import { createApp } from "../app.js";
|
|
import { createLocalDiskStorageProvider } from "../storage/local-disk-provider.js";
|
|
import { createStorageService } from "../storage/service.js";
|
|
import { errorHandler } from "../middleware/error-handler.js";
|
|
import { workloadIdentityPublicRoutes, workloadIdentityRoutes } from "../routes/workload-identity.js";
|
|
import {
|
|
resetWorkloadIdentityKeyCacheForTests,
|
|
setWorkloadIdentityConfiguredPublicOrigin,
|
|
WORKLOAD_IDENTITY_PUBLISH_BEFORE_USE_MS,
|
|
WORKLOAD_IDENTITY_RETIRING_GRACE_MS,
|
|
workloadIdentityIssuerOrigin,
|
|
workloadIdentityService,
|
|
} from "../services/workload-identity.js";
|
|
|
|
const ORIGIN = "https://acme.paperclip.app";
|
|
const actor = { actorType: "user" as const, actorId: "board-user" };
|
|
const externalDatabase = resolveExternalTestDatabaseUrl("PAPERCLIP_WORKLOAD_IDENTITY_TEST_DATABASE_URL");
|
|
|
|
function decode(token: string) {
|
|
const [header, claims, signature] = token.split(".");
|
|
return {
|
|
header: JSON.parse(Buffer.from(header!, "base64url").toString("utf8")),
|
|
claims: JSON.parse(Buffer.from(claims!, "base64url").toString("utf8")),
|
|
signingInput: `${header}.${claims}`,
|
|
signature: Buffer.from(signature!, "base64url"),
|
|
};
|
|
}
|
|
|
|
/** What AWS does: find the key by `kid` in the published set and check the ES256 signature. */
|
|
function verifiesAgainst(token: string, keys: Array<Record<string, unknown>>): boolean {
|
|
const { header, signingInput, signature } = decode(token);
|
|
const jwk = keys.find((key) => key.kid === header.kid);
|
|
if (!jwk) return false;
|
|
return verify("sha256", Buffer.from(signingInput),
|
|
{ key: createPublicKey({ key: jwk as never, format: "jwk" }), dsaEncoding: "ieee-p1363" }, signature);
|
|
}
|
|
|
|
describe("company workload identity issuer", () => {
|
|
let db: Db;
|
|
let database: { connectionString: string; cleanup: () => Promise<void> };
|
|
let directory: string;
|
|
let clock: Date;
|
|
const service = () => workloadIdentityService(db, { now: () => clock });
|
|
|
|
async function company() {
|
|
const id = randomUUID();
|
|
await db.insert(companies).values({
|
|
id, name: `Identity ${id.slice(0, 6)}`, issuePrefix: `W${id.slice(0, 4).toUpperCase()}`,
|
|
defaultResponsibleUserId: "workload-identity-owner",
|
|
});
|
|
return id;
|
|
}
|
|
|
|
async function connection(companyId: string, state: { status?: "draft" | "active" | "disabled" | "archived"; enabled?: boolean } = {}) {
|
|
const [application] = await db.insert(toolApplications).values({
|
|
companyId, applicationKey: `aws-${randomUUID()}`, name: `AWS ${randomUUID().slice(0, 8)}`, type: "mcp_http", status: "active",
|
|
}).returning();
|
|
const [row] = await db.insert(toolConnections).values({
|
|
companyId, applicationId: application!.id, name: `AWS ${randomUUID().slice(0, 6)}`,
|
|
uid: `aws-${randomUUID()}`, connectionKind: "managed", ownership: "customer",
|
|
transport: "mcp_remote", authKind: "oauth", credentialPolicy: "shared",
|
|
status: state.status ?? "active", enabled: state.enabled ?? true,
|
|
}).returning();
|
|
return row!.id;
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
directory = await mkdtemp(join(tmpdir(), "paperclip-workload-identity-"));
|
|
vi.stubEnv("PAPERCLIP_HOME", directory);
|
|
vi.stubEnv("PAPERCLIP_SECRETS_MASTER_KEY", "");
|
|
vi.stubEnv("PAPERCLIP_SECRETS_MASTER_KEY_FILE", join(directory, "master.key"));
|
|
vi.stubEnv("PAPERCLIP_PUBLIC_URL", ORIGIN);
|
|
database = externalDatabase
|
|
? { connectionString: externalDatabase, cleanup: () => closeRegisteredClients(externalDatabase) }
|
|
: await startEmbeddedPostgresTestDatabase("paperclip-workload-identity-");
|
|
db = createDb(database.connectionString);
|
|
}, 120_000);
|
|
|
|
afterAll(async () => {
|
|
await database?.cleanup();
|
|
vi.unstubAllEnvs();
|
|
await rm(directory, { recursive: true, force: true });
|
|
});
|
|
|
|
beforeEach(() => {
|
|
clock = new Date("2026-11-17T12:00:00.000Z");
|
|
resetWorkloadIdentityKeyCacheForTests();
|
|
});
|
|
|
|
it("only offers an issuer on a public https address AWS can reach", () => {
|
|
expect(workloadIdentityIssuerOrigin(ORIGIN)).toEqual({ available: true, origin: ORIGIN });
|
|
expect(workloadIdentityIssuerOrigin(null)).toEqual({ available: false, reason: "no_public_url" });
|
|
expect(workloadIdentityIssuerOrigin("http://acme.example.com")).toEqual({ available: false, reason: "not_https" });
|
|
expect(workloadIdentityIssuerOrigin("https://acme.example.com:8443")).toEqual({ available: false, reason: "has_port" });
|
|
expect(workloadIdentityIssuerOrigin("https://localhost")).toEqual({ available: false, reason: "loopback" });
|
|
expect(workloadIdentityIssuerOrigin("https://127.0.0.1")).toEqual({ available: false, reason: "loopback" });
|
|
});
|
|
|
|
it("falls back to the configured auth address when no public URL variable is set", () => {
|
|
vi.stubEnv("PAPERCLIP_PUBLIC_URL", "");
|
|
vi.stubEnv("PAPERCLIP_AUTH_PUBLIC_BASE_URL", "");
|
|
vi.stubEnv("PAPERCLIP_API_URL", "");
|
|
try {
|
|
setWorkloadIdentityConfiguredPublicOrigin(null);
|
|
expect(workloadIdentityIssuerOrigin()).toEqual({ available: false, reason: "no_public_url" });
|
|
setWorkloadIdentityConfiguredPublicOrigin("https://paperclip.example.com/");
|
|
expect(workloadIdentityIssuerOrigin()).toEqual({ available: true, origin: "https://paperclip.example.com" });
|
|
vi.stubEnv("PAPERCLIP_PUBLIC_URL", ORIGIN);
|
|
expect(workloadIdentityIssuerOrigin()).toEqual({ available: true, origin: ORIGIN });
|
|
} finally {
|
|
setWorkloadIdentityConfiguredPublicOrigin(null);
|
|
vi.stubEnv("PAPERCLIP_PUBLIC_URL", ORIGIN);
|
|
}
|
|
});
|
|
|
|
it("publishes nothing until a company uses it, then converges on one key", async () => {
|
|
const companyId = await company();
|
|
const app = express().use(workloadIdentityPublicRoutes(db));
|
|
await request(app).get(`/oidc/companies/${companyId}/jwks.json`).expect(404);
|
|
await request(app).get(`/oidc/companies/${companyId}/.well-known/openid-configuration`).expect(404);
|
|
await request(app).get("/oidc/companies/not-a-uuid/jwks.json").expect(404);
|
|
expect(await db.select().from(companyWorkloadIdentityKeys)
|
|
.where(eq(companyWorkloadIdentityKeys.companyId, companyId))).toHaveLength(0);
|
|
|
|
const keys = await Promise.all(Array.from({ length: 6 }, () => service().ensureActiveKey(companyId)));
|
|
expect(new Set(keys.map((key) => key.keyId)).size).toBe(1);
|
|
|
|
const discovery = await request(app).get(`/oidc/companies/${companyId}/.well-known/openid-configuration`).expect(200);
|
|
const issuer = `${ORIGIN}/oidc/companies/${companyId}`;
|
|
expect(discovery.body).toMatchObject({
|
|
issuer, jwks_uri: `${issuer}/jwks.json`, id_token_signing_alg_values_supported: ["ES256"],
|
|
response_types_supported: ["id_token"], subject_types_supported: ["public"],
|
|
});
|
|
expect(discovery.headers["cache-control"]).toBe("public, max-age=300");
|
|
const jwks = await request(app).get(`/oidc/companies/${companyId}/jwks.json`).expect(200);
|
|
expect(jwks.body.keys).toHaveLength(1);
|
|
});
|
|
|
|
it("never publishes private key members (AWS rejects the set and the key counts as exposed)", async () => {
|
|
const companyId = await company();
|
|
await service().ensureActiveKey(companyId);
|
|
await service().beginRotation(companyId, actor);
|
|
const keySet = await service().publicKeySet(companyId);
|
|
expect(keySet!.keys).toHaveLength(2);
|
|
for (const key of keySet!.keys) {
|
|
expect(Object.keys(key).sort()).toEqual(["alg", "crv", "kid", "kty", "use", "x", "y"]);
|
|
expect(key).toMatchObject({ kty: "EC", crv: "P-256", alg: "ES256", use: "sig" });
|
|
}
|
|
const [stored] = await db.select().from(companyWorkloadIdentityKeys)
|
|
.where(eq(companyWorkloadIdentityKeys.companyId, companyId));
|
|
expect(JSON.stringify(stored!.publicJwk)).not.toMatch(/"d"/);
|
|
expect(JSON.stringify(stored!.privateKeyMaterial)).not.toMatch(/BEGIN PRIVATE KEY/);
|
|
});
|
|
|
|
it("signs a 5-minute token naming one connection that verifies against the published key", async () => {
|
|
const companyId = await company();
|
|
const connectionId = await connection(companyId);
|
|
const signed = await service().signConnectionToken({ companyId, connectionId, audience: "sts.amazonaws.com" });
|
|
const { header, claims } = decode(signed.token);
|
|
expect(header).toEqual({ alg: "ES256", typ: "JWT", kid: signed.keyId });
|
|
expect(claims).toMatchObject({
|
|
iss: `${ORIGIN}/oidc/companies/${companyId}`,
|
|
sub: `connection:${connectionId}`,
|
|
aud: "sts.amazonaws.com",
|
|
});
|
|
expect(claims.exp - claims.iat).toBe(300);
|
|
expect(claims.nbf).toBe(claims.iat);
|
|
expect(verifiesAgainst(signed.token, (await service().publicKeySet(companyId))!.keys)).toBe(true);
|
|
|
|
const [audit] = await db.select().from(toolAccessAuditEvents).where(and(
|
|
eq(toolAccessAuditEvents.companyId, companyId), eq(toolAccessAuditEvents.action, "workload_identity.token_issued"),
|
|
));
|
|
expect(audit).toMatchObject({ connectionId, outcome: "success" });
|
|
expect(JSON.stringify(audit)).not.toContain(signed.token.split(".")[2]!);
|
|
});
|
|
|
|
it("keeps companies and connections apart", async () => {
|
|
const companyA = await company();
|
|
const companyB = await company();
|
|
const connectionB = await connection(companyB);
|
|
await expect(service().signConnectionToken({ companyId: companyA, connectionId: connectionB, audience: "sts.amazonaws.com" }))
|
|
.rejects.toMatchObject({ status: 404 });
|
|
const tokenB = await service().signConnectionToken({ companyId: companyB, connectionId: connectionB, audience: "sts.amazonaws.com" });
|
|
await service().ensureActiveKey(companyA);
|
|
expect(verifiesAgainst(tokenB.token, (await service().publicKeySet(companyA))!.keys)).toBe(false);
|
|
});
|
|
|
|
it("refuses to sign without a usable public address or after the address changed", async () => {
|
|
const companyId = await company();
|
|
const connectionId = await connection(companyId);
|
|
await expect(service().signConnectionToken({
|
|
companyId, connectionId, audience: "sts.amazonaws.com", origin: workloadIdentityIssuerOrigin("http://localhost:3100"),
|
|
})).rejects.toMatchObject({ status: 422, details: { code: "workload_identity_unavailable", reason: "not_https" } });
|
|
await expect(service().signConnectionToken({
|
|
companyId, connectionId, audience: "sts.amazonaws.com", expectedIssuer: `https://old.example.com/oidc/companies/${companyId}`,
|
|
})).rejects.toMatchObject({ status: 409, details: { code: "workload_identity_issuer_changed" } });
|
|
await expect(service().signConnectionToken({ companyId, connectionId, audience: "has space" }))
|
|
.rejects.toMatchObject({ status: 422 });
|
|
});
|
|
|
|
it("rotates in two published steps, and an emergency rotation drops old keys at once", async () => {
|
|
const companyId = await company();
|
|
const connectionId = await connection(companyId);
|
|
const original = await service().ensureActiveKey(companyId);
|
|
const begun = await service().beginRotation(companyId, actor);
|
|
expect(begun.created).toBe(true);
|
|
expect((await service().beginRotation(companyId, actor)).created).toBe(false);
|
|
await expect(service().completeRotation(companyId, actor))
|
|
.rejects.toMatchObject({ status: 409, details: { code: "workload_identity_rotation_not_ready" } });
|
|
expect((await service().signConnectionToken({ companyId, connectionId, audience: "sts.amazonaws.com" })).keyId)
|
|
.toBe(original.keyId);
|
|
|
|
clock = new Date(clock.getTime() + WORKLOAD_IDENTITY_PUBLISH_BEFORE_USE_MS);
|
|
await service().completeRotation(companyId, actor);
|
|
const afterSwap = await service().signConnectionToken({ companyId, connectionId, audience: "sts.amazonaws.com" });
|
|
expect(afterSwap.keyId).toBe(begun.keyId);
|
|
expect((await service().publicKeySet(companyId))!.keys.map((key) => key.kid).sort())
|
|
.toEqual([original.keyId, begun.keyId].sort());
|
|
|
|
clock = new Date(clock.getTime() + WORKLOAD_IDENTITY_RETIRING_GRACE_MS);
|
|
expect((await service().publicKeySet(companyId))!.keys.map((key) => key.kid)).toEqual([begun.keyId]);
|
|
expect((await service().describe(companyId)).keys.map((key) => key.keyId)).toEqual([begun.keyId]);
|
|
await service().beginRotation(companyId, actor);
|
|
const [settled] = await db.select().from(companyWorkloadIdentityKeys).where(and(
|
|
eq(companyWorkloadIdentityKeys.companyId, companyId), eq(companyWorkloadIdentityKeys.keyId, original.keyId),
|
|
));
|
|
expect(settled!.status).toBe("retired");
|
|
|
|
const emergency = await service().emergencyRotation(companyId, actor);
|
|
const published = (await service().publicKeySet(companyId))!.keys.map((key) => key.kid);
|
|
expect(published).toEqual([emergency.keyId]);
|
|
const next = await service().signConnectionToken({ companyId, connectionId, audience: "sts.amazonaws.com" });
|
|
expect(next.keyId).toBe(emergency.keyId);
|
|
expect(verifiesAgainst(afterSwap.token, (await service().publicKeySet(companyId))!.keys)).toBe(false);
|
|
});
|
|
|
|
it("refuses to sign when the stored private key is not the published one", async () => {
|
|
const companyId = await company();
|
|
const connectionId = await connection(companyId);
|
|
const key = await service().ensureActiveKey(companyId);
|
|
const other = await generateClientKeyPair("ES256");
|
|
const { material } = await localEncryptedProvider.createSecret({ value: other.privateKeyPem });
|
|
await db.update(companyWorkloadIdentityKeys).set({ privateKeyMaterial: material })
|
|
.where(eq(companyWorkloadIdentityKeys.id, key.id));
|
|
resetWorkloadIdentityKeyCacheForTests();
|
|
await expect(service().signConnectionToken({ companyId, connectionId, audience: "sts.amazonaws.com" }))
|
|
.rejects.toThrow(/does not match its published key/);
|
|
expect(await db.select().from(toolAccessAuditEvents).where(and(
|
|
eq(toolAccessAuditEvents.companyId, companyId), eq(toolAccessAuditEvents.action, "workload_identity.token_issued"),
|
|
))).toHaveLength(0);
|
|
});
|
|
|
|
it("stops issuing tokens for a disabled, archived or switched-off connection, but lets setup test a draft", async () => {
|
|
const companyId = await company();
|
|
for (const state of [{ status: "disabled" as const }, { status: "archived" as const }, { enabled: false }]) {
|
|
const connectionId = await connection(companyId, state);
|
|
await expect(service().signConnectionToken({ companyId, connectionId, audience: "sts.amazonaws.com" }))
|
|
.rejects.toMatchObject({ status: 422, details: { code: "workload_identity_connection_inactive" } });
|
|
}
|
|
const draft = await connection(companyId, { status: "draft", enabled: false });
|
|
expect((await service().signConnectionToken({ companyId, connectionId: draft, audience: "sts.amazonaws.com" })).subject)
|
|
.toBe(`connection:${draft}`);
|
|
});
|
|
|
|
it("writes a rotation's activity entry only when the key change commits", async () => {
|
|
const companyId = await company();
|
|
await expect(service().beginRotation(companyId, actor)).rejects.toMatchObject({ status: 409 });
|
|
const entries = () => db.select().from(activityLog).where(and(
|
|
eq(activityLog.companyId, companyId), eq(activityLog.action, "workload_identity.rotation_begin"),
|
|
));
|
|
expect(await entries()).toHaveLength(0);
|
|
await service().ensureActiveKey(companyId);
|
|
await service().beginRotation(companyId, actor);
|
|
expect(await entries()).toHaveLength(1);
|
|
expect((await service().beginRotation(companyId, actor)).created).toBe(false);
|
|
expect(await entries()).toHaveLength(1);
|
|
});
|
|
|
|
it("answers the public paths anonymously in the assembled app", async () => {
|
|
const companyId = await company();
|
|
await service().ensureActiveKey(companyId);
|
|
const app = await createApp(db, {
|
|
uiMode: "none", serverPort: 0, bindHost: "127.0.0.1", allowedHostnames: ["127.0.0.1"],
|
|
deploymentMode: "authenticated", deploymentExposure: "private", authReady: true, companyDeletionEnabled: false,
|
|
managedPluginAutoInstall: [], storageService: createStorageService(createLocalDiskStorageProvider(join(directory, "storage"))),
|
|
decisionServiceOptions: { wakeOriginAgent: async () => null },
|
|
});
|
|
await app.locals.bundledPluginsStartup;
|
|
const server = app.listen(0, "127.0.0.1");
|
|
try {
|
|
await new Promise<void>((resolve) => server.once("listening", resolve));
|
|
const address = server.address();
|
|
if (!address || typeof address === "string") throw new Error("No HTTP test address");
|
|
const base = `http://127.0.0.1:${address.port}/oidc/companies/${companyId}`;
|
|
const discovery = await fetch(`${base}/.well-known/openid-configuration`);
|
|
expect(discovery.status).toBe(200);
|
|
expect((await discovery.json()).issuer).toBe(base.replace(`http://127.0.0.1:${address.port}`, ORIGIN));
|
|
const jwks = await fetch(`${base}/jwks.json`);
|
|
expect(jwks.status).toBe(200);
|
|
expect((await jwks.json()).keys).toHaveLength(1);
|
|
expect((await fetch(`http://127.0.0.1:${address.port}/api/companies/${companyId}/workload-identity`)).status)
|
|
.not.toBe(200);
|
|
} finally {
|
|
await app.locals.paperclipShutdown?.();
|
|
await new Promise<void>((resolve, reject) => server.close((error) => error ? reject(error) : resolve()));
|
|
}
|
|
}, 120_000);
|
|
|
|
it("limits status and rotation to the board and logs each rotation", async () => {
|
|
const companyId = await company();
|
|
await service().ensureActiveKey(companyId);
|
|
const boardApp = express().use(express.json(), (req, _res, next) => {
|
|
req.actor = {
|
|
type: "board", userId: "board-user", source: "local_implicit", companyIds: [companyId],
|
|
memberships: [{ companyId, membershipRole: "owner", status: "active" }], isInstanceAdmin: true,
|
|
} as Express.Request["actor"];
|
|
next();
|
|
}).use("/api", workloadIdentityRoutes(db)).use(errorHandler);
|
|
const state = await request(boardApp).get(`/api/companies/${companyId}/workload-identity`).expect(200);
|
|
expect(state.body.issuer).toBe(`${ORIGIN}/oidc/companies/${companyId}`);
|
|
expect(JSON.stringify(state.body)).not.toMatch(/private|"d"/i);
|
|
await request(boardApp).post(`/api/companies/${companyId}/workload-identity/rotation`).send({ step: "nope" }).expect(400);
|
|
await request(boardApp).post(`/api/companies/${companyId}/workload-identity/rotation`).send({ step: "begin" }).expect(200);
|
|
const [entry] = await db.select().from(activityLog).where(and(
|
|
eq(activityLog.companyId, companyId), eq(activityLog.action, "workload_identity.rotation_begin"),
|
|
));
|
|
expect(entry).toMatchObject({ actorType: "user", actorId: "board-user", entityType: "company" });
|
|
|
|
const agentApp = express().use(express.json(), (req, _res, next) => {
|
|
req.actor = { type: "agent", agentId: randomUUID(), companyId, source: "agent_key" } as Express.Request["actor"];
|
|
next();
|
|
}).use("/api", workloadIdentityRoutes(db)).use(errorHandler);
|
|
await request(agentApp).get(`/api/companies/${companyId}/workload-identity`).expect(403);
|
|
await request(agentApp).post(`/api/companies/${companyId}/workload-identity/rotation`).send({ step: "emergency" }).expect(403);
|
|
});
|
|
});
|