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<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;
}" />
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?