import logging
from http import HTTPStatus
from copy import deepcopy
+from Crypto.Cipher import AES
+from base64 import b64decode, b64encode
__author__ = "Alfonso Tierno <alfonso.tiernosepulveda@telefonica.com>"
"""
self.logger = logging.getLogger(logger_name)
self.master_password = master_password
+ self.secret_key = None
- def db_connect(self, config):
+ def db_connect(self, config, target_version=None):
"""
Connect to database
:param config: Configuration of database
+ :param target_version: if provided it checks if database contains required version, raising exception otherwise.
:return: None or raises DbException on error
"""
- pass
+ raise DbException("Method 'db_connect' not implemented")
def db_disconnect(self):
"""
"""
raise DbException("Method 'replace' not implemented")
- def encrypt(self, value, salt=None):
+ @staticmethod
+ def _join_passwords(passwd_byte, passwd_str):
+ """
+ Modifies passwd_byte with the xor of passwd_str. Used for adding salt, join passwords, etc
+ :param passwd_byte: original password in bytes, 32 byte length
+ :param passwd_str: string salt to be added
+ :return: modified password in bytes
+ """
+ if not passwd_str:
+ return passwd_byte
+ secret_key = bytearray(passwd_byte)
+ for i, b in enumerate(passwd_str.encode()):
+ secret_key[i % 32] ^= b
+ return bytes(secret_key)
+
+ def set_secret_key(self, secret_key):
+ """
+ Set internal secret key used for encryption
+ :param secret_key: byte array length 32 with the secret_key
+ :return: None
+ """
+ assert (len(secret_key) == 32)
+ self.secret_key = self._join_passwords(secret_key, self.master_password)
+
+ def encrypt(self, value, schema_version=None, salt=None):
"""
Encrypt a value
- :param value: value to be encrypted
- :param salt: optional salt to be used
+ :param value: value to be encrypted. It is string/unicode
+ :param schema_version: used for version control. If None or '1.0' no encryption is done.
+ If '1.1' symmetric AES encryption is done
+ :param salt: optional salt to be used. Must be str
:return: Encrypted content of value
"""
- # for the moment return same value. until all modules call this method
- return value
- # raise DbException("Method 'encrypt' not implemented")
-
- def decrypt(self, value, salt=None):
+ if not schema_version or schema_version == '1.0':
+ return value
+ else:
+ if not self.secret_key:
+ raise DbException("Cannot encrypt. Missing secret_key", http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
+ secret_key = self._join_passwords(self.secret_key, salt)
+ cipher = AES.new(secret_key)
+ padded_private_msg = value + ('\0' * ((16-len(value)) % 16))
+ encrypted_msg = cipher.encrypt(padded_private_msg)
+ encoded_encrypted_msg = b64encode(encrypted_msg)
+ return encoded_encrypted_msg.decode("ascii")
+
+ def decrypt(self, value, schema_version=None, salt=None):
"""
Decrypt an encrypted value
- :param value: value to be decrypted
+ :param value: value to be decrypted. It is a base64 string
+ :param schema_version: used for known encryption method used. If None or '1.0' no encryption has been done.
+ If '1.1' symmetric AES encryption has been done
:param salt: optional salt to be used
:return: Plain content of value
"""
- # for the moment return same value. until all modules call this method
- return value
- # raise DbException("Method 'decrypt' not implemented")
+ if not schema_version or schema_version == '1.0':
+ return value
+ else:
+ if not self.secret_key:
+ raise DbException("Cannot decrypt. Missing secret_key", http_code=HTTPStatus.INTERNAL_SERVER_ERROR)
+ secret_key = self._join_passwords(self.secret_key, salt)
+ encrypted_msg = b64decode(value)
+ cipher = AES.new(secret_key)
+ decrypted_msg = cipher.decrypt(encrypted_msg)
+ unpadded_private_msg = decrypted_msg.decode().rstrip('\0')
+ return unpadded_private_msg
def deep_update_rfc7396(dict_to_change, dict_reference, key_list=None):
import pytest
import unittest
from osm_common.dbbase import DbBase, DbException, deep_update
+from os import urandom
def exception_message(message):
def test_db_connect(db_base):
- db_base.db_connect(None)
+ with pytest.raises(DbException) as excinfo:
+ db_base.db_connect(None)
+ assert str(excinfo.value).startswith(exception_message("Method 'db_connect' not implemented"))
def test_db_disconnect(db_base):
assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
+class TestEncryption(unittest.TestCase):
+ def setUp(self):
+ master_password = "Setting a long master password with numbers 123 and capitals AGHBNHD and symbols %&8)!'"
+ db_base1 = DbBase(master_password=master_password)
+ db_base2 = DbBase()
+ # set self.secret_key obtained when connect
+ db_base1.secret_key = DbBase._join_passwords(urandom(32), db_base1.master_password)
+ db_base2.secret_key = DbBase._join_passwords(urandom(32), db_base2.master_password)
+ self.db_base = [db_base1, db_base2]
+
+ def test_encrypt_decrypt(self):
+ TEST = (
+ ("plain text 1 ! ", None),
+ ("plain text 2 with salt ! ", "1afd5d1a-4a7e-4d9c-8c65-251290183106"),
+ ("plain text 3 with usalt ! ", u"1afd5d1a-4a7e-4d9c-8c65-251290183106"),
+ (u"plain unicode 4 ! ", None),
+ (u"plain unicode 5 with salt ! ", "1a000d1a-4a7e-4d9c-8c65-251290183106"),
+ (u"plain unicode 6 with usalt ! ", u"1abcdd1a-4a7e-4d9c-8c65-251290183106"),
+ )
+ for db_base in self.db_base:
+ for value, salt in TEST:
+ # no encryption
+ encrypted = db_base.encrypt(value, schema_version='1.0', salt=salt)
+ self.assertEqual(encrypted, value, "value '{}' has been encrypted".format(value))
+ decrypted = db_base.decrypt(encrypted, schema_version='1.0', salt=salt)
+ self.assertEqual(decrypted, value, "value '{}' has been decrypted".format(value))
+
+ # encrypt/decrypt
+ encrypted = db_base.encrypt(value, schema_version='1.1', salt=salt)
+ self.assertNotEqual(encrypted, value, "value '{}' has not been encrypted".format(value))
+ self.assertIsInstance(encrypted, str, "Encrypted is not ascii text")
+ decrypted = db_base.decrypt(encrypted, schema_version='1.1', salt=salt)
+ self.assertEqual(decrypted, value, "value is not equal after encryption/decryption")
+
+ def test_encrypt_decrypt_salt(self):
+ value = "value to be encrypted!"
+ encrypted = []
+ for db_base in self.db_base:
+ for salt in (None, "salt 1", "1afd5d1a-4a7e-4d9c-8c65-251290183106"):
+ # encrypt/decrypt
+ encrypted.append(db_base.encrypt(value, schema_version='1.1', salt=salt))
+ self.assertNotEqual(encrypted[-1], value, "value '{}' has not been encrypted".format(value))
+ self.assertIsInstance(encrypted[-1], str, "Encrypted is not ascii text")
+ decrypted = db_base.decrypt(encrypted[-1], schema_version='1.1', salt=salt)
+ self.assertEqual(decrypted, value, "value is not equal after encryption/decryption")
+ for i in range(0, len(encrypted)):
+ for j in range(i+1, len(encrypted)):
+ self.assertNotEqual(encrypted[i], encrypted[j],
+ "encryption with different salt contains different result")
+
+
class TestDeepUpdate(unittest.TestCase):
def test_update_dict(self):
# Original, patch, expected result
deep_update(t[0], t[1])
except DbException as e:
print(e)
+
+
+if __name__ == '__main__':
+ unittest.main()
return "database exception Not found entry with filter='{}'".format(filter)
-def replace_exception_message(filter):
- return "database exception Not found entry with filter='{}'".format(filter)
+def replace_exception_message(value):
+ return "database exception Not found entry with _id='{}'".format(value)
def test_constructor():
assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
-@pytest.mark.parametrize("table, filter, indata", [
- ("test", {}, {"_id": 1, "data": 42}),
- ("test", {}, {"_id": 3, "data": 42}),
- ("test", {"_id": 1}, {"_id": 3, "data": 42}),
- ("test", {"_id": 3}, {"_id": 3, "data": 42}),
- ("test", {"data": 1}, {"_id": 3, "data": 42}),
- ("test", {"data": 3}, {"_id": 3, "data": 42}),
- ("test", {"_id": 1, "data": 1}, {"_id": 3, "data": 42}),
- ("test", {"_id": 3, "data": 3}, {"_id": 3, "data": 42})])
-def test_replace(db_memory_with_data, table, filter, indata):
- result = db_memory_with_data.replace(table, filter, indata)
+@pytest.mark.parametrize("table, _id, indata", [
+ ("test", 1, {"_id": 1, "data": 42}),
+ ("test", 1, {"_id": 1, "data": 42, "kk": 34}),
+ ("test", 1, {"_id": 1}),
+ ("test", 2, {"_id": 2, "data": 42}),
+ ("test", 2, {"_id": 2, "data": 42, "kk": 34}),
+ ("test", 2, {"_id": 2}),
+ ("test", 3, {"_id": 3, "data": 42}),
+ ("test", 3, {"_id": 3, "data": 42, "kk": 34}),
+ ("test", 3, {"_id": 3})])
+def test_replace(db_memory_with_data, table, _id, indata):
+ result = db_memory_with_data.replace(table, _id, indata)
assert result == {"updated": 1}
assert len(db_memory_with_data.db) == 1
assert table in db_memory_with_data.db
assert indata in db_memory_with_data.db[table]
-@pytest.mark.parametrize("table, filter, indata", [
- ("test", {}, {'_id': 1, 'data': 1}),
- ("test", {}, {'_id': 2, 'data': 1}),
- ("test", {}, {'_id': 1, 'data': 2}),
- ("test", {'_id': 1}, {'_id': 1, 'data': 1}),
- ("test", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
- ("test_table", {}, {'_id': 1, 'data': 1}),
- ("test_table", {}, {'_id': 2, 'data': 1}),
- ("test_table", {}, {'_id': 1, 'data': 2}),
- ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
- ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})])
-def test_replace_without_data_exception(db_memory, table, filter, indata):
+@pytest.mark.parametrize("table, _id, indata", [
+ ("test", 1, {"_id": 1, "data": 42}),
+ ("test", 2, {"_id": 2}),
+ ("test", 3, {"_id": 3})])
+def test_replace_without_data_exception(db_memory, table, _id, indata):
with pytest.raises(DbException) as excinfo:
- db_memory.replace(table, filter, indata, fail_on_empty=True)
- assert str(excinfo.value) == (empty_exception_message() + replace_exception_message(filter))
+ db_memory.replace(table, _id, indata, fail_on_empty=True)
+ assert str(excinfo.value) == (replace_exception_message(_id))
assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
-@pytest.mark.parametrize("table, filter, indata", [
- ("test", {}, {'_id': 1, 'data': 1}),
- ("test", {}, {'_id': 2, 'data': 1}),
- ("test", {}, {'_id': 1, 'data': 2}),
- ("test", {'_id': 1}, {'_id': 1, 'data': 1}),
- ("test", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1}),
- ("test_table", {}, {'_id': 1, 'data': 1}),
- ("test_table", {}, {'_id': 2, 'data': 1}),
- ("test_table", {}, {'_id': 1, 'data': 2}),
- ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
- ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})])
-def test_replace_without_data_none(db_memory, table, filter, indata):
- result = db_memory.replace(table, filter, indata, fail_on_empty=False)
+@pytest.mark.parametrize("table, _id, indata", [
+ ("test", 1, {"_id": 1, "data": 42}),
+ ("test", 2, {"_id": 2}),
+ ("test", 3, {"_id": 3})])
+def test_replace_without_data_none(db_memory, table, _id, indata):
+ result = db_memory.replace(table, _id, indata, fail_on_empty=False)
assert result is None
-@pytest.mark.parametrize("table, filter, indata", [
- ("test_table", {}, {'_id': 1, 'data': 1}),
- ("test_table", {}, {'_id': 2, 'data': 1}),
- ("test_table", {}, {'_id': 1, 'data': 2}),
- ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
- ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})])
-def test_replace_with_data_exception(db_memory_with_data, table, filter, indata):
+@pytest.mark.parametrize("table, _id, indata", [
+ ("test", 11, {"_id": 11, "data": 42}),
+ ("test", 12, {"_id": 12}),
+ ("test", 33, {"_id": 33})])
+def test_replace_with_data_exception(db_memory_with_data, table, _id, indata):
with pytest.raises(DbException) as excinfo:
- db_memory_with_data.replace(table, filter, indata, fail_on_empty=True)
- assert str(excinfo.value) == (empty_exception_message() + replace_exception_message(filter))
+ db_memory_with_data.replace(table, _id, indata, fail_on_empty=True)
+ assert str(excinfo.value) == (replace_exception_message(_id))
assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND
-@pytest.mark.parametrize("table, filter, indata", [
- ("test_table", {}, {'_id': 1, 'data': 1}),
- ("test_table", {}, {'_id': 2, 'data': 1}),
- ("test_table", {}, {'_id': 1, 'data': 2}),
- ("test_table", {'_id': 1}, {'_id': 1, 'data': 1}),
- ("test_table", {'_id': 1, 'data': 1}, {'_id': 1, 'data': 1})])
-def test_replace_with_data_none(db_memory_with_data, table, filter, indata):
- result = db_memory_with_data.replace(table, filter, indata, fail_on_empty=False)
+@pytest.mark.parametrize("table, _id, indata", [
+ ("test", 11, {"_id": 11, "data": 42}),
+ ("test", 12, {"_id": 12}),
+ ("test", 33, {"_id": 33})])
+def test_replace_with_data_none(db_memory_with_data, table, _id, indata):
+ result = db_memory_with_data.replace(table, _id, indata, fail_on_empty=False)
assert result is None
False])
def test_replace_generic_exception(db_memory_with_data, fail_on_empty):
table = 'test'
- filter = {}
+ _id = {}
indata = {'_id': 1, 'data': 1}
db_memory_with_data._find = MagicMock(side_effect=Exception())
with pytest.raises(DbException) as excinfo:
- db_memory_with_data.replace(table, filter, indata, fail_on_empty=fail_on_empty)
+ db_memory_with_data.replace(table, _id, indata, fail_on_empty=fail_on_empty)
assert str(excinfo.value) == empty_exception_message()
assert excinfo.value.http_code == http.HTTPStatus.NOT_FOUND