From a036e2a8a065e253cf5f5f2ee067a08956ee2dfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sat, 11 Mar 2017 19:20:22 +0100 Subject: [PATCH] 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. --- qubes/vm/qubesvm.py | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/qubes/vm/qubesvm.py b/qubes/vm/qubesvm.py index bae91936..8a5ae3f9 100644 --- a/qubes/vm/qubesvm.py +++ b/qubes/vm/qubesvm.py @@ -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