diff --git a/src/epplibwrapper/client.py b/src/epplibwrapper/client.py index 6fafb2fd6..f068bfad4 100644 --- a/src/epplibwrapper/client.py +++ b/src/epplibwrapper/client.py @@ -4,14 +4,18 @@ import logging from time import sleep from gevent import Timeout +from epplibwrapper.utility.pool_error import PoolError, PoolErrorCodes from epplibwrapper.utility.pool_status import PoolStatus +logger = logging.getLogger(__name__) + try: from epplib.client import Client from epplib import commands from epplib.exceptions import TransportError, ParsingError from epplib.transport import SocketTransport except ImportError: + logger.warning("There was an import error {}") pass from django.conf import settings @@ -21,7 +25,7 @@ from .errors import LoginError, RegistryError from .socket import Socket from .utility.pool import EPPConnectionPool -logger = logging.getLogger(__name__) + try: # 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 - self._client = Client( - SocketTransport( - settings.SECRET_REGISTRY_HOSTNAME, - cert_file=CERT.filename, - key_file=KEY.filename, - password=settings.SECRET_REGISTRY_KEY_PASSPHRASE, - ) - ) + self._client = self._get_default_client() + logger.warning(f"client is this {self._client}") self.pool_options = { # Pool size @@ -82,6 +82,16 @@ class EPPLibWrapper: if 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): """Helper function used by `send`.""" cmd_type = command.__class__.__name__ @@ -217,12 +227,34 @@ class EPPLibWrapper: credentials are valid, and/or if the Registrar can be contacted """ - socket = Socket(self._login, self._client) - can_login = False + socket = self._create_default_socket() + can_login = True # 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() - 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: diff --git a/src/epplibwrapper/errors.py b/src/epplibwrapper/errors.py index dba5f328c..96188750c 100644 --- a/src/epplibwrapper/errors.py +++ b/src/epplibwrapper/errors.py @@ -86,3 +86,5 @@ class RegistryError(Exception): class LoginError(RegistryError): pass + + diff --git a/src/epplibwrapper/socket.py b/src/epplibwrapper/socket.py index 00cad80af..63fab9743 100644 --- a/src/epplibwrapper/socket.py +++ b/src/epplibwrapper/socket.py @@ -1,13 +1,15 @@ import logging from time import sleep +from epplibwrapper.utility.pool_error import PoolError, PoolErrorCodes + try: from epplib import commands from epplib.client import Client except ImportError: pass -from .errors import LoginError +from .errors import LoginError, SocketError logger = logging.getLogger(__name__) @@ -46,8 +48,9 @@ class Socket: Tries 3 times""" # Something went wrong if this doesn't exist if not hasattr(self.client, "connect"): - logger.warning("self.client does not have a connect method") - return False + message = "self.client does not have a connect method" + logger.warning(message) + raise PoolError(code=PoolErrorCodes.INVALID_CLIENT_TYPE) counter = 0 # we'll try 3 times while True: diff --git a/src/epplibwrapper/utility/pool_error.py b/src/epplibwrapper/utility/pool_error.py index 70312f32e..2febcaaa0 100644 --- a/src/epplibwrapper/utility/pool_error.py +++ b/src/epplibwrapper/utility/pool_error.py @@ -9,11 +9,13 @@ class PoolErrorCodes(IntEnum): - 2000 KILL_ALL_FAILED - 2001 NEW_CONNECTION_FAILED - 2002 KEEP_ALIVE_FAILED + - 2003 INVALID_CLIENT_TYPE """ KILL_ALL_FAILED = 2000 NEW_CONNECTION_FAILED = 2001 KEEP_ALIVE_FAILED = 2002 + INVALID_CLIENT_TYPE = 2003 class PoolError(Exception): @@ -22,16 +24,19 @@ class PoolError(Exception): - 2000 KILL_ALL_FAILED - 2001 NEW_CONNECTION_FAILED - 2002 KEEP_ALIVE_FAILED + - 2003 INVALID_CLIENT_TYPE """ # For linter kill_failed = "Could not kill all connections." conn_failed = "Failed to execute due to a registry error." alive_failed = "Failed to keep the connection alive." + invalid_client = "Invalid client type." _error_mapping = { PoolErrorCodes.KILL_ALL_FAILED: kill_failed, PoolErrorCodes.NEW_CONNECTION_FAILED: conn_failed, PoolErrorCodes.KEEP_ALIVE_FAILED: alive_failed, + PoolErrorCodes.INVALID_CLIENT_TYPE: invalid_client } def __init__(self, *args, code=None, **kwargs):