Skip to content
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

[BUG] APIM policies rules parsing policies with C# code fail to convert deployable XML into type System.XML.XmlDocument #3184

Open
GABRIELNGBTUC opened this issue Nov 22, 2024 · 1 comment
Labels
bug Something isn't working rule: api-management Rules for API Management

Comments

@GABRIELNGBTUC
Copy link

GABRIELNGBTUC commented Nov 22, 2024

Existing rule

Azure.APIM.CORSPolicy and Azure.APIM.PolicyBase

Description of the issue

When running PSRule against APIM policies that sucessfully deploys on Azure, the rule fails due to being unable to convert the XML of the policy

Error messages


##[error]AZR-000371: apim-name/api-name failed Azure.APIM.PolicyBase with the error 'Cannot convert value "<!-- Add policies as children to the <inbound>, <outbound>, <backend>, and <on-error> elements -->
<policies>
	<!-- Throttle, authorize, validate, cache, or transform the requests -->
	<inbound>
		<base />
		<set-backend-service base-url="{{backend}}" />
		<authentication-managed-identity resource="{{audiencee}}" />
		<set-variable name="originalHost" value="@(context.Rquest.Headers.GetValueOrDefault("X-Original-Host", "NotAvailable"))" />
	</inbound>
	<!-- Control if and how the requests are forwarded to services  -->
	<backend>
		<base />
	</backend>
	<!-- Customize the responses -->
	<outbound>
		<base />
	</outbound>
	<!-- Handle exceptions and customize error responses  -->
	<on-error>
		<base />
	</on-error>
</policies>" to type "System.Xml.XmlDocument". Error: "'X-Original-Host' is an unexpected token. Expecting whitespace. Line 15, position 89."'.

At /usr/local/share/powershell/Modules/PSRule.Rules.Azure/1.39.3/rules/Azure.APIM.Rule.ps1:384 char:21
+         $policies | ForEach-Object {
+                     ~~~~~~~~~~~~~~~~

at <ScriptBlock>, /usr/local/share/powershell/Modules/PSRule.Rules.Azure/1.39.3/rules/Azure.APIM.Rule.ps1: line 386
at global:GetAPIMPolicyNode<Process>, /usr/local/share/powershell/Modules/PSRule.Rules.Azure/1.39.3/rules/Azure.APIM.Rule.ps1: line 384
at <ScriptBlock>, /usr/local/share/powershell/Modules/PSRule.Rules.Azure/1.39.3/rules/Azure.APIM.Rule.ps1: line 326
at Rule 'Azure.APIM.PolicyBase', /usr/local/share/powershell/Modules/PSRule.Rules.Azure/1.39.3/rules/Azure.APIM.Rule.ps1: line 325


Reproduction

Try to run PSRule against a bicep template that deploys the following policy for an API with rawxml as the format

<!--
    - Policies are applied in the order they appear.
    - Position <base/> inside a section to inherit policies from the outer scope.
    - Comments within policies are not preserved.
-->
<!-- Add policies as children to the <inbound>, <outbound>, <backend>, and <on-error> elements -->
<policies>
	<!-- Throttle, authorize, validate, cache, or transform the requests -->
	<inbound>
		<base />
		<set-backend-service base-url="{{backend}}" />
		<authentication-managed-identity resource="{{audiencee}}" />
		<set-variable name="originalHost" value="@(context.Rquest.Headers.GetValueOrDefault("X-Original-Host", "NotAvailable"))" />
	</inbound>
	<!-- Control if and how the requests are forwarded to services  -->
	<backend>
		<base />
	</backend>
	<!-- Customize the responses -->
	<outbound>
		<base />
	</outbound>
	<!-- Handle exceptions and customize error responses  -->
	<on-error>
		<base />
	</on-error>
</policies>

The rule will fails but a deployment of the template to Azure will succeed

Version of PSRule

2.9.0

Version of PSRule for Azure

1.39.3

Additional context

No response

@GABRIELNGBTUC GABRIELNGBTUC added bug Something isn't working Needs: Triage 🔍 Needs attention from the team. labels Nov 22, 2024
@GABRIELNGBTUC GABRIELNGBTUC changed the title [BUG] APIM policies with C# code rules fail to convert valid XML into type System.XML.XmlDocument [BUG] APIM policies rules parsing policies with C# code fail to convert valid XML into type System.XML.XmlDocument Nov 22, 2024
@GABRIELNGBTUC GABRIELNGBTUC changed the title [BUG] APIM policies rules parsing policies with C# code fail to convert valid XML into type System.XML.XmlDocument [BUG] APIM policies rules parsing policies with C# code fail to convert deployable XML into type System.XML.XmlDocument Nov 22, 2024
@BernieWhite
Copy link
Collaborator

Thanks for reporting the issue @GABRIELNGBTUC.

@BernieWhite BernieWhite added rule: api-management Rules for API Management and removed Needs: Triage 🔍 Needs attention from the team. labels Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working rule: api-management Rules for API Management
Projects
None yet
Development

No branches or pull requests

2 participants