-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Description
Library name and version
Azure.ResourceManager.PolicyInsights 1.0.0
Describe the bug
When loading the PolicyState for anything, if the "policySetDefinitionId" is null, it throws an error saying the resourceId is null. This field isn't required as a policy might not have a definition associated with it.
I believe the issue is that the type is a ResourceIdentifier and that object throws an error is the string is null or empty
Expected behavior
PolicyState is able to be loaded if policySetDefinitionId is empty
Actual behavior
System.ArgumentException: 'Value cannot be an empty string. (Parameter 'resourceId')' is thrown when the policySetDefinitionId is null.
In PolicyState.Serialization.cs (194), it passes an empty string to the contructor of the ResourceIdentifier object.
Here is the constructor for ResourceIdentifer
public ResourceIdentifier(string resourceId)
{
Argument.AssertNotNullOrEmpty(resourceId, nameof(resourceId));
_stringValue = resourceId;
if (resourceId.Length == 1 && resourceId[0] == Separator)
{
//this is the same as Root but can't return that since this is a ctor
Init(null, ResourceType.Tenant, string.Empty, false, SpecialType.None);
return;
}
}
I can't figure out what needs to change to made this work. Obviously a if(!string.IsNullOrEmpty(property.Value.GetString())) check in PolicyState.Serialization.cs before loading the ResourceIdentifier would work, but I don't know how to handle this with Autorest.
Reproduction Steps
Here is one of my payloads that is throwing the error with personal info removed:
{
"@odata.id": null,
"@odata.context": "https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroup}/providers/Microsoft.PolicyInsights/policyStates/$metadata#latest/$entity",
"timestamp": "2022-10-06T03:15:53.8325601Z",
"resourceId": "/subscriptions/{subscriptionId}/resourcegroups/{resourceGroup}/providers/microsoft.web/sites/{someSiteName}",
"policyAssignmentId": "/providers/microsoft.management/managementgroups/{guid}/providers/microsoft.authorization/policyassignments/102",
"policyDefinitionId": "/providers/microsoft.management/managementgroups/{guid}/providers/microsoft.authorization/policydefinitions/102",
"effectiveParameters": "",
"isCompliant": true,
"subscriptionId": "{subscriptionId}",
"resourceType": "Microsoft.Web/sites",
"resourceLocation": "eastus2",
"resourceGroup": "{resourceGroup}",
"resourceTags": "tbd",
"policyAssignmentName": "102",
"policyAssignmentOwner": "tbd",
"policyAssignmentParameters": "",
"policyAssignmentScope": "/providers/Microsoft.Management/managementgroups/cfa5342f-a686-4a51-9d41-3736c6ba8c2e",
"policyDefinitionName": "102",
"policyDefinitionAction": "auditifnotexists",
"policyDefinitionCategory": "tbd",
"policySetDefinitionId": "",
"policySetDefinitionName": "",
"policySetDefinitionOwner": "",
"policySetDefinitionCategory": "",
"policySetDefinitionParameters": "",
"managementGroupIds": "{guids}",
"policyDefinitionReferenceId": "",
"complianceState": "Compliant",
"policyDefinitionGroupNames": [ "" ],
"policyDefinitionVersion": "1.0.0",
"policySetDefinitionVersion": "",
"policyAssignmentVersion": "",
"complianceReasonCode": ""
}
Try to deserialize that and it will throw the error.
Environment
I'm running this on Windows 11 with .NET version 6.0.9 and Visual Studio 2022