@@ -27,6 +27,86 @@ export async function replaceEnvPlaceholders<A extends { }>(object: A, env: cxap
27
27
} ) ;
28
28
}
29
29
30
+ /**
31
+ * SDK obtained by assuming the lookup role
32
+ * for a given environment
33
+ */
34
+ export interface PreparedSdkWithLookupRoleForEnvironment {
35
+ /**
36
+ * The SDK for the given environment
37
+ */
38
+ readonly sdk : ISDK ;
39
+
40
+ /**
41
+ * The resolved environment for the stack
42
+ * (no more 'unknown-account/unknown-region')
43
+ */
44
+ readonly resolvedEnvironment : cxapi . Environment ;
45
+
46
+ /**
47
+ * Whether or not the assume role was successful.
48
+ * If the assume role was not successful (false)
49
+ * then that means that the 'sdk' returned contains
50
+ * the default credentials (not the assume role credentials)
51
+ */
52
+ readonly didAssumeRole : boolean ;
53
+ }
54
+
55
+ /**
56
+ * Try to use the bootstrap lookupRole. There are two scenarios that are handled here
57
+ * 1. The lookup role may not exist (it was added in bootstrap stack version 7)
58
+ * 2. The lookup role may not have the correct permissions (ReadOnlyAccess was added in
59
+ * bootstrap stack version 8)
60
+ *
61
+ * In the case of 1 (lookup role doesn't exist) `forEnvironment` will either:
62
+ * 1. Return the default credentials if the default credentials are for the stack account
63
+ * 2. Throw an error if the default credentials are not for the stack account.
64
+ *
65
+ * If we successfully assume the lookup role we then proceed to 2 and check whether the bootstrap
66
+ * stack version is valid. If it is not we throw an error which should be handled in the calling
67
+ * function (and fallback to use a different role, etc)
68
+ *
69
+ * If we do not successfully assume the lookup role, but do get back the default credentials
70
+ * then return those and note that we are returning the default credentials. The calling
71
+ * function can then decide to use them or fallback to another role.
72
+ */
73
+ export async function prepareSdkWithLookupRoleFor (
74
+ sdkProvider : SdkProvider ,
75
+ stack : cxapi . CloudFormationStackArtifact ,
76
+ ) : Promise < PreparedSdkWithLookupRoleForEnvironment > {
77
+ const resolvedEnvironment = await sdkProvider . resolveEnvironment ( stack . environment ) ;
78
+
79
+ // Substitute any placeholders with information about the current environment
80
+ const arns = await replaceEnvPlaceholders ( {
81
+ lookupRoleArn : stack . lookupRole ?. arn ,
82
+ } , resolvedEnvironment , sdkProvider ) ;
83
+
84
+ // try to assume the lookup role
85
+ const warningMessage = `Could not assume ${ arns . lookupRoleArn } , proceeding anyway.` ;
86
+ const upgradeMessage = `(To get rid of this warning, please upgrade to bootstrap version >= ${ stack . lookupRole ?. requiresBootstrapStackVersion } )` ;
87
+ try {
88
+ const stackSdk = await sdkProvider . forEnvironment ( resolvedEnvironment , Mode . ForReading , {
89
+ assumeRoleArn : arns . lookupRoleArn ,
90
+ assumeRoleExternalId : stack . lookupRole ?. assumeRoleExternalId ,
91
+ } ) ;
92
+
93
+ // if we succeed in assuming the lookup role, make sure we have the correct bootstrap stack version
94
+ if ( stackSdk . didAssumeRole && stack . lookupRole ?. bootstrapStackVersionSsmParameter && stack . lookupRole . requiresBootstrapStackVersion ) {
95
+ const version = await ToolkitInfo . versionFromSsmParameter ( stackSdk . sdk , stack . lookupRole . bootstrapStackVersionSsmParameter ) ;
96
+ if ( version < stack . lookupRole . requiresBootstrapStackVersion ) {
97
+ throw new Error ( `Bootstrap stack version '${ stack . lookupRole . requiresBootstrapStackVersion } ' is required, found version '${ version } '.` ) ;
98
+ }
99
+ } else if ( ! stackSdk . didAssumeRole ) {
100
+ warning ( upgradeMessage ) ;
101
+ }
102
+ return { ...stackSdk , resolvedEnvironment } ;
103
+ } catch ( e ) {
104
+ debug ( e ) ;
105
+ warning ( warningMessage ) ;
106
+ warning ( upgradeMessage ) ;
107
+ throw ( e ) ;
108
+ }
109
+ }
30
110
31
111
export interface DeployStackOptions {
32
112
/**
@@ -171,31 +251,6 @@ export interface ProvisionerProps {
171
251
sdkProvider : SdkProvider ;
172
252
}
173
253
174
- /**
175
- * SDK obtained by assuming the lookup role
176
- * for a given environment
177
- */
178
- export interface PreparedSdkWithLookupRoleForEnvironment {
179
- /**
180
- * The SDK for the given environment
181
- */
182
- readonly sdk : ISDK ;
183
-
184
- /**
185
- * The resolved environment for the stack
186
- * (no more 'unknown-account/unknown-region')
187
- */
188
- readonly resolvedEnvironment : cxapi . Environment ;
189
-
190
- /**
191
- * Whether or not the assume role was successful.
192
- * If the assume role was not successful (false)
193
- * then that means that the 'sdk' returned contains
194
- * the default credentials (not the assume role credentials)
195
- */
196
- readonly didAssumeRole : boolean ;
197
- }
198
-
199
254
/**
200
255
* SDK obtained by assuming the deploy role
201
256
* for a given environment
@@ -237,7 +292,7 @@ export class CloudFormationDeployments {
237
292
let stackSdk : ISDK | undefined = undefined ;
238
293
// try to assume the lookup role and fallback to the deploy role
239
294
try {
240
- const result = await this . prepareSdkWithLookupRoleFor ( stackArtifact ) ;
295
+ const result = await prepareSdkWithLookupRoleFor ( this . sdkProvider , stackArtifact ) ;
241
296
if ( result . didAssumeRole ) {
242
297
stackSdk = result . sdk ;
243
298
}
@@ -311,59 +366,6 @@ export class CloudFormationDeployments {
311
366
return stack . exists ;
312
367
}
313
368
314
- /**
315
- * Try to use the bootstrap lookupRole. There are two scenarios that are handled here
316
- * 1. The lookup role may not exist (it was added in bootstrap stack version 7)
317
- * 2. The lookup role may not have the correct permissions (ReadOnlyAccess was added in
318
- * bootstrap stack version 8)
319
- *
320
- * In the case of 1 (lookup role doesn't exist) `forEnvironment` will either:
321
- * 1. Return the default credentials if the default credentials are for the stack account
322
- * 2. Throw an error if the default credentials are not for the stack account.
323
- *
324
- * If we successfully assume the lookup role we then proceed to 2 and check whether the bootstrap
325
- * stack version is valid. If it is not we throw an error which should be handled in the calling
326
- * function (and fallback to use a different role, etc)
327
- *
328
- * If we do not successfully assume the lookup role, but do get back the default credentials
329
- * then return those and note that we are returning the default credentials. The calling
330
- * function can then decide to use them or fallback to another role.
331
- */
332
- private async prepareSdkWithLookupRoleFor ( stack : cxapi . CloudFormationStackArtifact ) : Promise < PreparedSdkWithLookupRoleForEnvironment > {
333
- const resolvedEnvironment = await this . sdkProvider . resolveEnvironment ( stack . environment ) ;
334
-
335
- // Substitute any placeholders with information about the current environment
336
- const arns = await replaceEnvPlaceholders ( {
337
- lookupRoleArn : stack . lookupRole ?. arn ,
338
- } , resolvedEnvironment , this . sdkProvider ) ;
339
-
340
- // try to assume the lookup role
341
- const warningMessage = `Could not assume ${ arns . lookupRoleArn } , proceeding anyway.` ;
342
- const upgradeMessage = `(To get rid of this warning, please upgrade to bootstrap version >= ${ stack . lookupRole ?. requiresBootstrapStackVersion } )` ;
343
- try {
344
- const stackSdk = await this . sdkProvider . forEnvironment ( resolvedEnvironment , Mode . ForReading , {
345
- assumeRoleArn : arns . lookupRoleArn ,
346
- assumeRoleExternalId : stack . lookupRole ?. assumeRoleExternalId ,
347
- } ) ;
348
-
349
- // if we succeed in assuming the lookup role, make sure we have the correct bootstrap stack version
350
- if ( stackSdk . didAssumeRole && stack . lookupRole ?. bootstrapStackVersionSsmParameter && stack . lookupRole . requiresBootstrapStackVersion ) {
351
- const version = await ToolkitInfo . versionFromSsmParameter ( stackSdk . sdk , stack . lookupRole . bootstrapStackVersionSsmParameter ) ;
352
- if ( version < stack . lookupRole . requiresBootstrapStackVersion ) {
353
- throw new Error ( `Bootstrap stack version '${ stack . lookupRole . requiresBootstrapStackVersion } ' is required, found version '${ version } '.` ) ;
354
- }
355
- } else if ( ! stackSdk . didAssumeRole ) {
356
- warning ( upgradeMessage ) ;
357
- }
358
- return { ...stackSdk , resolvedEnvironment } ;
359
- } catch ( e ) {
360
- debug ( e ) ;
361
- warning ( warningMessage ) ;
362
- warning ( upgradeMessage ) ;
363
- throw ( e ) ;
364
- }
365
- }
366
-
367
369
/**
368
370
* Get the environment necessary for touching the given stack
369
371
*
0 commit comments