Create Policy
Create OPA governance policies for Harness Software Supply Chain Assurance (SCS) via MCP.
Instructions
Step 1: Identify Policy Requirements
Determine what the policy should enforce:
- What entity type is the policy targeting? (pipeline, service, environment, feature flag, etc.)
- What is the enforcement action (warn, deny)?
- What scope should the policy apply to?
- What action triggers the policy? (onrun, onsave, onstep, etc.)
For writing Rego policies, consult references/rego-writing-guide.md for the complete Rego writing rules, entity types, package names, and common patterns. For entity-specific schemas and examples, see the entity reference files listed in that guide.
Step 2: Create the Policy
Call MCP tool: harness_create
Parameters:
resource_type: "policy"
org_id: "<organization>"
project_id: "<project>"
body: <policy definition>
OPA policies are managed under the governance toolset — resource_type: "policy" supports full CRUD (list, get, create, update, delete).
Step 3: Verify Compliance Results
After a policy is created, check compliance status on artifacts or repositories:
Call MCP tool: harness_list
Parameters:
resource_type: "scs_compliance_result"
org_id: "<organization>"
project_id: "<project>"
Common Policy Patterns
Require SBOM Generation
Enforce that all artifacts have an SBOM before deployment:
package harness.artifact
deny[msg] {
not input.artifact.sbom
msg := "Artifact must have an SBOM before deployment"
}
Block Critical Vulnerabilities
Deny deployment of artifacts with critical CVEs:
package harness.artifact
deny[msg] {
vuln := input.artifact.vulnerabilities[_]
vuln.severity == "CRITICAL"
msg := sprintf("Critical vulnerability %s found in artifact", [vuln.cve_id])
}
Enforce Approved Base Images
Restrict container images to approved base images:
package harness.artifact
approved_bases := {"alpine", "distroless", "ubuntu"}
deny[msg] {
not approved_bases[input.artifact.base_image]
msg := sprintf("Base image '%s' is not in the approved list", [input.artifact.base_image])
}
Require Signed Artifacts
Enforce artifact signing before deployment:
package harness.artifact
deny[msg] {
not input.artifact.signed
msg := "Artifact must be signed before deployment"
}
Related Resource Types
| Resource Type |
Operations |
Description |
policy |
list, get, create, update, delete |
OPA governance policies (governance toolset) |
policy_set |
list, get, create, update, delete |
Group policies with enforcement actions |
policy_evaluation |
list, get |
View policy evaluation results |
scs_compliance_result |
list |
Check SCS policy compliance status |
artifact_security |
list, get |
View artifact security posture |
code_repo_security |
list, get |
View repository security posture |
scs_chain_of_custody |
get |
Verify artifact provenance |
Rego Policy Reference Files
For writing Rego policies for any Harness entity, consult these reference files:
- Rego writing guide and rules — Entity types, package names, Rego patterns, quality checklist
- Pipeline policies and schema — Pipeline input schema, step/stage nesting, walk patterns
- Feature Flag / FME policies — Feature flag, definition, FME environment, segment schemas
- Service, Environment, Infrastructure — Service, env, infra schemas and examples
- Security Tests policies — Security test output schema, severity/coverage checks
- SBOM policies — SBOM deny/allow list patterns with semver comparison
- Terraform and Workspace — Terraform plan, cost, state, workspace schemas
- GitOps Application — GitOps app schema, namespace/label/revision policies
- Code Repository — Code repo naming, visibility, branch policies
- Variable policies — Variable schema, role-based restrictions
- Override policies — Override schema, config file and variable protection
- Connector policies — Connector schema, type/auth/naming restrictions
- Secret policies — Secret schema, naming/type/provider restrictions
- Template policies — Template schema, approval/versioning/environment checks
- Database DevOps policies — SQL statement governance, DDL restrictions, transaction limits
- Upstream Firewall — Firewall package schema, CVE/license policies
- Advanced patterns — Exception handling, walk, scoped references, exemptions
Examples
- "Create a policy to block critical CVEs" -- Create OPA deny rule for critical severity
- "Enforce SBOM generation for all artifacts" -- Create policy requiring SBOM presence
- "Only allow approved base images" -- Create policy with allowed base image list
- "Require artifact signing before production" -- Create policy checking signature status
- "Require approval before production deployments" -- Pipeline policy with Approval stage check
- "Enforce disallowPipelineExecutor on approval steps" -- Pipeline walk-based step check
- "Block Terraform plans exceeding $100/month" -- Terraform plan cost policy
- "Require feature flag descriptions" -- FME feature flag onsave policy
- "Prevent GitOps deployments to kube-system" -- GitOps namespace restriction
- "Check which artifacts violate our policies" -- List scs_compliance_result
Performance Notes
- Validate Rego syntax before submitting. Common issues: missing package declaration, deny rules without msg return.
- Ensure the policy package name follows
package harness.<domain> convention.
- Test policy logic mentally against expected inputs before creating.
Troubleshooting
Policy Not Enforcing
- Verify the policy was created successfully (list via
resource_type: "policy")
- Policies must be attached to a
policy_set with an enforcement action (warn/deny) before they fire
- Check that the policy scope matches the target artifacts/repositories
- Use
scs_compliance_result or policy_evaluation to verify the policy is being evaluated
Policy Syntax Errors
- OPA policies use Rego language -- validate syntax before submitting
- Package names should follow
package harness.<domain> convention
- Deny rules must return a
msg string explaining the violation
Limitations
- Policies apply within the project scope where they are created
- Attach policies to a
policy_set to activate enforcement
1---2name: create-policy3description: Create OPA governance policies for Harness via MCP. Define policies that enforce compliance rules on pipelines, services, environments, feature flags, artifacts, code repositories, templates, SBOM, security tests, Terraform, GitOps, connectors, secrets, and more. Use when asked to create, write, fix, or explain an OPA policy, Rego rule, deny rule, governance policy, compliance rule, or policy-as-code for any Harness entity. Trigger phrases: create policy, OPA policy, governance policy, compliance rule, rego policy, deny rule, enforce policy, security policy, supply chain governance.4license: Apache-2.05---67# Create Policy89Create OPA governance policies for Harness Software Supply Chain Assurance (SCS) via MCP.1011## Instructions1213### Step 1: Identify Policy Requirements1415Determine what the policy should enforce:16- What entity type is the policy targeting? (pipeline, service, environment, feature flag, etc.)17- What is the enforcement action (warn, deny)?18- What scope should the policy apply to?19- What action triggers the policy? (onrun, onsave, onstep, etc.)2021**For writing Rego policies**, consult `references/rego-writing-guide.md` for the complete Rego writing rules, entity types, package names, and common patterns. For entity-specific schemas and examples, see the entity reference files listed in that guide.2223### Step 2: Create the Policy2425```26Call MCP tool: harness_create27Parameters:28 resource_type: "policy"29 org_id: "<organization>"30 project_id: "<project>"31 body: <policy definition>32```3334OPA policies are managed under the `governance` toolset — `resource_type: "policy"` supports full CRUD (list, get, create, update, delete).3536### Step 3: Verify Compliance Results3738After a policy is created, check compliance status on artifacts or repositories:3940```41Call MCP tool: harness_list42Parameters:43 resource_type: "scs_compliance_result"44 org_id: "<organization>"45 project_id: "<project>"46```4748## Common Policy Patterns4950### Require SBOM Generation5152Enforce that all artifacts have an SBOM before deployment:5354```rego55package harness.artifact5657deny[msg] {58 not input.artifact.sbom59 msg := "Artifact must have an SBOM before deployment"60}61```6263### Block Critical Vulnerabilities6465Deny deployment of artifacts with critical CVEs:6667```rego68package harness.artifact6970deny[msg] {71 vuln := input.artifact.vulnerabilities[_]72 vuln.severity == "CRITICAL"73 msg := sprintf("Critical vulnerability %s found in artifact", [vuln.cve_id])74}75```7677### Enforce Approved Base Images7879Restrict container images to approved base images:8081```rego82package harness.artifact8384approved_bases := {"alpine", "distroless", "ubuntu"}8586deny[msg] {87 not approved_bases[input.artifact.base_image]88 msg := sprintf("Base image '%s' is not in the approved list", [input.artifact.base_image])89}90```9192### Require Signed Artifacts9394Enforce artifact signing before deployment:9596```rego97package harness.artifact9899deny[msg] {100 not input.artifact.signed101 msg := "Artifact must be signed before deployment"102}103```104105## Related Resource Types106107| Resource Type | Operations | Description |108|--------------|-----------|-------------|109| `policy` | list, get, create, update, delete | OPA governance policies (governance toolset) |110| `policy_set` | list, get, create, update, delete | Group policies with enforcement actions |111| `policy_evaluation` | list, get | View policy evaluation results |112| `scs_compliance_result` | list | Check SCS policy compliance status |113| `artifact_security` | list, get | View artifact security posture |114| `code_repo_security` | list, get | View repository security posture |115| `scs_chain_of_custody` | get | Verify artifact provenance |116117## Rego Policy Reference Files118119For writing Rego policies for any Harness entity, consult these reference files:120121- [Rego writing guide and rules](references/rego-writing-guide.md) — Entity types, package names, Rego patterns, quality checklist122- [Pipeline policies and schema](references/entity-pipeline.md) — Pipeline input schema, step/stage nesting, walk patterns123- [Feature Flag / FME policies](references/entity-feature-flag.md) — Feature flag, definition, FME environment, segment schemas124- [Service, Environment, Infrastructure](references/entity-service-env-infra.md) — Service, env, infra schemas and examples125- [Security Tests policies](references/entity-security-tests.md) — Security test output schema, severity/coverage checks126- [SBOM policies](references/entity-sbom.md) — SBOM deny/allow list patterns with semver comparison127- [Terraform and Workspace](references/entity-terraform.md) — Terraform plan, cost, state, workspace schemas128- [GitOps Application](references/entity-gitops.md) — GitOps app schema, namespace/label/revision policies129- [Code Repository](references/entity-code-repository.md) — Code repo naming, visibility, branch policies130- [Variable policies](references/entity-variable.md) — Variable schema, role-based restrictions131- [Override policies](references/entity-override.md) — Override schema, config file and variable protection132- [Connector policies](references/entity-connector.md) — Connector schema, type/auth/naming restrictions133- [Secret policies](references/entity-secret.md) — Secret schema, naming/type/provider restrictions134- [Template policies](references/entity-template.md) — Template schema, approval/versioning/environment checks135- [Database DevOps policies](references/entity-database.md) — SQL statement governance, DDL restrictions, transaction limits136- [Upstream Firewall](references/entity-upstream-firewall.md) — Firewall package schema, CVE/license policies137- [Advanced patterns](references/advanced-patterns.md) — Exception handling, walk, scoped references, exemptions138139## Examples140141- "Create a policy to block critical CVEs" -- Create OPA deny rule for critical severity142- "Enforce SBOM generation for all artifacts" -- Create policy requiring SBOM presence143- "Only allow approved base images" -- Create policy with allowed base image list144- "Require artifact signing before production" -- Create policy checking signature status145- "Require approval before production deployments" -- Pipeline policy with Approval stage check146- "Enforce disallowPipelineExecutor on approval steps" -- Pipeline walk-based step check147- "Block Terraform plans exceeding $100/month" -- Terraform plan cost policy148- "Require feature flag descriptions" -- FME feature flag onsave policy149- "Prevent GitOps deployments to kube-system" -- GitOps namespace restriction150- "Check which artifacts violate our policies" -- List scs_compliance_result151152## Performance Notes153154- Validate Rego syntax before submitting. Common issues: missing package declaration, deny rules without msg return.155- Ensure the policy package name follows `package harness.<domain>` convention.156- Test policy logic mentally against expected inputs before creating.157158## Troubleshooting159160### Policy Not Enforcing161- Verify the policy was created successfully (list via `resource_type: "policy"`)162- Policies must be attached to a `policy_set` with an enforcement action (warn/deny) before they fire163- Check that the policy scope matches the target artifacts/repositories164- Use `scs_compliance_result` or `policy_evaluation` to verify the policy is being evaluated165166### Policy Syntax Errors167- OPA policies use Rego language -- validate syntax before submitting168- Package names should follow `package harness.<domain>` convention169- Deny rules must return a `msg` string explaining the violation170171### Limitations172- Policies apply within the project scope where they are created173- Attach policies to a `policy_set` to activate enforcement