87339da1b2
SECURITY /api/agent/ws authenticated nothing. There was no agent registry, so any client reaching the SSO could register as a node, publish discovery and telemetry into the admin view, and receive commands -- including a signed arbitrary_bash -- addressed to a token it guessed. Tokens were generated in the BROWSER and never recorded server-side, so there was nothing to validate against and no way to revoke one. Agents are now rows in a new Agent table, authenticated by SHA-256 token hash before the connection is registered or the welcome payload is sent. Tokens are minted by POST /api/agent/enroll and shown once. Revoke and rotate drop the live socket immediately. All agent actions are audited. The Ed25519 command-signing key was generated in the AgentManager constructor, so it changed on every restart and the public_key pinned in an agent's agent.yml stopped matching. It now lives in OpenBao at secret/agent/signing-key; if it cannot be loaded the SSO refuses to send high-risk commands rather than signing with a key no agent has seen. DIRECTORY Agents bind to a host resource instead of being matched by hostname, and a bound agent's discovery is written onto that resource -- previously the one source running ON the host contributed nothing to the directory. The resource tree is collapsible, with state persisted per browser. DISCOVERY The Proxmox plugin zipped MACs and IPs from two flat lists by index, attributing addresses to the wrong NIC on multi-NIC guests. NICs are now keyed by MAC. Adds an endpoint resource parenting each node, sourceId/ vmid/node identity, container-interface filtering, node IP/MAC, and offline-node handling. The reconciler could make a resource its own parent, named hosts after their MAC address, had a dead isIp() regex (\\. matches a backslash), merged across kinds, and re-read the whole inventory per resource. Dockerfile.test-runner never copied nodejs/plugins, so every plugin test suite failed in CI as "Cannot find module". Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
83 lines
3.2 KiB
JavaScript
83 lines
3.2 KiB
JavaScript
'use strict';
|
|
|
|
const conf = require('@simpleworkjs/conf');
|
|
const { setUpTable } = require('model-redis');
|
|
|
|
// Keep model-redis for the ones not yet ported
|
|
const Table = setUpTable(conf.redis);
|
|
module.exports = Table;
|
|
|
|
const { Token, AuthToken, InviteToken, ImpersonationToken, PasswordResetToken, OtpToken, ServiceToken } = require('./token');
|
|
require('./verification');
|
|
require('./oauth_code');
|
|
require('./api_token');
|
|
|
|
const { init } = require('@simpleworkjs/orm');
|
|
const { Resource, ResourceEdge, ResourceGroup } = require('./resource');
|
|
const { AccessRequest } = require('./access_request');
|
|
const { Webhook } = require('./webhook');
|
|
const { PluginInstance } = require('./plugin_instance');
|
|
const { SharedSecret } = require('./shared_secret');
|
|
const { SharedSecretGrant } = require('./shared_secret_grant');
|
|
const { VaultAppToken } = require('./vault_app_token');
|
|
const { Agent } = require('./agent');
|
|
async function initORM() {
|
|
const ormConf = conf.orm || {
|
|
dialect: 'sqlite',
|
|
storage: './config/inventory.sqlite',
|
|
logging: false
|
|
};
|
|
ormConf.redis = conf.redis;
|
|
|
|
console.log('[initORM] Starting ORM initialization...');
|
|
try {
|
|
await init({
|
|
conf: { orm: ormConf },
|
|
models: [
|
|
Resource, ResourceEdge, ResourceGroup, AccessRequest, Webhook, PluginInstance,
|
|
SharedSecret, SharedSecretGrant, VaultAppToken, Agent,
|
|
Token, AuthToken, InviteToken, ImpersonationToken, PasswordResetToken, OtpToken, ServiceToken
|
|
]
|
|
});
|
|
console.log('[initORM] ORM initialized successfully');
|
|
console.log('[initORM] Resource.orm =', !!Resource.orm, 'Token.orm =', !!Token.orm);
|
|
await healSchema();
|
|
} catch (err) {
|
|
console.error('[initORM] ORM initialization failed:', err.message);
|
|
throw err;
|
|
}
|
|
}
|
|
|
|
// Add-only schema heal. @simpleworkjs/orm runs sequelize.sync() WITHOUT alter,
|
|
// which creates missing tables but never touches existing ones — so a column
|
|
// added in a newer release (e.g. PluginInstance.lastLog) simply never appears
|
|
// in an upgraded deployment's database and every query on the model fails
|
|
// ("no such column"). This walks each Sequelize model and ADDs any attribute
|
|
// missing from its table. Strictly additive (never drops or retypes), works on
|
|
// any dialect via the query interface, and fail-soft per column so one bad
|
|
// attribute can't take the boot down.
|
|
async function healSchema() {
|
|
const adapter = Resource.orm && Resource.orm.adapters && Resource.orm.adapters.sequelize;
|
|
if (!adapter || !adapter.sequelize) return;
|
|
const sequelize = adapter.sequelize;
|
|
const qi = sequelize.getQueryInterface();
|
|
for (const SM of Object.values(sequelize.models)) {
|
|
const table = SM.getTableName();
|
|
let existing;
|
|
try { existing = await qi.describeTable(table); }
|
|
catch (e) { continue; } // no table yet — sync() handles creation
|
|
for (const [name, attr] of Object.entries(SM.getAttributes())) {
|
|
const col = attr.field || name;
|
|
if (existing[col]) continue;
|
|
try {
|
|
await qi.addColumn(table, col, attr);
|
|
console.log(`[initORM] schema heal: added missing column ${table}.${col}`);
|
|
} catch (e) {
|
|
console.error(`[initORM] schema heal: could not add ${table}.${col}:`, e.message);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports.initORM = initORM;
|