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:
parent
ad456a3387
commit
ce3bedbf2c
@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import xml.parsers.expat
|
import xml.parsers.expat
|
||||||
@ -40,6 +41,31 @@ import qubes.log
|
|||||||
import qubes.tools.qvm_ls
|
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):
|
class Features(dict):
|
||||||
'''Manager of the features.
|
'''Manager of the features.
|
||||||
|
|
||||||
|
@ -28,7 +28,6 @@ import base64
|
|||||||
import datetime
|
import datetime
|
||||||
import os
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
|
||||||
import shutil
|
import shutil
|
||||||
import string
|
import string
|
||||||
import subprocess
|
import subprocess
|
||||||
@ -76,34 +75,9 @@ def _setter_qid(self, prop, value):
|
|||||||
return 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):
|
def _setter_name(self, prop, value):
|
||||||
''' Helper for setting the domain name '''
|
''' Helper for setting the domain name '''
|
||||||
validate_name(self, prop, value)
|
qubes.vm.validate_name(self, prop, value)
|
||||||
|
|
||||||
if self.is_running():
|
if self.is_running():
|
||||||
raise qubes.exc.QubesVMNotHaltedError(
|
raise qubes.exc.QubesVMNotHaltedError(
|
||||||
|
Loading…
Reference in New Issue
Block a user