runtime/WampClient.py
branchnevow_service_rework
changeset 2221 e03f7649bfb3
parent 2220 985f234b0d09
child 2223 909216419e45
equal deleted inserted replaced
2220:985f234b0d09 2221:e03f7649bfb3
    34 from autobahn.wamp.serializer import MsgPackSerializer
    34 from autobahn.wamp.serializer import MsgPackSerializer
    35 from twisted.internet.defer import inlineCallbacks
    35 from twisted.internet.defer import inlineCallbacks
    36 from twisted.internet.protocol import ReconnectingClientFactory
    36 from twisted.internet.protocol import ReconnectingClientFactory
    37 from twisted.python.components import registerAdapter
    37 from twisted.python.components import registerAdapter
    38 
    38 
    39 import runtime.NevowServer as NS
       
    40 
       
    41 from formless import annotate, webform
    39 from formless import annotate, webform
    42 import formless
    40 import formless
    43 from nevow import tags, url, static
    41 from nevow import tags, url, static
    44 
    42 
    45 mandatoryConfigItems = ["ID", "active", "realm", "url"]
    43 mandatoryConfigItems = ["ID", "active", "realm", "url"]
   189     global lastKnownConfig
   187     global lastKnownConfig
   190 
   188 
   191     if os.path.exists(_WampConf):
   189     if os.path.exists(_WampConf):
   192         WampClientConf = json.load(open(_WampConf))
   190         WampClientConf = json.load(open(_WampConf))
   193     else: 
   191     else: 
   194         WampClientConf = defaultWampConfig
   192         WampClientConf = defaultWampConfig.copy()
   195 
   193 
   196     for itemName in mandatoryConfigItems:
   194     for itemName in mandatoryConfigItems:
   197         if WampClientConf.get(itemName, None) is None :
   195         if WampClientConf.get(itemName, None) is None :
   198             raise Exception(_("WAMP configuration error : missing '{}' parameter.").format(itemName))
   196             raise Exception(_("WAMP configuration error : missing '{}' parameter.").format(itemName))
   199 
   197 
   240 def RegisterWampClient(wampconf=None, wampsecret=None):
   238 def RegisterWampClient(wampconf=None, wampsecret=None):
   241     global _WampConf, _WampSecret
   239     global _WampConf, _WampSecret
   242     _WampConfDefault = os.path.join(WorkingDir, "wampconf.json")
   240     _WampConfDefault = os.path.join(WorkingDir, "wampconf.json")
   243     _WampSecretDefault = os.path.join(WorkingDir, "wamp.secret")
   241     _WampSecretDefault = os.path.join(WorkingDir, "wamp.secret")
   244 
   242 
   245     # default project's wampconf has precedance over commandline given
   243     # set config file path only if not already set
   246     if os.path.exists(_WampConfDefault) or wampconf is None:
   244     if _WampConf is None:
   247         _WampConf = _WampConfDefault
   245         # default project's wampconf has precedance over commandline given
       
   246         if os.path.exists(_WampConfDefault) or wampconf is None:
       
   247             _WampConf = _WampConfDefault
       
   248         else:
       
   249             _WampConf = wampconf
       
   250 
       
   251     WampClientConf = GetConfiguration()
       
   252 
       
   253     # set secret file path only if not already set
       
   254     if _WampSecret is None:
       
   255         # default project's wamp secret also has precedance over commandline given
       
   256         if os.path.exists(_WampSecretDefault):
       
   257             _WampSecret = _WampSecretDefault
       
   258         else:
       
   259             _WampSecret = wampsecret
       
   260 
       
   261     if _WampSecret is not None:
       
   262         WampClientConf["secret"] = LoadWampSecret(_WampSecret)
   248     else :
   263     else :
   249         _WampConf = wampconf
   264         print(_("WAMP authentication has no secret configured"))
   250 
       
   251     WampClientConf = GetConfiguration()
       
   252 
       
   253     if wampsecret is not None:
       
   254         WampClientConf["secret"] = LoadWampSecret(wampsecret)
       
   255         _WampSecret = wampsecret
       
   256     else :
       
   257         if os.path.exists(_WampSecretDefault):
       
   258             WampClientConf["secret"] = LoadWampSecret(_WampSecretDefault)
       
   259         else :
       
   260             print(_("WAMP authentication has no secret configured"))
       
   261         _WampSecret = _WampSecretDefault
   265         _WampSecret = _WampSecretDefault
   262 
   266 
   263     if not WampClientConf["active"]:
   267     if not WampClientConf["active"]:
   264         print(_("WAMP deactivated in configuration"))
   268         print(_("WAMP deactivated in configuration"))
   265         return
   269         return
   333         return lastKnownConfig.get(argument.name, None)
   337         return lastKnownConfig.get(argument.name, None)
   334 
   338 
   335 def wampConfig(**kwargs):
   339 def wampConfig(**kwargs):
   336     secretfile_field = kwargs["secretfile"]
   340     secretfile_field = kwargs["secretfile"]
   337     if secretfile_field is not None:
   341     if secretfile_field is not None:
   338         secret = secretfile_field.file.read()
   342         secretfile = getattr(secretfile_field, "file", None)
   339         SetWampSecret(secret)
   343         if secretfile is not None:
       
   344             secret = secretfile_field.file.read()
       
   345             SetWampSecret(secret)
   340 
   346 
   341     newConfig = lastKnownConfig.copy()
   347     newConfig = lastKnownConfig.copy()
   342     for argname in webExposedConfigItems:
   348     for argname in webExposedConfigItems:
   343         newConfig[argname] = kwargs[argname]
   349         arg = kwargs.get(argname, None)
       
   350         if arg is not None :
       
   351             newConfig[argname] = arg
   344 
   352 
   345     SetConfiguration(newConfig)
   353     SetConfiguration(newConfig)
   346 
   354 
   347 class FileUploadDownload(annotate.FileUpload):
   355 class FileUploadDownload(annotate.FileUpload):
   348     pass
   356     pass
   357 
   365 
   358 registerAdapter(FileUploadDownloadRenderer, FileUploadDownload, formless.iformless.ITypedRenderer)
   366 registerAdapter(FileUploadDownloadRenderer, FileUploadDownload, formless.iformless.ITypedRenderer)
   359            
   367            
   360 def getDownloadUrl(ctx, argument):
   368 def getDownloadUrl(ctx, argument):
   361     if lastKnownConfig is not None :
   369     if lastKnownConfig is not None :
   362         currentID = lastKnownConfig.get("ID", None)
       
   363         return url.URL.fromContext(ctx).\
   370         return url.URL.fromContext(ctx).\
   364             child(WAMP_SECRET_URL).\
   371             child(WAMP_SECRET_URL).\
   365             child(lastKnownConfig["ID"]+".secret")
   372             child(lastKnownConfig["ID"]+".secret")
   366 
   373 
   367 webFormInterface = [
   374 webFormInterface = [
   383     ("url",
   390     ("url",
   384        annotate.String(label=_("WAMP Server URL"),
   391        annotate.String(label=_("WAMP Server URL"),
   385                        default=wampConfigDefault))]
   392                        default=wampConfigDefault))]
   386 
   393 
   387 
   394 
   388 NS.ConfigurableSettings.addExtension(
       
   389     "wamp", 
       
   390     _("Wamp Settings"),
       
   391     webFormInterface,
       
   392     _("Set"),
       
   393     wampConfig)
       
   394 
       
   395 
       
   396 def deliverWampSecret(ctx, segments):
   395 def deliverWampSecret(ctx, segments):
   397     filename = segments[1].decode('utf-8')
   396     filename = segments[1].decode('utf-8')
   398     # TODO : SECURITY compare filename to ID and blah...
   397     # FIXME: compare filename to ID+".secret"
       
   398     # for now all url under /secret returns the secret
       
   399 
       
   400     # TODO: make beutifull message in case of exception 
       
   401     # while loading secret (if empty or dont exist)
   399     secret = LoadWampSecret(_WampSecret)
   402     secret = LoadWampSecret(_WampSecret)
   400     return static.Data(secret, 'application/octet-stream'),()
   403     return static.Data(secret, 'application/octet-stream'),()
   401 
   404 
   402 NS.customSettingsURLs[WAMP_SECRET_URL] = deliverWampSecret
   405 def RegisterWebSettings(NS):
   403 
   406     NS.ConfigurableSettings.addExtension(
       
   407         "wamp", 
       
   408         _("Wamp Settings"),
       
   409         webFormInterface,
       
   410         _("Set"),
       
   411         wampConfig)
       
   412 
       
   413 
       
   414     NS.customSettingsURLs[WAMP_SECRET_URL] = deliverWampSecret
       
   415