Debian Codegraph @

ansible_collections/community/proxmox/plugins/module_utils/proxmox_acme_certificate.py

raw
1 #
2 # Copyright (c) 2026, Clément Cruau (@PendaGTP) <[email protected]>
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 """Shared helpers for Proxmox ACME certificate modules."""
7
8 _ACME_DOMAIN_SLOTS = 6 # acmedomain0 .. acmedomain5
9
10
11 def parse_acme_config(node_config):
12 """Parse a node config dict into structured ACME settings.
13
14 Returns a dict with ``account`` (str or None) and ``domains`` (list of dicts
15 with keys ``domain``, ``plugin``, ``alias``).
16 """
17 account = None
18 domains = []
19
20 acme_raw = node_config.get("acme")
21 if acme_raw:
22 for part in acme_raw.split(","):
23 kv = part.strip().split("=", 1)
24 if len(kv) == 2 and kv[0].strip() == "account": # noqa: PLR2004
25 account = kv[1].strip()
26
27 for i in range(_ACME_DOMAIN_SLOTS):
28 key = f"acmedomain{i}"
29 raw = node_config.get(key)
30 if not raw:
31 continue
32 domain_entry = _parse_acmedomain_string(raw)
33 if domain_entry:
34 domains.append(domain_entry)
35
36 return {"account": account, "domains": domains}
37
38
39 def _parse_acmedomain_string(value):
40 """Parse a single ``acmedomainN`` property string into a dict."""
41 result = {"domain": None, "plugin": None, "alias": None}
42 for part in value.split(","):
43 kv = part.strip().split("=", 1)
44 if len(kv) != 2: # noqa: PLR2004
45 continue
46 key, val = kv[0].strip(), kv[1].strip()
47 if key in result:
48 result[key] = val
49 return result if result["domain"] else None
50
51
52 def cert_info_to_ansible_result(cert):
53 """Transform a single certificate info dict from the Proxmox API into Ansible return values."""
54 san = cert.get("san") or []
55 if isinstance(san, str):
56 san = [s.strip() for s in san.split(",") if s.strip()]
57
58 fingerprint = cert.get("fingerprint", "")
59 not_before = cert.get("notbefore", "")
60 not_after = cert.get("notafter", "")
61
62 return {
63 "certificate": cert.get("pem") or "",
64 "fingerprint": fingerprint,
65 "issuer": cert.get("issuer") or "",
66 "subject": cert.get("subject") or "",
67 "not_before": int(not_before),
68 "not_after": int(not_after),
69 "subject_alternative_names": san,
70 }