Bylaw

Gallery › PostgreSQL › Azure built-in policies

Configure shared_preload_libraries to include pgaudit for PostgreSQL flexible servers

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Configures shared_preload_libraries to include 'pgaudit' for PostgreSQL flexible servers to enable PgAudit extension for comprehensive auditing. See https://aka.ms/postgresql-security

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

Azure Policy JSONFlexibleServers_EnablePgAudit_DINE.json · 84 lines
{
  "properties": {
    "displayName": "Configure shared_preload_libraries to include pgaudit for PostgreSQL flexible servers",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Configures shared_preload_libraries to include 'pgaudit' for PostgreSQL flexible servers to enable PgAudit extension for comprehensive auditing. See https://aka.ms/postgresql-security",
    "metadata": {
      "version": "1.0.0",
      "category": "PostgreSQL"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.DBforPostgreSQL/flexibleServers"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.DBforPostgreSQL/flexibleServers/configurations",
          "name": "shared_preload_libraries",
          "existenceCondition": {
            "field": "Microsoft.DBforPostgreSQL/flexibleServers/configurations/value",
            "contains": "pgaudit"
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "serverName": {
                  "value": "[field('name')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "serverName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.DBforPostgreSQL/flexibleServers/configurations",
                    "apiVersion": "2022-12-01",
                    "name": "[concat(parameters('serverName'), '/shared_preload_libraries')]",
                    "properties": {
                      "value": "pgaudit",
                      "source": "user-override"
                    }
                  }
                ],
                "outputs": {}
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/dd626066-39f5-4f72-aba8-06a99fabf278",
  "name": "dd626066-39f5-4f72-aba8-06a99fabf278"
}
Bylawdd626066-39f5-4f72-aba8-06a99fabf278.bylaw.ps1 · 30 lines
# Converted from FlexibleServers_EnablePgAudit_DINE.json by ConvertTo-Bylaw.
Policy 'dd626066-39f5-4f72-aba8-06a99fabf278' {

    Properties @{
        DisplayName   = 'Configure shared_preload_libraries to include pgaudit for PostgreSQL flexible servers'
        Description   = 'Configures shared_preload_libraries to include ''pgaudit'' for PostgreSQL flexible servers to enable PgAudit extension for comprehensive auditing. See https://aka.ms/postgresql-security'
        Version       = '1.0.0'
        Category      = 'PostgreSQL'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

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

    Resource 'Microsoft.DBforPostgreSQL/flexibleServers'

    DeployIfNotExists 'Microsoft.DBforPostgreSQL/flexibleServers/configurations' -Role 'Contributor' -Name 'shared_preload_libraries' -Effect (Param Effect) {
        Require 'value contains pgaudit' {
            Field value | Must -ContainText 'pgaudit'
        }
        Deployment -TemplateFile './templates/dd626066-39f5-4f72-aba8-06a99fabf278.json' -Parameters ([ordered]@{
                serverName = (Arm '[field(''name'')]')
            })
    }
}

Its deployment templates are written beside it:

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