WAMP: now config and secret given in project files always override those given in commandline. Also made extensions web customisations happen before registration of web interface. nevow_service_rework
authorEdouard Tisserant
Wed, 11 Jul 2018 14:32:19 +0200
branchnevow_service_rework
changeset 2221 e03f7649bfb3
parent 2220 985f234b0d09
child 2222 8ac0f1af5589
WAMP: now config and secret given in project files always override those given in commandline. Also made extensions web customisations happen before registration of web interface.
Beremiz_service.py
runtime/WampClient.py
--- a/Beremiz_service.py	Tue Jul 10 12:54:05 2018 +0200
+++ b/Beremiz_service.py	Wed Jul 11 14:32:19 2018 +0200
@@ -45,17 +45,17 @@
     print("""
 Usage of Beremiz PLC execution service :\n
 %s {[-n servicename] [-i IP] [-p port] [-x enabletaskbar] [-a autostart]|-h|--help} working_dir
-           -n        - zeroconf service name (default:disabled)
-           -i        - IP address of interface to bind to (default:localhost)
-           -p        - port number default:3000
-           -h        - print this help text and quit
-           -a        - autostart PLC (0:disable 1:enable) (default:0)
-           -x        - enable/disable wxTaskbarIcon (0:disable 1:enable) (default:1)
-           -t        - enable/disable Twisted web interface (0:disable 1:enable) (default:1)
-           -w        - web server port or "off" to disable web server (default:8009)
-           -c        - WAMP client default config file (default:wampconf.json)
-           -s        - WAMP client secret, given as a file
-           -e        - python extension (absolute path .py)
+  -n  zeroconf service name (default:disabled)
+  -i  IP address of interface to bind to (default:localhost)
+  -p  port number default:3000
+  -h  print this help text and quit
+  -a  autostart PLC (0:disable 1:enable) (default:0)
+  -x  enable/disable wxTaskbarIcon (0:disable 1:enable) (default:1)
+  -t  enable/disable Twisted web interface (0:disable 1:enable) (default:1)
+  -w  web server port or "off" to disable web server (default:8009)
+  -c  WAMP client config file (can be overriden by wampconf.json in project)
+  -s  WAMP client secret, given as a file (can be overriden by wamp.secret in project)
+  -e  python extension (absolute path .py)
 
            working_dir - directory where are stored PLC files
 """ % sys.argv[0])
@@ -616,6 +616,7 @@
     try:
         WC.SetServer(pyroserver)
         WC.RegisterWampClient(wampconf, wampsecret)
+        WC.RegisterWebSettings(NS)
     except Exception:
         LogMessageAndException(_("WAMP client startup failed. "))
 
--- a/runtime/WampClient.py	Tue Jul 10 12:54:05 2018 +0200
+++ b/runtime/WampClient.py	Wed Jul 11 14:32:19 2018 +0200
@@ -36,8 +36,6 @@
 from twisted.internet.protocol import ReconnectingClientFactory
 from twisted.python.components import registerAdapter
 
-import runtime.NevowServer as NS
-
 from formless import annotate, webform
 import formless
 from nevow import tags, url, static
@@ -191,7 +189,7 @@
     if os.path.exists(_WampConf):
         WampClientConf = json.load(open(_WampConf))
     else: 
-        WampClientConf = defaultWampConfig
+        WampClientConf = defaultWampConfig.copy()
 
     for itemName in mandatoryConfigItems:
         if WampClientConf.get(itemName, None) is None :
@@ -242,22 +240,28 @@
     _WampConfDefault = os.path.join(WorkingDir, "wampconf.json")
     _WampSecretDefault = os.path.join(WorkingDir, "wamp.secret")
 
-    # default project's wampconf has precedance over commandline given
-    if os.path.exists(_WampConfDefault) or wampconf is None:
-        _WampConf = _WampConfDefault
+    # set config file path only if not already set
+    if _WampConf is None:
+        # default project's wampconf has precedance over commandline given
+        if os.path.exists(_WampConfDefault) or wampconf is None:
+            _WampConf = _WampConfDefault
+        else:
+            _WampConf = wampconf
+
+    WampClientConf = GetConfiguration()
+
+    # set secret file path only if not already set
+    if _WampSecret is None:
+        # default project's wamp secret also has precedance over commandline given
+        if os.path.exists(_WampSecretDefault):
+            _WampSecret = _WampSecretDefault
+        else:
+            _WampSecret = wampsecret
+
+    if _WampSecret is not None:
+        WampClientConf["secret"] = LoadWampSecret(_WampSecret)
     else :
-        _WampConf = wampconf
-
-    WampClientConf = GetConfiguration()
-
-    if wampsecret is not None:
-        WampClientConf["secret"] = LoadWampSecret(wampsecret)
-        _WampSecret = wampsecret
-    else :
-        if os.path.exists(_WampSecretDefault):
-            WampClientConf["secret"] = LoadWampSecret(_WampSecretDefault)
-        else :
-            print(_("WAMP authentication has no secret configured"))
+        print(_("WAMP authentication has no secret configured"))
         _WampSecret = _WampSecretDefault
 
     if not WampClientConf["active"]:
@@ -335,12 +339,16 @@
 def wampConfig(**kwargs):
     secretfile_field = kwargs["secretfile"]
     if secretfile_field is not None:
-        secret = secretfile_field.file.read()
-        SetWampSecret(secret)
+        secretfile = getattr(secretfile_field, "file", None)
+        if secretfile is not None:
+            secret = secretfile_field.file.read()
+            SetWampSecret(secret)
 
     newConfig = lastKnownConfig.copy()
     for argname in webExposedConfigItems:
-        newConfig[argname] = kwargs[argname]
+        arg = kwargs.get(argname, None)
+        if arg is not None :
+            newConfig[argname] = arg
 
     SetConfiguration(newConfig)
 
@@ -359,7 +367,6 @@
            
 def getDownloadUrl(ctx, argument):
     if lastKnownConfig is not None :
-        currentID = lastKnownConfig.get("ID", None)
         return url.URL.fromContext(ctx).\
             child(WAMP_SECRET_URL).\
             child(lastKnownConfig["ID"]+".secret")
@@ -385,19 +392,24 @@
                        default=wampConfigDefault))]
 
 
-NS.ConfigurableSettings.addExtension(
-    "wamp", 
-    _("Wamp Settings"),
-    webFormInterface,
-    _("Set"),
-    wampConfig)
-
-
 def deliverWampSecret(ctx, segments):
     filename = segments[1].decode('utf-8')
-    # TODO : SECURITY compare filename to ID and blah...
+    # FIXME: compare filename to ID+".secret"
+    # for now all url under /secret returns the secret
+
+    # TODO: make beutifull message in case of exception 
+    # while loading secret (if empty or dont exist)
     secret = LoadWampSecret(_WampSecret)
     return static.Data(secret, 'application/octet-stream'),()
 
-NS.customSettingsURLs[WAMP_SECRET_URL] = deliverWampSecret
-
+def RegisterWebSettings(NS):
+    NS.ConfigurableSettings.addExtension(
+        "wamp", 
+        _("Wamp Settings"),
+        webFormInterface,
+        _("Set"),
+        wampConfig)
+
+
+    NS.customSettingsURLs[WAMP_SECRET_URL] = deliverWampSecret
+