EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
config.js
Go to the documentation of this file.
1/**
2 * @file routes/config.js
3 * @module routes/config
4 * @description RMM agent configuration API. Provides centralized configuration endpoint that
5 * agents query to retrieve policy settings, enabled plugins, and tenant contract details.
6 * Used by agents during initialization and periodic configuration refresh.
7 *
8 * **Configuration Components:**
9 * - Policy: Heartbeat interval, logging level, monitoring thresholds
10 * - Plugins: List of enabled plugins for this agent
11 * - Contract: Tenant's service contract details and limits
12 *
13 * **Agent Configuration Workflow:**
14 * 1. Agent startup/initialization
15 * 2. Query GET /config/{agentId}
16 * 3. Receive policy, plugins, contract settings
17 * 4. Apply configuration locally
18 * 5. Periodic refresh (e.g., every 1 hour)
19 *
20 * **Security:**
21 * No authentication required (public endpoint for agent communication).
22 * Agent identity validated via agentId lookup. Returns 404 if agent not found.
23 *
24 * **Related Models:**
25 * - agents table (agent_id, policy_id, tenant_id)
26 * - policies table (heartbeat, logging)
27 * - plugins table (plugin permissions per agent)
28 * - contracts table (service limits and billing)
29 * @requires express
30 * @requires ../models/Agent
31 * @requires ../models/Policies
32 * @requires ../models/Plugins
33 * @requires ../models/Contracts
34 * @author IBG MSP Development Team
35 * @date 2024-03-11
36 * @since 1.0.0
37 * @see {@link module:routes/agent} for agent registration and management
38 * @see {@link module:routes/plugins} for plugin management
39 */
40
41const express = require("express");
42const router = express.Router();
43
44const Agents = require("../models/Agent");
45const Policies = require("../models/Policies");
46const Plugins = require("../models/Plugins");
47const Contracts = require("../models/Contracts");
48
49/**
50 * @api {get} /config/:agentId Get Agent Configuration
51 * @apiName GetAgentConfig
52 * @apiGroup Agent Configuration
53 * @apiDescription Retrieves complete configuration for an RMM agent including policy settings,
54 * enabled plugins, and tenant contract details. Called by agents during initialization and
55 * periodic refresh cycles. Public endpoint (no authentication) for agent communication.
56 *
57 * **Configuration Returned:**
58 * - heartbeat: Heartbeat interval in seconds (how often agent should report status)
59 * - logging: Logging level (debug, info, warning, error)
60 * - plugins: Array of enabled plugin configurations for this agent
61 * - contracts: Tenant's service contract with resource limits and billing info
62 *
63 * **Agent Initialization Flow:**
64 * 1. Agent installed on device
65 * 2. Agent queries /config/{agentId} on first startup
66 * 3. Agent applies settings (heartbeat, logging, plugins)
67 * 4. Agent starts normal operation with these settings
68 * 5. Periodically re-queries for configuration updates
69 *
70 * **Policy Settings:**
71 * - heartbeat: Seconds between heartbeat transmissions (default: 300 = 5 minutes)
72 * - logging: Level of detail for agent logs sent to platform
73 *
74 * **Plugin Configuration:**
75 * Array of plugin objects with:
76 * - plugin_id: Unique plugin identifier
77 * - enabled: Whether plugin is active
78 * - config: Plugin-specific configuration JSON
79 *
80 * **Contract Information:**
81 * Tenant's service contract including:
82 * - contract_id: Contract identifier
83 * - max_devices: Maximum allowed devices
84 * - billing_frequency: Monthly, quarterly, annually
85 * - resource_limits: CPU/memory/disk monitoring thresholds
86 *
87 * **Error Handling:**
88 * Returns 404 if agent not found (invalid agentId or deleted agent).
89 * Returns 500 for database/model errors.
90 * @apiParam {string} agentId Agent UUID or ID (in URL path)
91 * @apiSuccess {number} heartbeat Heartbeat interval in seconds
92 * @apiSuccess {string} logging Logging level (debug, info, warning, error)
93 * @apiSuccess {object[]} plugins Array of enabled plugin configurations
94 * @apiSuccess {object} contracts Tenant's service contract details
95 * @apiError {number} 404 Agent not found (invalid agentId)
96 * @apiError {number} 500 Configuration error (database/model failure)
97 * @apiExample {curl} Example Request:
98 * curl -X GET \
99 * "https://api.everydaytech.au/config/550e8400-e29b-41d4-a716-446655440000"
100 * @apiExample {json} Success Response:
101 * HTTP/1.1 200 OK
102 * {
103 * "heartbeat": 300,
104 * "logging": "info",
105 * "plugins": [
106 * {
107 * "plugin_id": "monitoring",
108 * "enabled": true,
109 * "config": {"cpu_threshold": 90, "memory_threshold": 85}
110 * },
111 * {
112 * "plugin_id": "patching",
113 * "enabled": true,
114 * "config": {"auto_install": false, "reboot_allowed": false}
115 * }
116 * ],
117 * "contracts": {
118 * "contract_id": 42,
119 * "max_devices": 500,
120 * "billing_frequency": "monthly",
121 * "resource_limits": {"cpu": 90, "memory": 85, "disk": 90}
122 * }
123 * }
124 * @since 1.0.0
125 * @see {@link module:routes/agent} for agent registration
126 * @see {@link module:models/Policies} for policy management
127 */
128router.get("/config/:agentId", async (req, res) => {
129 try {
130 const agent = await Agents.getAgent(req.params.agentId);
131 if (!agent) return res.status(404).json({ error: "Agent not found" });
132 const policy = await Policies.getPolicy(agent.policy_id);
133 const plugins = await Plugins.getAllowedPlugins(agent.agent_id);
134 const contract = await Contracts.getContractForTenant(agent.tenant_id);
135
136 res.json({
137 heartbeat: policy.heartbeat,
138 logging: policy.logging,
139 plugins,
140 contracts: contract
141 });
142
143 } catch (err) {
144 res.status(500).json({ error: "CONFIG_ERROR" });
145 }
146});
147
148module.exports = router;