Bylaw

Gallery › App Service › Azure built-in policies

Function 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 Function 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 JSONFunctionApp_Slot_Audit_java_Latest.json · 81 lines
{
  "properties": {
    "displayName": "Function 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 Function 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 Function apps"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Web/sites/slots"
          },
          {
            "field": "kind",
            "contains": "functionapp"
          },
          {
            "field": "kind",
            "notContains": "workflowapp"
          },
          {
            "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/e1d1b522-02b0-4d18-a04f-5ab62d20445f",
  "name": "e1d1b522-02b0-4d18-a04f-5ab62d20445f"
}
Bylawe1d1b522-02b0-4d18-a04f-5ab62d20445f.bylaw.ps1 · 46 lines
# Converted from FunctionApp_Slot_Audit_java_Latest.json by ConvertTo-Bylaw.
Policy 'e1d1b522-02b0-4d18-a04f-5ab62d20445f' {

    Properties @{
        DisplayName   = 'Function 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 Function 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 Function apps'
        JsonName    = 'LinuxJavaVersion'
    }

    Resource 'Microsoft.Web/sites/slots' {

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

        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/FunctionApp_Slot_Audit_java_Latest.json in Azure/azure-policy, under the MIT licence.