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 check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

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.51EF 6.0.2VS2013
It seems that there …

[Post-Event] Codecamp Conference Cluj-Napoca - Nov 19, 2016

Last day I was invited to another Codecamp Conference, that took place in Cluj-Napoca. Like other Codecamp Conferences, the event was very big, with more than 1.000 participants and 70 sessions. There were 10 tracks in parallel, so it was pretty hard to decide at  what session you want to join.
It was great to join this conference and I hope that you discovered something new during the conference.
At this event I talked about Azure IoT Hub and how we can use it to connect devices from the field. I had a lot of demos using Raspberry PI 3 and Simplelink SensorTag. Most of the samples were written in C++ and Node.JS and people were impressed that even if we are using Microsoft technologies, we are not limited to C# and .NET. World and Microsoft are changing so fast. Just looking and Azure IoT Hub and new features that were launched and I'm pressed (Jobs, Methods, Device Twin).
On backend my demos covered Stream Analytics, Event Hub, Azure Object Storage and DocumentDB.

Title:
What abo…