Skip to content

Commit a475a7e

Browse files
committed
Upgrade cryptography v46.0.7 -> v49.0.0
Signed-off-by: Albert Callarisa <albert@diagrid.io>
1 parent 436d127 commit a475a7e

3 files changed

Lines changed: 88 additions & 91 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ dev = [
108108
"opentelemetry-instrumentation-grpc~=0.61b0",
109109
"opentelemetry-exporter-zipkin~=1.11.1",
110110
"httpx~=0.28.1",
111-
"pyOpenSSL~=26.0.0",
112-
"cryptography>=42.0.0",
111+
"cryptography>=49.0.0",
113112
"redis>=7.4.0",
114113
"Flask~=3.1.3",
115114
"pytest~=9.0.2",

tests/clients/certs.py

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,56 @@
11
import os
22
import ssl
3+
from datetime import datetime, timedelta, timezone
34

45
import grpc
5-
from OpenSSL import crypto
6+
from cryptography import x509
7+
from cryptography.hazmat.primitives import hashes, serialization
8+
from cryptography.hazmat.primitives.asymmetric import rsa
9+
from cryptography.x509.oid import NameOID
610

711

812
class Certs:
913
server_type = 'grpc'
1014

1115
@classmethod
1216
def create_certificates(cls):
13-
# create a key pair
14-
k = crypto.PKey()
15-
k.generate_key(crypto.TYPE_RSA, 4096)
16-
17-
# create a self-signed cert
18-
cert = crypto.X509()
19-
cert.get_subject().organizationName = 'Dapr'
20-
cert.get_subject().commonName = 'localhost'
21-
cert.gmtime_adj_notBefore(0)
22-
cert.gmtime_adj_notAfter(24 * 60 * 60)
23-
cert.set_issuer(cert.get_subject())
24-
cert.set_pubkey(k)
17+
key = rsa.generate_private_key(public_exponent=65537, key_size=4096)
18+
19+
subject = x509.Name(
20+
[
21+
x509.NameAttribute(NameOID.ORGANIZATION_NAME, 'Dapr'),
22+
x509.NameAttribute(NameOID.COMMON_NAME, 'localhost'),
23+
]
24+
)
25+
now = datetime.now(timezone.utc)
26+
cert_builder = (
27+
x509.CertificateBuilder()
28+
.subject_name(subject)
29+
.issuer_name(subject)
30+
.public_key(key.public_key())
31+
.serial_number(x509.random_serial_number())
32+
.not_valid_before(now)
33+
.not_valid_after(now + timedelta(days=1))
34+
)
2535

2636
if cls.server_type == 'http':
27-
cert.add_extensions([crypto.X509Extension(b'subjectAltName', False, b'DNS:localhost')])
37+
localhost_san = x509.SubjectAlternativeName([x509.DNSName('localhost')])
38+
cert_builder = cert_builder.add_extension(localhost_san, critical=False)
2839

29-
cert.sign(k, 'sha512')
40+
cert = cert_builder.sign(key, hashes.SHA512())
3041

31-
with open(cls.get_cert_path(), 'wt') as f_cert:
32-
f_cert.write(crypto.dump_certificate(crypto.FILETYPE_PEM, cert).decode('utf-8'))
42+
cert_pem = cert.public_bytes(serialization.Encoding.PEM)
43+
key_pem = key.private_bytes(
44+
encoding=serialization.Encoding.PEM,
45+
format=serialization.PrivateFormat.PKCS8,
46+
encryption_algorithm=serialization.NoEncryption(),
47+
)
3348

34-
with open(cls.get_pk_path(), 'wt') as f_key:
35-
f_key.write(crypto.dump_privatekey(crypto.FILETYPE_PEM, k).decode('utf-8'))
49+
with open(cls.get_cert_path(), 'wb') as f_cert:
50+
f_cert.write(cert_pem)
51+
52+
with open(cls.get_pk_path(), 'wb') as f_key:
53+
f_key.write(key_pem)
3654

3755
@classmethod
3856
def get_pk_path(cls):

0 commit comments

Comments
 (0)