def test_18_censor_connect_string(self):
     self.assertEqual(
         censor_connect_string(
             "sqlite:////home/foo/privacyidea/privacyidea/data.sqlite"),
         "sqlite:////home/foo/privacyidea/privacyidea/data.sqlite")
     self.assertEqual(censor_connect_string("mysql://pi@localhost/pi"),
                      "mysql://pi@localhost/pi")
     self.assertEqual(
         censor_connect_string("mysql://*****:*****@localhost/pi"),
         "mysql://*****:*****@localhost/pi")
     self.assertEqual(censor_connect_string("psql+odbc://pi@localhost/pi"),
                      "psql+odbc://pi@localhost/pi")
     self.assertEqual(
         censor_connect_string(
             "psql+odbc://pi:MySecretPassword123466$@localhost/pi"),
         "psql+odbc://pi:***@localhost/pi")
     self.assertEqual(
         censor_connect_string("mysql://*****:*****@@qqWtGYX@localhost/pi"),
         "mysql://*****:*****@localhost/pi")
     self.assertEqual(
         censor_connect_string(u"mysql://knöbel:föö@localhost/pi"),
         u"mysql://knöbel:***@localhost/pi")
     self.assertEqual(
         censor_connect_string(
             u"oracle+cx_oracle://pi:MySecretPassword1234@localhost:1521/?service_name=my_database"
         ),
         u"oracle+cx_oracle://pi:***@localhost:1521/?service_name=my_database"
     )
Exemple #2
0
    def testconnection(cls, param):
        """
        This function lets you test the to be saved SQL connection.

        :param param: A dictionary with all necessary parameter
                        to test the connection.
        :type param: dict

        :return: Tuple of success and a description
        :rtype: (bool, string)

        Parameters are: Server, Driver, Database, User, Password, Port,
                        Limit, Table, Map
                        Where, Encoding, conParams

        """
        num = -1
        desc = None

        connect_string = cls._create_connect_string(param)
        log.info(u"using the connect string {0!s}".format(
            censor_connect_string(connect_string)))
        engine = create_engine(connect_string)
        # create a configured "Session" class
        Session = scoped_session(sessionmaker(bind=engine))
        session = Session()
        db = SQLSoup(engine, session=Session)
        try:
            TABLE = db.entity(param.get("Table"))
            conditions = cls._append_where_filter([], TABLE,
                                                  param.get("Where"))
            filter_condition = and_(*conditions)
            result = session.query(TABLE).filter(filter_condition).count()

            num = result
            desc = "Found {0:d} users.".format(num)
        except Exception as exx:
            desc = "failed to retrieve users: {0!s}".format(exx)
        finally:
            # We do not want any leftover DB connection, so we first need to close
            # the session such that the DB connection gets returned to the pool (it
            # is still open at that point!) and then dispose the engine such that the
            # checked-in connection gets closed.
            session.close()
            engine.dispose()

        return num, desc
 def test_18_censor_connect_string(self):
     self.assertEqual(censor_connect_string("sqlite:////home/foo/privacyidea/privacyidea/data.sqlite"),
                      "sqlite:////home/foo/privacyidea/privacyidea/data.sqlite")
     self.assertEqual(censor_connect_string("mysql://pi@localhost/pi"),
                      "mysql://pi@localhost/pi")
     self.assertEqual(censor_connect_string("mysql://*****:*****@localhost/pi"),
                      "mysql://*****:*****@localhost/pi")
     self.assertEqual(censor_connect_string("psql+odbc://pi@localhost/pi"),
                      "psql+odbc://pi@localhost/pi")
     self.assertEqual(censor_connect_string("psql+odbc://pi:MySecretPassword123466$@localhost/pi"),
                      "psql+odbc://pi:xxxx@localhost/pi")
     self.assertEqual(censor_connect_string("mysql://*****:*****@@qqWtGYX@localhost/pi"),
                      "mysql://*****:*****@localhost/pi")
     self.assertEqual(censor_connect_string(u"mysql://knöbel:föö@localhost/pi"),
                      u"mysql://knöbel:xxxx@localhost/pi")
 def test_18_censor_connect_string(self):
     self.assertEqual(censor_connect_string("sqlite:////home/foo/privacyidea/privacyidea/data.sqlite"),
                      "sqlite:////home/foo/privacyidea/privacyidea/data.sqlite")
     self.assertEqual(censor_connect_string("mysql://pi@localhost/pi"),
                      "mysql://pi@localhost/pi")
     self.assertEqual(censor_connect_string("mysql://*****:*****@localhost/pi"),
                      "mysql://*****:*****@localhost/pi")
     self.assertEqual(censor_connect_string("psql+odbc://pi@localhost/pi"),
                      "psql+odbc://pi@localhost/pi")
     self.assertEqual(censor_connect_string("psql+odbc://pi:MySecretPassword123466$@localhost/pi"),
                      "psql+odbc://pi:xxxx@localhost/pi")
     self.assertEqual(censor_connect_string("mysql://*****:*****@@qqWtGYX@localhost/pi"),
                      "mysql://*****:*****@localhost/pi")
     self.assertEqual(censor_connect_string(u"mysql://knöbel:föö@localhost/pi"),
                      u"mysql://knöbel:xxxx@localhost/pi")
    def testconnection(cls, param):
        """
        This function lets you test the to be saved SQL connection.

        :param param: A dictionary with all necessary parameter
                        to test the connection.
        :type param: dict

        :return: Tuple of success and a description
        :rtype: (bool, string)

        Parameters are: Server, Driver, Database, User, Password, Port,
                        Limit, Table, Map
                        Where, Encoding, conParams

        """
        num = -1
        desc = None

        connect_string = cls._create_connect_string(param)
        log.info(u"using the connect string {0!s}".format(censor_connect_string(connect_string)))
        engine = create_engine(connect_string)
        # create a configured "Session" class
        Session = scoped_session(sessionmaker(bind=engine))
        session = Session()
        db = SQLSoup(engine, session=Session)
        try:
            TABLE = db.entity(param.get("Table"))
            conditions = cls._append_where_filter([], TABLE,
                                                  param.get("Where"))
            filter_condition = and_(*conditions)
            result = session.query(TABLE).filter(filter_condition).count()

            num = result
            desc = "Found {0:d} users.".format(num)
        except Exception as exx:
            desc = "failed to retrieve users: {0!s}".format(exx)
        finally:
            # We do not want any leftover DB connection, so we first need to close
            # the session such that the DB connection gets returned to the pool (it
            # is still open at that point!) and then dispose the engine such that the
            # checked-in connection gets closed.
            session.close()
            engine.dispose()

        return num, desc
 def _create_engine(self):
     log.info(u"using the connect string {0!s}".format(censor_connect_string(self.connect_string)))
     try:
         log.debug("using pool_size={0!s}, pool_timeout={1!s}, pool_recycle={2!s}".format(
             self.pool_size, self.pool_timeout, self.pool_recycle))
         engine = create_engine(self.connect_string,
                                     encoding=self.encoding,
                                     convert_unicode=False,
                                     pool_size=self.pool_size,
                                     pool_recycle=self.pool_recycle,
                                     pool_timeout=self.pool_timeout)
     except TypeError:
         # The DB Engine/Poolclass might not support the pool_size.
         log.debug("connecting without pool_size.")
         engine = create_engine(self.connect_string,
                                     encoding=self.encoding,
                                     convert_unicode=False)
     return engine
Exemple #7
0
 def _create_engine(self):
     log.info(u"using the connect string "
              u"{0!s}".format(censor_connect_string(self.connect_string)))
     try:
         log.debug("using pool_size={0!s}, pool_timeout={1!s}, pool_recycle={2!s}".format(
             self.pool_size, self.pool_timeout, self.pool_recycle))
         engine = create_engine(self.connect_string,
                                     encoding=self.encoding,
                                     convert_unicode=False,
                                     pool_size=self.pool_size,
                                     pool_recycle=self.pool_recycle,
                                     pool_timeout=self.pool_timeout)
     except TypeError:
         # The DB Engine/Poolclass might not support the pool_size.
         log.debug("connecting without pool_size.")
         engine = create_engine(self.connect_string,
                                     encoding=self.encoding,
                                     convert_unicode=False)
     return engine
Exemple #8
0
 def _create_engine(self):
     """
     :return: a new SQLAlchemy engine connecting to the database specified in PI_AUDIT_SQL_URI.
     """
     # an Engine, which the Session will use for connection
     # resources
     connect_string = self.config.get("PI_AUDIT_SQL_URI", self.config.get(
         "SQLALCHEMY_DATABASE_URI"))
     log.debug("using the connect string {0!s}".format(censor_connect_string(connect_string)))
     try:
         pool_size = self.config.get("PI_AUDIT_POOL_SIZE", 20)
         engine = create_engine(
             connect_string,
             pool_size=pool_size,
             pool_recycle=self.config.get("PI_AUDIT_POOL_RECYCLE", 600))
         log.debug("Using SQL pool size of {}".format(pool_size))
     except TypeError:
         # SQLite does not support pool_size
         engine = create_engine(connect_string)
         log.debug("Using no SQL pool_size.")
     return engine
 def _create_engine(self):
     """
     :return: a new SQLAlchemy engine connecting to the database specified in PI_AUDIT_SQL_URI.
     """
     # an Engine, which the Session will use for connection
     # resources
     connect_string = self.config.get("PI_AUDIT_SQL_URI", self.config.get(
         "SQLALCHEMY_DATABASE_URI"))
     log.debug("using the connect string {0!s}".format(censor_connect_string(connect_string)))
     try:
         pool_size = self.config.get("PI_AUDIT_POOL_SIZE", 20)
         engine = create_engine(
             connect_string,
             pool_size=pool_size,
             pool_recycle=self.config.get("PI_AUDIT_POOL_RECYCLE", 600))
         log.debug("Using SQL pool size of {}".format(pool_size))
     except TypeError:
         # SQLite does not support pool_size
         engine = create_engine(connect_string)
         log.debug("Using no SQL pool_size.")
     return engine