From e23eb7498b03e9bd03019a2644c51928310a3c32 Mon Sep 17 00:00:00 2001 From: Saman Sajedi Date: Tue, 18 Aug 2026 13:14:04 +0330 Subject: [PATCH] cert config - Changed SAN default for generating server certificate - Added two new options for organizations --- mqtt-cert-manager/CHANGELOG.md | 11 +++++++++++ mqtt-cert-manager/app/ca.py | 30 +++++++++++++++++++++++------- mqtt-cert-manager/app/main.py | 11 ++++++++--- mqtt-cert-manager/config.yaml | 8 ++++++-- 4 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 mqtt-cert-manager/CHANGELOG.md diff --git a/mqtt-cert-manager/CHANGELOG.md b/mqtt-cert-manager/CHANGELOG.md new file mode 100644 index 0000000..98a8716 --- /dev/null +++ b/mqtt-cert-manager/CHANGELOG.md @@ -0,0 +1,11 @@ +# Changelog + +## 0.4.1 +- Changed SAN default for generating server certificate +- Added two new options for organizations + +## 0.4.0 +- Replaced RSA algorithm with EC for encryption. + +## 0.3.0 +- Initial release. \ No newline at end of file diff --git a/mqtt-cert-manager/app/ca.py b/mqtt-cert-manager/app/ca.py index 170b123..852d458 100644 --- a/mqtt-cert-manager/app/ca.py +++ b/mqtt-cert-manager/app/ca.py @@ -24,19 +24,35 @@ CA_VALIDITY_YEARS = 20 class CertificateAuthority: - def __init__(self): + def __init__(self, organization: str = "", organizational_unit: str = ""): + # Not hardcoded: these come from add-on options (default empty, + # meaning "omit entirely" — the subject stays CN-only, same as + # before this existed). If set, they're applied consistently to + # every cert this CA issues: the root CA itself, the broker cert, + # and every device client cert. + self.organization = organization + self.organizational_unit = organizational_unit os.makedirs(CA_DIR, exist_ok=True) if not (os.path.exists(CA_KEY_PATH) and os.path.exists(CA_CERT_PATH)): self._generate_root_ca() self._load_root_ca() + def _build_name(self, common_name: str) -> x509.Name: + attrs = [] + if self.organization: + attrs.append(x509.NameAttribute(NameOID.ORGANIZATION_NAME, self.organization)) + if self.organizational_unit: + attrs.append( + x509.NameAttribute(NameOID.ORGANIZATIONAL_UNIT_NAME, self.organizational_unit) + ) + attrs.append(x509.NameAttribute(NameOID.COMMON_NAME, common_name)) + return x509.Name(attrs) + # -- setup --------------------------------------------------------- def _generate_root_ca(self): key = ec.generate_private_key(ec.SECP256R1()) - subject = issuer = x509.Name( - [x509.NameAttribute(NameOID.COMMON_NAME, CA_COMMON_NAME)] - ) + subject = issuer = self._build_name(CA_COMMON_NAME) now = datetime.datetime.now(datetime.timezone.utc) cert = ( x509.CertificateBuilder() @@ -109,7 +125,7 @@ class CertificateAuthority: raise ValueError("CSR signature is invalid") now = datetime.datetime.now(datetime.timezone.utc) - subject = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, device_id)]) + subject = self._build_name(device_id) builder = ( x509.CertificateBuilder() @@ -159,7 +175,7 @@ class CertificateAuthority: key = ec.generate_private_key(ec.SECP256R1()) now = datetime.datetime.now(datetime.timezone.utc) - subject = x509.Name([x509.NameAttribute(NameOID.COMMON_NAME, hostnames[0])]) + subject = self._build_name(hostnames[0]) san_entries = [] for name in hostnames: @@ -261,4 +277,4 @@ class CertificateAuthority: return False return True except Exception: - return False \ No newline at end of file + return False diff --git a/mqtt-cert-manager/app/main.py b/mqtt-cert-manager/app/main.py index e3cc9c2..1b60d79 100644 --- a/mqtt-cert-manager/app/main.py +++ b/mqtt-cert-manager/app/main.py @@ -31,8 +31,10 @@ def load_options(): "mqtt_password": "", "request_topic_prefix": "cert/enroll", "cert_validity_days": 825, - "broker_hostnames": "core-mosquitto,localhost", + "broker_hostnames": "homeassistant.local,192.168.1.80", "renewal_window_days": 30, + "cert_organization": "", + "cert_organizational_unit": "", } if os.path.exists(OPTIONS_PATH): with open(OPTIONS_PATH) as f: @@ -41,7 +43,10 @@ def load_options(): options = load_options() -ca = CertificateAuthority() +ca = CertificateAuthority( + organization=options["cert_organization"], + organizational_unit=options["cert_organizational_unit"], +) app = Flask(__name__) # Guard: this server has no login of its own. It relies entirely on only @@ -60,7 +65,7 @@ def restrict_to_ingress_proxy(): def parsed_broker_hostnames(): - raw = options.get("broker_hostnames", "core-mosquitto,localhost") + raw = options.get("broker_hostnames", "homeassistant.local,192.168.1.80") return [h.strip() for h in raw.split(",") if h.strip()] diff --git a/mqtt-cert-manager/config.yaml b/mqtt-cert-manager/config.yaml index 7f7d419..6e8ba69 100644 --- a/mqtt-cert-manager/config.yaml +++ b/mqtt-cert-manager/config.yaml @@ -1,5 +1,5 @@ name: "MQTT Certificate Manager" -version: "0.4.0" +version: "0.4.1" slug: "mqtt_cert_manager" description: "Local CA that issues and manages TLS client certificates for MQTT nodes" url: "https://gitea.viratak.com/bitopa/mqtt-cert-manager" @@ -42,8 +42,10 @@ options: mqtt_password: "" request_topic_prefix: "cert/enroll" cert_validity_days: 825 - broker_hostnames: "core-mosquitto,localhost" + broker_hostnames: "homeassistant.local,192.168.1.80" renewal_window_days: 30 + cert_organization: "Viratak Telecommunications" + cert_organizational_unit: "Cloudihome" schema: mqtt_host: str @@ -54,3 +56,5 @@ schema: cert_validity_days: int(30,3650) broker_hostnames: str renewal_window_days: int(1,365) + cert_organization: str? + cert_organizational_unit: str?