move sshfs key write logic to renderMountFile

This commit is contained in:
Girish Ramakrishnan
2022-03-29 20:15:55 -07:00
parent cbc44da102
commit 0dea2d283b
+4 -7
View File
@@ -83,7 +83,7 @@ function mountObjectFromBackupConfig(backupConfig) {
function renderMountFile(mount) {
assert.strictEqual(typeof mount, 'object');
const {name, hostPath, mountType, mountOptions} = mount;
const { name, hostPath, mountType, mountOptions } = mount;
let options, what, type;
switch (mountType) {
@@ -104,6 +104,9 @@ function renderMountFile(mount) {
break;
case 'sshfs': {
const keyFilePath = path.join(paths.SSHFS_KEYS_DIR, `id_rsa_${mountOptions.host}`);
if (!safe.fs.writeFileSync(keyFilePath, `${mount.mountOptions.privateKey}\n`, { mode: 0o600 })) throw new BoxError(BoxError.FS_ERROR, `Could not write private key: ${safe.error.message}`);
type = 'fuse.sshfs';
what = `${mountOptions.user}@${mountOptions.host}:${mountOptions.remoteDir}`;
options = `allow_other,port=${mountOptions.port},IdentityFile=${keyFilePath},StrictHostKeyChecking=no,reconnect`; // allow_other means non-root users can access it
@@ -181,12 +184,6 @@ async function tryAddMount(mount, options) {
if (constants.TEST) return;
if (mount.mountType === 'sshfs') {
const keyFilePath = path.join(paths.SSHFS_KEYS_DIR, `id_rsa_${mount.mountOptions.host}`);
if (!safe.fs.writeFileSync(keyFilePath, `${mount.mountOptions.privateKey}\n`, { mode: 0o600 })) throw new BoxError(BoxError.FS_ERROR, safe.error);
}
const [error] = await safe(shell.promises.sudo('addMount', [ ADD_MOUNT_CMD, renderMountFile(mount), options.timeout ], {}));
if (error && error.code === 2) throw new BoxError(BoxError.MOUNT_ERROR, 'Failed to unmount existing mount'); // at this point, the old mount config is still there