Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#2592 from jsonwan/github_fix/ipcho…
Browse files Browse the repository at this point in the history
…oser

fix: IP选择器输入重复IP报错 TencentBlueKing#2591
  • Loading branch information
wangyu096 authored Nov 7, 2023
2 parents e410ecb + a364320 commit 819e059
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<String>, List<String>> separateIpAndCloudIps(List<String> ipOrCloudIpList) {
List<String> ipList = new ArrayList<>();
List<String> cloudIpList = new ArrayList<>();
for (String ipOrCloudIp : ipOrCloudIpList) {
if (ipOrCloudIp.contains(":")) {
cloudIpList.add(ipOrCloudIp);
public static Pair<Set<String>, Set<String>> parseCleanIpv4AndCloudIpv4s(List<String> ipv4OrCloudIpv4List) {
Set<String> ipv4Set = new HashSet<>();
Set<String> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -92,6 +97,34 @@ void testExtractIp() {
assertThat(result).isNull();
}

@Test
void testParseCleanIpAndCloudIps() {
Pair<Set<String>, Set<String>> 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<String> 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<String>, Set<String>> pair = IpUtils.parseCleanIpv4AndCloudIpv4s(ipv4OrCloudIpv4List);
Set<String> ipv4Set = pair.getLeft();
Set<String> 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正例
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,22 +378,22 @@ private List<ApplicationHostDTO> findHosts(AppResourceScope appResourceScope, Ho
}
List<String> ipOrCloudIpList = req.getIpList();
if (CollectionUtils.isNotEmpty(ipOrCloudIpList)) {
Pair<List<String>, List<String>> pair = IpUtils.separateIpAndCloudIps(ipOrCloudIpList);
List<String> ipList = pair.getLeft();
List<String> cloudIpList = pair.getRight();
Pair<Set<String>, Set<String>> pair = IpUtils.parseCleanIpv4AndCloudIpv4s(ipOrCloudIpList);
Set<String> ipSet = pair.getLeft();
Set<String> 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
));
}
}
Expand Down

0 comments on commit 819e059

Please sign in to comment.