feat(certificates): get_certificates on both drivers, with ACME (NetOrk/netork#2656)
The node's TLS certificates from GET /nodes/{node}/certificates/info (PBS:
/nodes/localhost/certificates/info) in netOrk's certificate contract,
{name, issuer, valid_from, valid_to, in_use_by} with Unix timestamps, so
netOrk's expiry warnings cover a node's web UI and API certificate.
One more call, the node's config, says whether ACME renews the certificate
the proxy serves (acme). The PEM is not returned; unreadable certificates
are [] and an unreadable config reads as no ACME.
This commit is contained in:
1 parent
c2d9d7e75e
commit
ff4ccc4818
6 files changed
+390
No files matched your search
@@ -0,0 +1,115 @@
|
||||
"""The node's TLS certificates and whether ACME renews them (NetOrk/netork#2656).
|
||||
|
||||
``get_certificates`` answers netOrk's certificate contract, the one the OPNsense
|
||||
driver set: one ``{name, issuer, valid_from, valid_to, in_use_by}`` per
|
||||
certificate, the validity as Unix timestamps. netOrk's expiry warnings read it
|
||||
from every driver that has it.
|
||||
|
||||
Both products list their certificates at ``GET /nodes/{node}/certificates/info``
|
||||
(PBS: ``/nodes/localhost/certificates/info``). Proxmox VE has three: its
|
||||
cluster's CA (``pve-root-ca.pem``), the node certificate the CA signed
|
||||
(``pve-ssl.pem``) and, where one was uploaded or ACME ordered one,
|
||||
``pveproxy-ssl.pem``, which ``pveproxy`` then serves instead. A backup server has
|
||||
one, ``proxy.pem``. ``in_use_by`` is 1 for the certificate the web UI and API
|
||||
serve, 0 for the others.
|
||||
|
||||
One call more, the node's ``config``, says whether ACME renews the served
|
||||
certificate (``acme``): a domain is configured (``acmedomain0``…, or
|
||||
``domains=`` in the older ``acme`` property). The daily update job of either
|
||||
product renews it 30 days before it runs out, so an ACME certificate close to
|
||||
its end means the renewal failed. A config that cannot be read means no ACME.
|
||||
|
||||
``name`` is the file name, as the products' own certificate lists show it;
|
||||
``issuer`` and ``subject`` are the common names. The PEM never leaves the node
|
||||
reading: netOrk keeps only what it shows.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import re
|
||||
from typing import TYPE_CHECKING, Any, ClassVar
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_JsonDict = dict[str, Any]
|
||||
|
||||
#: ``CN=x`` in either form the products print a name in: ``/O=y/CN=x`` (PVE) or
|
||||
#: ``CN = x, O = y`` (PBS).
|
||||
_COMMON_NAME = re.compile(r"(?:^|[/,])\s*CN\s*=\s*([^/,]+)")
|
||||
|
||||
|
||||
def common_name(name: str | None) -> str:
|
||||
"""The CN of a distinguished name as Proxmox prints it, else the name itself."""
|
||||
if not name:
|
||||
return ""
|
||||
found = _COMMON_NAME.search(name)
|
||||
return found.group(1).strip() if found else name.strip()
|
||||
|
||||
|
||||
def _seconds(value: Any) -> int:
|
||||
try:
|
||||
return int(value or 0)
|
||||
except (TypeError, ValueError):
|
||||
return 0
|
||||
|
||||
|
||||
def acme_configured(config: _JsonDict) -> bool:
|
||||
"""Whether the node's config names a domain for ACME to order a certificate for."""
|
||||
if any(key.startswith("acmedomain") and value for key, value in config.items()):
|
||||
return True
|
||||
return "domains=" in str(config.get("acme") or "")
|
||||
|
||||
|
||||
class ProxmoxCertificatesMixin:
|
||||
"""``get_certificates`` for a Proxmox VE node; the backup server sets its file names."""
|
||||
|
||||
#: The files the proxy serves, the first one present wins.
|
||||
served_certificates: ClassVar[tuple[str, ...]] = ("pveproxy-ssl.pem", "pve-ssl.pem")
|
||||
#: The file an ACME order writes.
|
||||
acme_certificate: ClassVar[str] = "pveproxy-ssl.pem"
|
||||
|
||||
_node_name: str
|
||||
|
||||
if TYPE_CHECKING: # pragma: no cover - the driver's (napalm_proxmox._base)
|
||||
|
||||
def _node_api(self) -> Any: ...
|
||||
|
||||
def get_certificates(self) -> list[_JsonDict]:
|
||||
"""The node's certificates in netOrk's contract shape; ``[]`` when unreadable."""
|
||||
try:
|
||||
rows = self._node_api().certificates.info.get() or []
|
||||
except Exception as exc:
|
||||
logger.warning("get_certificates() failed on %s: %s", self._node_name, exc)
|
||||
return []
|
||||
files = [row.get("filename") for row in rows]
|
||||
served = next((f for f in self.served_certificates if f in files), None)
|
||||
acme = self.acme_certificate in files and self._acme_configured()
|
||||
return [self._certificate(row, served, acme) for row in rows]
|
||||
|
||||
def _acme_configured(self) -> bool:
|
||||
try:
|
||||
return acme_configured(self._node_api().config.get() or {})
|
||||
except Exception as exc:
|
||||
logger.debug("node config unreadable on %s: %s", self._node_name, exc)
|
||||
return False
|
||||
|
||||
def _certificate(self, row: _JsonDict, served: str | None, acme: bool) -> _JsonDict:
|
||||
filename = str(row.get("filename") or "")
|
||||
return {
|
||||
"name": filename,
|
||||
"issuer": common_name(row.get("issuer")),
|
||||
"valid_from": _seconds(row.get("notbefore")),
|
||||
"valid_to": _seconds(row.get("notafter")),
|
||||
"in_use_by": 1 if filename == served else 0,
|
||||
"subject": common_name(row.get("subject")),
|
||||
"fingerprint": row.get("fingerprint") or "",
|
||||
"acme": acme and filename == self.acme_certificate,
|
||||
}
|
||||
|
||||
|
||||
class PbsCertificatesMixin(ProxmoxCertificatesMixin):
|
||||
"""A backup server has one certificate, ``proxy.pem``, which ACME renews too."""
|
||||
|
||||
served_certificates: ClassVar[tuple[str, ...]] = ("proxy.pem",)
|
||||
acme_certificate: ClassVar[str] = "proxy.pem"
|
||||
Reference in new issue
Block a user