0

I am trying to retrieve the contents of a ppk file from secrets manager and use it to connect to SFTP.

I can do it easily if I store the file locally and pass the file.

const sshConfig = { //This works
    host: 'host',
    port: 22,
    username: 'username',
    passphrase:'passphrase',
    privateKey: fs.readFileSync(ppkFile.ppk),
    readyTimeout: 99999,
};

If I try to pass the contents of the ppk file as stored in AWS SecretsManager it doesn't work.

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,
};

I have also tried to convert the string returned from AWS SecretsManager into a buffer before instead of using fs.readFileSync but that didn't work.

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,
};

Can this be done and if so can someone help me get it right?

MountainBiker
  • 327
  • 5
  • 20

1 Answers1

0

you can not directly pass the filename if you are using lambda function. you can store a file in s3 bucket, and can pass the url to request url function, once you get the string from that function, convert that string to buffer and pass as private Key.

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('')
        })

         
        })

  }
  • I was actually trying to retrieve the contents of my ppk file from secrets manager. I ended up getting my third example to work. – MountainBiker Mar 31 '21 at 00:49