|
| 1 | +import { beforeEach, describe, it, mock } from 'node:test'; |
| 2 | +import { App, Stack } from 'aws-cdk-lib'; |
| 3 | +import { |
| 4 | + BackendOutputEntry, |
| 5 | + BackendOutputStorageStrategy, |
| 6 | + ConstructContainer, |
| 7 | + ConstructFactoryGetInstanceProps, |
| 8 | + ImportPathVerifier, |
| 9 | +} from '@aws-amplify/plugin-types'; |
| 10 | +import { |
| 11 | + ConstructContainerStub, |
| 12 | + ImportPathVerifierStub, |
| 13 | + StackResolverStub, |
| 14 | +} from '@aws-amplify/backend-platform-test-stubs'; |
| 15 | +import { StackMetadataBackendOutputStorageStrategy } from '@aws-amplify/backend-output-storage'; |
| 16 | +import { UserPool } from 'aws-cdk-lib/aws-cognito'; |
| 17 | +import { AuthAccessPolicyArbiter } from './auth_access_policy_arbiter.js'; |
| 18 | +import assert from 'node:assert'; |
| 19 | +import { UserPoolAccessPolicyFactory } from './userpool_access_policy_factory.js'; |
| 20 | + |
| 21 | +void describe('AuthAccessPolicyArbiter', () => { |
| 22 | + void describe('arbitratePolicies', () => { |
| 23 | + let stack: Stack; |
| 24 | + let constructContainer: ConstructContainer; |
| 25 | + let outputStorageStrategy: BackendOutputStorageStrategy<BackendOutputEntry>; |
| 26 | + let importPathVerifier: ImportPathVerifier; |
| 27 | + let getInstanceProps: ConstructFactoryGetInstanceProps; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + stack = createStackAndSetContext(); |
| 31 | + |
| 32 | + constructContainer = new ConstructContainerStub( |
| 33 | + new StackResolverStub(stack) |
| 34 | + ); |
| 35 | + |
| 36 | + outputStorageStrategy = new StackMetadataBackendOutputStorageStrategy( |
| 37 | + stack |
| 38 | + ); |
| 39 | + |
| 40 | + importPathVerifier = new ImportPathVerifierStub(); |
| 41 | + |
| 42 | + getInstanceProps = { |
| 43 | + constructContainer, |
| 44 | + outputStorageStrategy, |
| 45 | + importPathVerifier, |
| 46 | + }; |
| 47 | + }); |
| 48 | + |
| 49 | + void it('passes expected policy and ssm context to resource access acceptor', () => { |
| 50 | + const userpool = new UserPool(stack, 'testUserPool'); |
| 51 | + const acceptResourceAccessMock = mock.fn(); |
| 52 | + const authAccessPolicyArbiter = new AuthAccessPolicyArbiter( |
| 53 | + [ |
| 54 | + { |
| 55 | + actions: ['manageUsers'], |
| 56 | + getResourceAccessAcceptor: () => ({ |
| 57 | + identifier: 'testResourceAccessAcceptor', |
| 58 | + acceptResourceAccess: acceptResourceAccessMock, |
| 59 | + }), |
| 60 | + }, |
| 61 | + { |
| 62 | + actions: ['deleteUser', 'disableUser', 'deleteUserAttributes'], |
| 63 | + getResourceAccessAcceptor: () => ({ |
| 64 | + identifier: 'testResourceAccessAcceptor', |
| 65 | + acceptResourceAccess: acceptResourceAccessMock, |
| 66 | + }), |
| 67 | + }, |
| 68 | + ], |
| 69 | + getInstanceProps, |
| 70 | + [{ name: 'TEST_USERPOOL_ID', path: 'test/ssm/path/to/userpool/id' }], |
| 71 | + new UserPoolAccessPolicyFactory(userpool) |
| 72 | + ); |
| 73 | + |
| 74 | + authAccessPolicyArbiter.arbitratePolicies(); |
| 75 | + assert.equal(acceptResourceAccessMock.mock.callCount(), 2); |
| 76 | + assert.deepStrictEqual( |
| 77 | + acceptResourceAccessMock.mock.calls[0].arguments[0].document.toJSON(), |
| 78 | + { |
| 79 | + Statement: [ |
| 80 | + { |
| 81 | + Action: [ |
| 82 | + 'cognito-idp:AdminConfirmSignUp', |
| 83 | + 'cognito-idp:AdminCreateUser', |
| 84 | + 'cognito-idp:AdminDeleteUser', |
| 85 | + 'cognito-idp:AdminDeleteUserAttributes', |
| 86 | + 'cognito-idp:AdminDisableUser', |
| 87 | + 'cognito-idp:AdminEnableUser', |
| 88 | + 'cognito-idp:AdminGetUser', |
| 89 | + 'cognito-idp:AdminListGroupsForUser', |
| 90 | + 'cognito-idp:AdminRespondToAuthChallenge', |
| 91 | + 'cognito-idp:AdminSetUserMFAPreference', |
| 92 | + 'cognito-idp:AdminSetUserSettings', |
| 93 | + 'cognito-idp:AdminUpdateUserAttributes', |
| 94 | + 'cognito-idp:AdminUserGlobalSignOut', |
| 95 | + ], |
| 96 | + Effect: 'Allow', |
| 97 | + Resource: `${userpool.userPoolArn}`, |
| 98 | + }, |
| 99 | + ], |
| 100 | + Version: '2012-10-17', |
| 101 | + } |
| 102 | + ); |
| 103 | + assert.deepStrictEqual( |
| 104 | + acceptResourceAccessMock.mock.calls[1].arguments[0].document.toJSON(), |
| 105 | + { |
| 106 | + Statement: [ |
| 107 | + { |
| 108 | + Action: [ |
| 109 | + 'cognito-idp:AdminDeleteUser', |
| 110 | + 'cognito-idp:AdminDisableUser', |
| 111 | + 'cognito-idp:AdminDeleteUserAttributes', |
| 112 | + ], |
| 113 | + Effect: 'Allow', |
| 114 | + Resource: `${userpool.userPoolArn}`, |
| 115 | + }, |
| 116 | + ], |
| 117 | + Version: '2012-10-17', |
| 118 | + } |
| 119 | + ); |
| 120 | + assert.deepStrictEqual( |
| 121 | + acceptResourceAccessMock.mock.calls[0].arguments[1], |
| 122 | + [{ name: 'TEST_USERPOOL_ID', path: 'test/ssm/path/to/userpool/id' }] |
| 123 | + ); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
| 127 | + |
| 128 | +const createStackAndSetContext = (): Stack => { |
| 129 | + const app = new App(); |
| 130 | + const stack = new Stack(app); |
| 131 | + return stack; |
| 132 | +}; |
0 commit comments