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

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

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills)

Cloud Myths: Cloud is Cheaper (Pill 1 of 5 / Cloud Pills) The idea that moving to the cloud reduces the costs is a common misconception. The cloud infrastructure provides flexibility, scalability, and better CAPEX, but it does not guarantee lower costs without proper optimisation and management of the cloud services and infrastructure. Idle and unused resources, overprovisioning, oversize databases, and unnecessary data transfer can increase running costs. The regional pricing mode, multi-cloud complexity, and cost variety add extra complexity to the cost function. Cloud adoption without a cost governance strategy can result in unexpected expenses. Improper usage, combined with a pay-as-you-go model, can result in a nightmare for business stakeholders who cannot track and manage the monthly costs. Cloud-native services such as AI services, managed databases, and analytics platforms are powerful, provide out-of-the-shelve capabilities, and increase business agility and innovation. H...