Skip to content

Commit

Permalink
feat: addResourceSuppressionsByPath throws error when resource is not…
Browse files Browse the repository at this point in the history
… found. (#836)

When calling addResourceSuppressionsByPath, if no resource is found an error is thrown. Calling addResourceSuppressionsByPath and not finding a resource could indicate a problem in the code, and could lead to future unintended suppressions.

Co-authored-by: Matt MacLean <maclema@gmail.com>
  • Loading branch information
dontirun and maclema authored May 6, 2022
1 parent f4f5177 commit 9dfbbc6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/nag-suppressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class NagSuppressions {
suppressions: NagPackSuppression[],
applyToChildren: boolean = false
): void {
let added = false;
for (const child of stack.node.findAll()) {
const fixedPath = path.replace(/^\//, '');
if (
Expand All @@ -95,7 +96,13 @@ export class NagSuppressions {
suppressions,
applyToChildren
);
added = true;
}
}
if (!added) {
throw new Error(
`Suppression path "${path}" did not match any resource. This can occur when a resource does not exist or if a suppression is applied before a resource is created.`
);
}
}
}
14 changes: 14 additions & 0 deletions test/Engine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,20 @@ describe('Rule suppression system', () => {
})
);
});

test('Test path miss', () => {
const stack = new Stack();
try {
NagSuppressions.addResourceSuppressionsByPath(stack, '/No/Such/Path', [
{ id: 'NA', reason: '............' },
]);
throw new Error('Did not fail');
} catch (err) {
expect(err + '').toBe(
`Error: Suppression path "/No/Such/Path" did not match any resource. This can occur when a resource does not exist or if a suppression is applied before a resource is created.`
);
}
});
});

describe('Rule explanations', () => {
Expand Down

0 comments on commit 9dfbbc6

Please sign in to comment.