我正在尝试从密钥管理器中检索ppk文件的内容,并使用它连接到SFTP。
如果我将文件存储在本地并传递文件,我可以很容易地做到这一点。
const sshConfig = { //This works
host: 'host',
port: 22,
username: 'username',
passphrase:'passphrase',
privateKey: fs.readFileSync(ppkFile.ppk),
readyTimeout: 99999,
};如果我尝试传递存储在AWS SecretsManager中的ppk文件的内容,它不起作用。
const sshConfig = { //This doesn't work
host: 'host',
port: 22,
username: 'username',
passphrase:'passphrase',
privateKey: fs.readFileSync('file contents retrieved as a string from secretsmanager'),
readyTimeout: 99999,
};我以前也尝试过将亚马逊网络服务SecretsManager返回的字符串转换为缓冲区,而不是使用fs.readFileSync,但这不起作用。
const sshConfig = { //This doesn't work
host: 'host',
port: 22,
username: 'username',
passphrase:'passphrase',
privateKey: Buffer.from('file contents retrieved as a string from secretsmanager'),
readyTimeout: 99999,
};这能做到吗?如果能做到,有人能帮我把它做好吗?
发布于 2021-02-23 17:56:27
如果您正在使用lambda函数,则不能直接传递文件名。您可以将文件存储在s3存储桶中,并可以将url传递给请求url函数,一旦您从该函数获得字符串,将该字符串转换为缓冲区并作为私钥传递。
let Client = require("ssh2-sftp-client");
let fs = require("fs")
var request = require('request');
//var path = require('path');
function getFile(){
return new Promise(function(resolve,reject){
request.get('https://test.com/private.ppk', function (error, response, body) {
if (!error && response.statusCode == 200) {
var csv = body;
// Continue with your processing here.
console.log("data",csv)
resolve(csv)
}
});
})
}
function uploadToFtp() {
return new Promise(async (resolve, reject) => {
const c = new Client();
const file = await getFile()
c.connect({
host: "host",
port: '22',
user: "root",
password : "root>",
privateKey: Buffer.from(file),
passphrase: 'abcd1234'
}).then(()=>{
console.log("FTP CONNECTED")
resolve('')
}).catch((err)=>{
console.log(`funcName:Shared uploadToFtp ---> ${err.message}`)
reject('')
})
})
}
https://stackoverflow.com/questions/61022821
复制相似问题