Small pr refactoring

This commit is contained in:
VakarisZ 2019-04-20 11:45:20 +03:00
parent b17dbdc9de
commit 7c3ba14100
3 changed files with 17 additions and 19 deletions

View File

@ -3,7 +3,7 @@ import json
from flask import jsonify, request from flask import jsonify, request
from monkey_island.cc.auth import jwt_required from monkey_island.cc.auth import jwt_required
from monkey_island.cc.services.attack.attack_config import * import monkey_island.cc.services.attack.attack_config as attack_config
__author__ = "VakarisZ" __author__ = "VakarisZ"
@ -11,7 +11,7 @@ __author__ = "VakarisZ"
class AttackConfiguration(flask_restful.Resource): class AttackConfiguration(flask_restful.Resource):
@jwt_required() @jwt_required()
def get(self): def get(self):
return jsonify(configuration=get_config()['properties']) return jsonify(configuration=attack_config.get_config()['properties'])
@jwt_required() @jwt_required()
def post(self): def post(self):
@ -21,10 +21,10 @@ class AttackConfiguration(flask_restful.Resource):
""" """
config_json = json.loads(request.data) config_json = json.loads(request.data)
if 'reset_attack_matrix' in config_json: if 'reset_attack_matrix' in config_json:
reset_config() attack_config.reset_config()
return jsonify(configuration=get_config()['properties']) return jsonify(configuration=attack_config.get_config()['properties'])
else: else:
update_config({'properties': json.loads(request.data)}) attack_config.update_config({'properties': json.loads(request.data)})
apply_to_monkey_config() attack_config.apply_to_monkey_config()
return {} return {}

View File

@ -46,13 +46,7 @@ let parseTechniques = function (data, maxLen) {
class MatrixComponent extends AuthComponent { class MatrixComponent extends AuthComponent {
constructor(props) { constructor(props) {
super(props); super(props);
// Copy ATT&CK configuration and parse it for ATT&CK matrix table this.state = this.getStateFromConfig(this.props.configuration, 'none');
let configCopy = JSON.parse(JSON.stringify(this.props.configuration));
this.state = {lastAction: 'none',
configData: this.props.configuration,
maxTechniques: findMaxTechniques(Object.values(configCopy))};
this.state.matrixTableData = parseTechniques(Object.values(configCopy), this.state.maxTechniques);
this.state.columns = this.getColumns(this.state.matrixTableData)
}; };
getColumns(matrixData) { getColumns(matrixData) {
@ -68,7 +62,7 @@ class MatrixComponent extends AuthComponent {
renderTechnique(technique) { renderTechnique(technique) {
if (technique == null){ if (technique == null){
return (<div></div>) return (<div />)
} else { } else {
return (<Tooltip content={technique.description} direction="down"> return (<Tooltip content={technique.description} direction="down">
<Checkbox checked={technique.value} <Checkbox checked={technique.value}
@ -119,17 +113,21 @@ class MatrixComponent extends AuthComponent {
// Updates state based on values in config supplied. // Updates state based on values in config supplied.
updateStateFromConfig = (config, lastAction = '') => { updateStateFromConfig = (config, lastAction = '') => {
this.setState(this.getStateFromConfig(config, lastAction));
};
getStateFromConfig = (config, lastAction) => {
let configCopy = JSON.parse(JSON.stringify(config)); let configCopy = JSON.parse(JSON.stringify(config));
let maxTechniques = findMaxTechniques(Object.values(configCopy)); let maxTechniques = findMaxTechniques(Object.values(configCopy));
let matrixTableData = parseTechniques(Object.values(configCopy), maxTechniques); let matrixTableData = parseTechniques(Object.values(configCopy), maxTechniques);
let columns = this.getColumns(matrixTableData); let columns = this.getColumns(matrixTableData);
this.setState({ return {
lastAction: lastAction, lastAction: lastAction,
configData: config, configData: config,
maxTechniques: maxTechniques, maxTechniques: maxTechniques,
matrixTableData: matrixTableData, matrixTableData: matrixTableData,
columns: columns columns: columns
}); };
}; };
// Handles change in technique, when user toggles it // Handles change in technique, when user toggles it

View File

@ -1,7 +1,7 @@
import '../../styles/Checkbox.scss' import '../../styles/Checkbox.scss'
import React from 'react'; import React from 'react';
class Checkbox extends React.PureComponent { class CheckboxComponent extends React.PureComponent {
componentDidUpdate(prevProps) { componentDidUpdate(prevProps) {
if (this.props.checked !== prevProps.checked) { if (this.props.checked !== prevProps.checked) {
@ -63,4 +63,4 @@ class Checkbox extends React.PureComponent {
} }
} }
export default Checkbox; export default CheckboxComponent;