Skip to content

Commit 9c63c39

Browse files
JoaoJandreJoão Paraquetti
andauthored
Add new parameter to createLoadBalancerRule API (#6460)
* Add new parameter to createLoadBalancerRule API * address review Co-authored-by: João Paraquetti <joao@scclouds.com.br>
1 parent 117ce1a commit 9c63c39

File tree

20 files changed

+184
-22
lines changed

20 files changed

+184
-22
lines changed

api/src/main/java/com/cloud/agent/api/to/LoadBalancerTO.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class LoadBalancerTO {
5757
final static int MAX_STICKINESS_POLICIES = 1;
5858
final static int MAX_HEALTHCHECK_POLICIES = 1;
5959

60+
private String cidrList;
61+
6062
public LoadBalancerTO(String uuid, String srcIp, int srcPort, String protocol, String algorithm, boolean revoked, boolean alreadyAdded, boolean inline,
6163
List<LbDestination> destinations) {
6264
if (destinations == null) { // for autoscaleconfig destinations will be null;
@@ -239,6 +241,14 @@ public void setSrcIpNetmask(String srcIpNetmask) {
239241
this.srcIpNetmask = srcIpNetmask;
240242
}
241243

244+
public void setCidrList(String cidrList){
245+
this.cidrList = cidrList;
246+
}
247+
248+
public String getCidrList() {
249+
return cidrList;
250+
}
251+
242252
public static class StickinessPolicyTO {
243253
private String methodName;
244254
private List<Pair<String, String>> params;

api/src/main/java/com/cloud/network/lb/LoadBalancingRule.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,4 +482,8 @@ public Ip getSourceIp() {
482482
public Scheme getScheme() {
483483
return lb.getScheme();
484484
}
485+
486+
public String getCidrList(){
487+
return lb.getCidrList();
488+
}
485489
}

api/src/main/java/com/cloud/network/lb/LoadBalancingRulesService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ LoadBalancer createPublicLoadBalancerRule(String xId, String name, String descri
5454
Long ipAddrId, String protocol, String algorithm, long networkId, long lbOwnerId, boolean openFirewall, String lbProtocol, Boolean forDisplay) throws NetworkRuleConflictException,
5555
InsufficientAddressCapacityException;
5656

57+
LoadBalancer createPublicLoadBalancerRule(String xId, String name, String description, int srcPortStart, int srcPortEnd, int defPortStart, int defPortEnd,
58+
Long ipAddrId, String protocol, String algorithm, long networkId, long lbOwnerId, boolean openFirewall, String lbProtocol, Boolean forDisplay, List<String> cidrList) throws NetworkRuleConflictException,
59+
InsufficientAddressCapacityException;
60+
5761
LoadBalancer updateLoadBalancerRule(UpdateLoadBalancerRuleCmd cmd);
5862

5963
boolean deleteLoadBalancerRule(long lbRuleId, boolean apply);

api/src/main/java/com/cloud/network/rules/LoadBalancer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ public interface LoadBalancer extends FirewallRule, LoadBalancerContainer {
2525

2626
int getDefaultPortEnd();
2727

28+
String getCidrList();
29+
2830
}

api/src/main/java/org/apache/cloudstack/api/command/user/loadbalancer/CreateLoadBalancerRuleCmd.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ public class CreateLoadBalancerRuleCmd extends BaseAsyncCreateCmd /*implements L
107107
@Parameter(name = ApiConstants.DOMAIN_ID, type = CommandType.UUID, entityType = DomainResponse.class, description = "the domain ID associated with the load balancer")
108108
private Long domainId;
109109

110-
@Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, description = "the CIDR list to forward traffic from. Multiple entries must be separated by a single comma character (,). This parameter is deprecated. Do not use.")
110+
@Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, since = "4.18.0.0", description = "the CIDR list to allow traffic, "
111+
+ "all other CIDRs will be blocked. Multiple entries must be separated by a single comma character (,). By default, all CIDRs are allowed.")
111112
private List<String> cidrlist;
112113

113114
@Parameter(name = ApiConstants.NETWORK_ID, type = CommandType.UUID, entityType = NetworkResponse.class, description = "The guest network this "
@@ -306,15 +307,11 @@ public void execute() throws ResourceAllocationException, ResourceUnavailableExc
306307

307308
@Override
308309
public void create() {
309-
//cidr list parameter is deprecated
310-
if (cidrlist != null) {
311-
throw new InvalidParameterValueException(
312-
"Parameter cidrList is deprecated; if you need to open firewall rule for the specific CIDR, please refer to createFirewallRule command");
313-
}
314310
try {
315311
LoadBalancer result =
316312
_lbService.createPublicLoadBalancerRule(getXid(), getName(), getDescription(), getSourcePortStart(), getSourcePortEnd(), getDefaultPortStart(),
317-
getDefaultPortEnd(), getSourceIpAddressId(), getProtocol(), getAlgorithm(), getNetworkId(), getEntityOwnerId(), getOpenFirewall(), getLbProtocol(), isDisplay());
313+
getDefaultPortEnd(), getSourceIpAddressId(), getProtocol(), getAlgorithm(), getNetworkId(), getEntityOwnerId(), getOpenFirewall(), getLbProtocol(), isDisplay(),
314+
getCidrList());
318315
this.setEntityId(result.getId());
319316
this.setEntityUuid(result.getUuid());
320317
} catch (NetworkRuleConflictException e) {
@@ -425,4 +422,9 @@ public String getSyncObjType() {
425422
public Long getSyncObjId() {
426423
return getNetworkId();
427424
}
425+
426+
public List<String> getCidrList(){
427+
return cidrlist;
428+
}
429+
428430
}

api/src/main/java/org/apache/cloudstack/api/response/LoadBalancerResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public class LoadBalancerResponse extends BaseResponse implements ControlledEnti
6464
private String networkId;
6565

6666
@SerializedName(ApiConstants.CIDR_LIST)
67-
@Param(description = "the cidr list to forward traffic from. Multiple entries are separated by a single comma character (,).")
67+
@Param(description = "the CIDR list to allow traffic, all other CIDRs will be blocked. Multiple entries must be separated by a single comma character (,).")
6868
private String cidrList;
6969

7070
@SerializedName(ApiConstants.ACCOUNT)

core/src/main/java/com/cloud/network/HAProxyConfigurator.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828
import java.util.Set;
2929

30+
import org.apache.commons.lang3.StringUtils;
3031
import org.apache.log4j.Logger;
3132

3233
import com.cloud.agent.api.routing.LoadBalancerConfigCommand;
@@ -551,6 +552,12 @@ private List<String> getRulesForPool(final LoadBalancerTO lbTO, final boolean ke
551552
result.add(sb.toString());
552553
}
553554

555+
String cidrList = lbTO.getCidrList();
556+
557+
if (StringUtils.isNotBlank(cidrList)) {
558+
result.add(String.format("\tacl network_allowed src %s \n\ttcp-request connection reject if !network_allowed", cidrList));
559+
}
560+
554561
result.add(blankLine);
555562
return result;
556563
}

core/src/test/java/com/cloud/network/HAProxyConfiguratorTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.junit.After;
2525
import org.junit.AfterClass;
26+
import org.junit.Assert;
2627
import org.junit.Before;
2728
import org.junit.BeforeClass;
2829
import org.junit.Test;
@@ -109,6 +110,18 @@ public void testGenerateConfigurationLoadBalancerProxyProtocolConfigCommand() {
109110
assertTrue("'send-proxy' should result if protocol is 'tcp-proxy'", result.contains("send-proxy"));
110111
}
111112

113+
@Test
114+
public void generateConfigurationTestWithCidrList() {
115+
LoadBalancerTO lb = new LoadBalancerTO("1", "10.2.0.1", 22, "tcp", "roundrobin", false, false, false, null, null);
116+
lb.setCidrList("1.1.1.1 2.2.2.2/24");
117+
LoadBalancerTO[] lba = new LoadBalancerTO[1];
118+
lba[0] = lb;
119+
HAProxyConfigurator hpg = new HAProxyConfigurator();
120+
LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lba, "10.0.0.1", "10.1.0.1", "10.1.1.1", null, 1L, "12", false);
121+
String result = genConfig(hpg, cmd);
122+
Assert.assertTrue(result.contains("acl network_allowed src 1.1.1.1 2.2.2.2/24 \n\ttcp-request connection reject if !network_allowed"));
123+
}
124+
112125
private String genConfig(HAProxyConfigurator hpg, LoadBalancerConfigCommand cmd) {
113126
String[] sa = hpg.generateConfiguration(cmd);
114127
StringBuilder sb = new StringBuilder();

engine/components-api/src/main/java/com/cloud/network/lb/LoadBalancingRulesManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public interface LoadBalancingRulesManager {
3535

3636
LoadBalancer createPublicLoadBalancer(String xId, String name, String description, int srcPort, int destPort, long sourceIpId, String protocol, String algorithm,
37-
boolean openFirewall, CallContext caller, String lbProtocol, Boolean forDisplay) throws NetworkRuleConflictException;
37+
boolean openFirewall, CallContext caller, String lbProtocol, Boolean forDisplay, String cidrList) throws NetworkRuleConflictException;
3838

3939
boolean removeAllLoadBalanacersForIp(long ipId, Account caller, long callerUserId);
4040

engine/schema/src/main/java/com/cloud/network/dao/LoadBalancerVO.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import com.cloud.utils.net.NetUtils;
3030

3131
/**
32-
* This VO represent Public Load Balancer
32+
* This VO represents Public Load Balancer
3333
* It references source ip address by its Id.
3434
* To get the VO for Internal Load Balancer rule, please refer to LoadBalancerRuleVO
3535
*
@@ -62,11 +62,14 @@ public class LoadBalancerVO extends FirewallRuleVO implements LoadBalancer {
6262
@Column(name = "lb_protocol")
6363
String lbProtocol;
6464

65+
@Column(name = "cidr_list")
66+
String cidrList;
67+
6568
public LoadBalancerVO() {
6669
}
6770

6871
public LoadBalancerVO(String xId, String name, String description, long srcIpId, int srcPort, int dstPort, String algorithm, long networkId, long accountId,
69-
long domainId, String lbProtocol) {
72+
long domainId, String lbProtocol, String cidrList) {
7073
super(xId, srcIpId, srcPort, NetUtils.TCP_PROTO, networkId, accountId, domainId, Purpose.LoadBalancing, null, null, null, null);
7174
this.name = name;
7275
this.description = description;
@@ -75,6 +78,7 @@ public LoadBalancerVO(String xId, String name, String description, long srcIpId,
7578
this.defaultPortEnd = dstPort;
7679
this.scheme = Scheme.Public;
7780
this.lbProtocol = lbProtocol;
81+
this.cidrList = cidrList;
7882
}
7983

8084
@Override
@@ -127,4 +131,9 @@ public void setDescription(String description) {
127131
public Scheme getScheme() {
128132
return scheme;
129133
}
134+
135+
@Override
136+
public String getCidrList() {
137+
return cidrList;
138+
}
130139
}

0 commit comments

Comments
 (0)