Bylaw

Gallery › SQL › Azure Community Policy

Ensure server parameter 'audit_log_enabled' is set to 'ON' for MySQL Database Server

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Enabling audit_log_enabled helps MySQL Database to log items such as connection attempts to the server, DDL/DML access, and more. Log data can be used to identify, troubleshoot, and repair configuration errors and suboptimal performance.

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

Azure Policy JSONazurepolicy.json · 110 lines
{
  "name": "4dc90661-5d91-41f1-be00-d243f6f3fe9c",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "Ensure server parameter 'audit_log_enabled' is set to 'ON' for MySQL Database Server",
    "description": "Enabling audit_log_enabled helps MySQL Database to log items such as connection attempts to the server, DDL/DML access, and more. Log data can be used to identify, troubleshoot, and repair configuration errors and suboptimal performance.",
    "metadata": {
      "version": "1.0.0",
      "category": "SQL"
    },
    "mode": "Indexed",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "DeployIfNotExists",
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.DBforMySQL/flexibleServers"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.DBforMySQL/flexibleServers/configurations",
          "name": "audit_log_enabled",
          "existenceCondition": {
            "field": "Microsoft.DBForMySql/flexibleServers/configurations/value",
            "equals": "ON"
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "location": {
                    "type": "string"
                  },
                  "serverName": {
                    "type": "String"
                  },
                  "serverParameters": {
                    "type": "Array"
                  }
                },
                "variables": {
                  "api": "2021-05-01",
                  "serverName": "[concat(parameters('serverName'),'/',parameters('serverParameters')[0].name)]"
                },
                "resources": [
                  {
                    "type": "Microsoft.DBforMySQL/flexibleServers/configurations",
                    "apiVersion": "[variables('api')]",
                    "name": "[variables('serverName')]",
                    "properties": {
                      "value": "[parameters('serverParameters')[0].value]",
                      "source": "[parameters('serverParameters')[0].source]"
                    }
                  }
                ],
                "outputs": {
                  "policy": {
                    "type": "string",
                    "value": "[concat('paramServerName: ', parameters('serverName'), ' | varServerName: ', parameters('serverName'), ' | Value: ', parameters('serverParameters')[0].value, ' | Source: ', parameters('serverParameters')[0].source,'')]"
                  }
                }
              },
              "parameters": {
                "serverName": {
                  "value": "[field('name')]"
                },
                "location": {
                  "value": "[field('location')]"
                },
                "serverParameters": {
                  "value": [
                    {
                      "name": "audit_log_enabled",
                      "value": "ON",
                      "source": "user-override"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
}
Bylaw4dc90661-5d91-41f1-be00-d243f6f3fe9c.bylaw.ps1 · 31 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '4dc90661-5d91-41f1-be00-d243f6f3fe9c' {

    Properties @{
        DisplayName = 'Ensure server parameter ''audit_log_enabled'' is set to ''ON'' for MySQL Database Server'
        Description = 'Enabling audit_log_enabled helps MySQL Database to log items such as connection attempts to the server, DDL/DML access, and more. Log data can be used to identify, troubleshoot, and repair configuration errors and suboptimal performance.'
        Version     = '1.0.0'
        Category    = 'SQL'
        Mode        = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'AuditIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'DeployIfNotExists, AuditIfNotExists or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.DBforMySQL/flexibleServers'

    DeployIfNotExists 'Microsoft.DBforMySQL/flexibleServers/configurations' -Role 'Contributor' -Name 'audit_log_enabled' -Effect (Param Effect) {
        Require 'value is ON' {
            Field value | Must -Be 'ON'
        }
        Deployment -TemplateFile './templates/4dc90661-5d91-41f1-be00-d243f6f3fe9c.json' -Parameters ([ordered]@{
                serverName       = (Arm '[field(''name'')]')
                location         = (Arm '[field(''location'')]')
                serverParameters = @([ordered]@{ name = 'audit_log_enabled'; value = 'ON'; source = 'user-override' })
            })
    }
}

Its deployment templates are written beside it:

Source: policyDefinitions/SQL/ensure-server-parameter-'audit_log_enabled'-is-on-for-mysql-db-server/azurepolicy.json in Azure/Community-Policy, under the MIT licence.