Skip to content

Commit

Permalink
Merge pull request #35 from TheTorProject/feature/path_checks
Browse files Browse the repository at this point in the history
Implement checking of paths before starting oonib.
  • Loading branch information
hellais committed Mar 26, 2014
2 parents 5502131 + aebeb92 commit 755588b
Show file tree
Hide file tree
Showing 18 changed files with 114 additions and 53 deletions.
28 changes: 27 additions & 1 deletion bin/oonib
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion oonib/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from oonib.policy.api import policyAPI
from oonib.bouncer.api import bouncerAPI

from oonib import config
from oonib.config import config

ooniBouncer = None
ooniBackendAPI = []
Expand Down
7 changes: 6 additions & 1 deletion oonib/bouncer/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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):
Expand Down Expand Up @@ -138,6 +138,11 @@ def post(self):
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)
70 changes: 44 additions & 26 deletions oonib/config.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion oonib/deck/api.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
3 changes: 2 additions & 1 deletion oonib/deck/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

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):
Expand Down
12 changes: 12 additions & 0 deletions oonib/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 1 addition & 1 deletion oonib/input/api.py
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
3 changes: 2 additions & 1 deletion oonib/input/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
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):
Expand Down
2 changes: 1 addition & 1 deletion oonib/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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":
Expand Down
2 changes: 1 addition & 1 deletion oonib/oonibackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

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,
Expand Down
1 change: 0 additions & 1 deletion oonib/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
else:
from twisted.scripts._twistd_unix import ServerOptions

#class OONIBOptions(usage.Options):
class OONIBOptions(usage.Options):
synopsis = """%s [options] [path to test].py """

Expand Down
9 changes: 5 additions & 4 deletions oonib/policy/handlers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 7 additions & 5 deletions oonib/report/handlers.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 1 addition & 2 deletions oonib/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@

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':
"""
Expand Down
2 changes: 1 addition & 1 deletion oonib/testhelpers/dns_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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,
Expand Down
2 changes: 1 addition & 1 deletion oonib/testhelpers/ssl_helpers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
5 changes: 1 addition & 4 deletions oonib/testhelpers/tcp_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,6 +67,3 @@ def buildProtocol(self, addr):
p.role = "server"
p.factory = self
return p



0 comments on commit 755588b

Please sign in to comment.