-
Notifications
You must be signed in to change notification settings - Fork 86
feat: generate Cedar policies from natural language in project add policy #2127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import { setTimeout as sleep } from "node:timers/promises"; | ||
| import { | ||
| GetGatewayCommand, | ||
| GetPolicyGenerationCommand, | ||
| ListGatewaysCommand, | ||
| ListPolicyEngineSummariesCommand, | ||
| ListPolicyGenerationAssetsCommand, | ||
| StartPolicyGenerationCommand, | ||
| type GatewaySummary, | ||
| type PolicyEngineSummary, | ||
| } from "@aws-sdk/client-bedrock-agentcore-control"; | ||
| import { AgentCoreCLIError, ResourceNotFoundError } from "../errors"; | ||
| import type { | ||
| CorePolicyClient, | ||
| GeneratedPolicy, | ||
| GeneratePolicyInput, | ||
| } from "../handlers/project/add/policy/types"; | ||
| import type { Logger } from "../logging"; | ||
| import type { AwsClients, CoreOptions } from "./types"; | ||
| import { toClientConfig } from "./utils"; | ||
|
|
||
| const GENERATION_POLL_DELAY_MS = 3_000; | ||
| const GENERATION_MAX_POLLS = 40; | ||
|
|
||
| export class PolicyClient implements CorePolicyClient { | ||
| constructor( | ||
| private readonly clients: AwsClients, | ||
| private readonly logger: Logger, | ||
| private readonly pollDelayMs = GENERATION_POLL_DELAY_MS, | ||
| ) {} | ||
|
|
||
| async *generatePolicy( | ||
| input: GeneratePolicyInput, | ||
| options: CoreOptions, | ||
| ): AsyncGenerator<{ message: string }, GeneratedPolicy> { | ||
| const control = this.clients.control(toClientConfig(options)); | ||
|
|
||
| yield { message: `Resolving deployed policy engine '${input.engineName}'` }; | ||
| let engine: PolicyEngineSummary | undefined; | ||
| let engineToken: string | undefined; | ||
| do { | ||
| const page = await control.send( | ||
| new ListPolicyEngineSummariesCommand({ nextToken: engineToken }), | ||
| ); | ||
| engine = page.policyEngines?.find((candidate) => candidate.name === input.engineServiceName); | ||
| engineToken = page.nextToken; | ||
| } while (!engine && engineToken); | ||
| if (!engine?.policyEngineId) { | ||
| throw new ResourceNotFoundError( | ||
| `policy engine '${input.engineName}' is not deployed; run 'agentcore project deploy' first`, | ||
| ); | ||
| } | ||
|
|
||
| yield { message: `Resolving deployed gateway '${input.gatewayName}'` }; | ||
| let deployed: GatewaySummary | undefined; | ||
| let gatewayToken: string | undefined; | ||
| do { | ||
| const page = await control.send(new ListGatewaysCommand({ nextToken: gatewayToken })); | ||
| deployed = page.items?.find((candidate) => candidate.name === input.gatewayServiceName); | ||
| gatewayToken = page.nextToken; | ||
| } while (!deployed && gatewayToken); | ||
| if (!deployed) { | ||
| throw new ResourceNotFoundError( | ||
| `gateway '${input.gatewayName}' is not deployed; run 'agentcore project deploy' first`, | ||
| ); | ||
| } | ||
| const gateway = await control.send( | ||
| new GetGatewayCommand({ gatewayIdentifier: deployed.gatewayId }), | ||
| ); | ||
| if (!gateway.gatewayArn) { | ||
| throw new AgentCoreCLIError(`could not resolve the ARN of gateway '${input.gatewayName}'`); | ||
| } | ||
|
|
||
| yield { message: "Generating a Cedar policy from the description (may take a minute)" }; | ||
| const started = await control.send( | ||
| new StartPolicyGenerationCommand({ | ||
| policyEngineId: engine.policyEngineId, | ||
| resource: { arn: gateway.gatewayArn }, | ||
| content: { rawText: input.description }, | ||
| name: `cli_generation_${Date.now()}`, | ||
| }), | ||
| ); | ||
| if (!started.policyGenerationId) { | ||
| throw new AgentCoreCLIError("StartPolicyGeneration returned no generation id"); | ||
| } | ||
|
|
||
| let status: string | undefined = "GENERATING"; | ||
| let statusReasons: string[] | undefined; | ||
| for (let poll = 0; poll < GENERATION_MAX_POLLS && status === "GENERATING"; poll++) { | ||
| await sleep(this.pollDelayMs); | ||
| const current = await control.send( | ||
| new GetPolicyGenerationCommand({ | ||
| policyGenerationId: started.policyGenerationId, | ||
| policyEngineId: engine.policyEngineId, | ||
| }), | ||
| ); | ||
| status = current.status; | ||
| statusReasons = current.statusReasons; | ||
| this.logger.debug(`policy generation ${started.policyGenerationId} status: ${status}`); | ||
| if (status === "GENERATING") yield { message: "Still generating" }; | ||
| } | ||
| if (status !== "GENERATED") { | ||
| throw new AgentCoreCLIError( | ||
| status === "GENERATING" | ||
| ? "policy generation did not finish within the CLI's wait window; it may still complete, retry the command in a few minutes" | ||
| : `policy generation did not complete: ${statusReasons?.join(", ") ?? status}`, | ||
| ); | ||
| } | ||
|
|
||
| const assets = await control.send( | ||
| new ListPolicyGenerationAssetsCommand({ | ||
| policyGenerationId: started.policyGenerationId, | ||
| policyEngineId: engine.policyEngineId, | ||
| }), | ||
| ); | ||
| const asset = assets.policyGenerationAssets?.[0]; | ||
| // The service returns either plain Cedar or its Dogwood superset member. | ||
| const statement = asset?.definition?.cedar?.statement ?? asset?.definition?.policy?.statement; | ||
| if (!asset || !statement) { | ||
| const findings = (asset?.findings ?? []) | ||
| .map((finding) => `[${finding.type}] ${finding.description}`) | ||
| .join("; "); | ||
| throw new AgentCoreCLIError( | ||
| findings | ||
| ? `the description could not be translated into a Cedar policy: ${findings}` | ||
| : "generation completed but returned no generated policy statement", | ||
| ); | ||
| } | ||
| return { | ||
| statement, | ||
| findings: (asset.findings ?? []).map((finding) => ({ | ||
| type: finding.type ?? "UNKNOWN", | ||
| description: finding.description ?? "", | ||
| })), | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import { afterEach, describe, expect, test } from "bun:test"; | ||
| import { | ||
| GetGatewayCommand, | ||
| GetPolicyGenerationCommand, | ||
| ListGatewaysCommand, | ||
| ListPolicyEngineSummariesCommand, | ||
| ListPolicyGenerationAssetsCommand, | ||
| StartPolicyGenerationCommand, | ||
| } from "@aws-sdk/client-bedrock-agentcore-control"; | ||
| import { PolicyClient } from "../../../../core/policy"; | ||
| import type { AwsClients } from "../../../../core/types"; | ||
| import { createSilentLogger, TestCoreClient } from "../../../../testing"; | ||
| import { createGatewayProjectTestHarness } from "../gateway-test-support"; | ||
|
|
||
| const { addGateway, cleanup, inProject, projectSpec, run } = | ||
| createGatewayProjectTestHarness("policy-generate"); | ||
|
|
||
| afterEach(cleanup); | ||
|
|
||
| /** | ||
| Command-flow tests for `project add policy --generate`, driven through the real | ||
| root handler with the real PolicyClient over a control client mocked at .send(). | ||
| These cover the client edges the TestPolicyClient-backed tests in index.test.ts | ||
| cannot reach: deployed-resource resolution, polling, and asset parsing. | ||
| **/ | ||
|
|
||
| function fakeClients(responses: { | ||
| engines?: unknown; | ||
| gateways?: unknown; | ||
| getGateway?: unknown; | ||
| start?: unknown; | ||
| get?: unknown; | ||
| assets?: unknown; | ||
| }): AwsClients { | ||
| const control = { | ||
| send: async (command: unknown) => { | ||
| if (command instanceof ListPolicyEngineSummariesCommand) return responses.engines; | ||
| if (command instanceof ListGatewaysCommand) return responses.gateways; | ||
| if (command instanceof GetGatewayCommand) return responses.getGateway; | ||
| if (command instanceof StartPolicyGenerationCommand) return responses.start; | ||
| if (command instanceof GetPolicyGenerationCommand) return responses.get; | ||
| if (command instanceof ListPolicyGenerationAssetsCommand) return responses.assets; | ||
| throw new Error(`unexpected command: ${command?.constructor?.name}`); | ||
| }, | ||
| }; | ||
| return { control: () => control } as unknown as AwsClients; | ||
| } | ||
|
|
||
| const CEDAR = "forbid (principal, action, resource is AgentCore::Gateway);"; | ||
|
|
||
| const HAPPY = { | ||
| engines: { | ||
| policyEngines: [{ name: "TestProject_Guardrails", policyEngineId: "pe-abc123" }], | ||
| }, | ||
| gateways: { items: [{ name: "TestProject-tools", gatewayId: "gw-1" }] }, | ||
| getGateway: { gatewayArn: "arn:aws:bedrock-agentcore:us-west-2:1:gateway/gw-1" }, | ||
| start: { policyGenerationId: "gen-1" }, | ||
| get: { status: "GENERATED" }, | ||
| assets: { | ||
| policyGenerationAssets: [ | ||
| { | ||
| definition: { cedar: { statement: CEDAR } }, | ||
| findings: [{ type: "VALID", description: "ok" }], | ||
| }, | ||
| ], | ||
| }, | ||
| }; | ||
|
|
||
| function coreWith(responses: Parameters<typeof fakeClients>[0]) { | ||
| return { | ||
| ...new TestCoreClient(), | ||
| policy: new PolicyClient(fakeClients(responses), createSilentLogger(), 0), | ||
| }; | ||
| } | ||
|
|
||
| async function generate(responses: Parameters<typeof fakeClients>[0]) { | ||
| const projectRoot = await inProject(); | ||
| await run(["add", "policy-engine", "--name", "Guardrails"]); | ||
| await addGateway("tools"); | ||
| const io = await run( | ||
| [ | ||
| "add", | ||
| "policy", | ||
| "--engine", | ||
| "Guardrails", | ||
| "--name", | ||
| "Gen", | ||
| "--generate", | ||
| "forbid everything", | ||
| "--gateway", | ||
| "tools", | ||
| ], | ||
| undefined, | ||
| coreWith(responses), | ||
| ); | ||
| return { projectRoot, io }; | ||
| } | ||
|
|
||
| describe("project add policy --generate against the control plane", () => { | ||
| test("resolves deployed ids, prints the Cedar and findings, writes the spec", async () => { | ||
| const { projectRoot, io } = await generate(HAPPY); | ||
|
|
||
| expect(io.stderr()).toContain(`Generated Cedar policy:\n${CEDAR}`); | ||
| expect(io.stderr()).toContain("finding [VALID]: ok"); | ||
| expect((await projectSpec(projectRoot)).policyEngines[0].policies[0]).toMatchObject({ | ||
| name: "Gen", | ||
| statement: CEDAR, | ||
| }); | ||
| }); | ||
|
|
||
| test("reads a Dogwood policy definition member", async () => { | ||
| const { projectRoot } = await generate({ | ||
| ...HAPPY, | ||
| assets: { policyGenerationAssets: [{ definition: { policy: { statement: CEDAR } } }] }, | ||
| }); | ||
| expect((await projectSpec(projectRoot)).policyEngines[0].policies[0]).toMatchObject({ | ||
| statement: CEDAR, | ||
| }); | ||
| }); | ||
|
|
||
| test.each([ | ||
| ["engine not deployed", { ...HAPPY, engines: { policyEngines: [] } }, "is not deployed"], | ||
| ["gateway not deployed", { ...HAPPY, gateways: { items: [] } }, "not deployed"], | ||
| [ | ||
| "generation failed", | ||
| { ...HAPPY, get: { status: "GENERATE_FAILED", statusReasons: ["bad input"] } }, | ||
| "bad input", | ||
| ], | ||
| ["no assets", { ...HAPPY, assets: { policyGenerationAssets: [] } }, "no generated policy"], | ||
| [ | ||
| "the description is not translatable", | ||
| { | ||
| ...HAPPY, | ||
| assets: { | ||
| policyGenerationAssets: [ | ||
| { | ||
| rawTextFragment: "do the thing", | ||
| findings: [{ type: "INVALID", description: "Non-translatable" }], | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| "could not be translated into a Cedar policy: [INVALID] Non-translatable", | ||
| ], | ||
| [ | ||
| "polling exhausts while still generating", | ||
| { ...HAPPY, get: { status: "GENERATING" } }, | ||
| "may still complete", | ||
| ], | ||
| ])("fails when %s", async (_label, responses, message) => { | ||
| await expect(generate(responses)).rejects.toThrow(message); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PAUSE FOR STANDUP ASK: