sysinfo: Validate in shared code, not at cli parse time

This commit is contained in:
Cole Robinson 2017-03-05 20:55:21 -05:00
parent 1757e394bb
commit 7a8f756a42
2 changed files with 34 additions and 34 deletions

View File

@ -21,7 +21,6 @@
import argparse import argparse
import collections import collections
import datetime
import logging import logging
import logging.handlers import logging.handlers
import os import os
@ -1743,41 +1742,15 @@ class ParserSYSInfo(VirtCLIParser):
else: else:
fail(_("Unknown sysinfo flag '%s'") % val) fail(_("Unknown sysinfo flag '%s'") % val)
def validate_date_cb(self, inst, val, virtarg): def set_uuid_cb(self, inst, val, virtarg):
# If supplied, date must be in either mm/dd/yy or mm/dd/yyyy format
try:
datetime.datetime.strptime(val, '%m/%d/%Y')
except ValueError:
try:
datetime.datetime.strptime(val, '%m/%d/%y')
except ValueError:
raise RuntimeError(_("SMBios date string '%s' is invalid.")
% val)
inst.bios_date = val
return val
def validate_uuid_cb(self, inst, val, virtarg):
# If a uuid is supplied it must match the guest UUID. This would be # If a uuid is supplied it must match the guest UUID. This would be
# impossible to guess if the guest uuid is autogenerated so just # impossible to guess if the guest uuid is autogenerated so just
# overwrite the guest uuid with what is passed in assuming it passes # overwrite the guest uuid with what is passed in assuming it passes
# the sanity checking below. # the sanity checking below.
try:
util.validate_uuid(val)
except ValueError:
raise ValueError(_("Invalid uuid for SMBios: %s") % val)
if util.vm_uuid_collision(self.guest.conn, val):
raise ValueError(_("UUID '%s' is in use by another guest.") %
val)
# Override guest uuid with passed in SMBios value (they must match)
self.guest.uuid = val
inst.system_uuid = val inst.system_uuid = val
self.guest.uuid = val
def _parse(self, inst): def _parse(self, inst):
if self.optstr == "none":
self.guest.skip_default_sysinfo = True
return
if self.optstr == "host" or self.optstr == "emulate": if self.optstr == "host" or self.optstr == "emulate":
self.optdict['type'] = self.optstr self.optdict['type'] = self.optstr
@ -1791,8 +1764,7 @@ ParserSYSInfo.add_arg("type", "type",
# <bios> type 0 BIOS Information # <bios> type 0 BIOS Information
ParserSYSInfo.add_arg("bios_vendor", "bios_vendor") ParserSYSInfo.add_arg("bios_vendor", "bios_vendor")
ParserSYSInfo.add_arg("bios_version", "bios_version") ParserSYSInfo.add_arg("bios_version", "bios_version")
ParserSYSInfo.add_arg("bios_date", "bios_date", ParserSYSInfo.add_arg("bios_date", "bios_date")
cb=ParserSYSInfo.validate_date_cb)
ParserSYSInfo.add_arg("bios_release", "bios_release") ParserSYSInfo.add_arg("bios_release", "bios_release")
# <system> type 1 System Information # <system> type 1 System Information
@ -1801,7 +1773,7 @@ ParserSYSInfo.add_arg("system_product", "system_product")
ParserSYSInfo.add_arg("system_version", "system_version") ParserSYSInfo.add_arg("system_version", "system_version")
ParserSYSInfo.add_arg("system_serial", "system_serial") ParserSYSInfo.add_arg("system_serial", "system_serial")
ParserSYSInfo.add_arg("system_uuid", "system_uuid", ParserSYSInfo.add_arg("system_uuid", "system_uuid",
cb=ParserSYSInfo.validate_uuid_cb) cb=ParserSYSInfo.set_uuid_cb)
ParserSYSInfo.add_arg("system_sku", "system_sku") ParserSYSInfo.add_arg("system_sku", "system_sku")
ParserSYSInfo.add_arg("system_family", "system_family") ParserSYSInfo.add_arg("system_family", "system_family")

View File

@ -20,8 +20,10 @@
""" """
Classes for building and installing with libvirt <sysinfo> XML Classes for building and installing with libvirt <sysinfo> XML
""" """
import datetime
from .xmlbuilder import XMLBuilder, XMLProperty from .xmlbuilder import XMLBuilder, XMLProperty
from . import util
class SYSInfo(XMLBuilder): class SYSInfo(XMLBuilder):
@ -39,16 +41,42 @@ class SYSInfo(XMLBuilder):
type = XMLProperty("./@type") type = XMLProperty("./@type")
def _validate_date(self, val):
# If supplied, date must be in either mm/dd/yy or mm/dd/yyyy format
try:
datetime.datetime.strptime(val, '%m/%d/%Y')
except ValueError:
try:
datetime.datetime.strptime(val, '%m/%d/%y')
except ValueError:
raise RuntimeError(_("SMBios date string '%s' is invalid.")
% val)
return val
bios_date = XMLProperty("./bios/entry[@name='date']",
validate_cb=_validate_date)
bios_vendor = XMLProperty("./bios/entry[@name='vendor']") bios_vendor = XMLProperty("./bios/entry[@name='vendor']")
bios_version = XMLProperty("./bios/entry[@name='version']") bios_version = XMLProperty("./bios/entry[@name='version']")
bios_date = XMLProperty("./bios/entry[@name='date']")
bios_release = XMLProperty("./bios/entry[@name='release']") bios_release = XMLProperty("./bios/entry[@name='release']")
def _validate_uuid(self, val):
try:
util.validate_uuid(val)
except ValueError:
raise ValueError(_("Invalid uuid for SMBios: %s") % val)
if util.vm_uuid_collision(self.conn, val):
raise ValueError(_("UUID '%s' is in use by another guest.") %
val)
return val
system_uuid = XMLProperty("./system/entry[@name='uuid']",
validate_cb=_validate_uuid)
system_manufacturer = XMLProperty("./system/entry[@name='manufacturer']") system_manufacturer = XMLProperty("./system/entry[@name='manufacturer']")
system_product = XMLProperty("./system/entry[@name='product']") system_product = XMLProperty("./system/entry[@name='product']")
system_version = XMLProperty("./system/entry[@name='version']") system_version = XMLProperty("./system/entry[@name='version']")
system_serial = XMLProperty("./system/entry[@name='serial']") system_serial = XMLProperty("./system/entry[@name='serial']")
system_uuid = XMLProperty("./system/entry[@name='uuid']")
system_sku = XMLProperty("./system/entry[@name='sku']") system_sku = XMLProperty("./system/entry[@name='sku']")
system_family = XMLProperty("./system/entry[@name='family']") system_family = XMLProperty("./system/entry[@name='family']")