Skip to content

Commit a38afc9

Browse files
authored
feat(bedrock-agentcore): use IUserPool and IUserPoolClient interfaces instead of string identifiers (#35860)
### Issue # (if applicable) Closes #35854 . ### Reason for this change Improve the Cognito authorizer configuration by accepting Cognito construct references instead of string identifiers, providing better type safety and integration with the CDK ecosystem. Additionally, add support for multi-clients. ### Description of changes - Enhanced `RuntimeAuthorizerConfiguration.usingCognito()` to accept `IUserPool` and `IUserPoolClient` constructs instead of string parameters - Added support for multiple Cognito clients through an array parameter The implementation now provides better CDK integration and type safety by using construct references rather than raw string identifiers. ### Describe any new or updated permissions being added N/A ### Description of how you validated changes Add unit tests and integ test. BREAKING CHANGE: The signature of `RuntimeAuthorizerConfiguration.usingCognito()` has changed to accept IUserPool and IUserPoolClient constructs instead of string parameters, and now supports multiple clients. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 27ffd00 commit a38afc9

File tree

16 files changed

+1685
-27
lines changed

16 files changed

+1685
-27
lines changed

packages/@aws-cdk/aws-bedrock-agentcore-alpha/README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ IAM authentication is the default mode, when no authorizerConfiguration is set t
303303
To configure AWS Cognito User Pool authentication:
304304

305305
```typescript
306+
declare const userPool: cognito.UserPool;
307+
declare const userPoolClient: cognito.UserPoolClient;
308+
declare const anotherUserPoolClient: cognito.UserPoolClient;
309+
306310
const repository = new ecr.Repository(this, "TestRepository", {
307311
repositoryName: "test-agent-runtime",
308312
});
@@ -312,9 +316,8 @@ const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
312316
runtimeName: "myAgent",
313317
agentRuntimeArtifact: agentRuntimeArtifact,
314318
authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingCognito(
315-
"us-west-2_ABC123", // User Pool ID (required)
316-
"client123", // Client ID (required)
317-
"us-west-2" // Region (optional, defaults to stack region)
319+
userPool, // User Pool (required)
320+
[userPoolClient, anotherUserPoolClient], // User Pool Clients
318321
),
319322
});
320323
```

packages/@aws-cdk/aws-bedrock-agentcore-alpha/agentcore/runtime/runtime-authorizer-configuration.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
* and limitations under the License.
1212
*/
1313

14-
import { Token } from 'aws-cdk-lib';
1514
import { CfnRuntime } from 'aws-cdk-lib/aws-bedrockagentcore';
1615
import { ValidationError } from './validation-helpers';
16+
import { IUserPool, IUserPoolClient } from 'aws-cdk-lib/aws-cognito';
1717

1818
/**
1919
* Abstract base class for runtime authorizer configurations.
@@ -54,19 +54,17 @@ export abstract class RuntimeAuthorizerConfiguration {
5454
* Use AWS Cognito User Pool authentication.
5555
* Validates Cognito-issued JWT tokens.
5656
*
57-
* @param userPoolId The Cognito User Pool ID (e.g., 'us-west-2_ABC123')
58-
* @param clientId The Cognito App Client ID
59-
* @param region Optional AWS region where the User Pool is located (defaults to stack region)
57+
* @param userPool The Cognito User Pool
58+
* @param userPoolClient The Cognito User Pool App Clients
6059
* @param allowedAudience Optional array of allowed audiences
6160
* @returns RuntimeAuthorizerConfiguration for Cognito authentication
6261
*/
6362
public static usingCognito(
64-
userPoolId: string,
65-
clientId: string,
66-
region?: string,
63+
userPool: IUserPool,
64+
userPoolClients: IUserPoolClient[],
6765
allowedAudience?: string[],
6866
): RuntimeAuthorizerConfiguration {
69-
return new CognitoAuthorizerConfiguration(userPoolId, clientId, region, allowedAudience);
67+
return new CognitoAuthorizerConfiguration(userPool, userPoolClients, allowedAudience);
7068
}
7169

7270
/**
@@ -134,25 +132,21 @@ class JwtAuthorizerConfiguration extends RuntimeAuthorizerConfiguration {
134132
*/
135133
class CognitoAuthorizerConfiguration extends RuntimeAuthorizerConfiguration {
136134
constructor(
137-
private readonly userPoolId: string,
138-
private readonly clientId: string,
139-
private readonly region?: string,
135+
private readonly userPool: IUserPool,
136+
private readonly userPoolClients: IUserPoolClient[],
140137
private readonly allowedAudience?: string[],
141138
) {
142139
super();
143140
}
144141

145142
public _render(): CfnRuntime.AuthorizerConfigurationProperty {
146-
// If region is not provided, use a token that will be resolved to the stack region
147-
// This will be resolved during synthesis
148-
const region = this.region ?? Token.asString({ Ref: 'AWS::Region' });
149-
const discoveryUrl = `https://cognito-idp.${region}.amazonaws.com/${this.userPoolId}/.well-known/openid-configuration`;
143+
const discoveryUrl = `https://cognito-idp.${this.userPool.env.region}.amazonaws.com/${this.userPool.userPoolId}/.well-known/openid-configuration`;
150144

151145
// Use JWT format for Cognito (CloudFormation expects JWT format)
152146
return {
153147
customJwtAuthorizer: {
154148
discoveryUrl: discoveryUrl,
155-
allowedClients: [this.clientId],
149+
allowedClients: this.userPoolClients.map(client => client.userPoolClientId),
156150
allowedAudience: this.allowedAudience,
157151
},
158152
};

packages/@aws-cdk/aws-bedrock-agentcore-alpha/rosetta/default.ts-fixture

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Construct } from 'constructs';
44
import { Stack } from 'aws-cdk-lib';
55
import { Duration, RemovalPolicy, aws_s3_deployment } from 'aws-cdk-lib';
66
import * as agentcore from '@aws-cdk/aws-bedrock-agentcore-alpha';
7+
import * as cognito from 'aws-cdk-lib/aws-cognito';
78
import * as iam from 'aws-cdk-lib/aws-iam';
89
import * as s3 from 'aws-cdk-lib/aws-s3';
910
import * as sns from 'aws-cdk-lib/aws-sns';

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-cognito.js.snapshot/BedrockAgentCoreRuntimeCognitoTestDefaultTestDeployAssert362339C9.assets.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-cognito.js.snapshot/BedrockAgentCoreRuntimeCognitoTestDefaultTestDeployAssert362339C9.template.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-cognito.js.snapshot/asset.f06c9f54828243752afd2df4e39ab9d2987b5ccf44e6bdc05621c18d5488f240/Dockerfile

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-cognito.js.snapshot/asset.f06c9f54828243752afd2df4e39ab9d2987b5ccf44e6bdc05621c18d5488f240/app.py

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-cognito.js.snapshot/asset.f06c9f54828243752afd2df4e39ab9d2987b5ccf44e6bdc05621c18d5488f240/requirements.txt

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/integ.runtime-cognito.js.snapshot/aws-cdk-bedrock-agentcore-runtime-cognito.assets.json

Lines changed: 34 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)