Skip to content

Commit 27efc77

Browse files
authored
api,ui: fix empty source cidr value for firewall rule (#10208)
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 5447950 commit 27efc77

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

api/src/main/java/org/apache/cloudstack/api/command/user/firewall/CreateFirewallRuleCmd.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.apache.commons.collections.CollectionUtils;
23+
import org.apache.commons.lang3.StringUtils;
2224
import org.apache.log4j.Logger;
2325

2426
import org.apache.cloudstack.acl.RoleType;
@@ -103,14 +105,13 @@ public String getProtocol() {
103105

104106
@Override
105107
public List<String> getSourceCidrList() {
106-
if (cidrlist != null) {
108+
if (CollectionUtils.isNotEmpty(cidrlist) && !(cidrlist.size() == 1 && StringUtils.isBlank(cidrlist.get(0)))) {
107109
return cidrlist;
108110
} else {
109-
List<String> oneCidrList = new ArrayList<String>();
111+
List<String> oneCidrList = new ArrayList<>();
110112
oneCidrList.add(NetUtils.ALL_IP4_CIDRS);
111113
return oneCidrList;
112114
}
113-
114115
}
115116

116117
// ///////////////////////////////////////////////////
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
package org.apache.cloudstack.api.command.user.firewall;
18+
19+
import java.util.ArrayList;
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.List;
23+
24+
import org.apache.commons.collections.CollectionUtils;
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.junit.MockitoJUnitRunner;
29+
import org.springframework.test.util.ReflectionTestUtils;
30+
31+
import com.cloud.utils.net.NetUtils;
32+
33+
@RunWith(MockitoJUnitRunner.class)
34+
public class CreateFirewallRuleCmdTest {
35+
36+
private void validateAllIp4Cidr(final CreateFirewallRuleCmd cmd) {
37+
Assert.assertTrue(CollectionUtils.isNotEmpty(cmd.getSourceCidrList()));
38+
Assert.assertEquals(1, cmd.getSourceCidrList().size());
39+
Assert.assertEquals(NetUtils.ALL_IP4_CIDRS, cmd.getSourceCidrList().get(0));
40+
}
41+
42+
@Test
43+
public void testGetSourceCidrList_Null() {
44+
final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd();
45+
ReflectionTestUtils.setField(cmd, "cidrlist", null);
46+
validateAllIp4Cidr(cmd);
47+
}
48+
49+
@Test
50+
public void testGetSourceCidrList_Empty() {
51+
final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd();
52+
ReflectionTestUtils.setField(cmd, "cidrlist", new ArrayList<>());
53+
validateAllIp4Cidr(cmd);
54+
}
55+
56+
@Test
57+
public void testGetSourceCidrList_NullFirstElement() {
58+
final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd();
59+
List<String> list = new ArrayList<>();
60+
list.add(null);
61+
ReflectionTestUtils.setField(cmd, "cidrlist", list);
62+
validateAllIp4Cidr(cmd);
63+
}
64+
65+
@Test
66+
public void testGetSourceCidrList_EmptyFirstElement() {
67+
final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd();
68+
ReflectionTestUtils.setField(cmd, "cidrlist", Collections.singletonList(" "));
69+
validateAllIp4Cidr(cmd);
70+
}
71+
72+
@Test
73+
public void testGetSourceCidrList_Valid() {
74+
final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd();
75+
String cidr = "10.1.1.1/22";
76+
ReflectionTestUtils.setField(cmd, "cidrlist", Collections.singletonList(cidr));
77+
Assert.assertTrue(CollectionUtils.isNotEmpty(cmd.getSourceCidrList()));
78+
Assert.assertEquals(1, cmd.getSourceCidrList().size());
79+
Assert.assertEquals(cidr, cmd.getSourceCidrList().get(0));
80+
}
81+
82+
@Test
83+
public void testGetSourceCidrList_EmptyFirstElementButMore() {
84+
final CreateFirewallRuleCmd cmd = new CreateFirewallRuleCmd();
85+
String cidr = "10.1.1.1/22";
86+
ReflectionTestUtils.setField(cmd, "cidrlist", Arrays.asList(" ", cidr));
87+
Assert.assertTrue(CollectionUtils.isNotEmpty(cmd.getSourceCidrList()));
88+
Assert.assertEquals(2, cmd.getSourceCidrList().size());
89+
Assert.assertEquals(cidr, cmd.getSourceCidrList().get(1));
90+
}
91+
}

ui/src/views/network/FirewallRules.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,9 @@ export default {
404404
addRule () {
405405
if (this.loading) return
406406
this.loading = true
407+
if (this.newRule.cidrlist == null || this.newRule.cidrlist.trim?.() === '') {
408+
delete this.newRule.cidrlist
409+
}
407410
api('createFirewallRule', { ...this.newRule }).then(response => {
408411
this.$pollJob({
409412
jobId: response.createfirewallruleresponse.jobid,

0 commit comments

Comments
 (0)