Skip to content

Commit 3952e3e

Browse files
committed
Merge pull request #1581 from pdube/network-acl-rules-order
CLOUDSTACK-9404 Fixed ordering of network ACL rules being sent to the VR. The comparator was inverted. Issue: https://issues.apache.org/jira/browse/CLOUDSTACK-9404 In this example, I created rules with the port numbers the same as the rule numbers. Chain ACL_INBOUND_eth2 (1 references) target prot opt source destination ACCEPT all -- anywhere 225.0.0.50 ACCEPT all -- anywhere vrrp.mcast.net DROP tcp -- anywhere anywhere tcp dpt:netstat DROP tcp -- anywhere anywhere tcp dpt:10 DROP tcp -- anywhere anywhere tcp dpt:5 DROP tcp -- anywhere anywhere tcp dpt:3 DROP tcp -- anywhere anywhere tcp dpt:2 DROP all -- anywhere anywhere We can see above that the rules are inverted. After the fix: Chain ACL_INBOUND_eth2 (1 references) target prot opt source destination ACCEPT all -- anywhere 225.0.0.50 ACCEPT all -- anywhere vrrp.mcast.net DROP tcp -- anywhere anywhere tcp dpt:2 DROP tcp -- anywhere anywhere tcp dpt:3 DROP tcp -- anywhere anywhere tcp dpt:5 DROP tcp -- anywhere anywhere tcp dpt:10 DROP tcp -- anywhere anywhere tcp dpt:netstat DROP all -- anywhere anywhere * pr/1581: Added ASF license to unit test file Added unit test to verify ordering Fixed ordering of network ACL rules being sent to the VR. The comparator was inverted Signed-off-by: Will Stevens <williamstevens@gmail.com>
2 parents 9275ba2 + 9cdd23f commit 3952e3e

2 files changed

Lines changed: 64 additions & 6 deletions

File tree

core/src/com/cloud/agent/api/routing/SetNetworkACLCommand.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,8 @@ public NetworkACLTO[] getRules() {
4545

4646
public String[][] generateFwRules() {
4747
final List<NetworkACLTO> aclList = Arrays.asList(rules);
48-
Collections.sort(aclList, new Comparator<NetworkACLTO>() {
49-
@Override
50-
public int compare(final NetworkACLTO acl1, final NetworkACLTO acl2) {
51-
return acl1.getNumber() < acl2.getNumber() ? 1 : -1;
52-
}
53-
});
48+
49+
orderNetworkAclRulesByRuleNumber(aclList);
5450

5551
final String[][] result = new String[2][aclList.size()];
5652
int i = 0;
@@ -97,6 +93,15 @@ public int compare(final NetworkACLTO acl1, final NetworkACLTO acl2) {
9793
return result;
9894
}
9995

96+
protected void orderNetworkAclRulesByRuleNumber(List<NetworkACLTO> aclList) {
97+
Collections.sort(aclList, new Comparator<NetworkACLTO>() {
98+
@Override
99+
public int compare(final NetworkACLTO acl1, final NetworkACLTO acl2) {
100+
return acl1.getNumber() > acl2.getNumber() ? 1 : -1;
101+
}
102+
});
103+
}
104+
100105
public NicTO getNic() {
101106
return nic;
102107
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.agent.api.routing;
21+
22+
import static org.junit.Assert.assertEquals;
23+
24+
import java.util.List;
25+
26+
import org.junit.Test;
27+
28+
import com.cloud.agent.api.to.NetworkACLTO;
29+
import com.google.common.collect.Lists;
30+
31+
public class SetNetworkACLCommandTest {
32+
33+
@Test
34+
public void testNetworkAclRuleOrdering(){
35+
36+
//given
37+
List<NetworkACLTO> aclList = Lists.newArrayList();
38+
39+
aclList.add(new NetworkACLTO(3, null, null, null, null, false, false, null, null, null, null, false, 3));
40+
aclList.add(new NetworkACLTO(1, null, null, null, null, false, false, null, null, null, null, false, 1));
41+
aclList.add(new NetworkACLTO(2, null, null, null, null, false, false, null, null, null, null, false, 2));
42+
43+
SetNetworkACLCommand cmd = new SetNetworkACLCommand(aclList, null);
44+
45+
//when
46+
cmd.orderNetworkAclRulesByRuleNumber(aclList);
47+
48+
//then
49+
for(int i=0; i< aclList.size();i++){
50+
assertEquals(aclList.get(i).getNumber(), i+1);
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)