{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "description": "DugganUSA send-only-the-decisions lane: a DCR-based custom table on the Analytics plan, a Direct DCR (no DCE) whose transform keeps only the schema columns, and Monitoring Metrics Publisher on that DCR for the sender identity. Deploy into the resource group that holds the workspace."
  },
  "parameters": {
    "workspaceName": {
      "type": "string",
      "metadata": {
        "description": "Existing Log Analytics workspace (Sentinel-enabled) in this resource group."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Must equal the workspace region."
      }
    },
    "tableName": {
      "type": "string",
      "defaultValue": "DugganUSAEdgeDecisions_CL"
    },
    "dcrName": {
      "type": "string",
      "defaultValue": "dcr-dugganusa-edge-decisions"
    },
    "tablePlan": {
      "type": "string",
      "defaultValue": "Analytics",
      "allowedValues": [
        "Analytics",
        "Basic"
      ],
      "metadata": {
        "description": "Analytics: Sentinel scheduled analytics rules and workbooks at no per-query charge. Basic: cheaper ingestion, but no Sentinel scheduled rules and every query is billed."
      }
    },
    "retentionInDays": {
      "type": "int",
      "defaultValue": 90,
      "minValue": 4,
      "maxValue": 730,
      "metadata": {
        "description": "Interactive retention. 90 days is included at no extra charge on a Sentinel workspace."
      }
    },
    "senderPrincipalId": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "Object (principal) id of the managed identity that sends. Empty = skip the role assignment."
      }
    },
    "senderPrincipalType": {
      "type": "string",
      "defaultValue": "ServicePrincipal",
      "allowedValues": [
        "ServicePrincipal",
        "User",
        "Group"
      ]
    }
  },
  "variables": {
    "streamName": "Custom-DugganUSAEdgeDecisions",
    "columns": [
      {
        "name": "TimeGenerated",
        "type": "datetime"
      },
      {
        "name": "Kind",
        "type": "string"
      },
      {
        "name": "EventId",
        "type": "string"
      },
      {
        "name": "ClientIP",
        "type": "string"
      },
      {
        "name": "Country",
        "type": "string"
      },
      {
        "name": "ASN",
        "type": "string"
      },
      {
        "name": "Method",
        "type": "string"
      },
      {
        "name": "Path",
        "type": "string"
      },
      {
        "name": "Host",
        "type": "string"
      },
      {
        "name": "Ttp",
        "type": "string"
      },
      {
        "name": "Reason",
        "type": "string"
      },
      {
        "name": "RuleId",
        "type": "string"
      },
      {
        "name": "Confidence",
        "type": "int"
      },
      {
        "name": "HeldBasis",
        "type": "string"
      },
      {
        "name": "Actor",
        "type": "string"
      },
      {
        "name": "MalwareFamily",
        "type": "string"
      },
      {
        "name": "FirstSeen",
        "type": "datetime"
      },
      {
        "name": "Neighbors24",
        "type": "int"
      },
      {
        "name": "NeighborsNote",
        "type": "string"
      },
      {
        "name": "OnPublishedFeed",
        "type": "boolean"
      },
      {
        "name": "Cve",
        "type": "string"
      }
    ],
    "monitoringMetricsPublisher": "3913510d-42f4-4e42-8a64-420c390055eb",
    "transform": "source | where Kind in (\"block\", \"canary\") | project TimeGenerated, Kind, EventId, ClientIP, Country, ASN, Method, Path, Host, Ttp, Reason, RuleId, Confidence, HeldBasis, Actor, MalwareFamily, FirstSeen, Neighbors24, NeighborsNote, OnPublishedFeed, Cve"
  },
  "resources": [
    {
      "type": "Microsoft.OperationalInsights/workspaces/tables",
      "apiVersion": "2022-10-01",
      "name": "[format('{0}/{1}', parameters('workspaceName'), parameters('tableName'))]",
      "properties": {
        "plan": "[parameters('tablePlan')]",
        "retentionInDays": "[if(equals(parameters('tablePlan'), 'Analytics'), parameters('retentionInDays'), json('null'))]",
        "totalRetentionInDays": "[parameters('retentionInDays')]",
        "schema": {
          "name": "[parameters('tableName')]",
          "description": "DugganUSA Edge Shield decisions (block | canary), enriched before ingest. One row per decision; no raw request logs.",
          "columns": "[variables('columns')]"
        }
      }
    },
    {
      "type": "Microsoft.Insights/dataCollectionRules",
      "apiVersion": "2023-03-11",
      "name": "[parameters('dcrName')]",
      "location": "[parameters('location')]",
      "kind": "Direct",
      "dependsOn": [
        "[resourceId('Microsoft.OperationalInsights/workspaces/tables', parameters('workspaceName'), parameters('tableName'))]"
      ],
      "properties": {
        "description": "Edge Shield decisions to Sentinel. The transform keeps only the declared columns and only Kind block|canary.",
        "streamDeclarations": {
          "Custom-DugganUSAEdgeDecisions": {
            "columns": "[variables('columns')]"
          }
        },
        "destinations": {
          "logAnalytics": [
            {
              "name": "workspace",
              "workspaceResourceId": "[resourceId('Microsoft.OperationalInsights/workspaces', parameters('workspaceName'))]"
            }
          ]
        },
        "dataFlows": [
          {
            "streams": [
              "[variables('streamName')]"
            ],
            "destinations": [
              "workspace"
            ],
            "transformKql": "[variables('transform')]",
            "outputStream": "[format('Custom-{0}', parameters('tableName'))]"
          }
        ]
      }
    },
    {
      "condition": "[not(empty(parameters('senderPrincipalId')))]",
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2022-04-01",
      "scope": "[format('Microsoft.Insights/dataCollectionRules/{0}', parameters('dcrName'))]",
      "name": "[guid(resourceId('Microsoft.Insights/dataCollectionRules', parameters('dcrName')), parameters('senderPrincipalId'), variables('monitoringMetricsPublisher'))]",
      "dependsOn": [
        "[resourceId('Microsoft.Insights/dataCollectionRules', parameters('dcrName'))]"
      ],
      "properties": {
        "roleDefinitionId": "[subscriptionResourceId('Microsoft.Authorization/roleDefinitions', variables('monitoringMetricsPublisher'))]",
        "principalId": "[parameters('senderPrincipalId')]",
        "principalType": "[parameters('senderPrincipalType')]"
      }
    }
  ],
  "outputs": {
    "logsIngestionEndpoint": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.Insights/dataCollectionRules', parameters('dcrName')), '2023-03-11').endpoints.logsIngestion]"
    },
    "dcrImmutableId": {
      "type": "string",
      "value": "[reference(resourceId('Microsoft.Insights/dataCollectionRules', parameters('dcrName')), '2023-03-11').immutableId]"
    },
    "streamName": {
      "type": "string",
      "value": "[variables('streamName')]"
    }
  }
}
