mirror of
https://github.com/cisagov/manage.get.gov.git
synced 2025-07-23 11:16:07 +02:00
Merge branch 'main' into dk/1016-nameservers-ui
This commit is contained in:
commit
b529a0e86f
16 changed files with 1788 additions and 764 deletions
4
.github/ISSUE_TEMPLATE/issue-default.yml
vendored
4
.github/ISSUE_TEMPLATE/issue-default.yml
vendored
|
@ -12,7 +12,7 @@ body:
|
|||
attributes:
|
||||
label: Issue description
|
||||
description: |
|
||||
Describe the issue so that someone who wasn't present for its discovery can understand why it matters. Use full sentences, plain language, and good [formatting](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax).
|
||||
Describe the issue so that someone who wasn't present for its discovery can understand why it matters. Use full sentences, plain language, and [good formatting](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax).
|
||||
validations:
|
||||
required: true
|
||||
- type: textarea
|
||||
|
@ -31,7 +31,7 @@ body:
|
|||
attributes:
|
||||
label: Links to other issues
|
||||
description: |
|
||||
"Add issue #numbers this relates to and how (e.g., 🚧 :construction: Blocks, ⛔️ :no_entry: Is blocked by, 🔄 :repeat: Relates to)."
|
||||
"Add issue #numbers this relates to and how (e.g., 🚧 [construction] Blocks, ⛔️ [no_entry] Is blocked by, 🔄 [arrows_counterclockwise] Relates to)."
|
||||
placeholder: 🔄 Relates to...
|
||||
- type: markdown
|
||||
id: note
|
||||
|
|
|
@ -4,7 +4,7 @@ Date: 2023-13-10
|
|||
|
||||
## Status
|
||||
|
||||
In Review
|
||||
Accepted
|
||||
|
||||
## Context
|
||||
|
||||
|
|
|
@ -25,7 +25,10 @@ django-phonenumber-field = {extras = ["phonenumberslite"], version = "*"}
|
|||
boto3 = "*"
|
||||
typing-extensions ='*'
|
||||
django-login-required-middleware = "*"
|
||||
greenlet = "*"
|
||||
gevent = "*"
|
||||
fred-epplib = {git = "https://github.com/cisagov/epplib.git", ref = "master"}
|
||||
geventconnpool = {git = "https://github.com/rasky/geventconnpool.git", ref = "1bbb93a714a331a069adf27265fe582d9ba7ecd4"}
|
||||
|
||||
[dev-packages]
|
||||
django-debug-toolbar = "*"
|
||||
|
|
1707
src/Pipfile.lock
generated
1707
src/Pipfile.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -1,7 +1,10 @@
|
|||
"""Provide a wrapper around epplib to handle authentication and errors."""
|
||||
|
||||
import logging
|
||||
|
||||
from time import sleep
|
||||
from gevent import Timeout
|
||||
from epplibwrapper.utility.pool_status import PoolStatus
|
||||
|
||||
try:
|
||||
from epplib.client import Client
|
||||
|
@ -16,6 +19,7 @@ from django.conf import settings
|
|||
from .cert import Cert, Key
|
||||
from .errors import LoginError, RegistryError
|
||||
from .socket import Socket
|
||||
from .utility.pool import EPPConnectionPool
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -39,9 +43,8 @@ class EPPLibWrapper:
|
|||
ATTN: This should not be used directly. Use `Domain` from domain.py.
|
||||
"""
|
||||
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, start_connection_pool=True) -> None:
|
||||
"""Initialize settings which will be used for all connections."""
|
||||
|
||||
# prepare (but do not send) a Login command
|
||||
self._login = commands.Login(
|
||||
cl_id=settings.SECRET_REGISTRY_CL_ID,
|
||||
|
@ -51,6 +54,7 @@ class EPPLibWrapper:
|
|||
"urn:ietf:params:xml:ns:contact-1.0",
|
||||
],
|
||||
)
|
||||
|
||||
# establish a client object with a TCP socket transport
|
||||
self._client = Client(
|
||||
SocketTransport(
|
||||
|
@ -60,37 +64,77 @@ class EPPLibWrapper:
|
|||
password=settings.SECRET_REGISTRY_KEY_PASSPHRASE,
|
||||
)
|
||||
)
|
||||
# prepare a context manager which will connect and login when invoked
|
||||
# (it will also logout and disconnect when the context manager exits)
|
||||
self._connect = Socket(self._client, self._login)
|
||||
|
||||
self.pool_options = {
|
||||
# Pool size
|
||||
"size": settings.EPP_CONNECTION_POOL_SIZE,
|
||||
# Which errors the pool should look out for.
|
||||
# Avoid changing this unless necessary,
|
||||
# it can and will break things.
|
||||
"exc_classes": (TransportError,),
|
||||
# Occasionally pings the registry to keep the connection alive.
|
||||
# Value in seconds => (keepalive / size)
|
||||
"keepalive": settings.POOL_KEEP_ALIVE,
|
||||
}
|
||||
|
||||
self._pool = None
|
||||
|
||||
# Tracks the status of the pool
|
||||
self.pool_status = PoolStatus()
|
||||
|
||||
if start_connection_pool:
|
||||
self.start_connection_pool()
|
||||
|
||||
def _send(self, command):
|
||||
"""Helper function used by `send`."""
|
||||
try:
|
||||
cmd_type = command.__class__.__name__
|
||||
with self._connect as wire:
|
||||
response = wire.send(command)
|
||||
|
||||
# Start a timeout to check if the pool is hanging
|
||||
timeout = Timeout(settings.POOL_TIMEOUT)
|
||||
timeout.start()
|
||||
|
||||
try:
|
||||
if not self.pool_status.connection_success:
|
||||
raise LoginError(
|
||||
"Couldn't connect to the registry after three attempts"
|
||||
)
|
||||
with self._pool.get() as connection:
|
||||
response = connection.send(command)
|
||||
except Timeout as t:
|
||||
# If more than one pool exists,
|
||||
# multiple timeouts can be floating around.
|
||||
# We need to be specific as to which we are targeting.
|
||||
if t is timeout:
|
||||
# Flag that the pool is frozen,
|
||||
# then restart the pool.
|
||||
self.pool_status.pool_hanging = True
|
||||
self.start_connection_pool()
|
||||
except (ValueError, ParsingError) as err:
|
||||
message = "%s failed to execute due to some syntax error."
|
||||
logger.warning(message, cmd_type, exc_info=True)
|
||||
message = f"{cmd_type} failed to execute due to some syntax error."
|
||||
logger.error(f"{message} Error: {err}", exc_info=True)
|
||||
raise RegistryError(message) from err
|
||||
except TransportError as err:
|
||||
message = "%s failed to execute due to a connection error."
|
||||
logger.warning(message, cmd_type, exc_info=True)
|
||||
message = f"{cmd_type} failed to execute due to a connection error."
|
||||
logger.error(f"{message} Error: {err}", exc_info=True)
|
||||
raise RegistryError(message) from err
|
||||
except LoginError as err:
|
||||
message = "%s failed to execute due to a registry login error."
|
||||
logger.warning(message, cmd_type, exc_info=True)
|
||||
# For linter due to it not liking this line length
|
||||
text = "failed to execute due to a registry login error."
|
||||
message = f"{cmd_type} {text}"
|
||||
logger.error(f"{message} Error: {err}", exc_info=True)
|
||||
raise RegistryError(message) from err
|
||||
except Exception as err:
|
||||
message = "%s failed to execute due to an unknown error." % err
|
||||
logger.warning(message, cmd_type, exc_info=True)
|
||||
message = f"{cmd_type} failed to execute due to an unknown error."
|
||||
logger.error(f"{message} Error: {err}", exc_info=True)
|
||||
raise RegistryError(message) from err
|
||||
else:
|
||||
if response.code >= 2000:
|
||||
raise RegistryError(response.msg, code=response.code)
|
||||
else:
|
||||
return response
|
||||
finally:
|
||||
# Close the timeout no matter what happens
|
||||
timeout.close()
|
||||
|
||||
def send(self, command, *, cleaned=False):
|
||||
"""Login, send the command, then close the connection. Tries 3 times."""
|
||||
|
@ -98,6 +142,23 @@ class EPPLibWrapper:
|
|||
if not cleaned:
|
||||
raise ValueError("Please sanitize user input before sending it.")
|
||||
|
||||
# Reopen the pool if its closed
|
||||
# Only occurs when a login error is raised, after connection is successful
|
||||
if not self.pool_status.pool_running:
|
||||
# We want to reopen the connection pool,
|
||||
# but we don't want the end user to wait while it opens.
|
||||
# Raise syntax doesn't allow this, so we use a try/catch
|
||||
# block.
|
||||
try:
|
||||
logger.error("Can't contact the Registry. Pool was not running.")
|
||||
raise RegistryError("Can't contact the Registry. Pool was not running.")
|
||||
except RegistryError as err:
|
||||
raise err
|
||||
finally:
|
||||
# Code execution will halt after here.
|
||||
# The end user will need to recall .send.
|
||||
self.start_connection_pool()
|
||||
|
||||
counter = 0 # we'll try 3 times
|
||||
while True:
|
||||
try:
|
||||
|
@ -109,11 +170,73 @@ class EPPLibWrapper:
|
|||
else: # don't try again
|
||||
raise err
|
||||
|
||||
def get_pool(self):
|
||||
"""Get the current pool instance"""
|
||||
return self._pool
|
||||
|
||||
def _create_pool(self, client, login, options):
|
||||
"""Creates and returns new pool instance"""
|
||||
return EPPConnectionPool(client, login, options)
|
||||
|
||||
def start_connection_pool(self, restart_pool_if_exists=True):
|
||||
"""Starts a connection pool for the registry.
|
||||
|
||||
restart_pool_if_exists -> bool:
|
||||
If an instance of the pool already exists,
|
||||
then then that instance will be killed first.
|
||||
It is generally recommended to keep this enabled.
|
||||
"""
|
||||
# Since we reuse the same creds for each pool, we can test on
|
||||
# one socket, and if successful, then we know we can connect.
|
||||
if not self._test_registry_connection_success():
|
||||
logger.warning("Cannot contact the Registry")
|
||||
self.pool_status.connection_success = False
|
||||
else:
|
||||
self.pool_status.connection_success = True
|
||||
|
||||
# If this function is reinvoked, then ensure
|
||||
# that we don't have duplicate data sitting around.
|
||||
if self._pool is not None and restart_pool_if_exists:
|
||||
logger.info("Connection pool restarting...")
|
||||
self.kill_pool()
|
||||
|
||||
self._pool = self._create_pool(self._client, self._login, self.pool_options)
|
||||
|
||||
self.pool_status.pool_running = True
|
||||
self.pool_status.pool_hanging = False
|
||||
|
||||
logger.info("Connection pool started")
|
||||
|
||||
def kill_pool(self):
|
||||
"""Kills the existing pool. Use this instead
|
||||
of self._pool = None, as that doesn't clear
|
||||
gevent instances."""
|
||||
if self._pool is not None:
|
||||
self._pool.kill_all_connections()
|
||||
self._pool = None
|
||||
self.pool_status.pool_running = False
|
||||
return None
|
||||
logger.info("kill_pool() was invoked but there was no pool to delete")
|
||||
|
||||
def _test_registry_connection_success(self):
|
||||
"""Check that determines if our login
|
||||
credentials are valid, and/or if the Registrar
|
||||
can be contacted
|
||||
"""
|
||||
socket = Socket(self._client, self._login)
|
||||
can_login = False
|
||||
|
||||
# Something went wrong if this doesn't exist
|
||||
if hasattr(socket, "test_connection_success"):
|
||||
can_login = socket.test_connection_success()
|
||||
|
||||
return can_login
|
||||
|
||||
|
||||
try:
|
||||
# Initialize epplib
|
||||
CLIENT = EPPLibWrapper()
|
||||
logger.debug("registry client initialized")
|
||||
logger.info("registry client initialized")
|
||||
except Exception:
|
||||
CLIENT = None # type: ignore
|
||||
logger.warning(
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import logging
|
||||
from time import sleep
|
||||
|
||||
try:
|
||||
from epplib import commands
|
||||
from epplib.client import Client
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
|
@ -14,24 +16,84 @@ logger = logging.getLogger(__name__)
|
|||
class Socket:
|
||||
"""Context manager which establishes a TCP connection with registry."""
|
||||
|
||||
def __init__(self, client, login) -> None:
|
||||
def __init__(self, client: Client, login: commands.Login) -> None:
|
||||
"""Save the epplib client and login details."""
|
||||
self.client = client
|
||||
self.login = login
|
||||
|
||||
def __enter__(self):
|
||||
"""Runs connect(), which opens a connection with EPPLib."""
|
||||
self.connect()
|
||||
|
||||
def __exit__(self, *args, **kwargs):
|
||||
"""Runs disconnect(), which closes a connection with EPPLib."""
|
||||
self.disconnect()
|
||||
|
||||
def connect(self):
|
||||
"""Use epplib to connect."""
|
||||
self.client.connect()
|
||||
response = self.client.send(self.login)
|
||||
if response.code >= 2000:
|
||||
if self.is_login_error(response.code):
|
||||
self.client.close()
|
||||
raise LoginError(response.msg)
|
||||
return self.client
|
||||
|
||||
def __exit__(self, *args, **kwargs):
|
||||
def disconnect(self):
|
||||
"""Close the connection."""
|
||||
try:
|
||||
self.client.send(commands.Logout())
|
||||
self.client.close()
|
||||
except Exception:
|
||||
logger.warning("Connection to registry was not cleanly closed.")
|
||||
|
||||
def send(self, command):
|
||||
"""Sends a command to the registry.
|
||||
If the RegistryError code is >= 2000,
|
||||
then this function raises a LoginError.
|
||||
The calling function should handle this."""
|
||||
response = self.client.send(command)
|
||||
if self.is_login_error(response.code):
|
||||
self.client.close()
|
||||
raise LoginError(response.msg)
|
||||
|
||||
return response
|
||||
|
||||
def is_login_error(self, code):
|
||||
"""Returns the result of code >= 2000 for RegistryError.
|
||||
This indicates that something weird happened on the Registry,
|
||||
and that we should return a LoginError."""
|
||||
return code >= 2000
|
||||
|
||||
def test_connection_success(self):
|
||||
"""Tests if a successful connection can be made with the registry.
|
||||
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 attribute")
|
||||
return False
|
||||
|
||||
counter = 0 # we'll try 3 times
|
||||
while True:
|
||||
try:
|
||||
self.client.connect()
|
||||
response = self.client.send(self.login)
|
||||
except LoginError as err:
|
||||
if err.should_retry() and counter < 3:
|
||||
counter += 1
|
||||
sleep((counter * 50) / 1000) # sleep 50 ms to 150 ms
|
||||
else: # don't try again
|
||||
return False
|
||||
# Occurs when an invalid creds are passed in - such as on localhost
|
||||
except OSError as err:
|
||||
logger.error(err)
|
||||
return False
|
||||
else:
|
||||
self.disconnect()
|
||||
|
||||
# If we encounter a login error, fail
|
||||
if self.is_login_error(response.code):
|
||||
logger.warning("A login error was found in test_connection_success")
|
||||
return False
|
||||
|
||||
# Otherwise, just return true
|
||||
return True
|
||||
|
|
0
src/epplibwrapper/tests/__init__.py
Normal file
0
src/epplibwrapper/tests/__init__.py
Normal file
258
src/epplibwrapper/tests/test_pool.py
Normal file
258
src/epplibwrapper/tests/test_pool.py
Normal file
|
@ -0,0 +1,258 @@
|
|||
import datetime
|
||||
from pathlib import Path
|
||||
from unittest.mock import MagicMock, patch
|
||||
from dateutil.tz import tzlocal # type: ignore
|
||||
from django.test import TestCase
|
||||
from epplibwrapper.client import EPPLibWrapper
|
||||
from epplibwrapper.errors import RegistryError
|
||||
from epplibwrapper.socket import Socket
|
||||
from epplibwrapper.utility.pool import EPPConnectionPool
|
||||
from registrar.models.domain import registry
|
||||
from contextlib import ExitStack
|
||||
|
||||
import logging
|
||||
|
||||
try:
|
||||
from epplib import commands
|
||||
from epplib.client import Client
|
||||
from epplib.exceptions import TransportError
|
||||
from epplib.transport import SocketTransport
|
||||
from epplib.models import common, info
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class TestConnectionPool(TestCase):
|
||||
"""Tests for our connection pooling behaviour"""
|
||||
|
||||
def setUp(self):
|
||||
# Mimic the settings added to settings.py
|
||||
self.pool_options = {
|
||||
# Current pool size
|
||||
"size": 1,
|
||||
# Which errors the pool should look out for
|
||||
"exc_classes": (TransportError,),
|
||||
# Occasionally pings the registry to keep the connection alive.
|
||||
# Value in seconds => (keepalive / size)
|
||||
"keepalive": 60,
|
||||
}
|
||||
|
||||
def fake_socket(self, login, client):
|
||||
# Linter reasons
|
||||
pw = "none"
|
||||
# Create a fake client object
|
||||
fake_client = Client(
|
||||
SocketTransport(
|
||||
"none",
|
||||
cert_file="path/to/cert_file",
|
||||
key_file="path/to/key_file",
|
||||
password=pw,
|
||||
)
|
||||
)
|
||||
|
||||
return Socket(fake_client, MagicMock())
|
||||
|
||||
def patch_success(self):
|
||||
return True
|
||||
|
||||
def fake_send(self, command, cleaned=None):
|
||||
mock = MagicMock(
|
||||
code=1000,
|
||||
msg="Command completed successfully",
|
||||
res_data=None,
|
||||
cl_tr_id="xkw1uo#2023-10-17T15:29:09.559376",
|
||||
sv_tr_id="5CcH4gxISuGkq8eqvr1UyQ==-35a",
|
||||
extensions=[],
|
||||
msg_q=None,
|
||||
)
|
||||
return mock
|
||||
|
||||
def fake_client(mock_client):
|
||||
pw = "none"
|
||||
client = Client(
|
||||
SocketTransport(
|
||||
"none",
|
||||
cert_file="path/to/cert_file",
|
||||
key_file="path/to/key_file",
|
||||
password=pw,
|
||||
)
|
||||
)
|
||||
return client
|
||||
|
||||
@patch.object(EPPLibWrapper, "_test_registry_connection_success", patch_success)
|
||||
def test_pool_sends_data(self):
|
||||
"""A .send is invoked on the pool successfully"""
|
||||
expected_result = {
|
||||
"cl_tr_id": None,
|
||||
"code": 1000,
|
||||
"extensions": [],
|
||||
"msg": "Command completed successfully",
|
||||
"msg_q": None,
|
||||
"res_data": [
|
||||
info.InfoDomainResultData(
|
||||
roid="DF1340360-GOV",
|
||||
statuses=[
|
||||
common.Status(
|
||||
state="serverTransferProhibited",
|
||||
description=None,
|
||||
lang="en",
|
||||
),
|
||||
common.Status(state="inactive", description=None, lang="en"),
|
||||
],
|
||||
cl_id="gov2023-ote",
|
||||
cr_id="gov2023-ote",
|
||||
cr_date=datetime.datetime(
|
||||
2023, 8, 15, 23, 56, 36, tzinfo=tzlocal()
|
||||
),
|
||||
up_id="gov2023-ote",
|
||||
up_date=datetime.datetime(2023, 8, 17, 2, 3, 19, tzinfo=tzlocal()),
|
||||
tr_date=None,
|
||||
name="test3.gov",
|
||||
registrant="TuaWnx9hnm84GCSU",
|
||||
admins=[],
|
||||
nsset=None,
|
||||
keyset=None,
|
||||
ex_date=datetime.date(2024, 8, 15),
|
||||
auth_info=info.DomainAuthInfo(pw="2fooBAR123fooBaz"),
|
||||
)
|
||||
],
|
||||
"sv_tr_id": "wRRNVhKhQW2m6wsUHbo/lA==-29a",
|
||||
}
|
||||
|
||||
# Mock a response from EPP
|
||||
def fake_receive(command, cleaned=None):
|
||||
location = Path(__file__).parent / "utility" / "infoDomain.xml"
|
||||
xml = (location).read_bytes()
|
||||
return xml
|
||||
|
||||
# Mock what happens inside the "with"
|
||||
with ExitStack() as stack:
|
||||
stack.enter_context(
|
||||
patch.object(EPPConnectionPool, "_create_socket", self.fake_socket)
|
||||
)
|
||||
stack.enter_context(patch.object(Socket, "connect", self.fake_client))
|
||||
stack.enter_context(patch.object(SocketTransport, "send", self.fake_send))
|
||||
stack.enter_context(patch.object(SocketTransport, "receive", fake_receive))
|
||||
# Restart the connection pool
|
||||
registry.start_connection_pool()
|
||||
# Pool should be running, and be the right size
|
||||
self.assertEqual(registry.pool_status.connection_success, True)
|
||||
self.assertEqual(registry.pool_status.pool_running, True)
|
||||
|
||||
# Send a command
|
||||
result = registry.send(commands.InfoDomain(name="test.gov"), cleaned=True)
|
||||
|
||||
# Should this ever fail, it either means that the schema has changed,
|
||||
# or the pool is broken.
|
||||
# If the schema has changed: Update the associated infoDomain.xml file
|
||||
self.assertEqual(result.__dict__, expected_result)
|
||||
|
||||
# The number of open pools should match the number of requested ones.
|
||||
# If it is 0, then they failed to open
|
||||
self.assertEqual(len(registry._pool.conn), self.pool_options["size"])
|
||||
|
||||
@patch.object(EPPLibWrapper, "_test_registry_connection_success", patch_success)
|
||||
def test_pool_restarts_on_send(self):
|
||||
"""A .send is invoked, but the pool isn't running.
|
||||
The pool should restart."""
|
||||
expected_result = {
|
||||
"cl_tr_id": None,
|
||||
"code": 1000,
|
||||
"extensions": [],
|
||||
"msg": "Command completed successfully",
|
||||
"msg_q": None,
|
||||
"res_data": [
|
||||
info.InfoDomainResultData(
|
||||
roid="DF1340360-GOV",
|
||||
statuses=[
|
||||
common.Status(
|
||||
state="serverTransferProhibited",
|
||||
description=None,
|
||||
lang="en",
|
||||
),
|
||||
common.Status(state="inactive", description=None, lang="en"),
|
||||
],
|
||||
cl_id="gov2023-ote",
|
||||
cr_id="gov2023-ote",
|
||||
cr_date=datetime.datetime(
|
||||
2023, 8, 15, 23, 56, 36, tzinfo=tzlocal()
|
||||
),
|
||||
up_id="gov2023-ote",
|
||||
up_date=datetime.datetime(2023, 8, 17, 2, 3, 19, tzinfo=tzlocal()),
|
||||
tr_date=None,
|
||||
name="test3.gov",
|
||||
registrant="TuaWnx9hnm84GCSU",
|
||||
admins=[],
|
||||
nsset=None,
|
||||
keyset=None,
|
||||
ex_date=datetime.date(2024, 8, 15),
|
||||
auth_info=info.DomainAuthInfo(pw="2fooBAR123fooBaz"),
|
||||
)
|
||||
],
|
||||
"sv_tr_id": "wRRNVhKhQW2m6wsUHbo/lA==-29a",
|
||||
}
|
||||
|
||||
# Mock a response from EPP
|
||||
def fake_receive(command, cleaned=None):
|
||||
location = Path(__file__).parent / "utility" / "infoDomain.xml"
|
||||
xml = (location).read_bytes()
|
||||
return xml
|
||||
|
||||
# Mock what happens inside the "with"
|
||||
with ExitStack() as stack:
|
||||
stack.enter_context(
|
||||
patch.object(EPPConnectionPool, "_create_socket", self.fake_socket)
|
||||
)
|
||||
stack.enter_context(patch.object(Socket, "connect", self.fake_client))
|
||||
stack.enter_context(patch.object(SocketTransport, "send", self.fake_send))
|
||||
stack.enter_context(patch.object(SocketTransport, "receive", fake_receive))
|
||||
# Kill the connection pool
|
||||
registry.kill_pool()
|
||||
|
||||
self.assertEqual(registry.pool_status.connection_success, False)
|
||||
self.assertEqual(registry.pool_status.pool_running, False)
|
||||
|
||||
# An exception should be raised as end user will be informed
|
||||
# that they cannot connect to EPP
|
||||
with self.assertRaises(RegistryError):
|
||||
expected = "InfoDomain failed to execute due to a connection error."
|
||||
result = registry.send(
|
||||
commands.InfoDomain(name="test.gov"), cleaned=True
|
||||
)
|
||||
self.assertEqual(result, expected)
|
||||
|
||||
# A subsequent command should be successful, as the pool restarts
|
||||
result = registry.send(commands.InfoDomain(name="test.gov"), cleaned=True)
|
||||
# Should this ever fail, it either means that the schema has changed,
|
||||
# or the pool is broken.
|
||||
# If the schema has changed: Update the associated infoDomain.xml file
|
||||
self.assertEqual(result.__dict__, expected_result)
|
||||
|
||||
# The number of open pools should match the number of requested ones.
|
||||
# If it is 0, then they failed to open
|
||||
self.assertEqual(len(registry._pool.conn), self.pool_options["size"])
|
||||
|
||||
@patch.object(EPPLibWrapper, "_test_registry_connection_success", patch_success)
|
||||
def test_raises_connection_error(self):
|
||||
"""A .send is invoked on the pool, but registry connection is lost
|
||||
right as we send a command."""
|
||||
|
||||
with ExitStack() as stack:
|
||||
stack.enter_context(
|
||||
patch.object(EPPConnectionPool, "_create_socket", self.fake_socket)
|
||||
)
|
||||
stack.enter_context(patch.object(Socket, "connect", self.fake_client))
|
||||
|
||||
# Pool should be running
|
||||
self.assertEqual(registry.pool_status.connection_success, True)
|
||||
self.assertEqual(registry.pool_status.pool_running, True)
|
||||
|
||||
# Try to send a command out - should fail
|
||||
with self.assertRaises(RegistryError):
|
||||
expected = "InfoDomain failed to execute due to a connection error."
|
||||
result = registry.send(
|
||||
commands.InfoDomain(name="test.gov"), cleaned=True
|
||||
)
|
||||
self.assertEqual(result, expected)
|
33
src/epplibwrapper/tests/utility/infoDomain.xml
Normal file
33
src/epplibwrapper/tests/utility/infoDomain.xml
Normal file
|
@ -0,0 +1,33 @@
|
|||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||
|
||||
<epp xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xmlns:contact="urn:ietf:params:xml:ns:contact-1.0" xmlns:fee="urn:ietf:params:xml:ns:fee-0.6" xmlns:packageToken="urn:google:params:xml:ns:packageToken-1.0" xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:fee11="urn:ietf:params:xml:ns:fee-0.11" xmlns:fee12="urn:ietf:params:xml:ns:fee-0.12" xmlns:launch="urn:ietf:params:xml:ns:launch-1.0" xmlns:secDNS="urn:ietf:params:xml:ns:secDNS-1.1" xmlns:host="urn:ietf:params:xml:ns:host-1.0">
|
||||
<response>
|
||||
<result code="1000">
|
||||
<msg>Command completed successfully</msg>
|
||||
</result>
|
||||
<resData>
|
||||
<domain:infData>
|
||||
<domain:name>test3.gov</domain:name>
|
||||
<domain:roid>DF1340360-GOV</domain:roid>
|
||||
<domain:status s="serverTransferProhibited"/>
|
||||
<domain:status s="inactive"/>
|
||||
<domain:registrant>TuaWnx9hnm84GCSU</domain:registrant>
|
||||
<domain:contact type="security">CONT2</domain:contact>
|
||||
<domain:contact type="tech">CONT3</domain:contact>
|
||||
<domain:clID>gov2023-ote</domain:clID>
|
||||
<domain:crID>gov2023-ote</domain:crID>
|
||||
<domain:crDate>2023-08-15T23:56:36Z</domain:crDate>
|
||||
<domain:upID>gov2023-ote</domain:upID>
|
||||
<domain:upDate>2023-08-17T02:03:19Z</domain:upDate>
|
||||
<domain:exDate>2024-08-15T23:56:36Z</domain:exDate>
|
||||
<domain:authInfo>
|
||||
<domain:pw>2fooBAR123fooBaz</domain:pw>
|
||||
</domain:authInfo>
|
||||
</domain:infData>
|
||||
</resData>
|
||||
<trID>
|
||||
<svTRID>wRRNVhKhQW2m6wsUHbo/lA==-29a</svTRID>
|
||||
</trID>
|
||||
</response>
|
||||
|
||||
</epp>
|
134
src/epplibwrapper/utility/pool.py
Normal file
134
src/epplibwrapper/utility/pool.py
Normal file
|
@ -0,0 +1,134 @@
|
|||
import logging
|
||||
from typing import List
|
||||
import gevent
|
||||
from geventconnpool import ConnectionPool
|
||||
from epplibwrapper.socket import Socket
|
||||
from epplibwrapper.utility.pool_error import PoolError, PoolErrorCodes
|
||||
|
||||
try:
|
||||
from epplib.commands import Hello
|
||||
from epplib.exceptions import TransportError
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
from gevent.lock import BoundedSemaphore
|
||||
from collections import deque
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class EPPConnectionPool(ConnectionPool):
|
||||
"""A connection pool for EPPLib.
|
||||
|
||||
Args:
|
||||
client (Client): The client
|
||||
login (commands.Login): Login creds
|
||||
options (dict): Options for the ConnectionPool
|
||||
base class
|
||||
"""
|
||||
|
||||
def __init__(self, client, login, options: dict):
|
||||
# For storing shared credentials
|
||||
self._client = client
|
||||
self._login = login
|
||||
|
||||
# Keep track of each greenlet
|
||||
self.greenlets: List[gevent.Greenlet] = []
|
||||
|
||||
# Define optional pool settings.
|
||||
# Kept in a dict so that the parent class,
|
||||
# client.py, can maintain seperation/expandability
|
||||
self.size = 1
|
||||
if "size" in options:
|
||||
self.size = options["size"]
|
||||
|
||||
self.exc_classes = tuple((TransportError,))
|
||||
if "exc_classes" in options:
|
||||
self.exc_classes = options["exc_classes"]
|
||||
|
||||
self.keepalive = None
|
||||
if "keepalive" in options:
|
||||
self.keepalive = options["keepalive"]
|
||||
|
||||
# Determines the period in which new
|
||||
# gevent threads are spun up.
|
||||
# This time period is in seconds. So for instance, .1 would be .1 seconds.
|
||||
self.spawn_frequency = 0.1
|
||||
if "spawn_frequency" in options:
|
||||
self.spawn_frequency = options["spawn_frequency"]
|
||||
|
||||
self.conn: deque = deque()
|
||||
self.lock = BoundedSemaphore(self.size)
|
||||
|
||||
self.populate_all_connections()
|
||||
|
||||
def _new_connection(self):
|
||||
socket = self._create_socket(self._client, self._login)
|
||||
try:
|
||||
connection = socket.connect()
|
||||
return connection
|
||||
except Exception as err:
|
||||
message = f"Failed to execute due to a registry error: {err}"
|
||||
logger.error(message, exc_info=True)
|
||||
# We want to raise a pool error rather than a LoginError here
|
||||
# because if this occurs internally, we should handle this
|
||||
# differently than we otherwise would for LoginError.
|
||||
raise PoolError(code=PoolErrorCodes.NEW_CONNECTION_FAILED) from err
|
||||
|
||||
def _keepalive(self, c):
|
||||
"""Sends a command to the server to keep the connection alive."""
|
||||
try:
|
||||
# Sends a ping to the registry via EPPLib
|
||||
c.send(Hello())
|
||||
except Exception as err:
|
||||
message = "Failed to keep the connection alive."
|
||||
logger.error(message, exc_info=True)
|
||||
raise PoolError(code=PoolErrorCodes.KEEP_ALIVE_FAILED) from err
|
||||
|
||||
def _create_socket(self, client, login) -> Socket:
|
||||
"""Creates and returns a socket instance"""
|
||||
socket = Socket(client, login)
|
||||
return socket
|
||||
|
||||
def get_connections(self):
|
||||
"""Returns the connection queue"""
|
||||
return self.conn
|
||||
|
||||
def kill_all_connections(self):
|
||||
"""Kills all active connections in the pool."""
|
||||
try:
|
||||
if len(self.conn) > 0:
|
||||
gevent.killall(self.greenlets)
|
||||
|
||||
self.greenlets.clear()
|
||||
self.conn.clear()
|
||||
|
||||
# Clear the semaphore
|
||||
self.lock = BoundedSemaphore(self.size)
|
||||
else:
|
||||
logger.info("No connections to kill.")
|
||||
except Exception as err:
|
||||
logger.error("Could not kill all connections.")
|
||||
raise PoolError(code=PoolErrorCodes.KILL_ALL_FAILED) from err
|
||||
|
||||
def populate_all_connections(self):
|
||||
"""Generates the connection pool.
|
||||
If any connections exist, kill them first.
|
||||
Based off of the __init__ definition for geventconnpool.
|
||||
"""
|
||||
if len(self.conn) > 0:
|
||||
self.kill_all_connections()
|
||||
|
||||
# Setup the lock
|
||||
for i in range(self.size):
|
||||
self.lock.acquire()
|
||||
|
||||
# Open multiple connections
|
||||
for i in range(self.size):
|
||||
self.greenlets.append(
|
||||
gevent.spawn_later(self.spawn_frequency * i, self._addOne)
|
||||
)
|
||||
|
||||
# Open a "keepalive" thread if we want to ping open connections
|
||||
if self.keepalive:
|
||||
self.greenlets.append(gevent.spawn(self._keepalive_periodic))
|
52
src/epplibwrapper/utility/pool_error.py
Normal file
52
src/epplibwrapper/utility/pool_error.py
Normal file
|
@ -0,0 +1,52 @@
|
|||
from enum import IntEnum
|
||||
|
||||
|
||||
class PoolErrorCodes(IntEnum):
|
||||
"""Used in the PoolError class for
|
||||
error mapping.
|
||||
|
||||
Overview of contact error codes:
|
||||
- 2000 KILL_ALL_FAILED
|
||||
- 2001 NEW_CONNECTION_FAILED
|
||||
- 2002 KEEP_ALIVE_FAILED
|
||||
"""
|
||||
|
||||
KILL_ALL_FAILED = 2000
|
||||
NEW_CONNECTION_FAILED = 2001
|
||||
KEEP_ALIVE_FAILED = 2002
|
||||
|
||||
|
||||
class PoolError(Exception):
|
||||
"""
|
||||
Overview of contact error codes:
|
||||
- 2000 KILL_ALL_FAILED
|
||||
- 2001 NEW_CONNECTION_FAILED
|
||||
- 2002 KEEP_ALIVE_FAILED
|
||||
|
||||
Note: These are separate from the error codes returned from EppLib
|
||||
"""
|
||||
|
||||
# Used variables due to linter requirements
|
||||
kill_failed = "Could not kill all connections. Are multiple pools running?"
|
||||
conn_failed = (
|
||||
"Failed to execute due to a registry error."
|
||||
" See previous logs to determine the cause of the error."
|
||||
)
|
||||
alive_failed = (
|
||||
"Failed to keep the connection alive. "
|
||||
"It is likely that the registry returned a LoginError."
|
||||
)
|
||||
_error_mapping = {
|
||||
PoolErrorCodes.KILL_ALL_FAILED: kill_failed,
|
||||
PoolErrorCodes.NEW_CONNECTION_FAILED: conn_failed,
|
||||
PoolErrorCodes.KEEP_ALIVE_FAILED: alive_failed,
|
||||
}
|
||||
|
||||
def __init__(self, *args, code=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.code = code
|
||||
if self.code in self._error_mapping:
|
||||
self.message = self._error_mapping.get(self.code)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.message}"
|
12
src/epplibwrapper/utility/pool_status.py
Normal file
12
src/epplibwrapper/utility/pool_status.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
class PoolStatus:
|
||||
"""A list of Booleans to keep track of Pool Status.
|
||||
|
||||
pool_running -> bool: Tracks if the pool itself is active or not.
|
||||
connection_success -> bool: Tracks if connection is possible with the registry.
|
||||
pool_hanging -> pool: Tracks if the pool has exceeded its timeout period.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.pool_running = False
|
||||
self.connection_success = False
|
||||
self.pool_hanging = False
|
|
@ -534,6 +534,20 @@ SECRET_REGISTRY_KEY = secret_registry_key
|
|||
SECRET_REGISTRY_KEY_PASSPHRASE = secret_registry_key_passphrase
|
||||
SECRET_REGISTRY_HOSTNAME = secret_registry_hostname
|
||||
|
||||
# Use this variable to set the size of our connection pool in client.py
|
||||
# WARNING: Setting this value too high could cause frequent app crashes!
|
||||
# Having too many connections open could cause the sandbox to timeout,
|
||||
# as the spinup time could exceed the timeout time.
|
||||
EPP_CONNECTION_POOL_SIZE = 1
|
||||
|
||||
# Determines the interval in which we ping open connections in seconds
|
||||
# Calculated as POOL_KEEP_ALIVE / EPP_CONNECTION_POOL_SIZE
|
||||
POOL_KEEP_ALIVE = 60
|
||||
|
||||
# Determines how long we try to keep a pool alive for,
|
||||
# before restarting it.
|
||||
POOL_TIMEOUT = 60
|
||||
|
||||
# endregion
|
||||
# region: Security and Privacy----------------------------------------------###
|
||||
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
from auditlog.registry import auditlog # type: ignore
|
||||
|
||||
from .contact import Contact
|
||||
from .domain_application import DomainApplication
|
||||
from .domain_information import DomainInformation
|
||||
|
|
|
@ -9,6 +9,14 @@ from django_fsm import FSMField, transition, TransitionNotAllowed # type: ignor
|
|||
|
||||
from django.db import models
|
||||
from typing import Any
|
||||
|
||||
|
||||
from registrar.utility.errors import (
|
||||
ActionNotAllowed,
|
||||
NameserverError,
|
||||
NameserverErrorCodes as nsErrorCodes,
|
||||
)
|
||||
|
||||
from epplibwrapper import (
|
||||
CLIENT as registry,
|
||||
commands,
|
||||
|
@ -19,15 +27,8 @@ from epplibwrapper import (
|
|||
ErrorCode,
|
||||
)
|
||||
|
||||
from registrar.utility.errors import (
|
||||
ActionNotAllowed,
|
||||
NameserverError,
|
||||
NameserverErrorCodes as nsErrorCodes,
|
||||
)
|
||||
|
||||
from registrar.models.utility.contact_error import ContactError, ContactErrorCodes
|
||||
|
||||
|
||||
from .utility.domain_field import DomainField
|
||||
from .utility.domain_helper import DomainHelper
|
||||
from .utility.time_stamped_model import TimeStampedModel
|
||||
|
|
|
@ -1,53 +1,61 @@
|
|||
-i https://pypi.python.org/simple
|
||||
asgiref==3.7.2 ; python_version >= '3.7'
|
||||
boto3==1.26.145
|
||||
botocore==1.29.145 ; python_version >= '3.7'
|
||||
cachetools==5.3.1
|
||||
certifi==2023.7.22 ; python_version >= '3.6'
|
||||
annotated-types==0.6.0; python_version >= '3.8'
|
||||
asgiref==3.7.2; python_version >= '3.7'
|
||||
boto3==1.28.66; python_version >= '3.7'
|
||||
botocore==1.31.66; python_version >= '3.7'
|
||||
cachetools==5.3.1; python_version >= '3.7'
|
||||
certifi==2023.7.22; python_version >= '3.6'
|
||||
cfenv==0.5.3
|
||||
cffi==1.15.1
|
||||
charset-normalizer==3.1.0 ; python_full_version >= '3.7.0'
|
||||
cryptography==41.0.4 ; python_version >= '3.7'
|
||||
defusedxml==0.7.1 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
|
||||
dj-database-url==2.0.0
|
||||
cffi==1.16.0; python_version >= '3.8'
|
||||
charset-normalizer==3.3.0; python_full_version >= '3.7.0'
|
||||
cryptography==41.0.4; python_version >= '3.7'
|
||||
defusedxml==0.7.1; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
|
||||
dj-database-url==2.1.0
|
||||
dj-email-url==1.0.6
|
||||
django==4.2.3
|
||||
django-allow-cidr==0.6.0
|
||||
django-auditlog==2.3.0
|
||||
django==4.2.6; python_version >= '3.8'
|
||||
django-allow-cidr==0.7.1
|
||||
django-auditlog==2.3.0; python_version >= '3.7'
|
||||
django-cache-url==3.4.4
|
||||
django-csp==3.7
|
||||
django-fsm==2.8.1
|
||||
django-login-required-middleware==0.9.0
|
||||
django-phonenumber-field[phonenumberslite]==7.1.0
|
||||
django-widget-tweaks==1.4.12
|
||||
environs[django]==9.5.0
|
||||
faker==18.10.0
|
||||
git+https://github.com/cisagov/epplib.git@d56d183f1664f34c40ca9716a3a9a345f0ef561c#egg=fred-epplib
|
||||
django-phonenumber-field[phonenumberslite]==7.2.0; python_version >= '3.8'
|
||||
django-widget-tweaks==1.5.0; python_version >= '3.8'
|
||||
environs[django]==9.5.0; python_version >= '3.6'
|
||||
faker==19.11.0; python_version >= '3.8'
|
||||
fred-epplib@ git+https://github.com/cisagov/epplib.git@d56d183f1664f34c40ca9716a3a9a345f0ef561c
|
||||
furl==2.1.3
|
||||
future==0.18.3 ; python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
||||
gunicorn==20.1.0
|
||||
idna==3.4 ; python_version >= '3.5'
|
||||
jmespath==1.0.1 ; python_version >= '3.7'
|
||||
lxml==4.9.2 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
|
||||
mako==1.2.4 ; python_version >= '3.7'
|
||||
markupsafe==2.1.2 ; python_version >= '3.7'
|
||||
marshmallow==3.19.0 ; python_version >= '3.7'
|
||||
oic==1.6.0
|
||||
future==0.18.3; python_version >= '2.6' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
||||
gevent==23.9.1; python_version >= '3.8'
|
||||
geventconnpool@ git+https://github.com/rasky/geventconnpool.git@1bbb93a714a331a069adf27265fe582d9ba7ecd4
|
||||
greenlet==3.0.0; python_version >= '3.7'
|
||||
gunicorn==21.2.0; python_version >= '3.5'
|
||||
idna==3.4; python_version >= '3.5'
|
||||
jmespath==1.0.1; python_version >= '3.7'
|
||||
lxml==4.9.3; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
|
||||
mako==1.2.4; python_version >= '3.7'
|
||||
markupsafe==2.1.3; python_version >= '3.7'
|
||||
marshmallow==3.20.1; python_version >= '3.8'
|
||||
oic==1.6.1; python_version ~= '3.7'
|
||||
orderedmultidict==1.0.1
|
||||
packaging==23.1 ; python_version >= '3.7'
|
||||
phonenumberslite==8.13.13
|
||||
psycopg2-binary==2.9.6
|
||||
packaging==23.2; python_version >= '3.7'
|
||||
phonenumberslite==8.13.23
|
||||
psycopg2-binary==2.9.9; python_version >= '3.7'
|
||||
pycparser==2.21
|
||||
pycryptodomex==3.18.0
|
||||
pydantic==1.10.8 ; python_version >= '3.7'
|
||||
pycryptodomex==3.19.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4'
|
||||
pydantic==2.4.2; python_version >= '3.7'
|
||||
pydantic-core==2.10.1; python_version >= '3.7'
|
||||
pydantic-settings==2.0.3; python_version >= '3.7'
|
||||
pyjwkest==1.4.2
|
||||
python-dateutil==2.8.2 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
||||
python-dotenv==1.0.0 ; python_version >= '3.8'
|
||||
requests==2.31.0
|
||||
s3transfer==0.6.1 ; python_version >= '3.7'
|
||||
setuptools==67.8.0 ; python_version >= '3.7'
|
||||
six==1.16.0 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
||||
sqlparse==0.4.4 ; python_version >= '3.5'
|
||||
typing-extensions==4.6.3
|
||||
urllib3==1.26.17 ; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3, 3.4, 3.5'
|
||||
whitenoise==6.4.0
|
||||
python-dateutil==2.8.2; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
||||
python-dotenv==1.0.0; python_version >= '3.8'
|
||||
requests==2.31.0; python_version >= '3.7'
|
||||
s3transfer==0.7.0; python_version >= '3.7'
|
||||
setuptools==68.2.2; python_version >= '3.8'
|
||||
six==1.16.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
||||
sqlparse==0.4.4; python_version >= '3.5'
|
||||
typing-extensions==4.8.0; python_version >= '3.8'
|
||||
urllib3==2.0.7; python_version >= '3.7'
|
||||
whitenoise==6.6.0; python_version >= '3.8'
|
||||
zope.event==5.0; python_version >= '3.7'
|
||||
zope.interface==6.1; python_version >= '3.7'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue