Bylaw

Gallery › SQL › Azure Community Policy

Audit SQL Server firewall rule

Converted Policy AuditIfNotExists Disabled Mode All Version 1.0.0

Audits the existence of a rule that enables traffic from a specific IP range to a SQL Server.

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

Azure Policy JSONazurepolicy.json · 65 lines
{
  "name": "91130b0c-7c65-4569-9ed6-aaefb880427f",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Audit SQL Server firewall rule",
    "description": "Audits the existence of a rule that enables traffic from a specific IP range to a SQL Server.",
    "metadata": {
      "version": "1.0.0",
      "category": "SQL"
    },
    "mode": "All",
    "parameters": {
      "startIpAddress": {
        "type": "string",
        "metadata": {
          "displayName": "Start IP Address",
          "description": "The beginning of the IP address range. Example: 1.23.191.10"
        }
      },
      "endIpAddress": {
        "type": "string",
        "metadata": {
          "displayName": "End IP Address",
          "description": "The end of the IP address range. Example: 1.23.191.50"
        }
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Sql/servers"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Sql/servers/firewallRules",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Sql/servers/firewallRules/startIpAddress",
                "equals": "[parameters('startIpAddress')]"
              },
              {
                "field": "Microsoft.Sql/servers/firewallRules/endIpAddress",
                "equals": "[parameters('endIpAddress')]"
              }
            ]
          }
        }
      }
    }
  }
}
Bylaw91130b0c-7c65-4569-9ed6-aaefb880427f.bylaw.ps1 · 37 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '91130b0c-7c65-4569-9ed6-aaefb880427f' {

    Properties @{
        DisplayName = 'Audit SQL Server firewall rule'
        Description = 'Audits the existence of a rule that enables traffic from a specific IP range to a SQL Server.'
        Version     = '1.0.0'
        Category    = 'SQL'
        Mode        = 'All'
    }

    Parameter StartIpAddress @{
        Type        = 'string'
        DisplayName = 'Start IP Address'
        Description = 'The beginning of the IP address range. Example: 1.23.191.10'
    }
    Parameter EndIpAddress @{
        Type        = 'string'
        DisplayName = 'End IP Address'
        Description = 'The end of the IP address range. Example: 1.23.191.50'
    }
    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Sql/servers'

    AuditIfNotExists 'Microsoft.Sql/servers/firewallRules' -Effect (Param Effect) {
        Require 'startIpAddress is the StartIpAddress parameter and endIpAddress is the EndIpAddress parameter' {
            Field startIpAddress | Must -Be (Param StartIpAddress)
            Field endIpAddress | Must -Be (Param EndIpAddress)
        }
    }
}

Source: policyDefinitions/SQL/audit-sql-server-firewall-rule/azurepolicy.json in Azure/Community-Policy, under the MIT licence.