@capybara-hq/kernel 0.1.0-rc.68 → 0.1.0-rc.69
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin/admin.module.js +2 -0
- package/dist/admin/health.service.d.ts +11 -4
- package/dist/admin/health.service.js +33 -17
- package/dist/background/poll.job.js +2 -2
- package/dist/background/tasks.service.d.ts +5 -5
- package/dist/background/tasks.service.js +9 -8
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/catalog/requests.service.js +1 -1
- package/dist/config/schema.d.ts +1 -1
- package/dist/config/schema.js +9 -9
- package/dist/conversations/conversations.controller.d.ts +8 -3
- package/dist/conversations/conversations.controller.js +8 -3
- package/dist/conversations/conversations.service.d.ts +33 -15
- package/dist/conversations/conversations.service.js +60 -10
- package/dist/conversations/events.d.ts +21 -41
- package/dist/conversations/events.js +57 -31
- package/dist/conversations/permission-profiles.controller.d.ts +6 -4
- package/dist/conversations/permission-profiles.service.d.ts +6 -4
- package/dist/ctx/create-ctx.d.ts +20 -1
- package/dist/ctx/create-ctx.js +77 -6
- package/dist/ctx/device-provider-check.d.ts +6 -2
- package/dist/ctx/device-provider-check.js +17 -2
- package/dist/db/kernel-schema.d.ts +10 -48
- package/dist/devices/access.d.ts +3 -4
- package/dist/devices/access.js +3 -4
- package/dist/devices/conversation-devices.d.ts +1 -3
- package/dist/devices/conversation-devices.js +2 -7
- package/dist/devices/device-projects.service.js +24 -29
- package/dist/devices/device-provider-selection.d.ts +5 -6
- package/dist/devices/device-provider-selection.js +8 -9
- package/dist/devices/devices.controller.d.ts +8 -21
- package/dist/devices/devices.controller.js +9 -34
- package/dist/devices/devices.module.js +6 -4
- package/dist/devices/devices.schemas.d.ts +1 -5
- package/dist/devices/devices.schemas.js +1 -5
- package/dist/devices/devices.service.d.ts +84 -147
- package/dist/devices/devices.service.js +134 -229
- package/dist/devices/dto.d.ts +5 -32
- package/dist/devices/dto.js +5 -37
- package/dist/devices/errors.d.ts +6 -5
- package/dist/devices/errors.js +7 -8
- package/dist/devices/project-rules.d.ts +9 -11
- package/dist/devices/project-rules.js +7 -7
- package/dist/devices/working-root.d.ts +3 -6
- package/dist/devices/working-root.js +3 -8
- package/dist/dial-in/access.d.ts +23 -0
- package/dist/dial-in/access.js +64 -0
- package/dist/{local-devices/device-auth.controller.d.ts → dial-in/auth.controller.d.ts} +12 -6
- package/dist/{local-devices/device-auth.controller.js → dial-in/auth.controller.js} +30 -15
- package/dist/dial-in/auth.module.d.ts +7 -0
- package/dist/dial-in/auth.module.js +27 -0
- package/dist/{local-devices/device-auth.d.ts → dial-in/auth.service.d.ts} +42 -13
- package/dist/{local-devices/device-auth.js → dial-in/auth.service.js} +83 -32
- package/dist/dial-in/dial-in.module.d.ts +9 -0
- package/dist/dial-in/dial-in.module.js +37 -0
- package/dist/{local-devices → dial-in}/dto.d.ts +17 -2
- package/dist/{local-devices → dial-in}/dto.js +14 -2
- package/dist/dial-in/envelope.d.ts +95 -0
- package/dist/dial-in/envelope.js +89 -0
- package/dist/dial-in/gateway.d.ts +214 -0
- package/dist/{local-devices/local-device-gateway.js → dial-in/gateway.js} +337 -181
- package/dist/dial-in/handlers.d.ts +47 -0
- package/dist/dial-in/handlers.js +84 -0
- package/dist/dial-in/machines.d.ts +155 -0
- package/dist/dial-in/machines.js +383 -0
- package/dist/dial-in/relay.d.ts +135 -0
- package/dist/dial-in/relay.js +444 -0
- package/dist/dial-in/relay.module.d.ts +7 -0
- package/dist/dial-in/relay.module.js +24 -0
- package/dist/{local-devices/device-routes.d.ts → dial-in/routes.d.ts} +19 -11
- package/dist/{local-devices/device-routes.js → dial-in/routes.js} +45 -25
- package/dist/{local-devices → dial-in}/targets.d.ts +3 -18
- package/dist/{local-devices → dial-in}/targets.js +3 -48
- package/dist/errors.d.ts +8 -27
- package/dist/errors.js +8 -27
- package/dist/extensions/bootstrap.d.ts +5 -0
- package/dist/extensions/bootstrap.js +10 -1
- package/dist/extensions/extensions.module.js +6 -0
- package/dist/extensions/host.d.ts +6 -0
- package/dist/extensions/host.js +12 -2
- package/dist/extensions/permission-set.d.ts +15 -1
- package/dist/extensions/permission-set.js +11 -1
- package/dist/extensions/permission-view.d.ts +1 -1
- package/dist/extensions/permission-view.js +4 -1
- package/dist/http/api.module.js +4 -4
- package/dist/http/gateway-routes.d.ts +1 -1
- package/dist/http/gateway-routes.js +1 -1
- package/dist/http/meta.controller.js +1 -1
- package/dist/identity/settings.d.ts +1 -1
- package/dist/identity/settings.js +6 -8
- package/dist/infra/config/kernel-config.service.js +2 -2
- package/dist/infra/outbox/registry.d.ts +0 -14
- package/dist/infra/outbox/registry.js +0 -15
- package/dist/infra/throttler/named-throttlers.d.ts +1 -1
- package/dist/infra/throttler/named-throttlers.js +1 -1
- package/dist/kernel.module.js +0 -3
- package/dist/orgs/settings.service.js +5 -10
- package/dist/overrides.d.ts +0 -4
- package/dist/overrides.js +0 -2
- package/dist/platform/exclusions.js +5 -5
- package/dist/platform/ops/admin.ops.js +1 -1
- package/dist/platform/ops/conversations.ops.js +5 -2
- package/dist/platform/ops/devices.ops.js +11 -24
- package/dist/platform/ops/inbox.ops.js +1 -1
- package/dist/roles/gateway-role.d.ts +3 -3
- package/dist/roles/gateway-role.js +4 -4
- package/dist/roles/gateway.module.d.ts +1 -1
- package/dist/roles/gateway.module.js +5 -5
- package/dist/runtime/context/device-paths.d.ts +4 -1
- package/dist/runtime/context/permission-profile.js +5 -5
- package/dist/runtime/maintenance.d.ts +2 -5
- package/dist/runtime/maintenance.js +9 -19
- package/dist/runtime/project/knowledge.js +4 -5
- package/dist/runtime/project/knowledge.service.js +2 -1
- package/dist/sandbox/cloud-permission-snapshot.d.ts +1 -1
- package/dist/sandbox/cloud-permission-snapshot.js +1 -3
- package/dist/sandbox/device-reports.d.ts +74 -14
- package/dist/sandbox/device-reports.js +190 -33
- package/dist/sandbox/device-reports.module.d.ts +7 -0
- package/dist/sandbox/device-reports.module.js +25 -0
- package/dist/sandbox/device-sandbox.d.ts +39 -41
- package/dist/sandbox/device-sandbox.js +44 -83
- package/dist/sandbox/kernel-read.d.ts +2 -2
- package/dist/sandbox/legacy-retirement.d.ts +11 -59
- package/dist/sandbox/legacy-retirement.js +34 -186
- package/dist/sandbox/legacy-retirement.module.d.ts +2 -2
- package/dist/sandbox/legacy-retirement.module.js +2 -2
- package/dist/sandbox/managed-device-context.d.ts +16 -4
- package/dist/sandbox/managed-device-context.js +25 -8
- package/dist/sandbox/managed-device-context.module.js +4 -0
- package/dist/sandbox/modes.d.ts +3 -12
- package/dist/sandbox/modes.js +2 -14
- package/dist/{local-devices → sandbox}/network-grants.d.ts +2 -21
- package/dist/{local-devices → sandbox}/network-grants.js +4 -49
- package/dist/sandbox/operation-authority-types.d.ts +2 -1
- package/dist/sandbox/operation-authority.d.ts +6 -20
- package/dist/sandbox/operation-authority.js +43 -104
- package/dist/sandbox/operation-authority.module.js +1 -1
- package/dist/sandbox/operation-intents.d.ts +4 -1
- package/dist/sandbox/operation-intents.js +11 -2
- package/dist/sandbox/operation-reconcile.job.js +2 -4
- package/dist/sandbox/permission-availability.d.ts +6 -7
- package/dist/sandbox/permission-availability.js +7 -10
- package/dist/sandbox/permission-call-grants.d.ts +2 -1
- package/dist/sandbox/permission-call-grants.js +1 -7
- package/dist/sandbox/permission-call-types.d.ts +4 -4
- package/dist/sandbox/permission-ceiling-writes.d.ts +7 -23
- package/dist/sandbox/permission-ceiling-writes.js +8 -35
- package/dist/sandbox/permission-config-publication.d.ts +9 -1
- package/dist/sandbox/permission-config-publication.js +29 -21
- package/dist/sandbox/permission-context-types.d.ts +17 -10
- package/dist/sandbox/permission-facts.d.ts +98 -95
- package/dist/sandbox/permission-facts.js +160 -314
- package/dist/sandbox/permission-instance-writes.d.ts +23 -37
- package/dist/sandbox/permission-instance-writes.js +44 -121
- package/dist/sandbox/permission-runtime-reports.d.ts +38 -30
- package/dist/sandbox/permission-runtime-reports.js +105 -122
- package/dist/sandbox/providers.d.ts +26 -38
- package/dist/sandbox/providers.js +36 -18
- package/dist/sandbox/request-not-sent.d.ts +2 -13
- package/dist/sandbox/request-not-sent.js +2 -13
- package/dist/sandbox/review/permission-candidate.d.ts +13 -8
- package/dist/sandbox/review/permission-candidate.js +38 -48
- package/dist/sandbox/sandbox.module.js +7 -3
- package/dist/sandbox/sandbox.service.d.ts +49 -59
- package/dist/sandbox/sandbox.service.js +139 -259
- package/dist/schedules/fire.d.ts +15 -6
- package/dist/schedules/fire.js +27 -13
- package/dist/schedules/grant-rules.js +1 -1
- package/dist/schedules/scheduled-tasks.service.d.ts +0 -7
- package/dist/schedules/scheduled-tasks.service.js +1 -4
- package/dist/schedules/schedules.module.js +5 -3
- package/dist/skills/invoke.d.ts +1 -1
- package/dist/skills/invoke.js +2 -3
- package/dist/tools/approvals.service.d.ts +8 -5
- package/dist/tools/approvals.service.js +5 -5
- package/dist/tools/builtins/sandbox/bash.js +1 -2
- package/dist/tools/builtins/sandbox/common.d.ts +15 -9
- package/dist/tools/builtins/sandbox/common.js +37 -38
- package/dist/tools/network-asks.d.ts +19 -22
- package/dist/tools/network-asks.js +15 -21
- package/dist/tools/pipeline.d.ts +6 -7
- package/dist/tools/pipeline.js +17 -21
- package/dist/tools/tools.module.js +3 -7
- package/package.json +3 -4
- package/src/migrations/0177_dial_in_contract.sql +32 -0
- package/dist/local-devices/device-auth.module.d.ts +0 -7
- package/dist/local-devices/device-auth.module.js +0 -25
- package/dist/local-devices/device-policy.service.d.ts +0 -213
- package/dist/local-devices/device-policy.service.js +0 -605
- package/dist/local-devices/instance-bindings.d.ts +0 -82
- package/dist/local-devices/instance-bindings.js +0 -307
- package/dist/local-devices/local-device-gateway.d.ts +0 -153
- package/dist/local-devices/local-device-provider.d.ts +0 -122
- package/dist/local-devices/local-device-provider.js +0 -326
- package/dist/local-devices/local-devices.module.d.ts +0 -31
- package/dist/local-devices/local-devices.module.js +0 -100
- package/dist/local-devices/machines.d.ts +0 -211
- package/dist/local-devices/machines.js +0 -766
- package/dist/local-devices/owner-report.d.ts +0 -81
- package/dist/local-devices/owner-report.js +0 -150
- package/dist/local-devices/owner-reports.d.ts +0 -48
- package/dist/local-devices/owner-reports.js +0 -208
- package/dist/local-devices/policy-op-jobs.d.ts +0 -15
- package/dist/local-devices/policy-op-jobs.js +0 -12
- package/dist/local-devices/policy-summary.d.ts +0 -102
- package/dist/local-devices/policy-summary.js +0 -155
- package/dist/local-devices/relay.d.ts +0 -166
- package/dist/local-devices/relay.js +0 -559
- package/dist/tools/device-op-resolver.d.ts +0 -34
- package/dist/tools/device-op-resolver.js +0 -120
- /package/dist/{local-devices → dial-in}/keys.d.ts +0 -0
- /package/dist/{local-devices → dial-in}/keys.js +0 -0
- /package/dist/{local-devices → sandbox}/network-grants.module.d.ts +0 -0
- /package/dist/{local-devices → sandbox}/network-grants.module.js +0 -0
|
@@ -1,766 +0,0 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
-
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
-
};
|
|
10
|
-
import { randomBytes } from "node:crypto";
|
|
11
|
-
import { parseDeviceFrame, } from "@capybara-hq/plugin-sdk";
|
|
12
|
-
import { Inject, Injectable } from "@nestjs/common";
|
|
13
|
-
import { sql } from "kysely";
|
|
14
|
-
import { audit } from "../identity/audit.js";
|
|
15
|
-
import { sha256Hex } from "../identity/hash.js";
|
|
16
|
-
import { afterCommit } from "../infra/database/after-commit.js";
|
|
17
|
-
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
18
|
-
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
19
|
-
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
20
|
-
import { OutboxService } from "../infra/outbox/outbox.service.js";
|
|
21
|
-
import { KERNEL_BUS, KERNEL_LOGGER } from "../infra/tokens.js";
|
|
22
|
-
import { MAINTENANCE_QUEUE_NAME } from "../runtime/maintenance-queue.js";
|
|
23
|
-
import { LegacyRetirementStore } from "../sandbox/legacy-retirement.js";
|
|
24
|
-
import { PermissionCeilingWrites } from "../sandbox/permission-ceiling-writes.js";
|
|
25
|
-
import { PermissionRuntimeReports, sameRuntimeIdentity, } from "../sandbox/permission-runtime-reports.js";
|
|
26
|
-
import { LOCAL_PROVIDER } from "../sandbox/providers.js";
|
|
27
|
-
import { deviceNotFound } from "../sandbox/sandbox.service.js";
|
|
28
|
-
import { deviceRevokedTopic } from "./keys.js";
|
|
29
|
-
import { OwnerReports } from "./owner-reports.js";
|
|
30
|
-
import { DEVICE_OP_RESOLVE_JOB, deviceOpReportedJobId } from "./policy-op-jobs.js";
|
|
31
|
-
import { deviceFeaturesOf, localPolicyDiff, localPolicyOf, opExplains, opVisibleIn, ownerPolicyRevokes, pendingPolicyOpsOf, } from "./policy-summary.js";
|
|
32
|
-
import { LocalDeviceRelay } from "./relay.js";
|
|
33
|
-
/** The machine credential's prefix (`cap_dev_` plus 32 random bytes, base64url). */
|
|
34
|
-
export const MACHINE_CREDENTIAL_PREFIX = "cap_dev_";
|
|
35
|
-
const CREDENTIAL_RE = /^cap_dev_[A-Za-z0-9_-]{43}$/;
|
|
36
|
-
/** How long the `stop` frame told to a revoked machine may take (it only closes the connection). */
|
|
37
|
-
const STOP_DEADLINE_MS = 30_000;
|
|
38
|
-
/**
|
|
39
|
-
* Connected machines and their credentials (environments spec §13.2). One active machine per
|
|
40
|
-
* local device: issuing a credential revokes the device's previous machine. A
|
|
41
|
-
* credential is `cap_dev_` plus 32 random bytes; only its sha256 is stored. It is accepted only
|
|
42
|
-
* by the device connection route ({@link authenticate}), never by `/api`: it is no `Principal`.
|
|
43
|
-
*
|
|
44
|
-
* A revoked machine is told so after the commit: `stop { reason }` relayed while a replica holds
|
|
45
|
-
* its connection (fire-and-forget, ruling R14) and `device.revoked.<deviceId>` published with
|
|
46
|
-
* `{ machineId }`, so the holder closes that machine's connection.
|
|
47
|
-
*/
|
|
48
|
-
/** Roots in a fixed key order (null when absent), whatever order jsonb stored them in. */
|
|
49
|
-
const canonicalRoots = (roots) => {
|
|
50
|
-
if (!roots || typeof roots !== "object")
|
|
51
|
-
return "null";
|
|
52
|
-
const r = roots;
|
|
53
|
-
return JSON.stringify(Object.keys(r)
|
|
54
|
-
.sort()
|
|
55
|
-
.map((k) => [k, r[k]]));
|
|
56
|
-
};
|
|
57
|
-
let MachinesService = class MachinesService {
|
|
58
|
-
db;
|
|
59
|
-
txHost;
|
|
60
|
-
relay;
|
|
61
|
-
bus;
|
|
62
|
-
outbox;
|
|
63
|
-
log;
|
|
64
|
-
ceilings;
|
|
65
|
-
reports;
|
|
66
|
-
owners;
|
|
67
|
-
retirement;
|
|
68
|
-
constructor(db, txHost, relay, bus, outbox, log, ceilings, reports, owners,
|
|
69
|
-
/** The host's boot identity: restart evidence for local legacy obligations (8.5). */
|
|
70
|
-
retirement) {
|
|
71
|
-
this.db = db;
|
|
72
|
-
this.txHost = txHost;
|
|
73
|
-
this.relay = relay;
|
|
74
|
-
this.bus = bus;
|
|
75
|
-
this.outbox = outbox;
|
|
76
|
-
this.log = log;
|
|
77
|
-
this.ceilings = ceilings;
|
|
78
|
-
this.reports = reports;
|
|
79
|
-
this.owners = owners;
|
|
80
|
-
this.retirement = retirement;
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* A new machine for the device and its credential (returned once). Revokes the
|
|
84
|
-
* device's active machine first. Joins the caller's transaction (the device confirmation),
|
|
85
|
-
* which has already checked the device is the person's own live local device.
|
|
86
|
-
*/
|
|
87
|
-
async issue(input) {
|
|
88
|
-
// the machine the device was last served from (revoked or not)
|
|
89
|
-
const prev = await this.db
|
|
90
|
-
.selectFrom("device_machines")
|
|
91
|
-
.select(["name", "os", "arch"])
|
|
92
|
-
.where("device_id", "=", input.deviceId)
|
|
93
|
-
.orderBy("created_at", "desc")
|
|
94
|
-
.orderBy("id", "desc")
|
|
95
|
-
.limit(1)
|
|
96
|
-
.executeTakeFirst();
|
|
97
|
-
// another machine has other files (maintainer decision 2026-11-08)
|
|
98
|
-
const moved = prev !== undefined && !sameMachine(prev, input);
|
|
99
|
-
// no stop frame: it would wait on the list and could reach the replacement machine instead
|
|
100
|
-
await this.revokeActive(input.deviceId, null);
|
|
101
|
-
await this.resetInstance(input.deviceId, moved);
|
|
102
|
-
const credential = MACHINE_CREDENTIAL_PREFIX + randomBytes(32).toString("base64url");
|
|
103
|
-
const row = await this.db
|
|
104
|
-
.insertInto("device_machines")
|
|
105
|
-
.values({
|
|
106
|
-
org_id: input.orgId,
|
|
107
|
-
device_id: input.deviceId,
|
|
108
|
-
account_id: input.accountId,
|
|
109
|
-
credential_hash: sha256Hex(credential),
|
|
110
|
-
name: input.name,
|
|
111
|
-
os: input.os,
|
|
112
|
-
arch: input.arch,
|
|
113
|
-
program_version: input.programVersion,
|
|
114
|
-
})
|
|
115
|
-
.returning("id")
|
|
116
|
-
.executeTakeFirstOrThrow();
|
|
117
|
-
return { machineId: row.id, credential };
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Ruling R23: the new machine may serve the device from another directory or mode (other
|
|
121
|
-
* roots), and a login recreates a directory that was lost (ruling R24). So the instance row
|
|
122
|
-
* forgets its instance (status `none`, no handle) and its files count as there again; the next
|
|
123
|
-
* use creates a fresh instance on the machine (with its roots: a new `device_paths` event
|
|
124
|
-
* follows). The fence moves on, so a lifecycle holder of the old instance fails its next write.
|
|
125
|
-
* The reset count (`fs_resets`) moves on when the files the next instance sees are not the old
|
|
126
|
-
* ones, so conversations that used the old files are told their files were reset, not that they
|
|
127
|
-
* were kept: lost files come back as an empty directory, and another machine (`moved`: name, OS
|
|
128
|
-
* or arch differ from the device's previous machine) has other files. Another directory on
|
|
129
|
-
* the same machine is known only from the new machine's first hello ({@link recordHello}).
|
|
130
|
-
*/
|
|
131
|
-
async resetInstance(deviceId, moved) {
|
|
132
|
-
await this.db
|
|
133
|
-
.updateTable("device_instances")
|
|
134
|
-
.set((eb) => ({
|
|
135
|
-
status: "none",
|
|
136
|
-
handle: null,
|
|
137
|
-
fs_status: sql `CASE WHEN fs_handle IS NULL THEN 'none' ELSE 'ready' END`,
|
|
138
|
-
running_since: null,
|
|
139
|
-
suspended_at: null,
|
|
140
|
-
destroyed_at: sql `now()`,
|
|
141
|
-
generation: eb("generation", "+", 1),
|
|
142
|
-
fs_resets: moved
|
|
143
|
-
? sql `fs_resets + 1`
|
|
144
|
-
: sql `CASE WHEN fs_status = 'lost' THEN fs_resets + 1 ELSE fs_resets END`,
|
|
145
|
-
}))
|
|
146
|
-
.where("device_id", "=", deviceId)
|
|
147
|
-
.execute();
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Who a machine credential stands for, or null: the machine is not revoked, its device is
|
|
151
|
-
* live, the org is not deleted (ruling R12), and the person is an active account still a member
|
|
152
|
-
* of the org. Read live on every call (a connection re-checks it as it sees fit).
|
|
153
|
-
*/
|
|
154
|
-
async authenticate(credential) {
|
|
155
|
-
if (!CREDENTIAL_RE.test(credential))
|
|
156
|
-
return null;
|
|
157
|
-
const row = await this.db
|
|
158
|
-
.selectFrom("device_machines as m")
|
|
159
|
-
.innerJoin("devices as e", "e.id", "m.device_id")
|
|
160
|
-
.innerJoin("orgs as o", "o.id", "m.org_id")
|
|
161
|
-
.innerJoin("memberships as ms", (j) => j.onRef("ms.org_id", "=", "m.org_id").onRef("ms.account_id", "=", "m.account_id"))
|
|
162
|
-
.innerJoin("accounts as a", "a.id", "m.account_id")
|
|
163
|
-
.select(["m.id", "m.org_id", "m.device_id", "m.account_id"])
|
|
164
|
-
.where("m.credential_hash", "=", sha256Hex(credential))
|
|
165
|
-
.where("m.revoked_at", "is", null)
|
|
166
|
-
.whereRef("e.org_id", "=", "m.org_id")
|
|
167
|
-
.where("e.deleted_at", "is", null)
|
|
168
|
-
.where("e.source", "=", "local")
|
|
169
|
-
.where("o.deleted_at", "is", null)
|
|
170
|
-
.where("a.status", "=", "active")
|
|
171
|
-
.executeTakeFirst();
|
|
172
|
-
return row
|
|
173
|
-
? {
|
|
174
|
-
machineId: row.id,
|
|
175
|
-
orgId: row.org_id,
|
|
176
|
-
deviceId: row.device_id,
|
|
177
|
-
accountId: row.account_id,
|
|
178
|
-
}
|
|
179
|
-
: null;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Stores what the program reported in its hello frame and marks the machine seen. On the
|
|
183
|
-
* machine's first hello, when it is the same machine as the device's previous one (else
|
|
184
|
-
* {@link issue} already counted the reset) but serves another directory or mode (other roots,
|
|
185
|
-
* ruling R23), the device's files are other files: `fs_resets` moves on (maintainer
|
|
186
|
-
* decision 2026-11-08), so conversations get the files-reset notice. A previous machine that
|
|
187
|
-
* never said hello has no known roots: nothing to compare. The reported features (known ones
|
|
188
|
-
* only) and local policy summary (null when absent or malformed) replace the stored ones, so a
|
|
189
|
-
* program downgraded to one without them is no longer sent what it cannot do. The summary is the
|
|
190
|
-
* device's sandbox allowlist; a change of what the owner sets, made on the machine while it was
|
|
191
|
-
* away, is audited `device.policy_changed` (the owner ceiling's, `device.owner_ceiling_changed`)
|
|
192
|
-
* ({@link reportedPolicy}). The outcomes of its recent policy ops (`recentOps`) backfill the
|
|
193
|
-
* platform's ops still pending on the row ({@link backfillOps}). The owner's home directory
|
|
194
|
-
* (`home`) replaces the stored one too (null when the hello has none).
|
|
195
|
-
*/
|
|
196
|
-
async recordHello(machineId, hello,
|
|
197
|
-
/** A protocol 4 hello's authenticated owner report (B2): it, not the summary, decides publication. */
|
|
198
|
-
owner) {
|
|
199
|
-
const subject = await this.db
|
|
200
|
-
.selectFrom("device_machines")
|
|
201
|
-
.select(["org_id", "device_id"])
|
|
202
|
-
.where("id", "=", machineId)
|
|
203
|
-
.where("revoked_at", "is", null)
|
|
204
|
-
.executeTakeFirst();
|
|
205
|
-
if (!subject)
|
|
206
|
-
return;
|
|
207
|
-
await this.ceilings.lock({ orgId: subject.org_id, deviceId: subject.device_id }, "device");
|
|
208
|
-
const self = await this.db
|
|
209
|
-
.selectFrom("device_machines")
|
|
210
|
-
.select([
|
|
211
|
-
"device_id",
|
|
212
|
-
"org_id",
|
|
213
|
-
"account_id",
|
|
214
|
-
"name",
|
|
215
|
-
"os",
|
|
216
|
-
"arch",
|
|
217
|
-
"roots",
|
|
218
|
-
"created_at",
|
|
219
|
-
"local_policy",
|
|
220
|
-
"pending_policy_ops",
|
|
221
|
-
])
|
|
222
|
-
.where("id", "=", machineId)
|
|
223
|
-
.where("revoked_at", "is", null)
|
|
224
|
-
.where("org_id", "=", subject.org_id)
|
|
225
|
-
.where("device_id", "=", subject.device_id)
|
|
226
|
-
.forUpdate()
|
|
227
|
-
.executeTakeFirst();
|
|
228
|
-
if (self === undefined)
|
|
229
|
-
return;
|
|
230
|
-
if (self.roots === null) {
|
|
231
|
-
const prev = await this.db
|
|
232
|
-
.selectFrom("device_machines")
|
|
233
|
-
.select(["name", "os", "arch", "mode"])
|
|
234
|
-
.select(sql `roots = ${JSON.stringify(hello.roots)}::jsonb`.as("same_roots"))
|
|
235
|
-
.where("device_id", "=", self.device_id)
|
|
236
|
-
.where("id", "!=", machineId)
|
|
237
|
-
.where("created_at", "<=", self.created_at)
|
|
238
|
-
.where("roots", "is not", null)
|
|
239
|
-
.orderBy("created_at", "desc")
|
|
240
|
-
.orderBy("id", "desc")
|
|
241
|
-
.limit(1)
|
|
242
|
-
.executeTakeFirst();
|
|
243
|
-
if (prev !== undefined &&
|
|
244
|
-
sameMachine(prev, self) &&
|
|
245
|
-
(!prev.same_roots || prev.mode !== hello.mode)) {
|
|
246
|
-
await this.db
|
|
247
|
-
.updateTable("device_instances")
|
|
248
|
-
.set({ fs_resets: sql `fs_resets + 1` })
|
|
249
|
-
.where("device_id", "=", self.device_id)
|
|
250
|
-
.execute();
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
const policy = await this.reportedPolicy(machineId, self, hello.policy);
|
|
254
|
-
// jsonb returns keys in its own order: compare roots canonically, or every reconnect would
|
|
255
|
-
// look like a changed reach and reset Full consent (B4 consent stability)
|
|
256
|
-
const rootsChanged = canonicalRoots(self.roots) !== canonicalRoots(hello.roots);
|
|
257
|
-
if (owner !== undefined)
|
|
258
|
-
await this.owners.recordLocked(machineId, owner.report, {
|
|
259
|
-
connectionId: owner.connectionId,
|
|
260
|
-
via: "hello",
|
|
261
|
-
rootsChanged,
|
|
262
|
-
});
|
|
263
|
-
else if (rootsChanged ||
|
|
264
|
-
JSON.stringify(localPolicyOf(self.local_policy)) !== JSON.stringify(policy))
|
|
265
|
-
await this.ceilings.publishLocked({ orgId: self.org_id, deviceId: self.device_id }, "device", (scope, row) => rootsChanged ||
|
|
266
|
-
ownerPolicyRevokes(localPolicyOf(policy), scope, row.method, row.granted_hosts), rootsChanged);
|
|
267
|
-
await this.db
|
|
268
|
-
.updateTable("device_machines")
|
|
269
|
-
.set({
|
|
270
|
-
program_version: hello.programVersion,
|
|
271
|
-
os: hello.os,
|
|
272
|
-
arch: hello.arch,
|
|
273
|
-
mode: hello.mode,
|
|
274
|
-
roots: JSON.stringify(hello.roots),
|
|
275
|
-
session_isolation: hello.sessionIsolation,
|
|
276
|
-
features: deviceFeaturesOf(hello.features),
|
|
277
|
-
local_policy: jsonOrNull(policy),
|
|
278
|
-
// the base of an auto-mode project's relative path (project root §4.2); absent: unknown
|
|
279
|
-
home: hello.home ?? null,
|
|
280
|
-
last_seen_at: sql `now()`,
|
|
281
|
-
})
|
|
282
|
-
.where("id", "=", machineId)
|
|
283
|
-
.where("revoked_at", "is", null)
|
|
284
|
-
.execute();
|
|
285
|
-
if (hello.recentOps !== undefined) {
|
|
286
|
-
await this.backfillOps(machineId, self, hello.recentOps, localPolicyOf(hello.policy));
|
|
287
|
-
}
|
|
288
|
-
// only an authenticated, claimed connection's hello reaches here (the machine row locked):
|
|
289
|
-
// its boot identity is the evidence a restart ended the old processes
|
|
290
|
-
if (hello.bootId !== undefined)
|
|
291
|
-
await this.retirement.recordBoot(machineId, hello.bootId);
|
|
292
|
-
// a hello without one (a downgraded program) leaves no stale boot as evidence (M2)
|
|
293
|
-
else
|
|
294
|
-
await this.retirement.clearBoot(machineId);
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Hello backfill (transactional outbox spec §7): a pending op of the platform's whose outcome
|
|
298
|
-
* the hello reports (`applied`, `refused`), or that it does not list although the op can no
|
|
299
|
-
* longer reach the machine (`absent`: past its `resolveAt`), is marked `reported` and owes a
|
|
300
|
-
* `device-op-resolve` job (`device-op-reported-<opId>`) that records the outcome. An op the
|
|
301
|
-
* machine logged only as `started` (it stopped between the log and the answer) is judged once
|
|
302
|
-
* past its `resolveAt` by the policy the hello reports: its change there → `applied`, else
|
|
303
|
-
* `interrupted` (within the window, or with no readable policy, it is left). An expired entry
|
|
304
|
-
* (past `until`) is skipped: the machine's reports are no longer the op's. Runs in the caller's
|
|
305
|
-
* transaction, with the machine's row locked; an op already reported is not owed again.
|
|
306
|
-
*/
|
|
307
|
-
async backfillOps(machineId, self, recentOps, policy) {
|
|
308
|
-
const outcomes = new Map(recentOps.map((r) => [r.opId, r.outcome]));
|
|
309
|
-
const now = Date.now();
|
|
310
|
-
const reported = new Map();
|
|
311
|
-
for (const op of pendingPolicyOpsOf(self.pending_policy_ops)) {
|
|
312
|
-
if (op.reported !== undefined || op.until <= now)
|
|
313
|
-
continue;
|
|
314
|
-
const outcome = outcomes.get(op.id);
|
|
315
|
-
if (outcome === "applied" || outcome === "refused")
|
|
316
|
-
reported.set(op.id, { outcome });
|
|
317
|
-
else if (now < op.resolveAt)
|
|
318
|
-
continue;
|
|
319
|
-
else if (outcome === undefined)
|
|
320
|
-
reported.set(op.id, { outcome: "absent" });
|
|
321
|
-
else if (policy !== null) {
|
|
322
|
-
reported.set(op.id, { outcome: opVisibleIn(op, policy) ? "applied" : "interrupted" });
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
if (reported.size === 0)
|
|
326
|
-
return;
|
|
327
|
-
const ops = Array.isArray(self.pending_policy_ops) ? self.pending_policy_ops : [];
|
|
328
|
-
const next = ops.map((e) => {
|
|
329
|
-
const id = e?.id;
|
|
330
|
-
const r = typeof id === "string" ? reported.get(id) : undefined;
|
|
331
|
-
return r === undefined ? e : { ...e, reported: r };
|
|
332
|
-
});
|
|
333
|
-
await this.db
|
|
334
|
-
.updateTable("device_machines")
|
|
335
|
-
.set({ pending_policy_ops: JSON.stringify(next) })
|
|
336
|
-
.where("id", "=", machineId)
|
|
337
|
-
.execute();
|
|
338
|
-
for (const opId of reported.keys()) {
|
|
339
|
-
await this.outbox.add({
|
|
340
|
-
queue: MAINTENANCE_QUEUE_NAME,
|
|
341
|
-
name: DEVICE_OP_RESOLVE_JOB,
|
|
342
|
-
jobId: deviceOpReportedJobId(opId),
|
|
343
|
-
data: { machineId, opId },
|
|
344
|
-
orgId: self.org_id,
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
/**
|
|
349
|
-
* A protocol 4 hello on an authenticated connection: validated as a frame, then recorded like
|
|
350
|
-
* {@link recordHello} with its owner report and the connection it arrived on.
|
|
351
|
-
*/
|
|
352
|
-
async recordHelloV3(machineId, hello, connectionId = null) {
|
|
353
|
-
const parsed = parseDeviceFrame(JSON.stringify(hello), "device");
|
|
354
|
-
if (parsed.type !== "hello")
|
|
355
|
-
throw new Error("Expected a validated operation hello");
|
|
356
|
-
await this.recordHello(machineId, parsed, {
|
|
357
|
-
report: parsed.ownerCeiling,
|
|
358
|
-
connectionId,
|
|
359
|
-
});
|
|
360
|
-
const machine = await this.db
|
|
361
|
-
.selectFrom("device_machines")
|
|
362
|
-
.select(["org_id", "device_id"])
|
|
363
|
-
.where("id", "=", machineId)
|
|
364
|
-
.where("revoked_at", "is", null)
|
|
365
|
-
.executeTakeFirst();
|
|
366
|
-
if (!machine)
|
|
367
|
-
return;
|
|
368
|
-
const subject = { orgId: machine.org_id, deviceId: machine.device_id };
|
|
369
|
-
await this.ceilings.lock(subject, "device");
|
|
370
|
-
const attested = await this.boundIdentityLocked(subject, machineId, parsed, connectionId);
|
|
371
|
-
// the hello predates the binding its own connection installed: that install recorded the
|
|
372
|
-
// report, which this hello's must not overwrite
|
|
373
|
-
if (attested === "installed-here")
|
|
374
|
-
return;
|
|
375
|
-
await this.reports.record(machine.device_id, LOCAL_PROVIDER, parsed.programVersion, attested, parsed.permissionSupport);
|
|
376
|
-
}
|
|
377
|
-
/**
|
|
378
|
-
* The runtime identity a v3 hello may attest (B4): only a bound hello re-reporting exactly the
|
|
379
|
-
* identity of the current instance generation's installed binding. An unbound hello, another
|
|
380
|
-
* generation or a binding still awaiting the device attests nothing (unprobed; it never finishes
|
|
381
|
-
* an installation, which only the bindInstance answer does).
|
|
382
|
-
*
|
|
383
|
-
* The machine the current generation was installed for no longer attesting it means its program
|
|
384
|
-
* can never serve that generation again: a restarted program whose verified runtime differs from
|
|
385
|
-
* the one its stored binding attested (every capybara-device upgrade changes the runtime bytes)
|
|
386
|
-
* starts unbound, and the device refuses to rebind a generation under a changed runtime. The
|
|
387
|
-
* instance is then replaced ({@link replaceUnattestedLocked}), so the next use creates and binds
|
|
388
|
-
* a new generation under the new evidence instead of staying unprobed until a new login.
|
|
389
|
-
*
|
|
390
|
-
* A connection answers bindInstance before its own hello is recorded (the hello is recorded
|
|
391
|
-
* after `ready`), so a binding installed on the hello's own connection is newer than the hello:
|
|
392
|
-
* it is left as it is (`installed-here`), never replaced by the evidence the hello predates.
|
|
393
|
-
*/
|
|
394
|
-
async boundIdentityLocked(subject, machineId, hello, connectionId) {
|
|
395
|
-
const instance = await this.db
|
|
396
|
-
.selectFrom("device_instances")
|
|
397
|
-
.select(["provider", "status", "handle", "created_at", "permission_runtime_identity"])
|
|
398
|
-
.where("device_id", "=", subject.deviceId)
|
|
399
|
-
.executeTakeFirst();
|
|
400
|
-
if (!instance ||
|
|
401
|
-
instance.provider !== LOCAL_PROVIDER ||
|
|
402
|
-
!instance.handle ||
|
|
403
|
-
(instance.status !== "running" && instance.status !== "suspended"))
|
|
404
|
-
return undefined;
|
|
405
|
-
const generation = String(instance.created_at.getTime());
|
|
406
|
-
const binding = await this.db
|
|
407
|
-
.selectFrom("device_instance_bindings")
|
|
408
|
-
.select(["status", "runtime_identity", "machine_id", "installed_connection_id"])
|
|
409
|
-
.where("device_id", "=", subject.deviceId)
|
|
410
|
-
.where("instance_generation", "=", generation)
|
|
411
|
-
.executeTakeFirst();
|
|
412
|
-
// not installed yet (the kernel's bindInstance finishes it), or installed for another machine
|
|
413
|
-
// (a login replaced the instance already): nothing to attest, nothing to replace
|
|
414
|
-
if (binding?.status !== "installed" || !binding.runtime_identity)
|
|
415
|
-
return undefined;
|
|
416
|
-
if (binding.machine_id !== machineId)
|
|
417
|
-
return undefined;
|
|
418
|
-
if (connectionId !== null && binding.installed_connection_id === connectionId)
|
|
419
|
-
return "installed-here";
|
|
420
|
-
const reported = hello.runtimeIdentity;
|
|
421
|
-
if (hello.instanceGeneration === generation &&
|
|
422
|
-
reported?.instanceGeneration === generation &&
|
|
423
|
-
sameRuntimeIdentity(binding.runtime_identity, reported))
|
|
424
|
-
return reported;
|
|
425
|
-
await this.replaceUnattestedLocked(subject);
|
|
426
|
-
return undefined;
|
|
427
|
-
}
|
|
428
|
-
/**
|
|
429
|
-
* Replaces a local instance whose installed binding its machine no longer attests (under the
|
|
430
|
-
* device authority writer): the row forgets its instance (status `none`, no handle) and its
|
|
431
|
-
* fence moves on, so a lifecycle holder of the old instance fails its next write; the device's
|
|
432
|
-
* admission epoch advances, revoking every admitted or started operation of the old generation.
|
|
433
|
-
* The files stay (the same machine and directory): the next use creates a new instance on them,
|
|
434
|
-
* which the kernel binds under the program's new runtime evidence. Full consent is left to that
|
|
435
|
-
* install, which resets it when the proven environment fingerprint changed: a fingerprint a
|
|
436
|
-
* hello merely names proves nothing, and nothing is admitted before the install records a
|
|
437
|
-
* report, so resetting here too would reset twice.
|
|
438
|
-
*/
|
|
439
|
-
async replaceUnattestedLocked(subject) {
|
|
440
|
-
await this.ceilings.publishLocked(subject, "device", true, false);
|
|
441
|
-
await this.db
|
|
442
|
-
.updateTable("device_instances")
|
|
443
|
-
.set((eb) => ({
|
|
444
|
-
status: "none",
|
|
445
|
-
handle: null,
|
|
446
|
-
running_since: null,
|
|
447
|
-
suspended_at: null,
|
|
448
|
-
destroyed_at: sql `now()`,
|
|
449
|
-
generation: eb("generation", "+", 1),
|
|
450
|
-
permission_runtime_identity: null,
|
|
451
|
-
}))
|
|
452
|
-
.where("device_id", "=", subject.deviceId)
|
|
453
|
-
.execute();
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Stores the local policy summary a connected program pushed after its hello (a `policy` frame:
|
|
457
|
-
* its policy file changed on the machine: its sandbox allowlist), read and audited as the hello's
|
|
458
|
-
* ({@link recordHello}). A change the owner made on the machine is audited
|
|
459
|
-
* `device.policy_changed`. The next sandbox call then records a
|
|
460
|
-
* new `device_paths` event when the facts the model is told changed. A revoked machine is left
|
|
461
|
-
* alone.
|
|
462
|
-
*/
|
|
463
|
-
async recordPolicy(machineId, summary) {
|
|
464
|
-
const subject = await this.db
|
|
465
|
-
.selectFrom("device_machines")
|
|
466
|
-
.select(["org_id", "device_id"])
|
|
467
|
-
.where("id", "=", machineId)
|
|
468
|
-
.where("revoked_at", "is", null)
|
|
469
|
-
.executeTakeFirst();
|
|
470
|
-
if (!subject)
|
|
471
|
-
return;
|
|
472
|
-
await this.ceilings.lock({ orgId: subject.org_id, deviceId: subject.device_id }, "device");
|
|
473
|
-
const self = await this.db
|
|
474
|
-
.selectFrom("device_machines")
|
|
475
|
-
.select(["device_id", "org_id", "account_id", "local_policy", "pending_policy_ops"])
|
|
476
|
-
.where("id", "=", machineId)
|
|
477
|
-
.where("revoked_at", "is", null)
|
|
478
|
-
.where("org_id", "=", subject.org_id)
|
|
479
|
-
.where("device_id", "=", subject.device_id)
|
|
480
|
-
.forUpdate()
|
|
481
|
-
.executeTakeFirst();
|
|
482
|
-
if (self === undefined)
|
|
483
|
-
return;
|
|
484
|
-
const policy = await this.reportedPolicy(machineId, self, summary);
|
|
485
|
-
if (JSON.stringify(localPolicyOf(self.local_policy)) !== JSON.stringify(policy))
|
|
486
|
-
await this.ceilings.publishLocked({ orgId: self.org_id, deviceId: self.device_id }, "device", (scope, row) => ownerPolicyRevokes(localPolicyOf(policy), scope, row.method, row.granted_hosts), false);
|
|
487
|
-
await this.db
|
|
488
|
-
.updateTable("device_machines")
|
|
489
|
-
.set({ local_policy: jsonOrNull(policy), last_seen_at: sql `now()` })
|
|
490
|
-
.where("id", "=", machineId)
|
|
491
|
-
.where("revoked_at", "is", null)
|
|
492
|
-
.execute();
|
|
493
|
-
}
|
|
494
|
-
/**
|
|
495
|
-
* A staged (v3) `policy` frame (B2): the summary is kept for the device page like protocol 2's,
|
|
496
|
-
* while the authenticated owner report alone decides what changed ({@link OwnerReports}).
|
|
497
|
-
*/
|
|
498
|
-
async recordPolicyV3(machineId, frame, connectionId = null) {
|
|
499
|
-
const parsed = parseDeviceFrame(JSON.stringify(frame), "device");
|
|
500
|
-
if (parsed.type !== "policy")
|
|
501
|
-
throw new Error("Expected a validated policy frame");
|
|
502
|
-
const subject = await this.db
|
|
503
|
-
.selectFrom("device_machines")
|
|
504
|
-
.select(["org_id", "device_id", "account_id", "local_policy", "pending_policy_ops"])
|
|
505
|
-
.where("id", "=", machineId)
|
|
506
|
-
.where("revoked_at", "is", null)
|
|
507
|
-
.executeTakeFirst();
|
|
508
|
-
if (!subject)
|
|
509
|
-
return;
|
|
510
|
-
await this.ceilings.lock({ orgId: subject.org_id, deviceId: subject.device_id }, "device");
|
|
511
|
-
const policy = await this.reportedPolicy(machineId, { ...subject, device_id: subject.device_id }, parsed.policy);
|
|
512
|
-
await this.db
|
|
513
|
-
.updateTable("device_machines")
|
|
514
|
-
.set({ local_policy: jsonOrNull(policy), last_seen_at: sql `now()` })
|
|
515
|
-
.where("id", "=", machineId)
|
|
516
|
-
.where("revoked_at", "is", null)
|
|
517
|
-
.execute();
|
|
518
|
-
await this.owners.recordLocked(machineId, parsed.ownerCeiling, {
|
|
519
|
-
connectionId,
|
|
520
|
-
via: "policy",
|
|
521
|
-
});
|
|
522
|
-
}
|
|
523
|
-
/**
|
|
524
|
-
* The summary to store for one a program reported (hello or `policy` frame): the parsed one, else
|
|
525
|
-
* null. When both the stored and the reported summary are readable and what the owner sets
|
|
526
|
-
* changed ({@link localPolicyDiff}) by no `policy` op the platform sent ({@link opExplains}: its
|
|
527
|
-
* answer may come after the machine's own report), the owner changed it on the machine
|
|
528
|
-
* (`capybara-device policy add|remove allowedDomains ...`): the host changes are audited
|
|
529
|
-
* `device.policy_changed`, `via: "device"`, so the device page lists them beside the platform's
|
|
530
|
-
* changes (an owner ceiling change is the owner report writer's, `device.owner_ceiling_changed`);
|
|
531
|
-
* one an op explains is not audited here (the op's answer is). A summary reported again is no change. Runs in the caller's transaction,
|
|
532
|
-
* with the machine's row locked.
|
|
533
|
-
*/
|
|
534
|
-
async reportedPolicy(machineId, self, reported) {
|
|
535
|
-
const summary = localPolicyOf(reported);
|
|
536
|
-
const prev = localPolicyOf(self.local_policy);
|
|
537
|
-
const diff = summary !== null && prev !== null ? localPolicyDiff(prev, summary) : null;
|
|
538
|
-
if (diff !== null) {
|
|
539
|
-
const now = Date.now();
|
|
540
|
-
const byOp = pendingPolicyOpsOf(self.pending_policy_ops).some((op) => op.until > now && opExplains(op, diff));
|
|
541
|
-
if (byOp)
|
|
542
|
-
return summary;
|
|
543
|
-
const base = {
|
|
544
|
-
actorAccountId: self.account_id,
|
|
545
|
-
orgId: self.org_id,
|
|
546
|
-
target: self.device_id,
|
|
547
|
-
};
|
|
548
|
-
if (diff.add.length > 0 || diff.remove.length > 0) {
|
|
549
|
-
await audit(this.db.db, {
|
|
550
|
-
...base,
|
|
551
|
-
action: "device.policy_changed",
|
|
552
|
-
detail: {
|
|
553
|
-
add: diff.add,
|
|
554
|
-
remove: diff.remove,
|
|
555
|
-
by: self.account_id,
|
|
556
|
-
via: "device",
|
|
557
|
-
machineId,
|
|
558
|
-
},
|
|
559
|
-
});
|
|
560
|
-
}
|
|
561
|
-
return summary;
|
|
562
|
-
}
|
|
563
|
-
return summary;
|
|
564
|
-
}
|
|
565
|
-
/** Marks the machine seen (a ping); writes at most once every 60 seconds. */
|
|
566
|
-
async touch(machineId) {
|
|
567
|
-
await this.db
|
|
568
|
-
.updateTable("device_machines")
|
|
569
|
-
.set({ last_seen_at: sql `now()` })
|
|
570
|
-
.where("id", "=", machineId)
|
|
571
|
-
.where("revoked_at", "is", null)
|
|
572
|
-
.where((eb) => eb.or([
|
|
573
|
-
eb("last_seen_at", "is", null),
|
|
574
|
-
eb("last_seen_at", "<", sql `now() - interval '60 seconds'`),
|
|
575
|
-
]))
|
|
576
|
-
.execute();
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* Disconnects the machine of one of the person's own live local devices (`DELETE
|
|
580
|
-
* /orgs/:orgId/devices/:id/machine`): revokes it and audits
|
|
581
|
-
* `device.machine_revoked`. Anyone else's device, a cloud one or a missing one →
|
|
582
|
-
* `device.not_found`; no machine connected is a no-op.
|
|
583
|
-
*/
|
|
584
|
-
async revoke(orgId, accountId, deviceId, actorTokenId) {
|
|
585
|
-
const env = await this.db
|
|
586
|
-
.selectFrom("devices")
|
|
587
|
-
.select("id")
|
|
588
|
-
.where("id", "=", deviceId)
|
|
589
|
-
.where("org_id", "=", orgId)
|
|
590
|
-
.where("owner_kind", "=", "personal")
|
|
591
|
-
.where("owner_account_id", "=", accountId)
|
|
592
|
-
.where("source", "=", "local")
|
|
593
|
-
.where("provider", "=", LOCAL_PROVIDER)
|
|
594
|
-
.where("deleted_at", "is", null)
|
|
595
|
-
.forNoKeyUpdate()
|
|
596
|
-
.executeTakeFirst();
|
|
597
|
-
if (!env)
|
|
598
|
-
throw deviceNotFound();
|
|
599
|
-
const machineId = await this.revokeActive(deviceId, "revoked");
|
|
600
|
-
if (machineId === null)
|
|
601
|
-
return;
|
|
602
|
-
await audit(this.db.db, {
|
|
603
|
-
actorAccountId: accountId,
|
|
604
|
-
...(actorTokenId !== undefined && { actorTokenId }),
|
|
605
|
-
orgId,
|
|
606
|
-
action: "device.machine_revoked",
|
|
607
|
-
target: deviceId,
|
|
608
|
-
detail: { machineId },
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
/**
|
|
612
|
-
* Revokes the device's active machine, if any, in the caller's transaction, and after the
|
|
613
|
-
* commit tells it to stop for `reason` (none: only its connection is closed) and closes its
|
|
614
|
-
* connection. Returns the revoked machine's id. Used by {@link issue} (replaced: no stop),
|
|
615
|
-
* {@link revoke} and the device's deletion (`deleted`).
|
|
616
|
-
*/
|
|
617
|
-
async revokeActive(deviceId, reason) {
|
|
618
|
-
const device = await this.db
|
|
619
|
-
.selectFrom("devices")
|
|
620
|
-
.select("org_id")
|
|
621
|
-
.where("id", "=", deviceId)
|
|
622
|
-
.executeTakeFirstOrThrow();
|
|
623
|
-
await this.ceilings.lock({ orgId: device.org_id, deviceId }, "device");
|
|
624
|
-
await this.ceilings.publishLocked({ orgId: device.org_id, deviceId }, "device", true, true);
|
|
625
|
-
const row = await this.db
|
|
626
|
-
.updateTable("device_machines")
|
|
627
|
-
.set({ revoked_at: sql `now()` })
|
|
628
|
-
.where("device_id", "=", deviceId)
|
|
629
|
-
.where("revoked_at", "is", null)
|
|
630
|
-
.returning("id")
|
|
631
|
-
.executeTakeFirst();
|
|
632
|
-
if (!row)
|
|
633
|
-
return null;
|
|
634
|
-
const machineId = row.id;
|
|
635
|
-
afterCommit(() => this.disconnect(deviceId, machineId, reason));
|
|
636
|
-
return machineId;
|
|
637
|
-
}
|
|
638
|
-
/**
|
|
639
|
-
* Revokes one machine by id (a sign-in whose credential never reached the program), with the
|
|
640
|
-
* same post-commit disconnect as a replacement (no stop frame). True when it was active.
|
|
641
|
-
*/
|
|
642
|
-
async revokeMachine(machineId) {
|
|
643
|
-
const machine = await this.db
|
|
644
|
-
.selectFrom("device_machines")
|
|
645
|
-
.select(["org_id", "device_id"])
|
|
646
|
-
.where("id", "=", machineId)
|
|
647
|
-
.executeTakeFirst();
|
|
648
|
-
if (!machine)
|
|
649
|
-
return false;
|
|
650
|
-
await this.ceilings.lock({ orgId: machine.org_id, deviceId: machine.device_id }, "device");
|
|
651
|
-
await this.ceilings.publishLocked({ orgId: machine.org_id, deviceId: machine.device_id }, "device", true, true);
|
|
652
|
-
const row = await this.db
|
|
653
|
-
.updateTable("device_machines")
|
|
654
|
-
.set({ revoked_at: sql `now()` })
|
|
655
|
-
.where("id", "=", machineId)
|
|
656
|
-
.where("revoked_at", "is", null)
|
|
657
|
-
.returning("device_id")
|
|
658
|
-
.executeTakeFirst();
|
|
659
|
-
if (!row)
|
|
660
|
-
return false;
|
|
661
|
-
const deviceId = row.device_id;
|
|
662
|
-
afterCommit(() => this.disconnect(deviceId, machineId, null));
|
|
663
|
-
return true;
|
|
664
|
-
}
|
|
665
|
-
/** The device's active machine, or null. */
|
|
666
|
-
async activeFor(deviceId) {
|
|
667
|
-
const r = await this.db
|
|
668
|
-
.selectFrom("device_machines")
|
|
669
|
-
.select([
|
|
670
|
-
"id",
|
|
671
|
-
"org_id",
|
|
672
|
-
"account_id",
|
|
673
|
-
"name",
|
|
674
|
-
"os",
|
|
675
|
-
"arch",
|
|
676
|
-
"program_version",
|
|
677
|
-
"last_seen_at",
|
|
678
|
-
"created_at",
|
|
679
|
-
"features",
|
|
680
|
-
"local_policy",
|
|
681
|
-
])
|
|
682
|
-
.where("device_id", "=", deviceId)
|
|
683
|
-
.where("revoked_at", "is", null)
|
|
684
|
-
.executeTakeFirst();
|
|
685
|
-
return r
|
|
686
|
-
? {
|
|
687
|
-
id: r.id,
|
|
688
|
-
orgId: r.org_id,
|
|
689
|
-
accountId: r.account_id,
|
|
690
|
-
name: r.name,
|
|
691
|
-
os: r.os,
|
|
692
|
-
arch: r.arch,
|
|
693
|
-
programVersion: r.program_version,
|
|
694
|
-
lastSeenAt: r.last_seen_at,
|
|
695
|
-
createdAt: r.created_at,
|
|
696
|
-
features: deviceFeaturesOf(r.features),
|
|
697
|
-
localPolicy: localPolicyOf(r.local_policy),
|
|
698
|
-
}
|
|
699
|
-
: null;
|
|
700
|
-
}
|
|
701
|
-
/**
|
|
702
|
-
* After a revocation commits: `stop` is queued only while a replica holds the connection (an
|
|
703
|
-
* offline machine must not find it on the list later), then the revocation is published.
|
|
704
|
-
* Failures are logged; the revocation itself stands.
|
|
705
|
-
*/
|
|
706
|
-
async disconnect(deviceId, machineId, reason) {
|
|
707
|
-
try {
|
|
708
|
-
if (reason !== null && (await this.relay.holderLive(deviceId))) {
|
|
709
|
-
// fenced to this machine: on a staged connection it goes out as a protocol 4 `stop`
|
|
710
|
-
await this.relay.send(deviceId, "stop", { reason }, STOP_DEADLINE_MS, machineId);
|
|
711
|
-
}
|
|
712
|
-
}
|
|
713
|
-
catch (err) {
|
|
714
|
-
this.log.warn("could not tell a revoked machine to stop", { deviceId, machineId, err });
|
|
715
|
-
}
|
|
716
|
-
try {
|
|
717
|
-
await this.bus.publish(deviceRevokedTopic(deviceId), { machineId });
|
|
718
|
-
}
|
|
719
|
-
catch (err) {
|
|
720
|
-
this.log.warn("could not publish a machine revocation", { deviceId, machineId, err });
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
__decorate([
|
|
725
|
-
Transactional()
|
|
726
|
-
], MachinesService.prototype, "issue", null);
|
|
727
|
-
__decorate([
|
|
728
|
-
Transactional()
|
|
729
|
-
], MachinesService.prototype, "recordHello", null);
|
|
730
|
-
__decorate([
|
|
731
|
-
Transactional()
|
|
732
|
-
], MachinesService.prototype, "recordHelloV3", null);
|
|
733
|
-
__decorate([
|
|
734
|
-
Transactional()
|
|
735
|
-
], MachinesService.prototype, "recordPolicy", null);
|
|
736
|
-
__decorate([
|
|
737
|
-
Transactional()
|
|
738
|
-
], MachinesService.prototype, "recordPolicyV3", null);
|
|
739
|
-
__decorate([
|
|
740
|
-
Transactional()
|
|
741
|
-
], MachinesService.prototype, "revoke", null);
|
|
742
|
-
__decorate([
|
|
743
|
-
Transactional()
|
|
744
|
-
], MachinesService.prototype, "revokeActive", null);
|
|
745
|
-
__decorate([
|
|
746
|
-
Transactional()
|
|
747
|
-
], MachinesService.prototype, "revokeMachine", null);
|
|
748
|
-
MachinesService = __decorate([
|
|
749
|
-
Injectable(),
|
|
750
|
-
__param(0, Inject(KernelDb)),
|
|
751
|
-
__param(1, Inject(TransactionHost)),
|
|
752
|
-
__param(2, Inject(LocalDeviceRelay)),
|
|
753
|
-
__param(3, Inject(KERNEL_BUS)),
|
|
754
|
-
__param(4, Inject(OutboxService)),
|
|
755
|
-
__param(5, Inject(KERNEL_LOGGER)),
|
|
756
|
-
__param(6, Inject(PermissionCeilingWrites)),
|
|
757
|
-
__param(7, Inject(PermissionRuntimeReports)),
|
|
758
|
-
__param(8, Inject(OwnerReports)),
|
|
759
|
-
__param(9, Inject(LegacyRetirementStore))
|
|
760
|
-
], MachinesService);
|
|
761
|
-
export { MachinesService };
|
|
762
|
-
const jsonOrNull = (v) => (v === null ? null : JSON.stringify(v));
|
|
763
|
-
/** Two machine rows (or a login) name the same machine: same name, OS and arch. */
|
|
764
|
-
function sameMachine(a, b) {
|
|
765
|
-
return a.name === b.name && a.os === b.os && a.arch === b.arch;
|
|
766
|
-
}
|