Skip to content

Commit 0bf3242

Browse files
kajinamitmnaser
authored andcommitted
Fix errors caused by cryptography>=35.0.0
- _OID_NAMES was moved to a different module by [1]. - default_backend() is silently ignored, so should be dropped[2]. - The new Rust backend does not accept mocked private keys which caused failures with invalid private keys for tests. [1]: pyca/cryptography@7b56349 [2]: https://cryptography.io/en/latest/faq/#faq-missing-backend Change-Id: I44407703fbcf2da97c29a28043520c781ef4c3b2
1 parent 6951ac4 commit 0bf3242

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

magnum/common/x509/operations.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import six
1717
import uuid
1818

19-
from cryptography.hazmat.backends import default_backend
2019
from cryptography.hazmat.primitives.asymmetric import rsa
2120
from cryptography.hazmat.primitives import hashes
2221
from cryptography.hazmat.primitives import serialization
@@ -112,8 +111,7 @@ def _generate_certificate(issuer_name, subject_name, extensions,
112111

113112
private_key = rsa.generate_private_key(
114113
public_exponent=65537,
115-
key_size=CONF.x509.rsa_key_size,
116-
backend=default_backend()
114+
key_size=CONF.x509.rsa_key_size
117115
)
118116

119117
# subject name is set as common name
@@ -132,7 +130,7 @@ def _generate_certificate(issuer_name, subject_name, extensions,
132130
ca_key = private_key
133131
ca_key_password = encryption_password
134132

135-
csr = csr.sign(private_key, hashes.SHA256(), default_backend())
133+
csr = csr.sign(private_key, hashes.SHA256())
136134

137135
if six.PY3 and isinstance(encryption_password, six.text_type):
138136
encryption_password = encryption_password.encode()
@@ -170,8 +168,7 @@ def _load_pem_private_key(ca_key, ca_key_password=None):
170168

171169
ca_key = serialization.load_pem_private_key(
172170
ca_key,
173-
password=ca_key_password,
174-
backend=default_backend()
171+
password=ca_key_password
175172
)
176173

177174
return ca_key
@@ -198,7 +195,7 @@ def sign(csr, issuer_name, ca_key, ca_key_password=None,
198195
csr = six.b(str(csr))
199196
if not isinstance(csr, x509.CertificateSigningRequest):
200197
try:
201-
csr = x509.load_pem_x509_csr(csr, backend=default_backend())
198+
csr = x509.load_pem_x509_csr(csr)
202199
except ValueError:
203200
LOG.exception("Received invalid csr %s.", csr)
204201
raise exception.InvalidCsr(csr=csr)
@@ -229,7 +226,6 @@ def sign(csr, issuer_name, ca_key, ca_key_password=None,
229226

230227
certificate = builder.sign(
231228
private_key=ca_key, algorithm=hashes.SHA256(),
232-
backend=default_backend()
233229
).public_bytes(serialization.Encoding.PEM).strip()
234230

235231
return certificate
@@ -239,14 +235,14 @@ def generate_csr_and_key(common_name):
239235
"""Return a dict with a new csr, public key and private key."""
240236
private_key = rsa.generate_private_key(
241237
public_exponent=65537,
242-
key_size=2048,
243-
backend=default_backend())
238+
key_size=2048
239+
)
244240

245241
public_key = private_key.public_key()
246242

247243
csr = x509.CertificateSigningRequestBuilder().subject_name(x509.Name([
248244
x509.NameAttribute(x509.oid.NameOID.COMMON_NAME, common_name),
249-
])).sign(private_key, hashes.SHA256(), default_backend())
245+
])).sign(private_key, hashes.SHA256())
250246

251247
result = {
252248
'csr': csr.public_bytes(

magnum/common/x509/validator.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
from magnum.common.x509 import extensions
1919
import magnum.conf
2020

21+
try:
22+
# for cryptography >= 35.0.0
23+
from cryptography.hazmat._oid import _OID_NAMES as OID_NAMES
24+
except ImportError:
25+
from cryptography.x509.oid import _OID_NAMES as OID_NAMES
26+
2127
_CA_KEY_USAGES = [
2228
extensions.KeyUsages.KEY_CERT_SIGN.value[0],
2329
extensions.KeyUsages.CRL_SIGN.value[0]
@@ -50,7 +56,7 @@ def filter_allowed_extensions(extensions, allowed_extensions=None):
5056
allowed_extensions = allowed_extensions or []
5157

5258
for ext in extensions:
53-
ext_name = x509.oid._OID_NAMES.get(ext.oid, None)
59+
ext_name = OID_NAMES.get(ext.oid, None)
5460
if ext_name in allowed_extensions:
5561
yield ext
5662
else:

magnum/tests/unit/common/x509/test_operations.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ def setUp(self):
2525
super(TestX509Operations, self).setUp()
2626

2727
@mock.patch.object(serialization, 'NoEncryption')
28-
@mock.patch.object(operations, 'default_backend')
2928
@mock.patch.object(operations, '_load_pem_private_key')
3029
def test_decrypt_key(self, mock_load_pem_private_key,
31-
mock_default_backend, mock_no_encryption_class):
30+
mock_no_encryption_class):
3231
mock_private_key = mock.MagicMock()
3332
mock_load_pem_private_key.return_value = mock_private_key
3433
mock_private_key.private_bytes.return_value = mock.sentinel.decrypted
@@ -45,11 +44,7 @@ def test_decrypt_key(self, mock_load_pem_private_key,
4544
)
4645
self.assertEqual(mock.sentinel.decrypted, actual_decrypted)
4746

48-
@mock.patch.object(operations, 'default_backend')
49-
@mock.patch.object(rsa, 'generate_private_key')
50-
def test_generate_csr_and_key(self, mock_generate_private_key,
51-
mock_default_backend):
52-
mock_generate_private_key.return_value = mock.MagicMock()
47+
def test_generate_csr_and_key(self):
5348
csr_keys = operations.generate_csr_and_key(u"Test")
5449
self.assertIsNotNone(csr_keys)
5550
self.assertTrue("public_key" in csr_keys)

magnum/tests/unit/common/x509/test_sign.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# License for the specific language governing permissions and limitations
1313
# under the License.
1414

15-
from cryptography.hazmat.backends import default_backend
1615
from cryptography.hazmat.primitives.asymmetric import rsa
1716
from cryptography.hazmat.primitives import hashes
1817
from cryptography.hazmat.primitives import serialization
@@ -40,11 +39,10 @@ def setUp(self):
4039
def _load_pems(self, keypairs, encryption_password):
4140
private_key = serialization.load_pem_private_key(
4241
keypairs['private_key'],
43-
password=encryption_password,
44-
backend=default_backend(),
42+
password=encryption_password
4543
)
4644
certificate = c_x509.load_pem_x509_certificate(
47-
keypairs['certificate'], default_backend())
45+
keypairs['certificate'])
4846

4947
return certificate, private_key
5048

@@ -85,8 +83,7 @@ def _private_bytes(self, private_key):
8583
def _generate_private_key(self):
8684
return rsa.generate_private_key(
8785
public_exponent=65537,
88-
key_size=2048,
89-
backend=default_backend()
86+
key_size=2048
9087
)
9188

9289
def _build_csr(self, private_key):
@@ -95,7 +92,7 @@ def _build_csr(self, private_key):
9592
c_x509.NameAttribute(NameOID.COMMON_NAME, self.subject_name)
9693
]))
9794

98-
return csr.sign(private_key, hashes.SHA256(), default_backend())
95+
return csr.sign(private_key, hashes.SHA256())
9996

10097
def assertHasPublicKey(self, keypairs):
10198
key = keypairs[1]

0 commit comments

Comments
 (0)