Files
dune-dashboard/app/utils/ssl.py
T

62 lines
2.0 KiB
Python

"""Generate self-signed SSL certificate for HTTPS"""
import os
import datetime
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives import serialization
def generate_cert(cert_path, key_path):
"""Generate a self-signed certificate."""
# Generate private key
key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
)
# Generate subject/issuer
subject = issuer = x509.Name([
x509.NameAttribute(NameOID.COUNTRY_NAME, "US"),
x509.NameAttribute(NameOID.STATE_OR_PROVINCE_NAME, "Remote"),
x509.NameAttribute(NameOID.LOCALITY_NAME, "Local"),
x509.NameAttribute(NameOID.ORGANIZATION_NAME, "Dune Dashboard"),
x509.NameAttribute(NameOID.COMMON_NAME, "localhost"),
])
# Build cert
cert = (
x509.CertificateBuilder()
.subject_name(subject)
.issuer_name(issuer)
.public_key(key.public_key())
.serial_number(x509.random_serial_number())
.not_valid_before(datetime.datetime.utcnow())
.not_valid_after(datetime.datetime.utcnow() + datetime.timedelta(days=3650))
.sign(key, hashes.SHA256())
)
# Write cert
with open(cert_path, "wb") as f:
f.write(cert.public_bytes(serialization.Encoding.PEM))
# Write key
with open(key_path, "wb") as f:
f.write(
key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)
)
print(f"SSL certificate generated: {cert_path}")
print(f"SSL key generated: {key_path}")
if __name__ == "__main__":
base_dir = os.path.dirname(os.path.abspath(__file__))
cert_path = os.path.join(base_dir, "cert.pem")
key_path = os.path.join(base_dir, "key.pem")
generate_cert(cert_path, key_path)