feat(device-manager): align functional UI with Mission Core interaction patterns
Device Core qualification / qualify (push) Successful in 8m53s
Device Core qualification / qualify (push) Successful in 8m53s
This commit is contained in:
@@ -83,7 +83,7 @@ export class B2Infrastructure {
|
||||
// Host management can affect all projects of the company. A project-only
|
||||
// grant must not turn into company-wide administration.
|
||||
if (action !== 'devices') assertActorCanManageOwnerScope(actor, { scopeKind: project.scope_kind, ownerRef: project.owner_ref });
|
||||
if (['connect', 'key-issue', 'key-revoke'].includes(action)) {
|
||||
if (['connect', 'key-issue', 'key-revoke', 'application-connect'].includes(action)) {
|
||||
await findProjectWithCapability(db, actor, projectId, 'credential.manage');
|
||||
}
|
||||
const host = (await db.query('select id,host_key,display_name,provider_ref from device_infrastructure_hosts where id=$1 and project_id=$2 and lifecycle_state <> $3 for share',
|
||||
@@ -129,12 +129,21 @@ export class B2Infrastructure {
|
||||
result = { connected: true, endpoint: source.endpoint, authority: source.authority,
|
||||
contours: value.contours || value.items, applications: value.applications || [],
|
||||
links: links.map((link) => ({ contourId: link.contour_id, projectRef: `project:${link.project_id}` })) };
|
||||
} else if (action === 'link') {
|
||||
fields(input, ['contourId', 'targetProjectRef']);
|
||||
} else if (action === 'link' || action === 'project-connect') {
|
||||
fields(input, action === 'link' ? ['contourId', 'targetProjectRef'] : ['targetProjectRef']);
|
||||
const targetId = ref(input.targetProjectRef || input.projectRef, 'project');
|
||||
const target = await findProjectWithCapability(db, actor, targetId, 'infrastructure.manage');
|
||||
if (target.owner_scope_id !== project.owner_scope_id) throw b2Error('b2_company_mismatch', 403);
|
||||
const contour = opaque(input.contourId);
|
||||
let contour;
|
||||
if (action === 'project-connect') {
|
||||
if (source.authority !== 'host') throw b2Error('b2_host_management_required', 403);
|
||||
const linked = links.filter((link) => link.project_id === targetId);
|
||||
if (linked.length > 1) throw b2Error('b2_conflict', 409);
|
||||
// Stable project identity makes a retry recover the same contour.
|
||||
contour = linked[0]?.contour_id || (await remote.call('/v2/management/contours', {
|
||||
key: 'dc-' + targetId, name: target.name,
|
||||
})).id;
|
||||
} else contour = opaque(input.contourId);
|
||||
const permitted = await remote.call('/v2/contours');
|
||||
if (!permitted.items.some((item) => item.id === contour)) throw b2Error('b2_access_denied', 403);
|
||||
const current = links.find((link) => link.contour_id === contour);
|
||||
@@ -176,6 +185,20 @@ export class B2Infrastructure {
|
||||
result = await remote.call('/v2/management/applications', {
|
||||
key: input.key, name: input.name, grants: { [opaque(input.contourId)]: 'manage' },
|
||||
});
|
||||
} else if (action === 'application-connect') {
|
||||
fields(input, ['name', 'contourId']);
|
||||
if (typeof input.name !== 'string' || !input.name.trim() || input.name.length > 160) throw b2Error('b2_name_invalid');
|
||||
const contour = opaque(input.contourId);
|
||||
if (!links.some((link) => link.contour_id === contour)) throw b2Error('b2_access_denied', 403);
|
||||
if (!/^[a-f0-9]{32}$/.test(requestId)) throw b2Error('b2_request_invalid');
|
||||
const app = await remote.call('/v2/management/applications', {
|
||||
key: 'app-' + requestId, name: input.name.trim(), grants: { [contour]: 'manage' },
|
||||
});
|
||||
// The VPS reserves issuance before returning a one-time secret. If
|
||||
// the response is lost, status exposes the app for explicit reissue.
|
||||
result = { id: app.id, connection: await remote.call('/v2/management/credentials', {
|
||||
integration_id: app.id, replace: false, request_id: requestId,
|
||||
}) };
|
||||
} else if (action === 'key-issue') {
|
||||
fields(input, ['integrationId', 'replace']);
|
||||
if (typeof input.replace !== 'boolean') throw b2Error('b2_replace_invalid');
|
||||
|
||||
@@ -24,7 +24,7 @@ function repository({ role = 'owner', links = [], otherOwner = ownerId } = {}) {
|
||||
const db = { release() {}, async query(sql, values) {
|
||||
if (['begin', 'commit', 'rollback'].includes(sql)) return { rows: [] };
|
||||
if (sql.includes('from device_projects')) return { rows: [{ id: values[0], owner_scope_id: values[0] === projectId ? ownerId : otherOwner,
|
||||
lifecycle_state: 'active', owner_lifecycle_state: 'active', scope_kind: 'company', owner_ref: 'company:example' }] };
|
||||
name: 'Test project', lifecycle_state: 'active', owner_lifecycle_state: 'active', scope_kind: 'company', owner_ref: 'company:example' }] };
|
||||
if (sql.includes('from device_project_grants')) return { rows: [{ id: 'grant', principal_kind: 'user', principal_ref: actor.userRef,
|
||||
project_role: role, capability_allow: [], capability_deny: [], lifecycle_state: 'active' }] };
|
||||
if (sql.includes('from device_infrastructure_hosts')) return { rows: [{ id: hostId }] };
|
||||
@@ -195,3 +195,42 @@ test('unreachable or mismatched host is reported as unavailable, not an empty he
|
||||
assert.deepEqual(result.items, []); assert.equal(JSON.stringify(result).includes('private-details'), false);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
test('project connection generates stable contour identity and links it without user codes', async () => {
|
||||
const repo = repository(); const calls = []; const contour = 'c'.repeat(32);
|
||||
const service = new B2Infrastructure({ pool: repo.pool, secrets: { read: async () => pkg }, clientFactory: () => ({ async call(path, body) {
|
||||
calls.push({ path, body });
|
||||
return path === '/v2/management/contours' ? { id: contour } : { items: [{ id: contour }] };
|
||||
} }) });
|
||||
const result = await service.execute(actor, { action: 'project-connect', projectRef: `project:${projectId}`, hostRef: `host:${hostId}`, targetProjectRef: `project:${projectId}` }, 'a'.repeat(32));
|
||||
assert.equal(result.linked, true);
|
||||
assert.equal(calls[0].body.key, 'dc-' + projectId);
|
||||
assert.equal(calls[0].body.name, 'Test project');
|
||||
assert.equal(repo.writes.filter((x) => x.sql.includes('device_b2_contour_links')).length, 1);
|
||||
});
|
||||
|
||||
test('connecting an already linked project does not create another contour', async () => {
|
||||
const contour = 'c'.repeat(32); const repo = repository({ links: [{ contour_id: contour, project_id: projectId }] });
|
||||
const service = new B2Infrastructure({ pool: repo.pool, secrets: { read: async () => pkg }, clientFactory: () => ({ async call(path) {
|
||||
assert.equal(path, '/v2/contours'); return { items: [{ id: contour }] };
|
||||
} }) });
|
||||
const result = await service.execute(actor, { action: 'project-connect', projectRef: `project:${projectId}`, hostRef: `host:${hostId}` }, 'a'.repeat(32));
|
||||
assert.equal(result.contourId, contour);
|
||||
});
|
||||
|
||||
test('application creation and issuance use one server-generated request identity', async () => {
|
||||
const contour = 'c'.repeat(32); const repo = repository({ links: [{ contour_id: contour, project_id: projectId }] });
|
||||
const calls = []; const requestId = 'd'.repeat(32); const app = 'e'.repeat(32);
|
||||
const service = new B2Infrastructure({ pool: repo.pool, secrets: { read: async () => pkg }, clientFactory: () => ({ async call(path, body) {
|
||||
calls.push({ path, body }); return path.endsWith('applications') ? { id: app } : { synthetic: true };
|
||||
} }) });
|
||||
const result = await service.execute(actor, { action: 'application-connect', projectRef: `project:${projectId}`, hostRef: `host:${hostId}`, name: 'Partner', contourId: contour }, requestId);
|
||||
assert.equal(calls[0].body.key, 'app-' + requestId);
|
||||
assert.equal(calls[1].body.request_id, requestId);
|
||||
assert.equal(result.id, app);
|
||||
assert.ok(result.connection);
|
||||
assert.equal(JSON.stringify(repo.writes).includes('synthetic'), false);
|
||||
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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user