From a364320cc1234fce5c2e2d0511984ea79da83d17 Mon Sep 17 00:00:00 2001 From: jsonwan Date: Tue, 7 Nov 2023 14:44:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20IP=E9=80=89=E6=8B=A9=E5=99=A8=E8=BE=93?= =?UTF-8?q?=E5=85=A5=E9=87=8D=E5=A4=8DIP=E6=8A=A5=E9=94=99=20#2591?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bk/job/common/util/ip/IpUtils.java | 28 +++++++++------- .../bk/job/common/util/IpUtilsTest.java | 33 +++++++++++++++++++ .../api/web/impl/WebHostResourceImpl.java | 14 ++++---- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/ip/IpUtils.java b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/ip/IpUtils.java index 2c1d352f39..3f502ba123 100644 --- a/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/ip/IpUtils.java +++ b/src/backend/commons/common/src/main/java/com/tencent/bk/job/common/util/ip/IpUtils.java @@ -32,6 +32,7 @@ import org.apache.commons.lang3.math.NumberUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.validator.routines.InetAddressValidator; +import org.springframework.util.CollectionUtils; import java.net.Inet4Address; import java.net.Inet6Address; @@ -41,8 +42,10 @@ import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -326,22 +329,25 @@ public static String extractIp(String cloudIp) { } /** - * 将纯IP与含云区域的IP分离开 + * 将纯IPv4地址与含云区域的IPv4地址分离开,清洗掉其中的空白字符并去重 * - * @param ipOrCloudIpList ip/cloudIp列表 - * @return <纯IP列表,含云区域IP列表> + * @param ipv4OrCloudIpv4List ipv4/cloudIpv4列表 + * @return <纯IPv4地址集合,含云区域IPv4地址集合> */ - public static Pair, List> separateIpAndCloudIps(List ipOrCloudIpList) { - List ipList = new ArrayList<>(); - List cloudIpList = new ArrayList<>(); - for (String ipOrCloudIp : ipOrCloudIpList) { - if (ipOrCloudIp.contains(":")) { - cloudIpList.add(ipOrCloudIp); + public static Pair, Set> parseCleanIpv4AndCloudIpv4s(List ipv4OrCloudIpv4List) { + Set ipv4Set = new HashSet<>(); + Set cloudIpv4Set = new HashSet<>(); + if (CollectionUtils.isEmpty(ipv4OrCloudIpv4List)) { + return Pair.of(ipv4Set, cloudIpv4Set); + } + for (String ipv4OrCloudIpv4 : ipv4OrCloudIpv4List) { + if (ipv4OrCloudIpv4.contains(":")) { + cloudIpv4Set.add(StringUtils.deleteWhitespace(ipv4OrCloudIpv4)); } else { - ipList.add(ipOrCloudIp); + ipv4Set.add(StringUtils.deleteWhitespace(ipv4OrCloudIpv4)); } } - return Pair.of(ipList, cloudIpList); + return Pair.of(ipv4Set, cloudIpv4Set); } /** diff --git a/src/backend/commons/common/src/test/java/com/tencent/bk/job/common/util/IpUtilsTest.java b/src/backend/commons/common/src/test/java/com/tencent/bk/job/common/util/IpUtilsTest.java index a67731cb6a..d7b313235d 100644 --- a/src/backend/commons/common/src/test/java/com/tencent/bk/job/common/util/IpUtilsTest.java +++ b/src/backend/commons/common/src/test/java/com/tencent/bk/job/common/util/IpUtilsTest.java @@ -25,8 +25,13 @@ package com.tencent.bk.job.common.util; import com.tencent.bk.job.common.util.ip.IpUtils; +import org.apache.commons.lang3.tuple.Pair; import org.junit.jupiter.api.Test; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy; @@ -92,6 +97,34 @@ void testExtractIp() { assertThat(result).isNull(); } + @Test + void testParseCleanIpAndCloudIps() { + Pair, Set> emptyPair = IpUtils.parseCleanIpv4AndCloudIpv4s(null); + assertThat(emptyPair.getLeft().size()).isEqualTo(0); + assertThat(emptyPair.getRight().size()).isEqualTo(0); + + emptyPair = IpUtils.parseCleanIpv4AndCloudIpv4s(new ArrayList<>()); + assertThat(emptyPair.getLeft().size()).isEqualTo(0); + assertThat(emptyPair.getRight().size()).isEqualTo(0); + + List ipv4OrCloudIpv4List = new ArrayList<>(); + ipv4OrCloudIpv4List.add("127.0.0.1"); + ipv4OrCloudIpv4List.add(" 127.0.0.1"); + ipv4OrCloudIpv4List.add("0:127 .0.0.1"); + ipv4OrCloudIpv4List.add("127.0.0.2"); + ipv4OrCloudIpv4List.add(" 127.0.0.2 \n"); + ipv4OrCloudIpv4List.add("1:127.0.0.1 \r\n"); + Pair, Set> pair = IpUtils.parseCleanIpv4AndCloudIpv4s(ipv4OrCloudIpv4List); + Set ipv4Set = pair.getLeft(); + Set cloudIpv4Set = pair.getRight(); + assertThat(ipv4Set.size()).isEqualTo(2); + assertThat(ipv4Set.contains("127.0.0.1")).isTrue(); + assertThat(ipv4Set.contains("127.0.0.2")).isTrue(); + assertThat(cloudIpv4Set.size()).isEqualTo(2); + assertThat(cloudIpv4Set.contains("0:127.0.0.1")).isTrue(); + assertThat(cloudIpv4Set.contains("1:127.0.0.1")).isTrue(); + } + @Test void testInferProtocolByIp() { // Ipv6正例 diff --git a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebHostResourceImpl.java b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebHostResourceImpl.java index 3470495c60..57cde0510e 100644 --- a/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebHostResourceImpl.java +++ b/src/backend/job-manage/service-job-manage/src/main/java/com/tencent/bk/job/manage/api/web/impl/WebHostResourceImpl.java @@ -378,22 +378,22 @@ private List findHosts(AppResourceScope appResourceScope, Ho } List ipOrCloudIpList = req.getIpList(); if (CollectionUtils.isNotEmpty(ipOrCloudIpList)) { - Pair, List> pair = IpUtils.separateIpAndCloudIps(ipOrCloudIpList); - List ipList = pair.getLeft(); - List cloudIpList = pair.getRight(); + Pair, Set> pair = IpUtils.parseCleanIpv4AndCloudIpv4s(ipOrCloudIpList); + Set ipSet = pair.getLeft(); + Set cloudIpSet = pair.getRight(); // 根据ip地址查资源范围及白名单内的主机详情 - if (CollectionUtils.isNotEmpty(ipList)) { + if (CollectionUtils.isNotEmpty(ipSet)) { hostDTOList.addAll(whiteIpAwareScopeHostService.getScopeHostsIncludingWhiteIPByIp( appResourceScope, req.getActionScope(), - ipList + ipSet )); } - if (CollectionUtils.isNotEmpty(cloudIpList)) { + if (CollectionUtils.isNotEmpty(cloudIpSet)) { hostDTOList.addAll(whiteIpAwareScopeHostService.getScopeHostsIncludingWhiteIPByCloudIp( appResourceScope, req.getActionScope(), - cloudIpList + cloudIpSet )); } }