> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/governance/policy-as-code/using-policy-enforcement-on-pipeline-steps.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# Enforce policies on steps

Learn how to use policy as code to enforce policies on pipeline steps.

This topic explains steps to create policies using the Harness Policy As Code and apply them to your pipeline steps. Harness Policy As Code uses the Open Policy Agency (OPA) to store policies on the Harness Platform. Go to [Harness Policy As Code Overview](https://developer.harness.io/feature-flags-classic/troubleshooting-and-resources/troubleshoot-ff-classic/harness-policy-engine.md) for more detail.

### Before you begin <a href="#before-you-begin" id="before-you-begin"></a>

* Go to [Harness Policy As Code Overview](https://developer.harness.io/feature-flags-classic/troubleshooting-and-resources/troubleshoot-ff-classic/harness-policy-engine.md) for an overview of policy as code.
* Make sure you have set up a project and a working pipeline in the Harness platform.
* Policies use OPA authoring language Rego. Go to the following resources to learn Rego:
  * [OPA Policy Authoring](https://academy.styra.com/courses/opa-rego).
  * [Policy Language](https://www.openpolicyagent.org/docs/latest/policy-language/) from OPA.
  * [Rego Cheat Sheet](https://dboles-opa-docs.netlify.app/docs/v0.10.7/rego-cheatsheet/).

### Video of using policies with Harness steps <a href="#video-of-using-policies-with-harness-steps" id="video-of-using-policies-with-harness-steps"></a>

{% embed url="<https://www.loom.com/share/5ca3d3a110694ab6b272ec604998395e>" %}

### Sample pipeline <a href="#sample-pipeline" id="sample-pipeline"></a>

Here is a sample YAML of a pipeline with steps:

```yaml
pipeline:
  name: OPA Policy Pipeline
  identifier: OPA_Policy_Pipeline
  projectIdentifier: ## YOUR PROJECT ID
  orgIdentifier: ## YOUR ORG ID
  tags: {}
  stages:
    - stage:
        name: OPA Sandbox
        identifier: OPA_Sandbox
        description: ""
        type: Custom
        spec:
          execution:
            steps:
              - step:
                  type: ShellScript
                  name: Compute Variable
                  identifier: Compute_Variable
                  timeout: 10m
                  spec:
                    shell: Bash
                    onDelegate: true
                    source:
                      type: Inline
                      spec:
                        script: |-
                          export serviceMsg=<+pipeline.variables.msg>
                          echo serviceMsg
                    environmentVariables: []
                    outputVariables:
                      - name: serviceMsg
                        type: String
                        value: serviceMsg
                  enforce:
                    policySets:
                      - Variable_Sanitization
        tags: {}
  variables:
    - name: msg
      type: String
      description: ""
      value: <+input>

```

### Set up your policy and policy set for the pipeline step <a href="#set-up-your-policy-and-policy-set-for-the-pipeline-step" id="set-up-your-policy-and-policy-set-for-the-pipeline-step"></a>

To apply a policy to a pipeline step, you must:

1. Create a policy
2. Create a policy set
3. Add a policy set to a step in the pipeline
4. Run the pipeline

#### Create a policy <a href="#create-a-policy" id="create-a-policy"></a>

The first step of using policies with your steps is navigating to policies and creating your own policy.

The policy will be coupled with how the step is designed and configured. If the step is more free-form like a shell script step, it is essential that you properly map and capture the outputs of variables you wish to reference in your policy step for validation.

More out-of-the-box steps like the rolling deployment step, have a pre-defined output variable path and make it easier for the policy to be re-used across multiple steps.

For this example, we will be modeling the policy around this shell script step

YAML snippet:

```yaml
- step:
   type: ShellScript
   name: Compute Variable
   identifier: Compute_Variable
   timeout: 10m
   spec:
      shell: Bash
      onDelegate: true
      source:
         type: Inline
         spec:
            script: |-
                     export serviceMsg=<+pipeline.variables.msg>
                     echo serviceMsg
               environmentVariables: []
                  outputVariables:
                   - name: serviceMsg
                     type: String
                     value: serviceMsg
```

Create a policy with the following:

```rego
package pipeline

deny [msg] {
true
  # Show a human friendly message
  input[0].outcome.outputVariables["serviceMsg"]==""
  msg:= "serviceMsg cannot be empty"

}
```

The policy above is checking to make sure that the message is not empty. We want to enforce that all variables have some value that is not an empty value.

#### Create a policy set <a href="#create-a-policy-set" id="create-a-policy-set"></a>

Navigate to your Policy Set and click "+ New Policy Set".

* Provide a name for your policy - (i.e. `Variable Sanitization`)
* Entity Type that this policy set applies to: `Custom`
* On what event should the policy set be evaluated: `On Step`

*Note: Entity type is custom because the step we are enforcing is a custom object type.*

By setting the policy on step users' can now access the outputs and inputs of a step and enforce the policy on them.

#### Add a policy set to a step <a href="#add-a-policy-set-to-a-step" id="add-a-policy-set-to-a-step"></a>

You will need to add your policy set to your step in order for the policy engine to evaluate the outputs of the step and provide a judgment.

Navigate to the *advanced section* of the step and select the *policy enforcement* Accordion.

You will be prompted to pick a policy set to associate with the step.

After the selection, you will see the policy set associated with the step:

#### Run the pipeline <a href="#run-the-pipeline" id="run-the-pipeline"></a>

Do not provide any input into the Run form and just hit run, you will see the policy evaluate and error out with the below message:

You will see the policy evaluate and the corresponding denial message. If you re-run the pipeline and provide input, you will see the pipeline succeed.

In the example below, we provided deploy and the variable was assigned "deploy" and the validation passed

### Troubleshooting <a href="#troubleshooting" id="troubleshooting"></a>

* If the policy set does not appear when you add it to the step, confirm its entity type is set to `Custom` and its evaluation event is set to `On Step`.
* If the pipeline run fails with a denial message instead of the expected validation, check that the output variable name in the policy (for example, `serviceMsg`) matches the output variable defined in the step.
* If the policy evaluates against an empty value when a value was provided, verify the shell script step correctly exports and captures the output variable before the policy runs.

### Related concepts <a href="#see-also" id="see-also"></a>

* [Add Policy Step in Pipeline](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/governance/policy-as-code/add-a-governance-policy-step-to-a-pipeline.md)

{% @harness-feedback/feedback %}
