Skip to content

Commit

Permalink
Upload button function is done
Browse files Browse the repository at this point in the history
  • Loading branch information
krdmnbrk committed Dec 11, 2024
1 parent 5a8938b commit d0533f1
Show file tree
Hide file tree
Showing 11 changed files with 254 additions and 153 deletions.
8 changes: 2 additions & 6 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { red, grey } from '@mui/material/colors';

const executor_names = [
"powershell",
"command prompt",
"command_prompt",
"bash",
"sh",
];
Expand Down Expand Up @@ -131,11 +131,6 @@ function App() {
const [darkMode, setDarkMode] = useState(true);
const [isPortrait, setIsPortrait] = useState(window.innerHeight > window.innerWidth);
const [changed, setChanged] = useState(false);
const [componentVariant, setComponentVariant] = useState('outlined');

useEffect(() => {
setComponentVariant(darkMode ? 'outlined' : 'contained');
}, [darkMode]);

// Prevent page reload
useEffect(() => {
Expand Down Expand Up @@ -234,6 +229,7 @@ function App() {
<Grid container spacing={2}>
<Grid size={isPortrait ? 12 : 6}>
<Inputs
base={base}
darkMode={darkMode}
changed={changed}
setChanged={setChanged}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Inputs.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const MenuProps = {
};

// Functional component for managing inputs
function Inputs({ darkMode, componentVariant, changed, setChanged, errors, setErrors, inputs, setInputs, executor_names, supported_platforms }) {
function Inputs({ base, darkMode, changed, setChanged, errors, setErrors, inputs, setInputs, executor_names, supported_platforms }) {

// Handle changes to text fields (e.g., Atomic Name, Atomic Description)
const handleChangeText = (e) => {
Expand Down Expand Up @@ -103,6 +103,7 @@ function Inputs({ darkMode, componentVariant, changed, setChanged, errors, setEr
<Box>
<Box sx={{ mb: 2 }}>
<InputButtons
base={base}
setInputs={setInputs}
setChanged={setChanged}
changed={changed}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Inputs/Dependency.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ function Dependency({ darkMode, inputs, setInputs, executor_names }) {
Prerequisite Command
</Typography>
<Editor
darkMode={darkMode}
mode={inputs.executor.name === "powershell" ? "powershell" : "sh"}
name="prerequisite-command-editor"
value={dependency.prereq_command || ''}
Expand All @@ -169,6 +170,7 @@ function Dependency({ darkMode, inputs, setInputs, executor_names }) {
Get Prerequisite Command
</Typography>
<Editor
darkMode={darkMode}
mode={inputs.executor.name === "powershell" ? "powershell" : "sh"}
name="prerequisite-command-editor"
value={dependency.get_prereq_command || ''}
Expand Down
103 changes: 60 additions & 43 deletions src/components/Inputs/InputButtons.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as React from 'react';
import React, { useState, useEffect } from 'react';
import yaml from 'js-yaml';
import Button from '@mui/material/Button';
import ButtonGroup from '@mui/material/ButtonGroup';
import ArrowDropDownIcon from '@mui/icons-material/ArrowDropDown';
Expand All @@ -10,16 +11,36 @@ import Chip from '@mui/material/Chip';
import Box from '@mui/material/Box';
import MenuItem from '@mui/material/MenuItem';
import MenuList from '@mui/material/MenuList';
import { basic, moderate, complex } from './SampleTests';
import Alert from '@mui/material/Alert';
import basic from './samples/hostname_discovery_(windows).yaml';
import moderate from './samples/scheduled_task_startup_script.yaml';
import complex from './samples/windows_push_file_using_scp.exe.yaml';
import { Typography } from '@mui/material';
import UploadButton from './UploadButton';
import transformInputArguments from './transformInputArguments';



export default function InputButtons({ darkMode, setInputs, setChanged, changed }) {
export default function InputButtons({ base, darkMode, setInputs, setChanged, changed }) {
const [open, setOpen] = React.useState(false);
const [errorMessage, setErrorMessage] = useState(null);
const [samples, setSamples] = useState([]);
const anchorRef = React.useRef(null);

useEffect(() => {
const fetchSamples = async () => {
const tests = []
for (let test of [basic, moderate, complex]) {
let response = await fetch(test);
let yamlText = await response.text();
let parsed = yaml.load(yamlText);
tests.push({ ...base, ...transformInputArguments(parsed[0]) });
}
setSamples(tests);
}
fetchSamples();
}, [base]);

const handleToggle = () => {
setOpen((prevOpen) => !prevOpen);
};
Expand All @@ -32,20 +53,23 @@ export default function InputButtons({ darkMode, setInputs, setChanged, changed
setOpen(false);
};




const loadSample = async (level) => {
if (changed) {
const confirm = window.confirm('Are you sure you want to load this sample? Your current inputs will be overwritten.');
if (!confirm) return;
}
await setInputs(level);
await setChanged(false);
await setInputs({ ...base, ...samples[level] });
setChanged(false);
handleToggle(null);
}

return (
<React.Fragment>
<ButtonGroup
variant={darkMode ? "outlined" : "contained"}
variant={darkMode ? "outlined" : "contained"}
ref={anchorRef}
aria-label="Load attack test sample"
>
Expand All @@ -59,8 +83,21 @@ export default function InputButtons({ darkMode, setInputs, setChanged, changed
>
Load Sample
</Button>
<UploadButton setInputs={setInputs} darkMode={darkMode}/>
<UploadButton
base={base}
setInputs={setInputs}
darkMode={darkMode}
setErrorMessage={setErrorMessage}
errorMessage={errorMessage}
setChanged={setChanged}
changed={changed}
/>
</ButtonGroup>
{errorMessage &&
<Alert sx={{ mt: 1 }} variant={darkMode ? "outlined" : "filled"} severity='error'>
{errorMessage}
</Alert>
}
<Popper
sx={{ zIndex: 1 }}
open={open}
Expand All @@ -77,42 +114,22 @@ export default function InputButtons({ darkMode, setInputs, setChanged, changed
<Paper elevation={6}>
<ClickAwayListener onClickAway={handleClose}>
<MenuList id="split-button-menu" autoFocusItem>
<MenuItem onClick={() => loadSample(basic)}>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
<Chip
sx={{ width: 80 }}
label="Basic"
variant={darkMode ? "outlined" : "filled"}
color='warning'
size="small"
/>
<Typography fontSize={15}>{basic.name}</Typography>
</Box>
</MenuItem>
<MenuItem onClick={() => loadSample(moderate)}>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
<Chip
sx={{ width: 80 }}
label="Moderate"
variant={darkMode ? "outlined" : "filled"}
color="warning"
size="small"
/>
<Typography fontSize={15}>{moderate.name}</Typography>
</Box>
</MenuItem>
<MenuItem onClick={() => loadSample(complex)}>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
<Chip
sx={{ width: 80 }}
label="Complex"
variant={darkMode ? "outlined" : "filled"}
color="warning"
size="small"
/>
<Typography fontSize={15}>{complex.name}</Typography>
</Box>
</MenuItem>
{
samples.map((sample, index) => (
<MenuItem key={index} onClick={() => loadSample(index)}>
<Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}>
<Chip
sx={{ width: 80 }}
label={index === 0 ? "Basic" : index === 1 ? "Moderate" : "Complex"}
variant={darkMode ? "outlined" : "filled"}
color='warning'
size="small"
/>
<Typography fontSize={15}>{sample.name}</Typography>
</Box>
</MenuItem>
))
}
</MenuList>

</ClickAwayListener>
Expand Down
89 changes: 0 additions & 89 deletions src/components/Inputs/SampleTests.js

This file was deleted.

Loading

0 comments on commit d0533f1

Please sign in to comment.