Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 2416c6f

Browse files
committed
addressed PR comments
1 parent b455d59 commit 2416c6f

3 files changed

Lines changed: 68 additions & 64 deletions

File tree

google/auth/crypt/rsa.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,8 @@
3333
class RSAVerifier(base.Verifier):
3434
"""Verifies RSA cryptographic signatures using public keys.
3535
36-
Requires installation of `cryptography` optional dependency.
37-
38-
.. deprecated::
39-
The `rsa` library has been archived. Please migrate to
40-
`cryptography` for public keys.
41-
4236
Args:
43-
public_key (Union[rsa.key.PublicKey, cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey]):
37+
public_key (Union["rsa.key.PublicKey", cryptography.hazmat.primitives.asymmetric.rsa.RSAPublicKey]):
4438
The public key used to verify signatures.
4539
Raises:
4640
ValueError: if an unrecognized public key is provided
@@ -83,14 +77,8 @@ def from_string(cls, public_key):
8377
class RSASigner(base.Signer, base.FromServiceAccountMixin):
8478
"""Signs messages with an RSA private key.
8579
86-
Requires installation of `cryptography` optional dependency.
87-
88-
.. deprecated::
89-
The `rsa` library has been archived. Please migrate to
90-
`cryptography` for public keys.
91-
9280
Args:
93-
private_key (Union[rsa.key.PrivateKey, cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey]):
81+
private_key (Union["rsa.key.PrivateKey", cryptography.hazmat.primitives.asymmetric.rsa.RSAPrivateKey]):
9482
The private key to sign with.
9583
key_id (str): Optional key ID used to identify this private key. This
9684
can be useful to associate the private key with its associated

setup.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@
1818
from setuptools import find_namespace_packages
1919
from setuptools import setup
2020

21+
cryptography_base_require = [
22+
"cryptography >= 38.0.3",
23+
]
2124

2225
DEPENDENCIES = (
2326
"pyasn1-modules>=0.2.1",
24-
"cryptography >= 38.0.3",
25-
# TODO: remove rsa from dependencies in next release (replaced with cryptography)
27+
cryptography_base_require,
28+
# TODO: remove rsa from dependencies in next release (replaced with cryptography)i
29+
# https://github.com/googleapis/google-auth-library-python/issues/1810
2630
"rsa>=3.1.4,<5",
2731
)
2832

29-
# Note: cryptography was made into a required dependency. Extra is kept for backwards compatibility
30-
cryptography_extra_require = [
31-
"cryptography >= 38.0.3",
32-
]
33-
3433
requests_extra_require = ["requests >= 2.20.0, < 3.0.0"]
3534

3635
aiohttp_extra_require = ["aiohttp >= 3.6.2, < 4.0.0", *requests_extra_require]
@@ -77,7 +76,8 @@
7776
]
7877

7978
extras = {
80-
"cryptography": cryptography_extra_require,
79+
# Note: cryptography was made into a required dependency. Extra is kept for backwards compatibility
80+
"cryptography": cryptography_base_require,
8181
"aiohttp": aiohttp_extra_require,
8282
"enterprise_cert": enterprise_cert_extra_require,
8383
"pyopenssl": pyopenssl_extra_require,

tests/crypt/test_rsa.py

Lines changed: 58 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,60 @@
2727

2828
DATA_DIR = os.path.join(os.path.dirname(__file__), "..", "data")
2929

30-
with open(os.path.join(DATA_DIR, "privatekey.pem"), "rb") as fh:
31-
PRIVATE_KEY_BYTES = fh.read()
32-
CRYPTOGRAPHY_PRIVATE_KEY = serialization.load_pem_private_key(
33-
PRIVATE_KEY_BYTES, password=None, backend=backends.default_backend()
30+
@pytest.fixture
31+
def private_key_bytes():
32+
with open(os.path.join(DATA_DIR, "privatekey.pem"), "rb") as fh:
33+
return fh.read()
34+
35+
@pytest.fixture
36+
def public_key_bytes():
37+
with open(os.path.join(DATA_DIR, "privatekey.pub"), "rb") as fh:
38+
return fh.read()
39+
40+
@pytest.fixture
41+
def cryptography_private_key(private_key_bytes):
42+
return serialization.load_pem_private_key(
43+
private_key_bytes, password=None, backend=backends.default_backend()
3444
)
35-
RSA_PRIVATE_KEY = rsa_lib.PrivateKey.load_pkcs1(PRIVATE_KEY_BYTES)
3645

37-
with open(os.path.join(DATA_DIR, "privatekey.pub"), "rb") as fh:
38-
PUBLIC_KEY_BYTES = fh.read()
39-
CRYPTOGRAPHY_PUBLIC_KEY = serialization.load_pem_public_key(
40-
PUBLIC_KEY_BYTES, backend=backends.default_backend()
46+
@pytest.fixture
47+
def rsa_private_key(private_key_bytes):
48+
return rsa_lib.PrivateKey.load_pkcs1(private_key_bytes)
49+
50+
@pytest.fixture
51+
def cryptography_public_key(public_key_bytes):
52+
return serialization.load_pem_public_key(
53+
public_key_bytes, backend=backends.default_backend()
4154
)
42-
RSA_PUBLIC_KEY = rsa_lib.PublicKey.load_pkcs1(PUBLIC_KEY_BYTES)
55+
56+
@pytest.fixture
57+
def rsa_public_key(public_key_bytes):
58+
return rsa_lib.PublicKey.load_pkcs1(public_key_bytes)
4359

4460

4561
class TestRSAVerifier:
46-
def test_init_with_cryptography_key(self):
47-
verifier = rsa.RSAVerifier(CRYPTOGRAPHY_PUBLIC_KEY)
62+
def test_init_with_cryptography_key(self, cryptography_public_key):
63+
verifier = rsa.RSAVerifier(cryptography_public_key)
4864
assert isinstance(verifier._impl, _cryptography_rsa.RSAVerifier)
49-
assert verifier._impl._pubkey == CRYPTOGRAPHY_PUBLIC_KEY
65+
assert verifier._impl._pubkey == cryptography_public_key
5066

51-
def test_init_with_rsa_key(self):
52-
verifier = rsa.RSAVerifier(RSA_PUBLIC_KEY)
67+
def test_init_with_rsa_key(self, rsa_public_key):
68+
verifier = rsa.RSAVerifier(rsa_public_key)
5369
assert isinstance(verifier._impl, _python_rsa.RSAVerifier)
54-
assert verifier._impl._pubkey == RSA_PUBLIC_KEY
70+
assert verifier._impl._pubkey == rsa_public_key
5571

56-
def test_warning_with_rsa(self):
72+
def test_warning_with_rsa(self, rsa_public_key):
5773
with pytest.warns(DeprecationWarning, match="The 'rsa' library is deprecated"):
58-
rsa.RSAVerifier(RSA_PUBLIC_KEY)
74+
rsa.RSAVerifier(rsa_public_key)
5975

6076
def test_init_with_unknown_key(self):
6177
unknown_key = object()
6278

6379
with pytest.raises(ValueError):
6480
rsa.RSAVerifier(unknown_key)
6581

66-
def test_verify_delegates(self):
67-
verifier = rsa.RSAVerifier(CRYPTOGRAPHY_PUBLIC_KEY)
82+
def test_verify_delegates(self, cryptography_public_key):
83+
verifier = rsa.RSAVerifier(cryptography_public_key)
6884

6985
# Mock the implementation's verify method
7086
with mock.patch.object(
@@ -75,41 +91,41 @@ def test_verify_delegates(self):
7591
mock_verify.assert_called_once_with(b"message", b"signature")
7692

7793
@mock.patch("google.auth.crypt.rsa._cryptography_rsa")
78-
def test_from_string_cryptography(self, mock_crypto):
94+
def test_from_string_cryptography(self, mock_crypto, public_key_bytes):
7995
expected_verifier = mock.Mock()
8096
mock_crypto.RSAVerifier.from_string.return_value = expected_verifier
8197

82-
result = rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
98+
result = rsa.RSAVerifier.from_string(public_key_bytes)
8399

84100
assert result._impl == expected_verifier
85-
mock_crypto.RSAVerifier.from_string.assert_called_once_with(PUBLIC_KEY_BYTES)
101+
mock_crypto.RSAVerifier.from_string.assert_called_once_with(public_key_bytes)
86102

87103

88104
class TestRSASigner:
89-
def test_init_with_cryptography_key(self):
90-
signer = rsa.RSASigner(CRYPTOGRAPHY_PRIVATE_KEY, key_id="123")
105+
def test_init_with_cryptography_key(self, cryptography_private_key):
106+
signer = rsa.RSASigner(cryptography_private_key, key_id="123")
91107
assert isinstance(signer._impl, _cryptography_rsa.RSASigner)
92-
assert signer._impl._key == CRYPTOGRAPHY_PRIVATE_KEY
108+
assert signer._impl._key == cryptography_private_key
93109
assert signer._impl.key_id == "123"
94110

95-
def test_init_with_rsa_key(self):
96-
signer = rsa.RSASigner(RSA_PRIVATE_KEY, key_id="123")
111+
def test_init_with_rsa_key(self, rsa_private_key):
112+
signer = rsa.RSASigner(rsa_private_key, key_id="123")
97113
assert isinstance(signer._impl, _python_rsa.RSASigner)
98-
assert signer._impl._key == RSA_PRIVATE_KEY
114+
assert signer._impl._key == rsa_private_key
99115
assert signer._impl.key_id == "123"
100116

101-
def test_warning_with_rsa(self):
117+
def test_warning_with_rsa(self, rsa_private_key):
102118
with pytest.warns(DeprecationWarning, match="The 'rsa' library is deprecated"):
103-
rsa.RSASigner(RSA_PRIVATE_KEY, key_id="123")
119+
rsa.RSASigner(rsa_private_key, key_id="123")
104120

105121
def test_init_with_unknown_key(self):
106122
unknown_key = object()
107123

108124
with pytest.raises(ValueError):
109125
rsa.RSASigner(unknown_key)
110126

111-
def test_sign_delegates(self):
112-
signer = rsa.RSASigner(RSA_PRIVATE_KEY)
127+
def test_sign_delegates(self, rsa_private_key):
128+
signer = rsa.RSASigner(rsa_private_key)
113129

114130
with mock.patch.object(
115131
signer._impl, "sign", return_value=b"signature"
@@ -119,32 +135,32 @@ def test_sign_delegates(self):
119135
mock_sign.assert_called_once_with(b"message")
120136

121137
@mock.patch("google.auth.crypt.rsa._cryptography_rsa")
122-
def test_from_string_delegates_to_cryptography(self, mock_crypto):
138+
def test_from_string_delegates_to_cryptography(self, mock_crypto, private_key_bytes):
123139
expected_signer = mock.Mock()
124140
mock_crypto.RSASigner.from_string.return_value = expected_signer
125141

126-
result = rsa.RSASigner.from_string(PRIVATE_KEY_BYTES, key_id="123")
142+
result = rsa.RSASigner.from_string(private_key_bytes, key_id="123")
127143

128144
assert result._impl == expected_signer
129145
mock_crypto.RSASigner.from_string.assert_called_once_with(
130-
PRIVATE_KEY_BYTES, key_id="123"
146+
private_key_bytes, key_id="123"
131147
)
132148

133-
def test_end_to_end_cryptography_lib(self):
134-
signer = rsa.RSASigner.from_string(PRIVATE_KEY_BYTES)
149+
def test_end_to_end_cryptography_lib(self, private_key_bytes, public_key_bytes):
150+
signer = rsa.RSASigner.from_string(private_key_bytes)
135151
message = b"Hello World"
136152
sig = signer.sign(message)
137-
verifier = rsa.RSAVerifier.from_string(PUBLIC_KEY_BYTES)
153+
verifier = rsa.RSAVerifier.from_string(public_key_bytes)
138154
result = verifier.verify(message, sig)
139155
assert result is True
140156
assert isinstance(verifier._impl, _cryptography_rsa.RSAVerifier)
141157
assert isinstance(signer._impl, _cryptography_rsa.RSASigner)
142158

143-
def test_end_to_end_rsa_lib(self):
144-
signer = rsa.RSASigner(RSA_PRIVATE_KEY)
159+
def test_end_to_end_rsa_lib(self, rsa_private_key, rsa_public_key):
160+
signer = rsa.RSASigner(rsa_private_key)
145161
message = b"Hello World"
146162
sig = signer.sign(message)
147-
verifier = rsa.RSAVerifier(RSA_PUBLIC_KEY)
163+
verifier = rsa.RSAVerifier(rsa_public_key)
148164
result = verifier.verify(message, sig)
149165
assert bool(result) is True
150166
assert isinstance(verifier._impl, _python_rsa.RSAVerifier)

0 commit comments

Comments
 (0)