[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [oonib/master] Implement checking of paths before starting oonib.



commit aebeb92b87bd7b4f19f8b1d0923efd4003a8df9a
Author: Arturo Filastò <art@xxxxxxxxx>
Date:   Mon Mar 24 12:34:59 2014 +0100

    Implement checking of paths before starting oonib.
    
    This is a fix related to #34
---
 bin/oonib                        |   28 ++++++++++++++-
 oonib/api.py                     |    2 +-
 oonib/bouncer/handlers.py        |    7 +++-
 oonib/config.py                  |   70 ++++++++++++++++++++++++--------------
 oonib/deck/api.py                |    2 +-
 oonib/deck/handlers.py           |    3 +-
 oonib/errors.py                  |   12 +++++++
 oonib/input/api.py               |    2 +-
 oonib/input/handlers.py          |    3 +-
 oonib/log.py                     |    2 +-
 oonib/oonibackend.py             |    2 +-
 oonib/options.py                 |    1 -
 oonib/policy/handlers.py         |    9 ++---
 oonib/report/handlers.py         |   12 ++++---
 oonib/runner.py                  |    3 +-
 oonib/testhelpers/dns_helpers.py |    2 +-
 oonib/testhelpers/ssl_helpers.py |    2 +-
 oonib/testhelpers/tcp_helpers.py |    5 +--
 18 files changed, 114 insertions(+), 53 deletions(-)

diff --git a/bin/oonib b/bin/oonib
index 554d3bb..7fb1c35 100755
--- a/bin/oonib
+++ b/bin/oonib
@@ -10,7 +10,33 @@ from twisted.application import app
 sys.path[:] = map(os.path.abspath, sys.path)
 sys.path.insert(0, os.path.abspath(os.getcwd()))
 
-from oonib import runner, config
+from oonib import errors as e
+from oonib.config import config
+
+try:
+    config.load()
+except e.ConfigFileNotSpecified:
+    print "Config file not specified!"
+    print "Use -c to specify one!"
+    config.usageOptions()
+    sys.exit(1)
+except e.ConfigFileDoesNotExist, path:
+    print "Config file \"%s\" does not exist!" % path
+    sys.exit(2)
+except e.InvalidReportDirectory, path:
+    print "Invalid report directory: %s!" % path
+    sys.exit(3)
+except e.InvalidArchiveDirectory, path:
+    print "Invalid archive directory: %s!" % path
+    sys.exit(4)
+except e.InvalidInputDirectory, path:
+    print "Invalid input directory: %s" % path
+    sys.exit(5)
+except e.InvalidDeckDirectory, path:
+    print "Invalid deck directory: %s" % path
+    sys.exit(6)
+
+from oonib import runner
 from oonib.oonibackend import application
 
 if config.main.chroot:
diff --git a/oonib/api.py b/oonib/api.py
index 8b7ad4e..2427837 100644
--- a/oonib/api.py
+++ b/oonib/api.py
@@ -6,7 +6,7 @@ from oonib.input.api import inputAPI
 from oonib.policy.api import policyAPI
 from oonib.bouncer.api import bouncerAPI
 
-from oonib import config
+from oonib.config import config
 
 ooniBouncer = None
 ooniBackendAPI = []
diff --git a/oonib/bouncer/handlers.py b/oonib/bouncer/handlers.py
index c6b249d..38ac1f5 100644
--- a/oonib/bouncer/handlers.py
+++ b/oonib/bouncer/handlers.py
@@ -3,7 +3,7 @@ import random
 import yaml
 from oonib import errors as e
 from oonib.handlers import OONIBHandler
-from oonib import config
+from oonib.config import config
 
 class Bouncer(object):
     def __init__(self):
@@ -138,6 +138,11 @@ class BouncerQueryHandler(OONIBHandler):
             requested_helpers = query['test-helpers']
         except KeyError:
             raise e.TestHelpersKeyMissing
+        
+        try:
+            assert isinstance(requested_helpers, list)
+        except AssertionError:
+            raise e.InvalidRequest
 
         response = self.bouncer.filterHelperAddresses(requested_helpers)
         self.write(response)
diff --git a/oonib/config.py b/oonib/config.py
index a1a5e79..4f818b0 100644
--- a/oonib/config.py
+++ b/oonib/config.py
@@ -1,37 +1,55 @@
 import yaml
+from oonib import errors as e
 from oonib import Storage
 from oonib import __version__
 
 from oonib.options import OONIBOptions
 import os
 
-def get_root_path():
-    this_directory = os.path.dirname(__file__)
-    root = os.path.join(this_directory, '..')
-    root = os.path.abspath(root)
-    return root
-
-def loadConfigFile():
+class Config(object):
+    main = None
+    helpers = None
+    reports = {}
+    backend_version = __version__
     opts = OONIBOptions()
-    opts.parseOptions()
-    if 'config' in opts.keys():
-        with open(opts['config']) as f:
-            config_file_contents = '\n'.join(f.readlines())
-            configuration = yaml.safe_load(config_file_contents)
-            main = Storage(opts)
-            for k, v in configuration['main'].items():
-                main[k] = v
-            helpers = Storage()
-            for k, v in configuration['helpers'].items():
-                helpers[k] = Storage()
-                for k2, v2 in v.items():
-                    helpers[k][k2] = v2
-            return main, helpers
-    return None, None
-
-main = None
+
+    def __init__(self):
+        self.opts.parseOptions()
+
+    def load(self):
+        try:
+            config_file = self.opts['config']
+        except KeyError:
+            raise e.ConfigFileNotSpecified
+
+        try:
+            with open(self.opts['config']) as f:
+                configuration = yaml.safe_load(f)
+        except IOError:
+            raise e.ConfigFileDoesNotExist(self.opts['config'])
+
+        self.main = Storage()
+        for k, v in configuration['main'].items():
+            self.main[k] = v
+        self.helpers = Storage()
+        for name, helper in configuration['helpers'].items():
+            self.helpers[name] = Storage()
+            for k, v in helper.items():
+                self.helpers[name][k] = v
+        self.check_paths()
+    
+    def check_paths(self):
+        if not self.main.report_dir or not os.path.isdir(self.main.report_dir):
+            raise e.InvalidReportDirectory(self.main.report_dir)
+        if not self.main.archive_dir or not os.path.isdir(self.main.archive_dir):
+            raise e.InvalidArchiveDirectory(self.main.report_dir)
+
+        if self.main.input_dir and not os.path.isdir(self.main.input_dir):
+            raise e.InvalidInputDirectory(self.main.input_dir)
+        if self.main.deck_dir and not os.path.isdir(self.main.deck_dir):
+            raise e.InvalidDeckDirectory(self.main.deck_dir)
+
 backend_version = __version__
 reports = {}
 
-if not main:
-    main, helpers = loadConfigFile()
+config = Config()
diff --git a/oonib/deck/api.py b/oonib/deck/api.py
index ffccb0a..a2f1164 100644
--- a/oonib/deck/api.py
+++ b/oonib/deck/api.py
@@ -1,6 +1,6 @@
 from cyclone import web
 from oonib.deck import handlers
-from oonib import config
+from oonib.config import config
 
 deckAPI = [
     (r"/deck", handlers.DeckListHandler),
diff --git a/oonib/deck/handlers.py b/oonib/deck/handlers.py
index c8aed69..44ff24f 100644
--- a/oonib/deck/handlers.py
+++ b/oonib/deck/handlers.py
@@ -6,7 +6,8 @@ import yaml
 
 from oonib import errors as e
 from oonib.handlers import OONIBHandler
-from oonib import config, log
+from oonib import log
+from oonib.config import config
 
 class DeckDescHandler(OONIBHandler):
     def get(self, deckID):
diff --git a/oonib/errors.py b/oonib/errors.py
index 8d6015e..6f768d0 100644
--- a/oonib/errors.py
+++ b/oonib/errors.py
@@ -73,3 +73,15 @@ class TestHelperNotFound(OONIBError):
     status_code = 404
     log_message = "test-helper-not-found"
 
+class ConfigFileNotSpecified(Exception): pass
+
+class ConfigFileDoesNotExist(Exception): pass
+
+class InvalidReportDirectory(Exception): pass
+
+class InvalidArchiveDirectory(Exception): pass
+
+class InvalidInputDirectory(Exception): pass
+
+class InvalidDeckDirectory(Exception): pass
+
diff --git a/oonib/input/api.py b/oonib/input/api.py
index 88d85a2..811da29 100644
--- a/oonib/input/api.py
+++ b/oonib/input/api.py
@@ -1,6 +1,6 @@
 from cyclone import web
 from oonib.input import handlers
-from oonib import config
+from oonib.config import config
 
 inputAPI = [
     (r"/input", handlers.InputListHandler),
diff --git a/oonib/input/handlers.py b/oonib/input/handlers.py
index d467854..fce7451 100644
--- a/oonib/input/handlers.py
+++ b/oonib/input/handlers.py
@@ -4,7 +4,8 @@ import os
 import yaml
 
 from oonib.handlers import OONIBHandler
-from oonib import config, log
+from oonib import log
+from oonib.config import config
 
 class InputDescHandler(OONIBHandler):
     def get(self, inputID):
diff --git a/oonib/log.py b/oonib/log.py
index b28b5bf..8229446 100644
--- a/oonib/log.py
+++ b/oonib/log.py
@@ -17,7 +17,7 @@ from twisted.python.failure import Failure
 from twisted.python.logfile import DailyLogFile
 
 from oonib import otime
-from oonib import config
+from oonib.config import config
 
 ## Get rid of the annoying "No route found for
 ## IPv6 destination warnings":
diff --git a/oonib/oonibackend.py b/oonib/oonibackend.py
index c5f96ee..a6654b8 100644
--- a/oonib/oonibackend.py
+++ b/oonib/oonibackend.py
@@ -20,7 +20,7 @@ from oonib.testhelpers import http_helpers, tcp_helpers
 
 from oonib import log
 
-from oonib import config
+from oonib.config import config
 
 if config.main.uid and config.main.gid:
     application = service.Application('oonibackend', uid=config.main.uid,
diff --git a/oonib/options.py b/oonib/options.py
index cce1590..7acdc23 100644
--- a/oonib/options.py
+++ b/oonib/options.py
@@ -7,7 +7,6 @@ if platformType == "win32":
 else:
     from twisted.scripts._twistd_unix import ServerOptions
 
-#class OONIBOptions(usage.Options):
 class OONIBOptions(usage.Options):
     synopsis = """%s [options] [path to test].py """
 
diff --git a/oonib/policy/handlers.py b/oonib/policy/handlers.py
index abce0b3..913aeb1 100644
--- a/oonib/policy/handlers.py
+++ b/oonib/policy/handlers.py
@@ -1,11 +1,12 @@
-from oonib import errors as e
-from oonib.handlers import OONIBHandler
-
-from oonib import config
 import json
 import os
 import yaml
 
+from oonib import errors as e
+from oonib.handlers import OONIBHandler
+
+from oonib.config import config
+
 class Policy(object):
     nettest = None
     input = None
diff --git a/oonib/report/handlers.py b/oonib/report/handlers.py
index 2ee73ac..dea5326 100644
--- a/oonib/report/handlers.py
+++ b/oonib/report/handlers.py
@@ -1,18 +1,20 @@
-import json
-import os
 import random
-import re
 import string
 import time
 import yaml
+import json
+import os
+import re
+
+from twisted.internet import fdesc, reactor
 
 from oonib import errors as e
 from oonib.handlers import OONIBHandler
 from oonib.policy.handlers import Policy
 
 from datetime import datetime
-from oonib import randomStr, otime, config, log
-from twisted.internet import fdesc, reactor
+from oonib import randomStr, otime, log
+from oonib.config import config
 
 class MissingField(Exception):
     pass
diff --git a/oonib/runner.py b/oonib/runner.py
index 1243ee8..57b0b9f 100644
--- a/oonib/runner.py
+++ b/oonib/runner.py
@@ -22,12 +22,11 @@ from txtorcon import launch_tor
 
 from oonib.report.api import reportAPI 
 from oonib.api import ooniBackend, ooniBouncer
+from oonib.config import config
 
 from oonib import oonibackend
-from oonib import config
 from oonib import log
 
-
 from txtorcon import __version__ as txtorcon_version
 if txtorcon_version < '0.9.0':
     """
diff --git a/oonib/testhelpers/dns_helpers.py b/oonib/testhelpers/dns_helpers.py
index 8959215..9b2e5e3 100644
--- a/oonib/testhelpers/dns_helpers.py
+++ b/oonib/testhelpers/dns_helpers.py
@@ -3,7 +3,7 @@ from twisted.internet import reactor
 from twisted.names import dns
 from twisted.names import client, server
 
-from oonib import config
+from oonib.config import config
 
 class DNSTestHelper(server.DNSServerFactory):
     def __init__(self, authorities = None,
diff --git a/oonib/testhelpers/ssl_helpers.py b/oonib/testhelpers/ssl_helpers.py
index 5c74996..d511ddd 100644
--- a/oonib/testhelpers/ssl_helpers.py
+++ b/oonib/testhelpers/ssl_helpers.py
@@ -1,5 +1,5 @@
 from twisted.internet import ssl
-from oonib import config
+from oonib.config import config
 
 class SSLContext(ssl.DefaultOpenSSLContextFactory):
     def __init__(self, *args, **kw):
diff --git a/oonib/testhelpers/tcp_helpers.py b/oonib/testhelpers/tcp_helpers.py
index ab3c835..91c334b 100644
--- a/oonib/testhelpers/tcp_helpers.py
+++ b/oonib/testhelpers/tcp_helpers.py
@@ -2,7 +2,7 @@
 from twisted.internet.protocol import Protocol, Factory, ServerFactory
 from twisted.internet.error import ConnectionDone
 
-from oonib import config
+from oonib.config import config
 from oonib import log
 from oonib.daphn3 import Daphn3Protocol
 from oonib.daphn3 import read_pcap, read_yaml
@@ -67,6 +67,3 @@ class Daphn3Server(ServerFactory):
         p.role = "server"
         p.factory = self
         return p
-
-
-



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits