From 500f270aa97553389635875a24983b738628fc59 Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Wed, 2 Jun 2021 15:15:21 +0300 Subject: [PATCH] Fixed, improved and tested configuration import and export. --- .../monkey_island/cc/resources/configuration_import.py | 8 ++++++-- .../monkey_island/cc/services/utils/config_encryption.py | 7 +------ .../configuration-components/ImportConfigModal.tsx | 9 ++++----- .../cc/services/utils/test_config_encryption.py | 8 +++++--- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/monkey/monkey_island/cc/resources/configuration_import.py b/monkey/monkey_island/cc/resources/configuration_import.py index 9e4731d7f..7e3327cbe 100644 --- a/monkey/monkey_island/cc/resources/configuration_import.py +++ b/monkey/monkey_island/cc/resources/configuration_import.py @@ -1,6 +1,7 @@ import json import logging from dataclasses import dataclass +from json.decoder import JSONDecodeError import flask_restful from flask import request @@ -35,8 +36,11 @@ class ConfigurationImport(flask_restful.Resource): def post(self): request_contents = json.loads(request.data) try: - decrypt_config(request_contents["config"], request_contents["password"]) - ConfigurationImport.import_config(request_contents["config"]) + try: + config = json.loads(request_contents["config"]) + except JSONDecodeError: + config = decrypt_config(request_contents["config"], request_contents["password"]) + ConfigurationImport.import_config(config) return ResponseContents().form_response() except InvalidCredentialsError: return ResponseContents( diff --git a/monkey/monkey_island/cc/services/utils/config_encryption.py b/monkey/monkey_island/cc/services/utils/config_encryption.py index 086ad00a6..9c2858db3 100644 --- a/monkey/monkey_island/cc/services/utils/config_encryption.py +++ b/monkey/monkey_island/cc/services/utils/config_encryption.py @@ -31,12 +31,7 @@ def decrypt_config(cyphertext: Union[str, dict], password: str) -> Dict: if not password: raise NoCredentialsError - try: - cyphertext = base64.b64decode(cyphertext) - except TypeError: - logger.info("Configuration doesn't require decryption.") - return cyphertext - + cyphertext = base64.b64decode(cyphertext) ciphertext_config_stream = io.BytesIO(cyphertext) dec_plaintext_config_stream = io.BytesIO() diff --git a/monkey/monkey_island/cc/ui/src/components/configuration-components/ImportConfigModal.tsx b/monkey/monkey_island/cc/ui/src/components/configuration-components/ImportConfigModal.tsx index 6404dc317..9da33cd4a 100644 --- a/monkey/monkey_island/cc/ui/src/components/configuration-components/ImportConfigModal.tsx +++ b/monkey/monkey_island/cc/ui/src/components/configuration-components/ImportConfigModal.tsx @@ -18,14 +18,14 @@ const ConfigImportModal = (props: Props) => { const configImportEndpoint = '/api/configuration/import'; const [uploadStatus, setUploadStatus] = useState(UploadStatuses.clean); - const [configContents, setConfigContents] = useState(''); + const [configContents, setConfigContents] = useState(null); const [password, setPassword] = useState(''); const [showPassword, setShowPassword] = useState(false); const [errorMessage, setErrorMessage] = useState(''); const authComponent = new AuthComponent({}); useEffect(() => { - if (configContents !== '') { + if (configContents !== null) { sendConfigToServer(); } }, [configContents]) @@ -54,7 +54,6 @@ const ConfigImportModal = (props: Props) => { } else { setUploadStatus(UploadStatuses.success); } - console.log(res['import_status']); return res['import_status']; }) } @@ -66,7 +65,7 @@ const ConfigImportModal = (props: Props) => { function resetState() { setUploadStatus(UploadStatuses.clean); setPassword(''); - setConfigContents(''); + setConfigContents(null); setErrorMessage(''); setShowPassword(false); } @@ -74,7 +73,7 @@ const ConfigImportModal = (props: Props) => { function uploadFile(event) { let reader = new FileReader(); reader.onload = (event) => { - setConfigContents(JSON.stringify(event.target.result)) + setConfigContents(event.target.result); }; reader.readAsText(event.target.files[0]); 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 8f3ba4cbf..4bc21c041 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 @@ -7,7 +7,7 @@ from tests.unit_tests.monkey_island.cc.services.utils.cyphertexts_for_encryption MALFORMED_CYPHER_TEXT_TOO_SHORT, ) -from common.utils.exceptions import InvalidCredentialsError +from common.utils.exceptions import InvalidCredentialsError, NoCredentialsError from monkey_island.cc.services.utils.config_encryption import decrypt_config, encrypt_config MONKEY_CONFIGS_DIR_PATH = "monkey_configs" @@ -43,5 +43,7 @@ def test_encrypt_decrypt_config__malformed(): decrypt_config(MALFORMED_CYPHER_TEXT_CORRUPTED, PASSWORD) -def test_decrypt_config__unencrypted(plaintext_config): - assert plaintext_config == decrypt_config(plaintext_config, PASSWORD) +def test_decrypt_config__no_password(plaintext_config): + encrypted_config = encrypt_config(plaintext_config, PASSWORD) + with pytest.raises(NoCredentialsError): + decrypt_config(encrypted_config, "")