Skip to content

Commit 56b69b1

Browse files
prevent duplicate ip table rules in SSVM (apache#8530)
Co-authored-by: Wei Zhou <[email protected]>
1 parent 4e7c668 commit 56b69b1

File tree

6 files changed

+105
-38
lines changed

6 files changed

+105
-38
lines changed

services/secondary-storage/controller/src/main/java/org/apache/cloudstack/secondarystorage/SecondaryStorageManagerImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ public SecondaryStorageVmVO startNew(long dataCenterId, SecondaryStorageVm.Role
531531

532532
/**
533533
* Get the default network for the secondary storage VM, based on the zone it is in. Delegates to
534-
* either {@link #getDefaultNetworkForZone(DataCenter)} or {@link #getDefaultNetworkForAdvancedSGZone(DataCenter)},
535-
* depending on the zone network type and whether or not security groups are enabled in the zone.
534+
* either {@link #getDefaultNetworkForAdvancedZone(DataCenter)} or {@link #getDefaultNetworkForBasicZone(DataCenter)},
535+
* depending on the zone network type and whether security groups are enabled in the zone.
536536
* @param dc - The zone (DataCenter) of the secondary storage VM.
537537
* @return The default network for use with the secondary storage VM.
538538
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 org.apache.cloudstack.storage.resource;
21+
22+
import com.cloud.utils.script.Script;
23+
import org.apache.log4j.Logger;
24+
25+
public class IpTablesHelper {
26+
public static final Logger LOGGER = Logger.getLogger(IpTablesHelper.class);
27+
28+
public static final String OUTPUT_CHAIN = "OUTPUT";
29+
public static final String INPUT_CHAIN = "INPUT";
30+
public static final String INSERT = " -I ";
31+
public static final String APPEND = " -A ";
32+
33+
public static boolean needsAdding(String chain, String rule) {
34+
Script command = new Script("/bin/bash", LOGGER);
35+
command.add("-c");
36+
command.add("iptables -C " + chain + " " + rule);
37+
38+
String commandOutput = command.execute();
39+
boolean needsAdding = (commandOutput != null && commandOutput.contains("iptables: Bad rule (does a matching rule exist in that chain?)."));
40+
LOGGER.debug(String.format("Rule [%s], %s need adding to [%s] : %s",
41+
rule,
42+
needsAdding ? "does indeed" : "doesn't",
43+
chain,
44+
commandOutput
45+
));
46+
return needsAdding;
47+
}
48+
49+
public static String addConditionally(String chain, boolean insert, String rule, String errMsg) {
50+
LOGGER.info(String.format("Adding rule [%s] to [%s] if required.", rule, chain));
51+
if (needsAdding(chain, rule)) {
52+
Script command = new Script("/bin/bash", LOGGER);
53+
command.add("-c");
54+
command.add("iptables" + (insert ? INSERT : APPEND) + chain + " " + rule);
55+
String result = command.execute();
56+
LOGGER.debug(String.format("Executed [%s] with result [%s]", command, result));
57+
if (result != null) {
58+
LOGGER.warn(String.format("%s , err = %s", errMsg, result));
59+
return errMsg + result;
60+
}
61+
} else {
62+
LOGGER.warn("Rule already defined in SVM: " + rule);
63+
}
64+
return null;
65+
}
66+
}

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,15 +2287,14 @@ public synchronized String allowOutgoingOnPrivate(String destCidr) {
22872287
if (!_inSystemVM) {
22882288
return null;
22892289
}
2290-
Script command = new Script("/bin/bash", s_logger);
22912290
String intf = "eth1";
2292-
command.add("-c");
2293-
command.add("iptables -I OUTPUT -o " + intf + " -d " + destCidr + " -p tcp -m state --state NEW -m tcp -j ACCEPT");
2291+
String rule = String.format("-o %s -d %s -p tcp -m state --state NEW -m tcp -j ACCEPT", intf, destCidr);
2292+
String errMsg = String.format("Error in allowing outgoing to %s", destCidr);
22942293

2295-
String result = command.execute();
2294+
s_logger.info(String.format("Adding rule if required: " + rule));
2295+
String result = IpTablesHelper.addConditionally(IpTablesHelper.OUTPUT_CHAIN, true, rule, errMsg);
22962296
if (result != null) {
2297-
s_logger.warn("Error in allowing outgoing to " + destCidr + ", err=" + result);
2298-
return "Error in allowing outgoing to " + destCidr + ", err=" + result;
2297+
return result;
22992298
}
23002299

23012300
addRouteToInternalIpOrCidr(_localgw, _eth1ip, _eth1mask, destCidr);
@@ -2832,13 +2831,8 @@ private void startAdditionalServices() {
28322831
if (result != null) {
28332832
s_logger.warn("Error in starting sshd service err=" + result);
28342833
}
2835-
command = new Script("/bin/bash", s_logger);
2836-
command.add("-c");
2837-
command.add("iptables -I INPUT -i eth1 -p tcp -m state --state NEW -m tcp --dport 3922 -j ACCEPT");
2838-
result = command.execute();
2839-
if (result != null) {
2840-
s_logger.warn("Error in opening up ssh port err=" + result);
2841-
}
2834+
String rule = "-i eth1 -p tcp -m state --state NEW -m tcp --dport 3922 -j ACCEPT";
2835+
IpTablesHelper.addConditionally(IpTablesHelper.INPUT_CHAIN, true, rule, "Error in opening up ssh port");
28422836
}
28432837

28442838
private void addRouteToInternalIpOrCidr(String localgw, String eth1ip, String eth1mask, String destIpOrCidr) {

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/DownloadManagerImpl.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import org.apache.cloudstack.storage.command.DownloadProgressCommand;
6161
import org.apache.cloudstack.storage.command.DownloadProgressCommand.RequestType;
6262
import org.apache.cloudstack.storage.NfsMountManagerImpl.PathParser;
63+
import org.apache.cloudstack.storage.resource.IpTablesHelper;
6364
import org.apache.cloudstack.storage.resource.NfsSecondaryStorageResource;
6465
import org.apache.cloudstack.storage.resource.SecondaryStorageResource;
6566
import org.apache.log4j.Logger;
@@ -1092,17 +1093,14 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
10921093
}
10931094

10941095
private void blockOutgoingOnPrivate() {
1095-
Script command = new Script("/bin/bash", LOGGER);
1096-
String intf = "eth1";
1097-
command.add("-c");
1098-
command.add("iptables -A OUTPUT -o " + intf + " -p tcp -m state --state NEW -m tcp --dport " + "80" + " -j REJECT;" + "iptables -A OUTPUT -o " + intf +
1099-
" -p tcp -m state --state NEW -m tcp --dport " + "443" + " -j REJECT;");
1100-
1101-
String result = command.execute();
1102-
if (result != null) {
1103-
LOGGER.warn("Error in blocking outgoing to port 80/443 err=" + result);
1104-
return;
1105-
}
1096+
IpTablesHelper.addConditionally(IpTablesHelper.OUTPUT_CHAIN
1097+
, false
1098+
, "-o " + TemplateConstants.TMPLT_COPY_INTF_PRIVATE + " -p tcp -m state --state NEW -m tcp --dport 80 -j REJECT;"
1099+
, "Error in blocking outgoing to port 80");
1100+
IpTablesHelper.addConditionally(IpTablesHelper.OUTPUT_CHAIN
1101+
, false
1102+
, "-o " + TemplateConstants.TMPLT_COPY_INTF_PRIVATE + " -p tcp -m state --state NEW -m tcp --dport 443 -j REJECT;"
1103+
, "Error in blocking outgoing to port 443");
11061104
}
11071105

11081106
@Override
@@ -1128,17 +1126,19 @@ private void startAdditionalServices() {
11281126
if (result != null) {
11291127
LOGGER.warn("Error in stopping httpd service err=" + result);
11301128
}
1131-
String port = Integer.toString(TemplateConstants.DEFAULT_TMPLT_COPY_PORT);
1132-
String intf = TemplateConstants.DEFAULT_TMPLT_COPY_INTF;
11331129

1134-
command = new Script("/bin/bash", LOGGER);
1135-
command.add("-c");
1136-
command.add("iptables -I INPUT -i " + intf + " -p tcp -m state --state NEW -m tcp --dport " + port + " -j ACCEPT;" + "iptables -I INPUT -i " + intf +
1137-
" -p tcp -m state --state NEW -m tcp --dport " + "443" + " -j ACCEPT;");
1138-
1139-
result = command.execute();
1130+
result = IpTablesHelper.addConditionally(IpTablesHelper.INPUT_CHAIN
1131+
, true
1132+
, "-i " + TemplateConstants.DEFAULT_TMPLT_COPY_INTF + " -p tcp -m state --state NEW -m tcp --dport " + TemplateConstants.DEFAULT_TMPLT_COPY_PORT + " -j ACCEPT"
1133+
, "Error in opening up apache2 port " + TemplateConstants.TMPLT_COPY_INTF_PRIVATE);
1134+
if (result != null) {
1135+
return;
1136+
}
1137+
result = IpTablesHelper.addConditionally(IpTablesHelper.INPUT_CHAIN
1138+
, true
1139+
, "-i " + TemplateConstants.DEFAULT_TMPLT_COPY_INTF + " -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT;"
1140+
, "Error in opening up apache2 port 443");
11401141
if (result != null) {
1141-
LOGGER.warn("Error in opening up apache2 port err=" + result);
11421142
return;
11431143
}
11441144

systemvm/debian/opt/cloud/bin/cs/CsHelper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ def save_iptables(command, iptables_file):
221221

222222
def execute2(command, wait=True):
223223
""" Execute command """
224-
logging.info("Executing: %s" % command)
224+
logging.info("Executing2: %s" % command)
225225
p = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
226226
if wait:
227227
p.wait()

utils/src/main/java/com/cloud/utils/script/Script.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,19 @@ public String execute(OutputInterpreter interpreter) {
243243
//process completed successfully
244244
if (_process.exitValue() == 0) {
245245
_logger.debug("Execution is successful.");
246+
String result;
247+
String method;
246248
if (interpreter != null) {
247-
return interpreter.drain() ? task.getResult() : interpreter.interpret(ir);
249+
_logger.debug("interpreting the result...");
250+
method = "result interpretation of execution: ";
251+
result= interpreter.drain() ? task.getResult() : interpreter.interpret(ir);
248252
} else {
249253
// null return exitValue apparently
250-
return String.valueOf(_process.exitValue());
254+
method = "return code of execution: ";
255+
result = String.valueOf(_process.exitValue());
251256
}
257+
_logger.debug(method + result);
258+
return result;
252259
} else { //process failed
253260
break;
254261
}

0 commit comments

Comments
 (0)