Close

Adopción de un enfoque de DevSecOps con Bitbucket Pipelines y Snyk Pipe

Fotografía de Simon Maple
Simon Maple

Responsable de departamento tecnológico de campo de Snyk

Adopta un enfoque de DevSecOps integrando Snyk en Bitbucket Pipelines y Jira.

Time

5-minute read.

Audience

Developers, security/application teams, and DevOps/DevSecOps engineers.

Prerequisites

You have a Snyk account. Get started here.

You have an Atlassian Bitbucket account. Log in here, or get started here.

This tutorial outlines how to secure your build workflow on Bitbucket Pipelines with Snyk. An important step in securing your environment is to scan and analyze both your application and Linux-based container project for known vulnerabilities, which helps you identify and mitigate security vulnerabilities. The exercises in this tutorial will help secure your application and container by leveraging the Snyk Pipe for Bitbucket Pipelines to scan the application manifest file and the container base image for its dependencies.

The tutorial, How Snyk and Bitbucket Cloud enable DevSecOps, focused on application dependencies. However, by also scanning your container base image you can detect:

  • The operating system (OS) packages installed and managed by the package manager
  • Key binaries —layers that were not installed through the package manager

Based on these results, Snyk provides advice and guidance, including:

  • Origins of the vulnerabilities in OS packages and key binaries
  • Base image upgrade details or a recommendation to rebuild the image
  • Dockerfile layer where the affected package was introduced
  • Fixed-in version of the operating system and key binary packages

Application scanning in your Bitbucket Pipeline

The bitbucket-pipelines.yml file defines your Bitbucket Pipelines builds configuration. If you're new to Bitbucket Pipelines you can learn more about how to get started here.

This tutorial provides a sample bitbucket-pipelines.yml file that contains distinct steps mapped to the workflow. We’ll start by scanning the application, building the Docker image, and then scanning the container image. The following is a closer look at the application scanning step:

scan-app: &scan-app
 - step:
     name: "Scan open source dependencies"
     caches:
       - node
     script:
       - pipe: snyk/snyk-scan:0.4.3
         variables:
           SNYK_TOKEN: $SNYK_TOKEN
           LANGUAGE: "npm"
           PROJECT_FOLDER: "app/goof"
           TARGET_FILE: "package.json"
           CODE_INSIGHTS_RESULTS: "true"
           SEVERITY_THRESHOLD: "high"
           DONT_BREAK_BUILD: "true"
           MONITOR: "false"


This example leverages the Snyk Scan pipe in the pipeline to perform a scan of the application. The source contains a complete, YAML definition of all supported variables, but only those included in this snippet are necessary for this purpose.

Here’s a closer look at a few of these:

1. SNYK_TOKEN is passed into the pipe as a repository variable previously defined in the [Bitbucket Configuration] module.

2. PROJECT_FOLDER is the folder where the project resides and normally defaults to. However, in this example, we set this to app/goof and pass this as an artifact to other steps in ther pipeline.

3. CODE_INSIGHTS_RESULTS defaults to false. However, since we want to create a Code Insight report with Snyk test results, set this to true.

4. SEVERITY_THRESHOLD reports on issues equal or higher to the provided level. The default is low. But in this case, we are interested only in high, so we defined this variable accordingly.

5. The DONT_BREAK_BUILD default is false, which is expected. Under normal circumstances, you would want to break the build if issues are found. However, for the purpose of this learning exercise, set this to true.

Exclamation point

You can run Snyk security scans on your pull requests and view results in Code Insights with the help of the new Snyk Security Connect App on the Atlassian Marketplace. It's easy to get started and you can install the app with just a few clicks.

Análisis de imágenes de contenedores

Diagrama del proceso de Bitbucket

Según Gartner, más del 75 % de las organizaciones internacionales ejecutará aplicaciones en contenedores en el entorno de producción para 2022. En paralelo a esta adopción generalizada, se ha producido un aumento de las vulnerabilidades de los contenedores, con un incremento que cuadruplica las vulnerabilidades de sistemas operativos notificadas en 2018. Sin embargo, el 80 % de los desarrolladores reconoce que no prueba las imágenes de sus contenedores durante la fase de desarrollo. Lo justifican afirmando que esto no entra dentro de sus competencias o que quienes intervienen en etapas posteriores suelen encargarse de las incidencias, de modo que escalar la seguridad de los contenedores supone un gran reto para las empresas de rápida expansión.

Análisis de imágenes de contenedores de tu canalización

Al igual que en la sección anterior sobre análisis de aplicaciones, esta sección se centra en configurar el archivo bitbucket-pipelines.yml para compilar la imagen de Docker de la aplicación, analizarla y, a continuación, enviarla al registro. A continuación, se muestra un análisis más detallado de los pasos que hay que seguir para analizar la imagen de un contenedor:

scan-push-image: &scan-push-image
 - step:
     name: "Scan and push container image"
     services:
       - docker
     script:
       - docker build -t $IMAGE ./app/goof/
       - docker tag $IMAGE $IMAGE:${BITBUCKET_COMMIT}
       - pipe: snyk/snyk-scan:0.4.3
         variables:
           SNYK_TOKEN: $SNYK_TOKEN
           LANGUAGE: "docker"
           IMAGE_NAME: $IMAGE
           PROJECT_FOLDER: "app/goof"
           TARGET_FILE: "Dockerfile"
           CODE_INSIGHTS_RESULTS: "true"
           SEVERITY_THRESHOLD: "high"
           DONT_BREAK_BUILD: "true"
           MONITOR: "false"


Se trata de compilar la imagen del contenedor y etiquetarla para, a continuación, analizarla aprovechando el canal de Snyk Scan de la canalización. Para ello, define las variables CODE_INSIGHTS_RESULTS, SEVERITY_THRESHOLD y DONT_BREAK_BUILD con los mismos valores empleados en el ejercicio anterior. De este modo, también se transfieren algunas variables compatibles adicionales relevantes para Snyk Pipe con el fin de que se entienda la solicitud de un análisis de la imagen del contenedor, en lugar de un análisis de la aplicación. Esto consiste en establecer LANGUAGE como docker, declarar IMAGE_NAME y transferir la variable de repositorio adecuada, así como establecer TARGET_FILE como Dockerfile.

A continuación, tu canalización analiza la imagen del contenedor en busca de vulnerabilidades conocidas, así como el código de tu aplicación.

Ver más integraciones para Atlassian Open DevOps

Simon Maple
Simon Maple

Simon Maple is the Field CTO at Snyk, a Java Champion since 2014, Virtual JUG founder, and London Java Community co-leader. He is an experienced speaker, with a passion for community. When not traveling, Simon enjoys spending quality time with his family, cooking and eating great food.


Compartir este artículo

Lecturas recomendadas

Consulta estos recursos para conocer los tipos de equipos de DevOps o para estar al tanto de las novedades sobre DevOps en Atlassian.

Ilustración de Devops

La comunidad de DevOps

Ilustración de Devops

Taller de simulación

Ilustración de un mapa

Pruébalo gratis

Suscríbete para recibir el boletín de DevOps

Thank you for signing up