Skip to content

Commit abad9f7

Browse files
committed
fix aiohttp timeouts to be the new style
1 parent 6897fac commit abad9f7

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

src/rasenmaeher_api/cfssl/anoncsr.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
import aiohttp
55

66

7-
from .base import anon_session, get_result_cert, CFSSLError, ocsprest_base
8-
from ..rmsettings import RMSettings
7+
from .base import anon_session, get_result_cert, CFSSLError, ocsprest_base, default_timeout
98

109
LOGGER = logging.getLogger(__name__)
1110

@@ -23,7 +22,7 @@ async def anon_sign_csr(csr: str, bundle: bool = True) -> str:
2322
url = f"{ocsprest_base()}/api/v1/csr/sign"
2423
payload = {"certificate_request": csr, "profile": "client", "bundle": bundle}
2524
try:
26-
async with session.post(url, json=payload, timeout=RMSettings.singleton().cfssl_timeout) as response:
25+
async with session.post(url, json=payload, timeout=default_timeout()) as response:
2726
resp = await get_result_cert(response)
2827
return resp
2928
except aiohttp.ClientError as exc:

src/rasenmaeher_api/cfssl/base.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
from ..rmsettings import RMSettings
1010

1111
LOGGER = logging.getLogger(__name__)
12-
DEFAULT_TIMEOUT = RMSettings.singleton().cfssl_timeout
12+
13+
14+
def default_timeout() -> aiohttp.ClientTimeout:
15+
"""Return configured timeout wrapped in the new aiohttp way"""
16+
return aiohttp.ClientTimeout(total=RMSettings.singleton().cfssl_timeout)
1317

1418

1519
class CFSSLError(RuntimeError):

src/rasenmaeher_api/cfssl/private.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import cryptography.x509
1010
from libadvian.tasks import TaskMaster
1111

12-
from .base import base_url, get_result_cert, CFSSLError, get_result, NoResult, ocsprest_base, DBLocked
12+
from .base import base_url, get_result_cert, CFSSLError, get_result, NoResult, ocsprest_base, DBLocked, default_timeout
1313
from .mtls import mtls_session
1414
from ..rmsettings import RMSettings
1515

@@ -29,7 +29,7 @@ async def post_ocsprest(
2929
async with (await mtls_session()) as session:
3030
try:
3131
LOGGER.debug("POSTing to {}, payload={}".format(url, send_payload))
32-
async with session.post(url, data=send_payload, timeout=timeout) as response:
32+
async with session.post(url, data=send_payload, timeout=aiohttp.ClientTimeout(total=timeout)) as response:
3333
resp_payload = await response.json()
3434
LOGGER.debug("resp_payload={}".format(resp_payload))
3535
if not resp_payload["success"]:
@@ -59,7 +59,7 @@ async def sign_csr(csr: str, bundle: bool = True) -> str:
5959
payload = {"certificate_request": csr, "profile": "client", "bundle": bundle}
6060
try:
6161
LOGGER.debug("Calling {}".format(url))
62-
async with session.post(url, json=payload, timeout=RMSettings.singleton().cfssl_timeout) as response:
62+
async with session.post(url, json=payload, timeout=default_timeout()) as response:
6363
resp = await get_result_cert(response)
6464
TaskMaster.singleton().create_task(refresh_ocsp())
6565
return resp
@@ -80,7 +80,7 @@ async def sign_ocsp(cert: str, status: str = "good") -> Any:
8080
url = f"{base_url()}/api/v1/cfssl/ocspsign"
8181
payload = {"certificate": cert, "status": status}
8282
try:
83-
async with session.post(url, json=payload, timeout=RMSettings.singleton().cfssl_timeout) as response:
83+
async with session.post(url, json=payload, timeout=default_timeout()) as response:
8484
return await get_result(response)
8585
except aiohttp.ClientError as exc:
8686
raise CFSSLError(str(exc)) from exc
@@ -137,7 +137,7 @@ async def revoke_serial(serialno: str, authority_key_id: str, reason: ReasonType
137137
"reason": str(reason.value).replace("_", ""),
138138
}
139139
try:
140-
async with session.post(url, json=payload, timeout=RMSettings.singleton().cfssl_timeout) as response:
140+
async with session.post(url, json=payload, timeout=default_timeout()) as response:
141141
try:
142142
await get_result(response)
143143
except NoResult:
@@ -179,7 +179,7 @@ async def certadd_pem(pem: Union[str, Path], status: str = "good") -> Any:
179179
}
180180
try:
181181
LOGGER.debug("POSTing {} to {}".format(payload, url))
182-
async with session.post(url, json=payload, timeout=RMSettings.singleton().cfssl_timeout) as response:
182+
async with session.post(url, json=payload, timeout=default_timeout()) as response:
183183
return await get_result(response)
184184
except aiohttp.ClientError as exc:
185185
raise CFSSLError(str(exc)) from exc

src/rasenmaeher_api/cfssl/public.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@
55

66
import aiohttp
77

8-
from .base import base_url, anon_session, get_result, get_result_cert, CFSSLError, get_result_bundle, ocsprest_base
8+
from .base import (
9+
base_url,
10+
anon_session,
11+
get_result,
12+
get_result_cert,
13+
CFSSLError,
14+
get_result_bundle,
15+
ocsprest_base,
16+
default_timeout,
17+
)
918
from .private import refresh_ocsp
10-
from ..rmsettings import RMSettings
1119

1220

1321
LOGGER = logging.getLogger(__name__)
@@ -25,7 +33,7 @@ async def get_ca() -> str:
2533
payload: Dict[str, Any] = {}
2634
# PONDER: Why does this need to be a POST ??
2735
try:
28-
async with session.post(url, json=payload, timeout=RMSettings.singleton().cfssl_timeout) as response:
36+
async with session.post(url, json=payload, timeout=default_timeout()) as response:
2937
return await get_result_cert(response)
3038
except aiohttp.ClientError as exc:
3139
raise CFSSLError(str(exc)) from exc
@@ -54,9 +62,7 @@ async def get_crl() -> bytes:
5462
async with (await anon_session()) as session:
5563
url = f"{base_url()}/api/v1/cfssl/crl"
5664
try:
57-
async with session.get(
58-
url, params={"expiry": CRL_LIFETIME}, timeout=RMSettings.singleton().cfssl_timeout
59-
) as response:
65+
async with session.get(url, params={"expiry": CRL_LIFETIME}, timeout=default_timeout()) as response:
6066
crl_b64 = await get_result(response)
6167
data = base64.b64decode(crl_b64)
6268
return data
@@ -76,7 +82,7 @@ async def get_bundle(cert: str) -> str:
7682
url = f"{base_url()}/api/v1/cfssl/bundle"
7783
payload: Dict[str, Any] = {"certificate": cert, "flavor": "optimal"}
7884
try:
79-
async with session.post(url, json=payload, timeout=RMSettings.singleton().cfssl_timeout) as response:
85+
async with session.post(url, json=payload, timeout=default_timeout()) as response:
8086
return await get_result_bundle(response)
8187
except aiohttp.ClientError as exc:
8288
raise CFSSLError(str(exc)) from exc

tests/ptfpapi/fpinit.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
LOGGER = logging.getLogger(__name__)
1717
DATAPATH = Path("/data/persistent")
18+
TIMEOUT = aiohttp.ClientTimeout(total=2.0)
1819

1920
# we know we have copy-pasted this shit here, it's for the best, this time...
2021
# pylint: disable=R0801
@@ -80,7 +81,7 @@ async def get_ca() -> str:
8081
payload: Dict[str, Any] = {}
8182

8283
# FIXME: Why does this need to be a POST ??
83-
async with session.post(url, json=payload, timeout=2.0) as response:
84+
async with session.post(url, json=payload, timeout=TIMEOUT) as response:
8485
data = cast(Mapping[str, Union[Any, Mapping[str, Any]]], await response.json())
8586
result = data.get("result")
8687
if not result:
@@ -103,7 +104,7 @@ async def sign_csr(csr: str) -> str:
103104
session.headers.add("Content-Type", "application/json")
104105
url = f"{cfssl_host}:{cfssl_port}/api/v1/cfssl/sign"
105106
payload = {"certificate_request": csr}
106-
async with session.post(url, json=payload, timeout=2.0) as response:
107+
async with session.post(url, json=payload, timeout=TIMEOUT) as response:
107108
data = cast(Mapping[str, Union[Any, Mapping[str, Any]]], await response.json())
108109
result = data.get("result")
109110
if not result:

0 commit comments

Comments
 (0)