Skip to content
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

Add option to create docker image provenance #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jobs:
- run: ./builder/build.sh -c -B MYCOOLARG=iLikeTests centos-7
# - Second one will use the vendor cache, but the Docker layer cache gets invalidated by the new cache file
- run: ./builder/build.sh -c -B MYCOOLARG=iLikeTests centos-7
# - Third one is very fast due to the Docker layer cache
- run: ./builder/build.sh -c -B MYCOOLARG=iLikeTests centos-7
# - Third one is very fast due to the Docker layer cache, also generates provenance
- run: ./builder/build.sh -c -B MYCOOLARG=iLikeTests -t docker-images.json centos-7
- run: ../tests/test-docker-provenance.sh docker-images.json
# Do a reproducible centos-8 build (does not work for centos-7)
- run: ../tests/test-centos-8-reproducible.sh

26 changes: 25 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ usage() {
echo " -C - Run docker build with --no-cache"
echo " -L <limit>=<softlimit>:<hardlimit> - Overrides the default docker daemon ulimits, can be passed more than once"
echo " -P - Run docker build with --pull"
echo " -t FILENAME - Generate a JSON file with provenance of the docker images used"
echo
echo "Kaniko mode options, ignored in docker mode:"
echo " -k URL - Use URL as the cache for kaniko layers."
Expand All @@ -129,6 +130,7 @@ declare -a ulimitargs
declare -a buildargs
verbose=""
quiet=""
docker_provenance=""
dockeroutdev=/dev/stdout
forcetests=
buildmode=docker
Expand All @@ -146,7 +148,7 @@ BUILDER_MODULES=''
package_match=""
cache_buster=""

while getopts ":CcKk:V:R:svqm:Pp:b:e:B:L:r:" opt; do
while getopts ":CcKk:V:R:svqm:Pp:b:e:B:L:r:t:" opt; do
case $opt in
C) dockeropts+=('--no-cache')
;;
Expand Down Expand Up @@ -201,6 +203,8 @@ while getopts ":CcKk:V:R:svqm:Pp:b:e:B:L:r:" opt; do
;;
L) ulimitargs+=("--ulimit" "${OPTARG}")
;;
t) docker_provenance="${OPTARG}"
;;
\?) echo "Invalid option: -$OPTARG" >&2
usage
;;
Expand Down Expand Up @@ -384,6 +388,26 @@ else
fi
fi

if [ ! -z "${docker_provenance}" ]; then
echo '[' > ${docker_provenance}
declare -a ignored_images
set_comma=''
while read line; do
line_elems=($line)
if [[ "${line_elems[0]}" =~ [Ff][Rr][Oo][Mm] && ! "${ignored_images[*]}" =~ "${line_elems[1]}" ]]; then
if [ ! -z "${set_comma}" ]; then
echo ',' >> ${docker_provenance}
fi
docker image inspect -f '{"uri": "docker:{{ index .RepoTags 0 }}", "digest": { {{ $s := split .ID ":" }}"{{ index $s 0}}": "{{ index $s 1}}" } }' ${line_elems[1]} >> ${docker_provenance}
set_comma="y"
fi
if [[ "${line_elems[2]}" =~ [Aa][Ss] ]]; then
ignored_images+=("${line_elems[3]}")
fi
done < ${dockerfilepath}
echo ']' >> ${docker_provenance}
fi

#######################################################################
# Copy artifacts out of the image through a container
#
Expand Down
40 changes: 40 additions & 0 deletions tests/test-docker-provenance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

# We mostly test if we can parse the outputfile and if it is not too short

if [ -z "$1" ]; then
echo "No input file specified" >&2
exit 1
fi

if [ -z $(command -v jq) ]; then
echo "jq not installed" >&2
exit 1
fi

echo -n "+ Checking if json is valid... "
out=$(jq < "${1}" 2>&1)
if [ $? -ne 0 ]; then
echo "failed"
echo "error: $out"
echo "file contents"
echo "==============================="
cat "${1}"
echo "==============================="
exit 1
fi
echo "ok"

echo -n "+ Checking if we did output enough image data... "
out=$(jq < "${1}" | wc -l)
if [ $out -le 4 ]; then
echo "failed"
echo "==============================="
echo "file contents: "
echo "==============================="
cat "${1}"
exit 1
fi
echo "ok"

exit 0