Bylaw

Gallery › Kubernetes › Azure built-in policies

Resource logs in Azure Kubernetes Service should be enabled

Uses Condition Policy AuditIfNotExists Disabled Mode Indexed Version 1.0.0

Azure Kubernetes Service's resource logs can help recreate activity trails when investigating security incidents. Enable it to make sure the logs will exist when needed

ConvertTo-Bylaw converted this policy, and proved it compiles back to the same definition. Conditions the language can't express yet are written as raw JSON with Condition.

Azure Policy JSONAuditDiagnosticLog_Audit.json · 107 lines
{
  "properties": {
    "displayName": "Resource logs in Azure Kubernetes Service should be enabled",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Azure Kubernetes Service's resource logs can help recreate activity trails when investigating security incidents. Enable it to make sure the logs will exist when needed",
    "metadata": {
      "version": "1.0.0",
      "category": "Kubernetes"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      },
      "requiredRetentionDays": {
        "type": "String",
        "metadata": {
          "displayName": "Required retention (days)",
          "description": "The required resource logs retention (in days)"
        },
        "defaultValue": "365"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.ContainerService/managedClusters"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/diagnosticSettings",
          "existenceCondition": {
            "count": {
              "field": "Microsoft.Insights/diagnosticSettings/logs[*]",
              "where": {
                "anyOf": [
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.enabled",
                        "equals": "true"
                      },
                      {
                        "anyOf": [
                          {
                            "field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.days",
                            "equals": "0"
                          },
                          {
                            "value": "[padLeft(current('Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.days'), 3, '0')]",
                            "greaterOrEquals": "[padLeft(parameters('requiredRetentionDays'), 3, '0')]"
                          }
                        ]
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                        "equals": "true"
                      }
                    ]
                  },
                  {
                    "allOf": [
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                        "equals": "true"
                      },
                      {
                        "anyOf": [
                          {
                            "field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.enabled",
                            "notEquals": "true"
                          },
                          {
                            "field": "Microsoft.Insights/diagnosticSettings/storageAccountId",
                            "exists": false
                          }
                        ]
                      }
                    ]
                  }
                ]
              }
            },
            "greaterOrEquals": 1
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/245fc9df-fa96-4414-9a0b-3738c2f7341c",
  "type": "Microsoft.Authorization/policyDefinitions",
  "name": "245fc9df-fa96-4414-9a0b-3738c2f7341c"
}
Bylaw245fc9df-fa96-4414-9a0b-3738c2f7341c.bylaw.ps1 · 50 lines
# Converted from AuditDiagnosticLog_Audit.json by ConvertTo-Bylaw.
Policy '245fc9df-fa96-4414-9a0b-3738c2f7341c' {

    Properties @{
        DisplayName   = 'Resource logs in Azure Kubernetes Service should be enabled'
        Description   = 'Azure Kubernetes Service''s resource logs can help recreate activity trails when investigating security incidents. Enable it to make sure the logs will exist when needed'
        Version       = '1.0.0'
        Category      = 'Kubernetes'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter RequiredRetentionDays @{
        Default     = '365'
        DisplayName = 'Required retention (days)'
        Description = 'The required resource logs retention (in days)'
    }

    Resource 'Microsoft.ContainerService/managedClusters'

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' -Effect (Param Effect) {
        Require 'logs, retentionPolicy.enabled, retentionPolicy.days and 2 more are as required' {
            Count 'logs[*]' -Where {
                AnyOf {
                    AllOf {
                        Field retentionPolicy.enabled | Must -Be 'true'
                        AnyOf {
                            Field retentionPolicy.days | Must -Be '0'
                            Arm '[padLeft(current(''Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.days''), 3, ''0'')]' | Must -BeGreaterOrEqual (Arm '[padLeft(parameters(''requiredRetentionDays''), 3, ''0'')]')
                        }
                        Condition '{ "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "true" }'
                    }
                    AllOf {
                        Condition '{ "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "true" }'
                        AnyOf {
                            Field retentionPolicy.enabled | Must -Not -Be 'true'
                            Condition '{ "field": "Microsoft.Insights/diagnosticSettings/storageAccountId", "exists": false }'
                        }
                    }
                }
            } | Must -BeGreaterOrEqual 1
        }
    }
}

Source: built-in-policies/policyDefinitions/Kubernetes/AuditDiagnosticLog_Audit.json in Azure/azure-policy, under the MIT licence.