| name | policy-compliance |
| description | Investigate and resolve policy check failures in HCP Terraform. Use when runs are blocked by failed Sentinel or OPA policy checks, understanding why policies failed, deciding whether to fix code or override policies, or finding policy failures across the organization. |
Policy Compliance Skill
Overview
This skill helps investigate and resolve policy check failures in HCP Terraform. Policies enforce governance, security, and compliance standards using Sentinel or OPA. When policies fail, you need to understand what was checked, why it failed, and how to remediate.
Prerequisites
- Authenticated with
hcptf CLI
- Read access to workspaces and policy sets
- Override permissions if you need to bypass failed policies
- Write access to workspace code if fixing violations
Core Concepts
Policy Types:
- Sentinel: HashiCorp's policy-as-code framework
- OPA (Open Policy Agent): CNCF policy framework
Enforcement Levels:
- Advisory: Logs a warning, run continues (informational)
- Soft-Mandatory: Can be overridden by authorized users
- Hard-Mandatory: Cannot be overridden, run fails
Policy Check Statuses:
- Passed: All policies passed
- Failed: One or more mandatory policies failed
- Overridden: Failed policy was manually overridden
- Unreachable: Policy could not be evaluated (configuration error)
Workflow
1. Detect Policy Failures
From run status:
hcptf <org> <workspace> runs <run-id> show
Across organization:
hcptf queryrun list -organization=<org> -status=policy_checked
hcptf explorer query -org=<org> -type=workspaces \
-fields=workspace-name,current-run-status
2. View Policy Check Details
hcptf policycheck list -run-id=<run-id>
hcptf policycheck read -id=<policy-check-id>
URL-style navigation:
hcptf <org> <workspace> runs <run-id> policychecks
3. Understand What the Policy Does
For public policies:
hcptf publicregistry policy list | grep CIS
hcptf publicregistry policy -name=hashicorp/CIS-Policy-Set-for-AWS-Terraform
For private/custom policies:
hcptf policyset read -organization=<org> -id=<policy-set-id>
4. Identify Violating Resources
Policy check results show which Terraform resources violated policies:
hcptf policycheck read -id=<policy-check-id>
Cross-reference with plan:
hcptf <org> <workspace> runs <run-id> plan
5. Decide on Remediation Strategy
Decision Matrix:
| Situation | Action | Command |
|---|
| Policy is correct, code is wrong | Fix code | Clone repo, edit .tf files, commit |
| Policy is too strict for this case | Override policy | hcptf policycheck override |
| Policy needs adjustment | Update policy set | Edit policy in VCS, update parameters |
| Need temporary exception | Override with justification | hcptf policycheck override (requires permission) |
| Policy is informational | Acknowledge and continue | Review advisory warnings, no action needed |
6. Remediation Options
Option A: Fix the Code
Most common approach - update Terraform code to comply with policy.
RUN_ID=$(hcptf <org> <workspace> -output=json | jq -r '.CurrentRunID')
hcptf <org> <workspace> runs $RUN_ID configversion
git clone https://github.com/my-org/infrastructure
cd infrastructure
git add security_groups.tf
git commit -m "Restrict SSH access to private network for policy compliance"
git push origin main
hcptf <org> <workspace> runs list | head -5
hcptf <org> <workspace> runs <new-run-id> show
Option B: Override the Policy
Use when policy is too strict for a specific case. Requires override permissions.
hcptf policycheck override -id=<policy-check-id>
hcptf <org> <workspace> runs <run-id> apply
Option C: Adjust Policy Enforcement
Update the policy set to change enforcement level or parameters.
hcptf policyset read -organization=<org> -id=<policy-set-id>
git clone <policy-vcs-repo>
cd policy-repo
git add sentinel.hcl
git commit -m "Allow overrides for tagging policy in dev workspaces"
git push origin main
hcptf <org> <workspace> runs create -message="Re-run with updated policy"
Common Policy Scenarios
Scenario 1: CIS Benchmark Violation
hcptf <org> production-app runs run-abc123 policychecks
hcptf publicregistry policy -name=hashicorp/CIS-Policy-Set-for-AWS-Terraform
hcptf policycheck read -id=polchk-xyz789
resource "aws_s3_bucket_logging" "cloudtrail_logs" {
bucket = aws_s3_bucket.cloudtrail_logs.id
target_bucket = aws_s3_bucket.log_bucket.id
target_prefix = "cloudtrail/"
}
Scenario 2: Tagging Policy Failure
hcptf <org> dev-environment runs run-def456 policychecks
hcptf policycheck read -id=polchk-abc123
resource "aws_instance" "web" {
tags = {
Name = "web-server"
Environment = "development"
Owner = "platform-team"
CostCenter = "engineering"
}
}
provider "aws" {
default_tags {
tags = {
Environment = "development"
ManagedBy = "Terraform"
CostCenter = "engineering"
}
}
}
Scenario 3: Security Group Overly Permissive
hcptf <org> api-service runs run-ghi789 show
hcptf <org> api-service runs run-ghi789 policychecks
hcptf policycheck read -id=polchk-def456
resource "aws_security_group" "api" {
name = "api-sg"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["10.0.0.0/8"]
description = "SSH from VPN"
}
ingress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
description = "HTTPS from internet"
}
}
Scenario 4: Find All Policy Failures Across Organization
hcptf explorer query -org=<org> -type=workspaces \
-fields=workspace-name,current-run-status,current-run-id \
-filter="current-run-status:policy_checked"
for ws in workspace1 workspace2 workspace3; do
echo "=== $ws ==="
RUN_ID=$(hcptf <org> $ws -output=json | jq -r '.CurrentRunID')
hcptf policycheck list -run-id=$RUN_ID
done
hcptf explorer query -org=<org> -type=workspaces \
-fields=workspace-name,current-run-status,policy-set-names \
-output=json > compliance-report.json
Scenario 5: Test Policy Changes Before Enforcement
policy "new-security-check" {
enforcement_level = "advisory"
}
hcptf policyset update -organization=<org> -id=<policy-set-id> \
-workspace=test-workspace
hcptf test-workspace runs create -message="Test new security policy"
hcptf test-workspace runs <run-id> policychecks
policy "new-security-check" {
enforcement_level = "soft-mandatory"
}
Policy Troubleshooting
Policy check shows "unreachable":
- Policy code has syntax errors
- Policy cannot access required data (tfplan, tfconfig)
- Check policy set VCS repository for errors
Override button not available:
- Policy is hard-mandatory (cannot override)
- User lacks override permissions
- Check team access settings
Policy passes locally but fails in HCP Terraform:
- Sentinel CLI vs cloud environment differences
- Check policy parameters and configuration
- Verify policy set is correctly applied
Policy fails on every run:
- Policy configuration may be incorrect
- Check if policy matches workspace type (EC2 policy on GCP workspace)
- Review policy parameters
Best Practices
- Understand Before Overriding: Always investigate why a policy failed before overriding
- Document Overrides: Include justification when overriding policies
- Advisory First: Test new policies in advisory mode before enforcing
- Fix Code, Not Policy: Prefer fixing violations over relaxing policies
- Regular Reviews: Periodically review policy overrides and adjust policies if needed
- Workspace Exceptions: Use different policy sets for dev vs production
- Incremental Rollout: Apply new policies to test workspaces first
- Monitor Compliance: Track policy failures across organization
- Educate Teams: Share policy documentation and remediation guides
Related Commands
hcptf policycheck list - List policy checks for a run
hcptf policycheck read - View detailed policy check results
hcptf policycheck override - Override a failed policy (if allowed)
hcptf policyset list - List policy sets in organization
hcptf policyset read - View policy set configuration
hcptf publicregistry policy - Get public policy details and documentation
hcptf publicregistry policy list - Browse available public policies
hcptf explorer query - Find workspaces with policy failures
hcptf queryrun list - Find runs with specific statuses
hcptf <org> <workspace> runs <run-id> policychecks - View policy checks (URL-style)
Policy Compliance Metrics
Track compliance across your organization:
hcptf explorer query -org=<org> -type=workspaces \
-filter="current-run-status:policy_checked" \
-fields=workspace-name | wc -l
hcptf policyset list -organization=<org>
hcptf queryrun list -organization=<org> -status=policy_override
hcptf explorer query -org=<org> -type=workspaces \
-fields=workspace-name,policy-set-names