changeset 811:b1826adbeeff

server: tls_certificate option: - renamed ssl_certificate to tls_certificate - added a method to handle deprecated options - use C.APP_NAME for config section instead of hardcoded "libervia"
author Goffi <goffi@goffi.org>
date Sun, 20 Dec 2015 19:59:01 +0100
parents 3905bc24eb17
children fd6965c16e7e
files src/twisted/plugins/libervia_server.py
diffstat 1 files changed, 25 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/twisted/plugins/libervia_server.py	Fri Dec 18 21:29:24 2015 +0100
+++ b/src/twisted/plugins/libervia_server.py	Sun Dec 20 19:59:01 2015 +0100
@@ -33,15 +33,17 @@
 from libervia.server.constants import Const as C
 
 from sat.core.i18n import _
-from sat.tools.config import getConfig
+from sat.tools import config
 
 from zope.interface import implements
 
 from twisted.python import usage
 from twisted.plugin import IPlugin
 from twisted.application.service import IServiceMaker
+import ConfigParser
 
-from ConfigParser import SafeConfigParser, NoSectionError, NoOptionError
+
+CONFIG_SECTION = C.APP_NAME.lower()
 
 
 def coerceConnectionType(value):  # called from Libervia.OPT_PARAMETERS
@@ -64,7 +66,7 @@
                        ['port', 'p', 8080, _(u'The port number to listen HTTP on.').encode('utf-8'), int],
                        ['port_https', 's', 8443, _(u'The port number to listen HTTPS on.').encode('utf-8'), int],
                        ['port_https_ext', 'e', 0, _(u'The external port number used for HTTPS (0 means port_https value).').encode('utf-8'), int],
-                       ['ssl_certificate', 'c', 'libervia.pem', _(u'PEM certificate with both private and public parts.').encode('utf-8'), str],
+                       ['tls_certificate', 'c', 'libervia.pem', _(u'TLS certificate public certificate or private key and public certificate combined (PEM format)').encode('utf-8'), str],
                        ['redirect_to_https', 'r', 1, _(u'Automatically redirect from HTTP to HTTPS.').encode('utf-8'), int],
                        ['security_warning', 'w', 1, _(u'Warn user that he is about to connect on HTTP.').encode('utf-8'), int],
                        ['passphrase', 'k', '', (_(u"Passphrase for the SàT profile named '%s'") % C.SERVICE_PROFILE).encode('utf-8'), str],
@@ -105,20 +107,36 @@
         # if the  options values are the hard-coded ones or if they have been passed on the command line.
 
         # FIXME: must be refactored + code can be factorised with backend
-        config = SafeConfigParser()
-        config.read(C.CONFIG_FILES)
+        config_parser = ConfigParser.SafeConfigParser()
+        config_parser.read(C.CONFIG_FILES)
+        self.handleDeprecated(config_parser)
         for param in self.optParameters + OPT_PARAMETERS_CFG:
             name = param[0]
             try:
-                value = getConfig(config, 'libervia', name, Exception)
+                value = config.getConfig(config_parser, CONFIG_SECTION, name, Exception)
                 try:
                     param[2] = param[4](value)
                 except IndexError: # the coerce method is optional
                     param[2] = value
-            except (NoSectionError, NoOptionError):
+            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
                 pass
         usage.Options.__init__(self)
 
+    def handleDeprecated(self, config_parser):
+        """display warning and/or change option when a deprecated option if found
+
+        param config_parser(ConfigParser): read ConfigParser instance for sat.conf
+        """
+        replacements = (('ssl_certificate', 'tls_certificate'),)
+        for old, new in replacements:
+            try:
+                value = config.getConfig(config_parser, CONFIG_SECTION, old, Exception)
+            except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
+                pass
+            else:
+                print u"\n/!\\ Use of {old} is deprecated, please use {new} instead\n".format(old=old, new=new)
+                config_parser.set(CONFIG_SECTION, new, value)
+
 
 class LiberviaMaker(object):
     implements(IServiceMaker, IPlugin)