Add direct B2 device enrollment and consolidate VPS controls
Device Core qualification / qualify (push) Successful in 8m6s

This commit is contained in:
DCCONSTRUCTIONS
2026-10-01 09:21:43 +03:00
parent 010fab999e
commit dc9fe6e0fc
12 changed files with 384 additions and 86 deletions
@@ -164,10 +164,67 @@ export class B2Infrastructure {
// borrow the host administrator credential from another project.
return remote.call(`/v2/contours/${contourId}/devices/${deviceId}/settings`, request);
}
async enrollmentOptions(actor, input) {
fields(input, []);
const projectId = ref(input.projectRef, 'project');
const db = await this.pool.connect();
let sources;
try {
await db.query('begin');
await findProjectWithCapability(db, actor, projectId, 'device.enroll');
sources = (await db.query(`select s.*, v.host_id, h.display_name,
coalesce((select json_agg(l.contour_id order by l.contour_id) from device_b2_contour_links l
where l.source_id=s.id and l.project_id=v.project_id), '[]'::json) as contour_ids
from device_b2_hosts s join device_b2_host_views v on v.source_id=s.id
join device_infrastructure_hosts h on h.id=v.host_id and h.project_id=v.project_id
where v.project_id=$1 and h.lifecycle_state='active'`, [projectId])).rows;
await db.query('commit');
} catch (error) { await db.query('rollback'); throw error; } finally { db.release(); }
return { hosts: await Promise.all(sources.map(async source => {
const host = { hostRef: `host:${source.host_id}`, name: source.display_name };
try {
const remote = await this.clientFor(source);
const identity = await remote.call('/v2/identity');
if (identity.host_id !== source.remote_host_id) throw b2Error('b2_host_identity_mismatch');
const contours = (await remote.call('/v2/contours')).items
.filter(item => source.contour_ids.includes(item.id) && item.access === 'manage')
.map(({ id, name }) => ({ id, name }));
return { ...host, reachable: true, contours };
} catch { return { ...host, reachable: false, contours: [] }; }
})) };
}
async enrollment(actor, input) {
fields(input, ['contourId', 'request']);
const projectId = ref(input.projectRef, 'project'), hostId = ref(input.hostRef, 'host');
const contourId = opaque(input.contourId), request = input.request;
if (!request || typeof request !== 'object' || Array.isArray(request)
|| !['catalog', 'inspect', 'register'].includes(request.action)) throw b2Error('b2_request_invalid');
const db = await this.pool.connect();
let source;
try {
await db.query('begin');
await findProjectWithCapability(db, actor, projectId, 'device.enroll');
source = (await db.query(`select s.* from device_b2_hosts s
join device_b2_host_views v on v.source_id=s.id
join device_b2_contour_links l on l.source_id=s.id and l.project_id=v.project_id
join device_infrastructure_hosts h on h.id=v.host_id and h.project_id=v.project_id
where v.host_id=$1 and v.project_id=$2 and l.contour_id=$3 and h.lifecycle_state='active'`,
[hostId, projectId, contourId])).rows[0];
if (!source) throw b2Error('b2_access_denied', 403);
await db.query('commit');
} catch (error) { await db.query('rollback'); throw error; } finally { db.release(); }
const remote = await this.clientFor(source);
const identity = await remote.call('/v2/identity');
if (identity.host_id !== source.remote_host_id) throw b2Error('b2_host_identity_mismatch');
// Only the selected project's linked contour may receive identity/configuration.
return remote.call(`/v2/contours/${contourId}/enrollment`, request);
}
async execute(actor, input, requestId) {
if (input.action === 'register-host') return this.registerHost(actor, input, requestId);
if (input.action === 'inventory') return this.inventory(actor, input);
if (input.action === 'tracker') return this.tracker(actor, input);
if (input.action === 'enrollment-options') return this.enrollmentOptions(actor, input);
if (input.action === 'enrollment') return this.enrollment(actor, input);
const projectId = ref(input.projectRef, 'project');
const hostId = ref(input.hostRef, 'host');
const action = input.action;
@@ -318,3 +318,45 @@ test('application deletion requires company and credential authority and forward
assert.deepEqual(calls, [{ path: '/v2/management/applications/delete', body: { integration_id: input.integrationId } }]);
assert.equal(repo.writes.at(-1).values[1], 'b2.application-delete');
});
test('enrollment options reveal only managed contours linked to the current active project', async () => {
const own = 'a'.repeat(32), foreign = 'b'.repeat(32); const repo = repository();
const db = await repo.pool.connect(), original = db.query; let released = false;
db.release = () => { released = true; };
db.query = async (sql, values) => {
if (sql.includes('from device_b2_hosts')) {
assert.match(sql, /l.project_id=v.project_id/); assert.match(sql, /h.lifecycle_state='active'/);
assert.deepEqual(values, [projectId]); return { rows: [{ ...source, host_id: hostId, display_name: 'VPS', contour_ids: [own] }] };
}
return original(sql, values);
};
const service = new B2Infrastructure({ pool: repo.pool, secrets: { read: async () => pkg }, clientFactory: () => ({ async call(path) {
assert.ok(released);
return path === '/v2/identity' ? { host_id: pkg.host_id } : { items: [{ id: own, name: 'Own', access: 'manage' }, { id: foreign, name: 'Foreign', access: 'manage' }] };
} }) });
const result = await service.execute(actor, { projectRef: `project:${projectId}`, action: 'enrollment-options' });
assert.deepEqual(result.hosts[0].contours, [{ id: own, name: 'Own' }]); assert.ok(!JSON.stringify(result).includes('Foreign'));
});
test('enrollment validates project capability, active host and contour before forwarding USB', async () => {
const own = 'a'.repeat(32), foreign = 'b'.repeat(32); const repo = repository();
const db = await repo.pool.connect(), original = db.query; const calls = [];
db.query = async (sql, values) => {
if (sql.includes('from device_b2_hosts')) {
assert.match(sql, /l.project_id=v.project_id/); assert.match(sql, /h.lifecycle_state='active'/);
return { rows: values[0] === hostId && values[1] === projectId && values[2] === own ? [source] : [] };
}
return original(sql, values);
};
const service = new B2Infrastructure({ pool: repo.pool, secrets: { read: async () => pkg }, clientFactory: () => ({ async call(path, body) {
calls.push(path); return path === '/v2/identity' ? { host_id: pkg.host_id } : { accepted: body.action };
} }) });
const input = { projectRef: `project:${projectId}`, hostRef: `host:${hostId}`, action: 'enrollment', contourId: own, request: { action: 'register', name: 'B2', imei: '323456789012345', number: 300 } };
await assert.rejects(service.execute(actor, { ...input, contourId: foreign }), /b2_access_denied/);
assert.deepEqual(calls, []);
assert.deepEqual(await service.execute(actor, input), { accepted: 'register' });
assert.deepEqual(calls, ['/v2/identity', `/v2/contours/${own}/enrollment`]);
const denied = new B2Infrastructure({ pool: repository({ role: 'viewer' }).pool, secrets: { read: async () => assert.fail('no credentials') } });
await assert.rejects(denied.execute({ ...actor, hubRole: 'member' }, input));
await assert.rejects(denied.execute({ ...actor, hubRole: 'member' }, { projectRef: input.projectRef, action: 'enrollment-options' }));
});