From fda600eb602cb3ac5dd93baca4aa9b7f33e3a38a Mon Sep 17 00:00:00 2001 From: VakarisZ Date: Tue, 30 Jun 2020 18:10:48 +0300 Subject: [PATCH] Finished PBA refactoring --- .../configuration-components/PbaInput.js | 32 ++++++------------- .../configuration-components/UiSchema.js | 12 +++---- .../ui/src/components/pages/ConfigurePage.js | 32 ++++++++----------- 3 files changed, 29 insertions(+), 47 deletions(-) diff --git a/monkey/monkey_island/cc/ui/src/components/configuration-components/PbaInput.js b/monkey/monkey_island/cc/ui/src/components/configuration-components/PbaInput.js index 1d1e43be4..f341a085e 100644 --- a/monkey/monkey_island/cc/ui/src/components/configuration-components/PbaInput.js +++ b/monkey/monkey_island/cc/ui/src/components/configuration-components/PbaInput.js @@ -9,7 +9,6 @@ class PbaInput extends AuthComponent { constructor(props) { super(props); - console.log("Constructor called"); // set schema from server this.state = this.getStateFromProps(this.props); } @@ -18,35 +17,24 @@ class PbaInput extends AuthComponent { let options = props.options // set schema from server return { - PBAFile: options.PbaFile, filename: options.filename, apiEndpoint: options.apiEndpoint, - setPbaFile: options.setPbaFile + setPbaFilename: options.setPbaFilename }; } - getPBAfile() { - if (this.state.PBAFile.length !== 0) { - return this.state.PBAFile - return PbaInput.getMockPBAfile(this.state.PBAFile) - } else if (this.state.filename) { - return PbaInput.getFullPBAfile(this.state.filename) + componentDidUpdate(prevProps, prevState, snapshot) { + if(prevProps.options.filename !== this.props.options.filename && this.props.options.filename === ""){ + this.setState({filename: this.props.options.filename}) } } - static getMockPBAfile(mockFile) { - let pbaFile = [{ - name: mockFile.name, - source: mockFile.name, - options: { - type: 'limbo' - } - }]; - pbaFile[0].options.file = mockFile; - return pbaFile + getPBAfile() { + if (this.state.filename) { + return PbaInput.getFullPBAfile(this.state.filename) + } } - static getFullPBAfile(filename) { return [{ source: filename, @@ -78,9 +66,9 @@ class PbaInput extends AuthComponent { files={this.getPBAfile()} onupdatefiles={fileItems => { if (fileItems.length > 0) { - this.state.setPbaFile([fileItems[0].file], fileItems[0].file.name) + this.state.setPbaFilename(fileItems[0].file.name) } else { - this.state.setPbaFile([], "") + this.state.setPbaFilename("") } }} />) diff --git a/monkey/monkey_island/cc/ui/src/components/configuration-components/UiSchema.js b/monkey/monkey_island/cc/ui/src/components/configuration-components/UiSchema.js index ba1a6b19e..a9c592d7c 100644 --- a/monkey/monkey_island/cc/ui/src/components/configuration-components/UiSchema.js +++ b/monkey/monkey_island/cc/ui/src/components/configuration-components/UiSchema.js @@ -22,10 +22,9 @@ export default function UiSchema(props) { PBA_linux_file: { 'ui:widget': PbaInput, 'ui:options': { - PbaFile: props.configuration.PBAlinuxFile, - filename: props.configuration.configuration.monkey.behaviour.PBA_linux_filename, + filename: props.PBA_linux_filename, apiEndpoint: API_PBA_LINUX, - setPbaFile: props.setPbaFileLinux + setPbaFilename: props.setPbaFilenameLinux } }, custom_PBA_windows_cmd: { @@ -35,10 +34,9 @@ export default function UiSchema(props) { PBA_windows_file: { 'ui:widget': PbaInput, 'ui:options': { - PbaFile: props.configuration.PBAwindowsFile, - filename: props.configuration.configuration.monkey.behaviour.PBA_windows_filename, + filename: props.PBA_windows_filename, apiEndpoint: API_PBA_WINDOWS, - setPbaFile: props.setPbaFileWindows + setPbaFilename: props.setPbaFilenameWindows } }, PBA_linux_filename: { @@ -66,5 +64,5 @@ export default function UiSchema(props) { } } } - return UiSchema[props.configuration.selectedSection] + return UiSchema[props.selectedSection] } diff --git a/monkey/monkey_island/cc/ui/src/components/pages/ConfigurePage.js b/monkey/monkey_island/cc/ui/src/components/pages/ConfigurePage.js index ca07d3604..e75978b7f 100644 --- a/monkey/monkey_island/cc/ui/src/components/pages/ConfigurePage.js +++ b/monkey/monkey_island/cc/ui/src/components/pages/ConfigurePage.js @@ -33,9 +33,6 @@ class ConfigurePageComponent extends AuthComponent { sections: [], selectedSection: 'attack', showAttackAlert: false, - - PBAwindowsFile: [], - PBAlinuxFile: [] }; } @@ -245,7 +242,10 @@ class ConfigurePageComponent extends AuthComponent { this.setInitialConfig(res.configuration); this.props.onStatusChange(); } - ); + ).then(() => { + this.removePBAfile(API_PBA_WINDOWS, this.setPbaFilenameWindows) + this.removePBAfile(API_PBA_LINUX, this.setPbaFilenameLinux) + }); this.authFetch(ATTACK_URL, { method: 'POST', headers: {'Content-Type': 'application/json'}, @@ -257,13 +257,12 @@ class ConfigurePageComponent extends AuthComponent { this.setInitialAttackConfig(res.configuration); }) - this.removePBAfile(API_PBA_WINDOWS, this.setPbaFileWindows) - this.removePBAfile(API_PBA_LINUX, this.setPbaFileLinux) + }; - removePBAfile(apiEndpoint, setParamsFnc) { + removePBAfile(apiEndpoint, setFilenameFnc) { this.sendPbaRemoveRequest(apiEndpoint) - setParamsFnc([], "") + setFilenameFnc("") } sendPbaRemoveRequest(apiEndpoint) { @@ -335,9 +334,11 @@ class ConfigurePageComponent extends AuthComponent { {this.renderBasicNetworkWarning()}
) }; - setPbaFileWindows = (pbaFile, filename) => { - let pbaFileDeepCopy = JSON.parse(JSON.stringify(pbaFile)) + setPbaFilenameWindows = (filename) => { let config = this.state.configuration config.monkey.behaviour.PBA_windows_filename = filename this.setState({ - PBAwindowsFile: pbaFileDeepCopy, configuration: config }) } - setPbaFileLinux = (pbaFile, filename) => { - let pbaFileDeepCopy = JSON.parse(JSON.stringify(pbaFile)) + setPbaFilenameLinux = (filename) => { let config = this.state.configuration config.monkey.behaviour.PBA_linux_filename = filename - console.log(config); this.setState({ - PBAlinuxFile: pbaFileDeepCopy, configuration: config }) }