forked from p15670423/monkey
UI: Change configuration import to validate and decrypt on UI
This commit is contained in:
parent
5a531bcb04
commit
37152c2589
|
@ -9,10 +9,12 @@ import UnsafeConfigOptionsConfirmationModal
|
||||||
from './UnsafeConfigOptionsConfirmationModal.js';
|
from './UnsafeConfigOptionsConfirmationModal.js';
|
||||||
import UploadStatusIcon, {UploadStatuses} from '../ui-components/UploadStatusIcon';
|
import UploadStatusIcon, {UploadStatuses} from '../ui-components/UploadStatusIcon';
|
||||||
import isUnsafeOptionSelected from '../utils/SafeOptionValidator.js';
|
import isUnsafeOptionSelected from '../utils/SafeOptionValidator.js';
|
||||||
|
import {decryptText} from '../utils/PasswordBasedEncryptor';
|
||||||
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
show: boolean,
|
show: boolean,
|
||||||
|
schema: object,
|
||||||
onClose: (importSuccessful: boolean) => void
|
onClose: (importSuccessful: boolean) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,9 +25,9 @@ const ConfigImportModal = (props: Props) => {
|
||||||
|
|
||||||
const [uploadStatus, setUploadStatus] = useState(UploadStatuses.clean);
|
const [uploadStatus, setUploadStatus] = useState(UploadStatuses.clean);
|
||||||
const [configContents, setConfigContents] = useState(null);
|
const [configContents, setConfigContents] = useState(null);
|
||||||
const [candidateConfig, setCandidateConfig] = useState(null);
|
|
||||||
const [password, setPassword] = useState('');
|
const [password, setPassword] = useState('');
|
||||||
const [showPassword, setShowPassword] = useState(false);
|
const [showPassword, setShowPassword] = useState(false);
|
||||||
|
const [configEncrypted, setConfigEncrypted] = useState(false);
|
||||||
const [errorMessage, setErrorMessage] = useState('');
|
const [errorMessage, setErrorMessage] = useState('');
|
||||||
const [unsafeOptionsVerified, setUnsafeOptionsVerified] = useState(false);
|
const [unsafeOptionsVerified, setUnsafeOptionsVerified] = useState(false);
|
||||||
const [showUnsafeOptionsConfirmation,
|
const [showUnsafeOptionsConfirmation,
|
||||||
|
@ -36,10 +38,33 @@ const ConfigImportModal = (props: Props) => {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (configContents !== null) {
|
if (configContents !== null) {
|
||||||
sendConfigToServer();
|
saveConfig();
|
||||||
}
|
}
|
||||||
}, [configContents, unsafeOptionsVerified])
|
}, [configContents, unsafeOptionsVerified])
|
||||||
|
|
||||||
|
function saveConfig() {
|
||||||
|
if (configEncrypted && !showPassword){
|
||||||
|
setShowPassword(true);
|
||||||
|
} else if (configEncrypted && showPassword) {
|
||||||
|
try {
|
||||||
|
let decryptedConfig = JSON.parse(decryptText(configContents, password));
|
||||||
|
setConfigEncrypted(false);
|
||||||
|
setConfigContents(decryptedConfig);
|
||||||
|
} catch (e) {
|
||||||
|
setUploadStatus(UploadStatuses.error);
|
||||||
|
setErrorMessage('Decryption failed: Password is wrong or the file is corrupted');
|
||||||
|
}
|
||||||
|
} else if(!unsafeOptionsVerified) {
|
||||||
|
if(isUnsafeOptionSelected(props.schema, configContents)){
|
||||||
|
setShowUnsafeOptionsConfirmation(true);
|
||||||
|
} else {
|
||||||
|
setUnsafeOptionsVerified(true);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sendConfigToServer();
|
||||||
|
setUploadStatus(UploadStatuses.success);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function sendConfigToServer() {
|
function sendConfigToServer() {
|
||||||
authComponent.authFetch(configImportEndpoint,
|
authComponent.authFetch(configImportEndpoint,
|
||||||
|
@ -48,34 +73,18 @@ const ConfigImportModal = (props: Props) => {
|
||||||
headers: {'Content-Type': 'application/json'},
|
headers: {'Content-Type': 'application/json'},
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
config: configContents,
|
config: configContents,
|
||||||
password: password,
|
|
||||||
unsafeOptionsVerified: unsafeOptionsVerified
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
).then(res => res.json())
|
).then(res => res.json())
|
||||||
.then(res => {
|
.then(res => {
|
||||||
if (res['import_status'] === 'invalid_credentials') {
|
if (res['import_status'] === 'invalid_configuration') {
|
||||||
setUploadStatus(UploadStatuses.success);
|
if(showPassword){
|
||||||
if (showPassword){
|
|
||||||
setErrorMessage(res['message']);
|
|
||||||
} else {
|
|
||||||
setShowPassword(true);
|
setShowPassword(true);
|
||||||
setErrorMessage('');
|
|
||||||
}
|
|
||||||
} else if (res['import_status'] === 'invalid_configuration') {
|
|
||||||
setUploadStatus(UploadStatuses.error);
|
|
||||||
setErrorMessage(res['message']);
|
|
||||||
} else if (res['import_status'] === 'unsafe_options_verification_required') {
|
|
||||||
setUploadStatus(UploadStatuses.success);
|
|
||||||
setErrorMessage('');
|
|
||||||
|
|
||||||
if (isUnsafeOptionSelected(res['config_schema'], JSON.parse(res['config']))) {
|
|
||||||
setShowUnsafeOptionsConfirmation(true);
|
|
||||||
setCandidateConfig(res['config']);
|
|
||||||
} else {
|
} else {
|
||||||
setUnsafeOptionsVerified(true);
|
setUploadStatus(UploadStatuses.error);
|
||||||
|
setErrorMessage(res['message']);
|
||||||
}
|
}
|
||||||
} else if (res['import_status'] === 'imported'){
|
} else if (res['import_status'] === 'imported') {
|
||||||
resetState();
|
resetState();
|
||||||
props.onClose(true);
|
props.onClose(true);
|
||||||
}
|
}
|
||||||
|
@ -83,7 +92,8 @@ const ConfigImportModal = (props: Props) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
function isImportDisabled(): boolean {
|
function isImportDisabled(): boolean {
|
||||||
return uploadStatus !== UploadStatuses.success || (showPassword && password === '')
|
// Don't allow import if password input is empty or there's an error
|
||||||
|
return (showPassword && password === '') || (errorMessage !== '');
|
||||||
}
|
}
|
||||||
|
|
||||||
function resetState() {
|
function resetState() {
|
||||||
|
@ -95,13 +105,22 @@ const ConfigImportModal = (props: Props) => {
|
||||||
setShowUnsafeOptionsConfirmation(false);
|
setShowUnsafeOptionsConfirmation(false);
|
||||||
setUnsafeOptionsVerified(false);
|
setUnsafeOptionsVerified(false);
|
||||||
setFileFieldKey(Date.now()); // Resets the file input
|
setFileFieldKey(Date.now()); // Resets the file input
|
||||||
|
setConfigEncrypted(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
function uploadFile(event) {
|
function uploadFile(event) {
|
||||||
setShowPassword(false);
|
setShowPassword(false);
|
||||||
let reader = new FileReader();
|
let reader = new FileReader();
|
||||||
reader.onload = (event) => {
|
reader.onload = (event) => {
|
||||||
setConfigContents(event.target.result);
|
let importContents = null;
|
||||||
|
try {
|
||||||
|
importContents = JSON.parse(event.target.result);
|
||||||
|
} catch (e){
|
||||||
|
setErrorMessage("File is not in a valid json format");
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setConfigEncrypted(importContents['metadata']['encrypted']);
|
||||||
|
setConfigContents(importContents['contents']);
|
||||||
};
|
};
|
||||||
reader.readAsText(event.target.files[0]);
|
reader.readAsText(event.target.files[0]);
|
||||||
}
|
}
|
||||||
|
@ -115,7 +134,6 @@ const ConfigImportModal = (props: Props) => {
|
||||||
}}
|
}}
|
||||||
onContinueClick={() => {
|
onContinueClick={() => {
|
||||||
setUnsafeOptionsVerified(true);
|
setUnsafeOptionsVerified(true);
|
||||||
setConfigContents(candidateConfig);
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
@ -142,28 +160,29 @@ const ConfigImportModal = (props: Props) => {
|
||||||
<div className={`mb-3 config-import-option`}>
|
<div className={`mb-3 config-import-option`}>
|
||||||
{showVerificationDialog()}
|
{showVerificationDialog()}
|
||||||
<Form>
|
<Form>
|
||||||
<Form.File id='importConfigFileSelector'
|
<Form.File id="importConfigFileSelector"
|
||||||
label='Please choose a configuration file'
|
label="Please choose a configuration file"
|
||||||
accept='.conf'
|
accept=".conf"
|
||||||
onChange={uploadFile}
|
onChange={uploadFile}
|
||||||
className={'file-input'}
|
className={'file-input'}
|
||||||
key={fileFieldKey}/>
|
key={fileFieldKey}/>
|
||||||
<UploadStatusIcon status={uploadStatus}/>
|
<UploadStatusIcon status={uploadStatus}/>
|
||||||
|
|
||||||
{showPassword && <PasswordInput onChange={setPassword}/>}
|
{showPassword && <PasswordInput onChange={(password) => {setPassword(password);
|
||||||
|
setErrorMessage('')}}/>}
|
||||||
|
|
||||||
{errorMessage &&
|
{errorMessage &&
|
||||||
<Alert variant={'danger'} className={'import-error'}>
|
<Alert variant={'danger'} className={'import-error'}>
|
||||||
<FontAwesomeIcon icon={faExclamationCircle} style={{'marginRight': '5px'}}/>
|
<FontAwesomeIcon icon={faExclamationCircle} style={{'marginRight': '5px'}}/>
|
||||||
{errorMessage}
|
{errorMessage}
|
||||||
</Alert>
|
</Alert>
|
||||||
}
|
}
|
||||||
</Form>
|
</Form>
|
||||||
</div>
|
</div>
|
||||||
</Modal.Body>
|
</Modal.Body>
|
||||||
<Modal.Footer>
|
<Modal.Footer>
|
||||||
<Button variant={'info'}
|
<Button variant={'info'}
|
||||||
onClick={sendConfigToServer}
|
onClick={saveConfig}
|
||||||
disabled={isImportDisabled()}>
|
disabled={isImportDisabled()}>
|
||||||
Import
|
Import
|
||||||
</Button>
|
</Button>
|
||||||
|
@ -177,8 +196,8 @@ const PasswordInput = (props: {
|
||||||
return (
|
return (
|
||||||
<div className={'config-import-password-input'}>
|
<div className={'config-import-password-input'}>
|
||||||
<p>File is protected. Please enter the password:</p>
|
<p>File is protected. Please enter the password:</p>
|
||||||
<Form.Control type='password'
|
<Form.Control type="password"
|
||||||
placeholder='Password'
|
placeholder="Password"
|
||||||
onChange={evt => (props.onChange(evt.target.value))}/>
|
onChange={evt => (props.onChange(evt.target.value))}/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
@ -180,6 +180,7 @@ class ConfigurePageComponent extends AuthComponent {
|
||||||
|
|
||||||
renderConfigImportModal = () => {
|
renderConfigImportModal = () => {
|
||||||
return (<ConfigImportModal show={this.state.showConfigImportModal}
|
return (<ConfigImportModal show={this.state.showConfigImportModal}
|
||||||
|
schema={this.state.schema}
|
||||||
onClose={this.onClose}/>);
|
onClose={this.onClose}/>);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue