Bylaw

Gallery › Kubernetes › Azure built-in policies

Azure Kubernetes Service Clusters should enable node os auto-upgrade

Converted Policy Audit Disabled Mode Indexed Version 1.0.0

AKS node OS auto-upgrade controls node-level OS security updates. Learn more at: https://learn.microsoft.com/en-us/azure/aks/auto-upgrade-node-image.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONAKS_Autoupgrade_NodeOS_Audit.json · 91 lines
{
  "properties": {
    "policyType": "BuiltIn",
    "displayName": "Azure Kubernetes Service Clusters should enable node os auto-upgrade",
    "description": "AKS node OS auto-upgrade controls node-level OS security updates. Learn more at: https://learn.microsoft.com/en-us/azure/aks/auto-upgrade-node-image.",
    "metadata": {
      "version": "1.0.0",
      "category": "Kubernetes"
    },
    "version": "1.0.0",
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy."
        }
      },
      "allowedNodeOsUpgradeChannels": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed Node OS Upgrade Channels",
          "description": "Enable or disable the execution of the policy.",
          "portalReview": true
        },
        "allowedValues": [
          "None",
          "Unmanaged",
          "NodeImage",
          "SecurityPatch"
        ],
        "defaultValue": [
          "NodeImage",
          "SecurityPatch"
        ]
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.ContainerService/managedClusters"
          },
          {
            "allOf": [
              {
                "anyOf": [
                  {
                    "field": "Microsoft.ContainerService/managedClusters/autoUpgradeProfile.upgradeChannel",
                    "exists": "false"
                  },
                  {
                    "field": "Microsoft.ContainerService/managedClusters/autoUpgradeProfile.upgradeChannel",
                    "notEquals": "node-image"
                  }
                ]
              },
              {
                "anyOf": [
                  {
                    "field": "Microsoft.ContainerService/managedClusters/autoUpgradeProfile.nodeOSUpgradeChannel",
                    "exists": false
                  },
                  {
                    "field": "Microsoft.ContainerService/managedClusters/autoUpgradeProfile.nodeOSUpgradeChannel",
                    "notin": "[parameters('allowedNodeOsUpgradeChannels')]"
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/04408ca5-aa10-42ce-8536-98955cdddd4c",
  "name": "04408ca5-aa10-42ce-8536-98955cdddd4c"
}
Bylaw04408ca5-aa10-42ce-8536-98955cdddd4c.bylaw.ps1 · 47 lines
# Converted from AKS_Autoupgrade_NodeOS_Audit.json by ConvertTo-Bylaw.
Policy '04408ca5-aa10-42ce-8536-98955cdddd4c' {

    Properties @{
        DisplayName   = 'Azure Kubernetes Service Clusters should enable node os auto-upgrade'
        Description   = 'AKS node OS auto-upgrade controls node-level OS security updates. Learn more at: https://learn.microsoft.com/en-us/azure/aks/auto-upgrade-node-image.'
        Version       = '1.0.0'
        Category      = 'Kubernetes'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy.'
    }
    Parameter AllowedNodeOsUpgradeChannels @{
        Type          = 'Array'
        AllowedValues = @('None', 'Unmanaged', 'NodeImage', 'SecurityPatch')
        Default       = @('NodeImage', 'SecurityPatch')
        DisplayName   = 'Allowed Node OS Upgrade Channels'
        Description   = 'Enable or disable the execution of the policy.'
        Metadata      = [ordered]@{ portalReview = $true }
    }

    Resource 'Microsoft.ContainerService/managedClusters' {

        When {
            AnyOf {
                Field autoUpgradeProfile.upgradeChannel | Must -Not -Exist
                Field autoUpgradeProfile.upgradeChannel | Must -Not -Be 'node-image'
            }
        }

        Require 'autoUpgradeProfile.nodeOSUpgradeChannel exists' {
            Field autoUpgradeProfile.nodeOSUpgradeChannel | Must -Exist
        }

        Require 'autoUpgradeProfile.nodeOSUpgradeChannel is one of the AllowedNodeOsUpgradeChannels parameter' {
            Field autoUpgradeProfile.nodeOSUpgradeChannel | Must -BeIn (Param AllowedNodeOsUpgradeChannels)
        }
    }

    Effect (Param Effect)
}

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