Sentinel and OPA workflow integration
Separate policy language from enforcement
Sentinel and OPA are different policy systems. Sentinel can inspect Terraform information through imports such as tfplan/v2; OPA evaluates Rego rules over structured input. HCP Terraform can associate policy sets with selected workspaces and projects. It also documents a Terraform policy framework distinct from Sentinel and OPA. A local OPA invocation is not automatically an HCP policy set. Each integration has its own input wrapper, result contract, runtime version and enforcement settings.
Read a Sentinel rule
The following static Sentinel rule uses the Terraform plan import and checks managed resource changes for delete actions. It deliberately does not implement a complete region or storage policy. The empty collection passes this rule; a separate scope/completeness check is required.
import "tfplan/v2" as tfplan
managed = filter tfplan.resource_changes as _, rc { rc.mode is "managed"}main = rule { all managed as _, rc { not (rc.change.actions contains "delete") }}A replacement includes delete and is therefore rejected. Data source changes are outside this rule. This is a conservative deletion rule, not a backup or recovery strategy. Use mocked imports to test before and after cases and verify the actual import schema for the selected Sentinel runtime. No Sentinel execution was performed.
Evaluate a small Rego policy
This Rego v1 example works on the normalized teaching record from the preceding lesson, not directly on terraform show output or the HCP Terraform OPA input wrapper. A default false decision prevents missing fields from being accepted as proof of compliance. The complete flag is supplied by a trusted adapter; never let untrusted configuration set that flag for itself.
package bytemind.guardrailsimport rego.v1
default allow := falseallow if { input.complete == true input.public == false input.region in {"lab-north", "lab-south"} is_number(input.size) input.size >= 1 input.size <= 4 is_string(input.owner) trim_space(input.owner) != "" input.destructive == false}Use an approved OPA 1.x executable in an isolated copy of the fixture. The command below uses only a local policy and synthetic input; --fail makes an undefined or empty query result fail, and the equality query requires an explicit true value.
opa eval --fail --data policy.rego --input passing.json 'data.bytemind.guardrails.allow == true'opa test policy.rego policy_test.rego -vThe first command is expected to succeed on passing.json. Substitute rejected.json or unknown.json and it should fail. Four native test cases are provided, but no OPA binary was available to run them. The independent Python case checks verify the training decisions, not the Rego engine.
Decide what happens on failure
In HCP Terraform an advisory failure reports a finding without stopping a run. Sentinel policy checks distinguish soft mandatory failures that authorized operators can override from hard mandatory failures that cannot be overridden in that check mode. Sentinel policy evaluations have a different mandatory/override model; verify the set-level override configuration and permissions. HCP OPA mandatory failures stop progress unless overridden by an authorized user. Do not transfer these labels to a standalone OPA CLI. An external run task can evaluate run information at a configured stage; a mandatory task can stop progress on failure or timeout. A post-apply check cannot undo infrastructure already created. Confirm the selected edition, stage, scope and permissions before relying on enforcement.
Manage exceptions and regression tests
An exception is a controlled departure from a rule, not a comment saying to ignore all failures. Record the exact rule, resource scope, approved plan digest, justification, independent owner, expiry and compensating controls. Reevaluate after changes or expiry; audit overrides and restrict who can change the policy itself.
Rule: NO_DELETEScope: fictional_storage.ordersPlan: synthetic digest D17Expires: 2027-01-15T00:00:00ZApproval: independent change ownerCondition: verified restore rehearsalThis is an illustrative exception record, not an actual override or real approval. Test successful cases, prohibited actions, unknowns, absent fields, malformed inputs and evaluator outages. Treat errors as a hold for a mandatory gate. Protect plan and result artifacts; log minimum evidence and redact sensitive fields. A successful policy check does not prove runtime security or eliminate the need for deployment tests. Next: reviewed CI/CD workflow.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sentinel and OPA workflow integration — knowledge check
Review infrastructure policy decisions and enforcement.
What does the supplied Rego policy consume?
What happens when an HCP advisory policy fails?
What is a risk of checking only after apply?
What should a time-limited exception specify?
What should a mandatory gate do when its evaluator crashes?