Skip to content

Commit da5a305

Browse files
authored
fix(cli): warning to upgrade to bootstrap version >= undefined (#18489)
When we try to assume the lookup role and fail, we print out a warning telling the user to upgrade to the required bootstrap stack version. In cases where the user is not using the default stack synthesizer (legacy, custom, etc.) the cli might not have information on the lookup role. In those cases where we don't have the necessary information on the ARN of the lookup role or the required bootstrap version, we will not print any warnings. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 2415a80 commit da5a305

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

Diff for: packages/aws-cdk/lib/api/cloudformation-deployments.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,20 @@ export async function prepareSdkWithLookupRoleFor(
9696
if (version < stack.lookupRole.requiresBootstrapStackVersion) {
9797
throw new Error(`Bootstrap stack version '${stack.lookupRole.requiresBootstrapStackVersion}' is required, found version '${version}'.`);
9898
}
99-
} else if (!stackSdk.didAssumeRole) {
99+
// we may not have assumed the lookup role because one was not provided
100+
// if that is the case then don't print the upgrade warning
101+
} else if (!stackSdk.didAssumeRole && stack.lookupRole?.requiresBootstrapStackVersion) {
100102
warning(upgradeMessage);
101103
}
102104
return { ...stackSdk, resolvedEnvironment };
103105
} catch (e) {
104106
debug(e);
105-
warning(warningMessage);
106-
warning(upgradeMessage);
107+
// only print out the warnings if the lookupRole exists AND there is a required
108+
// bootstrap version, otherwise the warnings will print `undefined`
109+
if (stack.lookupRole && stack.lookupRole.requiresBootstrapStackVersion) {
110+
warning(warningMessage);
111+
warning(upgradeMessage);
112+
}
107113
throw (e);
108114
}
109115
}

Diff for: packages/aws-cdk/test/cdk-toolkit.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ describe('readCurrentTemplate', () => {
127127
},
128128
},
129129
},
130+
{
131+
stackName: 'Test-Stack-A',
132+
template,
133+
properties: {
134+
assumeRoleArn: 'bloop:${AWS::Region}:${AWS::AccountId}',
135+
},
136+
},
130137
],
131138
});
132139
mockForEnvironment = jest.fn().mockImplementation(() => { return { sdk: mockCloudExecutable.sdkProvider.sdk, didAssumeRole: true }; });
@@ -145,6 +152,11 @@ describe('readCurrentTemplate', () => {
145152
StackStatus: 'CREATE_COMPLETE',
146153
CreationTime: new Date(),
147154
},
155+
{
156+
StackName: 'Test-Stack-A',
157+
StackStatus: 'CREATE_COMPLETE',
158+
CreationTime: new Date(),
159+
},
148160
],
149161
};
150162
},
@@ -329,6 +341,38 @@ describe('readCurrentTemplate', () => {
329341
assumeRoleArn: 'bloop:here:123456789012',
330342
});
331343
});
344+
345+
test('do not print warnings if lookup role not provided in stack artifact', async () => {
346+
// GIVEN
347+
mockCloudExecutable.sdkProvider.stubSSM({
348+
getParameter() {
349+
return {};
350+
},
351+
});
352+
const cdkToolkit = new CdkToolkit({
353+
cloudExecutable: mockCloudExecutable,
354+
configuration: mockCloudExecutable.configuration,
355+
sdkProvider: mockCloudExecutable.sdkProvider,
356+
cloudFormation: new CloudFormationDeployments({ sdkProvider: mockCloudExecutable.sdkProvider }),
357+
});
358+
359+
// WHEN
360+
await cdkToolkit.deploy({
361+
selector: { patterns: ['Test-Stack-A'] },
362+
});
363+
364+
// THEN
365+
expect(flatten(stderrMock.mock.calls)).not.toEqual(expect.arrayContaining([
366+
expect.stringMatching(/Could not assume/),
367+
expect.stringMatching(/please upgrade to bootstrap version/),
368+
]));
369+
expect(mockCloudExecutable.sdkProvider.sdk.ssm).not.toHaveBeenCalled();
370+
expect(mockForEnvironment.mock.calls.length).toEqual(2);
371+
expect(mockForEnvironment.mock.calls[0][2]).toEqual({
372+
assumeRoleArn: undefined,
373+
assumeRoleExternalId: undefined,
374+
});
375+
});
332376
});
333377

334378
describe('deploy', () => {

0 commit comments

Comments
 (0)