def test_client_integrity_bpe(self): # Full Integrity Test including flask self.c = client.Client(self.user) self.c.set_password(self.password) self.c.metric = "offset-0.1" # Redirect requests to flask target = self.target # Server Records: matches: List[Record] = self.matches str_backend = StorageServer(test_dir) with self.str_app.app_context(): for m in matches: str_backend.store_record( to_base64(m.get_long_hash()), json.dumps(m.get_encrypted_record(self.enc_keys[0])), 'OwnerA') # PSI Matches psi_matches = [] for m in matches: psi_matches.append(m.get_psi_index()) s = Session(True) with patch("requests.get", s.get), \ patch("requests.post", s.post), \ patch.object(self.c, "_receive_ots", Mock(return_value=self.enc_keys_int[:3])): res = self.c.full_retrieve(target) # Set hash key for comparison for r in res: r.set_hash_key(self.hash_key) # Compare without order for m in matches: self.assertIn(m, res) for r in res: self.assertIn(r, matches)
def execute_psi(self: Task, port: int) -> None: """Execute PSI with celery.""" log.info(f"Celery offering PSI on Port {port}.") self.time_limit = 3600 self.update_state(state='STARTED') StorageServer.offer_psi(port=port) self.update_state(state='SUCCESS')
def _batch_store_records(record_list: List[List[str]], username: str) -> None: """ Batch storage of records. :param record_list: List of records to store of the following form: [ ('hash1', 'record1', 'owner1'), ('hash2', 'record2', 'owner2') ] :return: None """ StorageServer.batch_store_records_db(record_list) task = insert_bloom.delay(record_list) database.add_task(username, UserType.OWNER, task.id, TaskType.BLOOM_INSERT)
def batch_retrieve_records() -> str: """ Retrieve multiple records for the authenticated user. Requires JSON as POST data: { 'hashes': [ Base64(Hash-1) [str], Base64(Hash-2) [str], ... ] } :return: jsonified dict containing records on success and an error message otherwise. On success: 'records': [ ('Base64(HASH-1)', 'json.dumps(CIPHERTEXT-1)'), ('Base64(HASH-1)', 'json.dumps(CIPHERTEXT-2)'), ('Base64(HASH-2)', 'json.dumps(CIPHERTEXT-3)') ] """ try: if request.json is None: raise KeyError("Missing POST value 'hashes'.") hash_list = request.json['hashes'] r = StorageServer.batch_get_records(hash_list, client_auth.username()) except KeyError: return jsonify({ 'success': False, 'msg': "Missing POST value 'hashes'." }) return jsonify({'success': True, 'records': r})
def retrieve_record() -> str: """ Retrieve record for the authenticated user. Requires JSON as POST data: { 'hash': Base64(Hash) [str] } :return: Dict containing ist of records matching the hash or error msg 'records': [ ['Base64(HASH)', 'json.dumps(CIPHERTEXT-1)'], ['Base64(HASH)', 'json.dumps(CIPHERTEXT-2)'] ] """ try: if request.json is None or 'hash' not in request.json: raise ValueError("Missing POST value 'hash'.") h = request.json['hash'] r = StorageServer.get_record(h, client_auth.username()) except ValueError as e: return jsonify({'success': False, 'msg': str(e)}) return jsonify({'success': True, 'records': r})
def test_data_provider_int(self): # Full integrity Test including flask self.dp = data_provider.DataProvider(self.provider) self.dp.set_password(self.password) str_backend = StorageServer(test_dir) with self.str_app.app_context(): for r in self.sr: # check that bloom filter is empty b = str_backend.bloom self.assertNotIn(to_base64(r.get_long_hash()), b) # Check that DB empty res = str_backend.batch_get_records( [to_base64(r.get_long_hash()) for r in self.sr], "client") # Decrypt result = [ Record.from_ciphertext(json.loads(r), self.enc_keys[0]) for h, r in res ] self.assertEqual([], result) s = Session(True) with patch("requests.get", s.get), \ patch("requests.post", s.post), \ patch.object(self.dp, "_receive_ots", Mock(return_value=self.enc_keys_int[:len(self.sr)])): self.dp.store_records(self.sr) str_backend = StorageServer(test_dir) with self.str_app.app_context(): for r in self.sr: # check that records are in bloom filter b = str_backend.bloom self.assertIn(to_base64(r.get_long_hash()), b) # Check records in db res = str_backend.batch_get_records( [to_base64(r.get_long_hash()) for r in self.sr], "client") # Decrypt result = [ Record.from_ciphertext(json.loads(r), self.enc_keys[0]) for h, r in res ] for m in self.sr: self.assertIn(m, result) for r in result: self.assertIn(r, self.sr)
def mock_insert(records): StorageServer(test_dir).batch_store_records_bloom(records) return Mock()
def get_storageserver_backend() -> StorageServer: """Return the flask request's backend StorageServer object and create one if none exists.""" if 'storageserver' not in g: g.storageserver = StorageServer(app.config['DATA_DIR']) return g.storageserver