Island: Use cryptography.fernet to generate key in DataStoreEncryptor

and RepositoryEncryptor

We changed our encryption code to use cryptography.fernet instead of
pycryptodome. Using secrets.token_bytes() with fernet was causing
padding and encoding issues. This is a quicker and easier solution, and
also probably more reliable since everything to do with encryption is
from the same module now.
This commit is contained in:
Shreya Malviya 2022-07-15 11:51:15 +05:30
parent 5eb77dcbb6
commit f8eeda1e6f
2 changed files with 6 additions and 4 deletions

View File

@ -1,8 +1,9 @@
import os
import secrets
from pathlib import Path
from typing import Union
from cryptography.fernet import Fernet
from monkey_island.cc.server_utils.file_utils import open_new_securely_permissioned_file
from .i_encryptor import IEncryptor
@ -37,7 +38,7 @@ class DataStoreEncryptor(IEncryptor):
return KeyBasedEncryptor(plaintext_key)
def _create_key(self) -> KeyBasedEncryptor:
plaintext_key = secrets.token_bytes(DataStoreEncryptor._KEY_LENGTH_BYTES)
plaintext_key = Fernet.generate_key()
encrypted_key = self._password_based_encryptor.encrypt(plaintext_key)
with open_new_securely_permissioned_file(str(self._key_file), "wb") as f:

View File

@ -1,6 +1,7 @@
import secrets
from pathlib import Path
from cryptography.fernet import Fernet
from monkey_island.cc.server_utils.file_utils import open_new_securely_permissioned_file
from . import ILockableEncryptor, LockedKeyError, ResetKeyError, UnlockError
@ -37,7 +38,7 @@ class RepositoryEncryptor(ILockableEncryptor):
return KeyBasedEncryptor(plaintext_key)
def _create_key(self) -> KeyBasedEncryptor:
plaintext_key = secrets.token_bytes(RepositoryEncryptor._KEY_LENGTH_BYTES)
plaintext_key = Fernet.generate_key()
encrypted_key = self._password_based_encryptor.encrypt(plaintext_key)
with open_new_securely_permissioned_file(str(self._key_file), "wb") as f: