Esempio n. 1
0
    def test_sqlaudit_none(self, app):
        # GIVEN an empty configuration

        # WHEN I request an audit engine
        audit = getAudit(app.config)

        # THEN The engine is shared with the main database
        assert audit.engine and audit.engine == db.engine
Esempio n. 2
0
def load_environment(global_conf, app_conf):
    """
    Configure the Pylons environment via the ``pylons.config``
    object
    """

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[app_conf.get('custom_templates',
                                         os.path.join(root, 'templates')),
                            os.path.join(root, 'templates') ])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='linotp', paths=paths)

    config['linotp.root'] = root
    config['routes.map'] = make_map()
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = linotp.lib.helpers


    ## add per token a location for the mako template lookup
    ## @note: the location is defined in the .ini file by
    ##  the entry [linotpTokenModules]

    directories = paths['templates']

    ## add a template path for every token
    modules = get_token_module_list()
    for module in modules:
        mpath = os.path.dirname(module.__file__)
        directories.append(mpath)

    ## add a template path for every resolver
    modules = get_resolver_module_list()
    for module in modules:
        mpath = os.path.dirname(module.__file__)
        directories.append(mpath)

    unique_directories = _uniqify_list(directories)
    log.debug("[load_environment] Template directories: %r" % unique_directories)

    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=unique_directories,
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the SQLAlchemy database engine
    # If we load the linotp.model here, the pylons.config is loaded with
    # the entries from the config file. if it is loaded at the top of the file,
    #the pylons.config does not contain the config file, yet.
    from linotp.model import init_model
    engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    from linotp.lib.audit.base import getAudit
    audit = getAudit()
    config['audit'] = audit

    ## setup Security provider definition
    try:
        log.debug('[load_environment] loading token list definition')
        g = config['pylons.app_globals']
        g.security_provider.load_config(config)
    except Exception as e:
        log.error("Failed to load security provider definition: %r" % e)
        raise e

    ## load the list of tokenclasses
    try:
        log.debug('[load_environment] loading token list definition')
        (tcl, tpl) = get_token_class_list()

        config['tokenclasses'] = tcl
        g.setTokenclasses(tcl)

        config['tokenprefixes'] = tpl
        g.setTokenprefixes(tpl)

    except Exception as e:
        log.error("Failed to load token class list: %r" % e)
        raise e

    ## load the list of resolvers
    try:
        log.debug('[load_environment] loading resolver list definition')
        (rclass, rname) = get_resolver_class_list()

        ## make this globaly avaliable
        g.setResolverClasses(rclass)
        g.setResolverTypes(rname)

    except Exception as exx:
        log.error("Faild to load the list of resolvers: %r" % exx)
        raise exx

    ## get the help url
    url = config.get("linotpHelp.url", None)
    if url == None:
        version = pkg_resources.get_distribution("linotp").version
        offline_url = "file:///usr/share/doc/linotpdoc/html/"
        # First try to get the help for this specific version
        url = "http://linotp.org/doc/%s/index.html" % version
        try:
            ## Try to open the online url with a timeout of 5 seconds
            ## If the system is definitively offline the request will return immediately
            urllib2.urlopen(url, timeout=5)
        except urllib2.HTTPError:
            # it seems the version does not exist, we set the latest
            url = "http://linotp.org/doc/latest/index.html"
            log.info("the Help URL for the version %s is not available, using the latest one: %s" % (version, url))
        except urllib2.URLError:
            # it seems we are offline, using an offline url
            log.warning("it seems we are offline, so using the offline url %s" % offline_url)
            url = offline_url
    config['help_url'] = url

    log.debug("[load_environment] done")
    return
Esempio n. 3
0
    def test_sqlaudit_sqlaudit(self, app):
        audit = getAudit()

        # audit object should be a database audit
        assert isinstance(audit, Audit)
Esempio n. 4
0
    def test_sqlaudit_off(self, app):
        audit = getAudit()

        # audit object should be a dummy class without implementation
        assert isinstance(audit, AuditBase)
Esempio n. 5
0
def load_environment(global_conf, app_conf):
    """
    Configure the Pylons environment via the ``pylons.config``
    object
    """

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[app_conf.get('custom_templates',
                                         os.path.join(root, 'templates')),
                            os.path.join(root, 'templates')])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='linotp', paths=paths)

    config['linotp.root'] = root
    config['routes.map'] = make_map(global_conf, app_conf)
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = linotp.lib.helpers

    # add per token a location for the mako template lookup
    # @note: the location is defined in the .ini file by
    # the entry [linotpTokenModules]

    directories = paths['templates']

    import linotp.tokens as token_package

    token_package_path = os.path.dirname(token_package.__file__)
    directories.append(token_package_path)

    for token_package_sub_path, _subdir, _files in os.walk(token_package_path):
        directories.append(token_package_sub_path)

    # add a template path for every resolver
    resolver_module_path = UserIdResolver.__file__
    directories.append(resolver_module_path)

    unique_directories = _uniqify_list(directories)
    log.debug("[load_environment] Template directories: %r" % unique_directories)

    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=unique_directories,
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8', default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the SQLAlchemy database engine
    # If we load the linotp.model here, the pylons.config is loaded with
    # the entries from the config file. if it is loaded at the top of the file,
    # the pylons.config does not contain the config file, yet.
    from linotp.model import init_model
    engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    from linotp.lib.audit.base import getAudit
    audit = getAudit(config)
    config['audit'] = audit

    # setup Security provider definition
    try:
        log.debug('[load_environment] loading token list definition')
        g = config['pylons.app_globals']
        g.security_provider.load_config(config)
    except Exception as e:
        log.exception("Failed to load security provider definition: %r" % e)
        raise e

    # get the help url
    url = config.get("linotpHelp.url", None)
    if url is None:
        version = pkg_resources.get_distribution("linotp").version
        # First try to get the help for this specific version
        url = "https://linotp.org/doc/%s/index.html" % version
    config['help_url'] = url

    log.debug("[load_environment] done")

    return config
Esempio n. 6
0
def load_environment(global_conf, app_conf):
    """
    Configure the Pylons environment via the ``pylons.config``
    object
    """

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[
                     app_conf.get('custom_templates',
                                  os.path.join(root, 'templates')),
                     os.path.join(root, 'templates')
                 ])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='linotp', paths=paths)

    config['linotp.root'] = root
    config['routes.map'] = make_map(global_conf, app_conf)
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = linotp.lib.helpers

    # add per token a location for the mako template lookup
    # @note: the location is defined in the .ini file by
    # the entry [linotpTokenModules]

    directories = paths['templates']

    # add a template path for every token
    modules = get_token_module_list()
    for module in modules:
        mpath = os.path.dirname(module.__file__)
        directories.append(mpath)

    # add a template path for every resolver
    modules = get_resolver_module_list()
    for module in modules:
        mpath = os.path.dirname(module.__file__)
        directories.append(mpath)

    unique_directories = _uniqify_list(directories)
    log.debug("[load_environment] Template directories: %r" %
              unique_directories)

    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=unique_directories,
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8',
        default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the SQLAlchemy database engine
    # If we load the linotp.model here, the pylons.config is loaded with
    # the entries from the config file. if it is loaded at the top of the file,
    # the pylons.config does not contain the config file, yet.
    from linotp.model import init_model
    engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    from linotp.lib.audit.base import getAudit
    audit = getAudit()
    config['audit'] = audit

    # setup Security provider definition
    try:
        log.debug('[load_environment] loading token list definition')
        g = config['pylons.app_globals']
        g.security_provider.load_config(config)
    except Exception as e:
        log.error("Failed to load security provider definition: %r" % e)
        raise e

    # load the list of tokenclasses
    try:
        log.debug('[load_environment] loading token list definition')
        (tcl, tpl) = get_token_class_list()

        config['tokenclasses'] = tcl
        g.setTokenclasses(tcl)

        config['tokenprefixes'] = tpl
        g.setTokenprefixes(tpl)

    except Exception as e:
        log.error("Failed to load token class list: %r" % e)
        raise e

    # load the list of resolvers
    try:
        log.debug('[load_environment] loading resolver list definition')
        (rclass, rname) = get_resolver_class_list()

        # make this globaly avaliable
        g.setResolverClasses(rclass)
        g.setResolverTypes(rname)

    except Exception as exx:
        log.error("Faild to load the list of resolvers: %r" % exx)
        raise exx

    # get the help url
    url = config.get("linotpHelp.url", None)
    if url is None:
        version = pkg_resources.get_distribution("linotp").version
        # First try to get the help for this specific version
        url = "http://linotp.org/doc/%s/index.html" % version
    config['help_url'] = url

    log.debug("[load_environment] done")
    return config
Esempio n. 7
0
def load_environment(global_conf, app_conf):
    """
    Configure the Pylons environment via the ``pylons.config``
    object
    """

    # Pylons paths
    root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    paths = dict(root=root,
                 controllers=os.path.join(root, 'controllers'),
                 static_files=os.path.join(root, 'public'),
                 templates=[
                     app_conf.get('custom_templates',
                                  os.path.join(root, 'templates')),
                     os.path.join(root, 'templates')
                 ])

    # Initialize config with the basic options
    config.init_app(global_conf, app_conf, package='linotp', paths=paths)

    config['linotp.root'] = root
    config['routes.map'] = make_map()
    config['pylons.app_globals'] = app_globals.Globals()
    config['pylons.h'] = linotp.lib.helpers

    ## add per token a location for the mako template lookup
    ## @note: the location is defined in the .ini file by
    ##  the entry [linotpTokenModules]

    directories = paths['templates']

    ## add a template path for every token
    modules = get_token_module_list()
    for module in modules:
        mpath = os.path.dirname(module.__file__)
        directories.append(mpath)

    ## add a template path for every resolver
    modules = get_resolver_module_list()
    for module in modules:
        mpath = os.path.dirname(module.__file__)
        directories.append(mpath)

    unique_directories = _uniqify_list(directories)
    log.debug("[load_environment] Template directories: %r" %
              unique_directories)

    config['pylons.app_globals'].mako_lookup = TemplateLookup(
        directories=unique_directories,
        error_handler=handle_mako_error,
        module_directory=os.path.join(app_conf['cache_dir'], 'templates'),
        input_encoding='utf-8',
        default_filters=['escape'],
        imports=['from webhelpers.html import escape'])

    # Setup the SQLAlchemy database engine
    # If we load the linotp.model here, the pylons.config is loaded with
    # the entries from the config file. if it is loaded at the top of the file,
    #the pylons.config does not contain the config file, yet.
    from linotp.model import init_model
    engine = engine_from_config(config, 'sqlalchemy.')
    init_model(engine)

    # CONFIGURATION OPTIONS HERE (note: all config options will override
    # any Pylons config options)

    from linotp.lib.audit.base import getAudit
    audit = getAudit()
    config['audit'] = audit

    ## setup Security provider definition
    try:
        log.debug('[load_environment] loading token list definition')
        g = config['pylons.app_globals']
        g.security_provider.load_config(config)
    except Exception as e:
        log.error("Failed to load security provider definition: %r" % e)
        raise e

    ## load the list of tokenclasses
    try:
        log.debug('[load_environment] loading token list definition')
        (tcl, tpl) = get_token_class_list()

        config['tokenclasses'] = tcl
        g.setTokenclasses(tcl)

        config['tokenprefixes'] = tpl
        g.setTokenprefixes(tpl)

    except Exception as e:
        log.error("Failed to load token class list: %r" % e)
        raise e

    ## load the list of resolvers
    try:
        log.debug('[load_environment] loading resolver list definition')
        (rclass, rname) = get_resolver_class_list()

        ## make this globaly avaliable
        g.setResolverClasses(rclass)
        g.setResolverTypes(rname)

    except Exception as exx:
        log.error("Faild to load the list of resolvers: %r" % exx)
        raise exx

    ## get the help url
    url = config.get("linotpHelp.url", None)

    ## this is a quick hack for the test setup :-(
    ## the big one should handle the timeout when help button
    ## is pressed
    version = pkg_resources.get_distribution("linotp").version
    if not (url) and 'dev' in version:
        url = "file:///usr/share/doc/linotpdoc/html/"

    if url == None:
        version = pkg_resources.get_distribution("linotp").version
        offline_url = "file:///usr/share/doc/linotpdoc/html/"
        # First try to get the help for this specific version
        url = "http://linotp.org/doc/%s/index.html" % version
        try:
            ## Try to open the online url with a timeout of 5 seconds
            ## If the system is definitively offline the request will return immediately
            urllib2.urlopen(url, timeout=5)
        except urllib2.HTTPError:
            # it seems the version does not exist, we set the latest
            url = "http://linotp.org/doc/latest/index.html"
            log.info(
                "the Help URL for the version %s is not available, using the latest one: %s"
                % (version, url))
        except urllib2.URLError:
            # it seems we are offline, using an offline url
            log.warning(
                "it seems we are offline, so using the offline url %s" %
                offline_url)
            url = offline_url
    config['help_url'] = url

    log.debug("[load_environment] done")
    return
Esempio n. 8
0
    def migrate_3_0_0_0(self) -> None:
        """Migrate to linotp3 - to python3+mysql.

        The major challenge for the linotp3 migration is the migration from
        python2+mysql where the mysql driver was using latin1 encoded data,
        though the database might already support utf8.

        Thus we can exclude all fresh created and non-mysql databases
        """

        if self.is_db_untouched():
            log.info("Fresh database - no migration required!")
            return

        # ----------------------------------------------------------------- --

        # with linotp3 we drop all previous audit entries to fix audit signing

        from linotp.lib.audit.base import getAudit

        audit_obj = getAudit()
        audit_obj.delete_all_entries()

        log.info("All limotp2 audit entries deleted.")

        # ----------------------------------------------------------------- --

        if not self.engine.url.drivername.startswith("mysql"):
            log.info(
                "Non mysql databases %r - no migration required.",
                self.engine.url.drivername,
            )
            return

        # ----------------------------------------------------------------- --

        # MYSQL Schema and Data Migration:
        #
        # In case of pre buster db (or restored by a backup), the tabel schema
        # is defined with charset latin1. Thus the schema must be updated first
        # othewise the data migration will fail.
        #
        # Which tables are converted is reported by the schema migration.
        # If no table was converted, we have to assume that the mysql db was
        # created newly (e.g. on buster) where this is done with charset=utf8.
        #
        # In case of a newly (buster) e.g. linotp2 created db, we cannot imply
        # that the data migration has to be done, we only can suggest this.

        log.info("Starting mysql migration")

        # before we adjust the schema, we have to close former sessions,
        # otherwise we would run into a database lock

        model.db.session.close()

        # now we can run the schema migration

        mysql_mig = MYSQL_Migration(self.engine)
        migrated_tables = mysql_mig.migrate_schema()
        mysql_mig.migrate_data(migrated_tables)

        if not migrated_tables:

            config_entry = model.Config(
                Key="utf8_conversion", Value="suggested"
            )
            model.db.session.add(config_entry)

            log.warning(
                "Database conversion step suggested!\n"
                "Please run command:\n"
                " linotp admin fix-db-encoding"
            )