使用 azuredevops 管道创建 APIM 管理器 API 策略

问题描述 投票:0回答:1

寻找一种在 Azure API 管理器中幂等地自动跨 API 和操作范围创建自定义策略的方法。

管道输入如下

stages:
  - template: api-ops-pipeline.yaml
    parameters:
      policyList:
      - name: rate_limit_ip
        scope: api
        apiname: test-policy, test2-policy

      - name: IPfilter
        scope: operation
        ipAddressesFrom: xxxxx
        ipAddressesTo: xxxxxxx
        operationname: getxxx, getyy, getzz

policy.xml 模板

<policies>
    <inbound>
        <base />
         $(rate-limit-by_ip)
         $(rate-limit-by_subkey)
         $(rate-limit-by_DevID)
         $(rate-limit-by_ip)+$(rate-limit-by_DevID)
    </inbound>
    <backend>
        <base />
    </backend>
    <outbound>
        <base />
    </outbound>
    <on-error>
        <base />
         $(rate-limit-by-ip_error)
         $(rate-limit-by_DevID_error)
         
    </on-error>
</policies>

作为第一个任务,我们必须根据应用程序团队提供的策略列表在不同的场景中创建自定义的initial_policy.xml文件,并且需要根据策略的范围生成它(可能特定于api或可能具体针对其中的单个操作或其中的多个操作)

我们可能有如下不同的组合,当范围仅限于操作时,应首先检查其父 api 或 APIM 实例级别本身是否应用了相同的策略。

组合

  • 按_ip 进行速率限制
  • 按子密钥进行速率限制
  • 速率限制-by_DevID
  • 按_ip 速率限制+按_DevID 速率限制
  • 或者任何带有错误属性的自定义组合

完成上述步骤后,在上面创建的自定义initial_policy.xml中,用户输入的参数将被替换(阈值、时间等),并且将创建final_policy.xml。

然后,最后它可以应用于给定的范围,(可能是 api/api 或操作/操作)

管道模板起草如下。

  jobs:
  - job: api
    displayName: 'api policy'
    variables:
    - group: api_policy
    workspace:
      clean: all
    pool:
      name: xxxxxxx
    steps:
    - ${{ each policy in parameters.policyList }}:
      - ${{ if and(eq(policy.name, 'rate_limit_ip'), eq(policy.scope, 'api') ) }}:
        - bash: |      
           apiName=${{ policy.Name }}
           echo "##vso[task.setvariable variable=apiName]$apiName" 
           xxxxxxxxxxxxxxxxx
           xxxxxxxxxxxxxxxxxxxxxxxx
            xxxxxxxxxxxxxxxxxxxxxx          
          name: Resolve_variable  

        - task: qetza.replacetokens.replacetokens-task.replacetokens@3
          displayName: 'create initial xml'
          inputs:
            rootDirectory: '$(System.DefaultWorkingDirectory)/policy'
            targetFiles: initial_policy.xml
            tokenPrefix: '${'
            tokenSuffix: '}$'
            enableTelemetry: false
          continueOnError: true

        - task: qetza.replacetokens.replacetokens-task.replacetokens@3
          displayName: 'create final xml'
          inputs:
            rootDirectory: '$(System.DefaultWorkingDirectory)/policy'
            targetFiles: final_policy.xml
            tokenPrefix: '${'
            tokenSuffix: '}$'
            enableTelemetry: false
          continueOnError: true
bash azure-devops azure-api-management azure-cli azure-pipelines-yaml
1个回答
0
投票

您可以在 Azure Git Repos 中创建并保存模板 XML,然后填写从参数传递的实际值。


如果 APIM 具有某些现有策略配置,则可以使用 Azure PowerShell cmdlet“Get-AzApiManagementPolicy”获取配置并将其保存到 XML 文件。在 Azure Pipelines 中,您可以像下面一样调用此 cmdlet。

  1. 创建 ARM 服务连接(Azure 资源管理器服务连接)以连接到 APIM 实例所在的 Azure 订阅和资源组。

  2. 在管道作业中添加

    AzurePowerShell@5
    任务,如下所示。

    - task: AzurePowerShell@5
      displayName: 'create initial xml'
      inputs:
        azureSubscription: '{ARM service connection}'
        ScriptType: InlineScript
        Inline: |
          $apimContext = New-AzApiManagementContext -ResourceGroupName "{ResourceGroupName}" -ServiceName "{ServiceName}"
          Get-AzApiManagementPolicy -Context $apimContext -ApiId "{ApiId}" -SaveAs "$(System.DefaultWorkingDirectory)\initial_policy.xml"
        azurePowerShellVersion: LatestVersion
        pwsh: true
    
    • {ARM service connection}
      :上面创建的ARM服务连接的名称。
    • {ResourceGroupName}
      :资源组的名称。名称不区分大小写。
    • {ServiceName}
      :API 管理服务的名称。
    • {ApiId}
      :API 修订标识符。

生成 XML 后,您可以使用从参数传递的新值来更新它。


© www.soinside.com 2019 - 2024. All rights reserved.