Bylaw

Gallery › Kubernetes › Azure built-in policies

Azure Kubernetes Service Clusters should use managed identities

Converted Policy Audit Disabled Mode Indexed Version 1.0.1

Use managed identities to wrap around service principals, simplify cluster management and avoid the complexity required to managed service principals. Learn more at: https://aka.ms/aks-update-managed-identities

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

Azure Policy JSONAKS_MSI_Audit.json · 54 lines
{
  "properties": {
    "policyType": "BuiltIn",
    "displayName": "Azure Kubernetes Service Clusters should use managed identities",
    "description": "Use managed identities to wrap around service principals, simplify cluster management and avoid the complexity required to managed service principals. Learn more at: https://aka.ms/aks-update-managed-identities",
    "metadata": {
      "version": "1.0.1",
      "category": "Kubernetes"
    },
    "version": "1.0.1",
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "Audit",
        "allowedValues": [
          "Audit",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy",
          "portalReview": true
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.ContainerService/managedClusters"
          },
          {
            "field": "Microsoft.ContainerService/managedClusters/servicePrincipalProfile",
            "exists": "True"
          },
          {
            "field": "Microsoft.ContainerService/managedClusters/servicePrincipalProfile.clientId",
            "notEquals": "msi"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/da6e2401-19da-4532-9141-fb8fbde08431",
  "name": "da6e2401-19da-4532-9141-fb8fbde08431"
}
Bylawda6e2401-19da-4532-9141-fb8fbde08431.bylaw.ps1 · 33 lines
# Converted from AKS_MSI_Audit.json by ConvertTo-Bylaw.
Policy 'da6e2401-19da-4532-9141-fb8fbde08431' {

    Properties @{
        DisplayName   = 'Azure Kubernetes Service Clusters should use managed identities'
        Description   = 'Use managed identities to wrap around service principals, simplify cluster management and avoid the complexity required to managed service principals. Learn more at: https://aka.ms/aks-update-managed-identities'
        Version       = '1.0.1'
        Category      = 'Kubernetes'
        PolicyVersion = '1.0.1'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
        Metadata      = [ordered]@{ portalReview = $true }
    }

    Resource 'Microsoft.ContainerService/managedClusters' {

        When {
            Field servicePrincipalProfile | Must -Exist
        }

        Require 'servicePrincipalProfile.clientId is msi' {
            Field servicePrincipalProfile.clientId | Must -Be 'msi'
        }
    }

    Effect (Param Effect)
}

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