Is there a way to "Audit If Exists" for a related resource instead of AuditIfNotExists?
I need to audit if a related resource exists however I only see how to audit if related resources do not exist ("AuditIfNotExists).
Is there a method recommended for this? Is there an operator I missed for the effect details? I am hopeful there is a solution as simple as a "not" operator out there but a "AuditIfExists" would be a nice "effect" option.
For example I need to audit if the "Microsoft.ApiManagement/service" has a related resource of "Microsoft.ApiManagement/service/openidConnectProviders".
I don't believe the type "Microsoft.ApiManagement/service/openidConnectProviders" actually exists as a resource and as such I cannot audit for its existence in "if.field.type" and I am dependent on the "then.details.type" however this does not audit if something does exist. If I create a scenario where the existence criteria ("then.details.ExistenceCondition") does not exist I can get details about the type ("then.details.type") however this is the opposite audit of what I need.
Below is an example of an "AuditIfNotExists" but I need the inverse. Something like an "AuditIfExists":
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.ApiManagement/service"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.ApiManagement/service/openidConnectProviders",
"existenceCondition": {
"allOf": [
{
"field": "Microsoft.ApiManagement/service/openidConnectProviders/displayName",
"equals": "Skittles"
}
]
}
}
}
Any guidance on how to accomplish this would be helpful.
You can use "not" to reverse the existence condition logic. This policy should show non-compliance if "Microsoft.ApiManagement/service/openidConnectProviders/displayName is set to "Skittles", but show as compliant if it doesn't exist or is set to something else. Would that work for you?
{
"policyRule":
{
"if": {
"field": "type",
"equals": "Microsoft.ApiManagement/service"
},
"then": {
"effect": "auditIfNotExists",
"details": {
"type": "Microsoft.ApiManagement/service/openidConnectProviders",
"existenceCondition": {
"not": {
"field": "Microsoft.ApiManagement/service/openidConnectProviders/displayName",
"equals": "Skittles"
}
}
}
}
}
}
The existenceCondition piece is mostly irrelevant and I should have left it out of the policyRule I copied and pasted from one of my test definitions.
The real problem is the type in this case. If the "Microsoft.ApiManagement/service/openidConnectProviders" resourceType exists we want it to audit as non-compliant.
Currently any ApiManagement resourceType ("Microsoft.ApiManagement/service") without an openidConnectProviders resourceType ("Microsoft.ApiManagement/service/openidConnectProviders") is marked as non-compliant. We actually need to audit for the existence of an openidConnectProviders resourceType.
Below is an example of the compliance detail we see for a resource that doesn't have the openidConnectProviders resourceType. We want this resource to be compliant and ones that do have a related resourceType of openidConnectProviders to be non-compliant:
Reason for non-compliance
No related resources match the effect details in the policy definition.
EXISTENCE CONDITION
Type
Microsoft.ApiManagement/service/openidConnectProviders
Below is the PolicyRule that we need to reverse:
"policyRule": {
"if": {
"allOf": [
{
"field": "type",
"equals": "Microsoft.ApiManagement/service"
}
]
},
"then": {
"effect": "[parameters('effect')]",
"details": {
"type": "Microsoft.ApiManagement/service/openidConnectProviders"
}
}
}
@mylameaccount You should be able to use "audit" on the "Microsoft.ApiManagement/service/openidConnectProviders" type. Have you tried that?
Closing this issue per changes to the support model for this repo. See the Getting Support section in the readme for details and Azure Policy support information.