Skip to content

Commit 3fa3c00

Browse files
anthony-nhskris-szlapaOrkastrated
authored
Fix: [AEA-4348] - set up cfn guard (#162)
## Summary - Routine Change ### Details - set up cfn_guard --------- Co-authored-by: Kris Szlapa <kris.szlapa1@nhs.net> Co-authored-by: Adam Brown <adam.brown41@nhs.net>
1 parent 03296bc commit 3fa3c00

File tree

7 files changed

+65
-0
lines changed

7 files changed

+65
-0
lines changed

.github/workflows/quality_checks.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ jobs:
5252
- name: run lint
5353
run: make lint
5454

55+
- name: Run cfn-guard
56+
run: make cfn-guard
57+
58+
- name: show cfn-guard output
59+
if: failure()
60+
run: find cfn_guard_output -type f -print0 | xargs -0 cat
61+
62+
- uses: actions/upload-artifact@v4
63+
name: upload cfn_guard_output
64+
if: failure()
65+
with:
66+
name: cfn_guard_output
67+
path: cfn_guard_output
68+
5569
- name: run tests and Sonar scan
5670
env:
5771
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ node_modules/
4242
.jekyll-cache
4343
.jekyll-metadata
4444
vendor
45+
cfn_guard_output/

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,6 @@ aws-configure:
108108

109109
aws-login:
110110
aws sso login --sso-session sso-session
111+
112+
cfn-guard:
113+
./scripts/run_cfn_guard.sh

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ These are used to do common commands
185185
- `lint-githubactions` runs lint for github actions
186186
- `lint-githubactions-scriptns` runs lint for github actions scripts
187187
- `test` runs unit tests for all code
188+
- `cfn-guard` runs cfn-guard for sam and cloudformation templates
189+
188190

189191
#### Compiling
190192

SAMtemplates/lambda_resources.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ Resources:
109109

110110
LambdaLogGroup:
111111
Type: "AWS::Logs::LogGroup"
112+
Metadata:
113+
guard:
114+
SuppressedRules:
115+
- CW_LOGGROUP_RETENTION_PERIOD_CHECK
112116
Properties:
113117
LogGroupName: !Sub "/aws/lambda/${LambdaName}"
114118
RetentionInDays: !Ref LogRetentionDays

SAMtemplates/main_template.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ Resources:
7575
AWS_LAMBDA_LOG_LEVEL: !Ref LogLevel
7676
POWERTOOLS_LOG_LEVEL: !Ref LogLevel
7777
PROFILE_MANIFEST_FILE: uk_core.manifest.json
78+
Metadata:
79+
guard:
80+
SuppressedRules:
81+
- LAMBDA_DLQ_CHECK
82+
- LAMBDA_INSIDE_VPC
83+
- LAMBDA_CONCURRENCY_CHECK
7884

7985
Outputs:
8086
FHIRValidatorUKCoreLambdaName:

scripts/run_cfn_guard.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
set -eou pipefail
3+
4+
rm -rf /tmp/ruleset
5+
rm -rf cfn_guard_output
6+
7+
wget -O /tmp/ruleset.zip https://github.com/aws-cloudformation/aws-guard-rules-registry/releases/download/1.0.2/ruleset-build-v1.0.2.zip >/dev/null 2>&1
8+
unzip /tmp/ruleset.zip -d /tmp/ruleset/ >/dev/null 2>&1
9+
10+
curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/aws-cloudformation/cloudformation-guard/main/install-guard.sh | sh >/dev/null 2>&1
11+
12+
mkdir -p cfn_guard_output
13+
14+
declare -a rulesets=("ncsc" "ncsc-cafv3" "wa-Reliability-Pillar" "wa-Security-Pillar")
15+
for ruleset in "${rulesets[@]}"
16+
do
17+
18+
while IFS= read -r -d '' file
19+
do
20+
echo "checking SAM template $file with ruleset $ruleset"
21+
mkdir -p "$(dirname cfn_guard_output/"$file")"
22+
23+
# transform the SAM template to cloudformation and then run through cfn-guard
24+
SAM_OUPUT=$(sam validate -t "$file" --region eu-west-2 --debug 2>&1 | \
25+
grep -Pazo '(?s)AWSTemplateFormatVersion.*\n\/' | tr -d '\0')
26+
echo "${SAM_OUPUT::-1}" | ~/.guard/bin/cfn-guard validate \
27+
--rules "/tmp/ruleset/output/$ruleset.guard" \
28+
--show-summary fail \
29+
> "cfn_guard_output/${file}_${ruleset}.txt"
30+
31+
done < <(find ./SAMtemplates -name '*.y*ml' -print0)
32+
33+
done
34+
35+
rm -rf /tmp/ruleset

0 commit comments

Comments
 (0)