[Pkg-privacy-commits] [onionbalance] 58/117: Make relative paths in config file relative to config directory

Donncha O'Cearbahill donncha-guest at moszumanska.debian.org
Wed Dec 16 23:18:46 UTC 2015


This is an automated email from the git hooks/post-receive script.

donncha-guest pushed a commit to branch debian/sid
in repository onionbalance.

commit d3c1346ccc4be03247b217cae910fda75a294cdd
Author: Donncha O'Cearbhaill <donncha at donncha.is>
Date:   Fri Jun 26 16:50:11 2015 +0100

    Make relative paths in config file relative to config directory
---
 onionbalance/settings.py | 24 +++++++++++++++++++++--
 test/test_settings.py    | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 2 deletions(-)

diff --git a/onionbalance/settings.py b/onionbalance/settings.py
index 2060df2..4715f7e 100644
--- a/onionbalance/settings.py
+++ b/onionbalance/settings.py
@@ -6,6 +6,7 @@ Implements the generation and loading of configuration files.
 from builtins import input, range
 import os
 import sys
+import errno
 import argparse
 import getpass
 import logging
@@ -37,6 +38,13 @@ def parse_config_file(config_file):
                      config_path)
         sys.exit(1)
 
+    # Rewrite relative paths in the config to be relative to the config
+    # file directory
+    config_directory = os.path.dirname(config_path)
+    for service in config_data.get('services'):
+        if not os.path.isabs(service.get('key')):
+            service['key'] = os.path.join(config_directory, service['key'])
+
     return config_data
 
 
@@ -47,9 +55,21 @@ def initialize_services(controller, services_config):
 
     # Load the keys and config for each onion service
     for service in services_config:
-        service_key = util.key_decrypt_prompt(service.get("key"))
+        try:
+            service_key = util.key_decrypt_prompt(service.get("key"))
+        except OSError as e:
+            if e.errno == errno.ENOENT:
+                logger.error("Private key file %s could not be found. "
+                             "Relative paths in the config file are loaded "
+                             "relative to the config file directory.",
+                             service.get("key"))
+                sys.exit(1)
+            else:
+                raise
+        # Key file was read but a valid private key was not found.
         if not service_key:
-            logger.error("Private key %s could not be loaded.",
+            logger.error("Private key %s could not be loaded. It is a not "
+                         "valid 1024 bit PEM encoded RSA private key",
                          service.get("key"))
             sys.exit(1)
         else:
diff --git a/test/test_settings.py b/test/test_settings.py
new file mode 100644
index 0000000..f987803
--- /dev/null
+++ b/test/test_settings.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+import io
+import os
+
+import pytest
+
+from onionbalance import settings
+from .util import builtin
+
+CONFIG_FILE_VALID = u'\n'.join([
+    "services:",
+    "    - key: private.key",
+    "      instances:",
+    "        - address: fqyw6ojo2voercr7",
+    "        - address: facebookcorewwwi",
+])
+
+CONFIG_FILE_ABSOLUTE = u'\n'.join([
+    "services:",
+    "    - key: /absdir/private.key",
+    "      instances:",
+    "        - address: fqyw6ojo2voercr7",
+    "        - address: facebookcorewwwi",
+])
+
+
+def test_parse_config_file_valid(mocker):
+    # Patch config file read
+    mocker.patch('os.path.exists', return_value=True)
+    mocker.patch(builtin('open'),
+                 lambda *_: io.StringIO(CONFIG_FILE_VALID))
+
+    parsed_config = settings.parse_config_file('/configdir/config_rel.yaml')
+
+    assert len(parsed_config['services']) == 1
+    assert len(parsed_config['services'][0]['instances']) == 2
+
+    # Test key with absolute path
+    assert os.path.dirname(parsed_config['services'][0]['key']) == '/configdir'
+
+    # Test key with absolute path
+    mocker.patch(builtin('open'),
+                 lambda *_: io.StringIO(CONFIG_FILE_ABSOLUTE))
+    parsed_config = settings.parse_config_file('/configdir/config_abs.yaml')
+    assert os.path.dirname(parsed_config['services'][0]['key']) == '/absdir'
+
+
+def test_parse_config_file_does_not_exist(mocker):
+    with pytest.raises(SystemExit):
+        settings.parse_config_file('doesnotexist/config.yaml')

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/onionbalance.git



More information about the Pkg-privacy-commits mailing list