Skip to main content

ARM and Resources Policies

Why?
Managing an Azure Subscription where 10 or 20 people have access can become a nightmare after the a few weeks. You will end up with people that creates resources will wrong name or in the right location.
You might want to have control what kind or resources can be created in specific location, what name should be used or what tags needs to be set for each resource. I remember once we even had a resource created in another subscription, that was discovered after almost one year.

What about Role-Based Access Control (RBAC)? Aren't the same...
RBAC and Resource Policies are focusing on different thing. They are complementary and together give you ability to have control at all levels. Role-Base Access is focussing on managing rights and actions that that a user can do. In contrast using resource policies you enforce naming convention at resource level, what resources can be created, in what Azure Regions and so on.
A good example of using RBAC and resource policies is when you give using RBAC rights to a user to create new services under a specific resource group. But you limit the type of resources that he can create in specific Azure Regions using resource policies.

Base Concept
There is no point to describe the structure of resource policies. It is well documented. I would like to focus more on examples and narrow cases.
For each resource policy that you define you will need to specify a tuple that contains (parameters, display name, description, policy rule logical evaluation, policy rule effect). The logic evaluation can be pretty complex and accept logic conditions and even nested operators. Very useful is also the parameters, that give us the possibility to specify the list of values later on. This kind of capabilities give the the possibility to reuse resource policies, in different places. Conditions like 'Equals', 'Like', 'Contains', 'In', 'ContainsKey' are accepted.

An important step once you define a resource policy is to specify the scope where you want the resource policy to apply. The scope of a resource policy can be:

  • Azure Resource
  • Azure Resource Group
  • Azure Subscription


Example: Add a default tag when no tag is specified
In the below example we add department tag automatically to a new resource that is created if there are no tags specified. Default value for our new added tag is 'general'.
{
  "if": {
    "field": "tags",
    "exists": "false"
  },
  "then": {
    "effect": "append",
    "details": [
      {
        "field": "tags",
        "value": {"department":"general" }
      }
    ]
  }
}

Additional to this, we can define a resource policy that would add department tag if is not defined. In this way we can enforce that department tag to be added to all resources that are created.
{
  "if": {
    "allOf": [
      {
        "field": "tags",
        "exists": "true"
      },
      {
        "field": "tags.department",
        "exists": "false"
      }
    ]
  },
  "then": {
    "effect": "append",
    "details": [
      {
        "field": "tags.department",
        "value": "general"
      }
    ]
  }
}

Example: Force tag definition
There might be cases when we want to rejects the creation of new resource if a specific tag is not defined. This can be done similar with the previous example, but changing the effect from 'append' to 'deny'.
{
  "if": {
    "field": "tags.department",
    "exists": "false"
  },
  "then": {
    "effect": "deny"
  }
}

Or can be implemented in a similar way by using 'containsKey' attribute.
{
  "if": {
    "not" : {
      "field" : "tags",
      "containsKey" : "department"
    }
  },
  "then" : {
    "effect" : "deny"
  }
}

Example: Force VM tag to be specified
In this example we enforce that for all VMs resources that are created, the tag VM and department to be specified.
{
    "if": {
        "allOf": [
          {
            "not": {
              "field": "tags",
              "containsKey": "department"
            }
          },
          { 
            "not" : {
              "field" : "tags",
              "equals" : "VM"
            }
          },
          {
            "field": "type",
            "equals": "Microsoft.Compute/virtualMachines"
          }
        ]
    },
    "then": {
        "effect": "deny"
    }
}


Example: Write in audit if the storage that is created  is not GRS with encryption activated
In the below example we write a warning in the Azure audit when we create a new storage account that is not Global Redundancy Storage and don't have encryptions activated.
{
  "if": {
    "allOf": [
      {
        "field": "type",
        "equals": "Microsoft.Storage/storageAccounts"
      },
      {
        "not": {
          "field": "Microsoft.Storage/storageAccounts/sku.name",
      "equals": "Standard_GRS"  
        }
      },
    {
        "not": {
          "field": "Microsoft.Storage/storageAccounts/enableBlobEncryption",
          "equals": "true"
        }
      }
    ]
  },
  "then": {
    "effect": "audit"
  }
}

Example: Add owner tag if it is not set
The below Azure resource policy, adds the owner tag if is not set. When creating the resource policy it is necessary to specify the owner name as parameter.
{
  "if": {
    "allof": [
      {
        "field": "tags",
        "exists": "true"
      },
      {
        "field": "tags.owner",
        "exists": "false"
      }
    ]
  },
  "then": {
    "effect": "append",
    "details": [
      {
        "field": "tags.owner",
        "value": "[parameters('userName')]"
      }
    ]
  }
}

Example: Limits users to create only specific resources
Using the below example, users can create only Compute, Storage and Network resources. Creating SQL Azure instances for example would be denied.

{
  "if" : {
    "not" : {
      "anyOf" : [
        {
          "field" : "type",
          "like" : "Microsoft.Compute/*"
        },
        {
          "field" : "type",
          "like" : "Microsoft.Storage/*"
        },
        {
          "field" : "type",
          "like" : "Microsoft.Network/*"
        }
      ]
    }
  },
  "then" : {
    "effect" : "deny"
  }
}

Conclusion
Azure resource policies are strong enough to offer us the capability to enforce different rules and policies specific to our project or system from the first moment when a user wants to create or update a specific resource.
The capabilities of resource policies together with Azure Resource Manager and RBAC give us the power to control E2E the life-cycles of Azure Services. You shall not forget that resource policies are not supported by all Azure Services, bust most of them are supported.

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(...

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see...

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provi...