refactoring pagekite: configuration form works

splitting the services to a separate page is not yet finished
This commit is contained in:
fonfon committed 2015-05-03 09:44:46 +02:00
1 parent cf96797040
commit 1fc0064fd0
15 files changed
+818 -123

No files matched your search

+3 -20
View File
@@ -28,11 +28,11 @@ import augeas
import os
import subprocess
from pagekite_common import SERVICE_PARAMS, construct_params
from pagekite_util import SERVICE_PARAMS, construct_params, \
deconstruct_params, get_augeas_servicefile_path, CONF_PATH
aug = augeas.Augeas()
CONF_PATH = '/files/etc/pagekite.d'
PATHS = {
'service_on': os.path.join(CONF_PATH, '*', 'service_on', '*'),
'kitename': os.path.join(CONF_PATH, '10_account.rc', 'kitename'),
@@ -226,7 +226,7 @@ def subcommand_add_service(arguments):
# so do it manually here
path = convert_augeas_path_to_filepath(root)
with open(path, 'a') as servicefile:
line = "service_on = %s" % arguments.params
line = "service_on = %s" % deconstruct_params(params)
servicefile.write(line)
@@ -251,23 +251,6 @@ def get_new_service_path(protocol):
return os.path.join(root, str(new_index))
def get_augeas_servicefile_path(protocol):
"""Get the augeas path where a service for a protocol should be stored"""
if protocol == 'http':
relpath = '80_httpd.rc'
elif protocol == 'https':
relpath = '443_https.rc'
elif protocol == 'raw/22':
relpath = '22_ssh.rc'
elif protocol.startswith('raw'):
port = protocol.split('/')[1]
relpath = '%s.rc' % port
else:
raise ValueError('Unsupported protocol: %s' % protocol)
return os.path.join(CONF_PATH, relpath, 'service_on')
def subcommand_get_kite(_):
"""Print details of the currently configured kite"""
kitename = aug.get(PATHS['kitename'])
-52
View File
@@ -1,52 +0,0 @@
#!/usr/bin/python2
# -*- mode: python -*-
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
The variables/functions defined here are used by both the action script
and the plinth pagekite module.
Currently that's functionality for converting pagekite service_on strings like
"http:@kitename:localhost:80:@kitestring"
into parameter dictionaries and the other way round.
"""
SERVICE_PARAMS = ['protocol', 'kitename', 'backend_host', 'backend_port',
'secret']
def construct_params(string):
""" Convert a parameter string into a params dictionary"""
try:
params = dict(zip(SERVICE_PARAMS, string.split(':')))
except:
msg = """params are expected to be a ':'-separated string
containing values for: %s , for example:\n"--params
http:@kitename:localhost:8000:@kitesecret"
"""
raise ValueError(msg % ", ".join(SERVICE_PARAMS))
return params
def deconstruct_params(params):
""" Convert params into a ":"-separated parameter string """
try:
paramstring = ":".join([params[param] for param in SERVICE_PARAMS])
except KeyError:
raise ValueError("Could not parse params: %s " % params)
return paramstring
+104
View File
@@ -0,0 +1,104 @@
#!/usr/bin/python2
# -*- mode: python -*-
#
# This file is part of Plinth.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
The variables/functions defined here are used by both the action script
and the plinth pagekite module.
For example the functionality to convert pagekite service_on strings like
"http:@kitename:localhost:80:@kitestring"
into parameter dictionaries and the other way round. And functions that we want
to be covered by tests.
"""
# ATTENTION: This file has to be both python2 and python3 compatible
import os
import shlex
CONF_PATH = '/files/etc/pagekite.d'
SERVICE_PARAMS = ['protocol', 'kitename', 'backend_host', 'backend_port',
'secret']
def construct_params(string):
""" Convert a parameter string into a params dictionary"""
# The actions.py uses shlex.quote() to escape/quote malicious user input.
# That affects '*.@kitename', so the params string gets quoted.
# If the string is escaped and contains '*.@kitename', look whether shlex
# would still quote/escape the string when we remove '*.@kitename'.
# TODO: use shlex only once augeas-python supports python3
if hasattr(shlex, 'quote'):
quotefunction = shlex.quote
else:
import pipes
quotefunction = pipes.quote
if string.startswith("'") and string.endswith("'"):
unquoted_string = string[1:-1]
error_msg = "The parameters contain suspicious characters: %s "
if '*.@kitename' in string:
unquoted_test_string = unquoted_string.replace('*.@kitename', '')
if unquoted_test_string == quotefunction(unquoted_test_string):
# no other malicious characters found, use the unquoted string
string = unquoted_string
else:
raise RuntimeError(error_msg % string)
else:
raise RuntimeError(error_msg % string)
try:
params = dict(zip(SERVICE_PARAMS, string.split(':')))
except:
msg = """params are expected to be a ':'-separated string containing
values for: %s , for example:\n"--params
http/8000:@kitename:localhost:8000:@kitesecret"
"""
raise ValueError(msg % ", ".join(SERVICE_PARAMS))
return params
def deconstruct_params(params):
""" Convert params into a ":"-separated parameter string """
try:
paramstring = ":".join([str(params[param]) for param in
SERVICE_PARAMS])
except KeyError:
raise ValueError("Could not parse params: %s " % params)
return paramstring
def get_augeas_servicefile_path(protocol):
"""Get the augeas path where a service for a protocol should be stored"""
if not protocol.startswith(("http", "https", "raw")):
raise ValueError('Unsupported protocol: %s' % protocol)
try:
_protocol, port = protocol.split('/')
except ValueError:
if protocol == 'http':
relpath = '80_http.rc'
elif protocol == 'https':
relpath = '443_https.rc'
else:
raise ValueError('Unsupported protocol: %s' % protocol)
else:
relpath = '%s_%s.rc' % (port, _protocol)
return os.path.join(CONF_PATH, relpath, 'service_on')