-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for reading XML files #1537
Comments
Thanks for raising your first issue, the team appreciates the time you have taken 😉 |
@ronaldbosma Thanks for raising the issue. We'd like to improve XML support too since we found a few things that could be better such as #1518. Currently you can use PowerShell based rules to work with XML by using a convention. Similar to this https://github.com/microsoft/PSRule-samples/tree/main/samples/azure/BicepModuleRequires For example: # Synopsis: Imports XML policy in for analysis.
Export-PSRuleConvention 'APIMPolicy.Import' -Initialize {
$policies = @(Get-ChildItem -Path 'policies/' -Include '*.xml' -Recurse -File | ForEach-Object {
$name = $_.Name
[PSCustomObject]@{
Name = $name
Content = [Xml](Get-Content -Path $_.FullName -Raw)
}
})
$PSRule.ImportWithType('APIMPolicy', $policies);
}
# Synopsis: Your rule
Rule 'Rule001' -Type 'APIMPolicy' {
$policy = $TargetObject.Content
} See conventions |
Hi @BernieWhite. Thanks for answering and for your suggestion to use a convention. I was working a workaround which had several issues, but using a convention that includes the file name and importing them as custom types fixed all of them. |
I would like to be able to read XML files so I can create custom rules to check the XML. In particular, I want to check my Azure API Management policies. We define our policies in separate XML files, so I would like to be able to load these and check them with PSRule.
An extra XML option for Input.Format would most likely be the best approach. Handling both elements & attributes might be a bit tricky, but both should be supported. Also note that elements with the same name can occur multiple times, while attribute names should be unique for a given element.
Alternatives I looked at:
The PSRule.Rules.Azure suite provides the Azure.APIM.PolicyBase rule that asserts the contents of a policy. This rule expects the policy XML to be defined directly in the Bicep file. We use separate XML files for our policies because these are more readable and maintainable. So, the approach used for
Azure.APIM.PolicyBase
doesn't work for us.I've tried converting the Bicep into an JSON ARM template and then using a similar approach as is done by
Azure.APIM.PolicyBase
, but the policy contents is set via a generated variable as you can see in the sample below. Most likely because we use theloadTextContent
Bicep function when loading the policy XML file.The text was updated successfully, but these errors were encountered: