diff --git a/src/mounts.js b/src/mounts.js index 7395e7cff..1399d8087 100644 --- a/src/mounts.js +++ b/src/mounts.js @@ -13,6 +13,7 @@ const assert = require('assert'), ejs = require('ejs'), fs = require('fs'), path = require('path'), + paths = require('./paths.js'), safe = require('safetydance'), shell = require('./shell.js'); @@ -39,6 +40,13 @@ function validateMountOptions(type, options) { if (typeof options.host !== 'string') return new BoxError(BoxError.BAD_FIELD, 'host is not a string'); if (typeof options.remoteDir !== 'string') return new BoxError(BoxError.BAD_FIELD, 'remoteDir is not a string'); return null; + case 'sshfs': + if (typeof options.user !== 'string') return new BoxError(BoxError.BAD_FIELD, 'user is not a string'); + if (typeof options.privateKey !== 'string') return new BoxError(BoxError.BAD_FIELD, 'privateKey is not a string'); + if (typeof options.port !== 'number') return new BoxError(BoxError.BAD_FIELD, 'port is not a number'); + if (typeof options.host !== 'string') return new BoxError(BoxError.BAD_FIELD, 'host is not a string'); + if (typeof options.remoteDir !== 'string') return new BoxError(BoxError.BAD_FIELD, 'remoteDir is not a string'); + return null; case 'ext4': if (typeof options.diskPath !== 'string') return new BoxError(BoxError.BAD_FIELD, 'diskPath is not a string'); return null; @@ -72,9 +80,15 @@ async function writeMountFile(volume) { options = 'discard,defaults,noatime'; break; case 'sshfs': - // type = 'sshfs'; - // What={{ USER }}@{{ HOST }}:{{ REMOTE DIR }} - // Options=_netdev,allow_other,IdentityFile=/home/{{ MY LOCAL USER WITH SSH KEY IN ITS HOME DIRECTORY }}/.ssh/id_rsa,reconnect,x-systemd.automount,uid=1000,gid=1000 + const keyFilePath = path.join(paths.SSHFS_KEYS_DIR, `id_rsa_${mountOptions.host}`); + + safe.fs.mkdirSync(paths.SSHFS_KEYS_DIR); + if (!safe.fs.writeFileSync(keyFilePath, mountOptions.privateKey, { mode: 0o600 })) throw new BoxError(BoxError.FS_ERROR, safe.error); + + type = 'fuse.sshfs'; + what= `${mountOptions.user}@${mountOptions.host}:${mountOptions.remoteDir}`; + options = `defaults,allow_other,port=${mountOptions.port},IdentityFile=${keyFilePath},reconnect,uid=yellowtent,gid=yellowtent`; + break; } const mountFileContents = ejs.render(SYSTEMD_MOUNT_EJS, { name, what, where: hostPath, options, type }); diff --git a/src/paths.js b/src/paths.js index 8ceff2c71..d6074fd17 100644 --- a/src/paths.js +++ b/src/paths.js @@ -41,6 +41,7 @@ exports = module.exports = { FEATURES_INFO_FILE: path.join(baseDir(), 'platformdata/features-info.json'), PROXY_AUTH_TOKEN_SECRET_FILE: path.join(baseDir(), 'platformdata/proxy-auth-token-secret'), VERSION_FILE: path.join(baseDir(), 'platformdata/VERSION'), + SSHFS_KEYS_DIR: path.join(baseDir(), 'platformdata/sshfs'), SFTP_KEYS_DIR: path.join(baseDir(), 'platformdata/sftp/ssh'), SFTP_PUBLIC_KEY_FILE: path.join(baseDir(), 'platformdata/sftp/ssh/ssh_host_rsa_key.pub'), SFTP_PRIVATE_KEY_FILE: path.join(baseDir(), 'platformdata/sftp/ssh/ssh_host_rsa_key'),