Skip to content

Commit 4e25ea2

Browse files
committed
update 1
1 parent 8793d24 commit 4e25ea2

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

server/src/main/java/com/cloud/network/vpc/NetworkACLServiceImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import com.cloud.utils.db.SearchCriteria.Op;
7373
import com.cloud.utils.exception.CloudRuntimeException;
7474
import com.cloud.utils.net.NetUtils;
75+
import com.cloud.utils.net.NetworkProtocols;
7576

7677
@Component
7778
public class NetworkACLServiceImpl extends ManagerBase implements NetworkACLService {
@@ -588,7 +589,9 @@ protected void validateProtocol(NetworkACLItemVO networkACLItemVO) {
588589
throw new InvalidParameterValueException("Can specify icmpCode and icmpType for ICMP protocol only");
589590
}
590591
if (isIcmpProtocol) {
591-
NetUtils.validateIcmpTypeAndCode(icmpType, icmpCode);
592+
if (!NetworkProtocols.validateIcmpTypeAndCode(icmpType, icmpCode)) {
593+
throw new InvalidParameterValueException(String.format("Unsupported icmptype %s or icmpcode %s", icmpType, icmpCode));
594+
}
592595
}
593596

594597
Integer sourcePortStart = networkACLItemVO.getSourcePortStart();

utils/src/main/java/com/cloud/utils/net/NetworkProtocols.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.Arrays;
2121
import java.util.List;
22+
import java.util.Optional;
2223

2324
/**
2425
* Network protocols and parameters.
@@ -274,6 +275,23 @@ static void addIcmpCode(IcmpCode code) {
274275
type.addIcmpCodes(code);
275276
}
276277

278+
public static boolean validateIcmpTypeAndCode(Integer type, Integer code) {
279+
if (type != null && type != -1) {
280+
Optional<IcmpType> icmpTypeOptional = IcmpTypes.stream().filter(t -> t.getType().equals(type)).findFirst();
281+
if (icmpTypeOptional == null || icmpTypeOptional.isEmpty()) {
282+
return false;
283+
}
284+
IcmpType icmpType = icmpTypeOptional.get();
285+
if (code != null && code != -1) {
286+
Optional<IcmpCode> icmpCodeOptional = icmpType.getIcmpCodes().stream().filter(c -> c.getCode().equals(code)).findFirst();
287+
if (icmpCodeOptional == null || icmpCodeOptional.isEmpty()) {
288+
return false;
289+
}
290+
}
291+
}
292+
return true;
293+
}
294+
277295
static {
278296
addIcmpCode(new IcmpCode(0, 0, "Echo reply"));
279297
addIcmpCode(new IcmpCode(3, 0, "Net unreachable"));

utils/src/test/java/com/cloud/utils/net/NetUtilsTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,4 +843,31 @@ public void getNetworkInterfaceTestReturnInterfaceReturnedByGetByName() throws S
843843
Assert.assertEquals(expected, result);
844844
networkInterfaceMocked.close();
845845
}
846+
847+
private void validateIcmpTypeAndCodeInternal(Integer icmpType, Integer icmpCode, boolean expectedResult) {
848+
boolean actualResult = true;
849+
try {
850+
NetUtils.validateIcmpTypeAndCode(icmpType, icmpCode);
851+
} catch (CloudRuntimeException e) {
852+
actualResult = false;
853+
}
854+
Assert.assertEquals(expectedResult, actualResult);
855+
}
856+
857+
@Test
858+
@PrepareForTest(NetUtils.class)
859+
public void validateIcmpTypeAndCodes() {
860+
validateIcmpTypeAndCodeInternal(-1, -1, true);
861+
validateIcmpTypeAndCodeInternal(3, 2, true);
862+
validateIcmpTypeAndCodeInternal(null, null, false);
863+
validateIcmpTypeAndCodeInternal(null, -1, false);
864+
validateIcmpTypeAndCodeInternal(-1, null, false);
865+
validateIcmpTypeAndCodeInternal(-1, 2, false);
866+
validateIcmpTypeAndCodeInternal(3, -1, false); // need discussion
867+
validateIcmpTypeAndCodeInternal(-2, 2, false);
868+
validateIcmpTypeAndCodeInternal(257, 2, false);
869+
validateIcmpTypeAndCodeInternal(3, -2, false);
870+
validateIcmpTypeAndCodeInternal(3, -257, false);
871+
872+
}
846873
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.utils.net;
21+
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
25+
import org.junit.runner.RunWith;
26+
import org.powermock.modules.junit4.PowerMockRunner;
27+
28+
29+
@RunWith(PowerMockRunner.class)
30+
public class NetworkProtocolsTest {
31+
32+
@Test
33+
public void validateIcmpTypeAndCode() {
34+
validateIcmpTypeAndCodeInternal(null, null, true);
35+
validateIcmpTypeAndCodeInternal(null, -1, true);
36+
validateIcmpTypeAndCodeInternal(-1, -1, true);
37+
validateIcmpTypeAndCodeInternal(3, -1, true);
38+
validateIcmpTypeAndCodeInternal(3, 15, true);
39+
validateIcmpTypeAndCodeInternal(4, -1, false);
40+
validateIcmpTypeAndCodeInternal(5, 10, false);
41+
}
42+
43+
private void validateIcmpTypeAndCodeInternal(Integer type, Integer code, boolean expected) {
44+
boolean actual = NetworkProtocols.validateIcmpTypeAndCode(type, code);
45+
Assert.assertEquals(expected, actual);
46+
}
47+
}

0 commit comments

Comments
 (0)