]> git.siccegge.de Git - tooling/letool.git/blob - bin/newhost
Change everything
[tooling/letool.git] / bin / newhost
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (C) Christoph Egger <christoph@christoph-egger.org>
4
5 from __future__ import print_function
6
7 import os.path
8 import logging
9
10 from acme import client
11 from acme import jose
12 from acme import messages
13
14 from cryptography.hazmat.backends import default_backend
15 from cryptography.hazmat.primitives.asymmetric import rsa
16 from cryptography.hazmat.primitives import serialization
17
18 from sicceggetools.acme import constants
19
20 logging.basicConfig()
21 logging.getLogger().setLevel(logging.INFO)
22
23 if not os.path.exists("data"):
24 logging.info("Creating data directory")
25 os.mkdir("data")
26 os.chmod("data", 0700)
27
28
29 if not os.path.exists("data/account.key.pem"):
30 logging.info("Creating account key")
31 private_key = rsa.generate_private_key(
32 public_exponent=65537,
33 key_size=constants.KEY_SIZE,
34 backend=default_backend()
35 )
36
37 pem = private_key.private_bytes(
38 encoding=serialization.Encoding.PEM,
39 format=serialization.PrivateFormat.TraditionalOpenSSL,
40 encryption_algorithm=serialization.NoEncryption()
41 )
42
43 with open("data/account.key.pem", "wb") as keyfd:
44 keyfd.write(pem)
45 else:
46 logging.info("Loading account key")
47 with open("data/account.key.pem", "rb") as keyfd:
48 private_key = serialization.load_pem_private_key(
49 keyfd.read(),
50 password=None,
51 backend=default_backend()
52 )
53
54 if not os.path.exists("data/registration.json"):
55 logging.info("registering")
56 acmeclient = client.Client(constants.DIRECTORY_URL, jose.JWKRSA(key=private_key))
57 registration = messages.NewRegistration(contact=constants.CONTACT)
58 registration = acmeclient.register(registration)
59 registration = acmeclient.agree_to_tos(registration)
60
61 with open("data/registration.json", "wb") as regfd:
62 regfd.write(registration.json_dumps_pretty())
63