-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: copa-action initial #1
Merged
ashnamehrotra
merged 57 commits into
project-copacetic:main
from
ashnamehrotra:copa-action-initial
Aug 4, 2023
Merged
Changes from 53 commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
ec9a579
feat: copa-action initial
ashnamehrotra 62c6c21
action.yaml initial
ashnamehrotra 6363d4d
sonatype-lift changes
ashnamehrotra 3db5135
change action versions to digests, use real docker test images, renam…
ashnamehrotra 8da4d28
add buildkit version as param
ashnamehrotra 9aecb5b
Dockerfile changes
ashnamehrotra 033045a
change Docker install approach
ashnamehrotra 6649a37
remove set output from readme
ashnamehrotra aa80612
docker login action digest
ashnamehrotra 184ec31
use
ashnamehrotra efc888a
change checkout repository to copa-action in reference workflow
ashnamehrotra 8a9f3b6
resolve sonatype-lift comments
ashnamehrotra dce247d
update version in readme
ashnamehrotra e28f627
create one layer with packages before copa in dockefile
ashnamehrotra 0c8a9e8
resolve sonatype comments - remove wget and sudo
ashnamehrotra e938931
cleanup
ashnamehrotra aa072f7
add retry to curl commands
ashnamehrotra d0171ab
copa version as param
ashnamehrotra bce0766
0.3.0 as default copa version
ashnamehrotra f7f95a6
specify ./data..
ashnamehrotra f618232
add test
ashnamehrotra dda6c9c
change action to always use latest copa version
ashnamehrotra 95e9bda
optional copa version
ashnamehrotra 606dc88
use docker library nginx image in test
ashnamehrotra 5b43215
remove test dependencies, only use assert_equal
ashnamehrotra c92afc1
ci for bats test
ashnamehrotra d1770cf
run bats in test dir
ashnamehrotra 19401a3
chmod +x entrypoint.sh
ashnamehrotra 052b7d7
fix paths in bats
ashnamehrotra e141358
change order actual vs expected
ashnamehrotra 403b3b6
debug bats in build ci
ashnamehrotra 63b002b
load image, run docker images
ashnamehrotra 2a7ac85
pull nginx image in workflow
ashnamehrotra acbcffd
debug output
ashnamehrotra c992d2c
run entrypoint in separate test
ashnamehrotra 4e483be
change data files to use whole image name
ashnamehrotra 08b644f
change output vars
ashnamehrotra a710e45
check for file
ashnamehrotra 87751c5
use ubuntu-latest
ashnamehrotra a88c974
delete .gitmodules
ashnamehrotra fd8ce84
actions/checkout v3.5.3
ashnamehrotra 71ba4a1
change permission to read-all
ashnamehrotra 42e2ccc
install trivy binary
ashnamehrotra d0e0b3a
test with assert_success
ashnamehrotra 9ba67f5
run buildkitd with host network
ashnamehrotra 896a2b6
cleanup and test
ashnamehrotra 3d97522
debugging
ashnamehrotra b9cf670
debugging - move setup code to ci
ashnamehrotra d339ff3
check for patched image
ashnamehrotra f2fc1e2
fix patched tag
ashnamehrotra 7ba72ab
modify to check patched image exists and run trivy
ashnamehrotra 51f48bf
change to check for vulns
ashnamehrotra 1297a8a
check image exists in bats
ashnamehrotra ea9ba0d
buildkit version env var
ashnamehrotra 236dd9b
new tag instead of appending
ashnamehrotra b349144
remove debugging
ashnamehrotra de6bcb8
update documentation
ashnamehrotra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
name: "build" | ||
on: [push, pull_request] | ||
env: | ||
TRIVY_VERSION: 0.44.0 | ||
COPA_VERSION: 0.3.0 | ||
permissions: read-all | ||
jobs: | ||
build: | ||
name: build | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Setup BATS | ||
uses: mig4/setup-bats@v1 | ||
with: | ||
bats-version: 1.7.0 | ||
|
||
- name: Check out code | ||
uses: actions/[email protected] | ||
|
||
- name: Install Trivy | ||
run: | | ||
curl -fsSL -o trivy.tar.gz https://github.com/aquasecurity/trivy/releases/download/v${{ env.TRIVY_VERSION }}/trivy_${{ env.TRIVY_VERSION }}_Linux-64bit.tar.gz | ||
tar -zxvf trivy.tar.gz | ||
cp trivy /usr/local/bin/ | ||
|
||
- name: Set up Docker | ||
uses: docker/setup-buildx-action@ecf95283f03858871ff00b787d79c419715afc34 | ||
|
||
- name: Pull docker.io/library/nginx:1.21.6 | ||
run: docker pull docker.io/library/nginx:1.21.6 | ||
|
||
- name: Install Copa | ||
run: | | ||
curl --retry 5 -fsSL -o copa.tar.gz https://github.com/project-copacetic/copacetic/releases/download/v${{ env.COPA_VERSION }}/copa_${{ env.COPA_VERSION }}_linux_amd64.tar.gz | ||
tar -zxvf copa.tar.gz | ||
cp copa /usr/local/bin/ | ||
|
||
- name: Bats Test | ||
run: | | ||
docker run --net=host --detach --rm --privileged -p 127.0.0.1:8888:8888 --name buildkitd --entrypoint buildkitd moby/buildkit:v0.12.0 --addr tcp://0.0.0.0:8888 | ||
docker build --build-arg copa_version=${{ env.COPA_VERSION }} -t copa-action . | ||
cd ${{ github.workspace }}/test | ||
docker run --net=host \ | ||
--mount=type=bind,source=$(pwd)/data,target=/data \ | ||
--mount=type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \ | ||
--mount=type=bind,source=$GITHUB_OUTPUT,target=$GITHUB_OUTPUT -e GITHUB_OUTPUT \ | ||
--name=copa-action \ | ||
copa-action 'docker.io/library/nginx:1.21.6' 'nginx.1.21.6.json' '-patched' | ||
docker images | ||
bats --print-output-on-failure ./test.bats |
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,31 @@ | ||
FROM debian:12-slim | ||
|
||
SHELL ["/bin/bash", "-o", "pipefail", "-c"] | ||
|
||
# Get copa_version arg | ||
ARG copa_version | ||
|
||
# Copies your code file from your action repository to the filesystem path `/` of the container | ||
COPY entrypoint.sh /entrypoint.sh | ||
|
||
# Install required packages | ||
RUN apt-get update && \ | ||
ashnamehrotra marked this conversation as resolved.
Show resolved
Hide resolved
ashnamehrotra marked this conversation as resolved.
Show resolved
Hide resolved
ashnamehrotra marked this conversation as resolved.
Show resolved
Hide resolved
ashnamehrotra marked this conversation as resolved.
Show resolved
Hide resolved
ashnamehrotra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
apt-get install -y tar ca-certificates gnupg curl jq --no-install-recommends && \ | ||
# Import Docker GPG key | ||
install -m 0755 -d /etc/apt/keyrings && \ | ||
sozercan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
curl --retry 5 -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && \ | ||
chmod a+r /etc/apt/keyrings/docker.gpg && \ | ||
# Add the Docker repository with the correct key ID | ||
echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \ | ||
tee /etc/apt/sources.list.d/docker.list > /dev/null && \ | ||
# Install Docker | ||
apt-get update && \ | ||
apt-get install -y docker-ce docker-ce-cli containerd.io --no-install-recommends | ||
|
||
# Install Copa | ||
RUN curl --retry 5 -fsSL -o copa.tar.gz https://github.com/project-copacetic/copacetic/releases/download/v${copa_version}/copa_${copa_version}_linux_amd64.tar.gz && \ | ||
tar -zxvf copa.tar.gz && \ | ||
cp copa /usr/local/bin/ | ||
|
||
# Code file to execute when the docker container starts up (`entrypoint.sh`) | ||
ENTRYPOINT ["/entrypoint.sh"] |
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 +1,96 @@ | ||
# copa-action | ||
# Copa Action | ||
|
||
This action patches vulnerable containers using [Copa](https://github.com/project-copacetic/copacetic). | ||
|
||
## Inputs | ||
|
||
## `image` | ||
|
||
**Required** The image reference to patch. | ||
|
||
## `image-report` | ||
|
||
**Required** The trivy json vulnerability report of the image to patch. | ||
|
||
## `patched-tag` | ||
|
||
**Required** The patched image tag to append to the original tag. | ||
|
||
## `copa-version` | ||
|
||
**Optional** The Copa version used in the action, default is latest. | ||
|
||
## Output | ||
|
||
## `patched-image` | ||
|
||
Image reference of the resulting patched image. | ||
|
||
## Example usage | ||
|
||
``` | ||
on: [push] | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
strategy: | ||
fail-fast: false | ||
matrix: | ||
# provide relevant list of images to scan on each run | ||
images: ['docker.io/library/nginx:1.21.6', 'docker.io/openpolicyagent/opa:0.46.0', 'docker.io/library/hello-world:latest'] | ||
|
||
steps: | ||
- name: Checkout repository | ||
sozercan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v0.1.0 | ||
with: | ||
repository: project-copacetic/copa-action | ||
ref: main | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@ecf95283f03858871ff00b787d79c419715afc34 | ||
|
||
- name: Generate Trivy Report | ||
uses: aquasecurity/trivy-action@465a07811f14bebb1938fbed4728c6a1ff8901fc | ||
with: | ||
scan-type: 'image' | ||
format: 'json' | ||
output: 'report.json' | ||
ignore-unfixed: true | ||
vuln-type: 'os' | ||
image-ref: ${{ matrix.images }} | ||
|
||
- name: Check Vuln Count | ||
id: vuln_cout | ||
run: | | ||
report_file="report.json" | ||
vuln_count=$(jq '.Results | length' "$report_file") | ||
echo "vuln_count=$vuln_count" >> $GITHUB_OUTPUT | ||
|
||
- name: Copa Action | ||
if: steps.vuln_cout.outputs.vuln_count != '0' | ||
id: copa | ||
uses: project-copacetic/[email protected] | ||
with: | ||
image: ${{ matrix.images }} | ||
image-report: 'report.json' | ||
patched-tag: '-patched' | ||
buildkit-version: 'v0.11.6' | ||
# optional, default is latest | ||
copa-version: '0.2.0' | ||
|
||
- name: Login to Docker Hub | ||
if: steps.copa.conclusion == 'success' | ||
id: login | ||
uses: docker/login-action@465a07811f14bebb1938fbed4728c6a1ff8901fc | ||
with: | ||
username: 'user' | ||
password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
|
||
- name: Docker Push Patched Image | ||
if: steps.login.conclusion == 'success' | ||
run: | | ||
docker push ${{ steps.copa.outputs.patched-image }} | ||
sozercan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
``` |
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,42 @@ | ||
name: 'Copa Action' | ||
description: 'Patch Vulnerable Images' | ||
inputs: | ||
image: | ||
description: 'The image reference to patch' | ||
required: true | ||
image-report: | ||
description: 'The trivy json report of the image to patch' | ||
required: true | ||
patched-tag: | ||
description: 'The patched image tag to append to the original tag' | ||
required: true | ||
buildkit-version: | ||
description: "Buildkit version to use with Copa" | ||
copa-version: | ||
description: "Copa version to use" | ||
outputs: | ||
patched-image: | ||
description: 'Image reference of patched image' | ||
value: ${{ steps.copa-action.outputs.patched-image }} | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: docker build copa-action | ||
shell: bash | ||
run: | | ||
if [ -z "${{ inputs.copa-version }}" ]; then | ||
latest_tag=$(curl -s "https://api.github.com/repos/project-copacetic/copacetic/releases/latest" | jq -r '.tag_name') | ||
latest_version="${latest:1}" | ||
else | ||
latest_version=${{ inputs.copa-version }} | ||
fi | ||
docker build --build-arg copa_version=${latest_version} -t copa-action . | ||
- name: docker run buildkitd | ||
shell: bash | ||
run: | | ||
docker run --net=host --detach --rm --privileged -p 127.0.0.1:8888:8888 --name buildkitd --entrypoint buildkitd moby/buildkit:${{ inputs.buildkit-version }} --addr tcp://0.0.0.0:8888 | ||
- name: docker run copa-action | ||
id: copa-action | ||
shell: bash | ||
run : | | ||
docker run --net=host --mount=type=bind,source=$(pwd),target=/data --mount=type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock --mount=type=bind,source=$GITHUB_OUTPUT,target=$GITHUB_OUTPUT -e GITHUB_OUTPUT --name=copa-action copa-action ${{ inputs.image }} ${{ inputs.image-report }} ${{ inputs.patched-tag }} |
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,22 @@ | ||
#!/bin/sh | ||
sozercan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
image=$1 | ||
report=$2 | ||
patched_tag=$3 | ||
|
||
# parse image into image name and image tag | ||
image_no_tag=$(echo "$image" | cut -d':' -f1) | ||
old_tag=$(echo "$image" | cut -d':' -f2) | ||
|
||
# new patched image tag | ||
new_tag="$old_tag$patched_tag" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what do you think about just specifying a new tag instead of appending? this way user can specify anything |
||
|
||
# run copa to patch image | ||
if copa patch -i "$image" -r ./data/"$report" -t "$new_tag" --addr tcp://0.0.0.0:8888; | ||
then | ||
patched_image="$image_no_tag:$new_tag" | ||
echo "patched-image=$patched_image" >> "$GITHUB_OUTPUT" | ||
else | ||
echo "Error patching image $image with copa" | ||
exit 1 | ||
fi |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make buildkit version an env so it'll be easier to update