Skip to content

Commit 80d1145

Browse files
authored
Fix: [AEA-0000] - fix sonar issuses (#303)
## Summary - Routine Change ### Details - fix sonar issues
1 parent 06b292b commit 80d1145

File tree

7 files changed

+175
-102
lines changed

7 files changed

+175
-102
lines changed

.devcontainer/Dockerfile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
FROM mcr.microsoft.com/devcontainers/base:ubuntu
22

3-
43
ARG TARGETARCH
54
ENV TARGETARCH=${TARGETARCH}
65

Lines changed: 123 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env bash
22
set -e
33

4+
AWS_MAX_ATTEMPTS=20
5+
export AWS_MAX_ATTEMPTS
6+
47
if [ -z "${REPOSITORY_NAME}" ]; then
58
echo "REPOSITORY_NAME not set"
69
exit 1
@@ -11,65 +14,125 @@ if [ -z "${IMAGE_TAG}" ]; then
1114
exit 1
1215
fi
1316

14-
function wait_for_scan() {
15-
echo "Giving some time for scan to begin..."
16-
sleep 3
17-
while [[ $(aws ecr describe-image-scan-findings --repository-name "${REPOSITORY_NAME}" --image-id imageTag="${IMAGE_TAG}" | jq -r .imageScanStatus.status) != "COMPLETE" ]];do
18-
echo "SCAN IS NOT YET COMPLETE..."
19-
sleep 3
20-
done
21-
echo "Final sleep to ensure findings are shown correctly"
22-
sleep 60
23-
}
24-
25-
function check_for_high_critical_vuln() {
26-
scan_results=$(aws ecr describe-image-scan-findings --repository-name "${REPOSITORY_NAME}" --image-id imageTag="${IMAGE_TAG}")
27-
high=$(echo "$scan_results" | jq '.imageScanFindings.enhancedFindings[]? | select(.severity == "HIGH" and .status != "SUPPRESSED")')
28-
critical=$(echo "$scan_results" | jq '.imageScanFindings.enhancedFindings[]? | select(.severity == "CRITICAL" and .status != "SUPPRESSED")')
29-
}
30-
31-
function return_scan_results() {
32-
echo "=== BEGIN IMAGE SCAN RESULTS ==="
33-
echo "$scan_results"
34-
echo "=== END IMAGE SCAN RESULTS ==="
35-
}
36-
37-
function return_error() {
38-
echo -e "\n**********************************************************"
39-
echo "**********************************************************"
40-
echo "**********************************************************"
41-
echo "ERROR: There are CRITICAL/HIGH vulnerabilities. Stopping build."
42-
echo "**********************************************************"
43-
echo "**********************************************************"
44-
echo "**********************************************************"
45-
exit 2
46-
}
47-
48-
function analyze_scan_results() {
49-
if [[ -n "$critical" ]]; then
50-
echo "ERROR: There are CRITICAL vulnerabilities. Stopping build."
51-
52-
echo "=== BEGIN CRITICAL IMAGE SCAN RESULTS ==="
53-
echo "$critical"
54-
echo "=== END CRITICAL IMAGE SCAN RESULTS ==="
55-
56-
return_scan_results
57-
58-
return_error
59-
elif [[ -n "$high" ]]; then
60-
echo "ERROR: There are HIGH vulnerabilities. Stopping build."
61-
62-
echo "=== BEGIN HIGH IMAGE SCAN RESULTS ==="
63-
echo "$high"
64-
echo "=== END HIGH IMAGE SCAN RESULTS ==="
65-
66-
return_scan_results
67-
return_error
68-
else
69-
return_scan_results
17+
if [ -z "${AWS_REGION}" ]; then
18+
echo "AWS_REGION not set"
19+
exit 1
20+
fi
21+
22+
if [ -z "${ACCOUNT_ID}" ]; then
23+
echo "AWS_REGION not set"
24+
exit 1
25+
fi
26+
27+
IMAGE_DIGEST=$(aws ecr describe-images \
28+
--repository-name "$REPOSITORY_NAME" \
29+
--image-ids imageTag="$IMAGE_TAG" \
30+
--query 'imageDetails[0].imageDigest' \
31+
--output text)
32+
33+
RESOURCE_ARN="arn:aws:ecr:${AWS_REGION}:${ACCOUNT_ID}:repository/${REPOSITORY_NAME}/${IMAGE_DIGEST}"
34+
35+
echo "Monitoring scan for ${REPOSITORY_NAME}:${IMAGE_TAG}"
36+
echo "Resource ARN: ${RESOURCE_ARN}"
37+
echo
38+
39+
# Wait for ECR scan to reach COMPLETE
40+
STATUS=""
41+
echo "Waiting for ECR scan to complete..."
42+
for i in {1..30}; do
43+
echo "Checking scan status. Attempt ${i}"
44+
STATUS=$(aws ecr describe-image-scan-findings \
45+
--repository-name "$REPOSITORY_NAME" \
46+
--image-id imageDigest="$IMAGE_DIGEST" \
47+
--query 'imageScanStatus.status' \
48+
--output text 2>/dev/null || echo "NONE")
49+
50+
if [[ "$STATUS" == "COMPLETE" ]]; then
51+
echo "ECR scan completed."
52+
break
53+
fi
54+
55+
if [[ "$STATUS" == "FAILED" ]]; then
56+
echo "Scan failed."
57+
exit 1
58+
fi
59+
60+
echo "SCAN IS NOT YET COMPLETE. Waiting 10 seconds before checking again..."
61+
sleep 10
62+
done
63+
64+
if [[ "$STATUS" != "COMPLETE" ]]; then
65+
echo "Timeout waiting for ECR scan to complete."
66+
exit 1
67+
fi
68+
69+
# Wait for Inspector2 findings to appear & stabilize
70+
# this is in place as scan may show as complete but findings have not yet stabilize
71+
echo
72+
echo "Waiting for Inspector2 findings to stabilize..."
73+
74+
PREV_HASH=""
75+
for i in {1..12}; do # ~2 minutes max
76+
FINDINGS=$(aws inspector2 list-findings \
77+
--filter-criteria "{
78+
\"resourceId\": [{\"comparison\": \"EQUALS\", \"value\": \"${RESOURCE_ARN}\"}],
79+
\"findingStatus\": [{\"comparison\": \"EQUALS\", \"value\": \"ACTIVE\"}]
80+
}" \
81+
--output json 2>/dev/null || echo "{}")
82+
83+
CURR_HASH=$(echo "$FINDINGS" | sha256sum)
84+
COUNT=$(echo "$FINDINGS" | jq '.findings | length')
85+
86+
if [[ "$COUNT" -gt 0 && "$CURR_HASH" == "$PREV_HASH" ]]; then
87+
echo "Findings stabilized ($COUNT findings)."
88+
break
7089
fi
71-
}
7290

73-
wait_for_scan
74-
check_for_high_critical_vuln
75-
analyze_scan_results
91+
PREV_HASH="$CURR_HASH"
92+
echo "Attempt: ${i}. Still waiting... (${COUNT} findings so far)"
93+
sleep 10
94+
done
95+
96+
# Extract counts and display findings
97+
echo
98+
echo "Final Inspector2 findings with suppressions removed:"
99+
echo
100+
101+
echo "$FINDINGS" | jq '{
102+
findings: [
103+
.findings[]? | {
104+
severity: .severity,
105+
title: .title,
106+
package: .packageVulnerabilityDetails.vulnerablePackages[0].name,
107+
sourceUrl: .packageVulnerabilityDetails.sourceUrl,
108+
recommendation: (.remediation.recommendation.text // "N/A")
109+
}
110+
]
111+
}'
112+
113+
echo
114+
115+
# Check for critical/high severity
116+
CRITICAL_COUNT=$(echo "$FINDINGS" | jq '[.findings[]? | select(.severity=="CRITICAL")] | length')
117+
HIGH_COUNT=$(echo "$FINDINGS" | jq '[.findings[]? | select(.severity=="HIGH")] | length')
118+
119+
if (( CRITICAL_COUNT > 0 || HIGH_COUNT > 0 )); then
120+
echo "${CRITICAL_COUNT} CRITICAL and ${HIGH_COUNT} HIGH vulnerabilities detected!"
121+
echo
122+
echo "Critical/High vulnerabilities:"
123+
echo "$FINDINGS" | jq -r '
124+
.findings[]? |
125+
select(.severity=="CRITICAL" or .severity=="HIGH") |{
126+
severity: .severity,
127+
title: .title,
128+
package: .packageVulnerabilityDetails.vulnerablePackages[0].name,
129+
sourceUrl: .packageVulnerabilityDetails.sourceUrl,
130+
recommendation: (.remediation.recommendation.text // "N/A")
131+
}'
132+
echo
133+
echo "Failing pipeline due to Critical/High vulnerabilities."
134+
exit 2
135+
else
136+
echo "No Critical or High vulnerabilities found."
137+
exit 0
138+
fi

.github/workflows/docker_image_build.yml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,19 @@ jobs:
1919
packages: read
2020
steps:
2121
- name: Checkout code
22-
uses: actions/checkout@v5
22+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
2323
with:
2424
ref: ${{ env.BRANCH_NAME }}
2525

2626
- name: Build cdk-utils-build Docker image
2727
id: build-cdk-utils-build-image
28+
env:
29+
VERSION_NUMBER: ${{ inputs.VERSION_NUMBER }}
2830
run: |
29-
docker build -t "cdk-utils-build:${{ inputs.VERSION_NUMBER }}" -f docker/Dockerfile --build-arg VERSION=${{ inputs.VERSION_NUMBER }} .
30-
docker save "cdk-utils-build:${{ inputs.VERSION_NUMBER }}" -o cdk-utils-build.img
31+
docker build -t "cdk-utils-build:${VERSION_NUMBER}" -f docker/Dockerfile --build-arg VERSION="${VERSION_NUMBER}" .
32+
docker save "cdk-utils-build:${VERSION_NUMBER}" -o cdk-utils-build.img
3133
32-
- uses: actions/upload-artifact@v4
34+
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
3335
name: Upload docker images
3436
with:
3537
name: docker_artifact

.github/workflows/docker_image_upload.yml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ on:
2121
secrets:
2222
CDK_PUSH_IMAGE_ROLE:
2323
required: true
24-
24+
2525
jobs:
2626
upload_docker_image:
2727
runs-on: ubuntu-22.04
@@ -32,22 +32,22 @@ jobs:
3232

3333
steps:
3434
- name: Checkout local github actions
35-
uses: actions/checkout@v5
35+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8
3636
with:
3737
ref: ${{ env.BRANCH_NAME }}
3838
fetch-depth: 0
3939
sparse-checkout: |
4040
.github
4141
4242
- name: Configure AWS Credentials
43-
uses: aws-actions/configure-aws-credentials@v5
43+
uses: aws-actions/configure-aws-credentials@00943011d9042930efac3dcd3a170e4273319bc8
4444
with:
4545
aws-region: eu-west-2
4646
role-to-assume: ${{ secrets.CDK_PUSH_IMAGE_ROLE }}
4747
role-session-name: upload-cdk-utils-build
4848

4949
- name: docker_artifact download
50-
uses: actions/download-artifact@v5
50+
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0
5151
with:
5252
name: docker_artifact
5353
path: .
@@ -60,23 +60,28 @@ jobs:
6060
- name: Retrieve AWS Account ID
6161
id: retrieve-account-id
6262
run: echo "ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)" >> "$GITHUB_ENV"
63-
63+
6464
- name: Login to Amazon ECR
6565
id: login-ecr
6666
run: |
6767
aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin ${{ env.ACCOUNT_ID }}.dkr.ecr.eu-west-2.amazonaws.com
6868
6969
- name: Push tagged version cdk-utils-build to Amazon ECR
70+
env:
71+
VERSION_NUMBER: ${{ inputs.VERSION_NUMBER }}
72+
DOCKER_IMAGE_TAG: ${{ inputs.DOCKER_IMAGE_TAG }}
7073
run: |
71-
docker tag "cdk-utils-build:${{ inputs.VERSION_NUMBER }}" "${{ env.ACCOUNT_ID }}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:${{ inputs.DOCKER_IMAGE_TAG }}"
72-
docker push "${{ env.ACCOUNT_ID }}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:${{ inputs.DOCKER_IMAGE_TAG }}"
74+
docker tag "cdk-utils-build:${VERSION_NUMBER}" "${ACCOUNT_ID}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:${DOCKER_IMAGE_TAG}"
75+
docker push "${ACCOUNT_ID}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:${DOCKER_IMAGE_TAG}"
7376
7477
- name: Push latest cdk-utils-build to Amazon ECR
7578
if: ${{ inputs.TAG_LATEST == true }}
79+
env:
80+
VERSION_NUMBER: ${{ inputs.VERSION_NUMBER }}
7681
run: |
77-
docker tag "cdk-utils-build:${{ inputs.VERSION_NUMBER }}" "${{ env.ACCOUNT_ID }}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:latest"
78-
docker push "${{ env.ACCOUNT_ID }}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:latest"
79-
82+
docker tag "cdk-utils-build:${VERSION_NUMBER}" "${ACCOUNT_ID}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:latest"
83+
docker push "${ACCOUNT_ID}.dkr.ecr.eu-west-2.amazonaws.com/cdk-utils-build-repo:latest"
84+
8085
- name: Check cdk-utils-build scan results
8186
env:
8287
REPOSITORY_NAME: cdk-utils-build-repo

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
issue_number: ${{steps.get_issue_number.outputs.result}}
4242

4343
steps:
44-
- uses: actions/github-script@v8
44+
- uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd
4545
name: get issue number
4646
id: get_issue_number
4747
with:

docker/Dockerfile

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
11
FROM ubuntu:24.04
22

3+
ARG TARGETARCH
4+
ENV TARGETARCH=${TARGETARCH}
5+
6+
ARG ASDF_VERSION
7+
COPY .tool-versions.asdf /tmp/.tool-versions.asdf
8+
39
ARG VERSION
410

511
RUN apt-get update \
612
&& export DEBIAN_FRONTEND=noninteractive \
7-
&& apt-get -y upgrade
8-
9-
RUN export DEBIAN_FRONTEND=noninteractive \
13+
&& apt-get -y upgrade \
1014
&& apt-get -y install --no-install-recommends ca-certificates curl git jq make unzip wget \
1115
&& apt-get clean
1216

1317
# install aws stuff
14-
ADD https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip /tmp/awscliv2.zip
15-
RUN unzip /tmp/awscliv2.zip -d /tmp/aws-cli && \
18+
# Download correct AWS CLI for arch
19+
RUN if [ "$TARGETARCH" = "arm64" ] || [ "$TARGETARCH" == "aarch64" ]; then \
20+
wget -O /tmp/awscliv2.zip "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip"; \
21+
else \
22+
wget -O /tmp/awscliv2.zip "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip"; \
23+
fi && \
24+
unzip /tmp/awscliv2.zip -d /tmp/aws-cli && \
1625
/tmp/aws-cli/aws/install && \
17-
rm tmp/awscliv2.zip && \
18-
rm -rf /tmp/aws-cli
26+
rm /tmp/awscliv2.zip && rm -rf /tmp/aws-cli
27+
28+
# Install ASDF
29+
RUN ASDF_VERSION=$(awk '!/^#/ && NF {print $1; exit}' /tmp/.tool-versions.asdf) && \
30+
if [ "$TARGETARCH" = "arm64" ] || [ "$TARGETARCH" = "aarch64" ]; then \
31+
wget -O /tmp/asdf.tar.gz https://github.com/asdf-vm/asdf/releases/download/v${ASDF_VERSION}/asdf-v${ASDF_VERSION}-linux-arm64.tar.gz; \
32+
else \
33+
wget -O /tmp/asdf.tar.gz https://github.com/asdf-vm/asdf/releases/download/v${ASDF_VERSION}/asdf-v${ASDF_VERSION}-linux-amd64.tar.gz; \
34+
fi && \
35+
tar -xvzf /tmp/asdf.tar.gz && \
36+
mv asdf /usr/bin
1937

2038
RUN useradd -ms /bin/bash cdkuser
2139
RUN chown -R cdkuser /home/cdkuser
2240
WORKDIR /home/cdkuser
2341
USER cdkuser
24-
# Install ASDF
25-
RUN git clone https://github.com/asdf-vm/asdf.git /home/cdkuser/.asdf --branch v0.14.1; \
26-
echo '. /home/cdkuser/.asdf/asdf.sh' >> ~/.bashrc; \
27-
echo '. /home/cdkuser/.asdf/completions/asdf.bash' >> ~/.bashrc; \
28-
echo 'PATH="$PATH:/home/cdkuser/.asdf/bin/"' >> ~/.bashrc;
2942

30-
ENV PATH="$PATH:/home/cdkuser/.asdf/bin/:/home/cdkuser/node_modules/.bin"
43+
ENV PATH="$PATH:/home/cdkuser/.asdf/shims/:/home/cdkuser/node_modules/.bin"
3144

3245
# Install ASDF plugins
3346
RUN asdf plugin add nodejs https://github.com/asdf-vm/asdf-nodejs.git
3447
# install some common node versions that are used in builds to speed things up
35-
RUN asdf install nodejs 20.19.1; \
36-
asdf install nodejs 23.9.0
37-
# update npm
38-
RUN export ASDF_DIR=/home/cdkuser/.asdf && \
39-
. /home/cdkuser/.asdf/asdf.sh && \
40-
asdf shell nodejs 20.19.1 && \
41-
cd ~/.asdf/installs/nodejs/20.19.1/lib && npm update npm
48+
RUN asdf install nodejs 22.20.0;
4249

4350
# copy files needed for deployment
4451
COPY --chown=cdkuser docker/entrypoint.sh /home/cdkuser/

0 commit comments

Comments
 (0)