-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(postgres-manager): failing unit test from
cdk-nag
(#128)
- Loading branch information
1 parent
4aa5716
commit d846224
Showing
12 changed files
with
112 additions
and
64 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
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
2 changes: 1 addition & 1 deletion
2
lib/workload/stateless/postgres_manager/function/alter-pg-db-owner.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
2 changes: 1 addition & 1 deletion
2
lib/workload/stateless/postgres_manager/function/create-pg-db.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
3 changes: 1 addition & 2 deletions
3
lib/workload/stateless/postgres_manager/function/create-pg-iam-role.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
8 changes: 2 additions & 6 deletions
8
lib/workload/stateless/postgres_manager/function/create-pg-login-role.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
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,16 @@ | ||
/** | ||
* There are 2 ways of connecting from microservice to db | ||
*/ | ||
export enum DbAuthType { | ||
RDS_IAM, | ||
USERNAME_PASSWORD, | ||
} | ||
|
||
export type EventType = { | ||
microserviceName: string; | ||
}; | ||
|
||
export type MicroserviceConfig = { | ||
name: string; | ||
authType: DbAuthType; | ||
}[]; |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "lambda-with-rds", | ||
"name": "postgres-manager", | ||
"packageManager": "[email protected]", | ||
"dependencies": { | ||
"@aws-sdk/client-secrets-manager": "^3.515.0", | ||
|
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 |
---|---|---|
@@ -1,46 +1,94 @@ | ||
import { App, Aspects } from 'aws-cdk-lib'; | ||
import { App, Aspects, Stack } from 'aws-cdk-lib'; | ||
import { Annotations, Match } from 'aws-cdk-lib/assertions'; | ||
import { SynthesisMessage } from 'aws-cdk-lib/cx-api'; | ||
import { AwsSolutionsChecks } from 'cdk-nag'; | ||
import { AwsSolutionsChecks, NagSuppressions } from 'cdk-nag'; | ||
import { OrcaBusStatelessStack } from '../../lib/workload/orcabus-stateless-stack'; | ||
import { getEnvironmentConfig } from '../../config/constants'; | ||
|
||
function synthesisMessageToString(sm: SynthesisMessage): string { | ||
return `${sm.entry.data} [${sm.id}]`; | ||
} | ||
|
||
// Picking prod environment to test as it contain the sensitive data | ||
const config = getEnvironmentConfig('prod')!; | ||
|
||
describe('cdk-nag-stateless-stack', () => { | ||
let stack: OrcaBusStatelessStack; | ||
let app: App; | ||
|
||
beforeAll(() => { | ||
app = new App({}); | ||
stack = new OrcaBusStatelessStack(app, 'TestStack', { | ||
env: { | ||
account: '12345678', | ||
region: 'ap-southeast-2', | ||
}, | ||
...config.stackProps.orcaBusStatelessConfig, | ||
}); | ||
Aspects.of(stack).add(new AwsSolutionsChecks()); | ||
|
||
// Suppressions (if any) | ||
// ... | ||
const app: App = new App({}); | ||
const stack: OrcaBusStatelessStack = new OrcaBusStatelessStack(app, 'TestStack', { | ||
env: { | ||
account: '12345678', | ||
region: 'ap-southeast-2', | ||
}, | ||
...config.stackProps.orcaBusStatelessConfig, | ||
}); | ||
|
||
test('cdk-nag AwsSolutions Pack errors', () => { | ||
// stateless stack cdk-nag test | ||
Aspects.of(stack).add(new AwsSolutionsChecks()); | ||
test(`OrcaBusStatelessStack: cdk-nag AwsSolutions Pack errors`, () => { | ||
const errors = Annotations.fromStack(stack) | ||
.findError('*', Match.stringLikeRegexp('AwsSolutions-.*')) | ||
.map(synthesisMessageToString); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
|
||
test('cdk-nag AwsSolutions Pack warnings', () => { | ||
test(`OrcaBusStatelessStack: cdk-nag AwsSolutions Pack warnings`, () => { | ||
const warnings = Annotations.fromStack(stack) | ||
.findWarning('*', Match.stringLikeRegexp('AwsSolutions-.*')) | ||
.map(synthesisMessageToString); | ||
expect(warnings).toHaveLength(0); | ||
}); | ||
|
||
// microservice cdk-nag test | ||
for (const ms_stack of stack.microserviceStackArray) { | ||
const stackId = ms_stack.node.id; | ||
|
||
Aspects.of(ms_stack).add(new AwsSolutionsChecks()); | ||
|
||
applyNagSuppression(stackId, ms_stack); | ||
|
||
test(`${stackId}: cdk-nag AwsSolutions Pack errors`, () => { | ||
const errors = Annotations.fromStack(ms_stack) | ||
.findError('*', Match.stringLikeRegexp('AwsSolutions-.*')) | ||
.map(synthesisMessageToString); | ||
expect(errors).toHaveLength(0); | ||
}); | ||
|
||
test(`${stackId}: cdk-nag AwsSolutions Pack warnings`, () => { | ||
const warnings = Annotations.fromStack(ms_stack) | ||
.findWarning('*', Match.stringLikeRegexp('AwsSolutions-.*')) | ||
.map(synthesisMessageToString); | ||
expect(warnings).toHaveLength(0); | ||
}); | ||
} | ||
}); | ||
|
||
/** | ||
* apply nag suppression according to the relevant stackId | ||
* @param stackId the stackId | ||
* @param stack | ||
*/ | ||
function applyNagSuppression(stackId: string, stack: Stack) { | ||
switch (stackId) { | ||
case 'PostgresManager': | ||
NagSuppressions.addStackSuppressions(stack, [ | ||
{ id: 'AwsSolutions-IAM4', reason: 'allow to use AWS managed policy' }, | ||
]); | ||
|
||
// suppress by resource | ||
NagSuppressions.addResourceSuppressionsByPath( | ||
stack, | ||
`/TestStack/PostgresManager/CreateUserPassPostgresLambda/ServiceRole/DefaultPolicy/Resource`, | ||
[ | ||
{ | ||
id: 'AwsSolutions-IAM5', | ||
reason: | ||
"'*' is required for secretsmanager:GetRandomPassword and new SM ARN will contain random character", | ||
}, | ||
] | ||
); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} |
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