Skip to content

Modular logistics and supply chain operating system (LSOS)

11.3 KB
Raw Blame
'use strict';

/**
 * Shared test helpers (no tests of their own). `node --test` loads every .js file under
 * test/, so this file is also executed as a test file; it only defines functions.
 */

const crypto = require('crypto');
const http = require('http');
const socketClusterClient = require('socketcluster-client');
const { loadConfig } = require('../lib/config');
const { createSocketServer } = require('../lib/app');
const { deriveKey, signRequest, verifyRequest, PURPOSE_AUTHORIZE, PURPOSE_PUBLISH } = require('../lib/signing');

const AUTH_KEY = 'test-only-socket-auth-key-0123456789abcdefghij';

function base64url(value) {
    return Buffer.from(value).toString('base64url');
}

/** Signs an HS256 JWT with the given key. */
function signJwt(claims, key = AUTH_KEY, header = { alg: 'HS256', typ: 'JWT' }) {
    const head = base64url(JSON.stringify(header));
    const body = base64url(JSON.stringify(claims));
    const signature = crypto.createHmac('sha256', key).update(`${head}.${body}`).digest('base64url');
    return `${head}.${body}.${signature}`;
}

function nowSeconds() {
    return Math.floor(Date.now() / 1000);
}

/** Claims shaped like an API-minted user token. */
function userClaims(overrides = {}) {
    const now = nowSeconds();
    return {
        iss: 'fleetbase-api',
        aud: 'fleetbase-socket',
        iat: now,
        nbf: now,
        exp: now + 900,
        jti: crypto.randomUUID(),
        kind: 'user',
        sub: 'user-uuid-1',
        cid: 'company-uuid-1',
        cpid: 'company_abc123',
        env: 'live',
        ids: ['user-uuid-1', 'user_abc123'],
        adm: false,
        ...overrides,
    };
}

function withTimeout(promise, ms, label) {
    let timer;
    const timeout = new Promise((resolve, reject) => {
        timer = setTimeout(() => reject(new Error(`Timed out after ${ms}ms waiting for ${label}`)), ms);
    });
    return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}

function delay(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
}

/** A logger that records every line instead of writing it. */
function captureLogger() {
    const records = [];
    const push = (level) => (record) => records.push({ level, ...record });
    return {
        records,
        logger: { level: 3, error: push('error'), warn: push('warn'), info: push('info'), debug: push('debug') },
    };
}

/** In-memory token store, the way Fleetbase clients deliver tokens (never localStorage). */
function memoryAuthEngine(initialToken = null) {
    let token = initialToken;
    return {
        saveToken: async (name, value) => {
            token = value;
            return value;
        },
        removeToken: async () => {
            const old = token;
            token = null;
            return old;
        },
        loadToken: async () => token,
    };
}

const DEFAULT_REMOTE_CONFIG = () => ({ enabled: true, mode: null, origins: null, updated_at: null });

/**
 * A stand-in for the API's socket endpoints, both signed with the authorize key:
 * - POST .../authorize: `decide(body)` returns the JSON response (or { status, body } for a
 *   non-200); it may be async to simulate a slow API. Calls are recorded in `calls`.
 * - GET .../config: `remoteConfig()` returns the JSON response, { status, body }, or
 *   'hangup' to drop the connection. Calls are recorded in `configCalls`.
 */
async function startFakeAuthorizer(decide, remoteConfig = DEFAULT_REMOTE_CONFIG) {
    const calls = [];
    const configCalls = [];
    const authorizeKey = deriveKey(AUTH_KEY, PURPOSE_AUTHORIZE);
    const server = http.createServer((req, res) => {
        if (req.url.split('?')[0].endsWith('/config')) {
            req.resume();
            req.on('end', () => {
                const verdict = verifyRequest(authorizeKey, {
                    timestamp: req.headers['x-fleetbase-timestamp'],
                    signature: req.headers['x-fleetbase-signature'],
                    rawBody: '',
                });
                configCalls.push({ method: req.method, signatureOk: verdict.ok });
                const result = verdict.ok ? remoteConfig() : { status: 401, body: { error: 'invalid_signature' } };
                if (result === 'hangup') {
                    res.destroy();
                    return;
                }
                const status = result && result.status ? result.status : 200;
                const payload = result && result.status ? result.body : result;
                res.writeHead(status, { 'Content-Type': 'application/json' });
                res.end(JSON.stringify(payload));
            });
            return;
        }
        const chunks = [];
        req.on('data', (chunk) => chunks.push(chunk));
        req.on('end', async () => {
            const raw = Buffer.concat(chunks);
            const verdict = verifyRequest(authorizeKey, {
                timestamp: req.headers['x-fleetbase-timestamp'],
                signature: req.headers['x-fleetbase-signature'],
                rawBody: raw,
            });
            let body = null;
            try {
                body = JSON.parse(raw.toString('utf8'));
            } catch (error) {
                body = null;
            }
            calls.push({ path: req.url, signatureOk: verdict.ok, body });
            if (!verdict.ok) {
                res.writeHead(401, { 'Content-Type': 'application/json' });
                res.end('{"error":"invalid_signature"}');
                return;
            }
            let result;
            try {
                result = await decide(body);
            } catch (error) {
                result = { status: 500, body: { error: 'boom' } };
            }
            if (res.destroyed) {
                return;
            }
            const status = result && result.status ? result.status : 200;
            const payload = result && result.status ? result.body : result;
            res.writeHead(status, { 'Content-Type': 'application/json' });
            res.end(JSON.stringify(payload));
        });
    });
    await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
    const { port } = server.address();
    return {
        url: `http://127.0.0.1:${port}/int/v1/socket/authorize`,
        calls,
        configCalls,
        close: () =>
            new Promise((resolve) => {
                server.close(() => resolve());
                server.closeAllConnections();
            }),
    };
}

const denyEverything = () => ({ allow: false, ttl: 30, reason: 'not_allowed' });

/**
 * Starts a real socket server on ephemeral ports with a fake authorize endpoint.
 *
 * Config polling is off unless `remoteConfig` is given: then the server polls the fake
 * config endpoint every `pollIntervalMs` (default 50 ms).
 */
async function startServer({ env = {}, decide = denyEverything, authorizeTimeoutMs, remoteConfig, pollIntervalMs = 50 } = {}) {
    const fake = await startFakeAuthorizer(decide, remoteConfig);
    const config = loadConfig({
        SOCKETCLUSTER_PORT: '0',
        SOCKETCLUSTER_INTERNAL_PORT: '0',
        SOCKETCLUSTER_AUTH_ENABLED: 'true',
        SOCKETCLUSTER_AUTH_KEY: AUTH_KEY,
        SOCKETCLUSTER_AUTHORIZE_URL: fake.url,
        ...env,
    });
    const { records, logger } = captureLogger();
    const server = createSocketServer(config, { logger, authorizeTimeoutMs, configPollIntervalMs: remoteConfig ? pollIntervalMs : 0 });
    const { port, internalPort } = await server.start();
    const clients = [];

    async function connect({ token = null, query = {}, autoReconnect = false } = {}) {
        const socket = socketClusterClient.create({
            hostname: '127.0.0.1',
            port,
            path: '/socketcluster/',
            autoReconnect,
            ackTimeout: 5000,
            authEngine: memoryAuthEngine(token),
            query: { client: 'test/1.0', ...query },
        });
        clients.push(socket);
        const status = await withTimeout(socket.listener('connect').once(), 5000, 'socket connect');
        return { socket, status };
    }

    function internalRequest(path, { method = 'POST', body, key = deriveKey(AUTH_KEY, PURPOSE_PUBLISH), timestamp, headers = {} } = {}) {
        const raw = body === undefined ? '' : typeof body === 'string' ? body : JSON.stringify(body);
        const signed = key ? signRequest(key, raw, timestamp) : {};
        return fetch(`http://127.0.0.1:${internalPort}${path}`, {
            method,
            headers: { 'Content-Type': 'application/json', ...signed, ...headers },
            body: method === 'GET' || method === 'HEAD' ? undefined : raw,
        });
    }

    async function close() {
        for (const socket of clients) {
            try {
                socket.disconnect();
                socket.killAllListeners();
                socket.killAllChannels();
            } catch (error) {
                // already closed
            }
        }
        await server.stop();
        await fake.close();
    }

    async function stats() {
        const response = await internalRequest('/stats', { method: 'GET' });
        assertOk(response.status === 200, `GET /stats answered ${response.status}`);
        return response.json();
    }

    return { port, internalPort, config, server, logs: records, fake, connect, internalRequest, stats, close };
}

function assertOk(condition, message) {
    if (!condition) {
        throw new Error(message);
    }
}

/** Resolves once `predicate()` is truthy, polling every 10 ms. */
function waitFor(predicate, ms = 3000, label = 'condition') {
    return withTimeout(
        (async () => {
            while (!(await predicate())) {
                await delay(10);
            }
        })(),
        ms,
        label
    );
}

/**
 * Attempts a raw websocket handshake with the given Origin header and resolves with the
 * HTTP status: 101 when SocketCluster accepts the origin, 403 when it refuses it.
 */
function handshakeStatus(port, origin) {
    return new Promise((resolve, reject) => {
        const req = http.request({
            host: '127.0.0.1',
            port,
            path: '/socketcluster/',
            headers: {
                Connection: 'Upgrade',
                Upgrade: 'websocket',
                'Sec-WebSocket-Version': '13',
                'Sec-WebSocket-Key': crypto.randomBytes(16).toString('base64'),
                Origin: origin,
            },
        });
        req.on('upgrade', (res, socket) => {
            socket.destroy();
            resolve(res.statusCode);
        });
        req.on('response', (res) => {
            res.resume();
            resolve(res.statusCode);
        });
        req.on('error', reject);
        req.end();
    });
}

/**
 * Subscribes and resolves with { ok: true } or { ok: false, error } once the server answers.
 */
async function trySubscribe(socket, name, ms = 5000) {
    const channel = socket.subscribe(name);
    const result = await withTimeout(
        Promise.race([
            channel.listener('subscribe').once().then(() => ({ ok: true })),
            channel.listener('subscribeFail').once().then(({ error }) => ({ ok: false, error })),
        ]),
        ms,
        `subscribe ${name}`
    );
    return { channel, ...result };
}

module.exports = {
    AUTH_KEY,
    signJwt,
    userClaims,
    nowSeconds,
    withTimeout,
    delay,
    waitFor,
    handshakeStatus,
    captureLogger,
    memoryAuthEngine,
    startFakeAuthorizer,
    startServer,
    trySubscribe,
};