backups: remove validation mount point after testing it

this also moves out the attempt validation logic from mounts code
into volumes. mounts.tryAddMount is also used in backup code
This commit is contained in:
Girish Ramakrishnan
2023-09-29 06:49:55 +05:30
parent 8d0abf214c
commit 64381e2a04
5 changed files with 59 additions and 62 deletions
+47 -44
View File
@@ -349,7 +349,7 @@ async function validateEncryptionPassword(password) {
if (password.length < 8) return new BoxError(BoxError.BAD_FIELD, 'password must be atleast 8 characters');
}
function mountObjectFromBackupConfig(backupConfig) {
function managedBackupMountObject(backupConfig) {
assert(mounts.isManagedProvider(backupConfig.provider));
return {
@@ -366,7 +366,7 @@ async function remount(auditSource) {
const backupConfig = await getConfig();
if (mounts.isManagedProvider(backupConfig.provider)) {
await mounts.remount(mountObjectFromBackupConfig(backupConfig));
await mounts.remount(managedBackupMountObject(backupConfig));
}
}
@@ -449,48 +449,6 @@ function validateFormat(format) {
return new BoxError(BoxError.BAD_FIELD, 'Invalid backup format');
}
async function setStorage(storageConfig) {
assert.strictEqual(typeof storageConfig, 'object');
const oldConfig = await getConfig();
if (storageConfig.provider === oldConfig.provider) storage.api(storageConfig.provider).injectPrivateFields(storageConfig, oldConfig);
let error = validateFormat(storageConfig.format);
if (error) throw error;
debug('setStorage: validating new storage configuration');
await setupStorage(storageConfig, '/mnt/backup-storage-validation');
storageConfig.rootPath = getRootPath(storageConfig, '/mnt/backup-storage-validation');
error = await testStorage(storageConfig);
delete storageConfig.rootPath;
if (error) throw error;
debug('setStorage: removing old storage configuration');
if (mounts.isManagedProvider(oldConfig.provider)) await safe(mounts.removeMount(mountObjectFromBackupConfig(oldConfig)));
debug('setStorage: setting up new storage configuration');
await setupStorage(storageConfig, paths.MANAGED_BACKUP_MOUNT_DIR);
storageConfig.encryption = null;
if ('password' in storageConfig) { // user set password
if (storageConfig.password === constants.SECRET_PLACEHOLDER) {
storageConfig.encryption = oldConfig.encryption || null;
} else {
const error = await validateEncryptionPassword(storageConfig.password);
if (error) throw error;
storageConfig.encryption = generateEncryptionKeysSync(storageConfig.password);
}
delete storageConfig.password;
}
debug('setBackupConfig: clearing backup cache');
cleanupCacheFilesSync();
await settings.setJson(settings.BACKUP_STORAGE_KEY, storageConfig);
}
async function setupStorage(storageConfig, hostPath) {
assert.strictEqual(typeof storageConfig, 'object');
assert.strictEqual(typeof hostPath, 'string');
@@ -500,6 +458,8 @@ async function setupStorage(storageConfig, hostPath) {
const error = mounts.validateMountOptions(storageConfig.provider, storageConfig.mountOptions);
if (error) throw error;
debug(`setupStorage: setting up mount at ${hostPath} with ${storageConfig.provider}`);
const newMount = {
name: path.basename(hostPath),
hostPath: hostPath,
@@ -511,3 +471,46 @@ async function setupStorage(storageConfig, hostPath) {
return newMount;
}
async function setStorage(storageConfig) {
assert.strictEqual(typeof storageConfig, 'object');
const oldConfig = await getConfig();
if (storageConfig.provider === oldConfig.provider) storage.api(storageConfig.provider).injectPrivateFields(storageConfig, oldConfig);
const foratmError = validateFormat(storageConfig.format);
if (foratmError) throw foratmError;
debug('setStorage: validating new storage configuration');
const rootPath = getRootPath(storageConfig, '/mnt/backup-storage-validation');
const testStorageConfig = Object.assign({ rootPath }, storageConfig);
const testMountObject = await setupStorage(testStorageConfig, '/mnt/backup-storage-validation');
const testStorageError = await testStorage(testStorageConfig);
if (testMountObject) await mounts.removeMount(testMountObject);
if (testStorageError) throw testStorageError;
debug('setStorage: removing old storage configuration');
if (mounts.isManagedProvider(oldConfig.provider)) await safe(mounts.removeMount(managedBackupMountObject(oldConfig)));
debug('setStorage: setting up new storage configuration');
await setupStorage(storageConfig, paths.MANAGED_BACKUP_MOUNT_DIR);
storageConfig.encryption = null;
if ('password' in storageConfig) { // user set password
if (storageConfig.password === constants.SECRET_PLACEHOLDER) {
storageConfig.encryption = oldConfig.encryption || null;
} else {
const encryptionPasswordError = await validateEncryptionPassword(storageConfig.password);
if (encryptionPasswordError) throw encryptionPasswordError;
storageConfig.encryption = generateEncryptionKeysSync(storageConfig.password);
}
delete storageConfig.password;
}
debug('setBackupConfig: clearing backup cache');
cleanupCacheFilesSync();
await settings.setJson(settings.BACKUP_STORAGE_KEY, storageConfig);
}