Bring Device Core settings to Mission UI and expose scoped B2 controls
Device Core qualification / qualify (push) Successful in 6m40s

This commit is contained in:
DCCONSTRUCTIONS
2026-09-30 22:43:54 +03:00
parent 8031ed76a2
commit 8941769b4c
12 changed files with 295 additions and 4 deletions
@@ -149,7 +149,9 @@ export function createB2Client(input, { fetchImpl } = {}) {
const value = await res.json().catch(() => null);
if (!res.ok) {
if (res.status === 401 || res.status === 403) { token = null; throw b2Error('b2_access_denied', 403); }
if (res.status === 409 && ['settings_revision_changed', 'settings_initial_read_required', 'settings_sim_observation_required'].includes(value?.error)) throw b2Error('b2_' + value.error, 409);
if (res.status === 409) throw b2Error('b2_conflict', 409);
if (res.status === 400) throw b2Error('b2_request_invalid', 400);
throw b2Error('b2_host_request_failed', 502);
}
if (!value || typeof value !== 'object') throw b2Error('b2_response_invalid', 502);
@@ -70,8 +70,43 @@ export class B2Infrastructure {
}));
return { items: results.flatMap((result) => result.items), hosts: results.map((result) => result.host) };
}
async tracker(actor, input) {
fields(input, ['contourId', 'deviceId', 'request']);
const projectId = ref(input.projectRef, 'project'), hostId = ref(input.hostRef, 'host');
const contourId = opaque(input.contourId), deviceId = opaque(input.deviceId);
const request = input.request;
const permissions = {
state: 'configuration.read', journal: 'audit.read', attempts: 'audit.read',
'firmware-download': 'configuration.read', 'firmware-upload': 'configuration.manage',
save: 'configuration.manage', 'profile-save': 'configuration.manage',
read: 'command.dispatch', apply: 'configuration.manage', command: 'command.dispatch', cancel: 'command.dispatch',
};
if (!request || typeof request !== 'object' || !permissions[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, permissions[request.action]);
if (request.action === 'apply') await findProjectWithCapability(db, actor, projectId, 'command.dispatch');
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');
// VPS checks the device's actual contour as well; a forged device ID cannot
// borrow the host administrator credential from another project.
return remote.call(`/v2/contours/${contourId}/devices/${deviceId}/settings`, request);
}
async execute(actor, input, requestId) {
if (input.action === 'inventory') return this.inventory(actor, input);
if (input.action === 'tracker') return this.tracker(actor, input);
const projectId = ref(input.projectRef, 'project');
const hostId = ref(input.hostRef, 'host');
const action = input.action;
@@ -234,3 +234,33 @@ test('application creation and issuance use one server-generated request identit
await assert.rejects(service.execute(actor, { action: 'application-connect', projectRef: `project:${projectId}`, hostRef: `host:${hostId}`, name: 'Other', contourId: 'f'.repeat(32) }, requestId), /b2_access_denied/);
assert.equal(calls.length, 2);
});
test('tracker settings keep project and contour authority across remote calls', async () => {
const contour = 'c'.repeat(32), device = 'd'.repeat(32), calls = [];
const repo = repository({ role: 'viewer' }); const db = await repo.pool.connect();
let released = false; const original = db.query;
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, [hostId, projectId, contour]);
return { rows: [source] };
}
return original(sql, values);
};
const service = new B2Infrastructure({ pool: repo.pool, secrets: { read: async () => pkg }, clientFactory: () => ({ async call(path, body) {
assert.equal(released, true); calls.push({path,body});
return path === '/v2/identity' ? {host_id:pkg.host_id} : {revision:1};
} }) });
const input = {action:'tracker',projectRef:`project:${projectId}`,hostRef:`host:${hostId}`,contourId:contour,deviceId:device,request:{action:'state'}};
assert.deepEqual(await service.execute({...actor,hubRole:'member'},input), {revision:1});
assert.equal(calls[1].path, `/v2/contours/${contour}/devices/${device}/settings`);
for (const action of ['save','apply','read','command','cancel','firmware-upload','profile-save']) {
await assert.rejects(service.execute({...actor,hubRole:'member'},{...input,request:{action}}));
}
assert.equal(calls.length,2);
db.query = async (sql,values) => sql.includes('from device_b2_hosts') ? {rows:[]} : original(sql,values);
await assert.rejects(service.execute(actor,input), /b2_access_denied/);
assert.equal(calls.length,2);
});