Close

Obtenha o DevSecOps com o Bitbucket Pipelines e o Snyk Pipe

Foto do rosto de Simon Maple
Simon Maple

CTO de campo na Snyk

Obtenha o DevSecOps integrando o Snyk com o Bitbucket Pipelines e o 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.

Verifique as imagens do contêiner

Diagrama do processo do Bitbucket

Até 2022, mais de 75% das empresas globais vão executar aplicativos em contêineres em produção (Gartner). Junto com a adoção generalizada, houve um aumento nas vulnerabilidades de contêineres, com um crescimento quatro vezes maior nas vulnerabilidades relatadas pelo sistema operacional em 2018. E, no entanto, 80% dos desenvolvedores dizem que não testam as imagens de contêiner durante o desenvolvimento. Segundo eles, esse ponto não faz parte das responsabilidades deles ou estão acostumados a repassar esses itens a alguém no futuro, o que torna o dimensionamento da segurança de contêineres um desafio para empresas em rápido crescimento.

Verificação de imagens de contêiner no pipeline

Semelhante à seção anterior sobre Verificação de aplicativos, esta seção se concentra na configuração do arquivo bitbucket-pipelines.yml para criar a imagem do Docker para o aplicativo, verificar a imagem e enviá-la ao registro. Veja a seguir uma análise mais minuciosa da etapa de verificação da imagem do contêiner:

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"


O que cria a imagem do contêiner e a marca e, em seguida, aproveitando o pipe do Snyk Scan no pipeline para realizar uma verificação da imagem do contêiner. Mantenha os mesmos valores para CODE_INSIGHTS_RESULTS, SEVERITY_THRESHOLD e DONT_BREAK_BUILD. Também passa algumas variáveis adicionais compatíveis relevantes ao Snyk Pipe para entender a solicitação de uma verificação de imagem de contêiner em vez de uma verificação de aplicativo. Essa ação significa definir a configuração LANGUAGE para docker, declarar o IMAGE_NAME e passar a variável de repositório adequado, bem como definir o TARGET_FILE para Dockerfile.

O pipeline agora verifica a imagem do contêiner em busca de vulnerabilidades conhecidas, bem como o código do aplicativo.

Veja mais integrações para o 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.


Compartilhe este artigo
Próximo tópico

Leitura recomendada

Marque esses recursos para aprender sobre os tipos de equipes de DevOps ou para obter atualizações contínuas sobre DevOps na Atlassian.

Ilustração DevOps

Comunidade de DevOps

Ilustração DevOps

Workshop de simulação

Ilustração do mapa

Comece gratuitamente

Inscreva-se para receber a newsletter de DevOps

Thank you for signing up