-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(toolkit): requireApproval option for deploy (#32977)
adds toolkit tests for deploy ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
- Loading branch information
Showing
10 changed files
with
149 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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