Security / JS Security (npm audit) (push) Failing after 16s
Lint / JS (eslint) (push) Successful in 18s
Lint / Notify on failure (push) Skipped
Lint / Deploy (push) Skipped
Test / JS Tests (jest) (push) Successful in 15s
Lint / JS (eslint) (pull_request) Successful in 13s
Lint / Notify on failure (pull_request) Skipped
Lint / Deploy (pull_request) Skipped
Security / JS Security (npm audit) (pull_request) Failing after 17s
Test / JS Tests (jest) (pull_request) Successful in 15s
Any client that could reach :8080 got a WebSocket connection. Every connection started as a browser client and received every broadcast. worker_connect accepted a missing api_key, so anyone could register under an existing worker's name and receive its commands. command_result was accepted from any connection, so execution logs could be forged. - Classify each upgrade in verifyClient (lib/wsauth.js). A request is: - browser: a trusted-proxy Authelia identity in an allowed group; - worker: Authorization: Bearer <WORKER_API_KEY>; - pending: neither, allowed only while WORKER_AUTH_ENFORCE is off; - rejected (401): everything else, and always for a wrong key. - Only browser connections receive broadcasts. A pending connection may send only worker_connect and is closed after 30 s if it doesn't identify. - worker_connect requires the key. A keyless worker is allowed with a warning until WORKER_AUTH_ENFORCE=1, so existing workers keep working until they are updated. - Record which worker each command was sent to. command_result is accepted only from that worker (matched by DB id, so a reconnect mid-command still works) and only once. pong no longer trusts a client-supplied id. - Worker, heartbeat and Gandalf keys use a constant-time compare. Log an error at startup if WORKER_API_KEY is unset. - The worker sends its key at the handshake and in worker_connect. Closes #38 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
261 lines
7.0 KiB
JavaScript
261 lines
7.0 KiB
JavaScript
const axios = require('axios');
|
|
const WebSocket = require('ws');
|
|
const { exec } = require('child_process');
|
|
const { promisify } = require('util');
|
|
const os = require('os');
|
|
const crypto = require('crypto');
|
|
require('dotenv').config();
|
|
|
|
const execAsync = promisify(exec);
|
|
|
|
class PulseWorker {
|
|
constructor() {
|
|
this.workerId = crypto.randomUUID();
|
|
this.workerName = process.env.WORKER_NAME || os.hostname();
|
|
this.serverUrl = process.env.PULSE_SERVER || 'http://localhost:8080';
|
|
this.wsUrl = process.env.PULSE_WS || 'ws://localhost:8080';
|
|
this.apiKey = process.env.WORKER_API_KEY;
|
|
this.heartbeatInterval = parseInt(process.env.HEARTBEAT_INTERVAL || '30') * 1000;
|
|
this.maxConcurrentTasks = parseInt(process.env.MAX_CONCURRENT_TASKS || '5');
|
|
this.activeTasks = 0;
|
|
this.ws = null;
|
|
this.heartbeatTimer = null;
|
|
}
|
|
|
|
async start() {
|
|
console.log(`[PULSE Worker] Starting worker: ${this.workerName}`);
|
|
console.log(`[PULSE Worker] Worker ID: ${this.workerId}`);
|
|
console.log(`[PULSE Worker] Server: ${this.serverUrl}`);
|
|
|
|
// Send initial heartbeat
|
|
await this.sendHeartbeat();
|
|
|
|
// Start heartbeat timer
|
|
this.startHeartbeat();
|
|
|
|
// Connect to WebSocket for real-time commands
|
|
this.connectWebSocket();
|
|
|
|
console.log(`[PULSE Worker] Worker started successfully`);
|
|
}
|
|
|
|
startHeartbeat() {
|
|
this.heartbeatTimer = setInterval(async () => {
|
|
try {
|
|
await this.sendHeartbeat();
|
|
} catch (error) {
|
|
console.error('[PULSE Worker] Heartbeat failed:', error.message);
|
|
}
|
|
}, this.heartbeatInterval);
|
|
}
|
|
|
|
async sendHeartbeat() {
|
|
const metadata = {
|
|
hostname: os.hostname(),
|
|
platform: os.platform(),
|
|
arch: os.arch(),
|
|
cpus: os.cpus().length,
|
|
totalMem: os.totalmem(),
|
|
freeMem: os.freemem(),
|
|
uptime: os.uptime(),
|
|
loadavg: os.loadavg(),
|
|
activeTasks: this.activeTasks,
|
|
maxConcurrentTasks: this.maxConcurrentTasks
|
|
};
|
|
|
|
try {
|
|
const response = await axios.post(
|
|
`${this.serverUrl}/api/workers/heartbeat`,
|
|
{
|
|
worker_id: this.workerId,
|
|
name: this.workerName,
|
|
metadata: metadata
|
|
},
|
|
{
|
|
headers: {
|
|
'X-API-Key': this.apiKey,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
}
|
|
);
|
|
|
|
console.log(`[PULSE Worker] Heartbeat sent - Status: online`);
|
|
return response.data;
|
|
} catch (error) {
|
|
console.error('[PULSE Worker] Heartbeat error:', error.message);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
connectWebSocket() {
|
|
console.log(`[PULSE Worker] Connecting to WebSocket...`);
|
|
|
|
// The server authenticates the WebSocket upgrade with the worker key (issue #38).
|
|
const headers = this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {};
|
|
this.ws = new WebSocket(this.wsUrl, { headers });
|
|
|
|
this.ws.on('open', () => {
|
|
console.log('[PULSE Worker] WebSocket connected');
|
|
// Identify this worker
|
|
this.ws.send(JSON.stringify({
|
|
type: 'worker_connect',
|
|
worker_id: this.workerId,
|
|
worker_name: this.workerName,
|
|
api_key: this.apiKey
|
|
}));
|
|
});
|
|
|
|
this.ws.on('message', async (data) => {
|
|
try {
|
|
const message = JSON.parse(data.toString());
|
|
await this.handleMessage(message);
|
|
} catch (error) {
|
|
console.error('[PULSE Worker] Message handling error:', error);
|
|
}
|
|
});
|
|
|
|
this.ws.on('close', () => {
|
|
console.log('[PULSE Worker] WebSocket disconnected, reconnecting...');
|
|
setTimeout(() => this.connectWebSocket(), 5000);
|
|
});
|
|
|
|
this.ws.on('error', (error) => {
|
|
console.error('[PULSE Worker] WebSocket error:', error.message);
|
|
});
|
|
}
|
|
|
|
async handleMessage(message) {
|
|
console.log(`[PULSE Worker] Received message:`, message.type);
|
|
|
|
switch (message.type) {
|
|
case 'execute_command':
|
|
await this.executeCommand(message);
|
|
break;
|
|
case 'execute_workflow':
|
|
await this.executeWorkflow(message);
|
|
break;
|
|
case 'ping':
|
|
this.sendPong();
|
|
break;
|
|
default:
|
|
console.log(`[PULSE Worker] Unknown message type: ${message.type}`);
|
|
}
|
|
}
|
|
|
|
async executeCommand(message) {
|
|
const { command, execution_id, command_id, timeout = 300000 } = message;
|
|
|
|
if (this.activeTasks >= this.maxConcurrentTasks) {
|
|
console.log(`[PULSE Worker] Max concurrent tasks reached, rejecting command`);
|
|
return;
|
|
}
|
|
|
|
this.activeTasks++;
|
|
console.log(`[PULSE Worker] Executing command (active tasks: ${this.activeTasks})`);
|
|
|
|
try {
|
|
const startTime = Date.now();
|
|
const { stdout, stderr } = await execAsync(command, {
|
|
timeout: timeout,
|
|
maxBuffer: 10 * 1024 * 1024 // 10MB buffer
|
|
});
|
|
const duration = Date.now() - startTime;
|
|
|
|
const result = {
|
|
type: 'command_result',
|
|
execution_id,
|
|
worker_id: this.workerId,
|
|
command_id,
|
|
success: true,
|
|
stdout: stdout,
|
|
stderr: stderr,
|
|
duration: duration,
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
|
|
this.sendResult(result);
|
|
console.log(`[PULSE Worker] Command completed in ${duration}ms`);
|
|
} catch (error) {
|
|
const result = {
|
|
type: 'command_result',
|
|
execution_id,
|
|
worker_id: this.workerId,
|
|
command_id,
|
|
success: false,
|
|
error: error.message,
|
|
stdout: error.stdout || '',
|
|
stderr: error.stderr || '',
|
|
timestamp: new Date().toISOString()
|
|
};
|
|
|
|
this.sendResult(result);
|
|
console.error(`[PULSE Worker] Command failed:`, error.message);
|
|
} finally {
|
|
this.activeTasks--;
|
|
}
|
|
}
|
|
|
|
async executeWorkflow(message) {
|
|
const { workflow, execution_id } = message;
|
|
|
|
console.log(`[PULSE Worker] Executing workflow: ${workflow.name}`);
|
|
|
|
// Workflow execution will be implemented in phase 2
|
|
// For now, just acknowledge receipt
|
|
this.sendResult({
|
|
type: 'workflow_result',
|
|
execution_id,
|
|
worker_id: this.workerId,
|
|
success: true,
|
|
message: 'Workflow execution not yet implemented',
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
}
|
|
|
|
sendResult(result) {
|
|
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
this.ws.send(JSON.stringify(result));
|
|
}
|
|
}
|
|
|
|
sendPong() {
|
|
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
this.ws.send(JSON.stringify({ type: 'pong', worker_id: this.workerId }));
|
|
}
|
|
}
|
|
|
|
async stop() {
|
|
console.log('[PULSE Worker] Shutting down...');
|
|
|
|
if (this.heartbeatTimer) {
|
|
clearInterval(this.heartbeatTimer);
|
|
}
|
|
|
|
if (this.ws) {
|
|
this.ws.close();
|
|
}
|
|
|
|
console.log('[PULSE Worker] Shutdown complete');
|
|
}
|
|
}
|
|
|
|
// Start worker
|
|
const worker = new PulseWorker();
|
|
|
|
// Handle graceful shutdown
|
|
process.on('SIGTERM', async () => {
|
|
await worker.stop();
|
|
process.exit(0);
|
|
});
|
|
|
|
process.on('SIGINT', async () => {
|
|
await worker.stop();
|
|
process.exit(0);
|
|
});
|
|
|
|
// Start the worker
|
|
worker.start().catch((error) => {
|
|
console.error('[PULSE Worker] Fatal error:', error);
|
|
process.exit(1);
|
|
});
|