Add test to check a real database (pgsql) works
authorSimo Sorce <simo@redhat.com>
Wed, 29 Oct 2014 14:22:36 +0000 (10:22 -0400)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Wed, 12 Nov 2014 22:46:47 +0000 (23:46 +0100)
Change config template to e able to set up ipsilon with an extrenal
database.
For the easy install the database server must have 3 datbases configured,
and named exactly: admincondif, userprefs, transactions

If different names are required manual instalation will be necessary.
Database URLs (including credentials) can be set using the new option
named --database-url

Signed-off-by: Simo Sorce <simo@redhat.com>
Reviewed-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Makefile
ipsilon/install/ipsilon-server-install
quickrun.py
templates/install/ipsilon.conf
tests/helpers/common.py
tests/pgdb.py [new file with mode: 0755]

index e558b48..e122594 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -59,6 +59,7 @@ tests: wrappers
        PYTHONPATH=./ ./tests/tests.py --test=test1
        PYTHONPATH=./ ./tests/tests.py --test=attrs
        PYTHONPATH=./ ./tests/tests.py --test=trans
        PYTHONPATH=./ ./tests/tests.py --test=test1
        PYTHONPATH=./ ./tests/tests.py --test=attrs
        PYTHONPATH=./ ./tests/tests.py --test=trans
+       PYTHONPATH=./ ./tests/tests.py --test=pgdb
 
 test: lp-test tests
        PYTHONPATH=./ ./ipsilon/tools/saml2metadata.py
 
 test: lp-test tests
        PYTHONPATH=./ ./ipsilon/tools/saml2metadata.py
index 598a025..c6d656d 100755 (executable)
@@ -104,6 +104,12 @@ def install(plugins, args):
                 'sysuser': args['system_user'],
                 'ipsilondir': BINDIR,
                 'staticdir': STATICDIR,
                 'sysuser': args['system_user'],
                 'ipsilondir': BINDIR,
                 'staticdir': STATICDIR,
+                'admindb': args['database_url'] % {
+                    'datadir': args['data_dir'], 'dbname': 'adminconfig'},
+                'usersdb': args['database_url'] % {
+                    'datadir': args['data_dir'], 'dbname': 'userprefs'},
+                'transdb': args['database_url'] % {
+                    'datadir': args['data_dir'], 'dbname': 'transactions'},
                 'secure': "False" if args['secure'] == "no" else "True",
                 'debugging': "True" if args['server_debugging'] else "False"}
     if args['secure'] == 'no':
                 'secure': "False" if args['secure'] == "no" else "True",
                 'debugging': "True" if args['server_debugging'] else "False"}
     if args['secure'] == 'no':
@@ -191,7 +197,7 @@ def find_plugins():
 
 
 def parse_config_profile(args):
 
 
 def parse_config_profile(args):
-    config = ConfigParser.ConfigParser()
+    config = ConfigParser.RawConfigParser()
     files = config.read(args['config_profile'])
     if len(files) == 0:
         raise ConfigurationError('Config Profile file %s not found!' %
     files = config.read(args['config_profile'])
     if len(files) == 0:
         raise ConfigurationError('Config Profile file %s not found!' %
@@ -231,6 +237,9 @@ def parse_args(plugins):
                         help="User account used to run the server")
     parser.add_argument('--admin-user', default='admin',
                         help="User account that is assigned admin privileges")
                         help="User account used to run the server")
     parser.add_argument('--admin-user', default='admin',
                         help="User account that is assigned admin privileges")
+    parser.add_argument('--database-url',
+                        default='sqlite:///%(datadir)s/%(dbname)s.sqlite',
+                        help="The (templatized) database URL to use")
     parser.add_argument('--secure', choices=['yes', 'no'], default='yes',
                         help="Turn on all security checks")
     parser.add_argument('--config-profile', default=None,
     parser.add_argument('--secure', choices=['yes', 'no'], default='yes',
                         help="Turn on all security checks")
     parser.add_argument('--config-profile', default=None,
index 60b3899..852f8b0 100755 (executable)
@@ -65,6 +65,8 @@ def config(workdir):
         f.write(USERS_TEMPLATE)
     subprocess.call(['sqlite3', '-init', sql, users_db, '.quit'])
 
         f.write(USERS_TEMPLATE)
     subprocess.call(['sqlite3', '-init', sql, users_db, '.quit'])
 
+    trans_db = os.path.join(workdir, 'transactions.sqlite')
+
     with open(CONF_TEMPLATE) as f:
         conf_template = f.read()
     t = Template(conf_template)
     with open(CONF_TEMPLATE) as f:
         conf_template = f.read()
     t = Template(conf_template)
@@ -72,6 +74,9 @@ def config(workdir):
                          'instance': 'idp',
                          'staticdir': os.getcwd(),
                          'datadir': workdir,
                          'instance': 'idp',
                          'staticdir': os.getcwd(),
                          'datadir': workdir,
+                         'admindb': admin_db,
+                         'usersdb': users_db,
+                         'transdb': trans_db,
                          'secure': 'False'})
     conf = os.path.join(workdir, 'ipsilon.conf')
     with open(conf, 'w+') as f:
                          'secure': 'False'})
     conf = os.path.join(workdir, 'ipsilon.conf')
     with open(conf, 'w+') as f:
index 83b9704..2e402a3 100644 (file)
@@ -5,9 +5,9 @@ template_dir = "templates"
 log.screen = ${debugging}
 base.mount = "/${instance}"
 base.dir = "${staticdir}"
 log.screen = ${debugging}
 base.mount = "/${instance}"
 base.dir = "${staticdir}"
-admin.config.db = "${datadir}/adminconfig.sqlite"
-user.prefs.db = "${datadir}/userprefs.sqlite"
-transactions.db = "${datadir}/transactions.sqlite"
+admin.config.db = "${admindb}"
+user.prefs.db = "${usersdb}"
+transactions.db = "${transdb}"
 
 tools.sessions.on = True
 tools.sessions.name = "${instance}_ipsilon_session_id"
 
 tools.sessions.on = True
 tools.sessions.name = "${instance}_ipsilon_session_id"
index 00e2a35..a0adfae 100755 (executable)
@@ -117,11 +117,31 @@ class IpsilonTestBase(object):
 
         return http_conf_file
 
 
         return http_conf_file
 
+    def setup_pgdb(self, datadir, env):
+        cmd = ['/usr/bin/pg_ctl', 'initdb', '-D', datadir]
+        subprocess.check_call(cmd, env=env)
+        auth = 'host all all 127.0.0.1/24 trust\n'
+        filename = os.path.join(datadir, 'pg_hba.conf')
+        with open(filename, 'a') as f:
+            f.write(auth)
+
     def start_http_server(self, conf, env):
         p = subprocess.Popen(['/usr/sbin/httpd', '-DFOREGROUND', '-f', conf],
                              env=env, preexec_fn=os.setsid)
         self.processes.append(p)
 
     def start_http_server(self, conf, env):
         p = subprocess.Popen(['/usr/sbin/httpd', '-DFOREGROUND', '-f', conf],
                              env=env, preexec_fn=os.setsid)
         self.processes.append(p)
 
+    def start_pgdb_server(self, datadir, rundir, log, addr, port, env):
+        p = subprocess.Popen(['/usr/bin/pg_ctl', 'start', '-D', datadir, '-o',
+                              '-c unix_socket_directories=%s -c port=%s -c \
+                               listen_addresses=%s' % (rundir, port, addr),
+                              '-l', log, '-w'],
+                             env=env, preexec_fn=os.setsid)
+        self.processes.append(p)
+        p.wait()
+        for d in ['adminconfig', 'userprefs', 'transactions']:
+            cmd = ['/usr/bin/createdb', '-h', addr, '-p', port, d]
+            subprocess.check_call(cmd, env=env)
+
     def wait(self):
         for p in self.processes:
             os.killpg(p.pid, signal.SIGTERM)
     def wait(self):
         for p in self.processes:
             os.killpg(p.pid, signal.SIGTERM)
diff --git a/tests/pgdb.py b/tests/pgdb.py
new file mode 100755 (executable)
index 0000000..12f1cf2
--- /dev/null
@@ -0,0 +1,158 @@
+#!/usr/bin/python
+#
+# Copyright (C) 2014  Simo Sorce <simo@redhat.com>
+#
+# see file 'COPYING' for use and warranty information
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+from helpers.common import IpsilonTestBase  # pylint: disable=relative-import
+from helpers.http import HttpSessions  # pylint: disable=relative-import
+import os
+import pwd
+import sys
+from string import Template
+
+
+idp_g = {'TEMPLATES': '${TESTDIR}/templates/install',
+         'CONFDIR': '${TESTDIR}/etc',
+         'DATADIR': '${TESTDIR}/lib',
+         'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
+         'STATICDIR': '${ROOTDIR}',
+         'BINDIR': '${ROOTDIR}/ipsilon',
+         'WSGI_SOCKET_PREFIX': '${TESTDIR}/${NAME}/logs/wsgi'}
+
+
+idp_a = {'hostname': '${ADDRESS}:${PORT}',
+         'database_url': 'postgresql://@127.0.0.10:45432/%(dbname)s',
+         'admin_user': '${TEST_USER}',
+         'system_user': '${TEST_USER}',
+         'instance': '${NAME}',
+         'secure': 'no',
+         'testauth': 'yes',
+         'pam': 'no',
+         'krb': 'no',
+         'ipa': 'no',
+         'server_debugging': 'True'}
+
+
+sp_g = {'HTTPDCONFD': '${TESTDIR}/${NAME}/conf.d',
+        'SAML2_TEMPLATE': '${TESTDIR}/templates/install/saml2/sp.conf',
+        'SAML2_CONFFILE': '${TESTDIR}/${NAME}/conf.d/ipsilon-saml.conf',
+        'SAML2_HTTPDIR': '${TESTDIR}/${NAME}/saml2'}
+
+
+sp_a = {'hostname': '${ADDRESS}:${PORT}',
+        'saml_idp_metadata': 'http://127.0.0.10:45080/idp1/saml2/metadata',
+        'saml_secure_setup': 'False',
+        'saml_auth': '/sp',
+        'httpd_user': '${TEST_USER}'}
+
+
+def fixup_sp_httpd(httpdir):
+    location = """
+
+Alias /sp ${HTTPDIR}/sp
+
+<Directory ${HTTPDIR}/sp>
+    Require all granted
+</Directory>
+"""
+    index = """WORKS!"""
+
+    t = Template(location)
+    text = t.substitute({'HTTPDIR': httpdir})
+    with open(httpdir + '/conf.d/ipsilon-saml.conf', 'a') as f:
+        f.write(text)
+
+    os.mkdir(httpdir + '/sp')
+    with open(httpdir + '/sp/index.html', 'w') as f:
+        f.write(index)
+
+
+class IpsilonTest(IpsilonTestBase):
+
+    def __init__(self):
+        super(IpsilonTest, self).__init__('pgdb', __file__)
+
+    def setup_servers(self, env=None):
+
+        print "Installing IDP's database server"
+        datadir = os.path.join(self.testdir, 'pgdata')
+        rundir = self.testdir
+        log = os.path.join(self.testdir, 'log/pgdb.log')
+        addr = '127.0.0.10'
+        port = '45432'
+        self.setup_pgdb(datadir, env)
+
+        print "Starting IDP's database server"
+        self.start_pgdb_server(datadir, rundir, log, addr, port, env)
+
+        print "Installing IDP server"
+        name = 'idp1'
+        addr = '127.0.0.10'
+        port = '45080'
+        idp = self.generate_profile(idp_g, idp_a, name, addr, port)
+        conf = self.setup_idp_server(idp, name, addr, port, env)
+
+        print "Starting IDP's httpd server"
+        self.start_http_server(conf, env)
+
+        print "Installing SP server"
+        name = 'sp1'
+        addr = '127.0.0.11'
+        port = '45081'
+        sp = self.generate_profile(sp_g, sp_a, name, addr, port)
+        conf = self.setup_sp_server(sp, name, addr, port, env)
+        fixup_sp_httpd(os.path.dirname(conf))
+
+        print "Starting SP's httpd server"
+        self.start_http_server(conf, env)
+
+
+if __name__ == '__main__':
+
+    idpname = 'idp1'
+    spname = 'sp1'
+    user = pwd.getpwuid(os.getuid())[0]
+
+    sess = HttpSessions()
+    sess.add_server(idpname, 'http://127.0.0.10:45080', user, 'ipsilon')
+    sess.add_server(spname, 'http://127.0.0.11:45081')
+
+    print "test1: Authenticate to IDP ...",
+    try:
+        sess.auth_to_idp(idpname)
+    except Exception, e:  # pylint: disable=broad-except
+        print >> sys.stderr, " ERROR: %s" % repr(e)
+        sys.exit(1)
+    print " SUCCESS"
+
+    print "test1: Add SP Metadata to IDP ...",
+    try:
+        sess.add_sp_metadata(idpname, spname)
+    except Exception, e:  # pylint: disable=broad-except
+        print >> sys.stderr, " ERROR: %s" % repr(e)
+        sys.exit(1)
+    print " SUCCESS"
+
+    print "test1: Access SP Protected Area ...",
+    try:
+        page = sess.fetch_page(idpname, 'http://127.0.0.11:45081/sp/')
+        page.expected_value('text()', 'WORKS!')
+    except ValueError, e:
+        print >> sys.stderr, " ERROR: %s" % repr(e)
+        sys.exit(1)
+    print " SUCCESS"