【发布时间】:2018-11-22 00:41:07
【问题描述】:
有没有办法获取正在运行的托管机器的 IP 地址范围?
这与发布管道 -> 托管代理有关。
问题:连接时访问被拒绝,因为连接被防火墙拒绝。需要将此来自 DevOps 发布管道的请求的 IP 地址范围列入白名单。
【问题讨论】:
标签: azure-devops azure-pipelines
有没有办法获取正在运行的托管机器的 IP 地址范围?
这与发布管道 -> 托管代理有关。
问题:连接时访问被拒绝,因为连接被防火墙拒绝。需要将此来自 DevOps 发布管道的请求的 IP 地址范围列入白名单。
【问题讨论】:
标签: azure-devops azure-pipelines
我有一个步骤在 powershell 中获取托管代理 IP 地址:
Invoke-RestMethod http://ipinfo.io/json | Select -exp ip
希望有帮助。
【讨论】:
curl -s http://ipinfo.io/json | jq '.ip',然后您可以将 IP 地址与microsoft.com/en-nz/download/details.aspx?id=41653 的 XML 文件中的 CIDR 范围进行比较(请注意,它每周都会更改,如另一个答案中所述)。有谁知道 AzureDevops 上是否有配置/设置页面可以在不运行命令的情况下了解这些信息?
0.0.0.0 的防火墙规则(参见docs.microsoft.com/en-us/rest/api/sql/firewallrules/…)。这是 Azure 领域中的某种特殊通配符,以避免由于我提到的 XML 列表而更新 IP。在我的场景中,我的带有托管 VM 的 Azure DevOps 管道应该在 Azure 上创建/访问 SQL Server 实例。在 SQL Server 定义中添加防火墙规则时,一切正常
在管道中使用脚本步骤获取当前外部 ip 并将其列入白名单。管道完成后使用另一个脚本步骤进行清理。
不幸的是,这是唯一的方法(对于托管代理)。
【讨论】:
查看 Azure DevOps 的此插件 (https://marketplace.visualstudio.com/items?itemName=MartijnQuekel.AzureAppServiceIPRestrictions)。它允许您在构建管道期间更改应用服务 IP 限制。
【讨论】:
我们需要在下面提到的列表中将 Azure 数据中心使用的 IP 地址列入白名单: https://www.microsoft.com/en-nz/download/details.aspx?id=41653
注意:此列表每周都会更新,因此请在部署计划时注意这一点
【讨论】:
0.0.0.0 的变通方法,如下所述:docs.microsoft.com/en-us/rest/api/sql/firewallrules/… this这样你就不需要根据每周从microsoft.com/en-nz/download/details.aspx?id=41653下载的XML来更新IP列表了
池每周都会从 Microsoft 获得更新。因此,无法维护。
针对这个问题,我创建了一个新的 Bash 任务。该任务从代理获取当前 IP,并将其列入 AWS 安全组白名单。在完成管道之前,我已经撤销了上述IP。
Current_IP=$(curl ipinfo.io/ip)
echo $Current_IP
# Authorize access
aws ec2 authorize-security-group-ingress \
--group-id sg-xxxxxxxx \
--protocol tcp \
--port 9000 \
--cidr $Current_IP/32
# Revoke access
aws ec2 revoke-security-group-ingress \
--group-id sg-xxxxxxx \
--protocol tcp \
--port 9000 \
--cidr $Current_IP/32
为了使 aws-cli 命令正常工作,我们还需要一个受限的 aws 策略,以允许上述命令以受限的访问权限运行。
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:RevokeSecurityGroupIngress",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:ModifySecurityGroupRules"
],
"Resource": "arn:aws:ec2:us-east-2:xxxxxx:security-group/sg-xxxxx"
}
]
}
【讨论】:
我同意@4c74356b41 提到的内容。
我一直在使用以下解决方案将 Azure DevOps IP 地址添加到 Azure Kubernetes 服务的授权 IP 范围。您可以修改此解决方案以将其用于任何 Azure 服务。
您需要添加两个“Azure Cli”任务 - 一个用于添加 Azure DevOps 代理 IP 地址,另一个用于删除 Azure DevOps 代理 IP 地址。
在 Kubernetes 任务之前添加第一个任务:
我使用“Azure Cli”在 Azure DevOps 管道中添加了一个新任务,并将以下命令添加为内联脚本:
echo "Get Azure DevOps IP address"
azdoip=`curl -s icanhazip.com`
echo "Azure DevOps IP Address: $azdoip"
echo "Set Azure Subscription to MYSUBSCRIPTION"
az account set --subscription "MYSUBSCRIPTION"
echo "Get credentials for AKS Cluster Admin"
az aks get-credentials --resource-group MYAKSRG --name MYAKSCLUSTER --admin --file ~/.kube/config
Echo "Get existing authorized ip ranges"
authorizedips=`az aks show --resource-group MYAKSRG --name MYAKSCLUSTER --query apiServerAccessProfile |jq -r '.authorizedIpRanges | join(",")'`
echo "Update Azure DevOps IP Address in AKS Cluster Authorized IP Ranges"
az aks update --resource-group MYAKSRG --name MYAKSCLUSTER --api-server-authorized-ip-ranges $authorizedips,$azdoip
所有 kubernetes 任务完成后,在末尾添加另一个“Azure Cli”任务以删除 Azure DevOps IP。
echo "Set Azure Subscription to MYSUBSCRIPTION"
az account set --subscription "MYSUBSCRIPTION"
echo "Get credentials for AKS Cluster Admin"
az aks get-credentials --resource-group MYAKSRG --name MYAKSCLUSTER --admin --file ~/.kube/config
echo "Get New Authorized IP Ranges"
newauthorizedips=`az aks show --resource-group MYAKSRG --name MYAKSCLUSTER --query apiServerAccessProfile |jq -r '.authorizedIpRanges | join(",")'`
echo "Remove AzDo IP and store it as a variable" #Removes last element from the array of IPs
authorizedips=`echo $newauthorizedips | awk 'BEGIN{FS=OFS=","}NF--'`
echo "Update AKS by restoring Authorized IPs"
az aks update --resource-group MYAKSRG --name MYAKSCLUSTER --api-server-authorized-ip-ranges $authorizedips
【讨论】:
对于 windows 构建代理,使用 powershell 路由更安全:
steps:
- task: AzurePowerShell@5
displayName: 'Add buildserver public ip'
inputs:
azureSubscription: test
ScriptType: InlineScript
Inline: |
$ip = (Invoke-WebRequest -uri "http://ifconfig.me/ip").Content
New-AzSqlServerFirewallRule -ResourceGroupName "group" -ServerName "database-server-name" -FirewallRuleName "azuredevops" -StartIpAddress $ip -EndIpAddress $ip
azurePowerShellVersion: LatestVersion
【讨论】:
如果你来这里是因为你在尝试使用 Azure DevOps 到 MSBuild 并部署到 Azure SQL 服务器时遇到了这个错误,并且有点生气,因为互联网上似乎没有任何内容,人们都在使用power shell 脚本来找出服务器的 IP 地址和白名单等,那么您最好在 yml 文件以及 MSBuild 中使用名为“Azure SQL 数据库部署”的任务,如下所示:
- task: MSBuild@1
displayName: Build the database project
inputs:
solution: '**/projectname.sqlproj'
msbuildArguments: '/t:Restore /t:Build '
- task: SqlAzureDacpacDeployment@1
inputs:
azureSubscription: ''
AuthenticationType: 'server'
ServerName: '.database.windows.net'
DatabaseName: ''
SqlUsername: ''
SqlPassword: ''
deployType: 'DacpacTask'
DeploymentAction: 'Publish'
DacpacFile: '**/projectname.dacpac'
IpDetectionMethod: 'AutoDetect'
auto 的 IpDetectionMethod 对我有用,它确实允许您轻松输入自己的值(虽然我没有尝试过)
【讨论】: