mirror of
https://github.com/moepman/acertmgr.git
synced 2024-06-15 07:22:34 +02:00
acertmgr: Move factories to their packages and reuse objects with same config
This commit is contained in:
parent
75f597ac36
commit
735c986f0d
|
@ -7,7 +7,6 @@
|
||||||
# available under the ISC license, see LICENSE
|
# available under the ISC license, see LICENSE
|
||||||
|
|
||||||
import grp
|
import grp
|
||||||
import importlib
|
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import pwd
|
import pwd
|
||||||
|
@ -16,35 +15,8 @@ import stat
|
||||||
import subprocess
|
import subprocess
|
||||||
|
|
||||||
from acertmgr import configuration, tools
|
from acertmgr import configuration, tools
|
||||||
|
from acertmgr.authority import authority
|
||||||
|
from acertmgr.modes import challenge_handler
|
||||||
# @brief create a authority for the given configuration
|
|
||||||
# @param settings the authority configuration options
|
|
||||||
def create_authority(settings):
|
|
||||||
acc_file = settings['account_key']
|
|
||||||
if os.path.isfile(acc_file):
|
|
||||||
print("Reading account key from {}".format(acc_file))
|
|
||||||
acc_key = tools.read_pem_file(acc_file, key=True)
|
|
||||||
else:
|
|
||||||
print("Account key not found at '{0}'. Creating key.".format(acc_file))
|
|
||||||
acc_key = tools.new_account_key(acc_file)
|
|
||||||
|
|
||||||
authority_module = importlib.import_module("acertmgr.authority.{0}".format(settings["api"]))
|
|
||||||
authority_class = getattr(authority_module, "ACMEAuthority")
|
|
||||||
return authority_class(settings, acc_key)
|
|
||||||
|
|
||||||
|
|
||||||
# @brief create a challenge handler for the given configuration
|
|
||||||
# @param settings the domain's configuration options
|
|
||||||
def create_challenge_handler(settings):
|
|
||||||
if "mode" in settings:
|
|
||||||
mode = settings["mode"]
|
|
||||||
else:
|
|
||||||
mode = "standalone"
|
|
||||||
|
|
||||||
handler_module = importlib.import_module("acertmgr.modes.{0}".format(mode))
|
|
||||||
handler_class = getattr(handler_module, "ChallengeHandler")
|
|
||||||
return handler_class(settings)
|
|
||||||
|
|
||||||
|
|
||||||
# @brief fetch new certificate from letsencrypt
|
# @brief fetch new certificate from letsencrypt
|
||||||
|
@ -52,14 +24,14 @@ def create_challenge_handler(settings):
|
||||||
def cert_get(settings):
|
def cert_get(settings):
|
||||||
print("Getting certificate for '%s'." % settings['domains'])
|
print("Getting certificate for '%s'." % settings['domains'])
|
||||||
|
|
||||||
acme = create_authority(settings['authority'])
|
acme = authority(settings['authority'])
|
||||||
acme.register_account()
|
acme.register_account()
|
||||||
|
|
||||||
# create challenge handlers for this certificate
|
# create challenge handlers for this certificate
|
||||||
challenge_handlers = dict()
|
challenge_handlers = dict()
|
||||||
for domain in settings['domainlist']:
|
for domain in settings['domainlist']:
|
||||||
# Create the challenge handler
|
# Create the challenge handler
|
||||||
challenge_handlers[domain] = create_challenge_handler(settings['handlers'][domain])
|
challenge_handlers[domain] = challenge_handler(settings['handlers'][domain])
|
||||||
|
|
||||||
# create ssl key
|
# create ssl key
|
||||||
key_file = settings['key_file']
|
key_file = settings['key_file']
|
||||||
|
@ -147,7 +119,7 @@ def cert_revoke(cert, configs, reason=None):
|
||||||
domains = set(tools.get_cert_domains(cert))
|
domains = set(tools.get_cert_domains(cert))
|
||||||
for config in configs:
|
for config in configs:
|
||||||
if domains == set(config['domainlist']):
|
if domains == set(config['domainlist']):
|
||||||
acme = create_authority(config['authority'])
|
acme = authority(config['authority'])
|
||||||
acme.register_account()
|
acme.register_account()
|
||||||
acme.revoke_crt(cert, reason)
|
acme.revoke_crt(cert, reason)
|
||||||
return
|
return
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# authority - authority api package
|
||||||
|
# Copyright (c) Rudolf Mayerhofer, 2019.
|
||||||
|
# available under the ISC license, see LICENSE
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
from acertmgr import tools
|
||||||
|
|
||||||
|
authorities = dict()
|
||||||
|
|
||||||
|
|
||||||
|
# @brief find or create a suitable authority for the given settings
|
||||||
|
# @param settings the authority configuration options
|
||||||
|
def authority(settings):
|
||||||
|
key = json.dumps(settings, sort_keys=True)
|
||||||
|
if key in authorities:
|
||||||
|
return authorities[key]
|
||||||
|
else:
|
||||||
|
acc_file = settings['account_key']
|
||||||
|
if os.path.isfile(acc_file):
|
||||||
|
print("Reading account key from {}".format(acc_file))
|
||||||
|
acc_key = tools.read_pem_file(acc_file, key=True)
|
||||||
|
else:
|
||||||
|
print("Account key not found at '{0}'. Creating key.".format(acc_file))
|
||||||
|
acc_key = tools.new_account_key(acc_file)
|
||||||
|
|
||||||
|
authority_module = importlib.import_module("acertmgr.authority.{0}".format(settings["api"]))
|
||||||
|
authority_class = getattr(authority_module, "ACMEAuthority")
|
||||||
|
authority_obj = authority_class(settings, acc_key)
|
||||||
|
authorities[key] = authority_obj
|
||||||
|
return authority_obj
|
|
@ -0,0 +1,30 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# modes - challenge handler modes package
|
||||||
|
# Copyright (c) Rudolf Mayerhofer, 2019.
|
||||||
|
# available under the ISC license, see LICENSE
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
import json
|
||||||
|
|
||||||
|
challenge_handlers = dict()
|
||||||
|
|
||||||
|
|
||||||
|
# @brief find or create a challenge handler for the given settings
|
||||||
|
# @param settings the domain's configuration options
|
||||||
|
def challenge_handler(settings):
|
||||||
|
key = json.dumps(settings, sort_keys=True)
|
||||||
|
if key in challenge_handlers:
|
||||||
|
return challenge_handlers[key]
|
||||||
|
else:
|
||||||
|
if "mode" in settings:
|
||||||
|
mode = settings["mode"]
|
||||||
|
else:
|
||||||
|
mode = "standalone"
|
||||||
|
|
||||||
|
handler_module = importlib.import_module("acertmgr.modes.{0}".format(mode))
|
||||||
|
handler_class = getattr(handler_module, "ChallengeHandler")
|
||||||
|
handler_obj = handler_class(settings)
|
||||||
|
challenge_handlers[key] = handler_obj
|
||||||
|
return handler_obj
|
Loading…
Reference in New Issue
Block a user