Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

missing '}' in multiline policy expression

I am attempting to deploy an APIM policy file through an ARM template and am getting the following error:

Error in element 'set-variable' on line 24, column 6: The code block is missing a closing \\"}\\" character. Make sure you have a matching \\"}\\" character for all the \\"{\\" characters within this block, and that none of the \\"}\\" characters are being interpreted as markup.

I initially created this policy through the APIM management blade in the Azure portal and the policy in question looks like this there:

<set-variable name="digitalSignature" value="@{
  string privateKey = context.Variables.GetValueOrDefault<string>("privateKey", "");
  Encoding encoding = System.Text.Encoding.ASCII;
  string usablePrivateKey = privateKey.Replace("-", "+").Replace("_", "/");
  byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);
  byte[] encodedPathAndQueryBytes = encoding.GetBytes(context.Request.Url.Path + context.Request.Url.QueryString);
  HMACSHA1 hashAlgorithm = new HMACSHA1(privateKeyBytes);
  byte[] hash = hashAlgorithm.ComputeHash(encodedPathAndQueryBytes);
  string digitalSignature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");

  return digitalSignature;
}" />

However, the expression contains a number of characters that are not valid XML so I have escaped the above code as follows in the *.policy.xml file:

<set-variable name="digitalSignature" value="@{
  string privateKey = context.Variables.GetValueOrDefault&lt;string&gt;(&quot;privateKey&quot;, &quot;&quot;);
  Encoding encoding = System.Text.Encoding.ASCII;
  string usablePrivateKey = privateKey.Replace(&quot;-&quot;, &quot;+&quot;).Replace(&quot;_&quot;, &quot;/&quot;);
  byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);
  byte[] encodedPathAndQueryBytes = encoding.GetBytes(context.Request.Url.Path + context.Request.Url.QueryString);
  HMACSHA1 hashAlgorithm = new HMACSHA1(privateKeyBytes);
  byte[] hash = hashAlgorithm.ComputeHash(encodedPathAndQueryBytes);
  string digitalSignature = Convert.ToBase64String(hash).Replace(&quot;+&quot;, &quot;-&quot;).Replace(&quot;/&quot;, &quot;_&quot;);

  return digitalSignature;
}" />

What am I missing? Do I even need to escape the characters since the policy XML files in the GIT repo backing the APIM instance are not escaped?

like image 240
KyKo Avatar asked Oct 15 '25 13:10

KyKo


1 Answers

While making changes trying to get this to work I removed all of the comments in the code expression (which was not posted in this question) to remove anything not explicitly necessary and that appears to have solved the issue.

Looking at the comments I cannot see what would have caused the issue as all special characters were escaped (and verified with several different XML escaping utilities), but since the comments were not required and this is working I consider the issue closed.

like image 176
KyKo Avatar answered Oct 19 '25 14:10

KyKo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!