Skip to content

Commit

Permalink
Move create_tls_cert function into locust.test.util
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed May 4, 2020
1 parent 5d7abf4 commit f3f418f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
35 changes: 2 additions & 33 deletions locust/test/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from locust.web import WebUI

from .testcases import LocustTestCase
from .util import create_tls_cert


class TestWebUI(LocustTestCase):
Expand Down Expand Up @@ -311,41 +312,9 @@ def test_index_with_basic_auth_enabled_blank_credentials(self):


class TestWebUIWithTLS(LocustTestCase):

def _create_tls_cert(self):
""" Generate a TLS cert and private key to serve over https """
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa

key = rsa.generate_private_key(public_exponent=2**16+1, key_size=2048, backend=default_backend())
name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, "127.0.0.1")])
now = datetime.utcnow()
cert = (
x509.CertificateBuilder()
.subject_name(name)
.issuer_name(name)
.public_key(key.public_key())
.serial_number(1000)
.not_valid_before(now)
.not_valid_after(now + timedelta(days=10*365))
.sign(key, hashes.SHA256(), default_backend())
)
cert_pem = cert.public_bytes(encoding=serialization.Encoding.PEM)
key_pem = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)

return cert_pem, key_pem

def setUp(self):
super(TestWebUIWithTLS, self).setUp()
tls_cert, tls_key = self._create_tls_cert()
tls_cert, tls_key = create_tls_cert("127.0.0.1")
self.tls_cert_file = NamedTemporaryFile(delete=False)
self.tls_key_file = NamedTemporaryFile(delete=False)
with open(self.tls_cert_file.name, 'w') as f:
Expand Down
33 changes: 33 additions & 0 deletions locust/test/util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import os
import socket

from datetime import datetime, timedelta
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa

from contextlib import contextmanager
from tempfile import NamedTemporaryFile

Expand All @@ -25,3 +33,28 @@ def get_free_tcp_port():
port = s.getsockname()[1]
s.close()
return port


def create_tls_cert(hostname):
""" Generate a TLS cert and private key to serve over https """
key = rsa.generate_private_key(public_exponent=2**16+1, key_size=2048, backend=default_backend())
name = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostname)])
now = datetime.utcnow()
cert = (
x509.CertificateBuilder()
.subject_name(name)
.issuer_name(name)
.public_key(key.public_key())
.serial_number(1000)
.not_valid_before(now)
.not_valid_after(now + timedelta(days=10*365))
.sign(key, hashes.SHA256(), default_backend())
)
cert_pem = cert.public_bytes(encoding=serialization.Encoding.PEM)
key_pem = key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption(),
)

return cert_pem, key_pem

0 comments on commit f3f418f

Please sign in to comment.