Azure Policy Custom Template with Terraform

Branden

I am trying to build a custom seccomp template for Azure Policy using Terraform and keep running into errors when adding multiple paramaters similar to how the templates are built. If I build these into Azure manually, I have no problems.

My Terraform is below, the error I keep getting in this example is

╷
│ Error: creating/updating Policy Definition "k8s_seccomp_governance": policy.DefinitionsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="InvalidPolicyRuleEffectDetails" Message="The policy definition 'k8s_seccomp_governance' rule is invalid. The policy effect 'details' property could not be parsed."
│ 
│   with azurerm_policy_definition.k8s_seccomp_governance,
│   on policy_definitions.tf line 1, in resource "azurerm_policy_definition" "k8s_seccomp_governance":
│    1: resource "azurerm_policy_definition" "k8s_seccomp_governance" {
│ 
╵

Code:

resource "azurerm_policy_definition" "k8s_seccomp_governance" {
  name         = "k8s_seccomp_governance"
  description  = "Kubernetes cluster containers should only use allowed seccomp profiles"
  policy_type  = "Custom"
  mode         = "All"
  display_name = "AMPS K8s Seccomp Governance"

  metadata = <<METADATA
    {
    "category": "Kubernetes",
    "version": "1.0.0"
    }

METADATA

  policy_rule = <<POLICY_RULE
    {
      "if": {
        "field": "type",
        "in": [
          "AKS Engine",
          "Microsoft.Kubernetes/connectedClusters",
          "Microsoft.ContainerService/managedClusters"
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "constraintTemplate": "https://store.policy.core.windows.net/kubernetes/allowed-seccomp-profiles/v2/template.yaml",
          "constraint": "https://store.policy.core.windows.net/kubernetes/allowed-seccomp-profiles/v2/constraint.yaml",
          "excludedNamespaces": "[parameters('excludedNamespaces')]"
        }
      }
    }

POLICY_RULE

  parameters = <<PARAMETERS
  {
    "effect": {
      "type": "String",
      "metadata": {
        "displayName": "Effect",
        "description": "'audit' allows a non-compliant resource to be created or updated, but flags it as non-compliant. 'deny' blocks the non-compliant resource creation or update. 'disabled' turns off the policy."
      },
      "allowedValues": ["audit", "deny","disabled"],
      "defaultValue": "audit"
    },
    "excludedNamespaces": {
      "type": "Array",
      "metadata": {
        "displayName": "Namespace exclusions",
        "description": "List of Kubernetes namespaces to exclude from policy evaluation."
      },
      "defaultValue": ["kube-system", "gatekeeper-system", "azure-arc"]
    }
  }
PARAMETERS

}

To add,

If I don't include description, then I get this error:

╷
│ Error: creating/updating Policy Definition "k8s_seccomp_governance": policy.DefinitionsClient#CreateOrUpdate: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="UnusedPolicyParameters" Message="The policy 'k8s_seccomp_governance' has defined parameters 'excludedNamespaces' which are not used in the policy rule. Please either remove these parameters from the definition or ensure that they are used in the policy rule."
│ 
│   with azurerm_policy_definition.k8s_seccomp_governance,
│   on policy_definitions.tf line 1, in resource "azurerm_policy_definition" "k8s_seccomp_governance":
│    1: resource "azurerm_policy_definition" "k8s_seccomp_governance" {
│ 
╵

Branden

I was able to resolve this, the problem was that I was using mode: "All" and needed to change it to mode = "Microsoft.Kubernetes.Data" for these to work

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Terraform - audit tags with Azure policy

From Dev

Azure Custom Extension Policy

From Dev

Terraform: pass variable as a parameter in azure policy initiative

From Dev

how we can use inbuilt policy's in azure via terraform , i am finding code for only custom policy's , i am using below is this correct?

From Dev

Terraform : How to define the Azure Policy Initiative along with Azure Policies?

From Dev

Attaching AWS Managed Policy to a Custom Role via Terraform

From Dev

Access token not working Azure AD custom policy

From Dev

Azure Policy not denying Custom Role creation

From Dev

How to define and assign an Azure Policy on a Management Group Scope using Terraform?

From Dev

Azure Key Vault Access Policy Error - Terraform Configuration Issue

From Dev

Enforce tags and it's value Azure Policy using Terraform

From Dev

Deploying Azure policy ARM template using Azure Devops fails

From Dev

Terraform malformed policy

From Dev

Terraform bad policy

From Dev

How do I create an S3 bucket policy from a template in Terraform 0.12?

From Dev

Azure AD B2C Multi steps custom policy

From Dev

How to apply custom retry policy to Eventhub binding in Azure function

From Dev

Azure B2C multiple different customer custom policy

From Dev

Azure b2c profile edit policy custom UI

From Dev

Password Reset link Azure B2C Custom Policy

From Dev

Azure AD B2C passwordless sign in custom Policy

From Dev

Azure B2C custom policy conditional OrchestrationStep

From Dev

Azure B2C Add custom signin policy

From Dev

Azure B2C SAML Custom Policy Assert Email

From Dev

prevent email change in azure b2c custom policy

From Dev

How to create custom Azure Policy for k8s deployments?

From Dev

Azure AD B2C Custom Policy

From Dev

Terraform - Passing type Object as a parameter to Azure Template Deployment

From Dev

Recover output value of Azure ARm template with terraform output

Related Related

  1. 1

    Terraform - audit tags with Azure policy

  2. 2

    Azure Custom Extension Policy

  3. 3

    Terraform: pass variable as a parameter in azure policy initiative

  4. 4

    how we can use inbuilt policy's in azure via terraform , i am finding code for only custom policy's , i am using below is this correct?

  5. 5

    Terraform : How to define the Azure Policy Initiative along with Azure Policies?

  6. 6

    Attaching AWS Managed Policy to a Custom Role via Terraform

  7. 7

    Access token not working Azure AD custom policy

  8. 8

    Azure Policy not denying Custom Role creation

  9. 9

    How to define and assign an Azure Policy on a Management Group Scope using Terraform?

  10. 10

    Azure Key Vault Access Policy Error - Terraform Configuration Issue

  11. 11

    Enforce tags and it's value Azure Policy using Terraform

  12. 12

    Deploying Azure policy ARM template using Azure Devops fails

  13. 13

    Terraform malformed policy

  14. 14

    Terraform bad policy

  15. 15

    How do I create an S3 bucket policy from a template in Terraform 0.12?

  16. 16

    Azure AD B2C Multi steps custom policy

  17. 17

    How to apply custom retry policy to Eventhub binding in Azure function

  18. 18

    Azure B2C multiple different customer custom policy

  19. 19

    Azure b2c profile edit policy custom UI

  20. 20

    Password Reset link Azure B2C Custom Policy

  21. 21

    Azure AD B2C passwordless sign in custom Policy

  22. 22

    Azure B2C custom policy conditional OrchestrationStep

  23. 23

    Azure B2C Add custom signin policy

  24. 24

    Azure B2C SAML Custom Policy Assert Email

  25. 25

    prevent email change in azure b2c custom policy

  26. 26

    How to create custom Azure Policy for k8s deployments?

  27. 27

    Azure AD B2C Custom Policy

  28. 28

    Terraform - Passing type Object as a parameter to Azure Template Deployment

  29. 29

    Recover output value of Azure ARm template with terraform output

HotTag

Archive