> 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/add-a-policy-engine-step-to-a-secret.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# Policy as Code for secrets

Learn how to use policy as code for secrets.

Harness provides governance using Open Policy Agent (OPA), Policy Management, and Rego policies.

You can create the policy and apply it to all Secrets in your Account, Org, and Project. The policy is evaluated on Secret-level events like **On Save,** which occurs during Secret Creation and Updates. Go to [Harness Governance Quickstart](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/governance/policy-as-code/harness-governance-quickstart.md) for details.​

This topic describes how to add a Policy step to a Secret.

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

* [Harness Governance Overview](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/governance/policy-as-code/harness-governance-overview.md)
* [Harness Governance Quickstart](https://developer.harness.io/harness-platform/3.0/harness-platform-resources/governance/policy-as-code/harness-governance-quickstart.md).
* Policies use OPA authoring language Rego. Go to [OPA Policy Authoring](https://academy.styra.com/courses/opa-rego) to learn Rego syntax.

### Limitations <a href="#limitations" id="limitations"></a>

* The policies that can be enforced are currently restricted by the Harness entities supported by the OPA service.

### Step 1: Add a Policy <a href="#step-1-add-a-policy" id="step-1-add-a-policy"></a>

In Harness, go to **Account Settings**.

Select **Policies**.

Select **Policies**, and then select **New Policy**. **New Policy** settings appear.

Enter a **Name** for your policy and click **Apply**.

Next, you need to add your policy. Enter your own Rego policy. For example:

```rego
package docexamplepolicy  
  
deny {   
     input.secret.description = "Secret description"  
     }
```

Click **Save**.

### Step 2: Add the Policy to a Policy Set <a href="#step-2-add-the-policy-to-a-policy-set" id="step-2-add-the-policy-to-a-policy-set"></a>

After you create your policy, you must add it to a Policy Set before applying it to your Secrets.

In **Policies**, click **Policy Sets**, then click **New Policy Set**.

The **Policy Set** settings appear.

In **Name**, enter the name of the Policy Set.

In **Description**, enter a description of the Policy Set.

In **Entity type**, select **Secret**.

In **On what event should the Policy Set be evaluated**, select **On save.**

Click **Continue**.

Existing Secrets are not automatically updated with policies. Policies can be applied to Secrets only on a save when they are created or updated.In **Policy evaluation criteria**, click **Add Policy.**

**Select Policy** settings appear. Select the policy you want to use from the list.

Select the severity and action you want to apply when the policy is not adhered to. You can select one of the following:

* **Warn & continue** - You will receive a warning if the policy is not met when the Secret is evaluated, but the Secret will be saved and you may proceed.
* **Error and exit** - You will get an error and be exited without saving the Secret if the policy is not met when the Secret is examined.

Click **Apply**, and then click **Finish**.

To enforce your Policy Set, toggle on the **Enforced** button.

### Step 3: Apply the Policy to a Secret <a href="#step-3-apply-the-policy-to-a-secret" id="step-3-apply-the-policy-to-a-secret"></a>

After you have created your Policy Set, and added your policies to it, apply the policy to a Secret.

Let us look at an example.

You can add a Secret from any module in your Project in Project setup, or in your Organization, or Account Resources.

In Account Resources, click **Secrets**.

Click **New Secret**, and then click **Text**.

Enter all the required fields and click **Save and Continue**.

Based on your selection in the Policy Evaluation criteria, you will either receive a warning or an error.

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

Use the following to resolve common Policy as Code issues for Secrets.

* **A saved Secret skips policy evaluation:** Confirm the Policy Set's event is set to **On save** and that **Enforced** is toggled on, since only enforced Policy Sets evaluated on that event run against the Secret.
* **An existing Secret does not reflect a newly added policy:** Existing Secrets are not retroactively evaluated. Save or update the Secret again to trigger evaluation against the current Policy Set.
* **A policy violation does not block the save:** Confirm the Policy Set's evaluation criteria is set to **Error and exit** rather than **Warn & continue**, since the warn action always allows the save to proceed.

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

* [​Harness Policy As Code Overview​](https://developer.harness.io/feature-flags-classic/troubleshooting-and-resources/troubleshoot-ff-classic/harness-policy-engine.md)

{% @harness-feedback/feedback %}
