vm/qubesvm: improve name property setter

Split it into two functions: validate_name - context-less verification,
and actual _setter_name which perform additional verification in
context of actual VM.
Switch to qubes.exc.* exceptions where appropriate.
This commit is contained in:
Marek Marczykowski-Górecki 2017-03-11 19:20:22 +01:00
parent dbf2066dfd
commit a036e2a8a0
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724

View File

@ -75,20 +75,35 @@ def _setter_qid(self, prop, value):
return value
def _setter_name(self, prop, value):
''' Helper for setting the domain name '''
def validate_name(holder, prop, value):
''' Check if value is syntactically correct VM name '''
if not isinstance(value, str):
raise TypeError('{} value must be string, {!r} found'.format(
prop.__name__, type(value).__name__))
raise TypeError('VM name must be string, {!r} found'.format(
type(value).__name__))
if len(value) > 31:
raise ValueError('{} value must be shorter than 32 characters'.format(
prop.__name__))
if holder is not None and prop is not None:
raise qubes.exc.QubesPropertyValueError(holder, prop, value,
'{} value must be shorter than 32 characters'.format(
prop.__name__))
else:
raise qubes.exc.QubesValueError(
'VM name must be shorter than 32 characters')
# this regexp does not contain '+'; if it had it, we should specifically
# disallow 'lost+found' #1440
if re.match(r"^[a-zA-Z][a-zA-Z0-9_-]*$", value) is None:
raise ValueError('{} value contains illegal characters'.format(
prop.__name__))
if holder is not None and prop is not None:
raise qubes.exc.QubesPropertyValueError(holder, prop, value,
'{} value contains illegal characters'.format(prop.__name__))
else:
raise qubes.exc.QubesValueError(
'VM name contains illegal characters')
def _setter_name(self, prop, value):
''' Helper for setting the domain name '''
validate_name(self, prop, value)
if self.is_running():
raise qubes.exc.QubesVMNotHaltedError(
self, 'Cannot change name of running VM')
@ -101,7 +116,7 @@ def _setter_name(self, prop, value):
pass
if value in self.app.domains:
raise qubes.exc.QubesValueError(
raise qubes.exc.QubesPropertyValueError(self, prop, value,
'VM named {} alread exists'.format(value))
return value