Skip to content

Commit 366e284

Browse files
kaizenccgithub-actions
andauthored
feat(toolkit-lib): deploy prompt prints security diff and returns template diff (#437)
closes aws/aws-cdk#33435 Does the following: - sends the formatted security diff in the request response - returns the full diff to in the request response --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --------- Signed-off-by: github-actions <github-actions@github.com> Co-authored-by: github-actions <github-actions@github.com>
1 parent fdad86c commit 366e284

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

packages/@aws-cdk/toolkit-lib/lib/payloads/deploy.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { TemplateDiff } from '@aws-cdk/cloudformation-diff';
12
import type { CloudFormationStackArtifact } from '@aws-cdk/cx-api';
23
import type { IManifestEntry } from 'cdk-assets';
34
import type { PermissionChangeType } from './diff';
@@ -32,6 +33,11 @@ export interface DeployConfirmationRequest extends ConfirmationRequest {
3233
* The type of change being made to the IAM permissions.
3334
*/
3435
readonly permissionChangeType: PermissionChangeType;
36+
37+
/**
38+
* The template diffs of the stack
39+
*/
40+
readonly templateDiffs: { [name: string]: TemplateDiff };
3541
}
3642

3743
export interface BuildAsset {

packages/@aws-cdk/toolkit-lib/lib/toolkit/toolkit.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -514,13 +514,17 @@ export class Toolkit extends CloudAssemblySourceBuilder {
514514
});
515515

516516
const securityDiff = formatter.formatSecurityDiff();
517-
const permissionChangeType = securityDiff.permissionChangeType;
517+
518+
// Send a request response with the formatted security diff as part of the message,
519+
// and the template diff as data
520+
// (IoHost decides whether to print depending on permissionChangeType)
518521
const deployMotivation = '"--require-approval" is enabled and stack includes security-sensitive updates.';
519-
const deployQuestion = `${deployMotivation}\nDo you wish to deploy these changes`;
522+
const deployQuestion = `${securityDiff.formattedDiff}\n\n${deployMotivation}\nDo you wish to deploy these changes`;
520523
const deployConfirmed = await ioHelper.requestResponse(IO.CDK_TOOLKIT_I5060.req(deployQuestion, {
521524
motivation: deployMotivation,
522525
concurrency,
523-
permissionChangeType,
526+
permissionChangeType: securityDiff.permissionChangeType,
527+
templateDiffs: formatter.diffs,
524528
}));
525529
if (!deployConfirmed) {
526530
throw new ToolkitError('Aborted by user');

packages/@aws-cdk/toolkit-lib/test/actions/deploy.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ describe('deploy', () => {
5555
await toolkit.deploy(cx);
5656

5757
// THEN
58+
const request = ioHost.requestSpy.mock.calls[0][0].message.replace(/\x1B\[[0-?]*[ -/]*[@-~]/g, '');
59+
60+
// Message includes formatted security diff
61+
expect(request).toContain(`Stack Stack1
62+
IAM Statement Changes
63+
┌───┬─────────────┬────────┬────────────────┬───────────┬───────────┐
64+
│ │ Resource │ Effect │ Action │ Principal │ Condition │
65+
├───┼─────────────┼────────┼────────────────┼───────────┼───────────┤
66+
│ + │ $\{Role.Arn\} │ Allow │ sts:AssumeRole │ AWS:arn │ │
67+
└───┴─────────────┴────────┴────────────────┴───────────┴───────────┘
68+
`);
69+
// Request response returns template diff
5870
expect(ioHost.requestSpy).toHaveBeenCalledWith(expect.objectContaining({
5971
action: 'deploy',
6072
level: 'info',
@@ -63,6 +75,20 @@ describe('deploy', () => {
6375
data: expect.objectContaining({
6476
motivation: expect.stringContaining('stack includes security-sensitive updates.'),
6577
permissionChangeType: 'broadening',
78+
templateDiffs: expect.objectContaining({
79+
Stack1: expect.objectContaining({
80+
resources: expect.objectContaining({
81+
diffs: expect.objectContaining({
82+
Role1ABCC5F0: expect.objectContaining({
83+
newValue: expect.objectContaining({
84+
Type: 'AWS::IAM::Role',
85+
Properties: expect.anything(),
86+
}),
87+
}),
88+
}),
89+
}),
90+
}),
91+
}),
6692
}),
6793
}));
6894
});

0 commit comments

Comments
 (0)