Using Harness Policy as Code for Variables
Add a Policy step to your Variables.
Harness provides governance and compliance controls through integration with Open Policy Agent (OPA), Policy Management, and Rego policies.
You can define and enforce custom policies on Variables across account, organization, and project scopes. These policies will be evaluated during the On Save event (when a Variable is created or updated). This ensures that Variable configurations meet your organization's security, naming, or value standards before they are persisted.
Prerequisites
Complete these steps before you create a policy for variables.
Policies use OPA authoring language Rego. Go to OPA Policy Authoring to learn Rego syntax.
Step 1: Add a policy
Create the Rego policy that Harness evaluates against your variables.
In Harness, go to Account Settings → Policies → New Policy.

opa-navigation The New Policy settings will appear. Enter a Name for your policy → click Apply.

opa-variable Add your Rego policy.
3.1 Use Your Own Rego Policy: You can add a custom Rego policy based on your requirements. For example, let's add a policy to restrict the creation or updation of Variables starting with
helloworldby users with the Account viewer role:``` package variable deny[msg] { startswith(input.variable.name, "helloworld") some i input.metadata.roleAssignmentMetadata[i].roleIdentifier == "_account_viewer" msg := "Variable with name starting with 'helloworld' is not allowed when role '_account_viewer' is present" } ```3.2. Use an existing Rego Policy from the Harness Policy Library: To select an existing policy, go to the Library, search for variables, select the Variable - Deny Edit template, and click Use This Sample at the bottom.

sample-opa-policy Test your policy against a sample input, and then click Save.
Step 2: Add the policy to a policy set
After creating your policy, add it to a Policy Set before applying it to your Variable.
In Policies, click Policy Sets.

opa-select-policy-sets Click New Policy Set. Enter a Name and Description(optional) for the Policy Set. In Entity type, select Variable, and for On what event should the Policy Set be evaluated, select On save.
Click Continue to proceed.

opa-variable-setting
Step 3: Select policy evaluation criteria
Choose the policy and the severity for policy violations.
Click Add Policy in Policy evaluation criteria. Select the policy from the list.
Choose the severity for policy violations and click Apply. Currently, only one option is supported: Error and exit.
Error and exit– On saving a variable, if the policy is violated, an error will be shown and you won't be allowed to proceed.
select-opa-policy Click Finish.
Step 4: Policy enforcement
Toggle on the Enforced button, to enforce the policy.

Step 5: Evaluate a policy to a variable
Go to Account Settings → Account-level resources → Variables. Click New Variable.

new-variable Try using a variable name denied by your policy, e.g., "helloworld-123".
Once you try to save the variable the Policy will be evaluated, and you will receive an error.

Explore more on Harness policy as code
Go to the following resource for the broader Harness Policy As Code offering:
Next steps
Continue your governance setup with these related resources.
Go to Harness Governance Overview to understand how policy sets fit into the wider governance model.
Go to Harness Governance Quickstart to review the overall governance setup steps.
Last updated
Was this helpful?