Browse Source

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
master
lars 13 years ago
parent
commit
4301427e26
  1. 96
      src/cryptobox/core/settings.py

96
src/cryptobox/core/settings.py

@ -563,21 +563,25 @@ class CryptoBoxSettings:
## 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 = """
[Main]
AllowedDevices = listOfDevices(default="")
AllowedDevices = listOfDevices(default="/dev/invalid")
DefaultVolumePrefix = string(min=1)
DefaultCipher = string(default="aes-cbc-essiv:sha256")
ConfigVolumeLabel = string(min=1, default="cbox_config")
UseConfigPartition = integer(min=0, max=1, default=0)
DisabledPlugins = list(default=list())
DisabledPlugins = listOfPlugins(default=list())
[Locations]
MountParentDir = directoryExists(default="/var/cache/cryptobox-server/mnt")
SettingsDir = directoryExists(default="/var/cache/cryptobox-server/settings")
TemplateDir = directoryExists(default="/usr/share/cryptobox-server/templates")
DocDir = directoryExists(default="/usr/share/doc/cryptobox-server/html")
PluginDir = listOfExistingDirectories(default=list("/usr/share/cryptobox-server/plugins"))
MountParentDir = directoryMountExists(default=None)
SettingsDir = directorySettingsExists(default=None)
TemplateDir = directoryTemplateExists(default=None)
DocDir = directoryDocExists(default=None)
PluginDir = listOfExistingPluginDirectories(default=None)
EventDir = string(default="/etc/cryptobox-server/events.d")
[Log]
@ -624,17 +628,71 @@ class CryptoBoxSettingsValidator(validate.Validator):
def __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["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["listOfDevices"] = self.list_devices
self.functions["listOfPlugins"] = self.list_plugins
def check_directory_exists(self, value):
"""Is the directory accessible?
def check_mount_directory_exists(self, value):
"""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)
if not os.path.isdir(dir_path):
raise validate.VdtValueError("%s (not found)" % value)
@ -669,9 +727,12 @@ class CryptoBoxSettingsValidator(validate.Validator):
return file_path
def check_existing_directories(self, value):
def check_existing_plugin_directories(self, value):
"""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:
raise validate.VdtValueError("no plugin directory specified")
if not isinstance(value, list):
@ -706,6 +767,17 @@ class CryptoBoxSettingsValidator(validate.Validator):
devices = [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:

Loading…
Cancel
Save