Skip to content

Instantly share code, notes, and snippets.

@nitrocode
Last active November 22, 2022 05:07
Show Gist options
  • Save nitrocode/ff23fb63db26608ff981bd4052baaa9f to your computer and use it in GitHub Desktop.
Save nitrocode/ff23fb63db26608ff981bd4052baaa9f to your computer and use it in GitHub Desktop.
Use OPA on Terraform HCL code

Use OPA on Terraform HCL code

Sample terraform with a resource that we'd like to catch

# main.tf
resource "null_resource" "default" {
  provisioner "local-exec" {
    command = "sh -c 'echo hi'"
  }
}

Then convert the terraform to json using hcl2json

hcl2json main.tf > main.tf.json

Then write this rego policy to deny if the json terraform contains a denied resource

# deny_resource_types.rego
package resource_types

import future.keywords.in

denied_resource_types = [
    "null_resource",
]

deny["you shall not pass"] {
    some rtype in denied_resource_types
    count(input[_][rtype]) > 0
}

Finally, run opa test to verify the deny

opa eval \
  --data deny_resource_types.rego \
  --input main.tf.json \
  --fail-defined \
  "data.resource_types.deny"
{
  "result": [
    {
      "expressions": [
        {
          "value": [
            "you shall not pass"
          ],
          "text": "data.resource_types.deny",
          "location": {
            "row": 1,
            "col": 1
          }
        }
      ]
    }
  ]
}

This returns you shall not pass returned because more than zero banned resources appear. This results in a non-zero code since the --fail-defined flag is added to opa.

references

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment