Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 88
feat: add policy engine and policy support#579
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
33c4b00a80a0050bc9914b0197e0e118d1ec61bfcef5c37ead642bd70486995c9c86ca7bfc44517997d62f90d2bd03d32c62a73a1900810ad0a4df2File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
Large diffs are not rendered by default.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { getCredentialProvider } from './account'; | ||
| import { | ||
| BedrockAgentCoreControlClient, | ||
| GetPolicyGenerationCommand, | ||
| ListPolicyGenerationAssetsCommand, | ||
| StartPolicyGenerationCommand, | ||
| waitUntilPolicyGenerationCompleted, | ||
| } from '@aws-sdk/client-bedrock-agentcore-control'; | ||
| import { WaiterState } from '@smithy/util-waiter'; | ||
| export interface StartPolicyGenerationOptions { | ||
| policyEngineId: string; | ||
| description: string; | ||
| region: string; | ||
| resourceArn: string; | ||
| } | ||
| export interface StartPolicyGenerationResult { | ||
| generationId: string; | ||
| } | ||
| export interface GetPolicyGenerationOptions { | ||
| generationId: string; | ||
| policyEngineId: string; | ||
| region: string; | ||
| } | ||
| export interface GetPolicyGenerationResult { | ||
| status: string; | ||
| statement: string; | ||
| } | ||
| export async function startPolicyGeneration( | ||
| options: StartPolicyGenerationOptions | ||
| ): Promise<StartPolicyGenerationResult> { | ||
| const client = new BedrockAgentCoreControlClient({ | ||
| region: options.region, | ||
| credentials: getCredentialProvider(), | ||
| }); | ||
| const command = new StartPolicyGenerationCommand({ | ||
| policyEngineId: options.policyEngineId, | ||
| resource: { arn: options.resourceArn }, | ||
| content: { | ||
| rawText: options.description, | ||
| }, | ||
| name: `cli_generation_${Date.now()}`, | ||
| }); | ||
| const response = await client.send(command); | ||
| if (!response.policyGenerationId) { | ||
| throw new Error('No generation ID returned from StartPolicyGeneration'); | ||
| } | ||
| return { generationId: response.policyGenerationId }; | ||
| } | ||
| export async function getPolicyGeneration(options: GetPolicyGenerationOptions): Promise<GetPolicyGenerationResult> { | ||
| const client = new BedrockAgentCoreControlClient({ | ||
| region: options.region, | ||
| credentials: getCredentialProvider(), | ||
| }); | ||
| // Use the SDK waiter to poll until generation completes | ||
| const waiterResult = await waitUntilPolicyGenerationCompleted( | ||
| { client, maxWaitTime: 120, minDelay: 2, maxDelay: 5 }, | ||
| { policyGenerationId: options.generationId, policyEngineId: options.policyEngineId } | ||
| ); | ||
| if (waiterResult.state !== WaiterState.SUCCESS) { | ||
| throw new Error( | ||
| `Policy generation did not complete within the timeout period (state: ${waiterResult.state}). ` + | ||
| `Generation ID: ${options.generationId}` | ||
| ); | ||
| } | ||
| // Check the final status | ||
| const getCommand = new GetPolicyGenerationCommand({ | ||
| policyGenerationId: options.generationId, | ||
| policyEngineId: options.policyEngineId, | ||
| }); | ||
| const statusResponse = await client.send(getCommand); | ||
| if (statusResponse.status === 'GENERATE_FAILED') { | ||
| const reasons = statusResponse.statusReasons?.join(', ') ?? 'Unknown reason'; | ||
| throw new Error(`Policy generation failed: ${reasons}`); | ||
| } | ||
| // Fetch the generated assets | ||
| const assetsCommand = new ListPolicyGenerationAssetsCommand({ | ||
| policyGenerationId: options.generationId, | ||
| policyEngineId: options.policyEngineId, | ||
| }); | ||
| const assetsResponse = await client.send(assetsCommand); | ||
| const assets = assetsResponse.policyGenerationAssets ?? []; | ||
| if (assets.length === 0) { | ||
| throw new Error('Policy generation completed but no assets were returned'); | ||
| } | ||
| // Get the Cedar statement from the first asset | ||
| const firstAsset = assets[0]!; | ||
jesseturner21 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| const cedarStatement = firstAsset.definition?.cedar?.statement; | ||
| if (!cedarStatement) { | ||
| throw new Error('Policy generation completed but no Cedar policy statement was found in the assets'); | ||
| } | ||
| return { | ||
| status: statusResponse.status ?? 'GENERATED', | ||
| statement: cedarStatement, | ||
| }; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.