|
| 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 | +} |
0 commit comments