[PATCH 0/4] interface define: add support for validation against schema

Kristina Hanicova (4): api: add virInterfaceDefineFlags conf: add validation and propagate flags into virInterfaceDefParse() src: allow validation flag in interface define virsh: add support for '--validate' option in define interface docs/manpages/virsh.rst | 5 +++-- include/libvirt/libvirt-interface.h | 4 ++++ src/conf/interface_conf.c | 14 ++++++++------ src/conf/interface_conf.h | 3 ++- src/conf/virinterfaceobj.c | 2 +- src/interface/interface_backend_netcf.c | 6 +++--- src/libvirt-interface.c | 2 +- src/test/test_driver.c | 4 ++-- tests/interfacexml2xmltest.c | 2 +- tools/virsh-interface.c | 10 +++++++++- 10 files changed, 34 insertions(+), 18 deletions(-) -- 2.31.1

Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- include/libvirt/libvirt-interface.h | 4 ++++ src/libvirt-interface.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/libvirt/libvirt-interface.h b/include/libvirt/libvirt-interface.h index 7591c6c7fb..803cb33ffe 100644 --- a/include/libvirt/libvirt-interface.h +++ b/include/libvirt/libvirt-interface.h @@ -79,6 +79,10 @@ typedef enum { VIR_INTERFACE_XML_INACTIVE = 1 << 0 /* dump inactive interface information */ } virInterfaceXMLFlags; +typedef enum { + VIR_INTERFACE_DEFINE_VALIDATE = 1 << 0, /* Validate the XML document against schema */ +} virInterfaceDefineFlags; + char * virInterfaceGetXMLDesc (virInterfacePtr iface, unsigned int flags); virInterfacePtr virInterfaceDefineXML (virConnectPtr conn, diff --git a/src/libvirt-interface.c b/src/libvirt-interface.c index 5eb5980483..2af86291d3 100644 --- a/src/libvirt-interface.c +++ b/src/libvirt-interface.c @@ -437,7 +437,7 @@ virInterfaceGetXMLDesc(virInterfacePtr iface, unsigned int flags) * virInterfaceDefineXML: * @conn: pointer to the hypervisor connection * @xml: the XML description for the interface, preferably in UTF-8 - * @flags: extra flags; not used yet, so callers should always pass 0 + * @flags: bitwise-OR of virInterfaceDefineFlags * * Define an inactive persistent physical host interface or modify an existing * persistent one from the XML description. -- 2.31.1

We need to know if validation flag is present in order to validate given XML against schema in virXMLParse(). Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- src/conf/interface_conf.c | 14 ++++++++------ src/conf/interface_conf.h | 3 ++- src/conf/virinterfaceobj.c | 2 +- src/interface/interface_backend_netcf.c | 4 ++-- src/test/test_driver.c | 2 +- tests/interfacexml2xmltest.c | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/conf/interface_conf.c b/src/conf/interface_conf.c index c371a252a8..464ff26856 100644 --- a/src/conf/interface_conf.c +++ b/src/conf/interface_conf.c @@ -685,7 +685,6 @@ virInterfaceDefParseXML(xmlXPathContextPtr ctxt, VIR_XPATH_NODE_AUTORESTORE(ctxt) xmlNodePtr lnk; - /* check @type */ tmp = virXPathString("string(./@type)", ctxt); if (tmp == NULL) { @@ -819,12 +818,14 @@ virInterfaceDefParseNode(xmlDocPtr xml, static virInterfaceDef * virInterfaceDefParse(const char *xmlStr, - const char *filename) + const char *filename, + unsigned int flags) { g_autoptr(xmlDoc) xml = NULL; virInterfaceDef *def = NULL; - if ((xml = virXMLParse(filename, xmlStr, _("(interface_definition)"), NULL, false))) { + if ((xml = virXMLParse(filename, xmlStr, _("(interface_definition)"), "interface.rng", + flags & VIR_INTERFACE_DEFINE_VALIDATE))) { def = virInterfaceDefParseNode(xml, xmlDocGetRootElement(xml)); } @@ -833,16 +834,17 @@ virInterfaceDefParse(const char *xmlStr, virInterfaceDef * -virInterfaceDefParseString(const char *xmlStr) +virInterfaceDefParseString(const char *xmlStr, + unsigned int flags) { - return virInterfaceDefParse(xmlStr, NULL); + return virInterfaceDefParse(xmlStr, NULL, flags); } virInterfaceDef * virInterfaceDefParseFile(const char *filename) { - return virInterfaceDefParse(NULL, filename); + return virInterfaceDefParse(NULL, filename, 0); } diff --git a/src/conf/interface_conf.h b/src/conf/interface_conf.h index f5e802736b..ea92e0fb31 100644 --- a/src/conf/interface_conf.h +++ b/src/conf/interface_conf.h @@ -155,7 +155,8 @@ void virInterfaceDefFree(virInterfaceDef *def); virInterfaceDef * -virInterfaceDefParseString(const char *xmlStr); +virInterfaceDefParseString(const char *xmlStr, + unsigned int flags); virInterfaceDef * virInterfaceDefParseFile(const char *filename); diff --git a/src/conf/virinterfaceobj.c b/src/conf/virinterfaceobj.c index a73208f1fc..9439bb3d0b 100644 --- a/src/conf/virinterfaceobj.c +++ b/src/conf/virinterfaceobj.c @@ -373,7 +373,7 @@ virInterfaceObjListCloneCb(void *payload, if (!(xml = virInterfaceDefFormat(srcObj->def))) goto error; - if (!(backup = virInterfaceDefParseString(xml))) + if (!(backup = virInterfaceDefParseString(xml, 0))) goto error; VIR_FREE(xml); diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interface_backend_netcf.c index 416e0af36f..9f93cdd657 100644 --- a/src/interface/interface_backend_netcf.c +++ b/src/interface/interface_backend_netcf.c @@ -862,7 +862,7 @@ static char *netcfInterfaceGetXMLDesc(virInterfacePtr ifinfo, goto cleanup; } - ifacedef = virInterfaceDefParseString(xmlstr); + ifacedef = virInterfaceDefParseString(xmlstr, 0); if (!ifacedef) { /* error was already reported */ goto cleanup; @@ -898,7 +898,7 @@ static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn, virObjectLock(driver); - ifacedef = virInterfaceDefParseString(xml); + ifacedef = virInterfaceDefParseString(xml, 0); if (!ifacedef) { /* error was already reported */ goto cleanup; diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 10a1767542..450510ee54 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -6159,7 +6159,7 @@ testInterfaceDefineXML(virConnectPtr conn, virCheckFlags(0, NULL); virObjectLock(privconn); - if ((def = virInterfaceDefParseString(xmlStr)) == NULL) + if ((def = virInterfaceDefParseString(xmlStr, 0)) == NULL) goto cleanup; if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, def)) == NULL) diff --git a/tests/interfacexml2xmltest.c b/tests/interfacexml2xmltest.c index 07d179e3a3..3785467f84 100644 --- a/tests/interfacexml2xmltest.c +++ b/tests/interfacexml2xmltest.c @@ -24,7 +24,7 @@ testCompareXMLToXMLFiles(const char *xml) if (virTestLoadFile(xml, &xmlData) < 0) goto fail; - if (!(dev = virInterfaceDefParseString(xmlData))) + if (!(dev = virInterfaceDefParseString(xmlData, 0))) goto fail; if (!(actual = virInterfaceDefFormat(dev))) -- 2.31.1

On a Friday in 2021, Kristina Hanicova wrote:
We need to know if validation flag is present in order to validate given XML against schema in virXMLParse().
Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- src/conf/interface_conf.c | 14 ++++++++------ src/conf/interface_conf.h | 3 ++- src/conf/virinterfaceobj.c | 2 +- src/interface/interface_backend_netcf.c | 4 ++-- src/test/test_driver.c | 2 +- tests/interfacexml2xmltest.c | 2 +- 6 files changed, 15 insertions(+), 12 deletions(-)
diff --git a/src/conf/interface_conf.c b/src/conf/interface_conf.c index c371a252a8..464ff26856 100644 --- a/src/conf/interface_conf.c +++ b/src/conf/interface_conf.c @@ -685,7 +685,6 @@ virInterfaceDefParseXML(xmlXPathContextPtr ctxt, VIR_XPATH_NODE_AUTORESTORE(ctxt) xmlNodePtr lnk;
- /* check @type */ tmp = virXPathString("string(./@type)", ctxt); if (tmp == NULL) {
Unrelated whitespace change Jano

We need to validate the XML against schema if option 'validate' was passed to the 'iface-define' virsh command. For that we need to allow validation flag and propagate flags to parse function. Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- src/interface/interface_backend_netcf.c | 4 ++-- src/test/test_driver.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interface_backend_netcf.c index 9f93cdd657..78fd4f9bc7 100644 --- a/src/interface/interface_backend_netcf.c +++ b/src/interface/interface_backend_netcf.c @@ -894,11 +894,11 @@ static virInterfacePtr netcfInterfaceDefineXML(virConnectPtr conn, virInterfaceDef *ifacedef = NULL; virInterfacePtr ret = NULL; - virCheckFlags(0, NULL); + virCheckFlags(VIR_INTERFACE_DEFINE_VALIDATE, NULL); virObjectLock(driver); - ifacedef = virInterfaceDefParseString(xml, 0); + ifacedef = virInterfaceDefParseString(xml, flags); if (!ifacedef) { /* error was already reported */ goto cleanup; diff --git a/src/test/test_driver.c b/src/test/test_driver.c index 450510ee54..67db02b25d 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -6156,10 +6156,10 @@ testInterfaceDefineXML(virConnectPtr conn, virInterfaceDef *objdef; virInterfacePtr ret = NULL; - virCheckFlags(0, NULL); + virCheckFlags(VIR_INTERFACE_DEFINE_VALIDATE, NULL); virObjectLock(privconn); - if ((def = virInterfaceDefParseString(xmlStr, 0)) == NULL) + if ((def = virInterfaceDefParseString(xmlStr, flags)) == NULL) goto cleanup; if ((obj = virInterfaceObjListAssignDef(privconn->ifaces, def)) == NULL) -- 2.31.1

Signed-off-by: Kristina Hanicova <khanicov@redhat.com> --- docs/manpages/virsh.rst | 5 +++-- tools/virsh-interface.c | 10 +++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/docs/manpages/virsh.rst b/docs/manpages/virsh.rst index 3eb310d02e..da812ca5b7 100644 --- a/docs/manpages/virsh.rst +++ b/docs/manpages/virsh.rst @@ -5523,10 +5523,11 @@ iface-define :: - iface-define file + iface-define file [--validate] Define an inactive persistent physical host interface or modify an existing -persistent one from the XML *file*. +persistent one from the XML *file*. Optionally, the format of the input XML +file can be validated against an internal RNG schema with *--validate*. iface-destroy diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c index 07d5f50be3..46af45c97b 100644 --- a/tools/virsh-interface.c +++ b/tools/virsh-interface.c @@ -523,6 +523,10 @@ static const vshCmdInfo info_interface_define[] = { static const vshCmdOptDef opts_interface_define[] = { VIRSH_COMMON_OPT_FILE(N_("file containing an XML interface description")), + {.name = "validate", + .type = VSH_OT_BOOL, + .help = N_("validate the XML against the schema") + }, {.name = NULL} }; @@ -533,15 +537,19 @@ cmdInterfaceDefine(vshControl *ctl, const vshCmd *cmd) const char *from = NULL; bool ret = true; g_autofree char *buffer = NULL; + unsigned int flags = 0; virshControl *priv = ctl->privData; if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) return false; + if (vshCommandOptBool(cmd, "validate")) + flags |= VIR_INTERFACE_DEFINE_VALIDATE; + if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) return false; - iface = virInterfaceDefineXML(priv->conn, buffer, 0); + iface = virInterfaceDefineXML(priv->conn, buffer, flags); if (iface != NULL) { vshPrintExtra(ctl, _("Interface %s defined from %s\n"), -- 2.31.1

On a Friday in 2021, Kristina Hanicova wrote:
Kristina Hanicova (4): api: add virInterfaceDefineFlags conf: add validation and propagate flags into virInterfaceDefParse() src: allow validation flag in interface define virsh: add support for '--validate' option in define interface
docs/manpages/virsh.rst | 5 +++-- include/libvirt/libvirt-interface.h | 4 ++++ src/conf/interface_conf.c | 14 ++++++++------ src/conf/interface_conf.h | 3 ++- src/conf/virinterfaceobj.c | 2 +- src/interface/interface_backend_netcf.c | 6 +++--- src/libvirt-interface.c | 2 +- src/test/test_driver.c | 4 ++-- tests/interfacexml2xmltest.c | 2 +- tools/virsh-interface.c | 10 +++++++++- 10 files changed, 34 insertions(+), 18 deletions(-)
Reviewed-by: Ján Tomko <jtomko@redhat.com> Jano
participants (2)
-
Ján Tomko
-
Kristina Hanicova