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

[tor-commits] [stem/master] Better exception when set_conf fails due to immutability



commit f338c6f7b8e3807918b4988fc7689842ce9b9c43
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Wed Jul 5 09:45:08 2017 -0700

    Better exception when set_conf fails due to immutability
    
    Tor now indicates in its manual which options cannot be set while the process
    is running. Taking advantage of this to provide a better error message when
    folks try to set them...
    
      https://gitweb.torproject.org/tor.git/commit/?id=ceeaf04
---
 docs/change_log.rst              |  1 +
 stem/control.py                  | 24 ++++++++++++++++++++++++
 test/integ/control/controller.py | 10 ++++++++++
 3 files changed, 35 insertions(+)

diff --git a/docs/change_log.rst b/docs/change_log.rst
index 9de1ca1..af697a5 100644
--- a/docs/change_log.rst
+++ b/docs/change_log.rst
@@ -53,6 +53,7 @@ The following are only available within Stem's `git repository
   * Added the GUARD_WAIT :data:`~stem.CircStatus` (:spec:`6446210`)
   * Unable to use cookie auth when path includes wide characters (chinese, japanese, etc)
   * Tor change caused :func:`~stem.control.Controller.list_ephemeral_hidden_services` to provide empty strings if unset (:trac:`21329`)
+  * Better error message when :func:`~stem.control.Controller.set_conf` fails due to an option being immutable
   * Failed to parse torrcs without a port on ipv6 exit policy entries
 
  * **Descriptors**
diff --git a/stem/control.py b/stem/control.py
index 95b4d91..6475af4 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -326,6 +326,26 @@ Listener = stem.util.enum.UppercaseEnum(
   'CONTROL',
 )
 
+# torrc options that cannot be changed once tor's running
+
+IMMUTABLE_CONFIG_OPTIONS = map(str.lower, (
+  'AccelDir',
+  'AccelName',
+  'DataDirectory',
+  'DisableAllSwap',
+  'DisableDebuggerAttachment',
+  'HardwareAccel',
+  'HiddenServiceNonAnonymousMode',
+  'HiddenServiceSingleHopMode',
+  'KeepBindCapabilities',
+  'PidFile',
+  'RunAsDaemon',
+  'Sandbox',
+  'SyslogIdentityTag',
+  'TokenBucketRefillInterval',
+  'User',
+))
+
 LOG_CACHE_FETCHES = True  # provide trace level logging for cache hits
 
 # Configuration options that are fetched by a special key. The keys are
@@ -2404,6 +2424,10 @@ class Controller(BaseController):
         self._set_cache({'get_custom_options': None})
     else:
       log.debug('%s (failed, code: %s, message: %s)' % (query, response.code, response.message))
+      immutable_params = [k for k, v in params if k.lower() in IMMUTABLE_CONFIG_OPTIONS]
+
+      if immutable_params:
+        raise stem.InvalidArguments(message = "%s cannot be changed while tor's running" % ', '.join(sorted(immutable_params)), arguments = immutable_params)
 
       if response.code == '552':
         if response.message.startswith("Unrecognized option: Unknown option '"):
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 889ca0c..3b8bde5 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -756,6 +756,16 @@ class TestController(unittest.TestCase):
         shutil.rmtree(tmpdir)
 
   @test.require.controller
+  def test_set_conf_when_immutable(self):
+    """
+    Issue a SETCONF for tor options that cannot be changed while running.
+    """
+
+    with test.runner.get_runner().get_tor_controller() as controller:
+      self.assertRaisesRegexp(stem.InvalidArguments, "DisableAllSwap cannot be changed while tor's running", controller.set_conf, 'DisableAllSwap', '1')
+      self.assertRaisesRegexp(stem.InvalidArguments, "DisableAllSwap, User cannot be changed while tor's running", controller.set_options, {'User': 'atagar', 'DisableAllSwap': '1'})
+
+  @test.require.controller
   @test.require.version(Requirement.LOADCONF)
   def test_loadconf(self):
     """



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