Skip to content

[BUG] - Authentication issue with rsa key #2268

@TabsB

Description

@TabsB

Are you using paramiko as a client or server?

Client

What feature(s) aren't working right?

SFTP, Keys/auth

What version(s) of paramiko are you using?

3.2.0

What version(s) of Python are you using?

3.7.v27

What operating system and version are you using?

not sure, running from aws lambda function

If you're connecting as a client, which SSH server are you connecting to?

No response

If you're using paramiko as part of another tool, which tool/version?

No response

Expected/desired behavior

I'm using paramiko to connect to an sftp, with public/private key pair. The implementation is done in a lambda function, in aws. Paramiko is added as a layer to the lambda. My implementation for the authentication part:

transport = paramiko.Transport((host, port))
if self.password is not None:
transport.connect(username=username, password=password)
else:
rsa_private_key = paramiko.RSAKey.from_private_key(io.StringIO(private_key), passphrase)
transport.connect(username=self.username, pkey=rsa_private_key)

if transport is not None:
sftp_client = paramiko.SFTPClient.from_transport(transport)

The private_key holds an rsa private key witch starts with
-----BEGIN RSA PRIVATE KEY-----
......
-----END RSA PRIVATE KEY-----
The passphrase is None.

The authentication should pass at this point.
I already tested the key auth with another software, FileZilla and it works.

Actual behavior

Instead of connecting to the sftp server, I'm getting authentication failed.

"message": "AuthenticationException('Authentication failed.')",
"traceback": "Traceback (most recent call last):\n File "/var/task/upload.py", line 49, in main\n transport.connect(username=client_auth_data["username"], pkey=rsa_private_key)\n File "/opt/python/paramiko/transport.py", line 1411, in connect\n self.auth_publickey(username, pkey)\n File "/opt/python/paramiko/transport.py", line 1658, in auth_publickey\n return self.auth_handler.wait_for_response(my_event)\n File "/opt/python/paramiko/auth_handler.py", line 263, in wait_for_response\n raise e\nparamiko.ssh_exception.AuthenticationException: Authentication failed.\n"

I get no other specific error.

How to reproduce

sftp_client = None
transport = None
port = int(client_auth_data["port"]) if "port" in client_auth_data else 22 #default port is 22
try:
transport = paramiko.Transport((client_auth_data["host"], port))
if 'password' in client_auth_data:
transport.connect(username=client_auth_data["username"], password=client_auth_data["password"])
else:
passphrase = client_auth_data["passphrase"] if "passphrase" in client_auth_data else None
rsa_private_key = paramiko.RSAKey.from_private_key(io.StringIO((client_auth_data["private_key"])), passphrase)
transport.connect(username=client_auth_data["username"], pkey=rsa_private_key)

if transport is not None:
    sftp_client = paramiko.SFTPClient.from_transport(transport)

except Exception as e:
log.exception({"message": repr(e), "traceback":traceback.format_exc()})

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions