Skip to content

Commit

Permalink
Merge pull request scrapy#4761 from Gallaecio/on-the-fly-certificates
Browse files Browse the repository at this point in the history
Generate localhost keys for tests on the fly
  • Loading branch information
wRAR authored Sep 2, 2020
2 parents 307e35c + dd378b4 commit c1cc3f2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ htmlcov/
.coverage.*
.cache/
.mypy_cache/
/tests/keys/localhost.crt
/tests/keys/localhost.key

# Windows
Thumbs.db
6 changes: 6 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import pytest

from tests.keys import generate_keys


def _py_files(folder):
return (str(p) for p in Path(folder).rglob('*.py'))
Expand Down Expand Up @@ -51,3 +53,7 @@ def reactor_pytest(request):
def only_asyncio(request, reactor_pytest):
if request.node.get_closest_marker('only_asyncio') and reactor_pytest != 'asyncio':
pytest.skip('This test is only run with --reactor=asyncio')


# Generate localhost certificate files, needed by some tests
generate_keys()
63 changes: 63 additions & 0 deletions tests/keys/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
from datetime import datetime, timedelta

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from cryptography.hazmat.primitives.hashes import SHA256
from cryptography.hazmat.primitives.serialization import (
Encoding,
NoEncryption,
PrivateFormat,
)
from cryptography.x509 import (
CertificateBuilder,
DNSName,
Name,
NameAttribute,
random_serial_number,
SubjectAlternativeName,
)
from cryptography.x509.oid import NameOID


# https://cryptography.io/en/latest/x509/tutorial/#creating-a-self-signed-certificate
def generate_keys():
folder = os.path.dirname(__file__)

key = rsa.generate_private_key(
public_exponent=65537,
key_size=2048,
backend=default_backend(),
)
with open(os.path.join(folder, 'localhost.key'), "wb") as f:
f.write(
key.private_bytes(
encoding=Encoding.PEM,
format=PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=NoEncryption(),
)
)

subject = issuer = Name(
[
NameAttribute(NameOID.COUNTRY_NAME, u"IE"),
NameAttribute(NameOID.ORGANIZATION_NAME, u"Scrapy"),
NameAttribute(NameOID.COMMON_NAME, u"localhost"),
]
)
cert = (
CertificateBuilder()
.subject_name(subject)
.issuer_name(issuer)
.public_key(key.public_key())
.serial_number(random_serial_number())
.not_valid_before(datetime.utcnow())
.not_valid_after(datetime.utcnow() + timedelta(days=10))
.add_extension(
SubjectAlternativeName([DNSName(u"localhost")]),
critical=False,
)
.sign(key, SHA256(), default_backend())
)
with open(os.path.join(folder, 'localhost.crt'), "wb") as f:
f.write(cert.public_bytes(Encoding.PEM))
20 changes: 0 additions & 20 deletions tests/keys/localhost.crt

This file was deleted.

28 changes: 0 additions & 28 deletions tests/keys/localhost.key

This file was deleted.

0 comments on commit c1cc3f2

Please sign in to comment.