error if tcp=tor is requested but tor is unimportable

This only catches txtorcon not being installed (which should be fixed by
doing `pip install tahoe-lafs[tor]`). It doesn't notice that the Tor
daemon is not running (which we can't detect during startup, only
afterwards, when it's harder to notify the user), in which case Tor
connections (and all connections when "tcp = tor" is enabled) will just
fail silently.
This commit is contained in:
Brian Warner 2016-08-31 01:50:13 -07:00
parent 72f17afa76
commit 325028c967
2 changed files with 45 additions and 6 deletions

View File

@ -14,6 +14,21 @@ from allmydata.util.fileutil import abspath_expanduser_unicode
from allmydata.util.encodingutil import get_filesystem_encoding, quote_output
from allmydata.util import configutil
def _import_tor():
# this exists to be overridden by unit tests
try:
from foolscap.connections import tor
return tor
except ImportError: # pragma: no cover
return None
def _import_i2p():
try:
from foolscap.connections import i2p
return i2p
except ImportError: # pragma: no cover
return None
# Add our application versions to the data that Foolscap's LogPublisher
# reports.
for thing, things_version in get_package_versions().iteritems():
@ -175,9 +190,8 @@ class Node(service.MultiService):
enabled = self.get_config("tor", "enable", True, boolean=True)
if not enabled:
return None
try:
from foolscap.connections import tor
except ImportError:
tor = _import_tor()
if not tor:
return None
if self.get_config("tor", "launch", False, boolean=True):
@ -215,9 +229,8 @@ class Node(service.MultiService):
enabled = self.get_config("i2p", "enable", True, boolean=True)
if not enabled:
return None
try:
from foolscap.connections import i2p
except ImportError:
i2p = _import_i2p()
if not i2p:
return None
samport = self.get_config("i2p", "sam.port", None)
@ -259,6 +272,11 @@ class Node(service.MultiService):
raise ValueError("'tahoe.cfg [connections] tcp='"
" uses unknown handler type '%s'"
% tcp_handler_name)
if not handlers[tcp_handler_name]:
raise ValueError("'tahoe.cfg [connections] tcp=' uses "
"unavailable/unimportable handler type '%s'. "
"Please pip install tahoe-lafs[%s] to fix."
% (tcp_handler_name, tcp_handler_name))
self._default_connection_handlers["tcp"] = tcp_handler_name
def set_tub_options(self):

View File

@ -29,6 +29,12 @@ class Tor(unittest.TestCase):
h = n._make_tor_handler()
self.assertEqual(h, None)
def test_unimportable(self):
n = FakeNode(BASECONFIG)
with mock.patch("allmydata.node._import_tor", return_value=None):
h = n._make_tor_handler()
self.assertEqual(h, None)
def test_default(self):
n = FakeNode(BASECONFIG)
h1 = mock.Mock()
@ -109,6 +115,12 @@ class I2P(unittest.TestCase):
h = n._make_i2p_handler()
self.assertEqual(h, None)
def test_unimportable(self):
n = FakeNode(BASECONFIG)
with mock.patch("allmydata.node._import_i2p", return_value=None):
h = n._make_i2p_handler()
self.assertEqual(h, None)
def test_default(self):
n = FakeNode(BASECONFIG)
h1 = mock.Mock()
@ -204,6 +216,15 @@ class Connections(unittest.TestCase):
self.assertEqual(n._default_connection_handlers["tor"], "tor")
self.assertEqual(n._default_connection_handlers["i2p"], "i2p")
def test_tor_unimportable(self):
n = FakeNode(BASECONFIG+"[connections]\ntcp = tor\n")
with mock.patch("allmydata.node._import_tor", return_value=None):
e = self.assertRaises(ValueError, n.init_connections)
self.assertEqual(str(e),
"'tahoe.cfg [connections] tcp='"
" uses unavailable/unimportable handler type 'tor'."
" Please pip install tahoe-lafs[tor] to fix.")
def test_unknown(self):
n = FakeNode(BASECONFIG+"[connections]\ntcp = unknown\n")
e = self.assertRaises(ValueError, n.init_connections)