5

Find and fix vulnerabilities in your CI/CD pipeline with Snyk and Harness

 2 years ago
source link: https://snyk.io/blog/find-fix-vulnerabilities-ci-cd-pipeline-snyk-harness/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Find and fix vulnerabilities in your CI/CD pipeline with Snyk and Harness

Pas Apicella, Martin Ansong

December 10, 2021

When DevOps emerged more than ten years ago, the main focus was to bridge the gaps between dev and ops teams. This was achieved by introducing automation to the processes of designing, building, testing, and deploying applications. 

But as development teams continue to deliver faster and more frequently, security teams find it difficult to keep up. Often, they become the bottleneck in the delivery pipeline. For this reason, bringing security early into the DevOps process and embracing a DevSecOps culture has become increasingly important.

Integrating the Snyk developer-focused security platform into Harness’ unified delivery pipeline workflow ensures security and compliance testing is part of every release. This allows you to prevent applications with vulnerable dependencies and code from making their way into production. With modern tooling like Snyk and Harness, you can find, fix, and remediate through a CI/CD pipeline and mitigate the risk to the business without affecting your ability to release software quickly.

Getting started With Snyk

Snyk is a developer security platform that makes it easy for teams to find, prioritize, and fix security vulnerabilities in code, dependencies, containers, and infrastructure as code. It integrates directly into development tools, workflows, and automation pipelines.

Supported by industry-leading application and security intelligence, Snyk puts security expertise in any developer’s toolkit.

Getting started with Snyk is as simple as signing up for a free account.

Secure your CI/CD pipeline with Snyk

Find and fix vulnerabilities automatically within your pipelines and workflows for free.

Getting started With Harness

Harness is the industry’s first software delivery platform that uses AI to simplify your DevOps processes — CI, CD, Feature Flags, Cloud Cost Management, and much more.

Harness enables you to build complex deployments in minutes with reusable templates, saving both time and stress. With Harness, you can build pipelines in minutes.

Getting started with Harness is as simple as heading over to harness.io and setting up a free trial account. 

Simplify DevOps with Harness

Build pipelines with complex deployments and reusable templates in minutes.

Prevent vulnerabilities from passing through the build and deployment process

The best way to demonstrate the combined power of Snyk and Harness is via a walkthrough, which you can set up and run for yourself. In this example, we’ll use Snyk to demonstrate how to prevent vulnerabilities from passing through the build and deployment process by adding automated Snyk tests into a Harness workflow.

We use the following GitHub repository: https://github.com/mansong1/springbootemployee-api

If you wish to follow along, you will need the following:

This tutorial makes the most sense after you’ve completed the following with Harness:

If you’ve completed/reviewed the above, you’ve learned how to set up a Harness CI workflow. 

Seven steps for running Snyk tests in Harness

Step 1: Create a Snyk API token secret

You need a Snyk API token to perform Snyk CLI tests. For enterprise customers, you can set up service accounts and create a token through Snyk App. For non-enterprise Snyk accounts, including our free Snyk tier, you can use the main user token.

Next, create a secret in Harness to store your Snyk API token by following these steps.

Step 2: Create a new pipeline

  1. First, create a new project in Harness by clicking on +Project
blog-harness-add-project-1240x996.jpg
  1. Once you’ve created your project, go to the Continuous Integration module to create a new pipeline.
blog-harness-create-pipeline-1240x794.jpg

Step 3: Add the Codebase

If this is the first CI stage in the pipeline, enable Clone Codebase in the CI Stage settings. If you have an existing pipeline with a CI stage, click Codebase. See Edit Codebase Configuration.

Step 4: Define the build farm infrastructure

In the CI stage infrastructure, define the build farm for the Codebase. See Kubernetes Cluster Build Infrastructure Setup.

Step 5: Configure a Run Snyk test

In the CI Execution, click Add Step and then click Run.

blog-harness-config-test.jpg

The Run step executes one or more commands on a container image.

Snyk offers multiple container images that wrap the Snyk CLI and come with relevant tooling for different languages. In this example, we are using the Maven version since that’s what is used in this sample application.

For step settings on configuring a Run step, see Run Step Settings and add your Snyk API token. 

blog-harness-optional-config.jpg

You can now reference this environment variable in the Command section of the configuration.

blog-harness-config-env-1240x518.jpg

Step 6: Run the pipeline

Now you can run your Pipeline.

  1. Click Save and Publish.
  2. Click Run. The Pipeline Inputs settings appear.
  3. In CI Codebase, click Git Branch.
  4. In the Git Branch, enter the name of the branch where the Codebase is, such as master.
blog-harness-run-pipeline.jpg
  1. Click Run Pipeline.

Step 7: View pipeline execution

When the workflow runs, the Snyk Test step fails because the project includes vulnerable dependencies. We can also click on the Console View toggle to see the full report.

Note: To have better control over your tests, you can pass the severity-threshold flag to the snyk test command with one of the supported options (low|medium|high|critical). With this flag, only vulnerabilities of provided level or higher will be reported

blog-harness-severity-threshold-1240x715.jpg

The entire pipeline is available as YAML, also. In Builds:

  1. Click More Options (︙).
  2. Select Edit Pipeline.
  3. Click YAML.
blog-harness-yaml.jpg

Below, you can see the entire pipeline as YAML. You can edit anything in the pipeline and run it again.

pipeline:
  stages:
      - stage:
            type: CI
            spec:
                infrastructure:
                    type: KubernetesDirect
                    spec:
                        connectorRef: org.GKE
                        namespace: harness-builds
                execution:
                    steps:
                        - step:
                              type: Run
                              spec:
                                  connectorRef: org.Dockerhub
                                  image: snyk/snyk-cli:1.745.0-maven-3.5.4
                                  command: |-
                                      snyk config set api=$SNYK_TOKEN
                                      snyk test
                                  privileged: true
                                  envVariables:
                                      SNYK_TOKEN:  <+secrets.getValue("org.SNYK_TOKEN")>
                                  resources:
                                      limits:
                                          memory: 1Gi
                                          cpu: "1.0"
                              name: Snyk Test
                              identifier: Snyk_Test
                cloneCodebase: true
            name: CI
            identifier: CI
  properties:
      ci:
          codebase:
              repoName: springbootemployee-api
              connectorRef: org.Github
              build: <+input>
  projectIdentifier: Snyk
  orgIdentifier: default
  name: Snyk Test
  identifier: Snyk_Test
  description: Test local project for vulnerabilities.

CI/CD workflow

Besides scanning for application dependencies, vulnerabilities can creep in based on the choice of container used. In addition to that, misconfiguration of cloud resources attributes to the most prevalent cloud vulnerabilities exploited to access cloud data and services.

In this example, we will be building and deploying our application to Kubernetes then monitoring it using Snyk.

To build and push a container in Harness, simply follow these steps. Adding a Snyk container scanning step is similar to the Snyk Test step above. 

To deploy the container to our Kubernetes cluster, see the Harness documentation. In our CD pipeline, we’ll be executing the Snyk test of our Kubernetes manifest before we do that. Adding a snyk iac test step is done via a Shell Script step.

blog-harness-script-1240x682.jpg

The script executed in the Snyk IaC step is as follows:

rm -rf springbootemployee-api || true

git clone https://github.com/mansong1/springbootemployee-api
cd springbootemployee-api

SNYK_TOKEN=<+secrets.getValue("org.SNYK_TOKEN")>
snyk config set api=$SNYK_TOKEN
snyk iac test manifests/<+infra.namespace>/templates/*.yaml

The complete CI/CD workflow looks like this:

blog-harness-complete-1-1240x640.jpg
blog-harness-complete-2-1240x568.jpg

As before, this pipeline can be viewed as YAML and is located in this GitHub repo.

Snyk integrates with Kubernetes, enabling you to import and test your running workloads and identify vulnerabilities in their associated images and configurations. Once imported, Snyk continues to monitor those workloads, identifying additional security issues as new images are deployed and the workload configuration changes.

A successful deployment through our Harness CD workflow shows up in the Snyk UI as follows:

blog-harness-snyk-1240x348.jpg
blog-harness-vuln-1240x916.jpg

Try it now

When choosing a technology platform, it’s important to select one that places the needs of developers at the center of its solutions. It should also focus on remediating issues rather than just reporting them. Platforms with a developer-first approach can integrate security across the pipeline, helping multiple different stakeholders such as devs, sec, and ops teams get a holistic view. This helps embed a mutual DevSecOps mindset and ensures more secure software in the hands of consumers.

Thank you so much for joining us for this tutorial. Don’t forget to try Snyk and Harness today!

Secure your CI/CD pipeline

Find and fix vulnerabilities automatically within your pipelines and workflows for free.

About the authors

Martin Ansong is a Solutions Engineer in the Harness Commercial Sales organization, working to help customers accelerate the build and delivery of their software lifecycle.

Pas Apicella is a Principal Solution Engineer APJ at Snyk, working on the Snyk platform to help customers secure their cloud native applications and enable a DevSecOps culture while still meeting the business needs.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK