Coverage for jbank/management/commands/make_x509.py: 0%

# Coverage for jbank/management/commands/make_x509.py : 0%

## 29 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import logging

2 from datetime import datetime, timedelta

3 from cryptography import x509

4 from cryptography.hazmat.backends import default_backend

5 from cryptography.hazmat.primitives import serialization, hashes

6 from cryptography.hazmat.primitives.asymmetric import rsa

7 from django.core.management.base import CommandParser

8 from jutil.command import SafeCommand

9

10

11 logger = logging.getLogger(__name__)

12

13

14 class Command(SafeCommand):

15 help = """

16 Generates RSA private key and x509 certificate in .pem format (for testing)

17 """

18

19 def add_arguments(self, parser: CommandParser):

20 parser.add_argument("--key-file", type=str, default="key.pem")

21 parser.add_argument("--cert-file", type=str, default="cert.pem")

22 parser.add_argument("--country", type=str, default="US")

23 parser.add_argument("--state", type=str, default="TX")

24 parser.add_argument("--locality", type=str, default="Dallas")

25 parser.add_argument("--org-name", type=str, default="Kajala Group")

26 parser.add_argument("--common-name", type=str, default="kajala.com")

27

28 def do(self, *args, **options):

29 # Generate our key

30 key = rsa.generate_private_key(public_exponent=65537, key_size=2048, backend=default_backend())

31

32 # Write to disk unencrypted

33 with open(options["key_file"], "wb") as f:

34 f.write(

35 key.private_bytes(

36 encoding=serialization.Encoding.PEM,

37 format=serialization.PrivateFormat.TraditionalOpenSSL,

38 encryption_algorithm=serialization.NoEncryption(),

39 )

40 )

41 print("{} written".format(f.name))

42

43 # Various details about who we are. For a self-signed certificate the

44 # subject and issuer are always the same.

45 subject = issuer = x509.Name(

46 [

47 x509.NameAttribute(x509.NameOID.COUNTRY_NAME, options["country"]),

48 x509.NameAttribute(x509.NameOID.STATE_OR_PROVINCE_NAME, options["state"]),

49 x509.NameAttribute(x509.NameOID.LOCALITY_NAME, options["locality"]),

50 x509.NameAttribute(x509.NameOID.ORGANIZATION_NAME, options["org_name"]),

51 x509.NameAttribute(x509.NameOID.COMMON_NAME, options["common_name"]),

52 ]

53 )

54 cert = (

55 x509.CertificateBuilder()

56 .subject_name(subject)

57 .issuer_name(issuer)

58 .public_key(key.public_key())

59 .serial_number(x509.random_serial_number())

60 .not_valid_before(datetime.utcnow())

61 .not_valid_after(

62 # Our certificate will be valid for 10 days

63 datetime.utcnow()

64 + timedelta(days=10)

65 )

66 .add_extension(

67 x509.SubjectAlternativeName([x509.DNSName("localhost")]),

68 critical=False,

69 )

70 .sign(key, hashes.SHA256(), default_backend())

71 )

72

73 # Write our certificate out to disk.

74 with open(options["cert_file"], "wb") as f:

75 f.write(cert.public_bytes(serialization.Encoding.PEM))

76 print("{} written".format(f.name))