Skip to content

Commit

Permalink
feat: add docker files for CLI and update README
Browse files Browse the repository at this point in the history
  • Loading branch information
odinn1984 committed Dec 7, 2017
1 parent 633ecd1 commit 23f426c
Show file tree
Hide file tree
Showing 7 changed files with 611 additions and 5 deletions.
128 changes: 123 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,38 @@ Snyk helps you find, fix and monitor for known vulnerabilities in Node.js npm, R

[Full documentation is available on snyk.io](https://snyk.io/docs/using-snyk/?utm_campaign=docs&utm_medium=github&utm_source=full_docs)

## Table Of Contents:

- [Installation](#installation)
- [CLI](#cli)
- [Features](#features)
- [Build](#build)
- [Docker](#docker)
- [Badge](#badge)

## Installation

1. Install the Snyk utility using `npm install -g snyk`.
2. Once installed you will need to authenticate with your Snyk account: `snyk auth`

For more detail on how to authenticate take a look at the [CLI authentication](https://snyk.io/docs/using-snyk#authentication?utm_campaign=docs&utm_medium=github&utm_source=CLI_authentication) section of the Snyk documentation.
For more detail on how to authenticate take a look at the [CLI authentication](https://snyk.io/docs/using-snyk#authentication?utm_campaign=docs&utm_medium=github&utm_source=CLI_authentication) section of the Snyk documentation.

## CLI

```console
snyk [options] [command] [package]
```

Run `snyk --help` to get a quick overview of all commands or for full details on the CLI read the snyk.io [CLI docs](https://snyk.io/docs/using-snyk?utm_campaign=docs&utm_medium=github&utm_source=cli).
Run `snyk --help` to get a quick overview of all commands or for full details on the CLI read the snyk.io [CLI docs](https://snyk.io/docs/using-snyk?utm_campaign=docs&utm_medium=github&utm_source=cli).

The package argument is optional. If no package is given, Snyk will run the command against the current working directory allowing you test you non-public applications.


## Features
- **Find** known vulnerabilities by running `snyk test` on a project either as a one off or as part of your CI process.
- **Fix** vulnerabilities using `snyk wizard` and `snyk protect`.
- `snyk wizard` walks you through finding and fixing know vulnerabilities in your project. Remidiation options include configuring your policy file to update, auto patch and ignore vulnerabilities. (npm only)
- `snyk protect` your code from vulnerabilities by applying patches and optionally suppressing specific vulnerabilities.
- **Fix** vulnerabilities using `snyk wizard` and `snyk protect`.
- `snyk wizard` walks you through finding and fixing know vulnerabilities in your project. Remediation options include configuring your policy file to update, auto patch and ignore vulnerabilities. (npm only)
- `snyk protect` your code from vulnerabilities by applying patches and optionally suppressing specific vulnerabilities.
- **Alert** `snyk monitor` records the state of dependencies and any vulnerabilities on snyk.io so you can be alerted when new vulnerabilities or updates/patches are disclosed that affect your repositories.
- **Prevent** new vulnerable dependencies from being added to your project by running `snyk test` as part of your CI to fail tests when vulnerable Node.js or Ruby dependencies are added.

Expand All @@ -48,6 +56,114 @@ This will create a `dist` directory with the minimal lodash file.
When using the package via npm, the build is not needed as the `dist` directory is already included in the npm package.


## Docker

Snyk is also provided as a set of Docker images thatcarry the runtime environment of each package manager. For example the npm image will carry all of the needed setup to run `npm install` on the currently running container. Currently there are images for npm, Ruby, Maven, Gradle and SBT.

The images can perform `snyk test` by default on the specified project which is mounted to the container as a read/write volume and `snyk monitor` when the `MONITOR` environment variable is set when running the docker container. When running `snyk monitor` with the `GENERATE_REPORT` environment variable set an html file called `snyk-report.html` and a CSS file called `snyk-report.css` will be generated. The image also writes a file called `snyk-res.json` for internal use and `snyk-error.log` for errors that we can look at if something goes wrong.


The following environment variables can be used when running the container on docker:

- `SNYK_TOKEN` - Snyk API token, obtained from [https://snyk.io/account](https://snyk.io/account).
- `USER_ID` - [OPTIONAL] Current user ID on the host machine. If not provided will take the user ID of the currently running user inside the container. This is used for CI builds such as Jenkins where we are running with a non-privileged user and want to allow the user to access the mounted project folder.
- `MONITOR` - [OPTIONAL] If set, tells the image that we want to run `snyk monitor` after running `snyk test`.
- `TARGET_FILE_DIR` - [OPTIONAL] If set, this will cd to the directory inside the mounted project dir to run snyk inside it.
- `GENERATE_REPORT` - [OPTIONAL] if set, this will generate the HTML report with a summary of the vulnerabilities detected by snyk.

Docker images are tagged according to the package manager runtime they include the package manager version and snyk version.
The general format of tags is [snyk-version]-[package-manager]-[package-manager-version] or [package-manager]-[package-manager-version] if we want to use the latest version of snyk. Please see available tags to see the available options.

[snyk-version] - The version of snyk that is installed in the image, if version is omitted it will use the latest version.
[package-manager] - One of the available package managers (e.g: mvn, gradle, etc...).
[package-manager-version] - The version of the package manager that is installed inside the image.

### NodeJS


We will need to mount the project root folder when running the image so that Snyk can access the code within the container. The host project folder will be mounted to `/project` on the container and will be used to read the dependencies file and write results for CI builds. Please see the following examples on how to run Snyk inside docker:

This is an example of running `snyk test` and `snyk monitor` in the image with Snyk latest version

```
docker run -it
-e "SNYK_TOKEN=<TOKEN>"
-e "USER_ID=1234"
-e "MONITOR=true"
-e "TARGET_FILE=package.json"
-v "<PROJECT_DIRECTORY>:/project"
snyk/snyk-cli:npm test --org=my-org-name
```

### RubyGems

We will need to mount the project root folder when running the image so that Snyk can access the code within the container. The host project folder will be mounted to `/project` on the container and will be used to read the dependencies file and write results for CI builds. Please see the following examples on how to run Snyk inside docker:

This is an example of running `snyk test` and `snyk monitor` in the image with Snyk latest version

```
docker run -it
-e "SNYK_TOKEN=<TOKEN>"
-e "USER_ID=1234"
-e "MONITOR=true"
-e "TARGET_FILE=Gemfile.lock"
-v "<PROJECT_DIRECTORY>:/project"
snyk/snyk-cli:rubygems test --org=my-org-name
```

### Maven 3.5.2

We will need to mount the project root folder when running the image so that Snyk can access the code within the container and mount the local .m2 and .ivy2 folders. The host project folder will be mounted to `/project` on the container and will be used to read the dependencies file and write results for CI builds. Please see the following examples on how to run Snyk inside docker:

This is an example of running `snyk test` and `snyk monitor` in the image with Snyk latest version

```
docker run -it
-e "SNYK_TOKEN=<TOKEN>"
-e "USER_ID=1234"
-e "MONITOR=true"
-v "<PROJECT_DIRECTORY>:/project"
-v "/home/user/.m2:/home/node/.m2"
-v "/home/user/.ivy2:/home/node/.ivy2"
snyk/snyk-cli:mvn-3.5.2 test --org=my-org-name
```

### SBT 0.13.16

We will need to mount the project root folder when running the image so that Snyk can access the code within the container and mount the local .m2 and .ivy2 folders. The host project folder will be mounted to `/project` on the container and will be used to read the dependencies file and write results for CI builds. Please see the following examples on how to run Snyk inside docker:

dependency-tree module is required as a global module in version 0.8.2. Please see [this repo](https://github.com/jrudolph/sbt-dependency-graph/tree/v0.8.2) for installation instructions.

This is an example of running `snyk test` and `snyk monitor` in the image with Snyk latest version

```
docker run -it
-e "SNYK_TOKEN=<TOKEN>"
-e "USER_ID=1234"
-e "MONITOR=true"
-v "<PROJECT_DIRECTORY>:/project"
-v "/home/user/.m2:/home/node/.m2"
-v "/home/user/.ivy2:/home/node/.ivy2"
snyk/snyk-cli:sbt-0.13.16 test --org=my-org-name
```

### Gradle 2.8

We will need to mount the project root folder when running the image so that Snyk can access the code within the container and mount the local .m2 and .ivy2 folders. The host project folder will be mounted to `/project` on the container and will be used to read the dependencies file and write results for CI builds. Please see the following examples on how to run Snyk inside docker:

This is an example of running `snyk test` and `snyk monitor` in the image with Snyk latest version

```
docker run -it
-e "SNYK_TOKEN=<TOKEN>"
-e "USER_ID=1234"
-e "MONITOR=true"
-v "<PROJECT_DIRECTORY>:/project"
-v "/home/user/.m2:/home/node/.m2"
-v "/home/user/.ivy2:/home/node/.ivy2"
snyk/snyk-cli:gradle-2.8 test [args]
```

## Badge

Once you’re vulnerability free, you can put a badge on your README showing your package has no known security holes. This will show your users you care about security, and tell them that they should care too.
Expand Down Expand Up @@ -76,3 +192,5 @@ Markdown:


[![Analytics](https://ga-beacon.appspot.com/UA-69111857-2/Snyk/snyk?pixel)](https://snyk.io/)


43 changes: 43 additions & 0 deletions docker/Dockerfile.gradle-2.8
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
FROM node:8-slim

MAINTAINER Snyk Ltd

# Install Java 8
RUN echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee /etc/apt/sources.list.d/webupd8team-java.list
RUN echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list

# Accept license non-iteractive
RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
RUN apt-get update
RUN apt-get install -y oracle-java8-installer oracle-java8-set-default

#Install gradle
RUN curl -L https://services.gradle.org/distributions/gradle-2.8-bin.zip -o gradle-2.8-bin.zip && \
apt-get install -y unzip && \
unzip gradle-2.8-bin.zip -d /home/node/

# Install snyk cli
RUN npm install --global snyk snyk-to-html && \
apt-get update && \
apt-get install jq

ENV GRADLE_HOME=/home/node/gradle-2.8
ENV PATH=$PATH:$GRADLE_HOME/bin

RUN chmod -R a+wrx /home/node
WORKDIR /home/node
ENV HOME /home/node
ENV M2 /home/node/.m2

# The path at which the project is mounted (-v runtime arg)
ENV PROJECT_PATH /project

ADD docker-entrypoint.sh .
ADD snyk_report.css .

ENTRYPOINT ["./docker-entrypoint.sh"]

# Default command is `snyk test`
# Override with `docker run ... snyk/snyk-cli <command> <args>`
CMD ["test"]
42 changes: 42 additions & 0 deletions docker/Dockerfile.maven-3.5.2
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
FROM node:8-slim

MAINTAINER Snyk Ltd

# Install Java 8
RUN echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee /etc/apt/sources.list.d/webupd8team-java.list
RUN echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list

# Accept license non-iteractive
RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
RUN apt-get update
RUN apt-get install -y oracle-java8-installer oracle-java8-set-default

#Install maven
RUN wget http://www-eu.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
RUN tar -xvzf apache-maven-3.5.2-bin.tar.gz
RUN rm -f apache-maven-3.5.2-bin.tar.gz

# Install snyk cli
RUN npm install --global snyk snyk-to-html && \
apt-get update && \
apt-get install jq

RUN chmod -R a+wrx /home/node
WORKDIR /home/node
ENV HOME /home/node
ENV M2 /home/node/.m2
ENV PATH /apache-maven-3.5.2/bin:$PATH

# The path at which the project is mounted (-v runtime arg)
ENV PROJECT_PATH /project

ADD docker-entrypoint.sh .
ADD snyk_report.css .

ENTRYPOINT ["./docker-entrypoint.sh"]

# Default command is `snyk test`
# Override with `docker run ... snyk/snyk-cli <command> <args>`
CMD ["test"]

25 changes: 25 additions & 0 deletions docker/Dockerfile.npm_ruby
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM node:8-slim

MAINTAINER Snyk Ltd

# Install snyk cli
RUN npm install --global snyk snyk-to-html && \
apt-get update && \
apt-get install jq

RUN chmod -R a+wrx /home/node
WORKDIR /home/node
ENV HOME /home/node

# The path at which the project is mounted (-v runtime arg)
ENV PROJECT_PATH /project

ADD docker-entrypoint.sh .
ADD snyk_report.css .

ENTRYPOINT ["./docker-entrypoint.sh"]

# Default command is `snyk test`
# Override with `docker run ... snyk/snyk-cli <command> <args>`
CMD ["test"]

47 changes: 47 additions & 0 deletions docker/Dockerfile.sbt-0.13.16
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
FROM node:8-slim

MAINTAINER Snyk Ltd

# Install Java 8
RUN echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee /etc/apt/sources.list.d/webupd8team-java.list
RUN echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | tee -a /etc/apt/sources.list.d/webupd8team-java.list

# Accept license non-iteractive
RUN echo oracle-java8-installer shared/accepted-oracle-license-v1-1 select true | /usr/bin/debconf-set-selections
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
RUN apt-get update
RUN apt-get install -y oracle-java8-installer oracle-java8-set-default

#Install sbt
RUN echo "deb https://dl.bintray.com/sbt/debian /" | tee -a /etc/apt/sources.list.d/sbt.list && \
apt-get install apt-transport-https && \
curl -L -o sbt.deb http://dl.bintray.com/sbt/debian/sbt-0.13.16.deb && \
dpkg -i sbt.deb

RUN echo "docker-user ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers && \
mkdir -p /home/node/.sbt/0.13/plugins && \
echo "addSbtPlugin(\"net.virtual-void\" % \"sbt-dependency-graph\" % \"0.8.2\")" >> /home/node/.sbt/0.13/plugins/build.sbt && \
echo "net.virtualvoid.sbt.graph.DependencyGraphSettings.graphSettings" >> /home/node/.sbt/0.13/user.sbt && \
echo "-sbt-version 0.13.16" >> /etc/sbt/sbtopts

# Install snyk cli
RUN npm install --global snyk snyk-to-html && \
apt-get update && \
apt-get install -y jq

RUN chmod -R a+wrx /home/node
WORKDIR /home/node
ENV HOME /home/node
ENV M2 /home/node/.m2

# The path at which the project is mounted (-v runtime arg)
ENV PROJECT_PATH /project

ADD docker-entrypoint.sh .
ADD snyk_report.css .

ENTRYPOINT ["./docker-entrypoint.sh"]

# Default command is `snyk test`
# Override with `docker run ... snyk/snyk-cli <command> <args>`
CMD ["test"]
68 changes: 68 additions & 0 deletions docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash

OUTPUT_FILE=snyk-result.json
ERROR_FILE=snyk-error.log
HTML_FILE=snyk_report.html
SNYK_COMMAND="$1"
SNYK_PARAMS="${@:2}"

if [ -z $USER_ID ]; then
USER_ID=`id -u`
fi

if [ $USER_ID -ne 0 ]; then
useradd -m -o -u $USER_ID -d /home/node docker-user 2>/dev/null
fi

runCmdAsDockerUser () {
if [ $USER_ID -ne 0 ]; then
su docker-user -m -c "$1; status=$?"
else
bash -c "$1; status=$?"
fi

return $status
}

exitWithMsg () {
echo "Failed to run the process ..."

if [ -f $1 ]; then
cat "$1"
else
echo "$1"
fi

exit $2
}

if [ -z $SNYK_TOKEN ]; then
exitWithMsg "Missing \$SNYK_TOKEN" 1
fi

cd "$PROJECT_PATH/$TARGET_FILE_DIR"

runCmdAsDockerUser "PATH=$PATH snyk $SNYK_COMMAND $SNYK_PARAMS --json > $OUTPUT_FILE 2>$ERROR_FILE"

RC=$?

if [ $RC -ne "0" ] && [ $RC -ne "1" ]; then
exitWithMsg "$OUTPUT_FILE" $RC
fi

if [ ! -z $MONITOR ]; then
runCmdAsDockerUser "PATH=$PATH snyk monitor $SNYK_PARAMS"
fi

if [ ! -z $GENERATE_REPORT ]; then
runCmdAsDockerUser "cat $OUTPUT_FILE | jq '.vulnerabilities|= map(. + {severity_numeric: (if(.severity) == \"high\" then 1 else (if(.severity) == \"medium\" then 2 else (if(.severity) == \"low\" then 3 else 4 end) end) end)}) |.vulnerabilities |= sort_by(.severity_numeric) | del(.vulnerabilities[].severity_numeric)' | snyk-to-html | sed 's/<\/head>/ <link rel=\"stylesheet\" href=\"snyk_report.css\"><\/head>/' > $HTML_FILE"
runCmdAsDockerUser "cat /home/node/snyk_report.css > snyk_report.css"
fi

if [ $RC -ne "0" ]; then
exitWithMsg "$OUTPUT_FILE" $RC
fi

cat $OUTPUT_FILE

exit $RC
Loading

0 comments on commit 23f426c

Please sign in to comment.