forked from p15670423/monkey
Improved configuration export related code by making it cleaner/more trivial
This commit is contained in:
parent
295cacaffc
commit
321dd2c55e
|
@ -9,10 +9,6 @@ from monkey_island.cc.services.utils.config_encryption import encrypt_config
|
||||||
|
|
||||||
|
|
||||||
class ConfigurationExport(flask_restful.Resource):
|
class ConfigurationExport(flask_restful.Resource):
|
||||||
@jwt_required
|
|
||||||
def get(self):
|
|
||||||
return {"to_export": self.config_to_return, "is_plaintext": self.is_plaintext}
|
|
||||||
|
|
||||||
@jwt_required
|
@jwt_required
|
||||||
def post(self):
|
def post(self):
|
||||||
data = json.loads(request.data)
|
data = json.loads(request.data)
|
||||||
|
@ -20,11 +16,9 @@ class ConfigurationExport(flask_restful.Resource):
|
||||||
|
|
||||||
plaintext_config = ConfigService.get_config()
|
plaintext_config = ConfigService.get_config()
|
||||||
|
|
||||||
self.config_to_return = plaintext_config
|
config_export = plaintext_config
|
||||||
self.is_plaintext = True
|
|
||||||
if should_encrypt:
|
if should_encrypt:
|
||||||
password = data["password"]
|
password = data["password"]
|
||||||
self.config_to_return = encrypt_config(plaintext_config, password)
|
config_export = encrypt_config(plaintext_config, password)
|
||||||
self.is_plaintext = False
|
|
||||||
|
|
||||||
return self.get()
|
return {"config_export": config_export, "encrypted": should_encrypt}
|
||||||
|
|
|
@ -20,7 +20,7 @@ def encrypt_config(config: Dict, password: str) -> str:
|
||||||
|
|
||||||
ciphertext_b64 = base64.b64encode(ciphertext_config_stream.getvalue())
|
ciphertext_b64 = base64.b64encode(ciphertext_config_stream.getvalue())
|
||||||
|
|
||||||
return str(ciphertext_b64)
|
return ciphertext_b64.decode()
|
||||||
|
|
||||||
|
|
||||||
def decrypt_config(cyphertext: str, password: str) -> Dict:
|
def decrypt_config(cyphertext: str, password: str) -> Dict:
|
||||||
|
|
|
@ -12,7 +12,6 @@ type Props = {
|
||||||
}
|
}
|
||||||
|
|
||||||
const ConfigExportModal = (props: Props) => {
|
const ConfigExportModal = (props: Props) => {
|
||||||
// TODO implement the back end
|
|
||||||
const configExportEndpoint = '/api/configuration/export';
|
const configExportEndpoint = '/api/configuration/export';
|
||||||
|
|
||||||
const [pass, setPass] = useState('');
|
const [pass, setPass] = useState('');
|
||||||
|
@ -34,20 +33,19 @@ const ConfigExportModal = (props: Props) => {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.then(res => res.json())
|
.then(res => res.json())
|
||||||
.then(res => {
|
.then(res => {
|
||||||
let configToExport = res['to_export'];
|
let configToExport = res['config_export'];
|
||||||
if (res['is_plaintext'] === true) {
|
if (res['encrypted']) {
|
||||||
const configAsBinary = new Blob([JSON.stringify(configToExport, null, 2)],
|
configToExport = new Blob([configToExport]);
|
||||||
{type: 'text/plain;charset=utf-8'});
|
} else {
|
||||||
FileSaver.saveAs(configAsBinary, 'monkey.conf');
|
configToExport = new Blob(
|
||||||
}
|
[JSON.stringify(configToExport, null, 2)],
|
||||||
else {
|
{type: 'text/plain;charset=utf-8'}
|
||||||
const configAsBinary = new Blob([configToExport.slice(2, -1)]);
|
);
|
||||||
FileSaver.saveAs(configAsBinary, 'monkey.conf');
|
}
|
||||||
|
FileSaver.saveAs(configToExport, 'monkey.conf');
|
||||||
}
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -21,6 +21,6 @@ def plaintext_config(data_for_tests_dir):
|
||||||
|
|
||||||
|
|
||||||
def test_encrypt_decrypt_config(plaintext_config):
|
def test_encrypt_decrypt_config(plaintext_config):
|
||||||
encrypted_config = encrypt_config(plaintext_config, PASSWORD) # str of the form: `b'a1b2c3'`
|
encrypted_config = encrypt_config(plaintext_config, PASSWORD)
|
||||||
encrypted_config = encrypted_config[2:-1] # so we slice it here
|
encrypted_config = encrypted_config
|
||||||
assert decrypt_config(encrypted_config, PASSWORD) == plaintext_config
|
assert decrypt_config(encrypted_config, PASSWORD) == plaintext_config
|
||||||
|
|
Loading…
Reference in New Issue