From a2037d8c63e5d0917ae31a8174990cdc67570836 Mon Sep 17 00:00:00 2001 From: Dain Nilsson Date: Thu, 25 Apr 2013 15:26:02 +0200 Subject: [PATCH] Added form for dbconfig-common files. --- yubiadmin/apps/ksm.py | 7 +++++-- yubiadmin/apps/val.py | 37 +++++++------------------------- yubiadmin/util.py | 49 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 33 deletions(-) diff --git a/yubiadmin/apps/ksm.py b/yubiadmin/apps/ksm.py index fd2eaab..655eb02 100644 --- a/yubiadmin/apps/ksm.py +++ b/yubiadmin/apps/ksm.py @@ -1,9 +1,11 @@ +from yubiadmin.util import App, DBConfigForm + __all__ = [ 'app' ] -class YubikeyKsm(object): +class YubikeyKsm(App): """ YubiKey Key Storage Module @@ -17,6 +19,7 @@ class YubikeyKsm(object): """ Database Settings """ - return "Hello world" + return self.render_forms(request, [ + DBConfigForm('/etc/yubico/ksm/config-db.php')]) app = YubikeyKsm() diff --git a/yubiadmin/apps/val.py b/yubiadmin/apps/val.py index b2ca554..98128f1 100644 --- a/yubiadmin/apps/val.py +++ b/yubiadmin/apps/val.py @@ -1,7 +1,6 @@ -from wtforms.fields import StringField, IntegerField, PasswordField -from wtforms.validators import NumberRange, Optional -from wtforms.widgets import PasswordInput -from yubiadmin.util import App, ConfigForm, FileConfig, render +from wtforms.fields import IntegerField +from wtforms.validators import NumberRange +from yubiadmin.util import App, DBConfigForm, ConfigForm, FileConfig __all__ = [ 'app' @@ -78,29 +77,6 @@ class MiscForm(ConfigForm): ) -class DatabaseForm(ConfigForm): - legend = 'Database' - dbtype = StringField('DB type') - dbserver = StringField('Host') - dbport = IntegerField('Port', [Optional(), NumberRange(1, 65535)]) - dbname = StringField('DB name') - dbuser = StringField('DB username') - dbpass = PasswordField('DB password', - widget=PasswordInput(hide_value=False)) - - config = FileConfig( - '/home/dain/yubico/yubiadmin/config-db.php', - [ - ('dbtype', db_read('type'), db_write('type'), 'mysql'), - ('dbserver', db_read('server'), db_write('server'), 'localhost'), - ('dbport', db_read('port'), db_write('port'), ''), - ('dbname', db_read('name'), db_write('name'), 'ykval'), - ('dbuser', db_read('user'), db_write('user'), 'ykval_verifier'), - ('dbpass', db_read('pass'), db_write('pass'), ''), - ] - ) - - class YubikeyVal(App): """ YubiKey Validation Server @@ -121,18 +97,19 @@ class YubikeyVal(App): """ Database Settings """ - return self.render_forms(request, [DatabaseForm()]) + return self.render_forms(request, [ + DBConfigForm('/home/dain/yubico/yubiadmin/config-db.php')]) def syncpool(self, request): """ Sync pool """ - return render('form', target=request.path) + return 'Not yet implemented.' def ksms(self, request): """ Key Store Modules """ - return render('form', target=request.path) + return 'Not yet implemented.' app = YubikeyVal() diff --git a/yubiadmin/util.py b/yubiadmin/util.py index bbc2b89..b595692 100644 --- a/yubiadmin/util.py +++ b/yubiadmin/util.py @@ -1,13 +1,16 @@ import os import re from UserDict import DictMixin -from wtforms import Form +from wtforms import Form, StringField, IntegerField, PasswordField +from wtforms.widgets import PasswordInput +from wtforms.validators import Optional, NumberRange from jinja2 import Environment, FileSystemLoader __all__ = [ 'App', 'FileConfig', 'ConfigForm', + 'DBConfigForm', 'render', 'populate_forms', ] @@ -72,6 +75,9 @@ class ValueHandler(object): class FileConfig(DictMixin): + """ + Maps key-value pairs to a backing config file. + """ def __init__(self, filename, params=[]): self.filename = filename self.params = {} @@ -107,6 +113,9 @@ class FileConfig(DictMixin): class ConfigForm(Form): + """ + Form that can load and save data to a config. + """ config = None def load(self): @@ -121,3 +130,41 @@ class ConfigForm(Form): if field.id in self.config: self.config[field.id] = field.data self.config.commit() + + +def db_read(varname): + return r'\$db%s=\'(.*)\';' % varname + + +def db_write(varname): + return lambda x: '$db%s=\'%s\';' % (varname, x) + + +class DBConfigForm(ConfigForm): + """ + Complete form for editing a dbconfig-common generated for PHP. + """ + legend = 'Database' + dbtype = StringField('DB type') + dbserver = StringField('Host') + dbport = IntegerField('Port', [Optional(), NumberRange(1, 65535)]) + dbname = StringField('DB name') + dbuser = StringField('DB username') + dbpass = PasswordField('DB password', + widget=PasswordInput(hide_value=False)) + + config = FileConfig( + '/dev/null', + [ + ('dbtype', db_read('type'), db_write('type'), 'mysql'), + ('dbserver', db_read('server'), db_write('server'), 'localhost'), + ('dbport', db_read('port'), db_write('port'), ''), + ('dbname', db_read('name'), db_write('name'), 'ykval'), + ('dbuser', db_read('user'), db_write('user'), 'ykval_verifier'), + ('dbpass', db_read('pass'), db_write('pass'), ''), + ] + ) + + def __init__(self, filename, *args, **kwargs): + self.__class__.config.filename = filename + super(DBConfigForm, self).__init__(*args, **kwargs)