Skip to content

Commit 4189bac

Browse files
RodrigoDLopezLopezwinterhazel
authored
enable to create VPC portfowarding rules with source cidr (#7081)
Co-authored-by: Lopez <[email protected]> Co-authored-by: Fabricio Duarte <[email protected]>
1 parent a8cb7ab commit 4189bac

File tree

25 files changed

+396
-67
lines changed

25 files changed

+396
-67
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import com.cloud.network.rules.FirewallRule;
2020
import com.cloud.network.rules.PortForwardingRule;
2121
import com.cloud.utils.net.NetUtils;
22+
import org.apache.commons.lang3.StringUtils;
23+
24+
import java.util.List;
2225

2326
/**
2427
* PortForwardingRuleTO specifies one port forwarding rule.
@@ -29,6 +32,8 @@ public class PortForwardingRuleTO extends FirewallRuleTO {
2932
String dstIp;
3033
int[] dstPortRange;
3134

35+
List<String> sourceCidrList;
36+
3237
protected PortForwardingRuleTO() {
3338
super();
3439
}
@@ -37,6 +42,7 @@ public PortForwardingRuleTO(PortForwardingRule rule, String srcVlanTag, String s
3742
super(rule, srcVlanTag, srcIp);
3843
this.dstIp = rule.getDestinationIpAddress().addr();
3944
this.dstPortRange = new int[] {rule.getDestinationPortStart(), rule.getDestinationPortEnd()};
45+
this.sourceCidrList = rule.getSourceCidrList();
4046
}
4147

4248
public PortForwardingRuleTO(long id, String srcIp, int srcPortStart, int srcPortEnd, String dstIp, int dstPortStart, int dstPortEnd, String protocol,
@@ -58,4 +64,11 @@ public String getStringDstPortRange() {
5864
return NetUtils.portRangeToString(dstPortRange);
5965
}
6066

67+
public String getSourceCidrListAsString() {
68+
if (sourceCidrList != null) {
69+
return StringUtils.join(sourceCidrList, ",");
70+
}
71+
return null;
72+
}
73+
6174
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.cloud.user.Account;
2727
import com.cloud.utils.Pair;
2828
import com.cloud.utils.net.Ip;
29+
import org.apache.cloudstack.api.command.user.firewall.UpdatePortForwardingRuleCmd;
2930

3031
public interface RulesService {
3132
Pair<List<? extends FirewallRule>, Integer> searchStaticNatRules(Long ipId, Long id, Long vmId, Long start, Long size, String accountName, Long domainId,
@@ -81,6 +82,8 @@ Pair<List<? extends FirewallRule>, Integer> searchStaticNatRules(Long ipId, Long
8182

8283
boolean disableStaticNat(long ipId) throws ResourceUnavailableException, NetworkRuleConflictException, InsufficientAddressCapacityException;
8384

84-
PortForwardingRule updatePortForwardingRule(long id, Integer privatePort, Integer privateEndPort, Long virtualMachineId, Ip vmGuestIp, String customId, Boolean forDisplay);
85+
PortForwardingRule updatePortForwardingRule(UpdatePortForwardingRuleCmd cmd);
86+
87+
void validatePortForwardingSourceCidrList(List<String> sourceCidrList);
8588

8689
}

api/src/main/java/org/apache/cloudstack/api/ApiConstants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ public class ApiConstants {
427427
public static final String SNAPSHOT_TYPE = "snapshottype";
428428
public static final String SNAPSHOT_QUIESCEVM = "quiescevm";
429429
public static final String SUPPORTS_STORAGE_SNAPSHOT = "supportsstoragesnapshot";
430+
public static final String SOURCE_CIDR_LIST = "sourcecidrlist";
430431
public static final String SOURCE_ZONE_ID = "sourcezoneid";
431432
public static final String START_DATE = "startdate";
432433
public static final String START_ID = "startid";

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ public class CreatePortForwardingRuleCmd extends BaseAsyncCreateCmd implements P
107107
description = "the ID of the virtual machine for the port forwarding rule")
108108
private Long virtualMachineId;
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.")
111-
private List<String> cidrlist;
110+
@Parameter(name = ApiConstants.CIDR_LIST,
111+
type = CommandType.LIST,
112+
collectionType = CommandType.STRING,
113+
description = " the source CIDR list to allow traffic from; all other CIDRs will be blocked. " +
114+
"Multiple entries must be separated by a single comma character (,). This param will be used only for VPC tiers. By default, all CIDRs are allowed.")
115+
private List<String> sourceCidrList;
112116

113117
@Parameter(name = ApiConstants.OPEN_FIREWALL, type = CommandType.BOOLEAN, description = "if true, firewall rule for source/end public port is automatically created; "
114118
+ "if false - firewall rule has to be created explicitly. If not specified 1) defaulted to false when PF"
@@ -157,11 +161,7 @@ public long getVirtualMachineId() {
157161

158162
@Override
159163
public List<String> getSourceCidrList() {
160-
if (cidrlist != null) {
161-
throw new InvalidParameterValueException("Parameter cidrList is deprecated; if you need to open firewall "
162-
+ "rule for the specific cidr, please refer to createFirewallRule command");
163-
}
164-
return null;
164+
return sourceCidrList;
165165
}
166166

167167
public Boolean getOpenFirewall() {
@@ -334,19 +334,15 @@ public int getDestinationPortEnd() {
334334

335335
@Override
336336
public void create() {
337-
// cidr list parameter is deprecated
338-
if (cidrlist != null) {
339-
throw new InvalidParameterValueException(
340-
"Parameter cidrList is deprecated; if you need to open firewall rule for the specific cidr, please refer to createFirewallRule command");
341-
}
342-
343337
Ip privateIp = getVmSecondaryIp();
344338
if (privateIp != null) {
345339
if (!NetUtils.isValidIp4(privateIp.toString())) {
346340
throw new InvalidParameterValueException("Invalid vm ip address");
347341
}
348342
}
349343

344+
_rulesService.validatePortForwardingSourceCidrList(sourceCidrList);
345+
350346
try {
351347
PortForwardingRule result = _rulesService.createPortForwardingRule(this, virtualMachineId, privateIp, getOpenFirewall(), isDisplay());
352348
setEntityId(result.getId());

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import com.cloud.user.Account;
3434
import com.cloud.utils.net.Ip;
3535

36+
import java.util.List;
37+
3638
@APICommand(name = "updatePortForwardingRule",
3739
responseObject = FirewallRuleResponse.class,
3840
description = "Updates a port forwarding rule. Only the private port and the virtual machine can be updated.", entityType = {PortForwardingRule.class},
@@ -65,6 +67,13 @@ public class UpdatePortForwardingRuleCmd extends BaseAsyncCustomIdCmd {
6567
@Parameter(name = ApiConstants.FOR_DISPLAY, type = CommandType.BOOLEAN, description = "an optional field, whether to the display the rule to the end user or not", since = "4.4", authorized = {RoleType.Admin})
6668
private Boolean display;
6769

70+
@Parameter(name = ApiConstants.CIDR_LIST,
71+
type = CommandType.LIST,
72+
collectionType = CommandType.STRING,
73+
description = " the source CIDR list to allow traffic from; all other CIDRs will be blocked. " +
74+
"Multiple entries must be separated by a single comma character (,). This param will be used only for VPC tiers. By default, all CIDRs are allowed.")
75+
private List<String> sourceCidrList;
76+
6877
/////////////////////////////////////////////////////
6978
/////////////////// Accessors ///////////////////////
7079
/////////////////////////////////////////////////////
@@ -96,6 +105,10 @@ public Boolean getDisplay() {
96105
return display;
97106
}
98107

108+
public List<String> getSourceCidrList() {
109+
return sourceCidrList;
110+
}
111+
99112
/////////////////////////////////////////////////////
100113
/////////////// API Implementation///////////////////
101114
/////////////////////////////////////////////////////
@@ -132,7 +145,7 @@ public void checkUuid() {
132145

133146
@Override
134147
public void execute() {
135-
PortForwardingRule rule = _rulesService.updatePortForwardingRule(getId(), getPrivatePort(), getPrivateEndPort(), getVirtualMachineId(), getVmGuestIp(), getCustomId(), getDisplay());
148+
PortForwardingRule rule = _rulesService.updatePortForwardingRule(this);
136149
FirewallRuleResponse fwResponse = new FirewallRuleResponse();
137150
if (rule != null) {
138151
fwResponse = _responseGenerator.createPortForwardingRuleResponse(rule);

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

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

109-
@Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, since = "4.18.0.0", description = "the CIDR list to allow traffic, "
109+
@Parameter(name = ApiConstants.CIDR_LIST, type = CommandType.LIST, collectionType = CommandType.STRING, since = "4.18.0.0", description = "the source CIDR list to allow traffic from; "
110110
+ "all other CIDRs will be blocked. Multiple entries must be separated by a single comma character (,). By default, all CIDRs are allowed.")
111111
private List<String> cidrlist;
112112

core/src/main/java/com/cloud/agent/resource/virtualnetwork/facade/SetPortForwardingRulesConfigItem.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public List<ConfigItem> generateConfig(final NetworkElementCommand cmd) {
4141

4242
for (final PortForwardingRuleTO rule : command.getRules()) {
4343
final ForwardingRule fwdRule = new ForwardingRule(rule.revoked(), rule.getProtocol().toLowerCase(), rule.getSrcIp(), rule.getStringSrcPortRange(), rule.getDstIp(),
44-
rule.getStringDstPortRange());
44+
rule.getStringDstPortRange(), rule.getSourceCidrListAsString());
4545
rules.add(fwdRule);
4646
}
4747

core/src/main/java/com/cloud/agent/resource/virtualnetwork/model/ForwardingRule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,21 @@ public class ForwardingRule {
2626
private String sourcePortRange;
2727
private String destinationIpAddress;
2828
private String destinationPortRange;
29+
private String sourceCidrList;
2930

3031
public ForwardingRule() {
3132
// Empty constructor for (de)serialization
3233
}
3334

34-
public ForwardingRule(boolean revoke, String protocol, String sourceIpAddress, String sourcePortRange, String destinationIpAddress, String destinationPortRange) {
35+
public ForwardingRule(boolean revoke, String protocol, String sourceIpAddress, String sourcePortRange, String destinationIpAddress, String destinationPortRange,
36+
String sourceCidrList) {
3537
this.revoke = revoke;
3638
this.protocol = protocol;
3739
this.sourceIpAddress = sourceIpAddress;
3840
this.sourcePortRange = sourcePortRange;
3941
this.destinationIpAddress = destinationIpAddress;
4042
this.destinationPortRange = destinationPortRange;
43+
this.sourceCidrList = sourceCidrList;
4144
}
4245

4346
public boolean isRevoke() {
@@ -88,4 +91,8 @@ public void setDestinationPortRange(String destinationPortRange) {
8891
this.destinationPortRange = destinationPortRange;
8992
}
9093

94+
public String getSourceCidrList() {
95+
return sourceCidrList;
96+
}
97+
9198
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,6 @@ public interface FirewallRulesCidrsDao extends GenericDao<FirewallRulesCidrsVO,
2929

3030
@DB
3131
List<FirewallRulesCidrsVO> listByFirewallRuleId(long firewallRuleId);
32+
33+
void updateSourceCidrsForRule(Long firewallRuleId, List<String> sourceCidrList);
3234
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.List;
2121

2222

23+
import org.apache.commons.collections4.CollectionUtils;
2324
import org.apache.log4j.Logger;
2425
import org.springframework.stereotype.Component;
2526

@@ -65,9 +66,27 @@ public List<FirewallRulesCidrsVO> listByFirewallRuleId(long firewallRuleId) {
6566
return results;
6667
}
6768

69+
@Override
70+
public void updateSourceCidrsForRule(Long firewallRuleId, List<String> sourceCidrList) {
71+
TransactionLegacy txn = TransactionLegacy.currentTxn();
72+
txn.start();
73+
74+
SearchCriteria<FirewallRulesCidrsVO> sc = CidrsSearch.create();
75+
sc.setParameters("firewallRuleId", firewallRuleId);
76+
remove(sc);
77+
78+
persist(firewallRuleId, sourceCidrList);
79+
80+
txn.commit();
81+
}
82+
6883
@Override
6984
@DB
7085
public void persist(long firewallRuleId, List<String> sourceCidrs) {
86+
if (CollectionUtils.isEmpty(sourceCidrs)) {
87+
return;
88+
}
89+
7190
TransactionLegacy txn = TransactionLegacy.currentTxn();
7291

7392
txn.start();

0 commit comments

Comments
 (0)