Skip to main content

🛡️ Continuous Enforcement of Security Policies and Rules in IaC

🌟 Objective​

Ensure that all Infrastructure as Code (IaC) projects meet minimum security requirements in an automatic, consistent and verifiable way, through policy enforcement mechanisms integrated into the development and operations lifecycle.

This file defines how policies are applied technically, not which policies they are - that definition takes place in the requirements and in organisational policies.

Enforcement is a structural defence mechanism: when it fails, the organisation depends solely on human discipline.


🔖 What must be done​

  1. Define formal organisational policies applicable to IaC (security, identity, network, data);
  2. Translate those policies into executable and testable rules (policy-as-code);
  3. Integrate enforcement in a blocking way into the CI/CD pipelines (pre-merge, pre-apply);
  4. Centralise the rules in a controlled repository, with versioning and formal approval;
  5. Allow temporary exceptions that are justified, traceable and have automatic expiry;
  6. Continuously measure and review the effectiveness of enforcement (blocks, exceptions, bypass).

⚖️ How it must be done​

ComponentTechnical prescription
Policy engineOPA/Rego, Sentinel (HashiCorp), Conftest, InSpec
ModelDeclarative, versioned and testable Policy-as-Code
TriggerMandatory execution on PR/MR, pre-merge and pre-apply
SeverityClassification by impact (warn / block), aligned with L1–L3
FeedbackClear, actionable messages per violated rule
LoggingCentral record of results per project, commit, author and environment
ExceptionsControlled per rule, with justification, TTL and AppSec/GRC approval

🗓️ When to apply​

MomentExpected action
Pull Request with IaCMandatory execution of policy checks
Pre-apply in real environmentsBlocking enforcement before any apply
Policy updateReview and controlled rollout of the rules
Bypass detectionImmediate alert + root cause analysis

💼 Practical examples​

✏️ Example Rego rule (OPA)​

deny[msg] {
input.resource_type == "aws_s3_bucket"
not input.tags.Environment
msg := "Missing mandatory 'Environment' tag on S3 bucket"
}

🌍 Integration into the pipeline (GitHub Actions)​

- name: Policy enforcement (OPA)
run: conftest test ./iac/envs/prod

Expected result:

  • Violation → immediate fail of the job;
  • Clear message associated with the rule;
  • Evidence persisted for auditing.

📈 Direct benefits​

  • Reduction of risk from human error or omission;
  • Consistent application of organisational policies;
  • Objective and reproducible auditing;
  • Less dependence on manual review;
  • Solid technical foundation for zero trust in IaC.

🔗 Cross-references​

DocumentRelation to this file
addon/02-validacoes-e-checks.mdTechnical validations and quality before enforcement
addon/04-principios-sbd-iac.mdFail securely, least privilege, controlled visibility
addon/11-uso-ferramentas-automatizadas-iac.mdSpecific rules for automation and external tools
canon/20-checklist-revisao.mdBinary verification of policy application