use of org.batfish.representation.host.HostConfiguration in project batfish by batfish.
the class Batfish method readIptableFiles.
/**
* Read Iptable Files for each host in the keyset of {@code hostConfigurations}, and store the
* contents in {@code iptablesDate}. Each task fails if the Iptable file specified by host is not
* under {@code testRigPath} or does not exist.
*
* @throws BatfishException if there is a failed task and either {@link
* Settings#getExitOnFirstError()} or {@link Settings#getHaltOnParseError()} is set.
*/
void readIptableFiles(Path testRigPath, SortedMap<String, VendorConfiguration> hostConfigurations, SortedMap<Path, String> iptablesData, ParseVendorConfigurationAnswerElement answerElement) {
List<BatfishException> failureCauses = new ArrayList<>();
for (VendorConfiguration vc : hostConfigurations.values()) {
HostConfiguration hostConfig = (HostConfiguration) vc;
if (hostConfig.getIptablesFile() != null) {
Path path = Paths.get(testRigPath.toString(), hostConfig.getIptablesFile());
// testrig
try {
if (!path.toFile().getCanonicalPath().contains(testRigPath.toFile().getCanonicalPath()) || !path.toFile().exists()) {
String failureMessage = String.format("Iptables file %s for host %s is not contained within the testrig", hostConfig.getIptablesFile(), hostConfig.getHostname());
BatfishException bfc;
if (answerElement.getErrors().containsKey(hostConfig.getHostname())) {
bfc = new BatfishException(failureMessage, answerElement.getErrors().get(hostConfig.getHostname()).getException());
answerElement.getErrors().put(hostConfig.getHostname(), bfc.getBatfishStackTrace());
} else {
bfc = new BatfishException(failureMessage);
if (_settings.getExitOnFirstError()) {
throw bfc;
} else {
failureCauses.add(bfc);
answerElement.getErrors().put(hostConfig.getHostname(), bfc.getBatfishStackTrace());
answerElement.getParseStatus().put(hostConfig.getHostname(), ParseStatus.FAILED);
}
}
} else {
String fileText = CommonUtil.readFile(path);
iptablesData.put(path, fileText);
}
} catch (IOException e) {
throw new BatfishException("Could not get canonical path", e);
}
}
}
if (_settings.getHaltOnParseError() && !failureCauses.isEmpty()) {
BatfishException e = new BatfishException("Fatal exception due to at least one Iptables file is" + " not contained within the testrig");
failureCauses.forEach(e::addSuppressed);
throw e;
}
}
use of org.batfish.representation.host.HostConfiguration in project batfish by batfish.
the class Batfish method serializeHostConfigs.
private SortedMap<String, VendorConfiguration> serializeHostConfigs(Path testRigPath, Path outputPath, ParseVendorConfigurationAnswerElement answerElement) {
SortedMap<Path, String> configurationData = readConfigurationFiles(testRigPath, BfConsts.RELPATH_HOST_CONFIGS_DIR);
// read the host files
SortedMap<String, VendorConfiguration> allHostConfigurations;
try (ActiveSpan parseHostConfigsSpan = GlobalTracer.get().buildSpan("Parse host configs").startActive()) {
// avoid unused warning
assert parseHostConfigsSpan != null;
allHostConfigurations = parseVendorConfigurations(configurationData, answerElement, ConfigurationFormat.HOST);
}
if (allHostConfigurations == null) {
throw new BatfishException("Exiting due to parser errors");
}
_logger.infof("Testrig:%s in container:%s has total number of host configs:%d", getTestrigName(), getContainerName(), allHostConfigurations.size());
// split into hostConfigurations and overlayConfigurations
SortedMap<String, VendorConfiguration> overlayConfigurations = allHostConfigurations.entrySet().stream().filter(e -> ((HostConfiguration) e.getValue()).getOverlay()).collect(toMap(Entry::getKey, Entry::getValue, (v1, v2) -> v1, TreeMap::new));
SortedMap<String, VendorConfiguration> nonOverlayHostConfigurations = allHostConfigurations.entrySet().stream().filter(e -> !((HostConfiguration) e.getValue()).getOverlay()).collect(toMap(Entry::getKey, Entry::getValue, (v1, v2) -> v1, TreeMap::new));
// read and associate iptables files for specified hosts
SortedMap<Path, String> iptablesData = new TreeMap<>();
readIptableFiles(testRigPath, allHostConfigurations, iptablesData, answerElement);
SortedMap<String, VendorConfiguration> iptablesConfigurations = parseVendorConfigurations(iptablesData, answerElement, ConfigurationFormat.IPTABLES);
for (VendorConfiguration vc : allHostConfigurations.values()) {
HostConfiguration hostConfig = (HostConfiguration) vc;
if (hostConfig.getIptablesFile() != null) {
Path path = Paths.get(testRigPath.toString(), hostConfig.getIptablesFile());
String relativePathStr = _testrigSettings.getBasePath().relativize(path).toString();
if (iptablesConfigurations.containsKey(relativePathStr)) {
hostConfig.setIptablesVendorConfig((IptablesVendorConfiguration) iptablesConfigurations.get(relativePathStr));
}
}
}
// now, serialize
_logger.info("\n*** SERIALIZING VENDOR CONFIGURATION STRUCTURES ***\n");
_logger.resetTimer();
CommonUtil.createDirectories(outputPath);
Map<Path, VendorConfiguration> output = new TreeMap<>();
nonOverlayHostConfigurations.forEach((name, vc) -> {
Path currentOutputPath = outputPath.resolve(name);
output.put(currentOutputPath, vc);
});
serializeObjects(output);
// serialize warnings
serializeObject(answerElement, _testrigSettings.getParseAnswerPath());
_logger.printElapsedTime();
return overlayConfigurations;
}
use of org.batfish.representation.host.HostConfiguration in project batfish by batfish.
the class ConvertConfigurationJob method call.
@Override
public ConvertConfigurationResult call() {
long startTime = System.currentTimeMillis();
long elapsedTime;
_logger.infof("Processing: \"%s\"", _name);
Map<String, Configuration> configurations = new HashMap<>();
Map<String, Warnings> warningsByHost = new HashMap<>();
ConvertConfigurationAnswerElement answerElement = new ConvertConfigurationAnswerElement();
try {
// We have only two options: AWS VPCs or router configs
if (VendorConfiguration.class.isInstance(_configObject)) {
Warnings warnings = Batfish.buildWarnings(_settings);
VendorConfiguration vendorConfiguration = ((VendorConfiguration) _configObject);
vendorConfiguration.setWarnings(warnings);
vendorConfiguration.setAnswerElement(answerElement);
Configuration configuration = vendorConfiguration.toVendorIndependentConfiguration();
if (configuration.getDefaultCrossZoneAction() == null) {
throw new BatfishException("Implementation error: missing default cross-zone action for host: '" + configuration.getHostname() + "'");
}
if (configuration.getDefaultInboundAction() == null) {
throw new BatfishException("Implementation error: missing default inbound action for host: '" + configuration.getHostname() + "'");
}
// get iptables if applicable
IptablesVendorConfiguration iptablesConfig = null;
VendorConfiguration ov = vendorConfiguration.getOverlayConfiguration();
if (ov != null) {
// apply overlay
HostConfiguration oh = (HostConfiguration) ov;
iptablesConfig = oh.getIptablesVendorConfig();
} else if (vendorConfiguration instanceof HostConfiguration) {
// TODO: To enable below, we need to reconcile overlay and non-overlay iptables semantics.
// HostConfiguration oh = (HostConfiguration)vendorConfiguration;
// iptablesConfig = oh.getIptablesVendorConfig();
}
if (iptablesConfig != null) {
iptablesConfig.addAsIpAccessLists(configuration, vendorConfiguration, warnings);
iptablesConfig.applyAsOverlay(configuration, warnings);
}
configurations.put(_name, configuration);
warningsByHost.put(_name, warnings);
} else {
configurations = ((AwsConfiguration) _configObject).toConfigurations(_settings, warningsByHost);
}
_logger.info(" ...OK\n");
} catch (Exception e) {
String error = "Conversion error for node with hostname '" + _name + "'";
elapsedTime = System.currentTimeMillis() - startTime;
return new ConvertConfigurationResult(elapsedTime, _logger.getHistory(), _name, new BatfishException(error, e));
} finally {
warningsByHost.forEach((hostname, warnings) -> Batfish.logWarnings(_logger, warnings));
;
}
elapsedTime = System.currentTimeMillis() - startTime;
return new ConvertConfigurationResult(elapsedTime, _logger.getHistory(), warningsByHost, _name, configurations, answerElement);
}
use of org.batfish.representation.host.HostConfiguration in project batfish by batfish.
the class BatfishTest method testReadMissingIptableFile.
@Test
public void testReadMissingIptableFile() throws IOException {
HostConfiguration host1 = new HostConfiguration();
host1.setHostname("host1");
host1.setIptablesFile(Paths.get("iptables").resolve("host1.iptables").toString());
SortedMap<String, VendorConfiguration> hostConfigurations = new TreeMap<>();
hostConfigurations.put("host1", host1);
SortedMap<Path, String> iptablesData = new TreeMap<>();
Path testRigPath = _folder.newFolder("testrig").toPath();
ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
answerElement.getParseStatus().put("host1", ParseStatus.PASSED);
Batfish batfish = BatfishTestUtils.getBatfish(new TreeMap<>(), _folder);
String failureMessage = "Iptables file iptables" + File.separator + "host1.iptables for host host1 is not contained within the testrig";
batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
assertThat(answerElement.getParseStatus().get("host1"), equalTo(ParseStatus.FAILED));
assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString(failureMessage));
// When host file failed, verify that error message contains both failure messages
answerElement.getErrors().clear();
answerElement.getErrors().put("host1", new BatfishException("Failed to parse host file: host1").getBatfishStackTrace());
batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString(failureMessage));
assertThat(answerElement.getErrors().get("host1").prettyPrint(), containsString("Failed to parse host file: host1"));
// When the haltonparseerror flag is set to true
batfish.getSettings().setHaltOnParseError(true);
answerElement.getErrors().clear();
String parseErrorMessage = "Fatal exception due to at least one Iptables file is not contained" + " within the testrig";
_thrown.expect(BatfishException.class);
_thrown.expectMessage(parseErrorMessage);
batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
}
use of org.batfish.representation.host.HostConfiguration in project batfish by batfish.
the class BatfishTest method testReadValidIptableFile.
@Test
public void testReadValidIptableFile() throws IOException {
HostConfiguration host1 = new HostConfiguration();
host1.setHostname("host1");
Path iptablePath = Paths.get("iptables").resolve("host1.iptables");
host1.setIptablesFile(iptablePath.toString());
SortedMap<String, VendorConfiguration> hostConfigurations = new TreeMap<>();
hostConfigurations.put("host1", host1);
SortedMap<Path, String> iptablesData = new TreeMap<>();
Path testRigPath = _folder.newFolder("testrig").toPath();
File iptableFile = Paths.get(testRigPath.toString(), iptablePath.toString()).toFile();
iptableFile.getParentFile().mkdir();
assertThat(iptableFile.createNewFile(), is(true));
ParseVendorConfigurationAnswerElement answerElement = new ParseVendorConfigurationAnswerElement();
answerElement.getParseStatus().put("host1", ParseStatus.PASSED);
Batfish batfish = BatfishTestUtils.getBatfish(new TreeMap<>(), _folder);
batfish.readIptableFiles(testRigPath, hostConfigurations, iptablesData, answerElement);
assertThat(answerElement.getParseStatus().get("host1"), equalTo(ParseStatus.PASSED));
assertThat(answerElement.getErrors().size(), is(0));
}
Aggregations