mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-21 02:43:25 +01:00
feat(connections): guided AWS role setup with a per-connection CloudFormation template (#22233)
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work > - The AWS connector (#22195) lets agents use AWS with credentials minted per connection. Its role method stores no AWS keys > - That method needs the customer to create an IAM role that trusts exactly one Paperclip connection, but setup only offered the generic form and a doc > - The connect API also refused the draft step this method needs, so the role method could not be set up through the UI at all > - This pull request adds a guided setup screen. Paperclip saves a draft, hands the user a prompt for their own agent that creates the role, takes back the role ARN the agent reports, and tests the same draft. CloudFormation and IAM steps are there under "Or do it by hand" > - The benefit is that a person can connect AWS without stored keys and without typing account details, using a template AWS has validated ## Linked Issues or Issue Description Refs #22195. **Subsystem affected** Connections (AWS connector setup), UI, tool-access API. **Problem or motivation** The AWS role method must save a connection before AWS can trust it. The API rejected `saveDraft` for every method except custom MCP servers, so the UI could not complete this flow. There was also no screen that showed the issuer, the subject, the trust policy or a template. **Proposed solution** - The connect API accepts `saveDraft` for methods that declare `credentialMinter`. - `GET /tool-connections/:id/aws-setup` returns the setup values and a CloudFormation template generated for that connection. - A new `AwsRoleSetup` step runs between saving the draft and testing it. The role is named after the connection and CloudFormation fills in the account, so the first form no longer asks for a role ARN; the user pastes it back once the role exists. **Alternatives considered** - A one-click CloudFormation quick-create link. Rejected: AWS accepts quick-create templates only from Amazon S3, so Paperclip would need to host a public template bucket. The screen offers a download and a console link instead. - Instructions in a doc only. Rejected: too many hand-copied values. - Asking for the role ARN up front. Rejected: the role does not exist yet, so users had to invent an ARN and then match it exactly. - Letting the user's agent report the role ARN to Paperclip itself, through a short-lived link. Not done here: it needs a new unauthenticated endpoint (and a Paperclip Cloud front-door change). The user pastes the ARN instead. ## What Changed - `packages/shared`: - The `saveDraft` validation allows methods with minted credentials. - New `AwsConnectionSetup` type. - `server/src/services/connectors/aws/setup.ts` builds the setup values: - The issuer, audience and connection subject. - The provider host and the trust policy. - A CloudFormation template with an optional identity provider (`CreateIdentityProvider`), a role named `paperclip-<connection id>`, `AccessPolicyArn` (`ReadOnlyAccess` by default for read-only connections) and `AWSMCPSignInOAuthAccessPolicy`. The account comes from CloudFormation, so the template needs no edits. - The service method `getAwsSetup` and the route `GET /api/tool-connections/{connectionId}/aws-setup`: - The route requires `connections:configure`. - It returns 422 for a connection that is not an AWS role connection. - It reports whether the issuer key is published and any role ARN already saved. - It returns `available: false` (`address_changed`) when the instance address changed after the draft was saved, since a role set up for the old address could never pass the test. - It is registered in OpenAPI. - UI: - The AWS form no longer shows a Role ARN field (it is hidden with an empty default). - `AwsRoleSetup.tsx` shows, by default, only "Set it up with your agent" (a copyable prompt that checks the AWS account, deploys the template with `aws cloudformation deploy`, and reports the role ARN) and a field to paste that ARN. "Or do it by hand" reveals the CloudFormation steps (download, console link, parameter notes, IAM acknowledgement) and the IAM console / AWS CLI steps with copyable values; the CLI commands read the account ID themselves. - It shows an access-key message when AWS cannot reach the instance, and asks the user to save again when the instance address changed. - Failed tests appear in place. AWS error codes show the server's own plain message, because the shared error copy replaces every 502 with "temporarily unavailable". - `useAwsRoleSetupStep` owns the AWS sequence; `ConnectionSetupFlow` only asks it for request options, hands back results and renders its step: - The first Connect for `aws-role` saves a draft and opens the AWS step. - "Test connection" resumes the same draft with the pasted role ARN. - Back keeps the draft, so a later Connect updates it and does not create a second one. Returning to an unfinished draft reopens the step. - Docs: the `doc/connections/AWS.md` setup section and the plan's phase table. The SDK public input pins are refreshed. ## Verification - `server/src/__tests__/aws-mcp-connector.test.ts` covers a new API-level case through the real route validation: - Saving an `aws-role` draft over HTTP works without a role ARN, and stores the keys the screen uses to reopen it. - `aws-setup` returns the issuer, subject and the connection's role name. - The template's trust condition keys equal `<issuer host>:aud` and `<issuer host>:sub`. - The template provider URL equals the issuer. - After an address change it returns `address_changed`, and saving the draft again records the new address. - An access-key connection returns 422. - No response field looks like a secret. - `ui/src/features/connections/AwsRoleSetup.test.tsx` covers: - Only the agent setup shows by default; Test connection stays off until a valid role ARN is pasted, then sends it. - A saved role ARN is filled in. - The by-hand steps and CLI commands; the agent prompt for read-only and full access. - The template download (filename and object URL cleanup). - The unreachable-instance message. - The in-place error and the address-changed message. - The AWS message mapping. - `ui/src/pages/apps/AppsConnect.test.tsx` covers the full two-step flow: the form has no Role ARN field, the first Connect sends `saveDraft: true` with no role ARN and does not finish, the AWS step loads the setup, and "Test connection" resumes `aws-draft` with the pasted role ARN and finishes. A second test reopens the step for a returning draft. - Red/green: - Removing the API change fails the API test. - Removing the draft-first wiring fails the flow test. - Totals: server AWS, OpenAPI and connector tests (75), shared `app-definitions` (42), UI connections and apps pages (1108). Typecheck is clean for shared, server, UI (except Storybook's generated catalog, which this worktree has not built) and all four server test-tree layers. - Browser, on a real dev server with an HTTPS public address: - AWS appears in Connectors with its logo. - The form has only region and access level. Connect saves the draft (reusing an earlier one) and shows only the agent prompt and the role ARN field; "Or do it by hand" reveals the CloudFormation and IAM steps. - The copied prompt names the connection's role and stack, and the template. - Test connection with a pasted role ARN showed AWS's refusal in place (no such role, placeholder issuer), as expected. An earlier run found the generic 502 copy problem, which is fixed and re-checked. - AWS CloudFormation `validate-template` (dev account, read-only) accepts the current generated template (role named after the connection) and reports `CAPABILITY_NAMED_IAM`. The screen tells the user to acknowledge it. - Not run: an agent actually running the prompt and creating a stack in AWS, and Paperclip Cloud staging. ## Risks - The feature is still behind `enableAwsConnector` (off by default). No migration. - The template creates IAM resources in the customer's account, and the customer must acknowledge that. The role trusts only this connection's subject. - If the customer already has an identity provider for the issuer and leaves `CreateIdentityProvider` at true, AWS fails the stack with a clear error. The screen explains the parameter. ## Model Used - Claude Opus 5.5 (`claude-opus-5-5`) in Claude Code, with tool use (shell, file edits, the in-app browser against a local dev server, AWS CLI `validate-template` in the dev account, 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>
This commit is contained in:
25 files changed
+1425
-58
No files matched your search
@@ -12,12 +12,20 @@ import { instanceSettingsService } from "../services/instance-settings.js";
|
||||
import { AWS_MCP_READ_ONLY_SESSION_POLICIES, type AwsMcpClients } from "../services/connectors/aws/mcp-credentials.js";
|
||||
import { resetWorkloadIdentityKeyCacheForTests, workloadIdentityService } from "../services/workload-identity.js";
|
||||
import { forgetMcpHttpSessions } from "../services/mcp-http.js";
|
||||
import express from "express";
|
||||
import request from "supertest";
|
||||
import { toolAccessRoutes } from "../routes/tool-access.js";
|
||||
import { errorHandler } from "../middleware/error-handler.js";
|
||||
|
||||
const actor = { actorType: "user" as const, actorId: "aws-test-user", actorSource: "local_implicit" as const };
|
||||
const ACCESS_KEY_ID = "AKIAEXAMPLE1234567";
|
||||
const SECRET_ACCESS_KEY = "fixture-secret-access-key";
|
||||
const awsTool = (name: string, readOnlyHint: boolean) => ({ name, description: name, inputSchema: { type: "object", properties: {} }, annotations: { readOnlyHint } });
|
||||
|
||||
function asConfig(config: unknown): { methodConfig: Record<string, unknown> } {
|
||||
return config as { methodConfig: Record<string, unknown> };
|
||||
}
|
||||
|
||||
describe("AWS connector with minted credentials", () => {
|
||||
let fixture: Awaited<ReturnType<typeof startEmbeddedPostgresTestDatabase>>;
|
||||
let db: ReturnType<typeof createDb>;
|
||||
@@ -76,6 +84,80 @@ describe("AWS connector with minted credentials", () => {
|
||||
return { aws, requests, rejected, service: toolAccessService(db, common), gateway: createToolGatewayService(db, { ...common, toolActionSigningSecret: "fixture-signing-key" }) };
|
||||
}
|
||||
|
||||
it("saves a role draft through the API and returns what to configure in AWS", async () => {
|
||||
await instanceSettingsService(db).updateExperimental({ enableAwsConnector: true });
|
||||
const { org } = await company();
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use((req, _res, next) => { req.actor = { type: "board", userId: actor.actorId, source: "local_implicit", isInstanceAdmin: true } as Express.Request["actor"]; next(); });
|
||||
app.use("/api", toolAccessRoutes(db, { paperclipCloudConnector: null }));
|
||||
app.use(errorHandler);
|
||||
// The role does not exist yet, so the draft is saved without a role ARN.
|
||||
const draft = await request(app).post(`/api/companies/${org.id}/tools/apps/connect`).send({
|
||||
galleryKey: "aws", connectionMethodKey: "aws-role", saveDraft: true,
|
||||
configValues: { roleArn: "", endpointRegion: "us-east-1", accessLevel: "read_only" },
|
||||
});
|
||||
expect(draft.status, JSON.stringify(draft.body)).toBeLessThan(300);
|
||||
const connectionId = draft.body.connectionId as string;
|
||||
// The setup screen reopens a resumed draft by these stored keys.
|
||||
const [stored] = await db.select().from(toolConnections).where(eq(toolConnections.id, connectionId));
|
||||
expect(stored).toMatchObject({ status: "draft", config: { sourceTemplateKey: "aws", connectionMethodKey: "aws-role" } });
|
||||
const setup = await request(app).get(`/api/tool-connections/${connectionId}/aws-setup`).expect(200);
|
||||
const issuer = `https://acme.paperclip.app/oidc/companies/${org.id}`;
|
||||
expect(setup.body).toMatchObject({
|
||||
available: true, issuer, audience: "sts.amazonaws.com", subject: `connection:${connectionId}`,
|
||||
roleName: `paperclip-${connectionId}`, savedRoleArn: null, issuerPublished: true,
|
||||
});
|
||||
const host = issuer.replace("https://", "");
|
||||
const template = JSON.parse(setup.body.cloudFormationTemplate);
|
||||
const role = template.Resources.PaperclipRole.Properties;
|
||||
expect(role).toMatchObject({ RoleName: `paperclip-${connectionId}` });
|
||||
expect(role.AssumeRolePolicyDocument.Statement[0].Condition.StringEquals).toEqual({
|
||||
[`${host}:aud`]: "sts.amazonaws.com", [`${host}:sub`]: `connection:${connectionId}`,
|
||||
});
|
||||
expect(template.Resources.PaperclipIdentityProvider.Properties.Url).toBe(issuer);
|
||||
expect(setup.body.trustPolicy.Statement[0].Principal.Federated).toBe(`arn:aws:iam::ACCOUNT_ID:oidc-provider/${host}`);
|
||||
expect(JSON.stringify(setup.body)).not.toMatch(/secret|private/i);
|
||||
// A reconnect may change the access level before it is saved; the guide follows the level being set up.
|
||||
const full = await request(app).get(`/api/tool-connections/${connectionId}/aws-setup?accessLevel=full`).expect(200);
|
||||
expect(full.body.accessLevel).toBe("full");
|
||||
expect(JSON.parse(full.body.cloudFormationTemplate).Parameters.AccessPolicyArn.Default).toBeUndefined();
|
||||
|
||||
// After the address changes, setup for the old address could never pass, so it asks for a new save,
|
||||
// and saving the draft again records the current address.
|
||||
vi.stubEnv("PAPERCLIP_PUBLIC_URL", "https://renamed.paperclip.app");
|
||||
try {
|
||||
await request(app).get(`/api/tool-connections/${connectionId}/aws-setup`).expect(200, { available: false, reason: "address_changed" });
|
||||
await request(app).post(`/api/companies/${org.id}/tools/apps/connect`).send({
|
||||
galleryKey: "aws", connectionMethodKey: "aws-role", saveDraft: true, resumeConnectionId: connectionId,
|
||||
configValues: { endpointRegion: "us-east-1", accessLevel: "read_only" },
|
||||
}).expect((res) => expect(res.status, JSON.stringify(res.body)).toBeLessThan(300));
|
||||
const resaved = await request(app).get(`/api/tool-connections/${connectionId}/aws-setup`).expect(200);
|
||||
expect(resaved.body).toMatchObject({ available: true, issuer: `https://renamed.paperclip.app/oidc/companies/${org.id}` });
|
||||
} finally {
|
||||
vi.stubEnv("PAPERCLIP_PUBLIC_URL", "https://acme.paperclip.app");
|
||||
}
|
||||
|
||||
// A draft never keeps a role ARN AWS has not accepted: the setup steps would tell an AWS
|
||||
// administrator to update that role, so any role named in a draft would be at risk.
|
||||
const unchecked = await request(app).post(`/api/companies/${org.id}/tools/apps/connect`).send({
|
||||
galleryKey: "aws", connectionMethodKey: "aws-role", saveDraft: true,
|
||||
configValues: { roleArn: "arn:aws:iam::123456789012:role/OrganizationAccountAccessRole", endpointRegion: "us-east-1", accessLevel: "read_only" },
|
||||
});
|
||||
expect(unchecked.status, JSON.stringify(unchecked.body)).toBeLessThan(300);
|
||||
const [uncheckedRow] = await db.select().from(toolConnections).where(eq(toolConnections.id, unchecked.body.connectionId));
|
||||
expect(JSON.stringify(uncheckedRow!.config)).not.toContain("OrganizationAccountAccessRole");
|
||||
const uncheckedSetup = await request(app).get(`/api/tool-connections/${unchecked.body.connectionId}/aws-setup`).expect(200);
|
||||
expect(uncheckedSetup.body.savedRoleArn).toBeNull();
|
||||
|
||||
const keyDraft = await request(app).post(`/api/companies/${org.id}/tools/apps/connect`).send({
|
||||
galleryKey: "aws", connectionMethodKey: "aws-access-key", saveDraft: true,
|
||||
configValues: { endpointRegion: "us-east-1", accessLevel: "read_only" },
|
||||
credentialValues: { "credentials.accessKeyId": ACCESS_KEY_ID, "credentials.secretAccessKey": SECRET_ACCESS_KEY },
|
||||
});
|
||||
await request(app).get(`/api/tool-connections/${keyDraft.body.connectionId}/aws-setup`).expect(422);
|
||||
});
|
||||
|
||||
it("stays unavailable until the experimental setting is on", async () => {
|
||||
const { org } = await company();
|
||||
const { service } = harness();
|
||||
@@ -206,7 +288,10 @@ describe("AWS connector with minted credentials", () => {
|
||||
};
|
||||
// The role must trust a connection that already exists, so setup saves a draft first without contacting AWS,
|
||||
// and publishes the company's issuer so AWS can validate it when the customer registers it.
|
||||
const draft = await service.connectGalleryApp(org.id, { ...roleInput, saveDraft: true }, actor);
|
||||
// The role is created after the draft (it is named after it), so the draft has no role ARN yet.
|
||||
const draft = await service.connectGalleryApp(org.id, {
|
||||
...roleInput, configValues: { endpointRegion: "us-east-1", accessLevel: "full" }, saveDraft: true,
|
||||
}, actor);
|
||||
expect(draft.catalog).toEqual([]);
|
||||
expect(aws.assumeRoleWithWebIdentity).not.toHaveBeenCalled();
|
||||
expect(await workloadIdentityService(db).publicKeySet(org.id)).not.toBeNull();
|
||||
@@ -239,6 +324,20 @@ describe("AWS connector with minted credentials", () => {
|
||||
await service.checkHealth(connected.connectionId, actor).catch(() => undefined);
|
||||
const [moved] = await db.select().from(toolConnections).where(eq(toolConnections.id, connected.connectionId));
|
||||
expect(moved!.healthMessage).toContain("address changed");
|
||||
// Reconnect opens the AWS step for a finished connection, set up for the current address.
|
||||
await service.finishGalleryAppConnection(org.id, connected.connectionId, {
|
||||
enabledCatalogEntryIds: connected.catalog.map((entry) => entry.id), askFirstCatalogEntryIds: [], access: "all_agents",
|
||||
}, actor);
|
||||
// Setup steps tell an administrator to update the saved role, so only the role named after this
|
||||
// connection is offered: a role with any other name (in any account) could be an unrelated one.
|
||||
expect(await service.getAwsSetup(org.id, connected.connectionId)).toMatchObject({
|
||||
available: true, issuer: `https://renamed.paperclip.app/oidc/companies/${org.id}`, savedRoleArn: null,
|
||||
});
|
||||
const ownRole = `arn:aws:iam::123456789012:role/paperclip-${connected.connectionId}`;
|
||||
const [current] = await db.select().from(toolConnections).where(eq(toolConnections.id, connected.connectionId));
|
||||
await db.update(toolConnections).set({ config: { ...current!.config, methodConfig: { ...asConfig(current!.config).methodConfig, roleArn: ownRole } } })
|
||||
.where(eq(toolConnections.id, connected.connectionId));
|
||||
expect(await service.getAwsSetup(org.id, connected.connectionId)).toMatchObject({ savedRoleArn: ownRole });
|
||||
// After the administrator registers the new address in AWS, reconnecting records it and works again.
|
||||
const reconnected = await service.connectGalleryApp(org.id, { ...roleInput, reconnectConnectionId: connected.connectionId }, actor);
|
||||
expect(reconnected.catalog).toHaveLength(2);
|
||||
|
||||
@@ -12747,6 +12747,17 @@ registerCurrentRoute({
|
||||
responses: { 200: r.ok(), ...MACHINE_SIGN_IN_ERRORS },
|
||||
});
|
||||
|
||||
registerCurrentRoute({
|
||||
"x-paperclip-transport": "rest",
|
||||
"x-paperclip-connection-management": "connections:configure",
|
||||
method: "get",
|
||||
path: "/api/tool-connections/{connectionId}/aws-setup",
|
||||
tags: ["tool-access"],
|
||||
summary: "Read what to configure in AWS for an AWS role connection",
|
||||
description: "Returns the issuer, audience, connection subject, trust policy and a CloudFormation template for this connection. No secret values. Optional query `accessLevel` (`read_only` or `full`) sets up for an access level not saved yet. 422 when the connection does not use an AWS role.",
|
||||
responses: { 200: r.ok(), ...MACHINE_SIGN_IN_ERRORS },
|
||||
});
|
||||
|
||||
registerCurrentRoute({
|
||||
"x-paperclip-transport": "rest",
|
||||
"x-paperclip-connection-management": "connections:configure",
|
||||
|
||||
@@ -1121,6 +1121,15 @@ function connectorEnrollmentPrincipal(req: Request): string {
|
||||
res.json(await svc.getMachineSignIn(existing.companyId, existing.id));
|
||||
});
|
||||
|
||||
router.get("/tool-connections/:connectionId/aws-setup", managementGuard("connections:configure", "GET /tool-connections/:connectionId/aws-setup"), async (req, res) => {
|
||||
const existing = await getAccessibleResource(req, res, svc.getConnection(req.params.connectionId as string), "Tool connection not found");
|
||||
if (!existing) return;
|
||||
await assertToolConnectionConfigureAccess(req, existing);
|
||||
// The access level being set up (it may not be saved yet, as when reconnecting) decides the template's default policy.
|
||||
const accessLevel = req.query.accessLevel === "full" || req.query.accessLevel === "read_only" ? req.query.accessLevel : undefined;
|
||||
res.json(await svc.getAwsSetup(existing.companyId, existing.id, { accessLevel }));
|
||||
});
|
||||
|
||||
router.post("/tool-connections/:connectionId/machine-sign-in/test", managementGuard("connections:configure", "POST /tool-connections/:connectionId/machine-sign-in/test"), validate(testMachineSignInSchema), async (req, res) => {
|
||||
const existing = await getAccessibleResource(req, res, svc.getConnection(req.params.connectionId as string), "Tool connection not found");
|
||||
if (!existing) return;
|
||||
|
||||
@@ -38,7 +38,11 @@ const SESSION_SECONDS = 3600;
|
||||
const DEFAULT_STEP_TIMEOUT_MS = 10_000;
|
||||
/** Mint again this long before the token expires, so an in-flight call never carries an expiring token. */
|
||||
const REFRESH_MARGIN_MS = 5 * 60 * 1000;
|
||||
const ROLE_ARN_PATTERN = /^arn:aws[a-z-]*:iam::[0-9]{12}:role\/[A-Za-z0-9+=,.@_/-]{1,512}$/;
|
||||
/**
|
||||
* An IAM role ARN: partition, account, optional path segments, and a role name of
|
||||
* at most 64 characters. The app definition's field pattern mirrors this rule.
|
||||
*/
|
||||
export const AWS_ROLE_ARN_PATTERN = /^arn:(aws[a-z-]*):iam::([0-9]{12}):role\/((?:[A-Za-z0-9+=,.@_-]+\/)*)([A-Za-z0-9+=,.@_-]{1,64})$/;
|
||||
|
||||
export type AwsMcpAccessLevel = "read_only" | "full";
|
||||
|
||||
@@ -117,7 +121,7 @@ export function awsMcpSettings(connection: ConnectionLike & { config: Record<str
|
||||
if (!(AWS_MCP_ENDPOINT_REGIONS as readonly unknown[]).includes(endpointRegion)) throw invalid();
|
||||
if (connection.config.url !== `https://aws-mcp.${endpointRegion}.api.aws/mcp`) throw invalid();
|
||||
const roleArn = typeof values.roleArn === "string" && values.roleArn.trim() ? values.roleArn.trim() : null;
|
||||
if (roleArn !== null && !ROLE_ARN_PATTERN.test(roleArn)) throw invalid();
|
||||
if (roleArn !== null && !AWS_ROLE_ARN_PATTERN.test(roleArn)) throw invalid();
|
||||
if (method === "aws-role" && !roleArn) throw invalid();
|
||||
const accessLevel = values.accessLevel === "full" ? "full" : "read_only";
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { APP_DEFINITIONS } from "@paperclipai/shared";
|
||||
import { AWS_ROLE_ARN_PATTERN } from "./mcp-credentials.js";
|
||||
import { awsConnectionRoleName, awsRoleSetup } from "./setup.js";
|
||||
|
||||
const ISSUER = "https://acme.paperclip.app/oidc/companies/company-1";
|
||||
const HOST = "acme.paperclip.app/oidc/companies/company-1";
|
||||
|
||||
function template(setup: ReturnType<typeof awsRoleSetup>) {
|
||||
return JSON.parse(setup.cloudFormationTemplate);
|
||||
}
|
||||
|
||||
describe("awsRoleSetup", () => {
|
||||
it("builds a role that trusts only this connection, with the sign-in policy and a one-hour session", () => {
|
||||
const setup = awsRoleSetup({ issuer: ISSUER, connectionId: "conn-1", accessLevel: "read_only" });
|
||||
expect(setup).toMatchObject({ roleName: "paperclip-conn-1", subject: "connection:conn-1", providerHost: HOST });
|
||||
const t = template(setup);
|
||||
const role = t.Resources.PaperclipRole.Properties;
|
||||
expect(role).toMatchObject({ RoleName: "paperclip-conn-1", MaxSessionDuration: 3600 });
|
||||
expect(role.Path).toBeUndefined();
|
||||
expect(role.ManagedPolicyArns).toEqual([
|
||||
{ Ref: "AccessPolicyArn" },
|
||||
{ "Fn::Sub": "arn:${AWS::Partition}:iam::aws:policy/AWSMCPSignInOAuthAccessPolicy" },
|
||||
]);
|
||||
const statement = role.AssumeRolePolicyDocument.Statement[0];
|
||||
expect(statement.Action).toBe("sts:AssumeRoleWithWebIdentity");
|
||||
expect(statement.Condition.StringEquals).toEqual({ [`${HOST}:aud`]: "sts.amazonaws.com", [`${HOST}:sub`]: "connection:conn-1" });
|
||||
// Both identity-provider branches: the one this stack creates, or an existing one in the account.
|
||||
expect(statement.Principal.Federated["Fn::If"]).toEqual([
|
||||
"CreateProvider",
|
||||
{ "Fn::GetAtt": ["PaperclipIdentityProvider", "Arn"] },
|
||||
{ "Fn::Sub": `arn:\${AWS::Partition}:iam::\${AWS::AccountId}:oidc-provider/${HOST}` },
|
||||
]);
|
||||
expect(t.Resources.PaperclipIdentityProvider).toMatchObject({ Condition: "CreateProvider", Properties: { Url: ISSUER, ClientIdList: ["sts.amazonaws.com"] } });
|
||||
// A stack update that stops creating the provider, or a stack deletion, must not remove a provider other roles trust.
|
||||
expect(t.Resources.PaperclipIdentityProvider).toMatchObject({ DeletionPolicy: "Retain", UpdateReplacePolicy: "Retain" });
|
||||
expect(t.Conditions.CreateProvider).toEqual({ "Fn::Equals": [{ Ref: "CreateIdentityProvider" }, "true"] });
|
||||
expect(t.Parameters.AccessPolicyArn.Default).toBe("arn:aws:iam::aws:policy/ReadOnlyAccess");
|
||||
expect(setup.trustPolicy).toMatchObject({ Statement: [{ Principal: { Federated: `arn:aws:iam::ACCOUNT_ID:oidc-provider/${HOST}` } }] });
|
||||
});
|
||||
|
||||
it("asks for the access policy instead of defaulting to read only when the connection has full access", () => {
|
||||
const t = template(awsRoleSetup({ issuer: ISSUER, connectionId: "c", accessLevel: "full" }));
|
||||
expect(t.Parameters.AccessPolicyArn.Default).toBeUndefined();
|
||||
expect(t.Parameters.AccessPolicyArn.AllowedPattern).toBeTruthy();
|
||||
});
|
||||
|
||||
it("names the role after the connection, within IAM's 64 characters", () => {
|
||||
const id = "26e99114-bde0-428a-944b-eee3c44366de";
|
||||
expect(awsConnectionRoleName(id)).toBe(`paperclip-${id}`);
|
||||
expect(awsConnectionRoleName("x".repeat(80))).toHaveLength(64);
|
||||
expect(awsConnectionRoleName("a/b c")).toBe("paperclip-a-b-c");
|
||||
});
|
||||
});
|
||||
|
||||
describe("role ARN rule", () => {
|
||||
it("is the same in the app definition (what the form accepts) and on the server", () => {
|
||||
const aws = APP_DEFINITIONS.find((app) => app.slug === "aws")!;
|
||||
const patterns = aws.methods.flatMap((method) => [...(method.tenantFields ?? []), ...(method.extensionFields ?? [])])
|
||||
.filter((field) => field.key === "roleArn")
|
||||
.map((field) => new RegExp(field.validation!.pattern!));
|
||||
expect(patterns.length).toBeGreaterThan(0);
|
||||
const accepted = [
|
||||
"arn:aws:iam::123456789012:role/paperclip-agents",
|
||||
"arn:aws:iam::123456789012:role/team/sub/paperclip-ops",
|
||||
`arn:aws:iam::123456789012:role/${"x".repeat(64)}`,
|
||||
"arn:aws-us-gov:iam::123456789012:role/ops",
|
||||
];
|
||||
const rejected = [
|
||||
`arn:aws:iam::123456789012:role/${"x".repeat(65)}`,
|
||||
"arn:aws:iam::123456789012:role/team/",
|
||||
"arn:azure:iam::123456789012:role/ops",
|
||||
"arn:aws:iam::12345:role/ops",
|
||||
"arn:aws:iam::123456789012:user/ops",
|
||||
];
|
||||
for (const arn of [...accepted, ...rejected]) {
|
||||
const expected = accepted.includes(arn);
|
||||
expect(AWS_ROLE_ARN_PATTERN.test(arn), arn).toBe(expected);
|
||||
for (const pattern of patterns) expect(pattern.test(arn), `${pattern} ${arn}`).toBe(expected);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,146 @@
|
||||
import { AWS_MCP_STS_AUDIENCE } from "./mcp-credentials.js";
|
||||
import { workloadIdentitySubject } from "../../workload-identity.js";
|
||||
|
||||
/**
|
||||
* What a customer configures in AWS for an AWS role connection: an IAM OIDC
|
||||
* identity provider for the company's issuer, and a role that trusts exactly
|
||||
* this connection. Everything is generated for one connection (the role is named
|
||||
* after it, and CloudFormation fills in the account), so nothing is entered by hand
|
||||
* until the customer pastes back the role ARN AWS created.
|
||||
*/
|
||||
|
||||
const SIGN_IN_POLICY = "AWSMCPSignInOAuthAccessPolicy";
|
||||
|
||||
/** The IAM role name for a connection: unique per connection, within IAM's 64 characters. */
|
||||
export function awsConnectionRoleName(connectionId: string): string {
|
||||
return `paperclip-${connectionId}`.replace(/[^A-Za-z0-9+=,.@_-]/g, "-").slice(0, 64);
|
||||
}
|
||||
|
||||
export interface AwsRoleSetupInput {
|
||||
issuer: string;
|
||||
connectionId: string;
|
||||
/** Read-only connections default the template to ReadOnlyAccess; full access asks for the policy. */
|
||||
accessLevel: "read_only" | "full";
|
||||
}
|
||||
|
||||
export interface AwsRoleSetup {
|
||||
issuer: string;
|
||||
audience: string;
|
||||
subject: string;
|
||||
/** The issuer without its scheme: IAM's provider URL and the prefix of the trust condition keys. */
|
||||
providerHost: string;
|
||||
/** The role the template and the agent prompt create. */
|
||||
roleName: string;
|
||||
accessLevel: "read_only" | "full";
|
||||
/** For setting up by hand; `ACCOUNT_ID` stands for the customer's AWS account ID. */
|
||||
trustPolicy: Record<string, unknown>;
|
||||
/** A CloudFormation template (JSON) that creates the identity provider (optional) and the role. */
|
||||
cloudFormationTemplate: string;
|
||||
cloudFormationConsoleUrl: string;
|
||||
}
|
||||
|
||||
export function awsRoleSetup(input: AwsRoleSetupInput): AwsRoleSetup {
|
||||
const roleName = awsConnectionRoleName(input.connectionId);
|
||||
const providerHost = input.issuer.replace(/^https:\/\//, "");
|
||||
// The same subject the signer puts in the token, so the trust policy always matches it.
|
||||
const subject = workloadIdentitySubject(input.connectionId);
|
||||
const condition = {
|
||||
StringEquals: {
|
||||
[`${providerHost}:aud`]: AWS_MCP_STS_AUDIENCE,
|
||||
[`${providerHost}:sub`]: subject,
|
||||
},
|
||||
};
|
||||
const providerArn = `arn:aws:iam::ACCOUNT_ID:oidc-provider/${providerHost}`;
|
||||
const trustPolicy = {
|
||||
Version: "2012-10-17",
|
||||
Statement: [{
|
||||
Effect: "Allow",
|
||||
Principal: { Federated: providerArn },
|
||||
Action: "sts:AssumeRoleWithWebIdentity",
|
||||
Condition: condition,
|
||||
}],
|
||||
};
|
||||
const template = {
|
||||
AWSTemplateFormatVersion: "2010-09-09",
|
||||
Description: `Paperclip AWS connection: an IAM role that trusts only ${subject} from ${input.issuer}.`,
|
||||
Parameters: {
|
||||
CreateIdentityProvider: {
|
||||
Type: "String",
|
||||
AllowedValues: ["true", "false"],
|
||||
Default: "true",
|
||||
Description: "Choose false if this account already has an IAM identity provider for this Paperclip issuer.",
|
||||
},
|
||||
AccessPolicyArn: input.accessLevel === "read_only"
|
||||
? {
|
||||
Type: "String",
|
||||
Default: "arn:aws:iam::aws:policy/ReadOnlyAccess",
|
||||
Description: "The managed policy that decides what agents can do. This connection is read only, so ReadOnlyAccess fits.",
|
||||
}
|
||||
: {
|
||||
Type: "String",
|
||||
AllowedPattern: "^arn:aws[a-z-]*:iam::(aws|[0-9]{12}):policy/.+$",
|
||||
ConstraintDescription: "Enter the ARN of a managed policy.",
|
||||
Description: "Required. The managed policy that decides what agents can change in this account.",
|
||||
},
|
||||
},
|
||||
Conditions: {
|
||||
CreateProvider: { "Fn::Equals": [{ Ref: "CreateIdentityProvider" }, "true"] },
|
||||
},
|
||||
Resources: {
|
||||
PaperclipIdentityProvider: {
|
||||
Type: "AWS::IAM::OIDCProvider",
|
||||
Condition: "CreateProvider",
|
||||
// Other connections from this company's issuer may trust the same provider, so updating
|
||||
// or deleting this stack must never remove it.
|
||||
DeletionPolicy: "Retain",
|
||||
UpdateReplacePolicy: "Retain",
|
||||
Properties: {
|
||||
Url: input.issuer,
|
||||
ClientIdList: [AWS_MCP_STS_AUDIENCE],
|
||||
},
|
||||
},
|
||||
PaperclipRole: {
|
||||
Type: "AWS::IAM::Role",
|
||||
Properties: {
|
||||
RoleName: roleName,
|
||||
MaxSessionDuration: 3600,
|
||||
AssumeRolePolicyDocument: {
|
||||
Version: "2012-10-17",
|
||||
Statement: [{
|
||||
Effect: "Allow",
|
||||
Principal: {
|
||||
Federated: {
|
||||
"Fn::If": [
|
||||
"CreateProvider",
|
||||
{ "Fn::GetAtt": ["PaperclipIdentityProvider", "Arn"] },
|
||||
{ "Fn::Sub": `arn:\${AWS::Partition}:iam::\${AWS::AccountId}:oidc-provider/${providerHost}` },
|
||||
],
|
||||
},
|
||||
},
|
||||
Action: "sts:AssumeRoleWithWebIdentity",
|
||||
Condition: condition,
|
||||
}],
|
||||
},
|
||||
ManagedPolicyArns: [
|
||||
{ Ref: "AccessPolicyArn" },
|
||||
{ "Fn::Sub": `arn:\${AWS::Partition}:iam::aws:policy/${SIGN_IN_POLICY}` },
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
Outputs: {
|
||||
RoleArn: { Description: "Paste this into Paperclip to finish the connection.", Value: { "Fn::GetAtt": ["PaperclipRole", "Arn"] } },
|
||||
},
|
||||
};
|
||||
return {
|
||||
issuer: input.issuer,
|
||||
audience: AWS_MCP_STS_AUDIENCE,
|
||||
subject,
|
||||
providerHost,
|
||||
roleName,
|
||||
accessLevel: input.accessLevel,
|
||||
trustPolicy,
|
||||
cloudFormationTemplate: `${JSON.stringify(template, null, 2)}\n`,
|
||||
cloudFormationConsoleUrl: "https://console.aws.amazon.com/cloudformation/home#/stacks/create",
|
||||
};
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
import { notifyGitHubCredentialChange } from "./github-connection-notifications.js";
|
||||
import { AWS_MCP_ISSUER_CONFIG_KEY, awsMcpMethodUsesIssuer, type AwsMcpClients } from "./connectors/aws/mcp-credentials.js";
|
||||
import { AWS_MCP_ISSUER_CONFIG_KEY, AWS_ROLE_ARN_PATTERN, awsMcpMethodUsesIssuer, isAwsMcpConnection, type AwsMcpClients } from "./connectors/aws/mcp-credentials.js";
|
||||
import { awsRoleSetup } from "./connectors/aws/setup.js";
|
||||
import { workloadIdentityIssuer, workloadIdentityIssuerOrigin, workloadIdentityService } from "./workload-identity.js";
|
||||
import { RepositoryCatalogUnavailableError, findRepositoryCatalogUnavailableError } from "./repository-catalog-unavailable-error.js";
|
||||
import { notifyVoiceChange } from "./voice/voice-recovery-notifications.js";
|
||||
@@ -97,7 +98,7 @@ import { defaultConnectionAgentInstructions, paperclipPeerAgentInstructions, pap
|
||||
import { isBrowserUseConnection } from "./browser-use-client.js";
|
||||
|
||||
import { COGNEE_STDIO_TEMPLATE } from "./cognee-connection.js";
|
||||
import { isRemoteMcpConnectorMethod } from "@paperclipai/shared";
|
||||
import { isRemoteMcpConnectorMethod, type AwsConnectionSetup } from "@paperclipai/shared";
|
||||
|
||||
import { githubBotRequest } from "./chat-github-client.js";
|
||||
import { syncConnectionCredentialBindings } from "./connection-credential-bindings.js";
|
||||
@@ -2256,9 +2257,9 @@ export function toolAccessService(
|
||||
}
|
||||
: { templateId: method?.defaults?.templateKey };
|
||||
// The issuer an AWS role is asked to trust, recorded so a later change of this instance's address
|
||||
// gets a clear message instead of a role refusal. A draft being resumed keeps the issuer recorded at
|
||||
// the draft step; any other setup or reconnect records the current address (the one now in AWS).
|
||||
const recordedAwsIssuer = !input.reconnectConnectionId && retainedConnection?.status === "draft"
|
||||
// gets a clear message instead of a role refusal. Testing a draft keeps the issuer recorded at the
|
||||
// draft step; saving the draft again, any other setup, or a reconnect records the current address.
|
||||
const recordedAwsIssuer = !input.reconnectConnectionId && !input.saveDraft && retainedConnection?.status === "draft"
|
||||
? asRecord(retainedConnection.config)[AWS_MCP_ISSUER_CONFIG_KEY]
|
||||
: undefined;
|
||||
const awsIssuerOrigin = workloadIdentityIssuerOrigin();
|
||||
@@ -2271,7 +2272,11 @@ export function toolAccessService(
|
||||
...(awsRoleIssuer ? { [AWS_MCP_ISSUER_CONFIG_KEY]: awsRoleIssuer } : {}),
|
||||
sourceTemplateKey: galleryEntry.slug,
|
||||
connectionMethodKey: method?.key,
|
||||
methodConfig: normalizedMethodConfig?.values ?? {},
|
||||
methodConfig: input.saveDraft && awsMcpMethodUsesIssuer(galleryEntry.slug, method?.key)
|
||||
// A role ARN is only kept once AWS accepted it in a test, so an unchecked ARN never reaches
|
||||
// the setup steps (which tell an AWS administrator to update that role).
|
||||
? Object.fromEntries(Object.entries(normalizedMethodConfig?.values ?? {}).filter(([key]) => key !== "roleArn"))
|
||||
: normalizedMethodConfig?.values ?? {},
|
||||
...(galleryEntry.slug === "google-workspace" && retainedConnection?.status === "draft"
|
||||
? { workspaceSetup: retainedConnection.config.workspaceSetup }
|
||||
: {}),
|
||||
@@ -4000,6 +4005,45 @@ export function toolAccessService(
|
||||
return preflightConnectionMetadata(app, method, method.defaults, serverUrl, () => options.now?.() ?? new Date());
|
||||
}
|
||||
|
||||
/** The values and template a customer needs to create an AWS role for this connection. */
|
||||
async function getAwsSetup(
|
||||
companyId: string, connectionId: string, options: { accessLevel?: "read_only" | "full" } = {},
|
||||
): Promise<AwsConnectionSetup> {
|
||||
const [row] = await db.select().from(toolConnections)
|
||||
.where(and(eq(toolConnections.id, connectionId), eq(toolConnections.companyId, companyId)));
|
||||
if (!row) throw notFound("Tool connection not found");
|
||||
if (!isAwsMcpConnection(row) || row.config.connectionMethodKey !== "aws-role") {
|
||||
throw unprocessable("This connection does not use an AWS role.", { code: "aws_role_setup_unavailable" });
|
||||
}
|
||||
// Without a usable public address Paperclip cannot sign, so a recorded issuer would only look usable.
|
||||
const origin = workloadIdentityIssuerOrigin();
|
||||
if (!origin.available) return { available: false, reason: origin.reason };
|
||||
const issuer = workloadIdentityIssuer(origin.origin, companyId);
|
||||
// Paperclip signs only as its current address, so a draft set up for an older one could never pass
|
||||
// the test: it is saved again first. Reconnecting a connection records the current address, so it
|
||||
// gets setup for that address.
|
||||
const recorded = row.config[AWS_MCP_ISSUER_CONFIG_KEY];
|
||||
if (row.status === "draft" && typeof recorded === "string" && recorded !== issuer) {
|
||||
return { available: false, reason: "address_changed" };
|
||||
}
|
||||
const methodConfig = asRecord(row.config.methodConfig);
|
||||
const setup = awsRoleSetup({
|
||||
issuer, connectionId: row.id,
|
||||
accessLevel: options.accessLevel ?? (methodConfig.accessLevel === "full" ? "full" : "read_only"),
|
||||
});
|
||||
return {
|
||||
available: true,
|
||||
...setup,
|
||||
// The setup steps tell an AWS administrator to update this role, so only the role made for this
|
||||
// connection (named after it, so no other role shares the name) on a finished connection is
|
||||
// offered. A draft's unchecked value, or any other role, could point that update at an unrelated role.
|
||||
savedRoleArn: row.status !== "draft" && typeof methodConfig.roleArn === "string"
|
||||
&& methodConfig.roleArn.split(":role/")[1] === setup.roleName && AWS_ROLE_ARN_PATTERN.test(methodConfig.roleArn)
|
||||
? methodConfig.roleArn : null,
|
||||
issuerPublished: Boolean(await workloadIdentityService(db).publicKeySet(companyId)),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
/** Identity-only verification: no bot installation/repository access required. */
|
||||
verifyPersonalGitHubIdentity: async (companyId: string, connectionId: string, userId: string) => {
|
||||
@@ -4108,6 +4152,7 @@ export function toolAccessService(
|
||||
rotateMachineSignIn: clientCredentials.rotateMachineSignIn,
|
||||
completeMachineSignInRotation: clientCredentials.completeMachineSignInRotation,
|
||||
getMachineSignIn: clientCredentials.getMachineSignIn,
|
||||
getAwsSetup,
|
||||
|
||||
reconnectGalleryApp: withPluginApps(reconnectGalleryApp),
|
||||
|
||||
|
||||
Reference in new issue
Block a user