@operad-io/access-match 0.1.1 → 0.3.0
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/access-grants.schema.json +54 -0
- package/package.json +3 -2
- package/src/command.ts +100 -0
- package/src/index.ts +446 -24
- package/src/outward.ts +302 -0
- package/src/readonly.ts +71 -9
- package/src/summary.ts +176 -1
- package/src/target-grants.ts +258 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The target grants of a background run: a person's "always for this flow and
|
|
3
|
+
* this target" answer, in `$OPERAD_HOME/access-grants.json` (ADR 0025 D12).
|
|
4
|
+
*
|
|
5
|
+
* A grant is bound to a flow's definition identity and to one outward target.
|
|
6
|
+
* The identity is a generated id kept beside the definition's digest: every
|
|
7
|
+
* time the digest differs from the last one seen, a new id is made. An edit
|
|
8
|
+
* therefore asks again even after it is reverted, because reverting is another
|
|
9
|
+
* change from the digest last seen. The grant is never bound to the content
|
|
10
|
+
* that is sent, so a daily message with new text to the same target stays
|
|
11
|
+
* allowed.
|
|
12
|
+
*
|
|
13
|
+
* The file is the user layer, like `access-user.json`: hand-editable, written
|
|
14
|
+
* under the platform file lock and replaced whole. It lives here, beside the
|
|
15
|
+
* guard, because the shell and dev steps call the guard without the harness
|
|
16
|
+
* packages.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { messageOf } from "@operad-io/engine";
|
|
20
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
21
|
+
import { readFile } from "node:fs/promises";
|
|
22
|
+
import { join } from "node:path";
|
|
23
|
+
import { isMissingFile, isRecord, type Actor, type FlowDefinition, type OutwardWrite } from "@operad-io/engine";
|
|
24
|
+
import { withLockFile, writeJsonAtomic } from "@operad-io/engine/platform";
|
|
25
|
+
|
|
26
|
+
/** The file name of the target grants under the operad home. */
|
|
27
|
+
export const ACCESS_GRANTS_FILE = "access-grants.json";
|
|
28
|
+
|
|
29
|
+
/** One flow definition's identity: the digest last seen and its generated id. */
|
|
30
|
+
export interface FlowDefinitionIdentity {
|
|
31
|
+
readonly flow: string;
|
|
32
|
+
readonly digest: string;
|
|
33
|
+
/** Changes whenever the digest differs from the last one seen; never reused. */
|
|
34
|
+
readonly id: string;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** One target a person allowed for one flow definition. */
|
|
38
|
+
export interface TargetGrant {
|
|
39
|
+
readonly flow: string;
|
|
40
|
+
/** The definition identity the person answered for. */
|
|
41
|
+
readonly definition: string;
|
|
42
|
+
readonly kind: OutwardWrite["kind"];
|
|
43
|
+
readonly target: string;
|
|
44
|
+
/** Who answered. */
|
|
45
|
+
readonly by: Actor;
|
|
46
|
+
/** When the answer came, in epoch milliseconds. */
|
|
47
|
+
readonly at: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Everything the file holds. */
|
|
51
|
+
export interface TargetGrantFile {
|
|
52
|
+
readonly flows: readonly FlowDefinitionIdentity[];
|
|
53
|
+
readonly grants: readonly TargetGrant[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The file, and a readable problem when it had to be treated as empty. */
|
|
57
|
+
export interface TargetGrantRead {
|
|
58
|
+
readonly file: TargetGrantFile;
|
|
59
|
+
readonly problem?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** The file of the target grants in an operad home. */
|
|
63
|
+
export function accessGrantsPath(home: string): string {
|
|
64
|
+
return join(home, ACCESS_GRANTS_FILE);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function emptyFile(): TargetGrantFile {
|
|
68
|
+
return { flows: [], grants: [] };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function nonEmpty(value: unknown): value is string {
|
|
72
|
+
return typeof value === "string" && value.length > 0;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function actorOf(value: unknown): Actor | undefined {
|
|
76
|
+
if (!isRecord(value) || !nonEmpty(value["via"]) || !nonEmpty(value["id"])) return undefined;
|
|
77
|
+
return { via: value["via"], id: value["id"] };
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function flowIdentityOf(value: unknown): FlowDefinitionIdentity | undefined {
|
|
81
|
+
if (!isRecord(value)) return undefined;
|
|
82
|
+
const flow = value["flow"];
|
|
83
|
+
const digest = value["digest"];
|
|
84
|
+
const id = value["id"];
|
|
85
|
+
if (!nonEmpty(flow) || !nonEmpty(digest) || !nonEmpty(id)) return undefined;
|
|
86
|
+
return { flow, digest, id };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const OUTWARD_KINDS = new Set<OutwardWrite["kind"]>(["channel", "mail", "post", "repository", "service", "command"]);
|
|
90
|
+
|
|
91
|
+
function targetGrantOf(value: unknown): TargetGrant | undefined {
|
|
92
|
+
if (!isRecord(value)) return undefined;
|
|
93
|
+
const flow = value["flow"];
|
|
94
|
+
const definition = value["definition"];
|
|
95
|
+
const kind = value["kind"];
|
|
96
|
+
const target = value["target"];
|
|
97
|
+
const by = actorOf(value["by"]);
|
|
98
|
+
const at = value["at"];
|
|
99
|
+
if (!nonEmpty(flow) || !nonEmpty(definition) || !nonEmpty(target)) return undefined;
|
|
100
|
+
if (typeof kind !== "string" || !OUTWARD_KINDS.has(kind as OutwardWrite["kind"])) return undefined;
|
|
101
|
+
if (by === undefined || typeof at !== "number" || !Number.isFinite(at)) return undefined;
|
|
102
|
+
return { flow, definition, kind: kind as OutwardWrite["kind"], target, by, at };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function invalid(path: string, detail: string): TargetGrantRead {
|
|
106
|
+
return { file: emptyFile(), problem: `the target grants file "${path}" ${detail}; it is treated as empty` };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Reads the target grants; a missing file is empty, and an invalid one is empty with a problem. */
|
|
110
|
+
export async function readTargetGrants(home: string): Promise<TargetGrantRead> {
|
|
111
|
+
const path = accessGrantsPath(home);
|
|
112
|
+
let text: string;
|
|
113
|
+
try {
|
|
114
|
+
text = await readFile(path, "utf8");
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (isMissingFile(error)) return { file: emptyFile() };
|
|
117
|
+
return invalid(path, `cannot be read: ${messageOf(error)}`);
|
|
118
|
+
}
|
|
119
|
+
let raw: unknown;
|
|
120
|
+
try {
|
|
121
|
+
raw = JSON.parse(text);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return invalid(path, `is not valid JSON: ${messageOf(error)}`);
|
|
124
|
+
}
|
|
125
|
+
if (!isRecord(raw)) return invalid(path, "must be a JSON object with flows and grants arrays");
|
|
126
|
+
const flowsRaw = raw["flows"] ?? [];
|
|
127
|
+
const grantsRaw = raw["grants"] ?? [];
|
|
128
|
+
if (!Array.isArray(flowsRaw)) return invalid(path, "flows must be an array");
|
|
129
|
+
if (!Array.isArray(grantsRaw)) return invalid(path, "grants must be an array");
|
|
130
|
+
const flows: FlowDefinitionIdentity[] = [];
|
|
131
|
+
for (let index = 0; index < flowsRaw.length; index += 1) {
|
|
132
|
+
const entry = flowIdentityOf(flowsRaw[index]);
|
|
133
|
+
if (entry === undefined) return invalid(path, `flow entry ${index} needs a flow, digest and id`);
|
|
134
|
+
flows.push(entry);
|
|
135
|
+
}
|
|
136
|
+
const grants: TargetGrant[] = [];
|
|
137
|
+
for (let index = 0; index < grantsRaw.length; index += 1) {
|
|
138
|
+
const entry = targetGrantOf(grantsRaw[index]);
|
|
139
|
+
if (entry === undefined) {
|
|
140
|
+
return invalid(path, `grant entry ${index} needs a flow, definition, kind, target, by and at`);
|
|
141
|
+
}
|
|
142
|
+
grants.push(entry);
|
|
143
|
+
}
|
|
144
|
+
return { file: { flows, grants } };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** A stable JSON reading, so the same definition always gives the same digest. */
|
|
148
|
+
function canonical(value: unknown): string {
|
|
149
|
+
if (Array.isArray(value)) return `[${value.map((item) => canonical(item)).join(",")}]`;
|
|
150
|
+
if (isRecord(value)) {
|
|
151
|
+
return `{${Object.keys(value)
|
|
152
|
+
.sort()
|
|
153
|
+
.filter((key) => value[key] !== undefined)
|
|
154
|
+
.map((key) => `${JSON.stringify(key)}:${canonical(value[key])}`)
|
|
155
|
+
.join(",")}}`;
|
|
156
|
+
}
|
|
157
|
+
const text = JSON.stringify(value);
|
|
158
|
+
return text === undefined ? "null" : text;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** The digest of one flow definition, without its name alone standing for its structure. */
|
|
162
|
+
export function flowDefinitionDigest(flow: FlowDefinition): string {
|
|
163
|
+
return createHash("sha256").update(canonical(flow)).digest("hex");
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async function writeTargetGrants(home: string, file: TargetGrantFile): Promise<void> {
|
|
167
|
+
const path = accessGrantsPath(home);
|
|
168
|
+
await writeJsonAtomic(path, file, { space: 2, mode: 0o600 });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Reads the file again inside the lock, and gives the current definition identity. */
|
|
172
|
+
async function refreshFlow(home: string, flow: FlowDefinition): Promise<string> {
|
|
173
|
+
const path = accessGrantsPath(home);
|
|
174
|
+
return withLockFile(`${path}.lock`, async () => {
|
|
175
|
+
const read = await readTargetGrants(home);
|
|
176
|
+
const digest = flowDefinitionDigest(flow);
|
|
177
|
+
const current = read.file.flows.find((entry) => entry.flow === flow.name);
|
|
178
|
+
if (current !== undefined && current.digest === digest) return current.id;
|
|
179
|
+
const id = randomUUID();
|
|
180
|
+
const flows = [...read.file.flows.filter((entry) => entry.flow !== flow.name), { flow: flow.name, digest, id }];
|
|
181
|
+
await writeTargetGrants(home, { flows, grants: read.file.grants });
|
|
182
|
+
return id;
|
|
183
|
+
}, { label: "access target grants" });
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* The grant for this flow definition and target, if one is there. Looking at a
|
|
188
|
+
* write first records a changed definition, so an edit and a later revert each
|
|
189
|
+
* give a new identity and no older grant can come back.
|
|
190
|
+
*/
|
|
191
|
+
export async function targetGrantFor(
|
|
192
|
+
home: string,
|
|
193
|
+
flow: FlowDefinition,
|
|
194
|
+
target: string,
|
|
195
|
+
): Promise<TargetGrant | undefined> {
|
|
196
|
+
const identity = await refreshFlow(home, flow);
|
|
197
|
+
const read = await readTargetGrants(home);
|
|
198
|
+
const wanted = target.trim();
|
|
199
|
+
return read.file.grants.find(
|
|
200
|
+
(grant) => grant.flow === flow.name && grant.definition === identity && grant.target === wanted,
|
|
201
|
+
);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Appends or replaces the grant of one flow definition and target. */
|
|
205
|
+
export async function appendTargetGrant(
|
|
206
|
+
home: string,
|
|
207
|
+
flow: FlowDefinition,
|
|
208
|
+
write: OutwardWrite,
|
|
209
|
+
by: Actor,
|
|
210
|
+
at: number,
|
|
211
|
+
): Promise<TargetGrant> {
|
|
212
|
+
const path = accessGrantsPath(home);
|
|
213
|
+
return withLockFile(`${path}.lock`, async () => {
|
|
214
|
+
const read = await readTargetGrants(home);
|
|
215
|
+
const digest = flowDefinitionDigest(flow);
|
|
216
|
+
const current = read.file.flows.find((entry) => entry.flow === flow.name);
|
|
217
|
+
const id = current !== undefined && current.digest === digest ? current.id : randomUUID();
|
|
218
|
+
const flows = current !== undefined && current.digest === digest
|
|
219
|
+
? read.file.flows
|
|
220
|
+
: [...read.file.flows.filter((entry) => entry.flow !== flow.name), { flow: flow.name, digest, id }];
|
|
221
|
+
const target = write.target.trim();
|
|
222
|
+
const entry: TargetGrant = {
|
|
223
|
+
flow: flow.name,
|
|
224
|
+
definition: id,
|
|
225
|
+
kind: write.kind,
|
|
226
|
+
target,
|
|
227
|
+
by,
|
|
228
|
+
at,
|
|
229
|
+
};
|
|
230
|
+
const grants = [
|
|
231
|
+
...read.file.grants.filter(
|
|
232
|
+
(grant) => !(grant.flow === flow.name && grant.definition === id && grant.target === target),
|
|
233
|
+
),
|
|
234
|
+
entry,
|
|
235
|
+
];
|
|
236
|
+
await writeTargetGrants(home, { flows, grants });
|
|
237
|
+
return entry;
|
|
238
|
+
}, { label: "access target grants" });
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** Removes every grant for one target, or for one flow's target; returns how many went. */
|
|
242
|
+
export async function revokeTargetGrants(
|
|
243
|
+
home: string,
|
|
244
|
+
target: string,
|
|
245
|
+
flow?: string,
|
|
246
|
+
): Promise<number> {
|
|
247
|
+
const path = accessGrantsPath(home);
|
|
248
|
+
return withLockFile(`${path}.lock`, async () => {
|
|
249
|
+
const read = await readTargetGrants(home);
|
|
250
|
+
const wanted = target.trim();
|
|
251
|
+
const kept = read.file.grants.filter(
|
|
252
|
+
(grant) => !(grant.target === wanted && (flow === undefined || grant.flow === flow)),
|
|
253
|
+
);
|
|
254
|
+
const removed = read.file.grants.length - kept.length;
|
|
255
|
+
if (removed > 0) await writeTargetGrants(home, { flows: read.file.flows, grants: kept });
|
|
256
|
+
return removed;
|
|
257
|
+
}, { label: "access target grants" });
|
|
258
|
+
}
|