summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrebs <drebs@riseup.net>2017-11-19 18:32:50 -0200
committerdrebs <drebs@riseup.net>2017-11-19 20:10:00 -0200
commit0844dc3a1a15f60757a680c819cad686b5724bf4 (patch)
treeceb3bbf8e461edeca9d632440590323909e69407
parent70cd9349faad311d645157f1961f63e030998a57 (diff)
[feature] allow setting couchdb url from environment
-rw-r--r--docs/man/soledad-server.1.rst3
-rw-r--r--docs/reference/environment_variables.rst1
-rw-r--r--src/leap/soledad/server/_config.py74
-rw-r--r--tests/server/test_config.py30
4 files changed, 65 insertions, 43 deletions
diff --git a/docs/man/soledad-server.1.rst b/docs/man/soledad-server.1.rst
index 3d37a8d2..e40e9a63 100644
--- a/docs/man/soledad-server.1.rst
+++ b/docs/man/soledad-server.1.rst
@@ -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.
diff --git a/docs/reference/environment_variables.rst b/docs/reference/environment_variables.rst
index bfd155ee..321c925c 100644
--- a/docs/reference/environment_variables.rst
+++ b/docs/reference/environment_variables.rst
@@ -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.
diff --git a/src/leap/soledad/server/_config.py b/src/leap/soledad/server/_config.py
index 0d37879e..7fa01b38 100644
--- a/src/leap/soledad/server/_config.py
+++ b/src/leap/soledad/server/_config.py
@@ -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]
diff --git a/tests/server/test_config.py b/tests/server/test_config.py
index 92b0f67f..b21efbbe 100644
--- a/tests/server/test_config.py
+++ b/tests/server/test_config.py
@@ -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',
- 'create_cmd':
- 'sudo -u soledad-admin /usr/bin/soledad-create-userdb',
- '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'])
+ 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',
+ 'batching': False,
+ 'blobs': False,
+ 'services_tokens_file': '/etc/soledad/services.tokens',
+ 'blobs_path': '/var/lib/soledad/blobs',
+ 'concurrent_blob_writes': 50
+ }
+ expected = _reflect_environment({'soledad-server': expected})
+ self.assertDictEqual(
+ expected['soledad-server'],
+ config['soledad-server'])