Bylaw

Gallery › Monitoring › Azure built-in policies

[Preview]: Data Collection Rules that target a specific workspace should use the specified Data Collection Endpoint

Converted Policy Audit Deny Disabled Mode Indexed Version 1.0.0-preview Preview

Audit or deny the creation of DCRs that target a workspace and don't use the specified DCE

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

Azure Policy JSONAzureMonitor_DCRs_Should_Use_DCE_Audit.json · 75 lines
{
  "properties": {
    "displayName": "[Preview]: Data Collection Rules that target a specific workspace should use the specified Data Collection Endpoint",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Audit or deny the creation of DCRs that target a workspace and don't use the specified DCE",
    "metadata": {
      "version": "1.0.0-preview",
      "category": "Monitoring",
      "preview": true
    },
    "version": "1.0.0-preview",
    "parameters": {
      "WorkspaceResourceId": {
        "type": "String",
        "metadata": {
          "displayName": "Log Analytics Workspace resource ID",
          "description": "The Workspace resource ID formatted as a resource path"
        }
      },
      "DceResourceId": {
        "type": "String",
        "metadata": {
          "displayName": "Data Collection Endpoint resource ID",
          "description": "The resource ID of the DCE associated with the workspace (System DCE) formatted as a resource path"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Insights/dataCollectionRules"
          },
          {
            "count": {
              "field": "Microsoft.Insights/dataCollectionRules/destinations.logAnalytics[*]",
              "where": {
                "field": "Microsoft.Insights/dataCollectionRules/destinations.logAnalytics[*].workspaceResourceId",
                "equals": "[parameters('WorkspaceResourceId')]"
              }
            },
            "equals": "[length(field('Microsoft.Insights/dataCollectionRules/destinations.logAnalytics[*]'))]"
          },
          {
            "field": "Microsoft.Insights/dataCollectionRules/dataCollectionEndpointId",
            "notEquals": "[parameters('DceResourceId')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "1.0.0-PREVIEW"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/1a94e687-23be-4c29-87a9-92c9cabae28f",
  "name": "1a94e687-23be-4c29-87a9-92c9cabae28f"
}
Bylaw1a94e687-23be-4c29-87a9-92c9cabae28f.bylaw.ps1 · 47 lines
# Converted from AzureMonitor_DCRs_Should_Use_DCE_Audit.json by ConvertTo-Bylaw.
Policy '1a94e687-23be-4c29-87a9-92c9cabae28f' {

    Properties @{
        DisplayName   = '[Preview]: Data Collection Rules that target a specific workspace should use the specified Data Collection Endpoint'
        Description   = 'Audit or deny the creation of DCRs that target a workspace and don''t use the specified DCE'
        Version       = '1.0.0-preview'
        Category      = 'Monitoring'
        Preview       = $true
        PolicyVersion = '1.0.0-preview'
        Mode          = 'Indexed'
    }

    Parameter WorkspaceResourceId @{
        Type        = 'String'
        DisplayName = 'Log Analytics Workspace resource ID'
        Description = 'The Workspace resource ID formatted as a resource path'
        JsonName    = 'WorkspaceResourceId'
    }
    Parameter DceResourceId @{
        Type        = 'String'
        DisplayName = 'Data Collection Endpoint resource ID'
        Description = 'The resource ID of the DCE associated with the workspace (System DCE) formatted as a resource path'
        JsonName    = 'DceResourceId'
    }
    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.Insights/dataCollectionRules' {

        When {
            Count 'destinations.logAnalytics[*]' -Where {
                Field workspaceResourceId | Must -Be (Param WorkspaceResourceId)
            } | Must -Be (Arm '[length(field(''Microsoft.Insights/dataCollectionRules/destinations.logAnalytics[*]''))]')
        }

        Require 'dataCollectionEndpointId is the DceResourceId parameter' {
            Field dataCollectionEndpointId | Must -Be (Param DceResourceId)
        }
    }

    Effect (Param Effect)
}

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