Create OPA policies to stop STO pipelines automatically
Create OPA policies to stop pipelines automatically.
You can use Harness Policy as Code to write and enforce policies against your security tests, and to stop your pipelines if a security test has any issues that violate those policies.
You can use Harness Policy as Code to enforce policies such as:
A security test cannot include any issues in a list of severities such as Critical or New Critical.
A security test cannot include any issues for CVEs past a certain age, for example no critical-severity CVEs more than three years old.
A security test cannot include any issues in a list of titles such as
libsqlite3orjavascript.express.security.audit.A security test cannot include any more than 75 occurrences of TAR-related issues (issue title matches regex
".*tar.*").A security test cannot include any issues in a list of reference IDs such as CWE-78 or CVE-2023-52138.
Important notes
This topic assumes that you have a basic knowledge of the following:
Governance policies and how to implement them:
Security Test policy samples
The Harness Policy Library includes the following policy samples that make it easy to create security test policies and enforce them against your scan results.
Warn or Block vulnerabilities by severity
Apply a policy to a scan step to warn or block on any vulnerabilities with the specified severity.
You must copy the entire sample code from the OPA policy library, as described in Create a new Security Tests OPA policy.
Here is a sample policy that you can evaluate using the On Step event for a scan step.
Warn or Block vulnerabilities by reference ID
Apply a policy to a scan step to warn or block on any vulnerabilities in a specific list of CVEs or CWEs.
You must copy the entire sample code from the OPA policy library, as described in Create a new Security Tests OPA policy.
Here is a sample policy that you can evaluate using the On Step event for a scan step.
Warn or Block vulnerabilities by title
Apply a policy to a scan step to warn or block on any vulnerabilities in a specific list of issue titles.
You must copy the entire sample code from the OPA policy library, as described in Create a new Security Tests OPA policy.
You can use the ~ operator to find titles based on Python regular expressions.
Here is a sample policy that you can evaluate using the On Step event for a scan step.
Warn or Block vulnerabilities by number of occurrences
Apply a policy to a scan step to warn or block vulnerabilities based on a set of titles and the maximum allowed number of occurrences for each vulnerability.
You must copy the entire sample code from the OPA policy library, as described in Create a new Security Tests OPA policy.
You can use the ~ operator to find titles based on Python regular expressions.
Here is a sample policy that you can evaluate using the On Step event for a scan step.
Warn or Block vulnerabilities by CVE age
Apply a policy to a scan step to warn or block vulnerabilities based on CVEs by severity and age.
You must copy the entire sample code from the OPA policy library, as described in Create a new Security Tests OPA policy.
Here is a sample policy that you can evaluate using the On Step event for a scan step.
Warn or Block vulnerabilities using STO output variables
You can create policies based on the output variables generated by an STO scan step.
For example, suppose you want a policy to warn or block if a scan step finds any new vulnerabilities with severities of Critical or High. In this case, you can create a policy with the following OPA code:
Warn or Block the pipeline based on the code coverage results
Apply a policy to the scan step to either warn or block the pipeline based on the code coverage value. You can use the sample policy Security Test - Code Coverage. Below is a sample policy for reference:
Warn or Block the pipeline based on external policy failures
Apply a policy to the scan step to either warn or block the pipeline based on the external policy failures. You can use the sample policy Security Tests - External Policy Failures. Below is a sample policy for reference:
Warn or Block vulnerabilities from application layers of your container image
Apply a policy to the scan step to either warn or block the pipeline based on the vulnerabilities found in the application layers of your container image. You can use the following sample policy:
Warn or Block vulnerabilities from base image of your container image
Apply a policy to the scan step to either warn or block the pipeline based on vulnerabilities found in the base image of your container image.
The following sample policy works as follows:
Verifies whether the base image of your container image is approved.
If the base image is approved, no further checks are performed and the policy passes.
If the base image is not approved, it checks for vulnerabilities in the base image and warns or blocks the pipeline based on the severity count of the vulnerabilities.
Warn or Block vulnerabilities based on the EPSS score
Apply a policy to the scan step to either warn or block the pipeline based on the code coverage value. You can use the sample policy Security Test - EPSS score found in issues. Below is a sample policy for reference:
Warn or Block vulnerabilities based on CISA KEV count
Apply a policy to the scan step to warn or block the pipeline when the number of issues on the CISA Known Exploited Vulnerabilities (KEV) catalog exceeds your threshold. You can use the sample policy Security Tests – CISA Known Exploited Vulnerabilities. Below is a sample policy for reference:
Warn or Block Reachable or Exploitable Vulnerabilities reported by the Harness Scanner
Apply a policy to the Harness scan step to either warn or block the pipeline based on the reachability or exploitable vulnerabilities reported by the Harness Scanner.
You can use the sample policy Security Tests - Static Reachability of an Issue. Below is a sample policy for reference:
Workflow description
The following steps describes the end-to-end workflow:
Create a policy set with the policies you want to enforce.
Enforce the policy set in your scan step.
Create a new Security Tests OPA policy
You can create policies at the account or the project scope. Go to your account or project, then select Security and Governance > Policies.
Select Policies (top right) and then New Policy.
Select a Security Tests policy from the Policy samples library.

Select policy sample Select Use this sample (bottom). This copies the entire policy sample to the edit pane (left).

Select policy sample Configure the policy as needed. In this example, the policy excludes vulnerabilities with a severity of Critical.

Select policy sample Test your policy to verify that it works as intended.
Each policy sample includes a set of test data that you can use. In the Testing Terminal, examine the test data and edit it as needed. Then click Test to verify the results.
It is good practice to test both a Success and Failure case for your policy. The following example illustrates this workflow.
In this example, the policy denies on reference ID CWE-1230. In this case, you would do the following:
Search the test results for the string
1230. In this case, the ID is not found.
Select policy sample Click Test. The test succeeds.

Select policy sample Search the test results for the string
cweand edit an entry so it matches the reference ID.
Select policy sample Click Test again. The test fails because the data includes the specified CWE.

Select policy sample
Once you're satisfied that the policy works as intended, save it.
Create a policy set
A policy set is a collection of one or more policies. You combine policies into a set and then include it in a scan step.
Go to Security and Governance > Policies. Then click Policy Sets (top right) and then New Policy Set.
Click New Policy Set. The Policy Set wizard appears.
Overview:
Name — Enter a descriptive name such as myorg/myimage policies.
Entity type this policy applies to = Security Tests
On what event should the policy be set to = On Step
These settings allow you to apply the member policies to a specific step, which you'll define below.
Policy evaluation criteria:
Click Add Policy.
Select the policy you just created and set the pull-down to Error and Exit. This is the action to take if any policies in the set are violated.

Select policy sample Click Apply to add the policy to the set, then Finish to close the Policy Set wizard.
❗ In the Policy Sets page, enable Enforced for your new policy set.

Select policy sample
Enforce the policy in your scan step
Now you can set up your scan step to stop builds automatically when the policy gets violated.
Go to the scan step and click Advanced.
Under Policy Enforcement, click Add/Modify Policy Set and add the policy set you just created.
Click Apply Changes and then save the updated pipeline.

Select policy sample
Set up email notifications for pipeline failures
You have a Policy that fails the pipeline based on an OPA policy. Now you can configure the stage to send an email notification automatically whenever the pipeline fails.
Click Notifications (right-side menu). The New Notification wizard appears.
Set up the notification as follows:
Overview page — Enter a notification name such as Pipeline failed -- NEW_CRITICAL or NEW_HIGH issues detected.
Pipeline Events page — Select Stage Failed for the event that triggers the notification. Then select the stage that has the Policy step you just created.

Notification Method page — Specify Email for the method and specify the recipient emails.
YAML pipeline example
The following pipeline that can generate two different notifications. If the code scan detects any CRITICAL or NEW_CRITICAL issues, it sends an automated email like this:
If the scan finds any NEW_CRITICAL or NEW_HIGH issues, it stops the pipeline execution and sends an email like this:
Here's the full pipeline. Note that the policy and policy set are referenced, but not defined, in the pipeline itself.
Last updated
Was this helpful?