vm: move validate_name to qubes/vm

This will be needed by VMProperty class in the next commit.

QubesOS/qubes-issues#2622
This commit is contained in:
Marek Marczykowski-Górecki 2017-03-28 20:42:10 +02:00
parent ad456a3387
commit ce3bedbf2c
No known key found for this signature in database
GPG Key ID: 063938BA42CFA724
2 changed files with 27 additions and 27 deletions

View File

@ -27,6 +27,7 @@
import datetime
import os
import re
import subprocess
import sys
import xml.parsers.expat
@ -40,6 +41,31 @@ import qubes.log
import qubes.tools.qvm_ls
def validate_name(holder, prop, value):
''' Check if value is syntactically correct VM name '''
if not isinstance(value, str):
raise TypeError('VM name must be string, {!r} found'.format(
type(value).__name__))
if len(value) > 31:
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:
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')
class Features(dict):
'''Manager of the features.

View File

@ -28,7 +28,6 @@ import base64
import datetime
import os
import os.path
import re
import shutil
import string
import subprocess
@ -76,34 +75,9 @@ def _setter_qid(self, prop, value):
return value
def validate_name(holder, prop, value):
''' Check if value is syntactically correct VM name '''
if not isinstance(value, str):
raise TypeError('VM name must be string, {!r} found'.format(
type(value).__name__))
if len(value) > 31:
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:
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)
qubes.vm.validate_name(self, prop, value)
if self.is_running():
raise qubes.exc.QubesVMNotHaltedError(