From 321dd2c55eeb7c25de23c1607ff944fb657194a9 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 1 Jun 2021 15:51:44 +0300 Subject: [PATCH] Improved configuration export related code by making it cleaner/more trivial --- .../cc/resources/configuration_export.py | 12 ++------ .../cc/services/utils/config_encryption.py | 2 +- .../ExportConfigModal.tsx | 28 +++++++++---------- .../services/utils/test_config_encryption.py | 4 +-- 4 files changed, 19 insertions(+), 27 deletions(-) diff --git a/monkey/monkey_island/cc/resources/configuration_export.py b/monkey/monkey_island/cc/resources/configuration_export.py index b3de58f92..40dd90044 100644 --- a/monkey/monkey_island/cc/resources/configuration_export.py +++ b/monkey/monkey_island/cc/resources/configuration_export.py @@ -9,10 +9,6 @@ from monkey_island.cc.services.utils.config_encryption import encrypt_config class ConfigurationExport(flask_restful.Resource): - @jwt_required - def get(self): - return {"to_export": self.config_to_return, "is_plaintext": self.is_plaintext} - @jwt_required def post(self): data = json.loads(request.data) @@ -20,11 +16,9 @@ class ConfigurationExport(flask_restful.Resource): plaintext_config = ConfigService.get_config() - self.config_to_return = plaintext_config - self.is_plaintext = True + config_export = plaintext_config if should_encrypt: password = data["password"] - self.config_to_return = encrypt_config(plaintext_config, password) - self.is_plaintext = False + config_export = encrypt_config(plaintext_config, password) - return self.get() + return {"config_export": config_export, "encrypted": should_encrypt} diff --git a/monkey/monkey_island/cc/services/utils/config_encryption.py b/monkey/monkey_island/cc/services/utils/config_encryption.py index dbe09db4c..9a3783a47 100644 --- a/monkey/monkey_island/cc/services/utils/config_encryption.py +++ b/monkey/monkey_island/cc/services/utils/config_encryption.py @@ -20,7 +20,7 @@ def encrypt_config(config: Dict, password: str) -> str: ciphertext_b64 = base64.b64encode(ciphertext_config_stream.getvalue()) - return str(ciphertext_b64) + return ciphertext_b64.decode() def decrypt_config(cyphertext: str, password: str) -> Dict: diff --git a/monkey/monkey_island/cc/ui/src/components/configuration-components/ExportConfigModal.tsx b/monkey/monkey_island/cc/ui/src/components/configuration-components/ExportConfigModal.tsx index a6dc47c2d..0b1df67e2 100644 --- a/monkey/monkey_island/cc/ui/src/components/configuration-components/ExportConfigModal.tsx +++ b/monkey/monkey_island/cc/ui/src/components/configuration-components/ExportConfigModal.tsx @@ -12,7 +12,6 @@ type Props = { } const ConfigExportModal = (props: Props) => { - // TODO implement the back end const configExportEndpoint = '/api/configuration/export'; const [pass, setPass] = useState(''); @@ -34,20 +33,19 @@ const ConfigExportModal = (props: Props) => { }) } ) - .then(res => res.json()) - .then(res => { - let configToExport = res['to_export']; - if (res['is_plaintext'] === true) { - const configAsBinary = new Blob([JSON.stringify(configToExport, null, 2)], - {type: 'text/plain;charset=utf-8'}); - FileSaver.saveAs(configAsBinary, 'monkey.conf'); - } - else { - const configAsBinary = new Blob([configToExport.slice(2, -1)]); - FileSaver.saveAs(configAsBinary, 'monkey.conf'); - - } - }) + .then(res => res.json()) + .then(res => { + let configToExport = res['config_export']; + if (res['encrypted']) { + configToExport = new Blob([configToExport]); + } else { + configToExport = new Blob( + [JSON.stringify(configToExport, null, 2)], + {type: 'text/plain;charset=utf-8'} + ); + } + FileSaver.saveAs(configToExport, 'monkey.conf'); + }) } return ( diff --git a/monkey/tests/unit_tests/monkey_island/cc/services/utils/test_config_encryption.py b/monkey/tests/unit_tests/monkey_island/cc/services/utils/test_config_encryption.py index 8455ba593..87996756f 100644 --- a/monkey/tests/unit_tests/monkey_island/cc/services/utils/test_config_encryption.py +++ b/monkey/tests/unit_tests/monkey_island/cc/services/utils/test_config_encryption.py @@ -21,6 +21,6 @@ def plaintext_config(data_for_tests_dir): def test_encrypt_decrypt_config(plaintext_config): - encrypted_config = encrypt_config(plaintext_config, PASSWORD) # str of the form: `b'a1b2c3'` - encrypted_config = encrypted_config[2:-1] # so we slice it here + encrypted_config = encrypt_config(plaintext_config, PASSWORD) + encrypted_config = encrypted_config assert decrypt_config(encrypted_config, PASSWORD) == plaintext_config