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:
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'.
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.
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'.
Or can be implemented in a similar way by using 'containsKey' attribute.
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.
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.
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.
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.
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.
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
Post a Comment