Skip to content

Commit 4995bda

Browse files
authored
Initial iteration of platform support for resource access mechanism (#969)
1 parent cfc3bc4 commit 4995bda

File tree

63 files changed

+2553
-660
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+2553
-660
lines changed

.changeset/smooth-tigers-double.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
'@aws-amplify/backend-function': minor
3+
'@aws-amplify/backend-storage': minor
4+
'@aws-amplify/auth-construct-alpha': minor
5+
'@aws-amplify/platform-core': minor
6+
'@aws-amplify/backend-auth': minor
7+
'@aws-amplify/backend-data': minor
8+
'@aws-amplify/plugin-types': minor
9+
'@aws-amplify/backend': minor
10+
'@aws-amplify/backend-cli': minor
11+
---
12+
13+
Introduce initial iteration of access control mechanism between backend resources.
14+
The APIs and functioality are NOT final and are subject to change without notice.

.eslint_dictionary.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
[
2+
"acceptor",
3+
"acceptors",
24
"aggregator",
35
"amazonaws",
46
"amazoncognito",

.prettierignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ expected-cdk-out
1111
.changeset/pre.json
1212
concurrent_workspace_script_cache.json
1313
scripts/components/api-changes-validator/test-resources/working-directory
14-
test-projects
14+
/test-projects
1515
testDir

package-lock.json

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

packages/auth-construct/API.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
55
```ts
66

7-
import { AmplifyFunction } from '@aws-amplify/plugin-types';
87
import { AuthOutput } from '@aws-amplify/backend-output-schemas';
98
import { AuthResources } from '@aws-amplify/plugin-types';
109
import { aws_cognito } from 'aws-cdk-lib';
1110
import { BackendOutputStorageStrategy } from '@aws-amplify/plugin-types';
1211
import { Construct } from 'constructs';
13-
import { IFunction } from 'aws-cdk-lib/aws-lambda';
1412
import { ResourceProvider } from '@aws-amplify/plugin-types';
1513
import { SecretValue } from 'aws-cdk-lib';
1614
import { StandardAttributes } from 'aws-cdk-lib/aws-cognito';
@@ -22,7 +20,6 @@ export type AmazonProviderProps = Omit<aws_cognito.UserPoolIdentityProviderAmazo
2220
// @public
2321
export class AmplifyAuth extends Construct implements ResourceProvider<AuthResources> {
2422
constructor(scope: Construct, id: string, props?: AuthProps);
25-
addTrigger: (event: TriggerEvent, handler: IFunction | AmplifyFunction) => void;
2623
readonly resources: AuthResources;
2724
}
2825

packages/auth-construct/src/construct.test.ts

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { App, SecretValue, Stack } from 'aws-cdk-lib';
44
import { Match, Template } from 'aws-cdk-lib/assertions';
55
import assert from 'node:assert';
66
import {
7-
AmplifyFunction,
87
BackendOutputEntry,
98
BackendOutputStorageStrategy,
109
} from '@aws-amplify/plugin-types';
@@ -16,7 +15,6 @@ import {
1615
UserPoolClient,
1716
} from 'aws-cdk-lib/aws-cognito';
1817
import { authOutputKey } from '@aws-amplify/backend-output-schemas';
19-
import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda';
2018
import { DEFAULTS } from './defaults.js';
2119

2220
const googleClientId = 'googleClientId';
@@ -2423,83 +2421,4 @@ void describe('Auth construct', () => {
24232421
assert.equal(name.startsWith(expectedPrefix), true);
24242422
});
24252423
});
2426-
2427-
void describe('addTrigger', () => {
2428-
void it('attaches lambda function to UserPool Lambda config', () => {
2429-
const app = new App();
2430-
const stack = new Stack(app);
2431-
const testFunc = new Function(stack, 'testFunc', {
2432-
code: Code.fromInline('test code'),
2433-
handler: 'index.handler',
2434-
runtime: Runtime.NODEJS_18_X,
2435-
});
2436-
const authConstruct = new AmplifyAuth(stack, 'testAuth', {
2437-
loginWith: { email: true },
2438-
});
2439-
authConstruct.addTrigger('createAuthChallenge', testFunc);
2440-
const template = Template.fromStack(stack);
2441-
const lambdas = template.findResources('AWS::Lambda::Function');
2442-
if (Object.keys(lambdas).length !== 1) {
2443-
assert.fail(
2444-
'Expected one and only one lambda function in the template'
2445-
);
2446-
}
2447-
const handlerLogicalId = Object.keys(lambdas)[0];
2448-
template.hasResourceProperties('AWS::Cognito::UserPool', {
2449-
LambdaConfig: {
2450-
CreateAuthChallenge: {
2451-
['Fn::GetAtt']: [handlerLogicalId, 'Arn'],
2452-
},
2453-
},
2454-
});
2455-
});
2456-
2457-
void it('attaches AmplifyFunction to UserPool Lambda config', () => {
2458-
const app = new App();
2459-
const stack = new Stack(app);
2460-
const testFunc = new Function(stack, 'testFunc', {
2461-
code: Code.fromInline('test code'),
2462-
handler: 'index.handler',
2463-
runtime: Runtime.NODEJS_18_X,
2464-
});
2465-
const amplifyFuncStub: AmplifyFunction = {
2466-
resources: {
2467-
lambda: testFunc,
2468-
},
2469-
};
2470-
const authConstruct = new AmplifyAuth(stack, 'testAuth', {
2471-
loginWith: { email: true },
2472-
});
2473-
authConstruct.addTrigger('createAuthChallenge', amplifyFuncStub);
2474-
const template = Template.fromStack(stack);
2475-
const lambdas = template.findResources('AWS::Lambda::Function');
2476-
if (Object.keys(lambdas).length !== 1) {
2477-
assert.fail(
2478-
'Expected one and only one lambda function in the template'
2479-
);
2480-
}
2481-
const handlerLogicalId = Object.keys(lambdas)[0];
2482-
template.hasResourceProperties('AWS::Cognito::UserPool', {
2483-
LambdaConfig: {
2484-
CreateAuthChallenge: {
2485-
['Fn::GetAtt']: [handlerLogicalId, 'Arn'],
2486-
},
2487-
},
2488-
});
2489-
});
2490-
2491-
void it('stores attribution data in stack', () => {
2492-
const app = new App();
2493-
const stack = new Stack(app);
2494-
new AmplifyAuth(stack, 'testAuth', {
2495-
loginWith: { email: true },
2496-
});
2497-
2498-
const template = Template.fromStack(stack);
2499-
assert.equal(
2500-
JSON.parse(template.toJSON().Description).stackType,
2501-
'auth-Cognito'
2502-
);
2503-
});
2504-
});
25052424
});

packages/auth-construct/src/construct.ts

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Construct } from 'constructs';
22
import { RemovalPolicy, Stack, aws_cognito as cognito } from 'aws-cdk-lib';
33
import {
4-
AmplifyFunction,
54
AuthResources,
65
BackendOutputStorageStrategy,
76
ResourceProvider,
@@ -24,7 +23,6 @@ import {
2423
UserPoolIdentityProviderOidc,
2524
UserPoolIdentityProviderSaml,
2625
UserPoolIdentityProviderSamlMetadataType,
27-
UserPoolOperation,
2826
UserPoolProps,
2927
} from 'aws-cdk-lib/aws-cognito';
3028
import { FederatedPrincipal, Role } from 'aws-cdk-lib/aws-iam';
@@ -33,10 +31,8 @@ import {
3331
AuthProps,
3432
EmailLoginSettings,
3533
ExternalProviderOptions,
36-
TriggerEvent,
3734
} from './types.js';
3835
import { DEFAULTS } from './defaults.js';
39-
import { IFunction } from 'aws-cdk-lib/aws-lambda';
4036
import {
4137
AttributionMetadataStorage,
4238
StackMetadataBackendOutputStorageStrategy,
@@ -194,26 +190,6 @@ export class AmplifyAuth
194190
);
195191
}
196192

197-
/**
198-
* Attach a Lambda function trigger handler to the UserPool in this construct
199-
* @param event - The trigger event operation
200-
* @param handler - The function that will handle the event
201-
*/
202-
addTrigger = (
203-
event: TriggerEvent,
204-
handler: IFunction | AmplifyFunction
205-
): void => {
206-
if ('resources' in handler) {
207-
this.userPool.addTrigger(
208-
UserPoolOperation.of(event),
209-
handler.resources.lambda
210-
);
211-
} else {
212-
// handler is an IFunction
213-
this.userPool.addTrigger(UserPoolOperation.of(event), handler);
214-
}
215-
};
216-
217193
/**
218194
* Create Auth/UnAuth Roles
219195
* @returns DefaultRoles

packages/backend-auth/API.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
```ts
66

77
import { AmazonProviderProps } from '@aws-amplify/auth-construct-alpha';
8-
import { AmplifyAuth } from '@aws-amplify/auth-construct-alpha';
98
import { AppleProviderProps } from '@aws-amplify/auth-construct-alpha';
109
import { AuthProps } from '@aws-amplify/auth-construct-alpha';
10+
import { AuthResources } from '@aws-amplify/plugin-types';
11+
import { AuthRoleName } from '@aws-amplify/plugin-types';
1112
import { BackendSecret } from '@aws-amplify/plugin-types';
1213
import { ConstructFactory } from '@aws-amplify/plugin-types';
1314
import { ExternalProviderOptions } from '@aws-amplify/auth-construct-alpha';
1415
import { FacebookProviderProps } from '@aws-amplify/auth-construct-alpha';
1516
import { FunctionResources } from '@aws-amplify/plugin-types';
1617
import { GoogleProviderProps } from '@aws-amplify/auth-construct-alpha';
1718
import { OidcProviderProps } from '@aws-amplify/auth-construct-alpha';
19+
import { ResourceAccessAcceptorFactory } from '@aws-amplify/plugin-types';
1820
import { ResourceProvider } from '@aws-amplify/plugin-types';
1921
import { TriggerEvent } from '@aws-amplify/auth-construct-alpha';
2022

@@ -43,8 +45,11 @@ export type AuthLoginWithFactoryProps = Omit<AuthProps['loginWith'], 'externalPr
4345
externalProviders?: ExternalProviderSpecificFactoryProps;
4446
};
4547

48+
// @public (undocumented)
49+
export type BackendAuth = ResourceProvider<AuthResources> & ResourceAccessAcceptorFactory<AuthRoleName>;
50+
4651
// @public
47-
export const defineAuth: (props: AmplifyAuthProps) => ConstructFactory<AmplifyAuth>;
52+
export const defineAuth: (props: AmplifyAuthProps) => ConstructFactory<BackendAuth>;
4853

4954
// @public
5055
export type Expand<T> = T extends infer O ? {

0 commit comments

Comments
 (0)