azure - "Authorized IP ranges should be defined on Kubernetes Services"阻止部署管道

标签 azure kubernetes azure-devops azure-pipelines

Azure Advisor 中的安全警告之一是:

Authorized IP ranges should be defined on Kubernetes Services

Restrict access to the Kubernetes Service Management API by granting API access only to IP addresses in specific ranges. It is recommended to limit access to authorized IP ranges to ensure that only applications from allowed networks can access the cluster.

可以在这里阅读所有内容:

https://learn.microsoft.com/en-us/azure/aks/api-server-authorized-ip-ranges

更新现有 AKS 群集的命令如下:

az aks update \
    --resource-group myResourceGroup \
    --name myAKSCluster \
    --api-server-authorized-ip-ranges  73.140.245.0/24

或者删除任何:

az aks update \
    --resource-group myResourceGroup \
    --name myAKSCluster \
    --api-server-authorized-ip-ranges ""

无论如何,启用此功能后,我的部署管道会超时,因为它无法连接到 AKS。我添加了当提示无法连接并且仍然无法连接时弹出的 IP 地址。

所以基本上,当我需要运行管道时,我只能删除 IP 范围,然后在完成后将其添加回来。显然这不是处理这个问题的理想方法。

我应该如何解决此问题?Azure 是否有更优雅的方式将 AKS 中管道中的 IP 列入白名单?

最佳答案

设拉子让我朝着正确的方向前进。

但是,唯一相关的部分是这部分:

https://learn.microsoft.com/en-us/azure/devops/organizations/security/allow-list-ip-url?view=azure-devops#azure-pipelines-agents

相关的是我的构建和部署代理是 Microsoft 托管的。 Shiraz 链接的文章主要涉及本地 Azure DevOps:

Azure DevOps Services | Azure DevOps Server 2020 | Azure DevOps Server 2019 | TFS 2018 - TFS 2015

无论如何,这是 Microsoft 托管代理的相关文档:

https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/hosted?view=azure-devops&tabs=yaml#networking

基本上,有两种选择:

  1. 正在下载weekly JSON Azure IP Ranges然后在 AzureDevOps.{region} 部分下添加地理区域的每个 IP。因此,如果您位于美国中部地区,仅添加该地区是不够的。您必须添加美国的所有区域,因为在使用构建和部署代理时可能会使用不同的区域。

  2. 编写一个简单的程序来为您解析 json 文件并收集所有相关的 IP。文档使用的示例:

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace WeeklyFileIPRanges
{
    class Program
    {
        // Path to the locally saved weekly file
        const string weeklyFilePath = @"C:\MyPath\ServiceTags_Public_20200504.json";

        static void Main(string[] args)
        {
            // United States geography has the following regions:
            // Central US, East US 2, East US, North Central US, 
            // South Central US, West Central US, West US, West US 2
            List<string> USGeographyRegions = new List<string>
            {
                "centralus",
                "eastus",
                "eastus2",
                "northcentralus",
                "southcentralus",
                "westcentralus",
                "westus",
                "westus2"
            };

            // Load the weekly file
            JObject weeklyFile = JObject.Parse(File.ReadAllText(weeklyFilePath));
            JArray values = (JArray)weeklyFile["values"];

            foreach (string region in USGeographyRegions)
            {
                string azureCloudRegion = $"AzureCloud.{region}";
                Console.WriteLine(azureCloudRegion);

                var ipList =
                    from v in values
                    where (string)v["name"] == azureCloudRegion
                    select v["properties"]["addressPrefixes"];

                foreach (var ip in ipList.Children())
                {
                    Console.WriteLine(ip);
                }
            }
        }
    }
}

然后要添加 IP,您可以使用以下命令:

az aks update -g <resource_group> -n <aks_deployment_name> --api-server-authorized-ip-ranges [every,single,ip,address,for,the,geographic,area]

如果有人有更好的方法,我很乐意接受作为答案。必须有一个给定的值,即美国地理区域的 +1600 个 IP,而 --api-server-authorized-ip-ranges 最多只需要 200 个。

此时,暂时删除授权的 IP 地址会更容易。

这里有一些有前途的东西:

How to get the IP Address for Azure DevOps Hosted Agents to add to the white list

关于azure - "Authorized IP ranges should be defined on Kubernetes Services"阻止部署管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65372485/

相关文章:

azure - 如何使用不同的变量组在 Azure DevOps 管道中多次运行作业?

visual-studio - TFS 构建通知未显示任何项目

azure - 在azure任务上设置变量以传递Azure管道的另一个任务

scala - 无法将 Azure 广告公钥字符串转换为 PublicRSAKey

c# - USQL 查询从 Json 数据创建表

Django//版本更新后所有请求出现 Daphne 500 错误 "TypeError: object HttpResponse can' t 用于 'await' 表达式”

docker - 如何通过ip查找docker容器?

sql-server - 无法使用 SQL Server 连接到 Azure VM

kubernetes - Istio上消息的模式匹配

kubernetes - 如何将多个模板化配置文件加载到 Helm chart 中?