move sshfs key write logic to renderMountFile
This commit is contained in:
+4
-7
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user