def fuzzify(engine, config):
    """Do the actual fuzzification based on the loaded attributes of
       the models."""
    modelObjects = {}
    Session = sessionmaker(bind=engine)
    session = Session()
    metadata = MetaData(bind=engine, reflect=True)
    cascade_fkeys(metadata)

    for table_name, columns in config.items():
        tables = []
        if table_name in metadata.tables.keys():
            tables.append(utils.get_table(engine, table_name))
        if 'shadow_' + table_name in metadata.tables.keys():
            tables.append(utils.get_table(engine, 'shadow_' + table_name))
        for table in tables:
            print "Doing table: " + str(table)
            modelObjects[table] = type('Model_' + str(table), (object, ), {})
            mapper(modelObjects[table], table)
            q = session.query(modelObjects[table])
            for row in q.all():
                for column, column_type in columns.items():
                    if hasattr(row, column):
                        before = getattr(row, column)
                        after = randomness(before, column_type)
                        setattr(row, column, after)
    cascade_fkeys(metadata, restore=True)
    session.commit()
    session.flush()
def fuzzify(engine, config):
    """Do the actual fuzzification based on the loaded attributes of
       the models."""
    Session = sessionmaker(bind=engine)
    session = Session()
    metadata = MetaData(bind=engine, reflect=True)
    cascade_fkeys(metadata)

    for model_name, columns in config.items():
        table_name = getattr(models, model_name).__tablename__
        tables = [getattr(models, model_name)]
        if 'shadow_' + table_name in metadata.tables.keys():
            tables.append(utils.get_table(engine, 'shadow_' + table_name))
        for table in tables:
            q = session.query(table)
            for row in q.all():
                for column, column_type in columns:
                    setattr(row, column,
                            randomness(getattr(row, column), column_type))

    session.commit()
    cascade_fkeys(metadata, restore=True)
Example #3
0
    def _transmogrify(self, string, coltype, anontype):
        """ Anonymise the provided string, based upon it's type """
        # Note(mrda): TODO: handle mapping

        # Handle quoted strings
        need_single_quotes = False
        if string[0] == "'" and string[-1] == "'":
            need_single_quotes = True
            string = string[1:-1]

        randomised = randomise.randomness(string, anontype)

        if CONF.debug:
            print (
                '    ....transmogrifying from value "%s" to value "%s"'
                " with type %s, anon type %s" % (string, randomised, coltype, anontype)
            )

        if need_single_quotes:
            randomised = "'" + randomised + "'"

        return randomised