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

chore: Add snyk docker scanning feature #289

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions build_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set -o pipefail

# shellcheck source=common_functions.sh
source ./common_functions.sh
# shellcheck source=snyk.sh
source ./snyk.sh

for ver in ${supported_versions}
do
Expand Down
33 changes: 24 additions & 9 deletions build_latest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ version="9"

# shellcheck source=common_functions.sh
source ./common_functions.sh
# shellcheck source=snyk.sh
source ./snyk.sh

# shellcheck source=dockerfile_functions.sh
source ./dockerfile_functions.sh

Expand All @@ -43,25 +46,37 @@ function build_image() {
build=$1; shift;
btype=$1; shift;

tags=""
for tag in "$@"
do
tags="${tags} -t ${repo}:${tag}"
echo "docker push ${repo}:${tag}" >> "${push_cmdfile}"
done
local tags=("${@[@]}") # copy arguments to local array
for i in "${tags[@]}"
do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace embedding

echo "docker push ${repo}:${tags[$i]}" >> "${push_cmdfile}"
done

printf -v expanded_tags "-t ${repo}:%s " "${tags[@]}" # concatenate to single strin : -t repo:tag -t repo:tag2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitepsace again?

expanded_tags=${expanded_tags%?} # remove trailing space
dockerfile="Dockerfile.${vm}.${build}.${btype}"

echo "#####################################################"
echo "INFO: docker build --no-cache ${tags} -f ${dockerfile} ."
echo "INFO: docker build --no-cache ${expanded_tags} -f ${dockerfile} ."
echo "#####################################################"
# shellcheck disable=SC2086 # ignoring ${tags} due to whitespace problem
if ! docker build --pull --no-cache ${tags} -f "${dockerfile}" . ; then
if ! docker build --pull --no-cache ${expanded_tags} -f "${dockerfile}" . ; then
echo "#############################################"
echo
echo "ERROR: Docker build of image: ${tags} from ${dockerfile} failed."
echo "ERROR: Docker build of image: ${expanded_tags} from ${dockerfile} failed."
echo
echo "#############################################"
else
if ((SNYK_ENABLED)); then
echo "#####################################################"
echo " Scanning with snyk for vulnerabilities "
echo "#####################################################"
for i in "${tags[@]}"
do
printf "...scanning %s" "${tags[$i]}"
snyk test --docker "${tags[$i]}" --file=="${dockerfile}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whitepspace

done
fi
fi
}

Expand Down
32 changes: 32 additions & 0 deletions snyk.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env bash
if [ -z "${SNYK_AUTH_TOKEN}" ];then
printf "Snyk authentication token not set, skipping snyk analysis"
return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't you exit?

fi
set -o pipefail
export SNYK_ENABLED=0
if test -f "$HOME/.nvm/nvm.sh"; then
echo "nvm found"
else
echo "No nvm on machine, snyk check will be skipped"
exit 0
fi

# shellcheck disable=SC1090
source "$HOME/.nvm/nvm.sh"

echo "Installing node..."
nvm install node

echo "Installing snyk.."
npm install -g snyk

echo "Snyk version: $(snyk -v)"

export SNYK_ENABLED=1
printf "Snyk installed succesfully\n"
printf "Authenticating snyk\n"

snyk auth "${SNYK_AUTH_TOKEN}"


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kill extra whitespace