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

How to audit an Azure Cosmos DB

In this post, we will talk about how we can audit an Azure Cosmos DB database. Before jumping into the problem let us define the business requirement: As an Administrator I want to be able to audit all changes that were done to specific collection inside my Azure Cosmos DB. The requirement is simple, but can be a little tricky to implement fully. First of all when you are using Azure Cosmos DB or any other storage solution there are 99% odds that you’ll have more than one system that writes data to it. This means that you have or not have control on the systems that are doing any create/update/delete operations. Solution 1: Diagnostic Logs Cosmos DB allows us activate diagnostics logs and stream the output a storage account for achieving to other systems like Event Hub or Log Analytics. This would allow us to have information related to who, when, what, response code and how the access operation to our Cosmos DB was done. Beside this there is a field that specifies what was th...

Can User Technical Profile Influence AI Architecture Decisions?

In the last few days, I ran a small experiment on Devin/Windsurf/Cascade, BMAD, and LLM-based architecture decisions. The initial concern was simple: if an AI coding assistant knows something about the user, for example, that the user is a Java developer or a .NET developer, can this information influence the architecture and technology stack that the AI will propose? This question is important because many companies are starting to use AI tools not only for coding but also for product discovery, architecture, documentation, and technical decision-making. If the AI is influenced by the developer's personal profile, the result may not be fully neutral. It may look like a business or architecture decision, but in reality, it may be partially shaped by the context of the person using the tool. The test was done using BMAD with Devin/Windsurf/Cascade. The scenario was a unified field service operations platform. The task was to let BMAD refine the product, define the architecture and s...

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...