Beispiel #1
0
    def _init_u1db_sqlcipher_backend(self):
        """
        Initialize the U1DB SQLCipher database for local storage.

        Instantiates a modified twisted adbapi that will maintain a threadpool
        with a u1db-sqclipher connection for each thread, and will return
        deferreds for each u1db query.

        Currently, Soledad uses the default SQLCipher cipher, i.e.
        'aes-256-cbc'. We use scrypt to derive a 256-bit encryption key,
        and internally the SQLCipherDatabase initialization uses the 'raw
        PRAGMA key' format to handle the key to SQLCipher.
        """
        tohex = binascii.b2a_hex
        # sqlcipher only accepts the hex version
        key = tohex(self._secrets.get_local_storage_key())
        sync_db_key = tohex(self._secrets.get_sync_db_key())

        opts = SQLCipherOptions(
            self._local_db_path,
            key,
            is_raw_key=True,
            create=True,
            defer_encryption=self._defer_encryption,
            sync_db_key=sync_db_key,
        )
        self._sqlcipher_opts = opts
        self._dbpool = adbapi.getConnectionPool(opts)
Beispiel #2
0
    def setUp(self):
        SoledadWithCouchServerMixin.setUp(self)
        # config info
        self.db1_file = os.path.join(self.tempdir, "db1.u1db")
        os.unlink(self.db1_file)
        self.db_pass = DBPASS
        self.email = ADDRESS

        # get a random prefix for each test, so we do not mess with
        # concurrency during initialization and shutting down of
        # each local db.
        self.rand_prefix = ''.join(
            map(lambda x: random.choice(string.ascii_letters), range(6)))

        # open test dbs: db1 will be the local sqlcipher db (which
        # instantiates a syncdb). We use the self._soledad instance that was
        # already created on some setUp method.
        import binascii
        tohex = binascii.b2a_hex
        key = tohex(self._soledad.secrets.get_local_storage_key())
        sync_db_key = tohex(self._soledad.secrets.get_sync_db_key())
        dbpath = self._soledad._local_db_path

        self.opts = SQLCipherOptions(dbpath,
                                     key,
                                     is_raw_key=True,
                                     create=False,
                                     defer_encryption=True,
                                     sync_db_key=sync_db_key)
        self.db1 = SQLCipherDatabase(self.opts)

        self.db2 = couch.CouchDatabase.open_database(urljoin(
            'http://localhost:' + str(self.wrapper.port), 'test'),
                                                     create=True,
                                                     ensure_ddocs=True)
Beispiel #3
0
    def __init__(self, path, key=None):
        self.path = os.path.abspath(os.path.join(path, 'soledad_blob.db'))
        if not key:
            raise ValueError('key cannot be None')
        backend = 'pysqlcipher.dbapi2'
        opts = SQLCipherOptions('/tmp/ignored', key)
        pragmafun = partial(pragmas.set_init_pragmas, opts=opts)
        openfun = _sqlcipherInitFactory(pragmafun)

        self.dbpool = ConnectionPool(backend,
                                     self.path,
                                     check_same_thread=False,
                                     timeout=5,
                                     cp_openfun=openfun,
                                     cp_min=1,
                                     cp_max=2,
                                     cp_name='blob_pool')
Beispiel #4
0
    def setUp(self):
        """
        Need to explicitely invoke inicialization on all bases.
        """
        SoledadWithCouchServerMixin.setUp(self)
        self.server = self.server_thread = None
        self.startTwistedServer()
        self.syncer = None

        # config info
        self.db1_file = os.path.join(self.tempdir, "db1.u1db")
        os.unlink(self.db1_file)
        self.db_pass = DBPASS
        self.email = ADDRESS

        # get a random prefix for each test, so we do not mess with
        # concurrency during initialization and shutting down of
        # each local db.
        self.rand_prefix = ''.join(
            map(lambda x: random.choice(string.ascii_letters), range(6)))

        # open test dbs: db1 will be the local sqlcipher db (which
        # instantiates a syncdb). We use the self._soledad instance that was
        # already created on some setUp method.
        import binascii
        tohex = binascii.b2a_hex
        key = tohex(self._soledad.secrets.get_local_storage_key())
        sync_db_key = tohex(self._soledad.secrets.get_sync_db_key())
        dbpath = self._soledad._local_db_path

        self.opts = SQLCipherOptions(dbpath,
                                     key,
                                     is_raw_key=True,
                                     create=False,
                                     defer_encryption=True,
                                     sync_db_key=sync_db_key)
        self.db1 = SQLCipherDatabase(self.opts)

        self.db2 = self.request_state._create_database(replica_uid='test')
Beispiel #5
0
 def _get_dbpool(self):
     tmpdb = os.path.join(self.tempdir, "test.soledad")
     opts = SQLCipherOptions(tmpdb, "secret", create=True)
     return adbapi.getConnectionPool(opts)
Beispiel #6
0
def debug(*args):
    if not silent:
        print(*args)


debug("[+] db path:", tmpdb)
debug("[+] num docs", numdocs)

if os.path.isfile(tmpdb):
    debug("[+] Removing existing db file...")
    os.remove(tmpdb)

start_time = datetime.datetime.now()

opts = SQLCipherOptions(tmpdb, "secret", create=True)
dbpool = adbapi.getConnectionPool(opts)


def createDoc(doc, doc_id):
    return dbpool.runU1DBQuery("create_doc", doc, doc_id=doc_id)


db_indexes = {'by-chash': ['chash'], 'by-number': ['number']}


def create_indexes(_):
    deferreds = []
    for index, definition in db_indexes.items():
        d = dbpool.runU1DBQuery("create_index", index, *definition)
        deferreds.append(d)
Beispiel #7
0
def make_sqlcipher_database_for_test(test, replica_uid):
    db = SQLCipherDatabase(SQLCipherOptions(':memory:', PASSWORD))
    db._set_replica_uid(replica_uid)
    return db
Beispiel #8
0
def sqlcipher_open(path, passphrase, create=True, document_factory=None):
    return SQLCipherDatabase(SQLCipherOptions(path, passphrase, create=create))
Beispiel #9
0
 def __init__(self, dbname, ntry):
     self._try = ntry
     self._is_initialized_invocations = 0
     SQLCipherDatabase.__init__(self,
                                SQLCipherOptions(dbname, PASSWORD))