部署 XML 策略到 APIM 使用 Bicep。

huangapple go评论84阅读模式
英文:

deploying xml policy to apim using bicep

问题

  1. 我想将以下策略部署到Azure API管理:
  2. ```xml
  3. <policies>
  4. <inbound>
  5. <base />
  6. <choose>
  7. <when condition="@(!context.Variables.ContainsKey('cachedAccessToken') || DateTime.UtcNow >= (DateTime)context.Variables['tokenExpiry'])">
  8. <set-backend-service id="apim-generated-policy" backend-id="{0}" />
  9. <send-request mode="new" response-variable-name="tokenResponse" timeout="20" ignore-error="false">
  10. <set-url>{1}/token</set-url>
  11. <set-method>POST</set-method>
  12. <set-header name="Content-Type" exists-action="override">
  13. <value>application/x-www-form-urlencoded</value>
  14. </set-header>
  15. <set-body>@("grant_type=password&username={2}&password=thisShouldRefThe-AmxPassword-NamedValue")</set-body>
  16. </send-request>
  17. <set-variable name="tokenResponseJson" value="@{{ return JsonConvert.DeserializeObject((string)context.Variables['tokenResponse'].Body.As<string>()); }}" />
  18. <set-variable name="cachedAccessToken" value="@{{(string)context.Variables['tokenResponseJson']['access_token']}}" />
  19. <set-variable name="tokenExpiry" value="@{{ return DateTime.UtcNow.AddSeconds((int)context.Variables['tokenResponseJson']['expires_in'] - 60); }}" />
  20. <cache-store-value key="cachedAccessToken" value="@{{context.Variables['cachedAccessToken']}}" duration="@{{(new TimeSpan(0, (int)context.Variables['tokenResponseJson']['expires_in'], 0))}}" />
  21. <cache-store-value key="tokenExpiry" value="@{{context.Variables['tokenExpiry']}}" duration="@{{(new TimeSpan(0, (int)context.Variables['tokenResponseJson']['expires_in'], 0))}}" />
  22. </when>
  23. <otherwise>
  24. <cache-lookup-value key="cachedAccessToken" variable-name="cachedAccessToken" />
  25. <cache-lookup-value key="tokenExpiry" variable-name="tokenExpiry" />
  26. </otherwise>
  27. </choose>
  28. <set-header name="Authorization" exists-action="override">
  29. <value>@{{$"Bearer {{context.Variables['cachedAccessToken']}}}}</value>
  30. </set-header>
  31. </inbound>
  32. <backend>
  33. <base />
  34. </backend>
  35. <outbound>
  36. <base />
  37. </outbound>
  38. <on-error>
  39. <base />
  40. </on-error>
  41. </policies>

这是由DevOps管道运行的Bicep代码:

  1. resource apim 'Microsoft.ApiManagement/service@2022-04-01-preview' existing = {
  2. name: apimName
  3. resource apiVersionSet 'api-version-sets@2018-06-01-preview' = if (enableApiVersioning) {
  4. name: apiName
  5. properties: {
  6. displayName: displayName
  7. versioningScheme: versionScheme
  8. versionHeaderName: 'X-Api-Version'
  9. versionQueryName: 'api-version'
  10. }
  11. }
  12. resource api 'apis' = {
  13. name: enableApiVersioning ? apiNameVersioned : apiName
  14. properties: {
  15. displayName: displayName
  16. path: '${pathPrefix}${apiName}'
  17. format: definitionFormat
  18. protocols: [
  19. 'https'
  20. ]
  21. subscriptionRequired: true
  22. value: definition
  23. apiVersion: enableApiVersioning ? version : null
  24. apiVersionSetId: enableApiVersioning ? apiVersionSet.id : null
  25. subscriptionKeyParameterNames: {
  26. header: 'Ocp-Apim-Subscription-Key'
  27. query: 'subscription-key'
  28. }
  29. }
  30. resource apiPolicy 'policies' = if (!empty(policy)) {
  31. name: 'policy'
  32. properties: {
  33. value: policy
  34. }
  35. }
  36. }
  37. }

我收到以下错误:

'='是意外的标记。预期的标记是';'。第13行,位置58。

  1. <details>
  2. <summary>英文:</summary>
  3. I&#39;d like to deploy the following policy to azure api management:
  4. ````xml
  5. &lt;policies&gt;
  6. &lt;inbound&gt;
  7. &lt;base /&gt;
  8. &lt;choose&gt;
  9. &lt;when condition=&quot;@(!context.Variables.ContainsKey(&#39;&#39;cachedAccessToken&#39;&#39;) || DateTime.UtcNow &gt;= (DateTime)context.Variables[&#39;&#39;tokenExpiry&#39;&#39;])&quot;&gt;
  10. &lt;set-backend-service id=&quot;apim-generated-policy&quot; backend-id=&quot;{0}&quot; /&gt;
  11. &lt;send-request mode=&quot;new&quot; response-variable-name=&quot;tokenResponse&quot; timeout=&quot;20&quot; ignore-error=&quot;false&quot;&gt;
  12. &lt;set-url&gt;{1}/token&lt;/set-url&gt;
  13. &lt;set-method&gt;POST&lt;/set-method&gt;
  14. &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
  15. &lt;value&gt;application/x-www-form-urlencoded&lt;/value&gt;
  16. &lt;/set-header&gt;
  17. &lt;set-body&gt;@(&quot;grant_type=password&amp;username={2}&amp;password=thisShouldRefThe-AmxPassword-NamedValue&quot;)&lt;/set-body&gt;
  18. &lt;/send-request&gt;
  19. &lt;set-variable name=&quot;tokenResponseJson&quot; value=&quot;@{{ return JsonConvert.DeserializeObject((string)context.Variables[&#39;&#39;tokenResponse&#39;&#39;].Body.As&lt;string&gt;()); }}&quot; /&gt;
  20. &lt;set-variable name=&quot;cachedAccessToken&quot; value=&quot;@{{(string)context.Variables[&#39;&#39;tokenResponseJson&#39;&#39;][&#39;&#39;access_token&#39;&#39;]}}&quot; /&gt;
  21. &lt;set-variable name=&quot;tokenExpiry&quot; value=&quot;@{{ return DateTime.UtcNow.AddSeconds((int)context.Variables[&#39;&#39;tokenResponseJson&#39;&#39;][&#39;&#39;expires_in&#39;&#39;] - 60); }}&quot; /&gt;
  22. &lt;cache-store-value key=&quot;cachedAccessToken&quot; value=&quot;@{{context.Variables[&#39;&#39;cachedAccessToken&#39;&#39;]}}&quot; duration=&quot;@{{(new TimeSpan(0, (int)context.Variables[&#39;&#39;tokenResponseJson&#39;&#39;][&#39;&#39;expires_in&#39;&#39;], 0))}}&quot; /&gt;
  23. &lt;cache-store-value key=&quot;tokenExpiry&quot; value=&quot;@{{context.Variables[&#39;&#39;tokenExpiry&#39;&#39;]}}&quot; duration=&quot;@{{(new TimeSpan(0, (int)context.Variables[&#39;&#39;tokenResponseJson&#39;&#39;][&#39;&#39;expires_in&#39;&#39;], 0))}}&quot; /&gt;
  24. &lt;/when&gt;
  25. &lt;otherwise&gt;
  26. &lt;cache-lookup-value key=&quot;cachedAccessToken&quot; variable-name=&quot;cachedAccessToken&quot; /&gt;
  27. &lt;cache-lookup-value key=&quot;tokenExpiry&quot; variable-name=&quot;tokenExpiry&quot; /&gt;
  28. &lt;/otherwise&gt;
  29. &lt;/choose&gt;
  30. &lt;set-header name=&quot;Authorization&quot; exists-action=&quot;override&quot;&gt;
  31. &lt;value&gt;@{{$&quot;Bearer {{context.Variables[&#39;&#39;cachedAccessToken&#39;&#39;]}}&quot;}}&lt;/value&gt;
  32. &lt;/set-header&gt;
  33. &lt;/inbound&gt;
  34. &lt;backend&gt;
  35. &lt;base /&gt;
  36. &lt;/backend&gt;
  37. &lt;outbound&gt;
  38. &lt;base /&gt;
  39. &lt;/outbound&gt;
  40. &lt;on-error&gt;
  41. &lt;base /&gt;
  42. &lt;/on-error&gt;
  43. &lt;/policies&gt;

This is the bicep that gets run by a devops pipeline:

  1. resource apim &#39;Microsoft.ApiManagement/service@2022-04-01-preview&#39; existing = {
  2. name: apimName
  3. resource apiVersionSet &#39;api-version-sets@2018-06-01-preview&#39; = if (enableApiVersioning) {
  4. name: apiName
  5. properties: {
  6. displayName: displayName
  7. versioningScheme: versionScheme
  8. versionHeaderName: &#39;X-Api-Version&#39;
  9. versionQueryName: &#39;api-version&#39;
  10. }
  11. }
  12. resource api &#39;apis&#39; = {
  13. name: enableApiVersioning ? apiNameVersioned : apiName
  14. properties: {
  15. displayName: displayName
  16. path: &#39;${pathPrefix}${apiName}&#39;
  17. format: definitionFormat
  18. protocols: [
  19. &#39;https&#39;
  20. ]
  21. subscriptionRequired: true
  22. value: definition
  23. apiVersion: enableApiVersioning ? version : null
  24. apiVersionSetId: enableApiVersioning ? apiVersionSet.id : null
  25. subscriptionKeyParameterNames: {
  26. header: &#39;Ocp-Apim-Subscription-Key&#39;
  27. query: &#39;subscription-key&#39;
  28. }
  29. }
  30. resource apiPolicy &#39;policies&#39; = if (!empty(policy)) {
  31. name: &#39;policy&#39;
  32. properties: {
  33. value: policy
  34. }
  35. }
  36. }
  37. }

I get the following error:

> '=' is an unexpected token. The expected token is ';'. Line 13, position 58.

答案1

得分: 1

以下是您要翻译的内容:

"Lesson learned - pay more attention to what chatgpt pumps out!

The main problem was the syntax for the C# code block. Here's the working policy:

  1. &lt;policies&gt;
  2. &lt;inbound&gt;
  3. &lt;base /&gt;
  4. &lt;choose&gt;
  5. &lt;when condition=&quot;@(!context.Variables.ContainsKey(&quot;cachedAccessToken&quot;) || DateTime.UtcNow &gt;= (DateTime)context.Variables[&quot;tokenExpiry&quot;])&quot;&gt;
  6. &lt;set-backend-service id=&quot;apim-generated-policy&quot; backend-id=&quot;func-amx-api-dev-001&quot; /&gt;
  7. &lt;send-request mode=&quot;new&quot; response-variable-name=&quot;tokenResponse&quot; timeout=&quot;20&quot; ignore-error=&quot;false&quot;&gt;
  8. &lt;set-url&gt;{{baseurl}}.azurewebsites.net/token&lt;/set-url&gt;
  9. &lt;set-method&gt;POST&lt;/set-method&gt;
  10. &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
  11. &lt;value&gt;application/x-www-form-urlencoded&lt;/value&gt;
  12. &lt;/set-header&gt;
  13. &lt;set-body&gt;@(&quot;grant_type=password&amp;username={{user}}&amp;password={{AmxPassword}}&quot;)&lt;/set-body&gt;
  14. &lt;/send-request&gt;
  15. &lt;set-variable name=&quot;cachedAccessToken&quot; value=&quot;@((String)((IResponse)context.Variables[&quot;jwt&quot;]).Body.As&lt;JObject&gt;()[&quot;access_token&quot;])&quot; /&gt;
  16. &lt;set-variable name=&quot;tokenExpiry&quot; value=&quot;@((String)((IResponse)context.Variables[&quot;jwt&quot;]).Body.As&lt;JObject&gt;()[&quot;expires_in&quot;])&quot; /&gt;
  17. &lt;cache-store-value key=&quot;cachedAccessToken&quot; value=&quot;@((String)context.Variables[&quot;cachedAccessToken&quot;])&quot; duration=&quot;3600&quot; caching-type=&quot;internal&quot; /&gt;
  18. &lt;cache-store-value key=&quot;tokenExpiry&quot; value=&quot;@((String)context.Variables[&quot;tokenExpiry&quot;])&quot; duration=&quot;3600&quot; caching-type=&quot;internal&quot; /&gt;
  19. &lt;/when&gt;
  20. &lt;otherwise&gt;
  21. &lt;cache-lookup-value key=&quot;cachedAccessToken&quot; variable-name=&quot;cachedAccessToken&quot; /&gt;
  22. &lt;cache-lookup-value key=&quot;tokenExpiry&quot; variable-name=&quot;tokenExpiry&quot; /&gt;
  23. &lt;/otherwise&gt;
  24. &lt;/choose&gt;
  25. &lt;set-header name=&quot;Authorization&quot; exists-action=&quot;override&quot;&gt;
  26. &lt;value&gt;@{
  27. return $&quot;Bearer {(String)context.Variables[&quot;cachedAccessToken&quot;]}&quot;;
  28. }&lt;/value&gt;
  29. &lt;/set-header&gt;
  30. &lt;/inbound&gt;
  31. &lt;backend&gt;
  32. &lt;base /&gt;
  33. &lt;/backend&gt;
  34. &lt;outbound&gt;
  35. &lt;base /&gt;
  36. &lt;/outbound&gt;
  37. &lt;on-error&gt;
  38. &lt;base /&gt;
  39. &lt;/on-error&gt;
  40. &lt;/policies&gt;

I also moved the policy to a separate file and deployed at apim operation level with the following bicep:

  1. resource apiPolicy &#39;Microsoft.ApiManagement/service/apis/operations/policies@2022-09-01-preview&#39; = {
  2. name: &#39;${apimName}/amxapi_v1_0_0/entity/policy&#39;
  3. properties: {
  4. value: loadTextContent(&#39;./createEntityPolicy.xml&#39;)
  5. }
  6. }
英文:

Lesson learned - pay more attention to what chatgpt pumps out!

The main problem was the syntax for the C# code block. Here's the working policy:

  1. &lt;policies&gt;
  2. &lt;inbound&gt;
  3. &lt;base /&gt;
  4. &lt;choose&gt;
  5. &lt;when condition=&quot;@(!context.Variables.ContainsKey(&quot;cachedAccessToken&quot;) || DateTime.UtcNow &gt;= (DateTime)context.Variables[&quot;tokenExpiry&quot;])&quot;&gt;
  6. &lt;set-backend-service id=&quot;apim-generated-policy&quot; backend-id=&quot;func-amx-api-dev-001&quot; /&gt;
  7. &lt;send-request mode=&quot;new&quot; response-variable-name=&quot;tokenResponse&quot; timeout=&quot;20&quot; ignore-error=&quot;false&quot;&gt;
  8. &lt;set-url&gt;{{baseurl}}.azurewebsites.net/token&lt;/set-url&gt;
  9. &lt;set-method&gt;POST&lt;/set-method&gt;
  10. &lt;set-header name=&quot;Content-Type&quot; exists-action=&quot;override&quot;&gt;
  11. &lt;value&gt;application/x-www-form-urlencoded&lt;/value&gt;
  12. &lt;/set-header&gt;
  13. &lt;set-body&gt;@(&quot;grant_type=password&amp;username={{user}}&amp;password={{AmxPassword}}&quot;)&lt;/set-body&gt;
  14. &lt;/send-request&gt;
  15. &lt;set-variable name=&quot;cachedAccessToken&quot; value=&quot;@((String)((IResponse)context.Variables[&quot;jwt&quot;]).Body.As&lt;JObject&gt;()[&quot;access_token&quot;])&quot; /&gt;
  16. &lt;set-variable name=&quot;tokenExpiry&quot; value=&quot;@((String)((IResponse)context.Variables[&quot;jwt&quot;]).Body.As&lt;JObject&gt;()[&quot;expires_in&quot;])&quot; /&gt;
  17. &lt;cache-store-value key=&quot;cachedAccessToken&quot; value=&quot;@((String)context.Variables[&quot;cachedAccessToken&quot;])&quot; duration=&quot;3600&quot; caching-type=&quot;internal&quot; /&gt;
  18. &lt;cache-store-value key=&quot;tokenExpiry&quot; value=&quot;@((String)context.Variables[&quot;tokenExpiry&quot;])&quot; duration=&quot;3600&quot; caching-type=&quot;internal&quot; /&gt;
  19. &lt;/when&gt;
  20. &lt;otherwise&gt;
  21. &lt;cache-lookup-value key=&quot;cachedAccessToken&quot; variable-name=&quot;cachedAccessToken&quot; /&gt;
  22. &lt;cache-lookup-value key=&quot;tokenExpiry&quot; variable-name=&quot;tokenExpiry&quot; /&gt;
  23. &lt;/otherwise&gt;
  24. &lt;/choose&gt;
  25. &lt;set-header name=&quot;Authorization&quot; exists-action=&quot;override&quot;&gt;
  26. &lt;value&gt;@{
  27. return $&quot;Bearer {(String)context.Variables[&quot;cachedAccessToken&quot;]}&quot;;
  28. }&lt;/value&gt;
  29. &lt;/set-header&gt;
  30. &lt;/inbound&gt;
  31. &lt;backend&gt;
  32. &lt;base /&gt;
  33. &lt;/backend&gt;
  34. &lt;outbound&gt;
  35. &lt;base /&gt;
  36. &lt;/outbound&gt;
  37. &lt;on-error&gt;
  38. &lt;base /&gt;
  39. &lt;/on-error&gt;
  40. &lt;/policies&gt;

I also moved the policy to a separate file and deployed at apim operation level with the following bicep:

  1. resource apiPolicy &#39;Microsoft.ApiManagement/service/apis/operations/policies@2022-09-01-preview&#39; = {
  2. name: &#39;${apimName}/amxapi_v1_0_0/entity/policy&#39;
  3. properties: {
  4. value: loadTextContent(&#39;./createEntityPolicy.xml&#39;)
  5. }
  6. }

huangapple
  • 本文由 发表于 2023年5月6日 22:54:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定