EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
agentCert.js
Go to the documentation of this file.
1
2/**
3 * @file agentCert.js
4 * @module routes/agentCert
5 * @description Agent certificate management endpoints for TLS client authentication.
6 * Handles certificate retrieval and CSR (Certificate Signing Request) processing for RMM agents.
7 * @requires express
8 * @requires crypto
9 * @requires services/db
10 * @author RMM-PSA Development Team
11 * @copyright 2026 RMM-PSA Platform
12 * @license Proprietary
13 */
14
15/**
16 * @apiDefine AgentCert Agent Certificates
17 * TLS certificate management for agent authentication
18 */
19
20const express = require('express');
21const router = express.Router();
22const crypto = require('crypto');
23const pool = require('../services/db');
24
25
26/**
27 * @api {get} /api/agent/:id/cert Get agent certificate
28 * @apiName GetAgentCert
29 * @apiGroup AgentCert
30 * @apiDescription Retrieve TLS certificate and keys for specified agent.
31 * Returns both certificate and privateKey (demo implementation echoes public_key).
32 * Used by agents for establishing secure communication channels.
33 * @apiParam {string} id Agent UUID
34 * @apiSuccess {string} certificate Agent TLS certificate
35 * @apiSuccess {string} privateKey Agent private key (demo: echoes public_key)
36 * @apiError (404) {String} error="Agent not found" Agent UUID not in database
37 * @apiError (500) {String} error="Failed to fetch agent cert" Database or crypto error
38 * @apiExample {curl} Example:
39 * curl -X GET http://localhost:3000/api/agent/abc-123-uuid/cert
40 * @apiSuccessExample {json} Success-Response:
41 * HTTP/1.1 200 OK
42 * {
43 * "certificate": "-----BEGIN CERTIFICATE-----\nMIIC...",
44 * "privateKey": "-----BEGIN RSA PRIVATE KEY-----\nMIIE..."
45 * }
46 */
47router.get('/:id/cert', async (req, res) => {
48 const { id } = req.params;
49 try {
50 const result = await pool.query(
51 'SELECT public_key FROM agents WHERE agent_uuid = $1 LIMIT 1',
52 [id]
53 );
54 if (result.rows.length === 0) {
55 return res.status(404).json({ error: 'Agent not found' });
56 }
57 // For demo, echo public_key as both certificate and privateKey
58 res.json({
59 certificate: result.rows[0].public_key,
60 privateKey: result.rows[0].public_key
61 });
62 } catch (err) {
63 console.error('Error fetching agent cert:', err);
64 res.status(500).json({ error: 'Failed to fetch agent cert' });
65 }
66});
67
68/**
69 * @api {post} /api/agent/request-cert Request certificate
70 * @apiName RequestAgentCert
71 * @apiGroup AgentCert
72 * @apiDescription Agent submits Certificate Signing Request (CSR) to obtain signed certificate.
73 * Stores CSR in database and returns certificate (demo: echoes CSR, production: use CA).
74 * Enables agent TLS authentication and secure communications.
75 * @apiParam {string} agent_uuid Agent UUID requesting certificate
76 * @apiParam {string} csr Certificate Signing Request (PEM format)
77 * @apiSuccess {string} certificate Signed certificate (demo: echoes CSR)
78 * @apiError (400) {String} error="agent_uuid and csr required" Missing required parameters
79 * @apiError (500) {String} error="Failed to process cert request" Database or signing error
80 * @apiExample {curl} Example:
81 * curl -X POST http://localhost:3000/api/agent/request-cert \\
82 * -H "Content-Type: application/json" \\
83 * -d '{
84 * "agent_uuid": "abc-123-uuid",
85 * "csr": "-----BEGIN CERTIFICATE REQUEST-----\\nMIIC...\\n-----END CERTIFICATE REQUEST-----"
86 * }'
87 * @apiSuccessExample {json} Success-Response:
88 * HTTP/1.1 200 OK
89 * {
90 * "certificate": "-----BEGIN CERTIFICATE-----\nMIIC...\n-----END CERTIFICATE-----"
91 * }
92 */
93router.post('/request-cert', async (req, res) => {
94 const { agent_uuid, csr } = req.body;
95 if (!agent_uuid || !csr) {
96 return res.status(400).json({ error: 'agent_uuid and csr required' });
97 }
98 try {
99 // For demo: self-sign the CSR (in production, use a CA)
100 // Here, just echo back the CSR as the "certificate"
101 // TODO: Actually sign the CSR and store the public key
102 await pool.query(
103 `UPDATE agents SET public_key = $1 WHERE agent_uuid = $2`,
104 [csr, agent_uuid]
105 );
106 res.json({ certificate: csr });
107 } catch (err) {
108 console.error('Error processing cert request:', err);
109 res.status(500).json({ error: 'Failed to process cert request' });
110 }
111});
112
113module.exports = router;