tcosolutions/betterscan

azure devops integration failing

Closed this issue · 5 comments

Hi @marcinguy I'm trying to integrate betterscan in azure devops pipeline as per documentation but while running SAST task I'm betting below error message

Starting: Static Application Security Test (SAST)

Task : Command line
Description : Run a command line script using Bash on Linux and macOS and cmd.exe on Windows
Version : 2.212.0
Author : Microsoft Corporation
Help : https://docs.microsoft.com/azure/devops/pipelines/tasks/utility/command-line

Generating script.
========================== Starting Command Output ===========================
/bin/bash --noprofile --norc /__w/_temp/78c91d4f-67b0-43bc-8ab3-6df1b970aab7.sh
Switched to a new branch 'master'
/root
/__w/1/s
Loading plugin: git
Loading plugin: trufflehog3
Loading plugin: trojansource
Loading plugin: metrics
Loading plugin: bandit
Loading plugin: brakeman
Loading plugin: phpanalyzer
Loading plugin: gosec
Loading plugin: confused
Loading plugin: pmd
Loading plugin: semgrep
Loading plugin: semgrepdefi
Loading plugin: semgrepjs
Loading plugin: checkov
Loading plugin: kubescape
Loading plugin: insidersecswift
Loading plugin: insiderseckotlin
Loading plugin: insiderseccsharp
Loading plugin: pmdapex
Loading plugin: semgrepccpp
Loading plugin: semgrepjava
Loading plugin: semgrepeslint
Loading plugin: graudit
Loading plugin: text4shell
Loading plugin: yara
Cannot find a checkmate project in the current directory tree, aborting.
##[error]Bash exited with code '255'.
Finishing: Static Application Security Test (SAST)

hi @dkajla

Make sure it is a Git repo, otherwise it will also error out.

On a mobile now. Try this:

  - job: SAST
    displayName: Static Application Security Test (SAST)
    condition: eq(variables['Build.SourceBranch'], 'refs/heads/main')
    pool:
      vmImage: 'ubuntu-latest'
    container: 'scanmycode/scanmycode3-ce:worker-cli'
    steps:
    - script: |
        sudo apt-get update
        sudo apt-get install git-lfs
      displayName: Install git LFS
      
    - checkout: self
      persistCredentials: true

    - script: |
        set -e
        git config --global user.email "azuredevops@microsoft.com"
        git config --global user.name "Azure DevOps" 
        git checkout -b $(Build.SourceBranchName)
        sudo CODE_DIR=$(Build.SourcesDirectory) checkmate init
        sudo CODE_DIR=$(Build.SourcesDirectory) checkmate git init
        sudo CODE_DIR=$(Build.SourcesDirectory) checkmate git analyze --branch $(Build.SourceBranchName)
        checkmate issues html
      displayName: Static Application Security Test (SAST)
      env:
        CODE_DIR: '$(Build.SourcesDirectory)'

    - task: PublishBuildArtifacts@1
      displayName: Publish SAST report
      inputs:
        PathtoPublish: $(Build.SourcesDirectory)
        ArtifactName: CodeAnalysisLogs

    - script: |
        git add .checkmate/db.sqlite
        git commit -m '[no ci] update checkmate db'
        git push origin $(Build.SourceBranchName):$(Build.SourceBranch)
      displayName: Commit and Push checkmate db

Notice two new lines:

        sudo CODE_DIR=$(Build.SourcesDirectory) checkmate init
        sudo CODE_DIR=$(Build.SourcesDirectory) checkmate git init

Please let me know how it works.

Also can you scan this repo and send screenshot from Azure DevOps server showing results:

https://github.com/topcodersonline/samplecode

Hi @marcinguy Thanks for the response.

Yes it did fix the issue I was facing. Additionally, I had to add sudo CODE_DIR=$(Build.SourcesDirectory) ahead of checkmate issues html for the job to complete successfully.

I have few more questions :)

  1. Is the final script needed ? which is committing the db file into repo git commit -m '[no ci] update checkmate db, as its triggering pipeline continuously.
  2. Is there a option that the tool can scan PR's and add comments automatically. Apologies for multiple questions, I couldn't find any detailed documentation about these configurations.

hi @devendrakajala

  1. this step ensures state is preserved (project will not be rescanned each time for checked files)

I think you can do exclusion for ".checkmate" folder with file types exclusion trigger:

https://stackoverflow.com/questions/55731861/exclude-file-types-in-ci-triggers-on-azure-devops-pipelines-builds

https://learn.microsoft.com/en-us/azure/devops/pipelines/build/triggers?view=azure-devops

  1. Scanning PR is possible, then you would see findings for PR only. You should check what Azure DevOps server can do.

Maybe @carlin-q-scott knows more. He contributed Azure DevOps server setup.

P.S @devendrakajala Screenshot of sample repo from Azure DevOps server would be helpful. Maybe you can paste it here or send me on my email. I will add it to documentation.

@devendrakajala The commit should not be triggering CI, as the [no ci] commit message is supposed to prevent that. I've found that they broke support for that particular message recently, but [ci skip] seems to work for me with a BitBucket repo.

Due to another bug I found with Azure DevOps, I ended up persisting my .checkmate folder to blob storage and leaving it out of the git repo entirely. My issue was that the git credentials aren't persisted properly within a container job when using BitBucket as the git repo.

Thanks for your comments guys @marcinguy @carlin-q-scott using [ci skip] helped.