Azure PsRule, check if resource exists, if so validate that there is another resource with it as parent #1969
-
Hey guys, This has been bugging me for a while now. Thanks, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
@C0smin Deployments are streamed through the pipeline for both Bicep and ARM templates. So this is probably the answer, although if you have any bicep sample code that would help. To write a rule to check a deployment use the Some examples are here: https://github.com/Azure/PSRule.Rules.Azure/blob/main/src/PSRule.Rules.Azure/rules/Azure.Deployment.Rule.ps1 Alternatively you may be able to check for a sub-resource of the protected object. PSRule for Azure automatically nests sub-resources or resources with a child scope. It does this to help build a graph of related resources for the purpose you are describing. For example: param storageAccountName string = 'sa001'
param location string = 'eastus'
resource sa 'Microsoft.Storage/storageAccounts@2021-04-01' = {
name: storageAccountName
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {}
}
resource atpSettings 'Microsoft.Security/advancedThreatProtectionSettings@2019-01-01' = {
name: 'current'
scope: sa
properties: {
isEnabled: true
}
} Will become: {
"type": "Microsoft.Storage/storageAccounts",
"name": "sa001",
"location": "eastus",
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {},
"id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/ps-rule-test-rg/providers/Microsoft.Storage/storageAccounts/sa001",
"_PSRule": {
"path": "resources[0]",
"source": [
{
"file": ".\\docs\\ex.json",
"type": "Template",
"line": 22,
"position": 5
}
]
},
"resources": [
{
"type": "Microsoft.Security/advancedThreatProtectionSettings",
"scope": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/ps-rule-test-rg/providers/Microsoft.Storage/storageAccounts/sa001",
"name": "current",
"properties": {
"isEnabled": true
},
"id": "/subscriptions/ffffffff-ffff-ffff-ffff-ffffffffffff/resourceGroups/ps-rule-test-rg/providers/Microsoft.Security/advancedThreatProtectionSettings/current",
"_PSRule": {
"path": "resources[1]",
"source": [
{
"file": ".\\docs\\ex.json",
"type": "Template",
"line": 33,
"position": 5
}
]
}
}
]
} No action is required for this to happen when expansion is enabled, that's just how it works. So then your would write a rule for There is quite a lot of rules in PSRule for Azure that do this, however here are some that might be close to what you are trying to achieve.
I hope that helps. |
Beta Was this translation helpful? Give feedback.
@C0smin Deployments are streamed through the pipeline for both Bicep and ARM templates. So this is probably the answer, although if you have any bicep sample code that would help.
To write a rule to check a deployment use the
Microsoft.Resources/deployments
type. This will allow you to evaluate a deployment prior to it being expanded, which may not be ideal but it would have a similar result as$PSRule.GetContent($TargetObject)[0]
.Some examples are here: https://github.com/Azure/PSRule.Rules.Azure/blob/main/src/PSRule.Rules.Azure/rules/Azure.Deployment.Rule.ps1
Alternatively you may be able to check for a sub-resource of the protected object. PSRule for Azure automatically nests sub-res…