Skip to content
Snippets Groups Projects
Unverified Commit 0844dc3a authored by drebs's avatar drebs
Browse files

[feature] allow setting couchdb url from environment

parent 70cd9349
Branches
Tags
1 merge request!160#8992 - fix TAC file test
......@@ -61,6 +61,9 @@ SOLEDAD_SERVER_CONFIG_FILE
Load configuration from this file instead of using the default one
(*/etc/soledad/soledad-server.conf*).
SOLEDAD_COUCH_URL
If set, use this URL for accessing couchdb (overrides the configuration file).
SOLEDAD_HTTP_PERSIST
If set, persist HTTP connections.
......
......@@ -6,6 +6,7 @@ Some environment variables affect the behaviour of Soledad:
============================== =============== =================================
variable affects description
============================== =============== =================================
``SOLEDAD_COUCH_URL`` server override the CouchDB url.
``SOLEDAD_HTTP_PERSIST`` client persist HTTP connections.
``SOLEDAD_USE_PYTHON_LOGGING`` client / server use python logging instead of
twisted's logger.
......
......@@ -19,9 +19,13 @@
import configparser
import os
from twisted.logger import Logger
__all__ = ['get_config']
logger = Logger()
# make sure to update documentation if this default is changed.
DEFAULT_CONFIG_FILE = '/etc/soledad/soledad-server.conf'
CONFIG_DEFAULTS = {
......@@ -44,45 +48,55 @@ CONFIG_DEFAULTS = {
}
_config = None
def get_config(section='soledad-server'):
global _config
if not _config:
fname = os.environ.get(
'SOLEDAD_SERVER_CONFIG_FILE', DEFAULT_CONFIG_FILE)
_config = _load_config(fname)
return _config[section]
def _load_config(file_path):
"""
Load server configuration from file.
@param file_path: The path to the configuration file.
@type file_path: str
@return: A dictionary with the configuration.
@rtype: dict
"""
def _load_from_file(file_path):
logger.info('Loading configuration from %s' % file_path)
conf = dict(CONFIG_DEFAULTS)
config = configparser.SafeConfigParser()
config.read(file_path)
parsed = configparser.SafeConfigParser()
parsed.read(file_path)
for section in conf:
if not config.has_section(section):
if not parsed.has_section(section):
continue
for key, value in conf[section].items():
if not config.has_option(section, key):
if not parsed.has_option(section, key):
continue
elif type(value) == bool:
conf[section][key] = config.getboolean(section, key)
conf[section][key] = parsed.getboolean(section, key)
elif type(value) == list:
values = config.get(section, key).split(',')
values = parsed.get(section, key).split(',')
values = [v.strip() for v in values]
conf[section][key] = values
else:
conf[section][key] = config.get(section, key)
conf[section][key] = parsed.get(section, key)
# TODO: implement basic parsing/sanitization of options comming from
# config file.
# parsed file.
return conf
def _reflect_environment(conf):
from_environment = ['couch_url']
for option in from_environment:
name = 'SOLEDAD_%s' % option.upper()
value = os.environ.get(name)
if value:
logger.info('Using %s=%s because of %s environment variable.'
% (option, value, name))
conf['soledad-server'][option] = value
return conf
def _load_config(file_path):
conf = _load_from_file(file_path)
conf = _reflect_environment(conf)
return conf
_config = None
def get_config(section='soledad-server'):
global _config
if not _config:
fname = os.environ.get(
'SOLEDAD_SERVER_CONFIG_FILE', DEFAULT_CONFIG_FILE)
_config = _load_config(fname)
return _config[section]
......@@ -22,6 +22,7 @@ from twisted.trial import unittest
from pkg_resources import resource_filename
from leap.soledad.server._config import _load_config
from leap.soledad.server._config import _reflect_environment
from leap.soledad.server._config import CONFIG_DEFAULTS
......@@ -32,7 +33,7 @@ class ConfigurationParsingTest(unittest.TestCase):
def test_use_defaults_on_failure(self):
config = _load_config('this file will never exist')
expected = CONFIG_DEFAULTS
expected = _reflect_environment(CONFIG_DEFAULTS)
self.assertEquals(expected, config)
def test_security_values_configuration(self):
......@@ -57,15 +58,18 @@ class ConfigurationParsingTest(unittest.TestCase):
config = _load_config(config_path)
# then
expected = {'couch_url':
'http://soledad:passwd@localhost:5984',
expected = {
'couch_url': 'http://soledad:passwd@localhost:5984',
'create_cmd':
'sudo -u soledad-admin /usr/bin/soledad-create-userdb',
'admin_netrc':
'/etc/couchdb/couchdb-soledad-admin.netrc',
'admin_netrc': '/etc/couchdb/couchdb-soledad-admin.netrc',
'batching': False,
'blobs': False,
'services_tokens_file': '/etc/soledad/services.tokens',
'blobs_path': '/var/lib/soledad/blobs',
'concurrent_blob_writes': 50}
self.assertDictEqual(expected, config['soledad-server'])
'concurrent_blob_writes': 50
}
expected = _reflect_environment({'soledad-server': expected})
self.assertDictEqual(
expected['soledad-server'],
config['soledad-server'])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment