Skip to content

Commit 10d13e4

Browse files
authored
feat(cognito): grant() for user pool (#20285)
Add a `grant()` method in `UserPool`. ---- ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/master/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/master/INTEGRATION_TESTS.md)? * [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent a5e74a9 commit 10d13e4

File tree

3 files changed

+69
-6
lines changed

3 files changed

+69
-6
lines changed

packages/@aws-cdk/aws-cognito/README.md

+16-5
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,17 @@ The default set up for the user pool is configured such that only administrators
7373
to create users. Features such as Multi-factor authentication (MFAs) and Lambda Triggers are not
7474
configured by default.
7575

76+
Use the `grant()` method to add an IAM policy statement associated with the user pool to an
77+
IAM principal's policy.
78+
79+
```ts
80+
const userPool = new cognito.UserPool(this, 'myuserpool');
81+
const role = new iam.Role(this, 'role', {
82+
assumedBy: new iam.ServicePrincipal('foo'),
83+
});
84+
userPool.grant(role, 'cognito-idp:AdminCreateUser');
85+
```
86+
7687
### Sign Up
7788

7889
Users can either be signed up by the app's administrators or can sign themselves up. Once a user has signed up, their
@@ -632,8 +643,8 @@ pool.addClient('app-client', {
632643
});
633644
```
634645

635-
If the identity provider and the app client are created in the same stack, specify the dependency between both constructs to
636-
make sure that the identity provider already exists when the app client will be created. The app client cannot handle the
646+
If the identity provider and the app client are created in the same stack, specify the dependency between both constructs to
647+
make sure that the identity provider already exists when the app client will be created. The app client cannot handle the
637648
dependency to the identity provider automatically because the client does not have access to the provider's construct.
638649

639650
```ts
@@ -668,11 +679,11 @@ pool.addClient('app-client', {
668679
});
669680
```
670681

671-
Clients can (and should) be allowed to read and write relevant user attributes only. Usually every client can be allowed to
682+
Clients can (and should) be allowed to read and write relevant user attributes only. Usually every client can be allowed to
672683
read the `given_name` attribute but not every client should be allowed to set the `email_verified` attribute.
673684
The same criteria applies for both standard and custom attributes, more info is available at
674685
[Attribute Permissions and Scopes](https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#user-pool-settings-attribute-permissions-and-scopes).
675-
The default behaviour is to allow read and write permissions on all attributes. The following code shows how this can be
686+
The default behaviour is to allow read and write permissions on all attributes. The following code shows how this can be
676687
configured for a client.
677688

678689
```ts
@@ -703,7 +714,7 @@ pool.addClient('app-client', {
703714
// ...
704715
enableTokenRevocation: true,
705716
});
706-
```
717+
```
707718

708719
### Resource Servers
709720

packages/@aws-cdk/aws-cognito/lib/user-pool.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IRole, PolicyDocument, PolicyStatement, Role, ServicePrincipal } from '@aws-cdk/aws-iam';
1+
import { Grant, IGrantable, IRole, PolicyDocument, PolicyStatement, Role, ServicePrincipal } from '@aws-cdk/aws-iam';
22
import { IKey } from '@aws-cdk/aws-kms';
33
import * as lambda from '@aws-cdk/aws-lambda';
44
import { ArnFormat, Duration, IResource, Lazy, Names, RemovalPolicy, Resource, Stack, Token } from '@aws-cdk/core';
@@ -734,6 +734,19 @@ abstract class UserPoolBase extends Resource implements IUserPool {
734734
public registerIdentityProvider(provider: IUserPoolIdentityProvider) {
735735
this.identityProviders.push(provider);
736736
}
737+
738+
/**
739+
* Adds an IAM policy statement associated with this user pool to an
740+
* IAM principal's policy.
741+
*/
742+
public grant(grantee: IGrantable, ...actions: string[]): Grant {
743+
return Grant.addToPrincipal({
744+
grantee,
745+
actions,
746+
resourceArns: [this.userPoolArn],
747+
scope: this,
748+
});
749+
}
737750
}
738751

739752
/**

packages/@aws-cdk/aws-cognito/test/user-pool.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,45 @@ test('device tracking is configured correctly', () => {
18211821
});
18221822
});
18231823

1824+
test('grant', () => {
1825+
// GIVEN
1826+
const stack = new Stack();
1827+
const role = new Role(stack, 'Role', {
1828+
assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
1829+
});
1830+
1831+
// WHEN
1832+
const userPool = new UserPool(stack, 'Pool');
1833+
userPool.grant(role, 'cognito-idp:AdminCreateUser', 'cognito-idp:ListUsers');
1834+
1835+
// THEN
1836+
Template.fromStack(stack).hasResourceProperties('AWS::IAM::Policy', {
1837+
PolicyDocument: {
1838+
Statement: [
1839+
{
1840+
Action: [
1841+
'cognito-idp:AdminCreateUser',
1842+
'cognito-idp:ListUsers',
1843+
],
1844+
Effect: 'Allow',
1845+
Resource: {
1846+
'Fn::GetAtt': [
1847+
'PoolD3F588B8',
1848+
'Arn',
1849+
],
1850+
},
1851+
},
1852+
],
1853+
Version: '2012-10-17',
1854+
},
1855+
Roles: [
1856+
{
1857+
Ref: 'Role1ABCC5F0',
1858+
},
1859+
],
1860+
});
1861+
1862+
});
18241863

18251864
function fooFunction(scope: Construct, name: string): lambda.IFunction {
18261865
return new lambda.Function(scope, name, {

0 commit comments

Comments
 (0)