Skip to content

Commit e313e27

Browse files
authored
fix(gen2-migration): relax lock status validation to check for Deny statement existence instead of exact policy match (#14756)
chore: relax lock statement validation Co-authored-by: Sai Ray <saisujit@amazon.com>
1 parent df66ffd commit e313e27

File tree

2 files changed

+38
-12
lines changed

2 files changed

+38
-12
lines changed

packages/amplify-cli/src/__tests__/commands/gen2-migration/_infra/validations.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,39 @@ describe('AmplifyGen2MigrationValidations', () => {
784784
await expect(validations.validateLockStatus()).resolves.not.toThrow();
785785
});
786786

787+
it('should pass when lock statement exists alongside other statements', async () => {
788+
jest.spyOn(stateManager, 'getTeamProviderInfo').mockReturnValue({
789+
mock: {
790+
awscloudformation: {
791+
StackName: 'test-stack',
792+
},
793+
},
794+
});
795+
796+
const policyWithBoth = {
797+
Statement: [
798+
{
799+
Effect: 'Allow',
800+
Action: 'Update:*',
801+
Principal: '*',
802+
Resource: '*',
803+
},
804+
{
805+
Effect: 'Deny',
806+
Action: 'Update:*',
807+
Principal: '*',
808+
Resource: '*',
809+
},
810+
],
811+
};
812+
813+
mockCfnSend.mockResolvedValue({
814+
StackPolicyBody: JSON.stringify(policyWithBoth),
815+
});
816+
817+
await expect(validations.validateLockStatus()).resolves.not.toThrow();
818+
});
819+
787820
it('should throw MigrationError when stack policy has wrong effect', async () => {
788821
jest.spyOn(stateManager, 'getTeamProviderInfo').mockReturnValue({
789822
mock: {

packages/amplify-cli/src/commands/gen2-migration/_infra/validations.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -161,18 +161,11 @@ export class AmplifyGen2MigrationValidations {
161161
}
162162

163163
const currentPolicy = JSON.parse(StackPolicyBody);
164-
const expectedPolicy = {
165-
Statement: [
166-
{
167-
Effect: 'Deny',
168-
Action: 'Update:*',
169-
Principal: '*',
170-
Resource: '*',
171-
},
172-
],
173-
};
174-
175-
if (JSON.stringify(currentPolicy) !== JSON.stringify(expectedPolicy)) {
164+
const hasLockStatement = currentPolicy.Statement.some(
165+
(s: Record<string, string>) => s.Effect === 'Deny' && s.Action === 'Update:*' && s.Principal === '*' && s.Resource === '*',
166+
);
167+
168+
if (!hasLockStatement) {
176169
throw new AmplifyError('MigrationError', {
177170
message: 'Stack policy does not match expected lock policy',
178171
resolution: 'Run the lock command to set the correct stack policy.',

0 commit comments

Comments
 (0)