Finished PBA refactoring

This commit is contained in:
VakarisZ 2020-06-30 18:10:48 +03:00
parent aad9e5069e
commit fda600eb60
3 changed files with 29 additions and 47 deletions

View File

@ -9,7 +9,6 @@ class PbaInput extends AuthComponent {
constructor(props) { constructor(props) {
super(props); super(props);
console.log("Constructor called");
// set schema from server // set schema from server
this.state = this.getStateFromProps(this.props); this.state = this.getStateFromProps(this.props);
} }
@ -18,35 +17,24 @@ class PbaInput extends AuthComponent {
let options = props.options let options = props.options
// set schema from server // set schema from server
return { return {
PBAFile: options.PbaFile,
filename: options.filename, filename: options.filename,
apiEndpoint: options.apiEndpoint, apiEndpoint: options.apiEndpoint,
setPbaFile: options.setPbaFile setPbaFilename: options.setPbaFilename
}; };
} }
componentDidUpdate(prevProps, prevState, snapshot) {
if(prevProps.options.filename !== this.props.options.filename && this.props.options.filename === ""){
this.setState({filename: this.props.options.filename})
}
}
getPBAfile() { getPBAfile() {
if (this.state.PBAFile.length !== 0) { if (this.state.filename) {
return this.state.PBAFile
return PbaInput.getMockPBAfile(this.state.PBAFile)
} else if (this.state.filename) {
return PbaInput.getFullPBAfile(this.state.filename) return PbaInput.getFullPBAfile(this.state.filename)
} }
} }
static getMockPBAfile(mockFile) {
let pbaFile = [{
name: mockFile.name,
source: mockFile.name,
options: {
type: 'limbo'
}
}];
pbaFile[0].options.file = mockFile;
return pbaFile
}
static getFullPBAfile(filename) { static getFullPBAfile(filename) {
return [{ return [{
source: filename, source: filename,
@ -78,9 +66,9 @@ class PbaInput extends AuthComponent {
files={this.getPBAfile()} files={this.getPBAfile()}
onupdatefiles={fileItems => { onupdatefiles={fileItems => {
if (fileItems.length > 0) { if (fileItems.length > 0) {
this.state.setPbaFile([fileItems[0].file], fileItems[0].file.name) this.state.setPbaFilename(fileItems[0].file.name)
} else { } else {
this.state.setPbaFile([], "") this.state.setPbaFilename("")
} }
}} }}
/>) />)

View File

@ -22,10 +22,9 @@ export default function UiSchema(props) {
PBA_linux_file: { PBA_linux_file: {
'ui:widget': PbaInput, 'ui:widget': PbaInput,
'ui:options': { 'ui:options': {
PbaFile: props.configuration.PBAlinuxFile, filename: props.PBA_linux_filename,
filename: props.configuration.configuration.monkey.behaviour.PBA_linux_filename,
apiEndpoint: API_PBA_LINUX, apiEndpoint: API_PBA_LINUX,
setPbaFile: props.setPbaFileLinux setPbaFilename: props.setPbaFilenameLinux
} }
}, },
custom_PBA_windows_cmd: { custom_PBA_windows_cmd: {
@ -35,10 +34,9 @@ export default function UiSchema(props) {
PBA_windows_file: { PBA_windows_file: {
'ui:widget': PbaInput, 'ui:widget': PbaInput,
'ui:options': { 'ui:options': {
PbaFile: props.configuration.PBAwindowsFile, filename: props.PBA_windows_filename,
filename: props.configuration.configuration.monkey.behaviour.PBA_windows_filename,
apiEndpoint: API_PBA_WINDOWS, apiEndpoint: API_PBA_WINDOWS,
setPbaFile: props.setPbaFileWindows setPbaFilename: props.setPbaFilenameWindows
} }
}, },
PBA_linux_filename: { PBA_linux_filename: {
@ -66,5 +64,5 @@ export default function UiSchema(props) {
} }
} }
} }
return UiSchema[props.configuration.selectedSection] return UiSchema[props.selectedSection]
} }

View File

@ -33,9 +33,6 @@ class ConfigurePageComponent extends AuthComponent {
sections: [], sections: [],
selectedSection: 'attack', selectedSection: 'attack',
showAttackAlert: false, showAttackAlert: false,
PBAwindowsFile: [],
PBAlinuxFile: []
}; };
} }
@ -245,7 +242,10 @@ class ConfigurePageComponent extends AuthComponent {
this.setInitialConfig(res.configuration); this.setInitialConfig(res.configuration);
this.props.onStatusChange(); this.props.onStatusChange();
} }
); ).then(() => {
this.removePBAfile(API_PBA_WINDOWS, this.setPbaFilenameWindows)
this.removePBAfile(API_PBA_LINUX, this.setPbaFilenameLinux)
});
this.authFetch(ATTACK_URL, { this.authFetch(ATTACK_URL, {
method: 'POST', method: 'POST',
headers: {'Content-Type': 'application/json'}, headers: {'Content-Type': 'application/json'},
@ -257,13 +257,12 @@ class ConfigurePageComponent extends AuthComponent {
this.setInitialAttackConfig(res.configuration); 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) this.sendPbaRemoveRequest(apiEndpoint)
setParamsFnc([], "") setFilenameFnc("")
} }
sendPbaRemoveRequest(apiEndpoint) { sendPbaRemoveRequest(apiEndpoint) {
@ -335,9 +334,11 @@ class ConfigurePageComponent extends AuthComponent {
{this.renderBasicNetworkWarning()} {this.renderBasicNetworkWarning()}
<Form schema={displayedSchema} <Form schema={displayedSchema}
uiSchema={UiSchema({ uiSchema={UiSchema({
configuration: this.state, PBA_linux_filename: this.state.configuration.monkey.behaviour.PBA_linux_filename,
setPbaFileWindows: this.setPbaFileWindows, PBA_windows_filename: this.state.configuration.monkey.behaviour.PBA_windows_filename,
setPbaFileLinux: this.setPbaFileLinux, setPbaFilenameWindows: this.setPbaFilenameWindows,
setPbaFilenameLinux: this.setPbaFilenameLinux,
selectedSection: this.state.selectedSection
})} })}
formData={this.state.configuration[this.state.selectedSection]} formData={this.state.configuration[this.state.selectedSection]}
onChange={this.onChange} onChange={this.onChange}
@ -348,23 +349,18 @@ class ConfigurePageComponent extends AuthComponent {
</div>) </div>)
}; };
setPbaFileWindows = (pbaFile, filename) => { setPbaFilenameWindows = (filename) => {
let pbaFileDeepCopy = JSON.parse(JSON.stringify(pbaFile))
let config = this.state.configuration let config = this.state.configuration
config.monkey.behaviour.PBA_windows_filename = filename config.monkey.behaviour.PBA_windows_filename = filename
this.setState({ this.setState({
PBAwindowsFile: pbaFileDeepCopy,
configuration: config configuration: config
}) })
} }
setPbaFileLinux = (pbaFile, filename) => { setPbaFilenameLinux = (filename) => {
let pbaFileDeepCopy = JSON.parse(JSON.stringify(pbaFile))
let config = this.state.configuration let config = this.state.configuration
config.monkey.behaviour.PBA_linux_filename = filename config.monkey.behaviour.PBA_linux_filename = filename
console.log(config);
this.setState({ this.setState({
PBAlinuxFile: pbaFileDeepCopy,
configuration: config configuration: config
}) })
} }