@@ -7,9 +7,13 @@ import {
7
7
CognitoIdentityProviderClient ,
8
8
ListUsersCommand ,
9
9
} from '@aws-sdk/client-cognito-identity-provider' ;
10
- import { /*GetRoleCommand,*/ IAMClient } from '@aws-sdk/client-iam' ;
10
+ import {
11
+ AttachRolePolicyCommand ,
12
+ CreatePolicyCommand ,
13
+ IAMClient ,
14
+ } from '@aws-sdk/client-iam' ;
11
15
import { DeployedResourcesFinder } from '../find_deployed_resource.js' ;
12
- import fs from 'fs/promises' ;
16
+ import fsp from 'fs/promises' ;
13
17
import assert from 'node:assert' ;
14
18
import { createEmptyAmplifyProject } from './create_empty_amplify_project.js' ;
15
19
import { BackendIdentifier } from '@aws-amplify/plugin-types' ;
@@ -21,9 +25,12 @@ import {
21
25
HttpLink ,
22
26
InMemoryCache ,
23
27
gql ,
24
- } from '@apollo/client' ;
28
+ } from '@apollo/client/core ' ;
25
29
import { AUTH_TYPE , createAuthLink } from 'aws-appsync-auth-link' ;
26
30
import { AmplifyAuthCredentialsFactory } from '../amplify_auth_credentials_factory.js' ;
31
+ import { execa , execaSync } from 'execa' ;
32
+ import { AssumeRoleCommand , STSClient } from '@aws-sdk/client-sts' ;
33
+ import { shortUuid } from '../short_uuid.js' ;
27
34
28
35
/**
29
36
* Creates test project for seed
@@ -47,7 +54,10 @@ export class SeedTestProjectCreator implements TestProjectCreator {
47
54
private readonly iamClient : IAMClient = new IAMClient (
48
55
e2eToolingClientConfig
49
56
) ,
50
- private readonly resourceFinder : DeployedResourcesFinder = new DeployedResourcesFinder ( )
57
+ private readonly resourceFinder : DeployedResourcesFinder = new DeployedResourcesFinder ( ) ,
58
+ private readonly stsClient : STSClient = new STSClient (
59
+ e2eToolingClientConfig
60
+ )
51
61
) { }
52
62
53
63
createProject = async ( e2eProjectDir : string ) : Promise < TestProjectBase > => {
@@ -62,9 +72,10 @@ export class SeedTestProjectCreator implements TestProjectCreator {
62
72
this . amplifyClient ,
63
73
this . cognitoIdentityProviderClient ,
64
74
this . iamClient ,
65
- this . resourceFinder
75
+ this . resourceFinder ,
76
+ this . stsClient
66
77
) ;
67
- await fs . cp (
78
+ await fsp . cp (
68
79
project . sourceProjectAmplifyDirURL ,
69
80
project . projectAmplifyDirPath ,
70
81
{
@@ -76,7 +87,7 @@ export class SeedTestProjectCreator implements TestProjectCreator {
76
87
}
77
88
78
89
class SeedTestProject extends TestProjectBase {
79
- readonly sourceProjectDirPath = '../../src/test-projects/seed' ;
90
+ readonly sourceProjectDirPath = '../../src/test-projects/seed-test-project ' ;
80
91
81
92
readonly sourceProjectAmplifyDirSuffix = `${ this . sourceProjectDirPath } /amplify` ;
82
93
@@ -93,7 +104,8 @@ class SeedTestProject extends TestProjectBase {
93
104
amplifyClient : AmplifyClient ,
94
105
private readonly cognitoIdentityProviderClient : CognitoIdentityProviderClient ,
95
106
private readonly iamClient : IAMClient ,
96
- private readonly resourceFinder : DeployedResourcesFinder
107
+ private readonly resourceFinder : DeployedResourcesFinder ,
108
+ private readonly stsClient : STSClient
97
109
) {
98
110
super (
99
111
name ,
@@ -109,19 +121,56 @@ class SeedTestProject extends TestProjectBase {
109
121
environment ?: Record < string , string >
110
122
) {
111
123
await super . deploy ( backendIdentifier , environment ) ;
112
- await ampxCli ( [ 'sandbox' , 'seed' , 'generate-policy' ] , this . projectDirPath , {
113
- env : environment ,
114
- } ) . run ( ) ;
115
- //await this.attachToRole(backendIdentifier);
124
+
125
+ console . log ( 'Executing seed policy command' ) ;
126
+ const command = execaSync ( 'npx' , [ 'which' , 'ampx' ] , {
127
+ cwd : this . projectDirPath ,
128
+ } ) . stdout . trim ( ) ;
129
+ const seedPolicyProcess = await execa (
130
+ command ,
131
+ [ 'sandbox' , 'seed' , 'generate-policy' ] ,
132
+ {
133
+ cwd : this . projectDirPath ,
134
+ env : environment ,
135
+ }
136
+ ) ;
137
+ await this . attachToRole ( seedPolicyProcess . stdout , backendIdentifier ) ;
138
+
139
+ console . log ( seedPolicyProcess . stdout ) ;
140
+ const clientConfig = await generateClientConfig ( backendIdentifier , '1.3' ) ;
141
+ if ( ! clientConfig . custom ) {
142
+ throw new Error ( 'Client config missing custom section' ) ;
143
+ }
144
+ const seedRoleArn = clientConfig . custom . seedRoleArn as string ;
145
+
146
+ const seedCredentials = await this . stsClient . send (
147
+ new AssumeRoleCommand ( {
148
+ RoleArn : seedRoleArn ,
149
+ RoleSessionName : `seedSession` ,
150
+ } )
151
+ ) ;
152
+
153
+ assert . ok ( seedCredentials . Credentials ) ;
154
+ assert . ok ( seedCredentials . Credentials . AccessKeyId ) ;
155
+ assert . ok ( seedCredentials . Credentials . SessionToken ) ;
156
+ assert . ok ( seedCredentials . Credentials . SecretAccessKey ) ;
157
+
158
+ console . log ( 'executing seed command' ) ;
116
159
await ampxCli ( [ 'sandbox' , 'seed' ] , this . projectDirPath , {
117
- env : environment ,
160
+ env : {
161
+ AWS_ACCESS_KEY_ID : seedCredentials . Credentials ! . AccessKeyId ,
162
+ AWS_SECRET_ACCESS_KEY : seedCredentials . Credentials ! . SecretAccessKey ,
163
+ AWS_SESSION_TOKEN : seedCredentials . Credentials ! . SessionToken ,
164
+ ...environment ,
165
+ } ,
118
166
} ) . run ( ) ;
119
167
}
120
168
121
169
override async assertPostDeployment (
122
170
backendId : BackendIdentifier
123
171
) : Promise < void > {
124
172
await super . assertPostDeployment ( backendId ) ;
173
+ const testUsername = '[email protected] ' ;
125
174
const clientConfig = await generateClientConfig ( backendId , '1.3' ) ;
126
175
127
176
const cognitoId = await this . resourceFinder . findByBackendIdentifier (
@@ -131,11 +180,20 @@ class SeedTestProject extends TestProjectBase {
131
180
) ;
132
181
133
182
const users = await this . cognitoIdentityProviderClient . send (
134
- new ListUsersCommand ( { UserPoolId : cognitoId [ 0 ] } )
183
+ new ListUsersCommand ( {
184
+ UserPoolId : cognitoId [ 0 ] ,
185
+ Filter : '"email"^="testUser"' ,
186
+ AttributesToGet : [ 'email' ] ,
187
+ } )
135
188
) ;
136
189
190
+ if ( users . Users && users . Users . length < 1 ) {
191
+ throw new Error ( 'Users are missing' ) ;
192
+ }
193
+
137
194
assert . ok ( users . Users ) ;
138
- assert . strictEqual ( users . Users [ 0 ] . Username , '[email protected] ' ) ;
195
+ assert . ok ( users . Users [ 0 ] . Attributes ) ;
196
+ assert . strictEqual ( users . Users [ 0 ] . Attributes [ 0 ] ! . Value , testUsername ) ;
139
197
140
198
if ( ! clientConfig . auth ) {
141
199
throw new Error ( 'Client config missing auth section' ) ;
@@ -183,13 +241,29 @@ class SeedTestProject extends TestProjectBase {
183
241
} ) ;
184
242
185
243
assert . strictEqual (
186
- content . data . content ,
187
- 'Todo list item for [email protected] '
244
+ content . data . listTodos . items [ 0 ] . content ,
245
+ `Todo list item for ${ testUsername } `
246
+ ) ;
247
+ }
248
+
249
+ async attachToRole ( policyString : string , backendId : BackendIdentifier ) {
250
+ const policy = await this . iamClient . send (
251
+ new CreatePolicyCommand ( {
252
+ PolicyName : `seedPolicy_${ shortUuid ( ) } ` ,
253
+ PolicyDocument : policyString ,
254
+ } )
255
+ ) ;
256
+
257
+ const clientConfig = await generateClientConfig ( backendId , '1.3' ) ;
258
+ if ( ! clientConfig . custom ) {
259
+ throw new Error ( 'Client config missing custom section' ) ;
260
+ }
261
+
262
+ await this . iamClient . send (
263
+ new AttachRolePolicyCommand ( {
264
+ RoleName : clientConfig . custom . seedRoleName as string ,
265
+ PolicyArn : policy . Policy ?. Arn ,
266
+ } )
188
267
) ;
189
268
}
190
- /*
191
- async attachToRole(backendId: BackendIdentifier) {
192
- // somehow need to get the deployment role to add the policy to it
193
- const role = (await this.iamClient.send(new GetRoleCommand({ RoleName: 'SeedRole'})));
194
- }*/
195
269
}
0 commit comments