Skip to content

Commit 936588d

Browse files
authored
fix: cdk nag test (#613)
1 parent bba8915 commit 936588d

File tree

6 files changed

+254
-196
lines changed

6 files changed

+254
-196
lines changed

cdk/my_service/service_stack.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import os
22
from pathlib import Path
33

4-
from aws_cdk import Stack, Tags
4+
from aws_cdk import Aspects, Stack, Tags
5+
from cdk_nag import AwsSolutionsChecks, NagSuppressions
56
from constructs import Construct
67
from git import Repo
78
from my_service.api_construct import ApiConstruct # type: ignore
@@ -36,4 +37,48 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
3637
# from running the service pipeline and without redeploying the service lambdas. For the sake of this template
3738
# example, it is deployed as part of the service stack
3839
self.dynamic_configuration = ConfigurationStore(self, f'{id}dynamic_conf'[0:64], ENVIRONMENT, SERVICE_NAME, CONFIGURATION_NAME)
39-
self.lambdas = ApiConstruct(self, f'{id}Service'[0:64], self.dynamic_configuration.config_app.name)
40+
self.api = ApiConstruct(self, f'{id}Service'[0:64], self.dynamic_configuration.config_app.name)
41+
42+
# add security check
43+
self._add_security_tests()
44+
45+
def _add_security_tests(self) -> None:
46+
Aspects.of(self).add(AwsSolutionsChecks(verbose=True))
47+
# Suppress a specific rule for this resource
48+
NagSuppressions.add_stack_suppressions(
49+
self,
50+
[
51+
{
52+
'id': 'AwsSolutions-IAM4',
53+
'reason': 'policy for cloudwatch logs.'
54+
},
55+
{
56+
'id': 'AwsSolutions-IAM5',
57+
'reason': 'policy for cloudwatch logs.'
58+
},
59+
{
60+
'id': 'AwsSolutions-APIG2',
61+
'reason': 'lambda does input validation'
62+
},
63+
{
64+
'id': 'AwsSolutions-APIG1',
65+
'reason': 'not mandatory in a sample template'
66+
},
67+
{
68+
'id': 'AwsSolutions-APIG3',
69+
'reason': 'not mandatory in a sample template'
70+
},
71+
{
72+
'id': 'AwsSolutions-APIG6',
73+
'reason': 'not mandatory in a sample template'
74+
},
75+
{
76+
'id': 'AwsSolutions-APIG4',
77+
'reason': 'authorization not mandatory in a sample template'
78+
},
79+
{
80+
'id': 'AwsSolutions-COG4',
81+
'reason': 'not using cognito'
82+
},
83+
],
84+
)

docs/cdk.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ All ASW Lambda function configurations are saved as constants at the `cdk.my_ser
5050

5151
### **Infrastructure CDK & Security Tests**
5252

53-
Under tests there is an `infrastructure` folder for CDK & security infrastructure tests.
53+
Under tests there is an `infrastructure` folder for CDK infrastructure tests.
5454

5555
The first test, 'test_cdk' uses CDK's testing framework which asserts that required resources exists so the application will not break anything upon deployment.
5656

57-
The second test, 'test_cdk_nag' checks your cloudformation output for security best practices. For more information click [here](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/check-aws-cdk-applications-or-cloudformation-templates-for-best-practices-by-using-cdk-nag-rule-packs.html){:target="_blank" rel="noopener"}.
57+
The security tests are based on 'cdk_nag'. It checks your cloudformation output for security best practices. It can be found in the 'service_stack.py' as part of the stack definition. It will fail the deployment when there is a security issue.
58+
59+
For more information click [here](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/check-aws-cdk-applications-or-cloudformation-templates-for-best-practices-by-using-cdk-nag-rule-packs.html){:target="_blank" rel="noopener"}.

docs/pipeline.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ All steps can be run locally using the makefile. See details below:
1818
- Python formatter Yapf as defined in `.style` - run `make yapf` in the IDE
1919
- Python complexity checks: radon and xenon - run `make complex` in the IDE
2020
- Unit tests. Run `make unit` to run unit tests in the IDE
21-
- Infrastructure test. Run `make infra-tests` to run the CDK & security infrastructure tests in the IDE
21+
- Infrastructure test. Run `make infra-tests` to run the CDK infrastructure tests in the IDE
2222
- Code coverage by [codecov.io](https://about.codecov.io/)
23-
- Deploy CDK - not run in GitHub yet (add your own AWS secrets), can be run locally at this moment - run `make deploy` in the IDE
24-
- E2E tests - not run in GitHub yet (add your own AWS secrets), can be run locally at this moment - run `make e2e` in the IDE
23+
- Deploy CDK - run `make deploy` in the IDE, will also run security tests based on cdk_nag
24+
- E2E tests - run `make e2e` in the IDE
2525
- Update GitHub documentation branch
2626

2727
### Other Capabilities

0 commit comments

Comments
 (0)