Bylaw

Gallery › SQL › Azure Community Policy

PostgreSQL flexible servers administrator - Enforce a naming convention to be used for Administrator groups

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

PostgreSQL flexible servers supports Entra ID Security groups to be PostgreSQL Administrator, This policy will only allow groups following a certain naming convention to be allowed as Administrator.

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

Azure Policy JSONazurepolicy.json · 56 lines
{
  "name": "d78f353a-a5e7-4747-8d31-62f361bafac6",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "mode": "All",
    "displayName": "PostgreSQL flexible servers administrator - Enforce a naming convention to be used for Administrator groups",
    "description": "PostgreSQL flexible servers supports Entra ID Security groups to be PostgreSQL Administrator, This policy will only allow groups following a certain naming convention to be allowed as Administrator.",
    "metadata": {
      "version": "1.0.0",
      "category": "SQL"
    },
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "Deny",
        "allowedValues": [
          "Audit",
          "Disabled",
          "Deny"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "namePattern": {
        "type": "string",
        "metadata": {
          "displayName": "Naming pattern to enforce",
          "description": "Allowed Entra ID Security group name pattern to enforce, for example Admin_* will enforce that the group starts with Admin_"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.DBforPostgreSQL/flexibleServers/administrators"
          },
          {
            "field": "Microsoft.DBforPostgreSQL/flexibleServers/administrators/principalType",
            "equals": "Group"
          },
          {
            "field": "Microsoft.DBforPostgreSQL/flexibleServers/administrators/principalName",
            "notLike": "[parameters('namePattern')]"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylawd78f353a-a5e7-4747-8d31-62f361bafac6.bylaw.ps1 · 36 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy 'd78f353a-a5e7-4747-8d31-62f361bafac6' {

    Properties @{
        DisplayName = 'PostgreSQL flexible servers administrator - Enforce a naming convention to be used for Administrator groups'
        Description = 'PostgreSQL flexible servers supports Entra ID Security groups to be PostgreSQL Administrator, This policy will only allow groups following a certain naming convention to be allowed as Administrator.'
        Version     = '1.0.0'
        Category    = 'SQL'
        Mode        = 'All'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Disabled', 'Deny')
        Default       = 'Deny'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter NamePattern @{
        Type        = 'string'
        DisplayName = 'Naming pattern to enforce'
        Description = 'Allowed Entra ID Security group name pattern to enforce, for example Admin_* will enforce that the group starts with Admin_'
    }

    Resource 'Microsoft.DBforPostgreSQL/flexibleServers/administrators' {

        When {
            Field principalType | Must -Be 'Group'
        }

        Require 'principalName is like the NamePattern parameter' {
            Field principalName | Must -BeLike (Param NamePattern)
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/SQL/enforce-postgresql-administrator-group-principal-name-prefix/azurepolicy.json in Azure/Community-Policy, under the MIT licence.