Skip to content

Commit 786f3f7

Browse files
committed
add:script,action file & readme for ecr-scan
1 parent 681b2a5 commit 786f3f7

File tree

6 files changed

+148
-1
lines changed

6 files changed

+148
-1
lines changed

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# ECR Image Scanning GitHub Action
2+
3+
This GitHub Action automatically scans an ECR (Elastic Container Registry) image when it is pushed to the repository. It utilizes the AWS CLI and ECR scanning capabilities to perform the scan and provide a scan report.
4+
5+
## Pre-requisites
6+
7+
To use this action, ensure that you have the AWS CLI installed and properly configured in your runner environment. Additionally, make sure you provide the required AWS credentials with the necessary permissions to access and scan the ECR image..
8+
9+
10+
## Description
11+
12+
This action utilizes a shell script executed within a Docker container to identify critical and high-level vulnerabilities linked to an image being pushed to the ECR repository.
13+
## Usage
14+
```
15+
name: Push to ECR
16+
17+
on:
18+
pull_request:
19+
branches:
20+
- main
21+
jobs:
22+
Build-Push-Scan-Image:
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Scan Docker image
26+
id: docker-scan
27+
uses: Ujjwal048/action-ecr-image-scan@main
28+
env:
29+
ECR_REPOSITORY: apparel-backend
30+
IMAGE_TAG: ${{ github.sha }}
31+
with:
32+
ecr_repository: ${{ env.ECR_REPOSITORY }}
33+
image_tag: ${{ env.IMAGE_TAG }}
34+
pr_comment: true
35+
github_token: ${{ secrets.GITHUB_TOKEN }}
36+
url: ${{ github.event.pull_request.comments_url }}
37+
aws_region: ap-south-1
38+
- name: Fail workflow if vulnerabilities found
39+
env:
40+
vulnerability: ${{ steps.docker-scan.outputs.VULNERABILITY }}
41+
block_build_on_failure: true
42+
run: |
43+
if [ "${{env.block_build_on_failure }}" = true && "${{ env.vulnerability }}" = true ]; then
44+
exit 1
45+
fi
46+
```
47+
## Input
48+
49+
- aws-region (required): The AWS region where your ECR repository is located.
50+
- ecr_repository (required): The name of your ECR repository.
51+
- image_tag (required): Tag of the image being pushed
52+
- pr_comment (required): true/false
53+
- github_token (required): For updating th PR comment with scan result
54+
- url (required): URL for calling the POST request to update PR
55+
56+
## Output
57+
58+
After the scan is completed, this action will produce the scan results and provide a link to the scan report. The scan report and the detailed report URL in the AWS console will be included as comments in the pull request and displayed in the GitHub step summary for easy access and visibility.
59+
### Parameters passed as ouput
60+
61+
- VULNERABILITY : If vulnerabilities are detected or not
62+
63+
64+
## Sample Output
65+
#### Github PR comment
66+
![Github PR Comment](outputs/github-pr-output.png)
67+
68+
#### Github Summary Report
69+
![Github Summary](outputs/github-summary-output.png)

action.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: "Scan Docker Images On Push to ECR"
2+
description: "Scans for container image vulnerabilities when an image is pushed to ECR"
3+
inputs:
4+
ecr_repository:
5+
description: "Name of the ECR repository"
6+
required: true
7+
image_tag:
8+
description: "ECR Image tag"
9+
required: true
10+
pr_comment:
11+
description: "Whether to comment the result on PR"
12+
required: true
13+
github_token:
14+
description: "Github token for updating PR"
15+
required: true
16+
url:
17+
description: "URL for calling POST request for updating PR"
18+
required: true
19+
aws_region:
20+
description: "AWS region"
21+
required: true
22+
runs:
23+
using: "docker"
24+
image: "Dockerfile"
25+
args:
26+
- ${{ inputs.ecr_repository }}
27+
- ${{ inputs.image_tag }}
28+
- ${{ inputs.pr_comment }}
29+
- ${{ inputs.github_token }}
30+
- ${{ inputs.url }}
31+
- ${{ inputs.aws_region }}
32+
outputs:
33+
VULNERABILITY:
34+
description: 'If vulnerabilities are detected or not'
35+
36+

entrypoint.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
REPO_NAME=$INPUT_ECR_REPOSITORY
4+
IMAGE_TAG=$INPUT_IMAGE_TAG
5+
URL=$INPUT_URL
6+
GITHUB_TOKEN=$INPUT_GITHUB_TOKEN
7+
PR_COMMENT=$INPUT_PR_COMMENT
8+
REGION=$INPUT_AWS_REGION
9+
10+
aws configure list >/dev/null 2>&1
11+
if [[ $? -eq 0 ]]; then
12+
aws ecr start-image-scan --repository-name $REPO_NAME --image-id imageTag=$IMAGE_TAG
13+
aws ecr wait image-scan-complete --repository-name $REPO_NAME --image-id imageTag=$IMAGE_TAG
14+
if [ $(echo $?) -eq 0 ]; then
15+
SCAN_FINDINGS=$(aws ecr describe-image-scan-findings --repository-name $REPO_NAME --image-id imageTag=$IMAGE_TAG | jq '.imageScanFindings.findingSeverityCounts')
16+
CRITICAL=$(echo $SCAN_FINDINGS | jq '.CRITICAL // 0')
17+
HIGH=$(echo $SCAN_FINDINGS | jq '.HIGH // 0')
18+
19+
ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
20+
IMAGE_DIGEST=$(aws ecr describe-images --repository-name $REPO_NAME --image-ids imageTag=$IMAGE_TAG --query 'imageDetails[].imageDigest' --output text)
21+
REPORT_URL="$(echo "https://$REGION.console.aws.amazon.com/ecr/repositories/private/$ACCOUNT_ID/$REPO_NAME/_/image/$IMAGE_DIGEST/scan-results?region=$REGION")"
22+
SCAN_RESULT="$(echo "Found $CRITICAL CRITICAL and $HIGH HIGH level vulnerabilities in Docker image")"
23+
24+
if [[ $PR_COMMENT == true ]]; then
25+
curl -X POST -H "Content-Type: application/json" -H "Authorization: Bearer $GITHUB_TOKEN" "$URL" -d "{ \"body\": \"Docker Image Scan Output \\n >$SCAN_RESULT \\n For detailed scan report <a href=\\\"$REPORT_URL\\\"> Click here </a>\" }"
26+
fi
27+
28+
if [[ $CRITICAL != 0 || $HIGH != 0 ]]; then
29+
VULNERABILITY=true
30+
fi
31+
fi
32+
HYPERLINK_URL="[Click here]($REPORT_URL)"
33+
echo "VULNERABILITY=$VULNERABILITY" >> $GITHUB_OUTPUT
34+
echo "$SCAN_RESULT" >> $GITHUB_STEP_SUMMARY
35+
echo "Detailed Scan Report - $HYPERLINK_URL" >> $GITHUB_STEP_SUMMARY
36+
37+
else
38+
echo "AWS CLI is not configured."
39+
exit 1
40+
fi
41+
42+
43+

outputs/github-pr-output.png

32.9 KB
Loading

outputs/github-summary-output.png

27.6 KB
Loading

readme.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)