Skip to content

Commit

Permalink
feat: 证书操作
Browse files Browse the repository at this point in the history
  • Loading branch information
SALTWOOD committed Dec 20, 2024
1 parent 1e4e5c3 commit d0dd9f9
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/certificate-manager/ACME.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,8 @@ export class ACME {

return [key, csr, certificate, validFrom, expiresAt];
}

public revokeCertificate(certificate: acme.CertificateBuffer | acme.CertificateString) {
this.client.revokeCertificate(certificate);
}
}
44 changes: 44 additions & 0 deletions src/routes/ApiAdmin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Config } from "../Config.js";
import { CertificateObject } from "../database/Certificate.js";
import { ClusterEntity } from "../database/Cluster.js";
import { UserEntity } from "../database/User.js";
import JwtHelper from "../JwtHelper.js";
Expand Down Expand Up @@ -91,5 +92,48 @@ export class ApiAdmin {
});
res.json(result);
});

inst.app.post("/api/admin/certificates/revoke", async (req, res) => {
if (!Utilities.verifyAdmin(req, res, inst.db)) return;
const data = req.body as {
id: string
};
const cluster = inst.db.getEntity<ClusterEntity>(ClusterEntity, data.id);
if (!cluster) {
res.status(404).json({ message: "Cluster not found" });
return;
}

const cert = inst.db.getEntity<CertificateObject>(CertificateObject, cluster.clusterId);
if (!cert) {
res.status(404).json({ message: "Certificate not found" });
return;
}

await inst.acme?.revokeCertificate(cert.certificate);
if (inst.acme) inst.db.remove(CertificateObject, cert);

res.status(200).json({ message: "Certificate revoked" });
});

inst.app.get("/api/admin/certificates/cluster", async (req, res) => {
if (!Utilities.verifyAdmin(req, res, inst.db)) return;
const data = req.query as {
id: string
};
const cluster = inst.db.getEntity<ClusterEntity>(ClusterEntity, data.id);
if (!cluster) {
res.status(404).json({ message: "Cluster not found" });
return;
}

const cert = inst.db.getEntity<CertificateObject>(CertificateObject, cluster.clusterId);
if (!cert) {
res.status(404).json({ message: "Certificate not found" });
return;
}

res.status(200).json(cert);
});
}
}

0 comments on commit d0dd9f9

Please sign in to comment.