Bylaw

Gallery › App Service › Azure built-in policies

App Service app slots that use Java should use a specified 'Java version'

Converted Policy AuditIfNotExists Disabled Mode Indexed Version 1.0.0

Periodically, newer versions are released for Java software either due to security flaws or to include additional functionality. Using the latest Java version for App Service apps is recommended in order to take advantage of security fixes, if any, and/or new functionalities of the latest version. This policy only applies to Linux apps. This policy requires you to specify a Java version that meets your requirements.

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

Azure Policy JSONWebApp_Slot_Audit_java_Latest.json · 77 lines
{
  "properties": {
    "displayName": "App Service app slots that use Java should use a specified 'Java version'",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Periodically, newer versions are released for Java software either due to security flaws or to include additional functionality. Using the latest Java version for App Service apps is recommended in order to take advantage of security fixes, if any, and/or new functionalities of the latest version. This policy only applies to Linux apps. This policy requires you to specify a Java version that meets your requirements.",
    "metadata": {
      "version": "1.0.0",
      "category": "App Service"
    },
    "version": "1.0.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        },
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "defaultValue": "AuditIfNotExists"
      },
      "LinuxJavaVersion": {
        "type": "String",
        "defaultValue": "",
        "metadata": {
          "displayName": "Linux Java version",
          "description": "Specify a supported Java version for App Service"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Web/sites/slots"
          },
          {
            "field": "kind",
            "notContains": "functionapp"
          },
          {
            "field": "kind",
            "contains": "linux"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Web/sites/slots/config",
          "name": "web",
          "existenceCondition": {
            "anyOf": [
              {
                "field": "Microsoft.Web/sites/slots/config/web.linuxFxVersion",
                "notContains": "JAVA"
              },
              {
                "field": "Microsoft.Web/sites/slots/config/web.linuxFxVersion",
                "equals": "[concat('JAVA|', parameters('LinuxJavaVersion'))]"
              }
            ]
          }
        }
      }
    },
    "versions": [
      "1.0.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/46dad49f-8945-44d7-9bb1-2e1542f627d3",
  "name": "46dad49f-8945-44d7-9bb1-2e1542f627d3"
}
Bylaw46dad49f-8945-44d7-9bb1-2e1542f627d3.bylaw.ps1 · 45 lines
# Converted from WebApp_Slot_Audit_java_Latest.json by ConvertTo-Bylaw.
Policy '46dad49f-8945-44d7-9bb1-2e1542f627d3' {

    Properties @{
        DisplayName   = 'App Service app slots that use Java should use a specified ''Java version'''
        Description   = 'Periodically, newer versions are released for Java software either due to security flaws or to include additional functionality. Using the latest Java version for App Service apps is recommended in order to take advantage of security fixes, if any, and/or new functionalities of the latest version. This policy only applies to Linux apps. This policy requires you to specify a Java version that meets your requirements.'
        Version       = '1.0.0'
        Category      = 'App Service'
        PolicyVersion = '1.0.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('AuditIfNotExists', 'Disabled')
        Default       = 'AuditIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }
    Parameter LinuxJavaVersion @{
        Default     = ''
        DisplayName = 'Linux Java version'
        Description = 'Specify a supported Java version for App Service'
        JsonName    = 'LinuxJavaVersion'
    }

    Resource 'Microsoft.Web/sites/slots' {

        When {
            Field kind | Must -Not -ContainText 'functionapp'
        }

        Require 'kind does not contain linux' {
            Field kind | Must -Not -ContainText 'linux' -IfPresent
        }
    }

    AuditIfNotExists 'Microsoft.Web/sites/slots/config' -Name 'web' -Effect (Param Effect) {
        Require 'web.linuxFxVersion is as required' {
            AnyOf {
                Field web.linuxFxVersion | Must -Not -ContainText 'JAVA' -IfPresent
                Field web.linuxFxVersion | Must -Be (Arm '[concat(''JAVA|'', parameters(''LinuxJavaVersion''))]')
            }
        }
    }
}

Source: built-in-policies/policyDefinitions/App Service/WebApp_Slot_Audit_java_Latest.json in Azure/azure-policy, under the MIT licence.