summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSimon Rettberg2023-04-26 15:13:26 +0200
committerSimon Rettberg2023-04-26 15:49:24 +0200
commit577654954b3ea5bf54b5955ec57440e21db66e87 (patch)
tree81f4b0774fed71ff01ffd305d71b9fa1ac7586c0
parent[qemu] Fix NPE (diff)
downloadmaster-sync-shared-577654954b3ea5bf54b5955ec57440e21db66e87.tar.gz
master-sync-shared-577654954b3ea5bf54b5955ec57440e21db66e87.tar.xz
master-sync-shared-577654954b3ea5bf54b5955ec57440e21db66e87.zip
[libvirt] Reintroduce a very basic sanity check in Domain constructor
This is to avoid false positives when we do virtualizer detection by handing the selected config file to every virtualizer class. Currently this should not matter since VBox comes before libvirt, but this might change in the future.
-rw-r--r--src/main/java/org/openslx/libvirt/domain/Domain.java28
-rw-r--r--src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java5
2 files changed, 31 insertions, 2 deletions
diff --git a/src/main/java/org/openslx/libvirt/domain/Domain.java b/src/main/java/org/openslx/libvirt/domain/Domain.java
index 6563079..61d3bcc 100644
--- a/src/main/java/org/openslx/libvirt/domain/Domain.java
+++ b/src/main/java/org/openslx/libvirt/domain/Domain.java
@@ -9,6 +9,9 @@ import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.xml.XMLConstants;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
import org.openslx.libvirt.domain.device.Controller;
import org.openslx.libvirt.domain.device.ControllerFloppy;
@@ -49,8 +52,6 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
-import org.w3c.dom.ls.DOMImplementationLS;
-import org.w3c.dom.ls.LSSerializer;
import org.xml.sax.InputSource;
/**
@@ -86,6 +87,7 @@ public class Domain extends LibvirtXmlDocument
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
super( xml, LibvirtXmlResources.getLibvirtRng( "domain.rng" ) );
+ this.assertDomainType();
}
/**
@@ -101,6 +103,7 @@ public class Domain extends LibvirtXmlDocument
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
super( xml, LibvirtXmlResources.getLibvirtRng( "domain.rng" ) );
+ this.assertDomainType();
}
/**
@@ -117,6 +120,7 @@ public class Domain extends LibvirtXmlDocument
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
super( xml, LibvirtXmlResources.getLibvirtRng( "domain.rng" ) );
+ this.assertDomainType();
}
/**
@@ -133,6 +137,26 @@ public class Domain extends LibvirtXmlDocument
throws LibvirtXmlDocumentException, LibvirtXmlSerializationException, LibvirtXmlValidationException
{
super( xml, LibvirtXmlResources.getLibvirtRng( "domain.rng" ) );
+ this.assertDomainType();
+ }
+
+ /**
+ * Quick sanity check whether this could be a libvirt domain file at all.
+ * We just check if the root node is called domain and has the attribute type.
+ *
+ * @throws LibvirtXmlValidationException If this is not met.
+ */
+ private void assertDomainType() throws LibvirtXmlValidationException
+ {
+ try {
+ XPathExpression expr = XmlHelper.compileXPath( "/domain[@type]" );
+ Object nodesObject = expr.evaluate( this.getRootXmlNode().getXmlDocument(), XPathConstants.NODESET );
+ NodeList nodes = (NodeList)nodesObject;
+ if ( nodes.getLength() == 0 )
+ throw new LibvirtXmlValidationException( "Root element isn't <domain type=...>" );
+ } catch ( XPathExpressionException e ) {
+ e.printStackTrace();
+ }
}
/**
diff --git a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java
index 06f8ba8..2d657e4 100644
--- a/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java
+++ b/src/main/java/org/openslx/libvirt/xml/LibvirtXmlDocument.java
@@ -18,8 +18,13 @@ import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathExpression;
+import javax.xml.xpath.XPathExpressionException;
+import org.openslx.util.XmlHelper;
import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;