UI: Fix Import Config to use the new configuration

This commit is contained in:
Ilija Lazoroski 2022-07-21 13:46:25 +02:00
parent ba05058ec2
commit 7769ec4aa3
1 changed files with 39 additions and 5 deletions

View File

@ -10,7 +10,10 @@ import UnsafeConfigOptionsConfirmationModal
import UploadStatusIcon, {UploadStatuses} from '../ui-components/UploadStatusIcon';
import isUnsafeOptionSelected from '../utils/SafeOptionValidator.js';
import {decryptText} from '../utils/PasswordBasedEncryptor';
import {
reformatConfig,
formatCredentialsForIsland
} from '../configuration-components/ReformatHook';
type Props = {
show: boolean,
@ -21,9 +24,11 @@ type Props = {
const ConfigImportModal = (props: Props) => {
const configImportEndpoint = '/api/agent-configuration';
const credentialsEndpoint = '/api/propagation-credentials/configured-credentials';
const [uploadStatus, setUploadStatus] = useState(UploadStatuses.clean);
const [configContents, setConfigContents] = useState(null);
const [configCredentials, setConfigCredentials] = useState(null);
const [password, setPassword] = useState('');
const [showPassword, setShowPassword] = useState(false);
const [configEncrypted, setConfigEncrypted] = useState(false);
@ -36,10 +41,11 @@ const ConfigImportModal = (props: Props) => {
const authComponent = new AuthComponent({});
useEffect(() => {
if (configContents !== null) {
if (configContents !== null && configCredentials !== null) {
console.log("useEffect was called");
tryImport();
}
}, [configContents, unsafeOptionsVerified])
}, [configContents, configCredentials, unsafeOptionsVerified])
function tryImport() {
if (configEncrypted && !showPassword){
@ -47,8 +53,10 @@ const ConfigImportModal = (props: Props) => {
} else if (configEncrypted && showPassword) {
try {
let decryptedConfig = JSON.parse(decryptText(configContents, password));
let decryptedConfigCredentials = JSON.parse(decryptText(configCredentials, password));
setConfigEncrypted(false);
setConfigContents(decryptedConfig);
setConfigCredentials(decryptedConfigCredentials);
} catch (e) {
setUploadStatus(UploadStatuses.error);
setErrorMessage('Decryption failed: Password is wrong or the file is corrupted');
@ -61,16 +69,40 @@ const ConfigImportModal = (props: Props) => {
}
} else {
sendConfigToServer();
sendConfigCredentialsToServer();
setUploadStatus(UploadStatuses.success);
}
}
function sendConfigCredentialsToServer() {
console.log("sendConfig to server called!!");
let credentials = formatCredentialsForIsland(configCredentials);
console.log(credentials);
authComponent.authFetch(credentialsEndpoint,
{
method: 'PATCH',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(credentials)
}
).then(res => {
if (res.ok) {
resetState();
props.onClose(true);
} else {
setUploadStatus(UploadStatuses.error);
setErrorMessage("Configuration file is corrupt or in an outdated format.");
}
})
}
function sendConfigToServer() {
let config = reformatConfig(configContents, true);
delete config['advanced'];
authComponent.authFetch(configImportEndpoint,
{
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify(configContents)
body: JSON.stringify(config)
}
).then(res => {
if (res.ok) {
@ -92,6 +124,7 @@ const ConfigImportModal = (props: Props) => {
setUploadStatus(UploadStatuses.clean);
setPassword('');
setConfigContents(null);
setConfigCredentials(null);
setErrorMessage('');
setShowPassword(false);
setShowUnsafeOptionsConfirmation(false);
@ -113,7 +146,8 @@ const ConfigImportModal = (props: Props) => {
return
}
setConfigEncrypted(importContents['metadata']['encrypted']);
setConfigContents(importContents['contents']);
setConfigContents(importContents['configuration']);
setConfigCredentials(importContents['credentials']);
};
reader.readAsText(event.target.files[0]);
}