Scan Container in Azure Container Registry with Snyk via Azure DevOps Pipeline

Monday, January 23, 2023

Scan Container in Azure Container Registry with Snyk via Azure DevOps Pipeline

#azure-container-registry #azure-devops #containers #devsecops #docker #security-vulnerabilities #snyk

This article is published at GitHub.You can raise issues, create pull requests or even fork the content...its open source.

In this article, you will learn how to scan a container image in Azure Container Registry with Snyk via Azure DevOps Pipelines.

Prerequisites

The following prerequisites will be required to complete this tutorial:

  • Azure DevOps account. If you don't have an Azure DevOps account, create one for free before you begin.
  • Azure DevOps project, with a code repository that has docker support.
  • Azure account. If you don't have an Azure account, create one for free before you begin.
  • Snyk account. If you don't have a Snyk account, create one for free before you begin.

Add Snyk Extension to Azure DevOps

  1. In the root of an Azure DevOps Organization, select Organization Settings in the bottom left of the screen.

    Azure DevOps Organization Settings

  2. In the General section, select Extensions.

    Azure DevOps Organization Settings General Extensions Navigation

  3. Select Browse marketplace.

    Azure DevOps Extensions Navigation Bar

  4. Search for snyk, and then select Snyk Security Scan.

    Azure DevOps Extension Search Snyk

  5. Select Get if free, and then select Install.

    Azure DevOps Snyk Extension Get It Free

  6. In the Extensions section, Snyk Security Scan is now installed.

    Azure DevOps Snyk Extension Installed

Create Azure Container Registry

  1. In Azure Portal, search for container registry.

    Azure Search for Container Registries

  2. Select + Create.

    Azure Container Registries Navigation Bar Create Link

  3. Enter the following values in the Basics tab, then select the Review + create tab, and then select Create.

    ParameterValue
    SubscriptionYour Azure Subscription
    Resource groupCreate new with a name of your choice
    Registry nameChosen name for the registry
    LocationNorth Europe
    SKUBasic

    Azure

Add Snyk Service Connection to Azure DevOps Project

  1. From the root of an Azure DevOps Project, select Project settings in the bottom left of the screen.

    Azure DevOps Project Settings Link

  2. In the Pipelines section, select Service connections.

    Azure DevOps Project Settings Service Connections Navigation

  3. Select Create service connection.

    Azure DevOps Project Create Service Connection

  4. Search for snyk, then select Next.

    Azure DevOps Service Connection Search Snyk

  5. Enter the following values.

    ParameterValue
    Server URLhttps://snyk.io/
    Snyk API TokenYour Snyk API Token (see below)
    Service connection nameSnyk
    Grant access permission to all pipelinesTrue

    Azure DevOps Snyk Service Connection Setup

    To get the Snyk API Token, in snyk account settings, select General, then select click to show the Key. Copy and paste the key into Azure DevOps Snyk Service Connection Setup Personal API Token box. Snyk Account API Token

Add Azure Container Registry Service Connection to Azure DevOps Project

  1. Complete steps 1 and 2 in Add Snyk Service Connection to Azure DevOps Project.

  2. Select New service connection.

    Azure DevOps Service Connection Navigation Bar

  3. Search for docker, and then select Docker Registry.

    Azure DevOps Service Connection Navigation Bar

  4. Enter the following values.

    ParameterValue
    Registry typeAzure Container Registry
    Authentication TypeService Principal
    SubscriptionYour Azure Subscription
    Azure container registryAzure Container Registry created earlier
    Service connection nameChosen name for the service connection
    Grant access permission to all pipelinesTrue

    Azure DevOps Service Connection Navigation Bar

Add Azure DevOps Snyk Pipeline

  1. In the root of an Azure DevOps Project, select Repos.

    Azure DevOps Repos Files Navigation

  2. Whilst in the root of your repo, select More actions, then + New, and then File.

    Azure DevOps Repos Files Add New File

  3. In the Modal Popup, add the file name snyk-az-pipeline.yml into the New file name text box.

    Azure DevOps Repos Files Add New File Modal Popup

  4. Add the following contents to the snyk-az-pipeline.yml file, replacing {Your...} with your project folder name, azure container registry, repository and login server url (usually ends in .azurecr.io).

    trigger:
    - main
    
    pool:
      vmImage: ubuntu-latest
    
    variables:
      buildConfiguration: 'Release'
    
    jobs:
    - job: Build
      steps: 
      - task: DotNetCoreCLI@2
        continueOnError: false
        inputs:
          command: 'build'
          projects: '{YourProjectFolder}*/**.csproj'
    
    - job: Build_and_Push_Docker_Image
      dependsOn: Build
      displayName: Build & Deploy to Azure Registry
      pool:
        vmImage: ubuntu-latest
      steps:
      - checkout: self
      - task: Docker@2
        displayName: Build an image
        inputs:
          containerRegistry: '{YourAzureContainerRegistryConnectionSetupAbove}'
          repository: '{YourNewAzureContainerRegistryRepository}'
          command: 'build'
          Dockerfile: '{YourProjectFolder}/Dockerfile'
          buildContext: '.'
          tags: 'dev'
    
      - task: SnykSecurityScan@1
        displayName: Scan an image
        inputs:
          serviceConnectionEndpoint: 'Snyk'
          testType: 'container'
          dockerImageName: '{YourAzureContainerRegistryLoginServerUrl}/{YourAzureContainerRegistryRepository}:dev'  
          dockerfilePath: '{YourProjectFolder}/Dockerfile'
          monitorWhen: 'always'
          failOnIssues: true
    
      - task: Docker@2
        displayName: Push an image
        inputs:
          containerRegistry: '{YourAzureContainerRegistryConnectionSetupAbove}'
          repository: '{YourNewAzureContainerRegistryRepository}'
          command: 'push'
          tags: dev
    
  5. In the top right, select Commit.

    Azure DevOps Repos Commit Navigation

  6. In the bottom right, select Commit.

    Azure DevOps Repos Commit Comment

Create and Run Azure DevOps Snyk Pipeline

  1. From the root of an Azure DevOps Project, select Pipelines.

    Azure DevOps Pipelines Navigation

  2. Select Create Pipeline.

    Azure DevOps Pipelines Create Your First Pipeline

  3. In the Connect tab, Select Azure Repos Git.

    Azure DevOps Pipelines Connect Azure Repos Git

  4. In the Select tab, select Existing Azure Pipelines YAML file.

    Azure DevOps Pipelines Configure Existing Azure Pipeline Yaml File

  5. Enter the following values.

    ParameterValue
    Branchmain
    Path/snyk-az-pipeline.yml

    Azure DevOps Pipelines Select Existing Azure Pipeline Yaml File

  6. Review the pipeline snyk-az-pipeline.yml yaml, and then select Run, to run the pipeline.

    Azure DevOps Pipelines Review Pipeline Yaml and Run Pipeline

View Snyk Test Report

  1. Navigate to Pipelines.

    Azure DevOps Pipelines Snyk

  2. In the Runs tab, select a pipeline that has been run.

    Azure DevOps Pipelines Runs Pipelines Run

  3. Select the Snyk Report tab to view the Snyk test report.

    Azure DevOps Pipelines Snyk Test Report

Got a comment?

All my articles are written and managed as Markdown files on GitHub.

Please add an issue or submit a pull request if something is not right on this article or you have a comment.

If you'd like to simply say "thanks", then please send me a so the rest of Twitter can see how awesome my work is.

An unhandled error has occurred. Reload