Merge branch 'dev-certbot_hook' into 'master'

Add certbook hook functionality

See merge request tools/inwx!1
This commit is contained in:
Christian Fraß 2023-05-29 10:04:00 +00:00
commit c05dfa3974
2 changed files with 72 additions and 18 deletions

View file

@ -8,5 +8,5 @@ import json as _json
import http.client as _http_client import http.client as _http_client
import argparse as _argparse import argparse as _argparse
import pathlib as _pathlib import pathlib as _pathlib
import time as _time

View file

@ -5,40 +5,77 @@ def args(
description = "INWX CLI Frontend" description = "INWX CLI Frontend"
) )
argumentparser.add_argument( argumentparser.add_argument(
'--conf', "-c",
"--conf",
dest = "conf", dest = "conf",
default = _os.path.join(str(_pathlib.Path.home()), ".inwx-conf.json") default = _os.path.join(str(_pathlib.Path.home()), ".inwx-conf.json"),
metavar = "<conf>",
help = "path to configuration file",
) )
argumentparser.add_argument( argumentparser.add_argument(
'--environment', "-e",
"--environment",
dest = "environment", dest = "environment",
default = None metavar = "<environment>",
default = None,
help = "environment to use; one of the keys in the 'url' filed of the configuration; overwrites the configuration value",
) )
argumentparser.add_argument( argumentparser.add_argument(
'--username', "-u",
"--username",
dest = "username", dest = "username",
default = None metavar = "<username>",
default = None,
help = "username; overwrites the configuration value",
) )
argumentparser.add_argument( argumentparser.add_argument(
'--password', "-p",
"--password",
dest = "password", dest = "password",
default = None metavar = "<password>",
default = None,
help = "password; overwrites the configuration value",
) )
''' '''
argumentparser.add_argument( argumentparser.add_argument(
'--domain', "-d",
"--domain",
dest = "domain", dest = "domain",
default = None default = None,
metavar = "<domain>",
help = "the domain to work with"
) )
''' '''
argumentparser.add_argument( argumentparser.add_argument(
"command", "-x",
type = str "--challenge-prefix",
dest = "challenge_prefix",
metavar = "<challenge-prefix>",
default = "_acme-challenge",
help = "which subdomain to use for ACME challanges",
)
argumentparser.add_argument(
"-w",
"--delay",
dest = "delay",
type = float,
default = 60.0,
metavar = "<delay>",
help = "seconds to wait at end of certbot auth hook",
)
argumentparser.add_argument(
"action",
type = str,
choices = ["info", "list", "save", "certbot-hook"],
metavar = "<action>",
help = "action to execute",
) )
argumentparser.add_argument( argumentparser.add_argument(
"parameter", "parameter",
nargs = "*", nargs = "*",
type = str type = str,
metavar = "<parameters>",
help = "action specific parameters",
) )
arguments = argumentparser.parse_args() arguments = argumentparser.parse_args()
return arguments return arguments
@ -53,14 +90,14 @@ def main(
if (not (arguments.username is None)): conf_set("account.username", arguments.username) if (not (arguments.username is None)): conf_set("account.username", arguments.username)
if (not (arguments.password is None)): conf_set("account.password", arguments.password) if (not (arguments.password is None)): conf_set("account.password", arguments.password)
if (arguments.command == "info"): if (arguments.action == "info"):
result = api_macro_info( result = api_macro_info(
conf_get("environment"), conf_get("environment"),
conf_get("account.username"), conf_get("account.username"),
conf_get("account.password") conf_get("account.password")
) )
print(_json.dumps(result, indent = "\t")) print(_json.dumps(result, indent = "\t"))
elif (arguments.command == "list"): elif (arguments.action == "list"):
domain = arguments.parameter[0] domain = arguments.parameter[0]
result = api_macro_list( result = api_macro_list(
conf_get("environment"), conf_get("environment"),
@ -69,7 +106,7 @@ def main(
domain domain
) )
print(_json.dumps(result, indent = "\t")) print(_json.dumps(result, indent = "\t"))
elif (arguments.command == "save"): elif (arguments.action == "save"):
domain = arguments.parameter[0] domain = arguments.parameter[0]
name = arguments.parameter[1] name = arguments.parameter[1]
type_ = arguments.parameter[2] type_ = arguments.parameter[2]
@ -84,8 +121,25 @@ def main(
content content
) )
# print(_json.dumps(result, indent = "\t")) # print(_json.dumps(result, indent = "\t"))
elif (arguments.action == "certbot-hook"):
domain_full_parts = _os.environ["CERTBOT_DOMAIN"].split(".", 1)
domain = domain_full_parts[1]
name = (arguments.challenge_prefix + "." + domain_full_parts[0])
type_ = "TXT"
content = _os.environ["CERTBOT_VALIDATION"]
api_macro_save(
conf_get("environment"),
conf_get("account.username"),
conf_get("account.password"),
domain,
name,
type_,
content
)
_time.sleep(arguments.delay)
# print(_json.dumps(result, indent = "\t"))
else: else:
log("unhandled command '%s'" % (arguments.command, )) log("unhandled action '%s'" % (arguments.action, ))
try: try: