Bylaw

Gallery › Storage › Azure built-in policies

Storage account keys should not be expired

Converted Policy Audit Deny Disabled Mode Indexed Version 3.0.0

Ensure the user storage account keys are not expired when key expiration policy is set, for improving security of account keys by taking action when the keys are expired.

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

Azure Policy JSONStorageAccountKeysExpired_Restrict.json · 58 lines
{
  "properties": {
    "displayName": "Storage account keys should not be expired",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Ensure the user storage account keys are not expired when key expiration policy is set, for improving security of account keys by taking action when the keys are expired.",
    "metadata": {
      "version": "3.0.0",
      "category": "Storage"
    },
    "version": "3.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Audit allows a non-compliant resource to be created, but flags it as non-compliant. Deny blocks the resource creation and update. Disable turns off the policy."
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Audit"
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Storage/storageAccounts"
          },
          {
            "anyOf": [
              {
                "value": "[utcNow()]",
                "greater": "[if(and(not(empty(coalesce(field('Microsoft.Storage/storageAccounts/keyCreationTime.key1'), ''))), not(empty(string(coalesce(field('Microsoft.Storage/storageAccounts/keyPolicy.keyExpirationPeriodInDays'), ''))))), addDays(field('Microsoft.Storage/storageAccounts/keyCreationTime.key1'), field('Microsoft.Storage/storageAccounts/keyPolicy.keyExpirationPeriodInDays')), utcNow())]"
              },
              {
                "value": "[utcNow()]",
                "greater": "[if(and(not(empty(coalesce(field('Microsoft.Storage/storageAccounts/keyCreationTime.key2'), ''))), not(empty(string(coalesce(field('Microsoft.Storage/storageAccounts/keyPolicy.keyExpirationPeriodInDays'), ''))))), addDays(field('Microsoft.Storage/storageAccounts/keyCreationTime.key2'), field('Microsoft.Storage/storageAccounts/keyPolicy.keyExpirationPeriodInDays')), utcNow())]"
              }
            ]
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]"
      }
    },
    "versions": [
      "3.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/044985bb-afe1-42cd-8a36-9d5d42424537",
  "name": "044985bb-afe1-42cd-8a36-9d5d42424537"
}
Bylaw044985bb-afe1-42cd-8a36-9d5d42424537.bylaw.ps1 · 32 lines
# Converted from StorageAccountKeysExpired_Restrict.json by ConvertTo-Bylaw.
Policy '044985bb-afe1-42cd-8a36-9d5d42424537' {

    Properties @{
        DisplayName   = 'Storage account keys should not be expired'
        Description   = 'Ensure the user storage account keys are not expired when key expiration policy is set, for improving security of account keys by taking action when the keys are expired.'
        Version       = '3.0.0'
        Category      = 'Storage'
        PolicyVersion = '3.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('Audit', 'Deny', 'Disabled')
        Default       = 'Audit'
        DisplayName   = 'Effect'
        Description   = 'Audit allows a non-compliant resource to be created, but flags it as non-compliant. Deny blocks the resource creation and update. Disable turns off the policy.'
    }

    Resource 'Microsoft.Storage/storageAccounts' {

        Require 'keyCreationTime.key1 and keyPolicy.keyExpirationPeriodInDays are as required' {
            Arm '[utcNow()]' | Must -BeLessOrEqual (Arm '[if(and(not(empty(coalesce(field(''Microsoft.Storage/storageAccounts/keyCreationTime.key1''), ''''))), not(empty(string(coalesce(field(''Microsoft.Storage/storageAccounts/keyPolicy.keyExpirationPeriodInDays''), ''''))))), addDays(field(''Microsoft.Storage/storageAccounts/keyCreationTime.key1''), field(''Microsoft.Storage/storageAccounts/keyPolicy.keyExpirationPeriodInDays'')), utcNow())]')
        }

        Require 'keyCreationTime.key2 and keyPolicy.keyExpirationPeriodInDays are as required' {
            Arm '[utcNow()]' | Must -BeLessOrEqual (Arm '[if(and(not(empty(coalesce(field(''Microsoft.Storage/storageAccounts/keyCreationTime.key2''), ''''))), not(empty(string(coalesce(field(''Microsoft.Storage/storageAccounts/keyPolicy.keyExpirationPeriodInDays''), ''''))))), addDays(field(''Microsoft.Storage/storageAccounts/keyCreationTime.key2''), field(''Microsoft.Storage/storageAccounts/keyPolicy.keyExpirationPeriodInDays'')), utcNow())]')
        }
    }

    Effect (Param Effect)
}

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