sshtunnel icon indicating copy to clipboard operation
sshtunnel copied to clipboard

UnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)

Open lucasgherculano opened this issue 2 years ago • 1 comments

Trying to connect to rds instance throught Bastion with SSH Tunneling throw this error.

import paramiko
import sshtunnel

with sshtunnel.open_tunnel(
    (ssh_host, 22),
    ssh_username=ssh_username,
    ssh_pkey=ssh_private_key,
    remote_bind_address=(database_host, 1433),
    local_bind_address=('0.0.0.0', 10022)
) as tunnel:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('127.0.0.1', 10022)
    # do some operations with client session
    client.close()

database_host is something like:

database.hostname.us-east-1.rds.amazonaws.com

Looks like it's related to this:

https://github.com/python/cpython/issues/77139

Full Error Message (doing it from Glue)

{
  "Event": "GlueETLJobExceptionEvent",
  "Timestamp": 1659044216231,
  "Failure Reason": "Traceback (most recent call last):\n  File \"/usr/lib64/python3.7/encodings/idna.py\", line 165, in encode\n    raise UnicodeError(\"label empty or too long\")\nUnicodeError: label empty or too long\n\nThe above exception was the direct cause of the following exception:\n\nTraceback (most recent call last):\n  File \"/tmp/test-ssh-glue3.py\", line 77, in <module>\n    local_bind_address=('0.0.0.0', 10022)\n  File \"/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py\", line 1608, in __enter__\n    self.start()\n  File \"/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py\", line 1329, in start\n    self._create_tunnels()\n  File \"/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py\", line 1214, in _create_tunnels\n    self._connect_to_gateway()\n  File \"/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py\", line 1403, in _connect_to_gateway\n    self._transport = self._get_transport()\n  File \"/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py\", line 1192, in _get_transport\n    transport = paramiko.Transport(_socket)\n  File \"/home/spark/.local/lib/python3.7/site-packages/paramiko/transport.py\", line 427, in __init__\n    hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM\n  File \"/usr/lib64/python3.7/socket.py\", line 752, in getaddrinfo\n    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):\nUnicodeError: encoding with 'idna' codec failed (UnicodeError: label empty or too long)",
  "Stack Trace": [
    {
      "Declaring Class": "getaddrinfo",
      "Method Name": "for res in _socket.getaddrinfo(host, port, family, type, proto, flags):",
      "File Name": "/usr/lib64/python3.7/socket.py",
      "Line Number": 752
    },
    {
      "Declaring Class": "__init__",
      "Method Name": "hostname, port, socket.AF_UNSPEC, socket.SOCK_STREAM",
      "File Name": "/home/spark/.local/lib/python3.7/site-packages/paramiko/transport.py",
      "Line Number": 427
    },
    {
      "Declaring Class": "_get_transport",
      "Method Name": "transport = paramiko.Transport(_socket)",
      "File Name": "/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py",
      "Line Number": 1192
    },
    {
      "Declaring Class": "_connect_to_gateway",
      "Method Name": "self._transport = self._get_transport()",
      "File Name": "/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py",
      "Line Number": 1403
    },
    {
      "Declaring Class": "_create_tunnels",
      "Method Name": "self._connect_to_gateway()",
      "File Name": "/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py",
      "Line Number": 1214
    },
    {
      "Declaring Class": "start",
      "Method Name": "self._create_tunnels()",
      "File Name": "/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py",
      "Line Number": 1329
    },
    {
      "Declaring Class": "__enter__",
      "Method Name": "self.start()",
      "File Name": "/home/spark/.local/lib/python3.7/site-packages/sshtunnel.py",
      "Line Number": 1608
    },
    {
      "Declaring Class": "<module>",
      "Method Name": "local_bind_address=('0.0.0.0', 10022)",
      "File Name": "/tmp/test-ssh-glue3.py",
      "Line Number": 77
    },
    {
      "Declaring Class": "encode",
      "Method Name": "raise UnicodeError(\"label empty or too long\")",
      "File Name": "/usr/lib64/python3.7/encodings/idna.py",
      "Line Number": 165
    }
  ],
  "Last Executed Line number": 77,
  "script": "test-ssh-glue3.py"
}

lucasgherculano avatar Jul 28 '22 21:07 lucasgherculano

Solve it by using the IP to connect instead of DNS.

lucasgherculano avatar Aug 02 '22 12:08 lucasgherculano