Search code examples
pythonsshsftpparamiko

Getting "not a valid RSA private key file" when loading key to Python Paramiko from Key Vault


I am trying to connect to a SFTP that use username, passphrase, SSH key (no password needed) in notebook in Synapse. SSH key is kept as a secret in Key Vault.

Host = "sftp.xxxxx.no"
Username = "xxxxx"
Passphrase = "xxxxx"
port = 22

from notebookutils import mssparkutils
SSHkey = mssparkutils.credentials.getSecret('keyvault','SSHkey') #so far has no problem

keydata = b"""AAAAxxx==""" #this is a public key I got through running ssh-keyscan in terminal
key = paramiko.RSAKey(data=decodebytes(keydata))

import io
privkey = io.StringIO(SSHkey)
ki = paramiko.RSAKey.from_private_key(privkey)

cnopts = pysftp.CnOpts()
cnopts.hostkeys.add('sftp.xxxxx.no', 'ssh-rsa', key)

client = pysftp.Connection(host = Host, username = Username, private_key = SSHkey, private_key_pass = Passphrase, cnopts=cnopts)
output = client.listdir()

My SSHkey starts with "ssh-rsa AAAA... " and ends with "RF rsa-key-20221126".

The error msg is:

not a valid RSA private key file

Anyone can show me the light of the tunnel?


Solution

  • You have public key stored in your key vault. Not a private key.

    You cannot authenticate using public key. You have to store the private key to the vault.