-
Notifications
You must be signed in to change notification settings - Fork 4.3k
chore(toolkit): requireApproval option for deploy #32977
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
059e0b4
api changes
mrgrain 9ebb093
test(toolkit): tests for synth
mrgrain ced7269
test(toolkit): tests for deploy
kaizencc 410b845
Merge branch 'main' into conroy/deploy-tests
kaizencc 77863b7
require approval is live
kaizencc e3c3670
Merge branch 'conroy/deploy-tests' of https://github.com/aws/aws-cdk …
kaizencc 3832cd4
mock deployments
kaizencc 3dad7c6
Merge branch 'main' into conroy/deploy-tests
kaizencc 82466be
use testiohost
kaizencc 09ea55a
pr feedback
kaizencc 40ea16b
Merge branch 'main' into conroy/deploy-tests
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
packages/@aws-cdk/toolkit/lib/actions/diff/private/helpers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { DescribeChangeSetOutput, fullDiff } from '@aws-cdk/cloudformation-diff'; | ||
| import * as cxapi from '@aws-cdk/cx-api'; | ||
| import { ToolkitError } from '../../../api/errors'; | ||
| import { RequireApproval } from '../../deploy'; | ||
|
|
||
| /** | ||
| * Return whether the diff has security-impacting changes that need confirmation | ||
| */ | ||
| export function diffRequiresApproval( | ||
| oldTemplate: any, | ||
| newTemplate: cxapi.CloudFormationStackArtifact, | ||
| requireApproval: RequireApproval, | ||
| changeSet?: DescribeChangeSetOutput, | ||
| ): boolean { | ||
| // @todo return or print the full diff. | ||
| const diff = fullDiff(oldTemplate, newTemplate.template, changeSet); | ||
|
|
||
| switch (requireApproval) { | ||
| case RequireApproval.NEVER: return false; | ||
| case RequireApproval.ANY_CHANGE: return diff.permissionsAnyChanges; | ||
| case RequireApproval.BROADENING: return diff.permissionsBroadened; | ||
| default: throw new ToolkitError(`Unrecognized approval level: ${requireApproval}`); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './helpers'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
packages/@aws-cdk/toolkit/test/_fixtures/stack-with-role/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import * as iam from 'aws-cdk-lib/aws-iam'; | ||
| import * as core from 'aws-cdk-lib/core'; | ||
|
|
||
| export default async() => { | ||
| const app = new core.App(); | ||
| const stack = new core.Stack(app, 'Stack1'); | ||
| new iam.Role(stack, 'Role', { | ||
| assumedBy: new iam.ArnPrincipal('arn'), | ||
| }); | ||
| return app.synth() as any; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,20 +5,24 @@ import { IIoHost, IoMessage, IoMessageLevel, IoRequest, isMessageRelevantForLeve | |
| * Optional set a level to filter out all irrelevant messages. | ||
| */ | ||
| export class TestIoHost implements IIoHost { | ||
| public readonly spy: jest.Mock<any, any, any>; | ||
| public readonly notifySpy: jest.Mock<any, any, any>; | ||
| public readonly requestSpy: jest.Mock<any, any, any>; | ||
|
Comment on lines
+8
to
+9
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
|
|
||
| constructor(public level: IoMessageLevel = 'info') { | ||
| this.spy = jest.fn(); | ||
| this.notifySpy = jest.fn(); | ||
| this.requestSpy = jest.fn(); | ||
| } | ||
|
|
||
| public async notify<T>(msg: IoMessage<T>): Promise<void> { | ||
| if (isMessageRelevantForLevel(msg, this.level)) { | ||
| this.spy(msg); | ||
| this.notifySpy(msg); | ||
| } | ||
| } | ||
|
|
||
| public async requestResponse<T, U>(msg: IoRequest<T, U>): Promise<U> { | ||
| await this.notify(msg); | ||
| if (isMessageRelevantForLevel(msg, this.level)) { | ||
| this.requestSpy(msg); | ||
| } | ||
| return msg.defaultResponse; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| import { RequireApproval } from '../../lib'; | ||
| import { Toolkit } from '../../lib/toolkit'; | ||
| import { builderFixture, TestIoHost } from '../_helpers'; | ||
|
|
||
| const ioHost = new TestIoHost(); | ||
| const toolkit = new Toolkit({ ioHost }); | ||
|
|
||
| jest.mock('../../lib/api/aws-cdk', () => { | ||
| return { | ||
| ...jest.requireActual('../../lib/api/aws-cdk'), | ||
| Deployments: jest.fn().mockImplementation(() => ({ | ||
| deployStack: jest.fn().mockResolvedValue({ | ||
| type: 'did-deploy-stack', | ||
| stackArn: 'arn:aws:cloudformation:region:account:stack/test-stack', | ||
| outputs: {}, | ||
| noOp: false, | ||
| }), | ||
| resolveEnvironment: jest.fn().mockResolvedValue({}), | ||
| isSingleAssetPublished: jest.fn().mockResolvedValue(true), | ||
| readCurrentTemplate: jest.fn().mockResolvedValue({ Resources: {} }), | ||
| })), | ||
| }; | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| ioHost.notifySpy.mockClear(); | ||
| ioHost.requestSpy.mockClear(); | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe('deploy', () => { | ||
| test('deploy from builder', async () => { | ||
| // WHEN | ||
| const cx = await builderFixture(toolkit, 'two-empty-stacks'); | ||
| await toolkit.deploy(cx); | ||
|
|
||
| // THEN | ||
| expect(ioHost.notifySpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
| action: 'deploy', | ||
| level: 'info', | ||
| message: expect.stringContaining('Deployment time:'), | ||
| })); | ||
| }); | ||
|
|
||
| test('request response when require approval is set', async () => { | ||
| // WHEN | ||
| const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
| await toolkit.deploy(cx, { | ||
| requireApproval: RequireApproval.ANY_CHANGE, | ||
| }); | ||
|
|
||
| // THEN | ||
| expect(ioHost.requestSpy).toHaveBeenCalledWith(expect.objectContaining({ | ||
| action: 'deploy', | ||
| level: 'info', | ||
| code: 'CDK_TOOLKIT_I5060', | ||
| message: expect.stringContaining('Do you wish to deploy these changes'), | ||
| })); | ||
| }); | ||
|
|
||
| test('skips response by default', async () => { | ||
| // WHEN | ||
| const cx = await builderFixture(toolkit, 'stack-with-role'); | ||
| await toolkit.deploy(cx, { | ||
| requireApproval: RequireApproval.NEVER, | ||
| }); | ||
|
|
||
| // THEN | ||
| expect(ioHost.requestSpy).not.toHaveBeenCalledWith(expect.objectContaining({ | ||
| action: 'deploy', | ||
| level: 'info', | ||
| code: 'CDK_TOOLKIT_I5060', | ||
| message: expect.stringContaining('Do you wish to deploy these changes'), | ||
| })); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.