繁体   English   中英

如何通过 Azure DevOps 发布管道中的 Azure Powershell 更新暂存槽的 IP 白名单?

[英]How do I update the IP whitelist for a staging slot via Azure Powershell from an Azure DevOps Release Pipeline?

我有一个托管在 Azure 中的应用程序,我使用 Azure DevOps 来管理我的构建和发布管道。 作为发布的一部分,我通过向根 URL(例如https://myapp.azurewebsites.net )发出请求来预热应用程序。 为了发出此请求,我必须首先确保运行部署的托管构建代理可以访问该 url(否则我将收到 403)。 我编写了一个简短的 powershell 脚本来实现这一点,并将其放入 Azure Powershell 任务中。 它将构建代理的 IP 添加到应用服务的 IpSecurityConfiguration。 到现在为止还挺好。 它非常适合只是应用程序的应用程序。 它失败的地方是当我尝试在登台环境中使用它时。 当我们发布到生产环境时,我们首先将代码推送到暂存槽,然后在我们运行测试并确保一切正常后将其翻转过来。 正确处理应用服务的 IpSecurityConfiguration 的 powershell 脚本在暂存槽上不起作用。 要访问暂存槽,我们使用 myappname/slots/staging 作为变量 $(WebApiName),通常它只是应用服务本身的名称。 同样,如果我从本地环境运行脚本,这将非常有效,它只会在管道中失败。 代码如下:

# Whitelist Azure Agent IPs

$agentIP = Invoke-RestMethod http://ipinfo.io/json | Select -exp ip

Write-Host "Connecting to Azure"

$APIVersion = ((Get-AzureRmResourceProvider -ProviderNamespace Microsoft.Web).ResourceTypes | Where-Object ResourceTypeName -eq sites).ApiVersions[0]
Write-Host "API Version is $APIVersion. Getting web app config for $(WebApiName) in $(ResourceGroupName)"

$WebApiConfig = (Get-AzureRmResource -ResourceType Microsoft.Web/sites/config -ResourceName $(WebApiName) -ResourceGroupName $(ResourceGroupName) -ApiVersion $APIVersion)


Write-Host "Got web app config: $WebApiConfig" 

$webIP = [PSCustomObject]@{
                          ipAddress = "$agentIP/32";
                          action = "Allow";
                          tag = 'Default';
                          priority = 300;
                          name = $agentIP.ToString();
                          description = $agentIP.ToString() 
                                }
Write-Host "Adding $agentIP to security restrictions"

$WebApiConfig.Properties.ipSecurityRestrictions += $webIP
Write-Host "Updating security restrictions"
# update app restrictions, do not prompt for confirmation
$result = Set-AzureRmResource -ResourceId $WebApiConfig.ResourceId -Properties $WebApiConfig.Properties -ApiVersion $APIVersion -Force

为了使水有些混浊,我可以通过更改获得完全相同的代码以与本地暂存槽完美配合

$WebApiConfig = (Get-AzureRmResource -ResourceType Microsoft.Web/sites/config -ResourceName $(WebApiName) -ResourceGroupName $(ResourceGroupName) -ApiVersion $APIVersion)

$WebApiConfig = (Get-AzureRmResource -ResourceType Microsoft.Web/sites -ResourceName $(WebApiName)/config -ResourceGroupName $(ResourceGroupName) -ApiVersion $APIVersion)

但这在 Azure Powershell 任务中不起作用。 相反,我无法部署到任何环境,因为在尝试访问 $WebApiConfig 对象上的 IpSecurityRestrictions 时任务失败。 例外是“ Exception setting "ipSecurityRestrictions": "The property 'ipSecurityRestrictions' cannot be found on this object. Verify that the property exists and can be set. Exception setting "ipSecurityRestrictions": "The property 'ipSecurityRestrictions' cannot be found on this object. Verify that the property exists and can be set.

正如我之前所说,如果我在本地完全以这种形式运行脚本,它就可以完美运行。 显然,我必须手动替换来自构建管道的变量,否则在本地机器上完全按照我希望的方式工作的代码与发布中失败的代码之间没有区别。 您可以通过将 $(WebApiName) 替换为有效的应用程序服务名称并将 $(ResourceGroupName) 替换为应用程序服务所在的资源组来验证这一点。是的,在我的本地机器上,我看到了一个有效的对象,而在任务的输出中,我什么也没得到。 该行只是说“有网络应用程序配置:”

有人有任何想法吗?

  • 我尝试更改任务使用的 powershell 版本以匹配我拥有的版本。
  • 我已经尝试使用任务的预览版(v4,否则我一直在使用 v3)。
  • 在调用 Get-AzureRmResource 时,我已经尝试了所有我能想到的 /sites/config 排列(因为它允许它在插槽上本地工作)。

以防万一有人想知道最后一件事。 我这样做而不是将 Microsoft 列表 ( https://www.microsoft.com/en-us/download/confirmation.aspx?id=41653 ) 中的所有 IP 列入白名单,原因有两个,首先它要容易得多维护我们自己的 IP 的简短列表,其次,Azure 处理这些 CIDR 定义的方式似乎存在错误,因为即使我们将整个文件列入白名单,在我们的部署期间,这些范围内的 IP 也经常被阻止。 这样我只需将当前正在动态使用的 IP 添加到白名单,并在我们完成后将其删除。 假设我可以让它工作......

终于想出了解决这个问题的办法。 为了使用插槽,资源类型必须略有不同。 此行适用于 Azure Powershell 任务:

$WebApiConfig = (Get-AzureRmResource -ResourceType Microsoft.Web/sites/slots/config -ResourceName $(WebApiName) -ResourceGroupName $(ResourceGroupName) -ApiVersion $APIVersion)

发布以防它可以帮助其他有同样问题的人。 我可以确认,我采用的方法在通过构建代理管理对 Azure 站点的访问方面非常有效,并且可以省去大量使用 Microsoft 的构建代理 xml 文件的麻烦。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM