This article shows you how to integrate ShiftLeft CORE's NG SAST into your Bitbucket pull request workflow for automated code analysis whenever you create a new pull request.
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.
We recommend creating the following environment variables and app password to facilitate communication between ShiftLeft and Bitbucket, to ensure that the scan can access your application correctly, and (if desired) to print the results of your scan to your merge request.
Please note that the presence of any set environment variables will override those in your configuration files (if present).
|The app password you've set to ensure that ShiftLeft can communicate with Bitbucket|
|The name of your application|
|Set its value as your CI token -- you can create your CI token in the ShiftLeft Dashboard.|
|Specify the application path ShiftLeft should use to identify scan's build target or project directory|
Step 1: Mandate successful merge checks
You can edit your project properties and enforce branch protection rules using an administrative account. To do so:
- Go to Repository settings and launch the branch protection page.
- Click Add a branch permission.
- In the pop-up window, ensure that By name or pattern is selected. Provide an
*to apply this rule to all branches. Then, 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.
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.
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 merging a merge request.
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 pull 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: