theophilusx / ssh2-sftp-client

a client for SSH2 SFTP
Apache License 2.0
808 stars 199 forks source link

Cannot parse privatekey #496

Closed mati-fp closed 1 year ago

mati-fp commented 1 year ago

Every time i try to connect in a server via sftp i getting that my RSA key is in an unsupported key format and cannot parse, what can i do to change that?

------ Code here ---------------

import { Injectable } from '@nestjs/common';
import { BaseComponent } from '@tecredi/shared/feature/core';
import Client from 'ssh2-sftp-client';
import { readFileSync } from 'fs';

@Injectable()
export class NegativacaoSerasaService extends BaseComponent {
  constructor() {
    super();
  }

  async sendNegativationSerasa(document: string) {
    let clientSerasa;

    try {
      clientSerasa = await this.connectViaSftp();
      this.logger.debug(await clientSerasa.cwd());
    } catch (error) {
      this.logger.error('Error em sendNegativationSerasa', error);
    } finally {
      if (clientSerasa) {
        await clientSerasa.end();
        this.logger.debug('Conexão com Serasa via SFTP finalizada');
      }
    }
  }

  private async connectViaSftp() {
    const sftp = new Client();
    const config = {
      host: 'blablabla',
      port: 0000,
      username: '0000',
      privateKey: readFileSync('/home/pole/new_private_serasa_key.pem'),
    };

    try {
      await sftp.connect(config);
      this.logger.debug('Conexão com Serasa via SFTP bem sucedida');
      return sftp;
    } catch (error) {
      this.logger.error('error', error);
      throw error;
    }
  }
}

---- Error here -------------

Error: connect: Cannot parse privateKey: Unsupported key format

theophilusx commented 1 year ago

This is a known issue with the ssh2 library. Basically, RSA key format is not supported. You need to use a different key, such as ed25519. Lots more can be found about this in the issues on the ssh2 repository.

theophilusx commented 1 year ago

As this is an ssh2 library limitation and cannot be addressed in this module, closing this issue.