Repository navigation
Expand file tree
/
Copy pathManagedPolicy.ts
More file actions
221 lines (206 loc) · 7.97 KB
/
Copy pathManagedPolicy.ts
File metadata and controls
221 lines (206 loc) · 7.97 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* ManagedPolicy - the administrator's machine-wide policy file, read once at startup. A file this
* build cannot fully enforce stops the server instead of leaving it looking managed.
*
* @module ManagedPolicy
*/
import * as NodeModule from "node:module";
import { ManagedPolicyDocument, type ManagedPolicySummary } from "@t3tools/contracts";
import * as Config from "effect/Config";
import * as Context from "effect/Context";
import * as Digest from "effect/crypto/Digest";
import * as Effect from "effect/Effect";
import * as Hex from "effect/encoding/Hex";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as HostProcess from "@t3tools/shared/HostProcess";
import {
ManagedPolicyFileError,
managedPolicyPath,
layerWindowsRegistry,
readManagedPolicy,
readManagedPolicyFile,
} from "@t3tools/shared/managedPolicyFile";
import { telemetryChannel } from "../config.ts";
import * as ManagedLimits from "./managedLimits.ts";
import * as ManagedProviders from "./managedProviders.ts";
import * as ManagedSettings from "./managedSettings.ts";
export class ManagedPolicy extends Context.Service<
ManagedPolicy,
{
/** Where the policy is read from, whether or not a file exists there. */
readonly path: string;
readonly document: ManagedPolicyDocument | undefined;
readonly settings: ManagedSettings.ManagedSettings;
readonly providers: ManagedProviders.ManagedProviders;
/** What clients are told; absent when no policy applies. */
readonly summary: ManagedPolicySummary | undefined;
}
>()("t3/policy/ManagedPolicy") {}
type LimitsSummary = NonNullable<ManagedPolicySummary["limits"]>;
const encodeDocumentJson = Schema.encodeEffect(Schema.fromJsonString(ManagedPolicyDocument));
/** What `t3 policy show` compares to tell whether a running server loaded the installed policy. */
export const loadedPolicy = Effect.fn("ManagedPolicy.loadedPolicy")(function* (
policy: Pick<ManagedPolicy["Service"], "path" | "document">,
) {
if (policy.document === undefined) return null;
const json = yield* encodeDocumentJson(policy.document).pipe(Effect.orDie);
const digest = yield* (yield* Digest.Digest).digest(
Digest.sha256,
new TextEncoder().encode(json),
);
return {
source: policy.path,
organization: policy.document.organization,
fingerprint: Hex.encode(digest).slice(0, 12),
};
});
/**
* The organization whose policy switches `capability` off, or undefined when it is allowed.
* Only an explicit `false` restricts.
*/
export function deniedBy(
policy: Pick<ManagedPolicy["Service"], "document">,
capability: "webhookTasks" | "telemetry" | "previewTunnel",
): string | undefined {
return policy.document?.limits?.[capability] === false ? policy.document.organization : undefined;
}
/** Build the service from a decoded document; fails when the document cannot be enforced. */
export const fromDocument = Effect.fn("ManagedPolicy.fromDocument")(function* (
path: string,
document: ManagedPolicyDocument | undefined,
) {
if (document === undefined) {
return ManagedPolicy.of({
path,
document: undefined,
settings: ManagedSettings.unmanagedSettings,
providers: ManagedProviders.unmanagedProviders,
summary: undefined,
});
}
const maxRuntimeMode = document.limits?.maxRuntimeMode;
const settings = ManagedSettings.makeManagedSettings(document.settings ?? {}, maxRuntimeMode);
if (settings._tag === "Invalid") {
return yield* new ManagedPolicyFileError({ path, reason: settings.reason });
}
// A ceiling alone also shapes providers: their launch configuration becomes the administrator's.
const providerLimits = document.limits?.providers;
const providers =
providerLimits === undefined && maxRuntimeMode === undefined
? undefined
: ManagedProviders.makeManagedProviders(
providerLimits ?? {},
maxRuntimeMode,
yield* HostProcess.Platform,
);
if (providers?._tag === "Invalid") {
return yield* new ManagedPolicyFileError({ path, reason: providers.reason });
}
const lockedInstanceFields = providers?.providers.lockedInstanceFields ?? {};
return ManagedPolicy.of({
path,
document,
settings: settings.settings,
providers: providers?.providers ?? ManagedProviders.unmanagedProviders,
summary: {
organization: document.organization,
...(document.supportUrl === undefined ? {} : { supportUrl: document.supportUrl }),
lockedSettings: settings.settings.lockedPaths,
...(document.limits === undefined
? {}
: { limits: summarizeLimits(document, lockedInstanceFields) }),
},
});
});
// Copies only what clients may see; other limits can carry values that stay on the server.
function summarizeLimits(
document: ManagedPolicyDocument,
lockedInstanceFields: Readonly<Record<string, ReadonlyArray<string>>>,
): LimitsSummary {
const {
maxRuntimeMode,
clients,
webhookTasks,
telemetry,
browserCookieImport,
previewTunnel,
providers,
updates,
} = document.limits ?? {};
const connect = ManagedLimits.allowsConnect({ document });
return {
...(maxRuntimeMode === undefined ? {} : { maxRuntimeMode }),
...(clients === undefined ? {} : { clients }),
...(connect ? {} : { connect: false }),
...(webhookTasks === undefined ? {} : { webhookTasks }),
...(telemetry === undefined ? {} : { telemetry }),
...(browserCookieImport === undefined ? {} : { browserCookieImport }),
...(previewTunnel === undefined ? {} : { previewTunnel }),
...(providers?.allowedDrivers === undefined && Object.keys(lockedInstanceFields).length === 0
? {}
: {
providers: {
...(providers?.allowedDrivers === undefined
? {}
: { allowedDrivers: providers.allowedDrivers }),
...(Object.keys(lockedInstanceFields).length === 0 ? {} : { lockedInstanceFields }),
},
}),
...(updates === undefined
? {}
: {
updates: {
mode: updates.mode,
...(updates.allowNightly === undefined ? {} : { allowNightly: updates.allowNightly }),
},
}),
};
}
/** Read and validate the policy at `path`; a missing file means no policy. */
export const load = Effect.fn("ManagedPolicy.load")(function* (path: string) {
return yield* fromDocument(path, yield* readManagedPolicyFile(path));
});
const make = Effect.gen(function* () {
// Source builds may read a policy from elsewhere so it can be tried without root. Release
// builds never do: any override a user can set is a way around the policy.
const devPath =
telemetryChannel === "dev"
? yield* Config.option(Config.String("T3CODE_DEV_MANAGED_POLICY_PATH")).pipe(
Effect.orElseSucceed(() => Option.none<string>()),
)
: Option.none<string>();
const platform = yield* HostProcess.Platform;
const policy = yield* Option.match(devPath, {
onSome: load,
onNone: () =>
readManagedPolicy(
platform,
ManagedProviders.knownDriverLabels().map(({ driver }) => driver),
).pipe(
Effect.flatMap((found) =>
fromDocument(found?.source ?? managedPolicyPath(platform), found?.document),
),
),
});
const path = policy.path;
if (policy.summary !== undefined) {
yield* Effect.logInfo("Managed policy applies.", {
path,
organization: policy.summary.organization,
lockedSettings: policy.summary.lockedSettings,
});
}
return policy;
});
// A single-executable server resolves packages on disk only through `require`.
const requireForFfi = NodeModule.createRequire(import.meta.url);
export const layer = Layer.effect(ManagedPolicy, make).pipe(
Layer.provide(
layerWindowsRegistry(async () => requireForFfi("ffi-rs") as typeof import("ffi-rs")),
),
);
/** A policy for tests; `undefined` means unmanaged. */
export const layerTest = (document?: ManagedPolicyDocument) =>
Layer.effect(ManagedPolicy, fromDocument("/test/policy.json", document));