Bylaw

Gallery › PostgreSQL › Azure built-in policies

Configure PostgreSQL flexible server to enable specified configuration

Converted Policy AuditIfNotExists DeployIfNotExists Disabled Mode Indexed Version 1.0.0

Configures the specified PostgreSQL configuration setting to 'ON' for PostgreSQL flexible servers. This consolidated policy supports enabling log_checkpoints, log_connections, log_disconnections, connection_throttle.enable, or require_secure_transport. 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_ConfigurationEnabled_DINE.json · 105 lines
{
  "properties": {
    "displayName": "Configure PostgreSQL flexible server to enable specified configuration",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Configures the specified PostgreSQL configuration setting to 'ON' for PostgreSQL flexible servers. This consolidated policy supports enabling log_checkpoints, log_connections, log_disconnections, connection_throttle.enable, or require_secure_transport. See https://aka.ms/postgresql-security",
    "metadata": {
      "version": "1.0.0",
      "category": "PostgreSQL"
    },
    "version": "1.0.0",
    "parameters": {
      "configurationName": {
        "type": "String",
        "metadata": {
          "displayName": "Configuration Name",
          "description": "The PostgreSQL configuration setting name to enable"
        },
        "allowedValues": [
          "log_checkpoints",
          "log_connections",
          "log_disconnections",
          "connection_throttle.enable",
          "require_secure_transport"
        ]
      },
      "effect": {
        "type": "String",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "AuditIfNotExists",
          "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": "[parameters('configurationName')]",
          "existenceCondition": {
            "field": "Microsoft.DBforPostgreSQL/flexibleServers/configurations/value",
            "equals": "ON"
          },
          "roleDefinitionIds": [
            "/providers/microsoft.authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "serverName": {
                  "value": "[field('name')]"
                },
                "configurationName": {
                  "value": "[parameters('configurationName')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "serverName": {
                    "type": "string"
                  },
                  "configurationName": {
                    "type": "string"
                  }
                },
                "variables": {},
                "resources": [
                  {
                    "type": "Microsoft.DBforPostgreSQL/flexibleServers/configurations",
                    "apiVersion": "2022-12-01",
                    "name": "[concat(parameters('serverName'), '/', parameters('configurationName'))]",
                    "properties": {
                      "value": "ON",
                      "source": "user-override"
                    }
                  }
                ],
                "outputs": {}
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/a8f89ff4-3d6e-4d7b-8c5a-2e9b1f4c8d3a",
  "name": "a8f89ff4-3d6e-4d7b-8c5a-2e9b1f4c8d3a"
}
Bylawa8f89ff4-3d6e-4d7b-8c5a-2e9b1f4c8d3a.bylaw.ps1 · 37 lines
# Converted from FlexibleServers_ConfigurationEnabled_DINE.json by ConvertTo-Bylaw.
Policy 'a8f89ff4-3d6e-4d7b-8c5a-2e9b1f4c8d3a' {

    Properties @{
        DisplayName   = 'Configure PostgreSQL flexible server to enable specified configuration'
        Description   = 'Configures the specified PostgreSQL configuration setting to ''ON'' for PostgreSQL flexible servers. This consolidated policy supports enabling log_checkpoints, log_connections, log_disconnections, connection_throttle.enable, or require_secure_transport. See https://aka.ms/postgresql-security'
        Version       = '1.0.0'
        Category      = 'PostgreSQL'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter ConfigurationName @{
        Type          = 'String'
        AllowedValues = @('log_checkpoints', 'log_connections', 'log_disconnections', 'connection_throttle.enable', 'require_secure_transport')
        DisplayName   = 'Configuration Name'
        Description   = 'The PostgreSQL configuration setting name to enable'
    }
    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', '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 (Param ConfigurationName) -Effect (Param Effect) {
        Require 'value is ON' {
            Field value | Must -Be 'ON'
        }
        Deployment -TemplateFile './templates/a8f89ff4-3d6e-4d7b-8c5a-2e9b1f4c8d3a.json' -Parameters ([ordered]@{
                serverName        = (Arm '[field(''name'')]')
                configurationName = (Param ConfigurationName)
            })
    }
}

Its deployment templates are written beside it:

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