OPA Policy Support for GitOps Applications
Use Open Policy Agent (OPA) policies to enforce governance for GitOps applications on save and sync
Harness supports Open Policy Agent (OPA) policies for GitOps Applications so you can enforce governance rules and compliance requirements during GitOps operations such as syncing applications.
With OPA policy support, you can:
Enforce deployment freezes: Prevent syncing GitOps Applications during maintenance windows or specific time periods
Control production deployments: Restrict manual syncs for production applications based on naming conventions or labels
Validate application configurations: Ensure GitOps Applications meet organizational standards before syncing
Implement time-based restrictions: Block or allow syncs based on specific time windows
What you will learn from this topic
How OPA policies work for GitOps applications and when they are evaluated
How to create a policy for GitOps applications
How to use sample policies for common governance scenarios
How to test policies using the Testing Terminal
Before you begin
Complete the following prerequisites before you create OPA policies for GitOps Applications:
GitOps Applications configured: At least one GitOps Application exists in your project. Go to Manage GitOps Applications to create and manage applications.
Policy permissions: Create/Edit (and View) permissions on Policies in Harness. Go to RBAC in Harness to configure roles.
Rego familiarity: Basic knowledge of the Rego policy language used by OPA.
Go to OPA Policy language and the Rego Cheatsheet to learn about writing Rego policies.
Go to Harness Policy as Code overview for a platform-wide overview of Harness Policy as Code and OPA.
How OPA policies work for GitOps applications
When you create a policy for GitOps Applications, the policy is evaluated against the GitOps Application entity during specific events:
On Save: Policies are evaluated when a GitOps Application is saved or updated
On Sync: Policies are evaluated when a sync operation is initiated for a GitOps Application
The policy receives the GitOps Application configuration as input, including:
Application metadata (name, namespace, labels)
Application source configuration
Application destination configuration
Sync operation details
Create a policy for GitOps applications
Navigate to policies
Perform the following steps to open Policies:
In Harness, go to Settings.
Under Security and Governance, click Policies.

Create a new policy
Perform the following steps to create a policy:
Click + New Policy.
Enter a name for your policy (for example, "GitOps Application - Deployment Freeze").
Select Inline or Remote for policy storage.
Click Apply.

Define the policy
In the policy editor, write your Rego policy. The policy should use the gitopsApplication package and access the application data through the input.gitopsApplication object. You can also search GitOps in the Sample Policies and pick the template best suited for the use case.

Add policy to a policy set
Perform the following steps to attach the policy to a Policy Set:
Go to Policy Sets.
Create a new Policy Set or edit an existing one.
Set the Entity Type to GitOps Application.
Select the event (On Save or On Sync).
Add your policy to the Policy Set.
Set the policy severity (Error and Exit or Warn and Continue).
Save the Policy Set.
Sample policies
Example: Restrict sync during off-hours
This policy prevents GitOps Application syncs during off-hours (for example, between 6 PM and 6 AM UTC):
Example: Require service and environment labels
This policy ensures that GitOps Applications have required Harness labels before syncing:
Example: Block protected namespaces
This policy prevents GitOps Applications from deploying to protected system namespaces:
Test policies
You can test your policies before applying them. Perform the following steps:
In the policy editor, use the Testing Terminal tab.
Select a sample GitOps Application payload or use a previous evaluation.
Run the test to confirm the policy evaluates correctly.
Review the results and adjust the policy as needed.
Go to Harness Policy as Code quickstart to test policies in more detail.
Policy input schema
The input payload for GitOps Application policies follows this structure:
Policy severity
When adding a policy to a Policy Set, you can set the severity:
Error and Exit: If the policy evaluation fails, the sync operation is blocked and an error message is displayed
Warn and Continue: If the policy evaluation fails, a warning is displayed but the sync operation continues
View policy evaluation results
You can view policy evaluation results in two ways.
View evaluations in policies
To view all policy evaluations across your GitOps Applications:
Go to Settings -> Policies -> Evaluations.
Filter evaluations by:
Type: Select "GitOps Application"
Action: Select "On Save" or "On Sync"
Status: Filter by SUCCESS, WARNING, or FAILED
Time range: Select the date range (for example, "Last 7 days")
The evaluations table shows:
ENTITY: The GitOps Application name and path
ENTITY TYPE: "GitOps Application"
EXECUTION: The Policy Set name that was evaluated
EVALUATED ON: When the evaluation occurred
ACTION: The event that triggered the evaluation ("On Sync" or "On Save")
STATUS: The evaluation result (SUCCESS, WARNING, or FAILED)

This view lets you audit policy evaluations over time and identify patterns in policy violations across all your GitOps Applications.
View evaluation results during sync
When you sync a GitOps Application, the Policy Set Evaluations modal appears automatically if policies are evaluated. This modal shows:
Evaluation timestamp: When the policies were evaluated
Policy Set information: The name and scope of the Policy Set
Overall status: The combined status of all policies (SUCCESS, WARNING, or FAILED)
Individual policy results: Each policy's status with specific messages
The modal displays a warning banner if any policies generate warnings, and shows detailed results for each policy in the Policy Set. Policies with SUCCESS status are marked with a green checkmark, while policies with WARNING or FAILED status show the specific error message from the policy evaluation.
This lets you see immediately which policies passed or failed during the sync operation, so you can understand why a sync might have been blocked or why warnings were generated.

Best practices
Apply these practices to get value from OPA policies for GitOps Applications:
Use descriptive policy names: Name your policies clearly to indicate their purpose (for example, "GitOps Application - Production Deployment Freeze")
Test policies thoroughly: Use the Testing Terminal to verify policies work as expected before applying them
Document policy logic: Add comments in your Rego code to explain complex logic
Start with warnings: Set policies to "Warn and Continue" first to monitor behavior before enforcing with "Error and Exit"
Keep time windows current: If you use time-based policies, keep maintenance windows and time restrictions accurate
Review evaluation results: Review policy evaluation results regularly to confirm policies behave as intended and to find gaps
Troubleshooting
Next steps
Harness Policy as Code overview: Review the platform-wide governance framework.
Policy samples: Explore additional policy use cases.
Manage GitOps Applications: Manage your GitOps Applications.
Last updated
Was this helpful?