CryptoNAS config validation:

* adapt to changes in configobj (v4.3.2 -> v4.5.2) - Closes #218
* workaround a misbehaviour of configobj for lists with only one item - Closes: #219
This commit is contained in:
lars 2009-01-07 03:34:48 +00:00
parent 54d69c506e
commit 4301427e26
1 changed files with 84 additions and 12 deletions

View File

@ -563,21 +563,25 @@ class CryptoBoxSettings:
## the logger named "CryptoBox" is configured now ## the logger named "CryptoBox" is configured now
# We can't use real default values for the "directory_exists" tests below.
# Otherwise configobj complains about the "invalid" default value (if the
# directory does not exist) - even if the default value is not used.
# Up to configobj version 4.3.2 this workaround was not necessary.
validation_spec = """ validation_spec = """
[Main] [Main]
AllowedDevices = listOfDevices(default="") AllowedDevices = listOfDevices(default="/dev/invalid")
DefaultVolumePrefix = string(min=1) DefaultVolumePrefix = string(min=1)
DefaultCipher = string(default="aes-cbc-essiv:sha256") DefaultCipher = string(default="aes-cbc-essiv:sha256")
ConfigVolumeLabel = string(min=1, default="cbox_config") ConfigVolumeLabel = string(min=1, default="cbox_config")
UseConfigPartition = integer(min=0, max=1, default=0) UseConfigPartition = integer(min=0, max=1, default=0)
DisabledPlugins = list(default=list()) DisabledPlugins = listOfPlugins(default=list())
[Locations] [Locations]
MountParentDir = directoryExists(default="/var/cache/cryptobox-server/mnt") MountParentDir = directoryMountExists(default=None)
SettingsDir = directoryExists(default="/var/cache/cryptobox-server/settings") SettingsDir = directorySettingsExists(default=None)
TemplateDir = directoryExists(default="/usr/share/cryptobox-server/templates") TemplateDir = directoryTemplateExists(default=None)
DocDir = directoryExists(default="/usr/share/doc/cryptobox-server/html") DocDir = directoryDocExists(default=None)
PluginDir = listOfExistingDirectories(default=list("/usr/share/cryptobox-server/plugins")) PluginDir = listOfExistingPluginDirectories(default=None)
EventDir = string(default="/etc/cryptobox-server/events.d") EventDir = string(default="/etc/cryptobox-server/events.d")
[Log] [Log]
@ -624,17 +628,71 @@ class CryptoBoxSettingsValidator(validate.Validator):
def __init__(self): def __init__(self):
validate.Validator.__init__(self) validate.Validator.__init__(self)
self.functions["directoryExists"] = self.check_directory_exists self.functions["directoryMountExists"] = \
self.check_mount_directory_exists
self.functions["directorySettingsExists"] = \
self.check_settings_directory_exists
self.functions["directoryTemplateExists"] = \
self.check_template_directory_exists
self.functions["directoryDocExists"] = \
self.check_doc_directory_exists
self.functions["fileExecutable"] = self.check_file_executable self.functions["fileExecutable"] = self.check_file_executable
self.functions["fileWriteable"] = self.check_file_writeable self.functions["fileWriteable"] = self.check_file_writeable
self.functions["listOfExistingDirectories"] = self.check_existing_directories self.functions["listOfExistingPluginDirectories"] \
= self.check_existing_plugin_directories
self.functions["listOfLanguages"] = self.list_languages self.functions["listOfLanguages"] = self.list_languages
self.functions["listOfDevices"] = self.list_devices self.functions["listOfDevices"] = self.list_devices
self.functions["listOfPlugins"] = self.list_plugins
def check_directory_exists(self, value): def check_mount_directory_exists(self, value):
"""Is the directory accessible? """Is the mount directory accessible?
""" """
# use the default path, if the setting is missing
if value is None:
value = "/var/cache/cryptobox-server/mnt"
dir_path = os.path.abspath(value)
if not os.path.isdir(dir_path):
raise validate.VdtValueError("%s (not found)" % value)
if not os.access(dir_path, os.X_OK):
raise validate.VdtValueError("%s (access denied)" % value)
return dir_path
def check_settings_directory_exists(self, value):
"""Is the settings directory accessible?
"""
# use the default path, if the setting is missing
if value is None:
value = "/var/cache/cryptobox-server/settings"
dir_path = os.path.abspath(value)
if not os.path.isdir(dir_path):
raise validate.VdtValueError("%s (not found)" % value)
if not os.access(dir_path, os.X_OK):
raise validate.VdtValueError("%s (access denied)" % value)
return dir_path
def check_template_directory_exists(self, value):
"""Is the template directory accessible?
"""
# use the default path, if the setting is missing
if value is None:
value = "/usr/share/cryptobox-server/templates"
dir_path = os.path.abspath(value)
if not os.path.isdir(dir_path):
raise validate.VdtValueError("%s (not found)" % value)
if not os.access(dir_path, os.X_OK):
raise validate.VdtValueError("%s (access denied)" % value)
return dir_path
def check_doc_directory_exists(self, value):
"""Is the documentation directory accessible?
"""
# use the default path, if the setting is missing
if value is None:
value = "/usr/share/doc/cryptobox-server/html"
dir_path = os.path.abspath(value) dir_path = os.path.abspath(value)
if not os.path.isdir(dir_path): if not os.path.isdir(dir_path):
raise validate.VdtValueError("%s (not found)" % value) raise validate.VdtValueError("%s (not found)" % value)
@ -669,9 +727,12 @@ class CryptoBoxSettingsValidator(validate.Validator):
return file_path return file_path
def check_existing_directories(self, value): def check_existing_plugin_directories(self, value):
"""Are these directories accessible? """Are these directories accessible?
""" """
# return the default value, if the settings is missing
if value is None:
value = ["/usr/share/cryptobox-server/plugins"]
if not value: if not value:
raise validate.VdtValueError("no plugin directory specified") raise validate.VdtValueError("no plugin directory specified")
if not isinstance(value, list): if not isinstance(value, list):
@ -706,6 +767,17 @@ class CryptoBoxSettingsValidator(validate.Validator):
devices = [devices] devices = [devices]
return devices return devices
def list_plugins(self, plugins):
"""Return plugin names as a list.
"""
if not plugins:
plugins = []
if isinstance(plugins, basestring):
plugins = [plugins]
elif not isinstance(plugins, list):
raise validate.VdtValueError("invalid list of disabled plugins")
return plugins
class MiscConfigFile: class MiscConfigFile: