Using Rego for Database DevOps Steps
Learn how to use Rego policies with Harness DB DevOps steps to enforce guardrails, governance, and security across database deployment workflows.
This guide explains how to use Harness Policy Agent to enforce policies on DataBase Devops steps. Rego is a declarative policy language used by Open Policy Agent (OPA) for policy-based control.
Before you begin
Rego knowledge: Basic understanding of the Rego policy language and OPA is recommended.
Write a Rego policy for Database DevOps steps
A Rego policy can validate that changesets conform to specific rules, such as enforcing naming conventions or restricting certain SQL operations.
Example policy: restrict DROP TABLE
Go to DB Governance section and create a new policy
package db_sql
policies := [
{
"error_message": "Dropping of table is not allowed.",
"rules": [
{
"types": [
"jdbc:sqlserver","jdbc:mysql","jdbc:postgresql","jdbc:oracle:thin"
],
"regex": [
"drop"
]
}
]
}
]
deny[msg] {
some i,j,k,l
policy := policies[i];
type := input.dbInstance.type;
rule := policy.rules[j];
type = rule.types[_];
regex.match(lower(concat("",[".*",rule.regex[k],".*"])),lower(input.sqlStatements[l]));
msg := concat("",["Policy violation:\n The following sql statement:\n",input.sqlStatements[l],"\n\n Matches the following regex: \n",rule.regex[k]])
}
Sample payload
You can test the policy on sample payloads
Create a custom policy set and attach the policy
)
Attach the policy set in Database DevOps step configuration

Validate Liquibase steps with OPA
Run the OPA policy check against the changeset during pipeline run:
If a violation occurs, OPA will output a message indicating the problem (e.g., "Dropping tables is not allowed: users") and result in error / warning as per configuration.

OPA policy examples
Table name limit
The function checks if any of the SQL statements in the input create a table with a name longer than 10 characters. If a match is found, it means that the table name violates the rule and the function returns a message indicating the violation.
Schema name limit
The existing code already has a schema name length check in the "Prevent Data Drop" section, but it could be formalized as a separate policy:
Prevent direct system table access
This policy checks if any SQL statement attempts to access system tables (e.g., those starting with "sys." in SQL Server). If such access is detected, it returns a violation message.
Prevent large transactions
DB policy populator
The types represent the different types of databases (e.g., sybase, oracle, mssql). The regular expressions represent the SQL statements that are not allowed in each type of database. if a match is found, it means that the SQL statement violates a policy and the function returns a message indicating the violation.
Next steps
Go to Approval gates to require human review before applying database changes.
Go to Audit trails to track all Database DevOps events for compliance.
Last updated
Was this helpful?