Bylaw

Gallery › Internet of Things › Azure built-in policies

Resource logs in IoT Hub should be enabled

Uses Condition Policy AuditIfNotExists Disabled Mode Indexed Version 3.1.0

Audit enabling of resource logs. This enables you to recreate activity trails to use for investigation purposes; when a security incident occurs or when your network is compromised

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 JSONIoTHub_AuditDiagnosticLog_Audit.json · 100 lines
{
  "properties": {
    "displayName": "Resource logs in IoT Hub should be enabled",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Audit enabling of resource logs. This enables you to recreate activity trails to use for investigation purposes; when a security incident occurs or when your network is compromised",
    "metadata": {
      "version": "3.1.0",
      "category": "Internet of Things"
    },
    "version": "3.1.0",
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "AuditIfNotExists",
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "requiredRetentionDays": {
        "type": "String",
        "defaultValue": "365",
        "metadata": {
          "displayName": "Required retention (days)",
          "description": "The required resource logs retention in days"
        }
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Devices/IotHubs"
      },
      "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": [
                      {
                        "not": {
                          "field": "Microsoft.Insights/diagnosticSettings/logs[*].retentionPolicy.enabled",
                          "equals": "true"
                        }
                      },
                      {
                        "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                        "equals": "true"
                      }
                    ]
                  }
                ]
              }
            },
            "greaterOrEquals": 1
          }
        }
      }
    },
    "versions": [
      "3.1.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/383856f8-de7f-44a2-81fc-e5135b5c2aa4",
  "name": "383856f8-de7f-44a2-81fc-e5135b5c2aa4"
}
Bylaw383856f8-de7f-44a2-81fc-e5135b5c2aa4.bylaw.ps1 · 48 lines
# Converted from IoTHub_AuditDiagnosticLog_Audit.json by ConvertTo-Bylaw.
Policy '383856f8-de7f-44a2-81fc-e5135b5c2aa4' {

    Properties @{
        DisplayName   = 'Resource logs in IoT Hub should be enabled'
        Description   = 'Audit enabling of resource logs. This enables you to recreate activity trails to use for investigation purposes; when a security incident occurs or when your network is compromised'
        Version       = '3.1.0'
        Category      = 'Internet of Things'
        PolicyVersion = '3.1.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        Type          = 'string'
        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.Devices/IotHubs'

    AuditIfNotExists 'Microsoft.Insights/diagnosticSettings' -Effect (Param Effect) {
        Require 'logs, retentionPolicy.enabled, retentionPolicy.days and 1 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 {
                        Field retentionPolicy.enabled | Must -Not -Be 'true'
                        Condition '{ "field": "Microsoft.Insights/diagnosticSettings/logs.enabled", "equals": "true" }'
                    }
                }
            } | Must -BeGreaterOrEqual 1
        }
    }
}

Source: built-in-policies/policyDefinitions/Internet of Things/IoTHub_AuditDiagnosticLog_Audit.json in Azure/azure-policy, under the MIT licence.