Files
virarepo/mqtt-cert-manager/app/static/app.js
T
eta 9472cfff8d
build-and-push / check-branch (push) Successful in 11s
build-and-push / build (amd64, linux/amd64) (push) Successful in 10m57s
Decouple certificate CN from device_id
- ca.sign_csr's identity parameter renamed to `common_name`, fully
  decoupled from device_id.
- New resolve_common_name(): CN comes from the device's own CSR: falls
  back to device_id only if the CSR has no CN (malformed request).
- Renewal unaffected: a renewed cert always keeps the CN of the
  device's current, still-valid cert, regardless of what the renewal
  CSR claims (verified against an adversarial CSR asserting a
  different CN).
- device_id remains the sole storage key (pending/issued/revoked) and
  MQTT topic routing key, and must still be unique per physical
  device even when CN is deliberately shared across many.
- /api/pending and /api/issued now also expose the CSR-resolved CN for
  display, without changing what's actually stored.

No topic structure changes. No new MQTT payload fields.
2026-08-27 16:34:42 +03:30

144 lines
5.0 KiB
JavaScript

async function refreshPending() {
const res = await fetch("api/pending");
const data = await res.json();
const tbody = document.querySelector("#pending-table tbody");
const empty = document.getElementById("pending-empty");
tbody.innerHTML = "";
const ids = Object.keys(data);
empty.style.display = ids.length ? "none" : "block";
for (const deviceId of ids) {
const entry = data[deviceId];
const row = document.createElement("tr");
const requested = new Date(entry.requested_at * 1000).toLocaleString();
const meta = Object.entries(entry.meta || {}).map(([k, v]) => `${k}: ${v}`).join(", ");
row.innerHTML = `
<td>${deviceId}</td>
<td>${entry.resolved_common_name}</td>
<td>${requested}</td>
<td>${meta || "-"}</td>
<td>
<button class="approve" data-id="${deviceId}">Approve</button>
<button class="reject" data-id="${deviceId}">Reject</button>
</td>`;
tbody.appendChild(row);
}
tbody.querySelectorAll(".approve").forEach((btn) =>
btn.addEventListener("click", () => approve(btn.dataset.id))
);
tbody.querySelectorAll(".reject").forEach((btn) =>
btn.addEventListener("click", () => reject(btn.dataset.id))
);
}
async function refreshIssued() {
const [issuedRes, revokedRes] = await Promise.all([fetch("api/issued"), fetch("api/revoked")]);
const data = await issuedRes.json();
const revoked = await revokedRes.json();
const tbody = document.querySelector("#issued-table tbody");
const empty = document.getElementById("issued-empty");
tbody.innerHTML = "";
const ids = Object.keys(data);
empty.style.display = ids.length ? "none" : "block";
for (const deviceId of ids) {
const entry = data[deviceId];
const row = document.createElement("tr");
const issued = new Date(entry.issued_at * 1000).toLocaleString();
const expires = new Date(entry.not_after).toLocaleDateString();
const isRevoked = Object.prototype.hasOwnProperty.call(revoked, deviceId);
const action = isRevoked
? `<span class="empty">revoked</span>`
: `<button class="revoke" data-id="${deviceId}">Revoke</button>`;
row.innerHTML = `<td>${deviceId}</td><td>${entry.common_name}</td><td>${issued}</td><td>${expires}</td><td>${action}</td>`;
tbody.appendChild(row);
}
tbody.querySelectorAll(".revoke").forEach((btn) =>
btn.addEventListener("click", () => revoke(btn.dataset.id))
);
}
async function refreshRevoked() {
const res = await fetch("api/revoked");
const data = await res.json();
const tbody = document.querySelector("#revoked-table tbody");
const empty = document.getElementById("revoked-empty");
tbody.innerHTML = "";
const ids = Object.keys(data);
empty.style.display = ids.length ? "none" : "block";
for (const deviceId of ids) {
const entry = data[deviceId];
const row = document.createElement("tr");
const revokedAt = new Date(entry.revoked_at * 1000).toLocaleString();
row.innerHTML = `<td>${deviceId}</td><td>${revokedAt}</td><td>${entry.reason}</td>`;
tbody.appendChild(row);
}
}
async function approve(deviceId) {
const res = await fetch(`api/approve/${encodeURIComponent(deviceId)}`, { method: "POST" });
if (!res.ok) {
const err = await res.json();
alert(`Could not issue certificate: ${err.error}`);
}
await refresh();
}
async function reject(deviceId) {
await fetch(`api/reject/${encodeURIComponent(deviceId)}`, { method: "POST" });
await refresh();
}
async function revoke(deviceId) {
if (!confirm(`Revoke the certificate for "${deviceId}"? This can't be undone, and requires a Mosquitto restart to take effect.`)) return;
const res = await fetch(`api/revoke/${encodeURIComponent(deviceId)}`, { method: "POST" });
if (!res.ok) {
const err = await res.json();
alert(`Could not revoke: ${err.error}`);
}
await refresh();
}
async function refreshBrokerCert() {
const res = await fetch("api/broker-cert");
const data = await res.json();
const statusEl = document.getElementById("broker-cert-status");
const configEl = document.getElementById("broker-cert-config");
if (!data.info) {
statusEl.textContent = "Not yet issued.";
} else {
const expires = new Date(data.info.not_after).toLocaleDateString();
statusEl.textContent = `Covers: ${data.info.hostnames.join(", ")} — expires ${expires}`;
}
configEl.textContent =
`certfile: ${data.cert_filename}\n` +
`keyfile: ${data.key_filename}\n` +
`require_certificate: true\n` +
`cafile: ${data.ca_filename}`;
}
document.getElementById("reissue-broker-cert").addEventListener("click", async () => {
if (!confirm("Reissue the broker's TLS certificate? Mosquitto will need a restart to pick up the new files.")) return;
const res = await fetch("api/broker-cert/reissue", { method: "POST" });
if (!res.ok) {
const err = await res.json();
alert(`Could not reissue: ${err.error}`);
}
await refreshBrokerCert();
});
async function refresh() {
await refreshPending();
await refreshIssued();
await refreshRevoked();
await refreshBrokerCert();
}
refresh();
setInterval(refresh, 5000);