예제 #1
0
def setup_app(conf, conf_global=None, unitTest=False):
    '''
    setup_app is the hook, which is called, when the application is created

    :param conf: the application configuration

    :return: - nothing -
    '''

    if unitTest is True:
        log.debug("Deleting previous tables...")
        meta.metadata.drop_all(bind=meta.engine)

    # Create the tables if they don't already exist
    log.info("Creating tables...")
    meta.metadata.create_all(bind=meta.engine)

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

    # for the cloud mode we require the admin_user table to
    # manage the admin users to allow password setting

    if 'linotpadmin.username' in conf and 'linotpadmin.password' in conf:

        from linotp.lib.tools.set_password import SetPasswordHandler
        from linotp.lib.tools.set_password import DataBaseContext

        db_context = DataBaseContext(sql_url=meta.engine.url)

        SetPasswordHandler.create_table(db_context)

        # create the initial admin
        admin_user = conf.get('linotpadmin.username', '')
        admin_pw = conf.get('linotpadmin.password', '')

        if admin_user and admin_pw:
            SetPasswordHandler.create_admin_user(db_context,
                                                 username=admin_user,
                                                 crypted_password=admin_pw)

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

    #
    # hook for schema upgrade -
    # - called by paster setup-app or on the first request to linotp
    #

    # define the most recent target version
    sql_data_model_version = "2.9.1.0"

    # get the actual version - should be None or should be the same
    # if migration is finished
    current_data_model_version = get_config('sql_data_model_version')

    #
    # in case of unitTest the database has been erased and recreated - thus
    # the db model update is not require - so we have already the most recent
    # target version

    if unitTest:
        current_data_model_version = sql_data_model_version
        set_config('sql_data_model_version',
                   sql_data_model_version,
                   typ='text')

    if current_data_model_version != sql_data_model_version:
        run_data_model_migration(meta, target_version=sql_data_model_version)
        set_config('sql_data_model_version',
                   sql_data_model_version,
                   typ='text')

    #
    # create the secret key file if it does not exist
    #

    if "linotpSecretFile" in conf:
        filename = conf.get("linotpSecretFile")
        try:
            open(filename)
        except IOError:
            log.warning(
                "The Linotp Secret File could not be found. " +
                "Creating a new one at %s", filename)
            f_handle = open(filename, 'ab+')
            secret = os.urandom(32 * 5)
            f_handle.write(secret)
            f_handle.close()
            os.chmod(filename, 0400)
        log.debug("linotpSecretFile: %s", filename)

    set_defaults()

    Session.commit()

    init_logging_config()

    log.info("Successfully set up.")
예제 #2
0
파일: base.py 프로젝트: super-rain/LinOTP
def setup_app(conf, conf_global=None, unitTest=False):
    '''
    setup_app is the hook, which is called, when the application is created

    :param conf: the application configuration

    :return: - nothing -
    '''

    if unitTest is True:
        log.debug("Deleting previous tables...")
        meta.metadata.drop_all(bind=meta.engine)

    # Create the tables if they don't already exist
    log.info("Creating tables...")
    meta.metadata.create_all(bind=meta.engine)

    #
    # hook for schema upgrade -
    # - called by paster setup-app or on the first request to linotp
    #

    # define the most recent target version
    sql_data_model_version = "2.9.1.0"

    # get the actual version - should be None or should be the same
    # if migration is finished
    current_data_model_version = get_config('sql_data_model_version')

    #
    # in case of unitTest the database has been erased and recreated - thus
    # the db model update is not require - so we have already the most recent
    # target version

    if unitTest:
        current_data_model_version = sql_data_model_version
        set_config('sql_data_model_version',
                   sql_data_model_version,
                   typ='text')

    if current_data_model_version != sql_data_model_version:
        run_data_model_migration(meta, target_version=sql_data_model_version)
        set_config('sql_data_model_version',
                   sql_data_model_version,
                   typ='text')

    #
    # create the secret key file if it does not exist
    #

    if "linotpSecretFile" in conf:
        filename = conf.get("linotpSecretFile")
        try:
            open(filename)
        except IOError:
            log.warning(
                "The Linotp Secret File could not be found. " +
                "Creating a new one at %s", filename)
            f_handle = open(filename, 'ab+')
            secret = os.urandom(32 * 5)
            f_handle.write(secret)
            f_handle.close()
            os.chmod(filename, 0400)
        log.debug("linotpSecretFile: %s", filename)

    set_defaults()

    Session.commit()

    init_logging_config()

    log.info("Successfully set up.")