This article shows you how to integrate ShiftLeft CORE's NG SAST into your Bitbucket Merge Request Workflow for automated code analysis whenever you create a new PR.
Integrating NG SAST into your Bitbucket Merge Request workflow is done using the merge checks (please note that merge checks are a premium Bitbucket feature).
Merge checks allow you to recommend or require specific conditions that must be met before someone can merge changes into your master branch.
We assume that you're working with a Bitbucket repo utilizing Bitbucket's CI Pipelines.
Create the following environment variables so that Bitbucket can communicate and use ShiftLeft:
|Your ShiftLeft Public API Token|
|Your Access Token|
You can find your API token by going to Dashboard > Account Settings.
When running in a production environment, we recommend that you use a CI token as the access token. You can create your CI token in the ShiftLeft Dashboard.
Please note that the presence of any set environment variables will override those in a configuration file.
Bitbucket App Password
You will need to create one more environment variable for your Bitbucket app password. If you haven't already do so, create an app password to grant access to your Bitbucket account.
Save this password in an environment variable called APP_PASSWORD.
Step 1: Mandate Successful Merge Checks
Using an administrative account, you can edit your project properties and enforce branch protection rules. To do so, go to Repository settings and launch the branch protection settings and check the Prevent a merge with unresolved merge checks option.
Click Save to proceed.
Step 2: Configure the Bitbucket Pipelines Configuration File and Include Code Analysis
The bitbucket-pipelines.yml file controls the jobs executed by Bitbucket's pipelines, and this is where you can insert the instructions needed to run NG SAST.
You can use Bitbucket's web-based UI to edit your configuration file.
The following snippet demonstrates what you need to add to your config file to build and automate code analysis for a Java app using Bitbucket merge checks and CI/CD pipelines:
Note that the configuration file calls a script called sl-analysis.sh. You will need to add the file, which includes the code shown below, to the root of your repository (be sure to replace the
<path-to-your-app> variable under the Analyze your code section):
Please note that the script above includes a section that runs build rules; we will cover build rules in step 3 of this article.
Step 3: Configure Your Build Rules
NG SAST allows you to create build rules, which define the security approval conditions that must be met before a merge request can be merged.
You can define build rules in a configuration file. By default, the name and location of your configuration file in the root of your repository should be shiftleft.yml. The file should be formatted as follows:
Step 4: Push Your Files to Bitbucket
Be sure to commit and push the files you've just created to Bitbucket (alternatively, if you created any of these files using Bitbucket's web UI, pull your changes to make sure that everything is in sync):
Step 5: Testing the Workflow
At this point, you can test your workflow. Bitbucket will automatically trigger your pipelines whenever your push changes to your repository and create a Merge Request to run NG SAST, analyze your code, and indicate whether the merge should fail or not based on the build rules you define.
For example, if one of your build rules is violated, you will see the following result in your build logs:
Bitbucket will report that a build rule failed:
You will also see a list of the findings as a comment on your Merge Request: