Repository navigation
Expand file tree
/
Copy pathdaemon-policy.ts
More file actions
135 lines (124 loc) · 5.52 KB
/
Copy pathdaemon-policy.ts
File metadata and controls
135 lines (124 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import { PUBLIC_COMMANDS } from '@agent-device/command-registry/catalog';
import { normalizeBatchCommandName } from '@agent-device/command-registry/batch-policy';
import type {
ComposedDeviceInventoryGateways,
DeviceInventoryGateway,
ProviderAwareDeviceInventoryGateway,
} from '@agent-device/contracts/platform-module';
import type { DeviceInfo } from '@agent-device/kernel/device';
import { AppError } from '@agent-device/kernel/errors';
import type { DaemonPolicy, DaemonPolicyCapability } from '../daemon-policy-file.ts';
import type { DaemonRequest } from './daemon-request.ts';
/**
* ADR 0029 enforcement: every request the daemon admits, including `batch` steps and `replay`
* actions, which re-enter request admission rather than the HTTP edge. The policy file itself is
* loaded and validated by `src/daemon-policy-file.ts`.
*/
// Internal protocol commands a public command reaches the daemon through. Every other internal
// command (leases, takeover, session bookkeeping) is protocol plumbing no rule names.
const PUBLIC_COMMAND_FOR_INTERNAL: Readonly<Record<string, string>> = {
install_source: 'install-from-source',
};
const PUBLIC_COMMAND_NAMES: ReadonlySet<string> = new Set(Object.values(PUBLIC_COMMANDS));
/** Refuses a request the policy denies before it resolves a device or takes a lock. */
export function assertDaemonPolicyAdmitsRequest(policy: DaemonPolicy, req: DaemonRequest): void {
assertInvocationAdmitted(policy, req.command, req.flags ?? {});
if (req.command !== 'batch') return;
// Refuse the whole batch before any step runs; each step is admitted again when it runs.
for (const step of req.flags?.batchSteps ?? []) {
assertInvocationAdmitted(policy, normalizeBatchCommandName(step.command), {
...step.input,
...step.flags,
});
}
}
function assertInvocationAdmitted(
policy: DaemonPolicy,
command: string,
fields: Readonly<Record<string, unknown>>,
): void {
assertCommandAdmitted(policy, command);
if (command === 'close' && fields.shutdown === true) {
assertDaemonPolicyAllowsCapability(policy, 'device-shutdown');
}
if (!policy.deviceIds) return;
for (const value of [fields.udid, fields.serial]) {
const id = typeof value === 'string' ? value.trim() : '';
if (!id || policy.deviceIds.has(id)) continue;
throw policyDenied(policy, 'device', `Device ${id} is outside this daemon's policy.`, {
deviceId: id,
});
}
}
function isDeviceAllowedByDaemonPolicy(policy: DaemonPolicy, device: DeviceInfo): boolean {
return !policy.deviceIds || policy.deviceIds.has(device.id);
}
/** Refuses a resolved device the policy does not allow, before the request binds it. */
export function assertDaemonPolicyAdmitsDevice(policy: DaemonPolicy, device: DeviceInfo): void {
if (isDeviceAllowedByDaemonPolicy(policy, device)) return;
throw policyDenied(policy, 'device', `Device ${device.id} is outside this daemon's policy.`, {
deviceId: device.id,
});
}
export function assertDaemonPolicyAllowsCapability(
policy: DaemonPolicy,
capability: DaemonPolicyCapability,
): void {
if (!policy.deniedCapabilities.has(capability)) return;
throw policyDenied(policy, 'capability', `This daemon's policy denies ${capability}.`, {
capability,
});
}
/** Device inventory, and therefore device selection, sees only the devices the policy allows. */
export function restrictDeviceInventoryToDaemonPolicy(
gateways: ComposedDeviceInventoryGateways,
policy: DaemonPolicy,
): ComposedDeviceInventoryGateways {
if (!policy.deviceIds) return gateways;
const allowed = (devices: readonly DeviceInfo[]) =>
devices.filter((device) => isDeviceAllowedByDaemonPolicy(policy, device));
const localOnly: DeviceInventoryGateway = Object.freeze({
discover: async (request, scope) => allowed(await gateways.localOnly.discover(request, scope)),
});
// A provider that answers with only out-of-scope devices must not hide allowed local devices.
const discoverWithSource: ProviderAwareDeviceInventoryGateway['discoverWithSource'] = async (
request,
scope,
) => {
const discovery = await gateways.providerFirst.discoverWithSource(request, scope);
const devices = allowed(discovery.devices);
if (devices.length > 0 || discovery.source === 'local') return { ...discovery, devices };
return { devices: await localOnly.discover(request, scope), source: 'local' };
};
const providerFirst: ProviderAwareDeviceInventoryGateway = Object.freeze({
discover: async (request, scope) => (await discoverWithSource(request, scope)).devices,
discoverWithSource,
});
return Object.freeze({ ...gateways, localOnly, providerFirst });
}
function assertCommandAdmitted(policy: DaemonPolicy, command: string): void {
const rules = policy.commands;
if (!rules) return;
const name = PUBLIC_COMMAND_FOR_INTERNAL[command] ?? command;
if (!PUBLIC_COMMAND_NAMES.has(name)) return;
const listed = rules.names.has(name);
if (rules.mode === 'allow' ? listed : !listed) return;
throw policyDenied(policy, 'command', `This daemon's policy denies the ${name} command.`, {
command: name,
});
}
function policyDenied(
policy: DaemonPolicy,
rule: 'command' | 'device' | 'capability',
message: string,
details: Record<string, unknown>,
): AppError {
return new AppError('UNAUTHORIZED', message, {
...details,
reason: 'DAEMON_POLICY_DENIED',
rule,
policyDigest: policy.digest,
retriable: false,
hint: "The daemon operator's policy denies this request; retrying will not help. Use an allowed command and device, or ask the operator to change the policy.",
});
}