Skip to content

Commit 8b783b9

Browse files
authored
fix: Serialize signer keys on __getstate__ for pickling (#1394)
Fixes #1383
1 parent 734da1b commit 8b783b9

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

google/auth/crypt/_cryptography_rsa.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,18 @@ def from_string(cls, key, key_id=None):
134134
key, password=None, backend=_BACKEND
135135
)
136136
return cls(private_key, key_id=key_id)
137+
138+
def __getstate__(self):
139+
"""Pickle helper that serializes the _key attribute."""
140+
state = self.__dict__.copy()
141+
state["_key"] = self._key.private_bytes(
142+
encoding=serialization.Encoding.PEM,
143+
format=serialization.PrivateFormat.PKCS8,
144+
encryption_algorithm=serialization.NoEncryption(),
145+
)
146+
return state
147+
148+
def __setstate__(self, state):
149+
"""Pickle helper that deserializes the _key attribute."""
150+
state["_key"] = serialization.load_pem_private_key(state["_key"], None)
151+
self.__dict__.update(state)

google/auth/crypt/es256.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,18 @@ def from_string(cls, key, key_id=None):
158158
key, password=None, backend=_BACKEND
159159
)
160160
return cls(private_key, key_id=key_id)
161+
162+
def __getstate__(self):
163+
"""Pickle helper that serializes the _key attribute."""
164+
state = self.__dict__.copy()
165+
state["_key"] = self._key.private_bytes(
166+
encoding=serialization.Encoding.PEM,
167+
format=serialization.PrivateFormat.PKCS8,
168+
encryption_algorithm=serialization.NoEncryption(),
169+
)
170+
return state
171+
172+
def __setstate__(self, state):
173+
"""Pickle helper that deserializes the _key attribute."""
174+
state["_key"] = serialization.load_pem_private_key(state["_key"], None)
175+
self.__dict__.update(state)

system_tests/secrets.tar.enc

0 Bytes
Binary file not shown.

tests/crypt/test__cryptography_rsa.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import json
1616
import os
17+
import pickle
1718

1819
from cryptography.hazmat.primitives.asymmetric import rsa
1920
import pytest # type: ignore
@@ -159,3 +160,17 @@ def test_from_service_account_file(self):
159160

160161
assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
161162
assert isinstance(signer._key, rsa.RSAPrivateKey)
163+
164+
def test_pickle(self):
165+
signer = _cryptography_rsa.RSASigner.from_service_account_file(
166+
SERVICE_ACCOUNT_JSON_FILE
167+
)
168+
169+
assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
170+
assert isinstance(signer._key, rsa.RSAPrivateKey)
171+
172+
pickled_signer = pickle.dumps(signer)
173+
signer = pickle.loads(pickled_signer)
174+
175+
assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
176+
assert isinstance(signer._key, rsa.RSAPrivateKey)

tests/crypt/test_es256.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import base64
1616
import json
1717
import os
18+
import pickle
1819

1920
from cryptography.hazmat.primitives.asymmetric import ec
2021
import pytest # type: ignore
@@ -141,3 +142,15 @@ def test_from_service_account_file(self):
141142

142143
assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
143144
assert isinstance(signer._key, ec.EllipticCurvePrivateKey)
145+
146+
def test_pickle(self):
147+
signer = es256.ES256Signer.from_service_account_file(SERVICE_ACCOUNT_JSON_FILE)
148+
149+
assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
150+
assert isinstance(signer._key, ec.EllipticCurvePrivateKey)
151+
152+
pickled_signer = pickle.dumps(signer)
153+
signer = pickle.loads(pickled_signer)
154+
155+
assert signer.key_id == SERVICE_ACCOUNT_INFO[base._JSON_FILE_PRIVATE_KEY_ID]
156+
assert isinstance(signer._key, ec.EllipticCurvePrivateKey)

0 commit comments

Comments
 (0)