Skip to content

Commit 5af2be9

Browse files
authored
chore: remove deprecated backend input (#18980)
cryptography's `default_backend` was removed in 36.0.0 Refs: pyca/cryptography#6499 Signed-off-by: Mike Fiedler <miketheman@gmail.com>
1 parent 9737224 commit 5af2be9

File tree

5 files changed

+11
-29
lines changed

5 files changed

+11
-29
lines changed

tests/unit/utils/test_otp.py

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

88
import pytest
99

10-
from cryptography.hazmat.backends import default_backend
1110
from cryptography.hazmat.primitives.hashes import SHA1
1211
from cryptography.hazmat.primitives.twofactor.totp import TOTP
1312
from urllib3.util import parse_url
@@ -49,19 +48,15 @@ def test_generate_totp_provisioning_uri():
4948
@pytest.mark.parametrize("skew", [0, -20, 20])
5049
def test_verify_totp_success(skew):
5150
secret = otp.generate_totp_secret()
52-
totp = TOTP(
53-
secret, otp.TOTP_LENGTH, SHA1(), otp.TOTP_INTERVAL, backend=default_backend()
54-
)
51+
totp = TOTP(secret, otp.TOTP_LENGTH, SHA1(), otp.TOTP_INTERVAL)
5552
value = totp.generate(time.time() + skew)
5653
assert otp.verify_totp(secret, value)
5754

5855

5956
@pytest.mark.parametrize("skew", [-60, 60])
6057
def test_verify_totp_failure(skew):
6158
secret = otp.generate_totp_secret()
62-
totp = TOTP(
63-
secret, otp.TOTP_LENGTH, SHA1(), otp.TOTP_INTERVAL, backend=default_backend()
64-
)
59+
totp = TOTP(secret, otp.TOTP_LENGTH, SHA1(), otp.TOTP_INTERVAL)
6560
value = totp.generate(time.time() + skew)
6661
with pytest.raises(otp.OutOfSyncTOTPError):
6762
otp.verify_totp(secret, value)

tests/unit/utils/test_sns.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import pytest
99

1010
from cryptography import x509
11-
from cryptography.hazmat.backends import default_backend
1211
from cryptography.hazmat.primitives import hashes
1312
from cryptography.hazmat.primitives.asymmetric import rsa
1413
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
@@ -32,16 +31,13 @@ def sns_privatekey():
3231
key = rsa.generate_private_key(
3332
public_exponent=65537,
3433
key_size=2048,
35-
backend=default_backend(),
3634
)
3735
return key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8, NoEncryption())
3836

3937

4038
@pytest.fixture(scope="module")
4139
def sns_publickey(sns_privatekey):
42-
private_key = load_pem_private_key(
43-
sns_privatekey, password=None, backend=default_backend()
44-
)
40+
private_key = load_pem_private_key(sns_privatekey, password=None)
4541
public_key = private_key.public_key()
4642
return public_key.public_bytes(Encoding.PEM, PublicFormat.SubjectPublicKeyInfo)
4743

@@ -50,10 +46,8 @@ def sns_publickey(sns_privatekey):
5046
def sns_certificate(sns_privatekey, sns_publickey):
5147
one_day = datetime.timedelta(1, 0, 0)
5248

53-
private_key = load_pem_private_key(
54-
sns_privatekey, password=None, backend=default_backend()
55-
)
56-
public_key = load_pem_public_key(sns_publickey, backend=default_backend())
49+
private_key = load_pem_private_key(sns_privatekey, password=None)
50+
public_key = load_pem_public_key(sns_publickey)
5751

5852
builder = x509.CertificateBuilder()
5953
builder = builder.subject_name(
@@ -74,7 +68,7 @@ def sns_certificate(sns_privatekey, sns_publickey):
7468
)
7569

7670
cert = builder.sign(
77-
private_key=private_key, algorithm=hashes.SHA256(), backend=default_backend()
71+
private_key=private_key, algorithm=hashes.SHA256(), backend=None
7872
)
7973

8074
return cert.public_bytes(Encoding.PEM)
@@ -190,9 +184,7 @@ def test_invalid(self, sns_certificate, sns_privatekey, topics, data, error):
190184
verifier = MessageVerifier(topics=topics, session=session)
191185

192186
if data.get("Signature") is VALID_SIGNATURE:
193-
private_key = load_pem_private_key(
194-
sns_privatekey, password=None, backend=default_backend()
195-
)
187+
private_key = load_pem_private_key(sns_privatekey, password=None)
196188
signature_bytes = private_key.sign(
197189
verifier._get_data_to_sign(data),
198190
PKCS1v15(),
@@ -280,9 +272,7 @@ def test_valid(self, sns_certificate, sns_privatekey, topics, data):
280272
session = pretend.stub(get=lambda url: response)
281273
verifier = MessageVerifier(topics=topics, session=session)
282274

283-
private_key = load_pem_private_key(
284-
sns_privatekey, password=None, backend=default_backend()
285-
)
275+
private_key = load_pem_private_key(sns_privatekey, password=None)
286276
signature_bytes = private_key.sign(
287277
verifier._get_data_to_sign(data),
288278
PKCS1v15(),

warehouse/integrations/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from typing import cast
77

88
from cryptography.exceptions import InvalidSignature
9-
from cryptography.hazmat.backends import default_backend
109
from cryptography.hazmat.primitives import serialization
1110
from cryptography.hazmat.primitives.asymmetric.ec import ECDSA, EllipticCurvePublicKey
1211
from cryptography.hazmat.primitives.hashes import SHA256
@@ -114,7 +113,7 @@ def _check_public_key(self, public_keys, key_id):
114113
def _check_signature(self, payload, public_key, signature):
115114
try:
116115
loaded_public_key = serialization.load_pem_public_key(
117-
data=public_key.encode("utf-8"), backend=default_backend()
116+
data=public_key.encode("utf-8")
118117
)
119118
# Use Type Narrowing to confirm the loaded_public_key is the correct type
120119
loaded_public_key = cast(EllipticCurvePublicKey, loaded_public_key)

warehouse/utils/otp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import os
55
import time
66

7-
from cryptography.hazmat.backends import default_backend
87
from cryptography.hazmat.primitives.hashes import SHA1
98
from cryptography.hazmat.primitives.twofactor import InvalidToken
109
from cryptography.hazmat.primitives.twofactor.totp import TOTP
@@ -31,7 +30,7 @@ def _get_totp(secret):
3130
* 6-digit code
3231
* 30-second interval
3332
"""
34-
return TOTP(secret, TOTP_LENGTH, SHA1(), TOTP_INTERVAL, backend=default_backend())
33+
return TOTP(secret, TOTP_LENGTH, SHA1(), TOTP_INTERVAL)
3534

3635

3736
def generate_totp_secret():

warehouse/utils/sns.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
from cryptography import x509
1010
from cryptography.exceptions import InvalidSignature as _InvalidSignature
11-
from cryptography.hazmat.backends import default_backend
1211
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15
1312
from cryptography.hazmat.primitives.hashes import SHA256
1413
from urllib3.util import parse_url
@@ -76,7 +75,7 @@ def _get_pubkey(self, cert_url):
7675
resp = self.http.get(cert_url)
7776
resp.raise_for_status()
7877

79-
cert = x509.load_pem_x509_certificate(resp.content, default_backend())
78+
cert = x509.load_pem_x509_certificate(resp.content)
8079
return cert.public_key()
8180

8281
def _get_signature(self, message):

0 commit comments

Comments
 (0)