Bylaw

Gallery › General › Azure Community Policy

allowed-nc-for-allowed-locations-for-rgs

Converted Policy Audit Deny Disabled Mode All Version 1.0.0

need to add description

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

Azure Policy JSONazurepolicy.json · 162 lines
{
  "name": "196a1902-cfbe-448e-9940-449e13251ee2",
  "type": "Microsoft.Authorization/policyDefinitions",
  "properties": {
    "displayName": "allowed-nc-for-allowed-locations-for-rgs",
    "description": "need to add description",
    "metadata": {
      "version": "1.0.0",
      "category": "General"
    },
    "mode": "All",
    "parameters": {
      "companyPrefix": {
        "type": "String",
        "metadata": {
          "displayName": "Company Prefix",
          "description": "The Company Prefix you will use through all Resource Groups"
        },
        "defaultValue": "DMF"
      },
      "allowedLocations": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed locations 1",
          "description": "First allowed location when deploying a resource group",
          "strongType": "location"
        },
        "defaultValue": [
          "eastus"
        ]
      },
      "statePrefix": {
        "type": "String",
        "metadata": {
          "displayName": "Allowed State Prefix 1",
          "description": "The State Prefix you will allow for location 1"
        },
        "defaultValue": "VIR"
      },
      "allowedLocations2": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed locations 2",
          "description": "Second allowed location when deploying a resource group",
          "strongType": "location"
        },
        "defaultValue": [
          "southcentralus"
        ]
      },
      "statePrefix2": {
        "type": "String",
        "metadata": {
          "displayName": "Allowed State Prefix 2",
          "description": "The State Prefix you will allow for location 2"
        },
        "defaultValue": "TEX"
      },
      "allowedLocations3": {
        "type": "Array",
        "metadata": {
          "displayName": "Allowed locations 3",
          "description": "Third allowed location when deploying a resource group",
          "strongType": "location"
        },
        "defaultValue": [
          "westus"
        ]
      },
      "statePrefix3": {
        "type": "String",
        "metadata": {
          "displayName": "Allowed State Prefix 3",
          "description": "The State Prefix you will allow for location 3"
        },
        "defaultValue": "CAL"
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Deny, Audit or Disabled the execution of the Policy"
        },
        "allowedValues": [
          "Deny",
          "Audit",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Resources/subscriptions/resourceGroups"
              },
              {
                "anyOf": [
                  {
                    "field": "name",
                    "notMatch": "[concat(parameters('statePrefix'), '##-', parameters('companyPrefix'), '-...')]"
                  },
                  {
                    "field": "location",
                    "notIn": "[parameters('allowedLocations')]"
                  }
                ]
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Resources/subscriptions/resourceGroups"
              },
              {
                "anyOf": [
                  {
                    "field": "name",
                    "notMatch": "[concat(parameters('statePrefix2'), '##-', parameters('companyPrefix'), '-...')]"
                  },
                  {
                    "field": "location",
                    "notIn": "[parameters('allowedLocations2')]"
                  }
                ]
              }
            ]
          },
          {
            "allOf": [
              {
                "field": "type",
                "equals": "Microsoft.Resources/subscriptions/resourceGroups"
              },
              {
                "anyOf": [
                  {
                    "field": "name",
                    "notMatch": "[concat(parameters('statePrefix3'), '##-', parameters('companyPrefix'), '-...')]"
                  },
                  {
                    "field": "location",
                    "notIn": "[parameters('allowedLocations3')]"
                  }
                ]
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    }
  }
}
Bylaw196a1902-cfbe-448e-9940-449e13251ee2.bylaw.ps1 · 85 lines
# Converted from azurepolicy.json by ConvertTo-Bylaw.
Policy '196a1902-cfbe-448e-9940-449e13251ee2' {

    Properties @{
        DisplayName = 'allowed-nc-for-allowed-locations-for-rgs'
        Description = 'need to add description'
        Version     = '1.0.0'
        Category    = 'General'
        Mode        = 'All'
    }

    Parameter CompanyPrefix @{
        Default     = 'DMF'
        DisplayName = 'Company Prefix'
        Description = 'The Company Prefix you will use through all Resource Groups'
    }
    Parameter AllowedLocations @{
        Type        = 'Array'
        Default     = @('eastus')
        DisplayName = 'Allowed locations 1'
        Description = 'First allowed location when deploying a resource group'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter StatePrefix @{
        Default     = 'VIR'
        DisplayName = 'Allowed State Prefix 1'
        Description = 'The State Prefix you will allow for location 1'
    }
    Parameter AllowedLocations2 @{
        Type        = 'Array'
        Default     = @('southcentralus')
        DisplayName = 'Allowed locations 2'
        Description = 'Second allowed location when deploying a resource group'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter StatePrefix2 @{
        Default     = 'TEX'
        DisplayName = 'Allowed State Prefix 2'
        Description = 'The State Prefix you will allow for location 2'
    }
    Parameter AllowedLocations3 @{
        Type        = 'Array'
        Default     = @('westus')
        DisplayName = 'Allowed locations 3'
        Description = 'Third allowed location when deploying a resource group'
        Metadata    = [ordered]@{ strongType = 'location' }
    }
    Parameter StatePrefix3 @{
        Default     = 'CAL'
        DisplayName = 'Allowed State Prefix 3'
        Description = 'The State Prefix you will allow for location 3'
    }
    Parameter Effect @{
        AllowedValues = @('Deny', 'Audit', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Deny, Audit or Disabled the execution of the Policy'
    }

    Resource 'Microsoft.Resources/subscriptions/resourceGroups' {

        When {
            AnyOf {
                Field name | Must -Not -MatchCase (Arm '[concat(parameters(''statePrefix''), ''##-'', parameters(''companyPrefix''), ''-...'')]')
                Field location | Must -Not -BeIn (Param AllowedLocations)
            }
            Field type | Must -Be 'Microsoft.Resources/subscriptions/resourceGroups'
            AnyOf {
                Field name | Must -Not -MatchCase (Arm '[concat(parameters(''statePrefix2''), ''##-'', parameters(''companyPrefix''), ''-...'')]')
                Field location | Must -Not -BeIn (Param AllowedLocations2)
            }
            Field type | Must -Be 'Microsoft.Resources/subscriptions/resourceGroups'
        }

        Require 'name is as required' {
            Field name | Must -MatchCase (Arm '[concat(parameters(''statePrefix3''), ''##-'', parameters(''companyPrefix''), ''-...'')]')
        }

        Require 'location is one of the AllowedLocations3 parameter' {
            Field location | Must -BeIn (Param AllowedLocations3)
        }
    }

    Effect (Param Effect)
}

Source: policyDefinitions/General/allowed-nc-for-allowed-locations-for-rgs/azurepolicy.json in Azure/Community-Policy, under the MIT licence.