Debug client bug

This commit is contained in:
zandercymatics 2023-10-19 07:59:09 -06:00
parent ceb2e5ec66
commit 825d07ba7c
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7
4 changed files with 58 additions and 16 deletions

View file

@ -4,14 +4,18 @@ import logging
from time import sleep from time import sleep
from gevent import Timeout from gevent import Timeout
from epplibwrapper.utility.pool_error import PoolError, PoolErrorCodes
from epplibwrapper.utility.pool_status import PoolStatus from epplibwrapper.utility.pool_status import PoolStatus
logger = logging.getLogger(__name__)
try: try:
from epplib.client import Client from epplib.client import Client
from epplib import commands from epplib import commands
from epplib.exceptions import TransportError, ParsingError from epplib.exceptions import TransportError, ParsingError
from epplib.transport import SocketTransport from epplib.transport import SocketTransport
except ImportError: except ImportError:
logger.warning("There was an import error {}")
pass pass
from django.conf import settings from django.conf import settings
@ -21,7 +25,7 @@ from .errors import LoginError, RegistryError
from .socket import Socket from .socket import Socket
from .utility.pool import EPPConnectionPool from .utility.pool import EPPConnectionPool
logger = logging.getLogger(__name__)
try: try:
# Write cert and key to disk # Write cert and key to disk
@ -55,15 +59,11 @@ class EPPLibWrapper:
], ],
) )
# TODO - if client is none, send signal up and set it
# back to this
# establish a client object with a TCP socket transport # establish a client object with a TCP socket transport
self._client = Client( self._client = self._get_default_client()
SocketTransport( logger.warning(f"client is this {self._client}")
settings.SECRET_REGISTRY_HOSTNAME,
cert_file=CERT.filename,
key_file=KEY.filename,
password=settings.SECRET_REGISTRY_KEY_PASSPHRASE,
)
)
self.pool_options = { self.pool_options = {
# Pool size # Pool size
@ -82,6 +82,16 @@ class EPPLibWrapper:
if start_connection_pool: if start_connection_pool:
self.start_connection_pool() self.start_connection_pool()
def _get_default_client(self):
return Client(
SocketTransport(
settings.SECRET_REGISTRY_HOSTNAME,
cert_file=CERT.filename,
key_file=KEY.filename,
password=settings.SECRET_REGISTRY_KEY_PASSPHRASE,
)
)
def _send(self, command): def _send(self, command):
"""Helper function used by `send`.""" """Helper function used by `send`."""
cmd_type = command.__class__.__name__ cmd_type = command.__class__.__name__
@ -217,12 +227,34 @@ class EPPLibWrapper:
credentials are valid, and/or if the Registrar credentials are valid, and/or if the Registrar
can be contacted can be contacted
""" """
socket = Socket(self._login, self._client) socket = self._create_default_socket()
can_login = False can_login = True
# Something went wrong if this doesn't exist # Something went wrong if this doesn't exist
if hasattr(socket, "test_connection_success"): if not hasattr(socket, "test_connection_success"):
return can_login
try:
can_login = socket.test_connection_success() can_login = socket.test_connection_success()
return can_login except PoolError as err:
logger.error(err)
# If the client isn't the right type,
# recreate it.
if err.code == PoolErrorCodes.INVALID_CLIENT_TYPE:
# Try to recreate the socket
self._client = self._get_default_client()
socket = self._create_default_socket()
# Test it again
can_login = socket.test_connection_success()
return can_login
else:
return can_login
def _create_default_socket(self):
"""Creates a default socket.
Uses self._login and self._client
"""
return Socket(self._login, self._client)
try: try:

View file

@ -86,3 +86,5 @@ class RegistryError(Exception):
class LoginError(RegistryError): class LoginError(RegistryError):
pass pass

View file

@ -1,13 +1,15 @@
import logging import logging
from time import sleep from time import sleep
from epplibwrapper.utility.pool_error import PoolError, PoolErrorCodes
try: try:
from epplib import commands from epplib import commands
from epplib.client import Client from epplib.client import Client
except ImportError: except ImportError:
pass pass
from .errors import LoginError from .errors import LoginError, SocketError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -46,8 +48,9 @@ class Socket:
Tries 3 times""" Tries 3 times"""
# Something went wrong if this doesn't exist # Something went wrong if this doesn't exist
if not hasattr(self.client, "connect"): if not hasattr(self.client, "connect"):
logger.warning("self.client does not have a connect method") message = "self.client does not have a connect method"
return False logger.warning(message)
raise PoolError(code=PoolErrorCodes.INVALID_CLIENT_TYPE)
counter = 0 # we'll try 3 times counter = 0 # we'll try 3 times
while True: while True:

View file

@ -9,11 +9,13 @@ class PoolErrorCodes(IntEnum):
- 2000 KILL_ALL_FAILED - 2000 KILL_ALL_FAILED
- 2001 NEW_CONNECTION_FAILED - 2001 NEW_CONNECTION_FAILED
- 2002 KEEP_ALIVE_FAILED - 2002 KEEP_ALIVE_FAILED
- 2003 INVALID_CLIENT_TYPE
""" """
KILL_ALL_FAILED = 2000 KILL_ALL_FAILED = 2000
NEW_CONNECTION_FAILED = 2001 NEW_CONNECTION_FAILED = 2001
KEEP_ALIVE_FAILED = 2002 KEEP_ALIVE_FAILED = 2002
INVALID_CLIENT_TYPE = 2003
class PoolError(Exception): class PoolError(Exception):
@ -22,16 +24,19 @@ class PoolError(Exception):
- 2000 KILL_ALL_FAILED - 2000 KILL_ALL_FAILED
- 2001 NEW_CONNECTION_FAILED - 2001 NEW_CONNECTION_FAILED
- 2002 KEEP_ALIVE_FAILED - 2002 KEEP_ALIVE_FAILED
- 2003 INVALID_CLIENT_TYPE
""" """
# For linter # For linter
kill_failed = "Could not kill all connections." kill_failed = "Could not kill all connections."
conn_failed = "Failed to execute due to a registry error." conn_failed = "Failed to execute due to a registry error."
alive_failed = "Failed to keep the connection alive." alive_failed = "Failed to keep the connection alive."
invalid_client = "Invalid client type."
_error_mapping = { _error_mapping = {
PoolErrorCodes.KILL_ALL_FAILED: kill_failed, PoolErrorCodes.KILL_ALL_FAILED: kill_failed,
PoolErrorCodes.NEW_CONNECTION_FAILED: conn_failed, PoolErrorCodes.NEW_CONNECTION_FAILED: conn_failed,
PoolErrorCodes.KEEP_ALIVE_FAILED: alive_failed, PoolErrorCodes.KEEP_ALIVE_FAILED: alive_failed,
PoolErrorCodes.INVALID_CLIENT_TYPE: invalid_client
} }
def __init__(self, *args, code=None, **kwargs): def __init__(self, *args, code=None, **kwargs):