diff --git a/README.md b/README.md index 70fd896..0d12f19 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ you are using. We currently support: - [Infrastructure as Code](iac) - [Setup](setup) +Examples for how to use the Snyk GitHub Actions are available in the [guides](https://github.com/snyk/actions/guides) folder. + Here's an example of using one of the Actions, in this case to test a Node.js project: ```yaml diff --git a/guides/find-and-fix-open-source-vulnerabilities-with-snyk.md b/guides/find-and-fix-open-source-vulnerabilities-with-snyk.md new file mode 100644 index 0000000..151fc0d --- /dev/null +++ b/guides/find-and-fix-open-source-vulnerabilities-with-snyk.md @@ -0,0 +1,118 @@ +# Find and fix Open Source vulnerabilities with Snyk + +You can use [Snyk](https://snyk.co/SnykGHGuide) to scan your applications' open source dependencies for security, license, and dependency health issues as part of your continuous integration \(CI\) workflow. + +{% hint style="info" %} +GitHub Actions is available with GitHub Free, GitHub Pro, GitHub Free for organizations, GitHub Team, GitHub Enterprise Cloud, GitHub Enterprise Server, and GitHub One. GitHub Actions is not available for private repositories owned by accounts using legacy per-repository plans. For more information, see "[GitHub's products](https://docs.github.com/articles/github-s-products)." +{% endhint %} + +## In this article + +* Introduction +* Prerequisites +* Scanning files with Snyk Open Source +* Adjusting severity thresholds for Snyk Open Source +* Uploading scan results to the Snyk UI + +## Introduction + +This guide explains how to use GitHub Actions to create a workflow that scans your application's open source dependencies for vulnerabilities with [Snyk Open Source](https://snyk.co/SnykOpenSource). It also covers setting severity thresholds for the Snyk check, and uploading results to the Snyk UI. + +## Prerequisites + +Create a GitHub Actions secret named `SNYK_TOKEN` to store the value for your Snyk Token. You can retrieve it from your [Snyk account settings](https://snyk.co/SnykSignUpGitHubGuide) or with the [Snyk CLI](https://snyk.co/SnykCLI): + +```text +snyk config get api +``` + +For more information on creating secrets for GitHub Actions, see "[Encrypted secrets](https://docs.github.com/en/actions/reference/encrypted-secrets#creating-encrypted-secrets-for-a-repository)." + +This guide assumes that you have an application containing open source dependencies in a GitHub repository. We recommend that you have a basic understanding of workflow configuration options and how to create a workflow file. For more information, see "[Learn GitHub Actions](https://docs.github.com/en/actions/learn-github-actions)." + +## Scanning with Snyk Open Source + +As part of your CI workflow to build your application, you can trigger a workflow to check it for security issues. The workflow in the example below runs when the `pull request` event is triggered. For more information on the `pull request` event, see "[Events that trigger workflows](https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#pull_request)". + +In the example workflow below, we use the `Snyk` action to scan the dependencies specified in a Node.js application's `package.json` file for vulnerabilities and other risks. + +The Snyk Action has properties that are passed to the underlying image using `with`: + +* `args` : override the default arguments to the Snyk image +* `command`: defaults to `test`, specify which command to run +* `json` : defaults to `false`, save the results as `snyk.json` + +```text +name: Scan a Node app for vulnerabilities using Snyk +on: pull_request +jobs: + security: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Snyk Test Vulnerabilities + uses: snyk/actions/node@master + env: + SNYK_TOKEN: ${{ Secrets.SNYK_TOKEN }} +``` + +Whenever a Pull Request is opened, this workflow checks out the code and uses the Snyk Action to scan for vulnerable open source dependencies. Snyk fails the check if any vulnerabilities are found. + +## Adjusting severity thresholds for Snyk Open Source + +You can adjust the severity level of the issues Snyk uses to determine wether to pass the check. For example, you can choose to fail only when high severity issues are found. This is accomplished with the `--severity-threshold` property. Accepted values are `high`, `medium`, and `low`. + +```text +name: Scan a Node app for vulnerabilities using Snyk +on: pull_request +jobs: + security: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Snyk Test for High Severity Vulnerabilities + uses: snyk/actions/node@master + env: + SNYK_TOKEN: ${{ Secrets.SNYK_TOKEN }} + args: --severity-threshold=high +``` + +Whenever a Pull Request is opened, this workflow checks out the code and uses the Snyk Action to scan for vulnerable open source dependencies. If any have High Severity vulnerabilities, Snyk fails the check. + +## Uploading scan results to the Snyk UI + +The default command used by the Snyk Actions is `snyk test`. Changing it to `snyk monitor` uploads a snapshot of our dependencies to the Snyk UI for continuous monitoring. This ensures we're notified of any new vulnerabilities disclosed for our open source components. + +For this last example, we'll upload a snapshot of our application dependencies to Snyk on the `release` event; for more information see "[Events that trigger workflows](https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#release)". + +```text +name: Upload a Snapshot of Open Source dependencies to Snyk +on: + release: + types: [published] +jobs: + security: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Upload Dependency Scan to Snyk Monitor + uses: snyk/actions/node@master + env: + SNYK_TOKEN: ${{ Secrets.SNYK_TOKEN }} + with: + command: monitor +``` + +When a new release in published, this workflow uploads a snapshot of the application's open source dependencies to the Snyk UI for continuous monitoring and alerting on newly disclosed vulnerabilities. + +## Additional Resources + +For more information on Snyk Container, including best practices and other examples, check out: + +* [Snyk Open Source Security Blog](https://snyk.co/SnykBlog) +* Official [Snyk CLI Cheat Sheet](https://snyk.co/CLIcheatsheet) +* Lab: [Securing a Toolchain with Snyk and GitHub](https://snyk.co/SecureToolChain) + diff --git a/guides/scan-terraform-and-kubernetes-files-for-configuration-issues-with-snyk.md b/guides/scan-terraform-and-kubernetes-files-for-configuration-issues-with-snyk.md new file mode 100644 index 0000000..0107f8f --- /dev/null +++ b/guides/scan-terraform-and-kubernetes-files-for-configuration-issues-with-snyk.md @@ -0,0 +1,134 @@ +# Scan Terraform, Helm, and Kubernetes files for issues with Snyk IaC + +You can use [Snyk Infrastructure as Code](https://snyk.co/InfraCode) to scan for security issues in Kubernetes and Terraform files, as part of your continuous integration \(CI\) workflow. + +{% hint style="info" %} +GitHub Actions is available with GitHub Free, GitHub Pro, GitHub Free for organizations, GitHub Team, GitHub Enterprise Cloud, GitHub Enterprise Server, and GitHub One. GitHub Actions is not available for private repositories owned by accounts using legacy per-repository plans. For more information, see "[GitHub's products](https://docs.github.com/articles/github-s-products)." +{% endhint %} + +## In this article + +* Introduction +* Prerequisites +* Scanning files with Snyk IaC +* Adjusting severity thresholds for Snyk IaC +* Uploading IaC scan results to GitHub Security Code Scanning + +## Introduction + +This guide shows you how to create a workflow that scans Kubernetes and/or Terraform files for issues with [Snyk Infrastructure as Code \(IaC\)](https://snyk.co/InfraCode). It also covers setting severity thresholds for the IaC check, and uploading results to GitHub Security. + +## Prerequisites + +Create a GitHub Actions secret named `SNYK_TOKEN` to store the value for your Snyk Token. You can retrieve it from your [Snyk account settings](https://snyk.co/SnykSignUpGitHubGuide) or with the [Snyk CLI](https://snyk.co/SnykCLI): + +```text +snyk config get api +``` + +For more information on creating secrets for GitHub Actions, see "[Encrypted secrets](https://docs.github.com/en/actions/reference/encrypted-secrets#creating-encrypted-secrets-for-a-repository)." + +This guide assumes you have Terraform or Kubernetes manifests stored in a GitHub repository. Supported file extensions are `.tf` and `.yaml` . We recommend that you have a basic understanding of workflow configuration options and how to create a workflow file. For more information, see "[Learn GitHub Actions](https://docs.github.com/en/actions/learn-github-actions)." + +## Scanning files with Snyk IaC + +Each time you update your deployment YAML or Terraform files, it's a good idea to check them for security issues and misconfiguration risks. The example workflow below runs when a `push` event is triggered for the provided file `paths`. For more information on the `push` event, see "[Events that trigger workflows](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#push)". + +In the example workflow below, we use the `Snyk IaC` action to scan a YAML file in a GitHub Repo. + +The `Snyk IaC` Action has properties that are passed to the underlying image using `with`: + +* `args` : override the default arguments to the Snyk IaC image +* `command`: defaults to `test`, specify which command to run +* `file` : the file, or files, to check for issues. +* `json` : defaults to `false`, save the results as `snyk.json` +* `sarif`: default to true, save the results as `snyk.sarif` + +```text +name: Example workflow for Snyk Infrastructure as Code +on: + push: + paths: + - 'your/kubernetes-manifest.yaml' +jobs: + iac-security: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Check Kubernetes manifest file for issues + uses: snyk/actions/iac@master + env: + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} + with: + file: your/kubernetes-manifest.yaml +``` + +The above workflow checks out the GitHub repository, and uses the Snyk `IaC` Action to scan the YAML file for issues. Snyk fails the check if any issues are found. + +## Adjusting severity thresholds for Snyk IaC + +You can adjust the severity level of the issues Snyk uses to determine wether to pass the check. For example, you can choose to fail only when medium severity issues are found . This is accomplished by with the `--severity-threshold` property. Accepted values are `high`, `medium`, and `low`. + +```text +name: Example workflow for Snyk Infrastructure as Code +on: + push: + paths: + - 'your/kubernetes-manifest.yaml' +jobs: + iac-security: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Check Kubernetes manifest file for issues + uses: snyk/actions/iac@master + env: + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} + with: + file: your/kubernetes-manifest.yaml + args: --severity-threshold=medium +``` + +The above workflow checks out the code, and uses the Snyk IaC action to scan the Kubernetes YAML file for issues. If High Severity issues are present, it will fail the check. + +## Uploading IaC scan results to GitHub Security Code Scanning + +The Snyk IaC Action also supports integrating with GitHub Security. When run, a `snyk.sarif` file will be generated which can be uploaded to GitHub Security to show issues in the repo's Security tab.. + +By default, Snyk IaC breaks the workflow when issues are present. You can continue the workflow to always upload results to GitHub Security by setting `continue-on-error`to true. + +```text +name: Example workflow for Snyk Infrastructure as Code +on: + push: + paths: + - 'your/kubernetes-manifest.yaml' +jobs: + iac-security: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Check Kubernetes manifest file for issues + continue-on-error: true + uses: snyk/actions/iac@master + env: + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} + with: + file: your/kubernetes-manifest.yaml + args: --severity-threshold=high + - name: Upload result to GitHub Code Scanning + uses: github/codeql-action/upload-sarif@v1 + with: + sarif_file: snyk.sarif +``` + +The above workflow checks out the code, uses the Snyk Infrastructure as Code action to scan the Kubernetes YAML file for high severity issues, then uploads the results to GitHub Security Code Scanning. + +## Additional Resources + +* Snyk Docs: [Test your Kubernetes files with our CLI tool](https://snyk.co/TestK8sSnykCLI) +* Lab: [Securing a Toolchain with Snyk and GitHub](https://snyk.co/SecureToolChain) + diff --git a/guides/secure-your-container-base-images-with-snyk.md b/guides/secure-your-container-base-images-with-snyk.md new file mode 100644 index 0000000..023f0ec --- /dev/null +++ b/guides/secure-your-container-base-images-with-snyk.md @@ -0,0 +1,170 @@ +# Scan Container Images for Vulnerabilities with Snyk Container + +You can use [Snyk Container](https://snyk.co/Container) to scan your container images for vulnerabilities as part of your continuous integration \(CI\) workflow. + +{% hint style="info" %} +GitHub Actions is available with GitHub Free, GitHub Pro, GitHub Free for organizations, GitHub Team, GitHub Enterprise Cloud, GitHub Enterprise Server, and GitHub One. GitHub Actions is not available for private repositories owned by accounts using legacy per-repository plans. For more information, see "[GitHub's products](https://docs.github.com/articles/github-s-products)." +{% endhint %} + +## In this article + +* Introduction +* Prerequisites +* Scanning files with Snyk Container +* Adjusting severity thresholds for Snyk Container +* Uploading Snyk Container scan results to GitHub Security + +## Introduction + +This guide explains how to use GitHub Actions to create a workflow that scans a container image for vulnerabilities with [Snyk Container.](https://snyk.co/Container) It also covers setting severity thresholds for the Container check, and uploading results to GitHub Security. + +## Prerequisites + +Create a GitHub Actions secret named `SNYK_TOKEN` to store the value for your Snyk Token. You can retrieve it from your [Snyk account settings](https://snyk.co/SnykSignUpGitHubGuide) or with the [Snyk CLI:](https://snyk.co/SnykCLI) + +```text +snyk config get api +``` + +For more information on creating secrets for GitHub Actions, see "[Encrypted secrets](https://docs.github.com/en/actions/reference/encrypted-secrets#creating-encrypted-secrets-for-a-repository)." + +This guide assumes that you have a complete definition and any other files needed to create a container image stored in a GitHub repository. We recommend that you have a basic understanding of workflow configuration options and how to create a workflow file. For more information, see "[Learn GitHub Actions](https://docs.github.com/en/actions/learn-github-actions)." + +## Scanning with Snyk Container + +As part of your CI workflow to build your container image, you can trigger a workflow to check it for security issues. The workflow in the example below runs when the `pull request` event is triggered. For more information on the `pull request` event, see "[Events that trigger workflows](https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#pull_request)". + +In the example workflow below, we use the `Snyk Container` action to scan the container image packaging our application and the `Dockerfile` used to build it. + +The `Snyk Container` Action has properties that are passed to the underlying image using `with`: + +* `args` : override the default arguments to the Snyk Container image +* `command`: defaults to `test`, specify which command to run +* `image` : the name of the image to test +* `json` : defaults to `false`, save the results as `snyk.json` +* `sarif`: default to `true`, save the results as `snyk.sarif` + +```text +name: Build Image and scan for Vulnerabilities with Snyk Container +on: pull_request +jobs: + build_scan_container: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Setup up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Build Docker Image + uses: docker/build-push-action@v2 + with: + push: false + load: true + tags: my-org/my-repo/my-image + - name: Snyk Container Test + uses: snyk/actions/docker@master + env: + SNYK_TOKEN: ${{ Secrets.SNYK_TOKEN }} + with: + image: my-org/my-repo/my-image + args: --file=Dockerfile +``` + +The above workflow checks out the GitHub repository, builds it with Docker Buildx, and uses the Snyk Container Action to scan the image and the `Dockerfile` that built it for issues. Snyk fails the check if any issues are found. + +## Adjusting severity thresholds for Snyk Container + +You can adjust the severity level of the issues Snyk uses to determine wether to pass the check. For example, you can choose to fail only when high severity issues are found. This is accomplished with the `--severity-threshold` property. Accepted values are `high`, `medium`, and `low`. + +```text +name: Publish Image and scan for Vulnerabilities with Snyk Container +on: pull_request +jobs: + build_scan_container: + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Setup up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Build Docker Image + uses: docker/build-push-action@v2 + with: + push: false + load: true + tags: my-org/my-repo/my-image + - name: Snyk Container Test for High Severity Vulnerabilities + uses: snyk/actions/docker@master + env: + SNYK_TOKEN: ${{ Secrets.SNYK_TOKEN }} + with: + image: my-org/my-repo/my-image + args: --file=Dockerfile --severity-threshold=high +``` + +The above workflow triggers whenever a Pull Request is opened and checks out the code, builds the container image with Docker Buildx, and scans it with Snyk Container. If the container image has any High Severity vulnerabilities, Snyk will fail the check. + +## Uploading Snyk Container scan results to GitHub Security + +The Snyk Container Action also supports integrating with GitHub Security. When run, a `snyk.sarif` file will be generated which can be uploaded to GitHub Security to show issues in the repo's Security tab.. + +For this last example, we'll scan the image on the `release` event; for more information see "[Events that trigger workflows](https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads#release)". By default, Snyk Container breaks the workflow when issues are present. You can continue the workflow to always upload results to GitHub Security by setting `continue-on-error`to true. + +```text +name: Publish Image and update Snyk Container scan results in GitHub Security +on: + release: + types: [published] +jobs: + push_to_registry: + name: Push Docker image to GitHub Container Registry + runs-on: ubuntu-latest + steps: + - name: Check out the repo + uses: actions/checkout@v2 + - name: Setup up Docker Buildx + uses: docker/setup-buildx-action@v1 + - name: Authenticate to GitHub Container Registry + uses: docker/login-action@v1 + with: + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build Docker Image + id: docker_build + uses: docker/build-push-action@v2 + with: + push: true + tags: docker.pkg.github.com/my-org/my-repo/my-image:latest + container_security: + name: Update GitHub Security with Snyk Container scan results + runs-on: ubuntu-latest + steps: + - name: Authenticate to GitHub Container Registry + uses: docker/login-action@v1 + with: + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Snyk Container Scan + continue-on-error: true + uses: snyk/actions/docker@master + env: + SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} + with: + image: docker.pkg.github.com/my-org/my-repo/my-image:latest + args: --file=Dockerfile --severity-threshold=high + - name: Upload result to GitHub Code Scanning + uses: github/codeql-action/upload-sarif@v1 + with: + sarif_file: snyk.sarif +``` + +The above workflow checks out the code, builds the container image, pushes it to GitHub Container Registry, then scans with Snyk Container to upload high severity vulnerabilities into the repo's Security tab. + +## Additional Resources + +For more information on Snyk Container, including best practices and other examples, check out: + +* [Snyk Guide to Container Security](https://snyk.co/GuidetoContainerSecurity) +* Official [Snyk CLI Cheat Sheet](https://snyk.co/CLIcheatsheet) +* Lab: [Securing a Toolchain with Snyk and GitHub](https://solutions.snyk.io/partner-workshops/github/securing-a-toolchain-with-snyk-and-github) +