Complete ARNavi VPS onboarding and contextual key controls
Device Core qualification / qualify (push) Successful in 6m45s
Device Core qualification / qualify (push) Successful in 6m45s
This commit is contained in:
@@ -32,6 +32,66 @@ export class B2Infrastructure {
|
||||
this.clients.set(source.secret_ref, client);
|
||||
return client;
|
||||
}
|
||||
async verifyConnection(input) {
|
||||
let value;
|
||||
try { value = typeof input === 'string' ? JSON.parse(input) : input; }
|
||||
catch { throw b2Error('b2_connection_invalid'); }
|
||||
const pkg = connectionPackage(value);
|
||||
const remote = this.clientFactory(pkg);
|
||||
const identity = await remote.call('/v2/identity');
|
||||
if (identity.host_id !== pkg.host_id) throw b2Error('b2_host_identity_mismatch', 409);
|
||||
const status = await remote.call(pkg.authority === 'host' ? '/v2/management' : '/v2/contours');
|
||||
if (pkg.authority === 'host' && status.host_id !== pkg.host_id) throw b2Error('b2_host_identity_mismatch', 409);
|
||||
return pkg;
|
||||
}
|
||||
async saveSource(db, project, pkg) {
|
||||
const secret = await this.secrets.save(pkg);
|
||||
const saved = (await db.query(`insert into device_b2_hosts
|
||||
(id,owner_scope_id,remote_host_id,endpoint,fingerprint,integration_id,authority,secret_ref)
|
||||
values($1,$2,$3,$4,$5,$6,$7,$8)
|
||||
on conflict(owner_scope_id,remote_host_id) do update set
|
||||
secret_ref=excluded.secret_ref,revision=device_b2_hosts.revision+1,updated_at=now()
|
||||
where device_b2_hosts.integration_id=excluded.integration_id
|
||||
and device_b2_hosts.endpoint=excluded.endpoint and device_b2_hosts.fingerprint=excluded.fingerprint
|
||||
and device_b2_hosts.authority=excluded.authority returning id`,
|
||||
[randomUUID(), project.owner_scope_id, pkg.host_id, pkg.endpoint, pkg.fingerprint, pkg.integration_id, pkg.authority, secret])).rows[0];
|
||||
if (!saved) throw b2Error('b2_connection_identity_conflict', 409);
|
||||
return saved;
|
||||
}
|
||||
async registerHost(actor, input, requestId) {
|
||||
fields(input, ['connection', 'name']);
|
||||
const projectId = ref(input.projectRef, 'project');
|
||||
if (typeof input.name !== 'string' || !input.name.trim() || input.name.length > 160) throw b2Error('b2_name_invalid');
|
||||
const db = await this.pool.connect();
|
||||
try {
|
||||
await db.query('begin');
|
||||
const project = await findProjectWithCapability(db, actor, projectId, 'infrastructure.manage');
|
||||
assertActorCanManageOwnerScope(actor, { scopeKind: project.scope_kind, ownerRef: project.owner_ref });
|
||||
await findProjectWithCapability(db, actor, projectId, 'credential.manage');
|
||||
// A rejected key never leaves an empty host in the user's registry.
|
||||
const pkg = await this.verifyConnection(input.connection);
|
||||
if (pkg.authority !== 'host') throw b2Error('b2_host_management_required', 403);
|
||||
await db.query('select pg_advisory_xact_lock(hashtextextended($1,0))', [project.owner_scope_id + ':' + pkg.host_id]);
|
||||
const saved = await this.saveSource(db, project, pkg);
|
||||
const existing = (await db.query(`select h.host_key from device_b2_host_views v
|
||||
join device_infrastructure_hosts h on h.id=v.host_id
|
||||
where v.source_id=$1 and v.project_id=$2`, [saved.id, projectId])).rows[0];
|
||||
const registered = await applyOntologyManagementCommand(db, { actor, commandKind: 'infrastructure_host.ensure',
|
||||
command: normalizeOntologyManagementCommand('infrastructure_host.ensure', {
|
||||
projectRef: input.projectRef, hostKey: existing?.host_key || `b2-${saved.id}`,
|
||||
displayName: input.name.trim(), providerRef: null, externalRef: pkg.host_id,
|
||||
managementCredentialRef: null, lifecycleState: 'active',
|
||||
}) });
|
||||
const hostRef = registered.host.hostRef;
|
||||
await db.query(`insert into device_b2_host_views(host_id,project_id,owner_scope_id,source_id)
|
||||
values($1,$2,$3,$4) on conflict(host_id) do nothing`, [ref(hostRef, 'host'), projectId, project.owner_scope_id, saved.id]);
|
||||
await db.query(`insert into device_audit_events(id,event_type,actor_ref,payload,project_id)
|
||||
values($1,$2,$3,$4::jsonb,$5)`, [randomUUID(), 'b2.register-host', actor.userRef,
|
||||
JSON.stringify({ projectRef: input.projectRef, hostRef, requestId }), projectId]);
|
||||
await db.query('commit');
|
||||
return { connected: true, hostRef, endpoint: pkg.endpoint, authority: pkg.authority };
|
||||
} catch (error) { await db.query('rollback'); throw error; } finally { db.release(); }
|
||||
}
|
||||
async inventory(actor, input) {
|
||||
fields(input, []);
|
||||
const projectId = ref(input.projectRef, 'project');
|
||||
@@ -105,6 +165,7 @@ export class B2Infrastructure {
|
||||
return remote.call(`/v2/contours/${contourId}/devices/${deviceId}/settings`, 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);
|
||||
const projectId = ref(input.projectRef, 'project');
|
||||
@@ -138,16 +199,7 @@ export class B2Infrastructure {
|
||||
if (identity.host_id !== pkg.host_id) throw b2Error('b2_host_identity_mismatch', 409);
|
||||
const status = pkg.authority === 'host' ? await remote.call('/v2/management') : await remote.call('/v2/contours');
|
||||
if (pkg.authority === 'host' && status.host_id !== pkg.host_id) throw b2Error('b2_host_identity_mismatch', 409);
|
||||
const secret = await this.secrets.save(pkg);
|
||||
const saved = (await db.query(`insert into device_b2_hosts
|
||||
(id,owner_scope_id,remote_host_id,endpoint,fingerprint,integration_id,authority,secret_ref)
|
||||
values($1,$2,$3,$4,$5,$6,$7,$8)
|
||||
on conflict(owner_scope_id,remote_host_id) do update set
|
||||
secret_ref=excluded.secret_ref,revision=device_b2_hosts.revision+1,updated_at=now()
|
||||
where device_b2_hosts.integration_id=excluded.integration_id
|
||||
and device_b2_hosts.endpoint=excluded.endpoint and device_b2_hosts.fingerprint=excluded.fingerprint
|
||||
and device_b2_hosts.authority=excluded.authority returning id`,
|
||||
[randomUUID(), project.owner_scope_id, pkg.host_id, pkg.endpoint, pkg.fingerprint, pkg.integration_id, pkg.authority, secret])).rows[0];
|
||||
const saved = await this.saveSource(db, project, pkg);
|
||||
if (!saved) throw b2Error('b2_connection_identity_conflict', 409);
|
||||
await db.query(`insert into device_b2_host_views(host_id,project_id,owner_scope_id,source_id)
|
||||
values($1,$2,$3,$4) on conflict(host_id) do nothing`, [hostId, projectId, project.owner_scope_id, saved.id]);
|
||||
|
||||
Reference in New Issue
Block a user