For the complete documentation index, see llms.txt. This page is also available as Markdown.

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 libsqlite3 or javascript.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

Security Test policy samples

Workflow description

The following steps describes the end-to-end workflow:

  1. Create a policy set with the policies you want to enforce.

  2. Enforce the policy set in your scan step.

Create a new Security Tests OPA policy

  1. You can create policies at the account or the project scope. Go to your account or project, then select Security and Governance > Policies.

  2. Select Policies (top right) and then New Policy.

  3. Select a Security Tests policy from the Policy samples library.

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

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

  6. 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:

    1. Search the test results for the string 1230. In this case, the ID is not found.

    2. Click Test. The test succeeds.

    3. Search the test results for the string cwe and edit an entry so it matches the reference ID.

    4. Click Test again. The test fails because the data includes the specified CWE.

  7. 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.

  1. Go to Security and Governance > Policies. Then click Policy Sets (top right) and then New Policy Set.

  2. Click New Policy Set. The Policy Set wizard appears.

  3. Overview:

    1. Name — Enter a descriptive name such as myorg/myimage policies.

    2. Entity type this policy applies to = Security Tests

    3. 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.

  4. Policy evaluation criteria:

    1. Click Add Policy.

    2. 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.

    3. Click Apply to add the policy to the set, then Finish to close the Policy Set wizard.

  5. In the Policy Sets page, enable Enforced for your new policy set.

Enforce the policy in your scan step

Now you can set up your scan step to stop builds automatically when the policy gets violated.

  1. Go to the scan step and click Advanced.

  2. Under Policy Enforcement, click Add/Modify Policy Set and add the policy set you just created.

  3. Click Apply Changes and then save the updated pipeline.

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.

  1. Click Notifications (right-side menu). The New Notification wizard appears.

  2. Set up the notification as follows:

    1. Overview page — Enter a notification name such as Pipeline failed -- NEW_CRITICAL or NEW_HIGH issues detected.

    2. 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.

    3. 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?