Improved configuration export related code by making it cleaner/more trivial

This commit is contained in:
VakarisZ 2021-06-01 15:51:44 +03:00
parent 295cacaffc
commit 321dd2c55e
4 changed files with 19 additions and 27 deletions

View File

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

View File

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

View File

@ -12,7 +12,6 @@ type Props = {
}
const ConfigExportModal = (props: Props) => {
// TODO implement the back end
const configExportEndpoint = '/api/configuration/export';
const [pass, setPass] = useState('');
@ -36,17 +35,16 @@ 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');
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');
})
}

View File

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