Enable HTTPS on Azure App Services with Azure Policy

7 Min. Read

Today, a common theme in cloud environments is enforcing organizational standards and adopting cloud governance since day one. And this is very important since it will give you the ability to define policies, processes, and procedures. These policies then dictate what can be done and verify that what does exist is correct. A service from Microsoft called Azure Policy is a great way to make that happen and take corrective action.

This article will demonstrate how to enable HTTPS on Azure App Services (Web App and Function App) with Azure Policy.

Introduction

Azure App Service enables you to build and host web apps, mobile back ends, and RESTful APIs in the programming language of your choice without managing infrastructure. It offers auto-scaling and high availability, supports both Windows and Linux, and enables automated deployments from GitHub, Azure DevOps, or any Git repo.

Azure Policy is a service in Azure that you use to create, assign, and manage policies. These policies enforce different rules and effects over your resources, so those resources stay compliant with your corporate standards and service level agreements. Azure Policy meets this need by continuously evaluating your resources for non-compliance with assigned policies.

You have a policy in your organization that dictates to enforce HTTPS on all new and existing App Services (web applications and functions) within your subscriptions. As you know very well, you should protect your applications from potential threats by encrypting data in transit.

If we take a quick look at Azure Security Center as shown in the figure below, we do have already a built-in Policy definition which is based on the default Azure Security Benchmark (V2) that requires having HTTPS enabled at the application level to pass this recommendation.

Security Center - FTPS should be required in your web App
Security Center – FTPS should be required in your web App

Based on this built-in policy, when you deploy a web app, Azure Security Center will evaluate the configuration and recommends enabling HTTPS to ensure server/service authentication and protect your data in transit from network layer eavesdropping attacks.

However, this policy does not auto-remediate the unhealthy resources, therefore, you should manually remediate them as described in the ‘Remediation steps‘, you can also use the ‘Quick fix logic‘ option to remediate all unhealthy resources at once, or you can trigger a logic app if you want. You can also exempt this recommendation if needed, but I don’t recommend doing so because it’s categorized as ‘Medium‘ severity.

Please note that when you enable HTTPS on your web app, it will redirect all HTTP traffic to HTTPS. So users with a custom domain should verify their domain has an SSL certificate before using this remediation. Because an HTTPS endpoint which doesn’t have an SSL certificate will show up in the browser with a “Privacy Error” and break your application. Therefore, users who have a custom domain need to verify they have set up an SSL certificate. Please make sure to notify the dev team before you remediate existing running system.

Once you have invested time and effort in remediating these resources to increase your Secure Score, you want to make sure that it will not decrease by a serious percentage, again. The problem is that the remediation is always done for existing resources, but NOT for new ones. You can think of remediating resources as a reactive versus a proactive approach.

As cybersecurity hygiene (practice), you need to have a solid Azure Governance to ensure that the new resources you deploy will have certain security standards, patterns, and configurations.

To ensure proper governance, we need to use Azure Policy. This will allow us to enable and enforce HTTPS on new deployed App Services, as well as remediating existing resources at scale, so you can make sure your organization’s policy and security requirements are met.

Prerequisites

To follow this article, you need to have the following:

  1. An Azure subscription. If you don’t have an Azure subscription, you can create a free one here.
  2. At least one web app or function app is deployed in your subscription.
  3. You need to have the appropriate permissions to create and manage Azure Policy definitions. The Azure RBAC built-in roles that you can use are Resource Policy Contributor or Security Admin.
  4. Last but not least, you need to have the appropriate permissions to assign the Contributor role for the Managed Identity (Application ID) created during the assignment of the policy either on a management group or a subscription, so the policy with “DeployIfNotExists” can remediate and modify your Web App settings. Azure Policy creates a managed identity for each assignment but must have details about what roles to grant the managed identity. If the managed identity is missing roles, an error is displayed during the assignment of the policy or an initiative. Please note that when creating an assignment through the Azure Portal, the role assignments are auto-granted. However, when using PowerShell, you must manually configure the managed identity. More information can be found here.

Assuming you have all the prerequisites in place, take now the following steps:

Create Custom Policy definition

A quick overview of Azure Policy effects. Each policy definition that you create in the Azure Policy has a single effect. That effect determines what happens when the policy rule is evaluated to match. The effects behave differently if they are for a new resource, an updated resource, or an existing resource.

In this example, we need to set and enable HTTPS configuration for all Azure App Services and on existing resource(s) in our environment. For this to work, we need to use the “DeployIfNotExists” policy. To understand how the Azure Policy effect works with the “DeployIfNotExists” policy definition, please check the official documentation from Microsoft here.

Open the Azure Portal, click “All services” and then search for “Policy” and then click on “Definitions” → “+ Policy definition”.

For the “Definition location“, select the location by clicking the ellipsis [] and select either a management group or a subscription.

In the “Name“ field, give a descriptive name for the policy definition such as – “Enable HTTPS for Azure App Services” and Description.

In the “Category” section, select the appropriate category for this policy. In this example, I will choose “App Service” as shown in the figure below.

Create Azure Policy Custom Definition
Create Azure Policy Custom Definition

For the “POLICY RULE“, paste the following policy definition in JSON format and then click “Save“.

For the “Role definitions“, make sure that the Contributor is selected. The contributor role is required to set and enable HTTPS settings on the resources.

Here is the Policy definition in JSON format:

{
  "mode": "All",
  "policyRule": {
    "if": {
      "allOf": [
        {
          "field": "type",
          "equals": "Microsoft.Web/sites"
        },
        {
          "field": "Microsoft.Web/sites/httpsOnly",
          "equals": "false"
        }
      ]
    },
    "then": {
      "effect": "[parameters('effect')]",
      "details": {
        "type": "Microsoft.Web/sites",
        "existenceCondition": {
          "field": "Microsoft.Web/sites/httpsOnly",
          "equals": "true"
        },
        "roleDefinitionIds": [
          "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
        ],
        "deployment": {
          "properties": {
            "mode": "incremental",
            "parameters": {
              "webAppName": {
                "value": "[field('name')]"
              },
              "location": {
                "value": "[field('location')]"
              }
            },
            "template": {
              "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
              "contentVersion": "1.0.0.0",
              "parameters": {
                "webAppName": {
                  "type": "string"
                },
                "location": {
                  "type": "string"
                }
              },
              "resources": [
                {
                  "name": "[concat(parameters('webAppName'))]",
                  "type": "Microsoft.Web/sites",
                  "apiVersion": "2020-09-01",
                  "location": "[parameters('location')]",
                  "properties": {
                    "httpsOnly": "true"
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "parameters": {
    "effect": {
      "type": "String",
      "metadata": {
        "displayName": "Effect",
        "description": "Enable or disable the execution of the policy"
      },
      "allowedValues": [
        "DeployIfNotExists",
        "Disabled"
      ],
      "defaultValue": "DeployIfNotExists"
    }
  }
}

In this custom policy, I am looking for all kinds of applications (web app, container, windows, and function app), and then I am checking if HTTPS is enabled.

The ExistenceCondition option in the template above is super useful for the Azure App Services that has already HTTPS settings enabled. If this condition is true, the policy definition will skip the deployment defined in the “DeployIfNotExists” section in the template. Please check the documentation here to learn more about the ExistenceCondition option for the “DeployIfNotExists” properties.

Assign Custom Policy definition

To assign the custom policy definition, take the following steps:

Open the Azure Portal, click “All services” and then search for “Policy” and then click on “Assignments”. An assignment is a policy that has been assigned to take place within a specific scope.

Select “Assign policy” from the top of the “Policy | Assignments” page.

On the “Assign Policy” page, select the Scope by clicking the ellipsis [] and select either a management group or subscription. You can optionally select a resource group if you want. A scope determines what resources or grouping of resources the policy assignment gets enforced on. Then click Select at the bottom of the Scope page.

Select the Policy definition ellipsis [] to open the list of available definitions. Choose the custom policy that we created in the previous step. The Policy enforcement is Enabled by default. Click Next to continue.

On the Parameters page, specify the parameters for this policy assignment (DeployIfNotExists or Disabled). The default value is set to “DeployIfNotExists“, you can disable the effect of this policy later on by setting the effect to “Disabled“. Click Next to continue.

Assign Azure Policy Custom Definition
Assign Azure Policy Custom Definition

In the Remediation page, by default “Create a Managed Identity” is selected for you because existing resources can be updated via a remediation task after the policy is assigned only. Choose the default location for the “Managed Identity“, this is required because policies with the “deployIfNotExists” and “Modify” effect types need the ability to modify resources and edit the configuration on existing resources respectively. To do this, a managed identity will be created automatically for each policy assignment. This identity will also be given the “Contributor” permissions on the scope that you have selected. Click Next to continue.

Remediation - Create a Managed Identity
Remediation – Create a Managed Identity

On the Non-compliance messages page, set the desired message. Then click the “Review + create” button.

Azure Policy - Non-compliance message
Azure Policy – Non-compliance message

Finally, click “Create” to create the assignment.

Verify HTTPS on Azure App Services

To verify that the custom policy is deployed successfully, you need to wait for at least 30 minutes after a resource has been created or updated. The policy won’t be triggered immediately, this is by design. To trigger the policy compliance scan manually (immediately), open the cloud shell and run the following command:

$job = Start-AzPolicyComplianceScan -AsJob
$job | ft -AutoSize

The scan job will run in the background as shown in the output below.

Trigger Azure Policy Compliance Evaluation Manually
Trigger Azure Policy Compliance Evaluation Manually

Behind the scene, Azure Policy will create a Remediation task as shown in the figure below. To remediate existing resources, select the desired policy and then click on the “Remediate” button.

Policy Remediation - Enable HTTPS for Azure App Services
Policy Remediation – Enable HTTPS for Azure App Services

Please note that during an evaluation cycle, the policy definition with a “DeployIfNotExists” effect that matches resources is marked as non-compliant only, but no action is taken on that resource. For this reason, the remediation task was created when you assign the policy definition as described in the previous section.

Open the Azure Portal, click “All services” and then search for “Policy” and then click on “Remediation”. In the Policy | Remediation page, select the Remediation tasks tab, and check the status of the auto-remediation task. It’s completed successfully without any error.

Azure Policy - Remediation tasks
Azure Policy – Remediation tasks

If you look at the Web App resource under Settings | Custom domains, you can see that the “HTTPS Only” toggle is set to On.

Azure App Service - HTTPS Only
Azure App Service – HTTPS Only

If we look at the compliance policy state, we will see that all Azure App Services in my environment are compliant and HTTPS is enabled.

Azure Policy Compliance State
Azure Policy Compliance State

That’s it there you have it!

Summary

In this article, I showed you how to enable and enforce HTTPS on Azure App Services with Azure Policy at the application level, so you can make sure your organization’s policy and security requirements are met.

Please note that you need to have a separate custom policy for each Azure resource or for each configuration you need to modify such as enabling FTPS or HTTPS. You could create all the needed policy definitions for your environment and then add them all to an initiative definition and then assign the initiative scope based on Management Group, Subscription, or Resource Group. I highly recommend using Management Groups if you are not doing so already.

To learn more about auditing publicly accessible Azure App Services with Azure Policy, please check the following step-by-step guide.

To learn more about auditing subnets that do not have Network Security Group (NSG) associated, please check the following step-by-step guide.

To learn more about Azure Policy, please check the official documentation from Microsoft.

__
Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

Related Posts

Previous

Enable FTPS on Azure App Services with Azure Policy

Enable Azure Backup on VMs using Azure Policy

Next

2 thoughts on “Enable HTTPS on Azure App Services with Azure Policy”

Leave a comment...

  1. Wonderful article Charbel, I’m working exactly the same policy which enforces HTTPS on web apps as expected. My only concern regarding the DeployIfNotExists type of policy is whether other properties that aren’t included in the template get reset as well, I don’t know if there is any way to rule out this possibility completely. Any thoughts? Thanks.

  2. Hello Tim, thank you for the feedback!
    Good question. The DeployIfNotExists type of policy won’t reset any other properties which aren’t included in the template because, under the deployment section properties, we are using the “incremental” mode so only what we have specified will get updated which in this case “Enable HTTPS”. I’ve tested it many times in production and it works as expected without breaking any other settings. Hope this helps!

Let me know what you think, or ask a question...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Stay in Touch

Never miss out on your favorite posts and our latest announcements!

The content of this website is copyrighted from being plagiarized!

You can copy from the 'Code Blocks' in 'Black' by selecting the Code.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!