Fixed, improved and tested configuration import and export.

This commit is contained in:
VakarisZ 2021-06-02 15:15:21 +03:00
parent 8b86e40259
commit 500f270aa9
4 changed files with 16 additions and 16 deletions

View File

@ -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(

View File

@ -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()

View File

@ -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]);

View File

@ -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, "")