@nylorun/admin 0.9.0-beta → 0.11.1-beta
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/CHANGELOG.md +92 -0
- package/README.md +114 -39
- package/dist/client.d.ts +7 -29
- package/dist/client.js +129 -243
- package/dist/derived-credentials.d.ts +6 -18
- package/dist/derived-credentials.js +7 -34
- package/dist/index.d.ts +18 -23
- package/dist/index.js +9 -5
- package/dist/management.d.ts +120 -0
- package/dist/management.js +161 -0
- package/dist/studio-login.d.ts +2 -2
- package/dist/studio-login.js +2 -2
- package/package.json +7 -3
package/dist/client.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
2
|
import { homedir } from "node:os";
|
|
3
3
|
import { dirname, join, parse, resolve } from "node:path";
|
|
4
|
-
import {
|
|
5
|
-
import { PROTOCOL_FEATURES,
|
|
6
|
-
import { deriveTenantKey } from "./derived-credentials.js";
|
|
4
|
+
import { ProjectLinkFileSchema, RejectedResponseSchema } from "@nylorun/core/contracts";
|
|
5
|
+
import { PROTOCOL_FEATURES, PROTOCOL_VERSION, checkCompatibility, } from "@nylorun/core/compatibility";
|
|
7
6
|
import { AdminError } from "./errors.js";
|
|
7
|
+
import { ManagementClient } from "./management.js";
|
|
8
8
|
function env(name) {
|
|
9
9
|
const value = process.env[name];
|
|
10
10
|
return value === undefined || value.trim() === "" ? undefined : value;
|
|
@@ -14,11 +14,11 @@ export function tenantHostRoot(name) {
|
|
|
14
14
|
return resolve(join(homedir(), ".nylorun", "tenants", name));
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* The
|
|
18
|
-
* path of a link from an older nylorun. The
|
|
19
|
-
* Tenants and is never a Project.
|
|
17
|
+
* The Project link (`.nylorun/link.json`, format 3) from `cwd` upwards: the Tenant it names
|
|
18
|
+
* and the Project's `.nylorun/` directory, or the path of a link from an older nylorun. The
|
|
19
|
+
* walk stops at the home directory, which holds the Tenants and is never a Project.
|
|
20
20
|
*/
|
|
21
|
-
function
|
|
21
|
+
function projectLink(cwd) {
|
|
22
22
|
const real = (path) => {
|
|
23
23
|
try {
|
|
24
24
|
return realpathSync(path);
|
|
@@ -42,7 +42,9 @@ function linkedTenant(cwd) {
|
|
|
42
42
|
if (raw !== undefined) {
|
|
43
43
|
try {
|
|
44
44
|
const link = ProjectLinkFileSchema.parse(JSON.parse(raw));
|
|
45
|
-
|
|
45
|
+
if (link.format < 3)
|
|
46
|
+
return { olderLink: path };
|
|
47
|
+
return { ...(link.tenant ? { tenant: link.tenant } : {}), directory: dirname(path) };
|
|
46
48
|
}
|
|
47
49
|
catch {
|
|
48
50
|
return {};
|
|
@@ -56,34 +58,41 @@ function linkedTenant(cwd) {
|
|
|
56
58
|
}
|
|
57
59
|
/**
|
|
58
60
|
* The Host root of the local Host: `options.home`, `NYLORUN_HOME`, or the Host root of the
|
|
59
|
-
* Tenant named by `options.tenant`, `NYLORUN_TENANT` or the Project link.
|
|
60
|
-
*
|
|
61
|
+
* Tenant named by `options.tenant`, `NYLORUN_TENANT` or the Project link. `project` is the
|
|
62
|
+
* linked Project's `.nylorun/` when the link names this Host root's Tenant.
|
|
61
63
|
*/
|
|
62
64
|
function resolveHome(options) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
+
const link = projectLink(options?.cwd ?? process.cwd());
|
|
66
|
+
const linked = link.tenant ? tenantHostRoot(link.tenant) : undefined;
|
|
65
67
|
const fromEnv = env("NYLORUN_HOME");
|
|
66
|
-
if (fromEnv)
|
|
67
|
-
return { home: resolve(fromEnv) };
|
|
68
68
|
const named = options?.tenant?.trim() || env("NYLORUN_TENANT")?.trim();
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
const home = options?.home !== undefined && options.home.trim() !== ""
|
|
70
|
+
? resolve(options.home)
|
|
71
|
+
: fromEnv
|
|
72
|
+
? resolve(fromEnv)
|
|
73
|
+
: named
|
|
74
|
+
? tenantHostRoot(named)
|
|
75
|
+
: linked;
|
|
76
|
+
return {
|
|
77
|
+
...(home ? { home } : {}),
|
|
78
|
+
...(home && home === linked ? { project: link.directory } : {}),
|
|
79
|
+
...(link.olderLink ? { olderLink: link.olderLink } : {}),
|
|
80
|
+
};
|
|
75
81
|
}
|
|
76
82
|
function connectionMissing(message) {
|
|
77
83
|
throw new AdminError("connection_missing", message);
|
|
78
84
|
}
|
|
79
85
|
function sourcesTriedMessage(home) {
|
|
80
|
-
return (
|
|
86
|
+
return ("Tried options (url + key), environment (NYLORUN_RUNTIME_URL + NYLORUN_MANAGEMENT_KEY), " +
|
|
81
87
|
(home
|
|
82
|
-
? `and local Host
|
|
83
|
-
|
|
88
|
+
? `and the local Host (host.json under ${home}, with a managementKey in the linked ` +
|
|
89
|
+
"Project's .nylorun/credentials.json, or the Host root's project-credentials.json or " +
|
|
90
|
+
"cli-credentials.json: `npx nylorun start` writes them)."
|
|
91
|
+
: "and the local Host (no Tenant named: pass `tenant`, set NYLORUN_TENANT or " +
|
|
84
92
|
"NYLORUN_HOME, or run in a Project that `npx nylorun start` linked)."));
|
|
85
93
|
}
|
|
86
|
-
|
|
94
|
+
/** A credentials file must be the user's and not group- or world-readable (POSIX). */
|
|
95
|
+
function assertCredentialsSafe(path, home) {
|
|
87
96
|
if (process.platform === "win32")
|
|
88
97
|
return;
|
|
89
98
|
let stats;
|
|
@@ -94,253 +103,130 @@ function assertCredentialsSafe(path) {
|
|
|
94
103
|
return;
|
|
95
104
|
}
|
|
96
105
|
const uid = typeof process.getuid === "function" ? process.getuid() : undefined;
|
|
97
|
-
if (uid !== undefined && stats.uid !== uid)
|
|
98
|
-
connectionMissing(
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
connectionMissing(`host-credentials.json at ${path} is group- or world-readable; fix permissions (chmod 600). ${sourcesTriedMessage(resolve(join(path, "..")))}`);
|
|
102
|
-
}
|
|
106
|
+
if (uid !== undefined && stats.uid !== uid)
|
|
107
|
+
connectionMissing(`${path} is not owned by the current user. ${sourcesTriedMessage(home)}`);
|
|
108
|
+
if ((stats.mode & 0o077) !== 0)
|
|
109
|
+
connectionMissing(`${path} is group- or world-readable; fix permissions (chmod 600). ${sourcesTriedMessage(home)}`);
|
|
103
110
|
}
|
|
104
|
-
function
|
|
105
|
-
const configPath = join(home, "host.json");
|
|
106
|
-
const credentialsPath = join(home, "host-credentials.json");
|
|
107
|
-
let config;
|
|
108
|
-
let credentials;
|
|
111
|
+
function readJson(path) {
|
|
109
112
|
try {
|
|
110
|
-
|
|
113
|
+
const value = JSON.parse(readFileSync(path, "utf8"));
|
|
114
|
+
return value && typeof value === "object" && !Array.isArray(value)
|
|
115
|
+
? value
|
|
116
|
+
: undefined;
|
|
111
117
|
}
|
|
112
118
|
catch {
|
|
113
119
|
return undefined;
|
|
114
120
|
}
|
|
115
|
-
try {
|
|
116
|
-
assertCredentialsSafe(credentialsPath);
|
|
117
|
-
credentials = JSON.parse(readFileSync(credentialsPath, "utf8"));
|
|
118
|
-
}
|
|
119
|
-
catch (error) {
|
|
120
|
-
if (error instanceof AdminError)
|
|
121
|
-
throw error;
|
|
122
|
-
return undefined;
|
|
123
|
-
}
|
|
124
|
-
if (!config || typeof config !== "object" || Array.isArray(config)) {
|
|
125
|
-
return undefined;
|
|
126
|
-
}
|
|
127
|
-
if (!credentials ||
|
|
128
|
-
typeof credentials !== "object" ||
|
|
129
|
-
Array.isArray(credentials)) {
|
|
130
|
-
return undefined;
|
|
131
|
-
}
|
|
132
|
-
const host = config.host;
|
|
133
|
-
const port = config.port;
|
|
134
|
-
const adminPort = config.adminPort;
|
|
135
|
-
const adminKey = credentials.adminKey;
|
|
136
|
-
if (typeof host !== "string" || typeof port !== "number")
|
|
137
|
-
return undefined;
|
|
138
|
-
if (typeof adminKey !== "string" || !/^[0-9a-f]{64}$/.test(adminKey)) {
|
|
139
|
-
return undefined;
|
|
140
|
-
}
|
|
141
|
-
return {
|
|
142
|
-
url: `http://${host}:${port}`,
|
|
143
|
-
// An older Host serves the Admin API on its only port.
|
|
144
|
-
adminUrl: `http://${host}:${typeof adminPort === "number" ? adminPort : port}`,
|
|
145
|
-
key: adminKey,
|
|
146
|
-
};
|
|
147
121
|
}
|
|
148
122
|
/**
|
|
149
|
-
*
|
|
123
|
+
* The local Host's URL (`host.json`) and a management key: from the linked Project's
|
|
124
|
+
* `.nylorun/credentials.json`, else the Host root's `project-credentials.json`, else its
|
|
125
|
+
* `cli-credentials.json`.
|
|
150
126
|
*/
|
|
127
|
+
function readLocalHost(home, project) {
|
|
128
|
+
const config = readJson(join(home, "host.json"));
|
|
129
|
+
if (typeof config?.host !== "string" || typeof config.port !== "number")
|
|
130
|
+
return undefined;
|
|
131
|
+
const files = [
|
|
132
|
+
...(project ? [join(project, "credentials.json")] : []),
|
|
133
|
+
join(home, "project-credentials.json"),
|
|
134
|
+
join(home, "cli-credentials.json"),
|
|
135
|
+
];
|
|
136
|
+
for (const path of files) {
|
|
137
|
+
const key = readJson(path)?.managementKey;
|
|
138
|
+
if (typeof key !== "string" || !/^[0-9a-f]{64}$/.test(key))
|
|
139
|
+
continue;
|
|
140
|
+
assertCredentialsSafe(path, home);
|
|
141
|
+
return { url: `http://${config.host}:${config.port}`, key };
|
|
142
|
+
}
|
|
143
|
+
return undefined;
|
|
144
|
+
}
|
|
145
|
+
/** Resolve the Management API connection once: options → environment → local Host. */
|
|
151
146
|
export function resolveAdminConnection(options) {
|
|
152
|
-
const { home, olderLink } = resolveHome(options);
|
|
147
|
+
const { home, project, olderLink } = resolveHome(options);
|
|
153
148
|
const optionUrl = options?.url?.trim() || undefined;
|
|
154
149
|
const optionKey = options?.key?.trim() || undefined;
|
|
155
150
|
if (optionUrl || optionKey) {
|
|
156
|
-
if (!optionUrl || !optionKey)
|
|
157
|
-
connectionMissing(`Incomplete
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
if (
|
|
169
|
-
if (!envUrl || !envKey) {
|
|
170
|
-
connectionMissing(`Incomplete Admin API environment: both NYLORUN_ADMIN_URL and NYLORUN_ADMIN_KEY are required. ${sourcesTriedMessage(home)}`);
|
|
171
|
-
}
|
|
172
|
-
return {
|
|
173
|
-
url: envUrl.replace(/\/$/, ""),
|
|
174
|
-
key: envKey,
|
|
175
|
-
source: "environment",
|
|
176
|
-
...(home ? { home } : {}),
|
|
177
|
-
};
|
|
178
|
-
}
|
|
179
|
-
if (olderLink) {
|
|
151
|
+
if (!optionUrl || !optionKey)
|
|
152
|
+
connectionMissing(`Incomplete options: both url and key (a management key) are required. ${sourcesTriedMessage(home)}`);
|
|
153
|
+
return { url: optionUrl.replace(/\/$/, ""), key: optionKey, source: "options", ...(home ? { home } : {}) };
|
|
154
|
+
}
|
|
155
|
+
// NYLORUN_RUNTIME_URL alone is an app's (with NYLORUN_SERVER_KEY): only the key selects this.
|
|
156
|
+
const envKey = env("NYLORUN_MANAGEMENT_KEY");
|
|
157
|
+
if (envKey) {
|
|
158
|
+
const envUrl = env("NYLORUN_RUNTIME_URL");
|
|
159
|
+
if (!envUrl)
|
|
160
|
+
connectionMissing(`Incomplete environment: NYLORUN_MANAGEMENT_KEY needs NYLORUN_RUNTIME_URL. ${sourcesTriedMessage(home)}`);
|
|
161
|
+
return { url: envUrl.replace(/\/$/, ""), key: envKey, source: "environment", ...(home ? { home } : {}) };
|
|
162
|
+
}
|
|
163
|
+
if (olderLink && !home)
|
|
180
164
|
connectionMissing(`The Project link at ${olderLink} is from an older nylorun. Run "npx nylorun start" in this project to link it again.`);
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
url: local.url.replace(/\/$/, ""),
|
|
186
|
-
adminUrl: local.adminUrl.replace(/\/$/, ""),
|
|
187
|
-
key: local.key,
|
|
188
|
-
source: "local-host",
|
|
189
|
-
home,
|
|
190
|
-
};
|
|
191
|
-
}
|
|
192
|
-
connectionMissing(`Could not resolve Admin API connection. ${sourcesTriedMessage(home)}`);
|
|
165
|
+
const local = home === undefined ? undefined : readLocalHost(home, project);
|
|
166
|
+
if (local)
|
|
167
|
+
return { url: local.url, key: local.key, source: "local-host", home };
|
|
168
|
+
connectionMissing(`Could not resolve the Management API connection. ${sourcesTriedMessage(home)}`);
|
|
193
169
|
}
|
|
194
170
|
function parseProtocolRange(value) {
|
|
195
|
-
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
171
|
+
if (!value || typeof value !== "object" || Array.isArray(value))
|
|
196
172
|
return undefined;
|
|
197
|
-
}
|
|
198
173
|
const record = value;
|
|
199
174
|
if (typeof record.min !== "number" ||
|
|
200
175
|
typeof record.max !== "number" ||
|
|
201
176
|
!Array.isArray(record.features) ||
|
|
202
|
-
!record.features.every((f) => typeof f === "string"))
|
|
177
|
+
!record.features.every((f) => typeof f === "string"))
|
|
203
178
|
return undefined;
|
|
204
|
-
}
|
|
205
|
-
return {
|
|
206
|
-
min: record.min,
|
|
207
|
-
max: record.max,
|
|
208
|
-
features: record.features,
|
|
209
|
-
};
|
|
179
|
+
return { min: record.min, max: record.max, features: record.features };
|
|
210
180
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
181
|
+
/** Throws unless the Host's `/health` advertises this client's protocol and features. */
|
|
182
|
+
async function checkHealth(url) {
|
|
183
|
+
const response = await fetch(`${url}/health`, { redirect: "error" });
|
|
184
|
+
const body = await response.json().catch(() => undefined);
|
|
185
|
+
if (!response.ok) {
|
|
186
|
+
const rejected = RejectedResponseSchema.safeParse(body);
|
|
187
|
+
throw rejected.success
|
|
188
|
+
? new AdminError(rejected.data.code, rejected.data.message, { status: response.status })
|
|
189
|
+
: new AdminError("not_found", `Host /health failed (${response.status})`, {
|
|
190
|
+
status: response.status,
|
|
191
|
+
details: body,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
const protocol = parseProtocolRange(body?.protocol);
|
|
195
|
+
if (!protocol)
|
|
196
|
+
throw new AdminError("incompatible_host", "Host /health did not advertise a protocol range.");
|
|
197
|
+
const result = checkCompatibility({ version: PROTOCOL_VERSION, required: [...PROTOCOL_FEATURES] }, protocol);
|
|
198
|
+
if (!result.ok) {
|
|
199
|
+
const detail = result.reason === "version"
|
|
200
|
+
? `client protocol ${result.client} is outside Host range ${result.host.min}–${result.host.max}`
|
|
201
|
+
: `Host is missing required features: ${result.missing.join(", ")}`;
|
|
202
|
+
throw new AdminError("incompatible_host", `Incompatible Host: ${detail}`, { details: result });
|
|
224
203
|
}
|
|
225
|
-
throw new AdminError("not_found", `Admin request failed (${status})`, {
|
|
226
|
-
status,
|
|
227
|
-
details: body,
|
|
228
|
-
});
|
|
229
204
|
}
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
}
|
|
205
|
+
/**
|
|
206
|
+
* `fetch` that checks the Host's `/health` before the first request, and again after a `426`,
|
|
207
|
+
* retrying the request once when the Host still serves this client.
|
|
208
|
+
*/
|
|
209
|
+
function compatibleFetch(url) {
|
|
210
|
+
let checked;
|
|
211
|
+
const check = () => (checked ??= checkHealth(url).catch((error) => {
|
|
212
|
+
checked = undefined;
|
|
213
|
+
throw error;
|
|
214
|
+
}));
|
|
215
|
+
return async (input, init) => {
|
|
216
|
+
await check();
|
|
217
|
+
const response = await fetch(input, init);
|
|
218
|
+
if (response.status !== 426)
|
|
219
|
+
return response;
|
|
220
|
+
checked = undefined;
|
|
221
|
+
await check();
|
|
222
|
+
return fetch(input, init);
|
|
223
|
+
};
|
|
240
224
|
}
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
url;
|
|
244
|
-
/** Where Admin API requests go: the operator listener, or `url` on a single-port Host. */
|
|
245
|
-
adminUrl;
|
|
225
|
+
/** What `createAdmin` returns: the Management API client, and where its connection came from. */
|
|
226
|
+
export class AdminClient extends ManagementClient {
|
|
246
227
|
source;
|
|
247
|
-
key;
|
|
248
|
-
compatible = false;
|
|
249
228
|
constructor(resolved) {
|
|
250
|
-
|
|
251
|
-
this.adminUrl = resolved.adminUrl ?? resolved.url;
|
|
229
|
+
super({ url: resolved.url, key: resolved.key, fetch: compatibleFetch(resolved.url) });
|
|
252
230
|
this.source = resolved.source;
|
|
253
|
-
this.key = resolved.key;
|
|
254
|
-
}
|
|
255
|
-
clearCompatibilityCache() {
|
|
256
|
-
this.compatible = false;
|
|
257
|
-
}
|
|
258
|
-
adminHeaders(init = {}) {
|
|
259
|
-
const headers = new Headers(init.headers);
|
|
260
|
-
headers.set("Authorization", `Bearer ${this.key}`);
|
|
261
|
-
headers.set(PROTOCOL_HEADER, String(PROTOCOL_VERSION));
|
|
262
|
-
headers.set("Accept", "application/json");
|
|
263
|
-
if (init.body)
|
|
264
|
-
headers.set("Content-Type", "application/json");
|
|
265
|
-
return headers;
|
|
266
|
-
}
|
|
267
|
-
async ensureCompatible(signal) {
|
|
268
|
-
if (this.compatible)
|
|
269
|
-
return;
|
|
270
|
-
const response = await fetch(`${this.adminUrl}/health`, {
|
|
271
|
-
method: "GET",
|
|
272
|
-
redirect: "error",
|
|
273
|
-
signal,
|
|
274
|
-
});
|
|
275
|
-
const body = await readBody(response);
|
|
276
|
-
if (!response.ok) {
|
|
277
|
-
throwFromResponse(response.status, body);
|
|
278
|
-
}
|
|
279
|
-
const protocol = parseProtocolRange(body && typeof body === "object" && !Array.isArray(body)
|
|
280
|
-
? body.protocol
|
|
281
|
-
: undefined);
|
|
282
|
-
if (!protocol) {
|
|
283
|
-
throw new AdminError("incompatible_host", "Host /health did not advertise a protocol range.");
|
|
284
|
-
}
|
|
285
|
-
const result = checkCompatibility({ version: PROTOCOL_VERSION, required: [...PROTOCOL_FEATURES] }, protocol);
|
|
286
|
-
if (!result.ok) {
|
|
287
|
-
const detail = result.reason === "version"
|
|
288
|
-
? `client protocol ${result.client} is outside Host range ${result.host.min}–${result.host.max}`
|
|
289
|
-
: `Host is missing required features: ${result.missing.join(", ")}`;
|
|
290
|
-
throw new AdminError("incompatible_host", `Incompatible Host: ${detail}`, { details: result });
|
|
291
|
-
}
|
|
292
|
-
this.compatible = true;
|
|
293
|
-
}
|
|
294
|
-
async request(path, init = {}, options = {}) {
|
|
295
|
-
await this.ensureCompatible(init.signal === null ? undefined : init.signal);
|
|
296
|
-
const response = await fetch(this.adminUrl + path, {
|
|
297
|
-
...init,
|
|
298
|
-
headers: this.adminHeaders(init),
|
|
299
|
-
redirect: "error",
|
|
300
|
-
});
|
|
301
|
-
if (response.status === 426) {
|
|
302
|
-
this.clearCompatibilityCache();
|
|
303
|
-
if (!options.retried426) {
|
|
304
|
-
await this.ensureCompatible(init.signal === null ? undefined : init.signal);
|
|
305
|
-
return this.request(path, init, { retried426: true });
|
|
306
|
-
}
|
|
307
|
-
const body = await readBody(response);
|
|
308
|
-
const protocol = parseProtocolRange(body && typeof body === "object" && !Array.isArray(body)
|
|
309
|
-
? body.protocol
|
|
310
|
-
: undefined);
|
|
311
|
-
if (protocol) {
|
|
312
|
-
const result = checkCompatibility({ version: PROTOCOL_VERSION, required: [...PROTOCOL_FEATURES] }, protocol);
|
|
313
|
-
if (!result.ok) {
|
|
314
|
-
throw new AdminError("incompatible_host", "Incompatible Host after protocol upgrade challenge.", { status: 426, details: result });
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
throwFromResponse(426, body);
|
|
318
|
-
}
|
|
319
|
-
return response;
|
|
320
|
-
}
|
|
321
|
-
async json(path, method = "GET", body) {
|
|
322
|
-
const response = await this.request(path, {
|
|
323
|
-
method,
|
|
324
|
-
...(body === undefined ? {} : { body: JSON.stringify(body) }),
|
|
325
|
-
signal: AbortSignal.timeout(15_000),
|
|
326
|
-
});
|
|
327
|
-
if (response.status === 204)
|
|
328
|
-
return undefined;
|
|
329
|
-
const parsed = await readBody(response);
|
|
330
|
-
if (!response.ok)
|
|
331
|
-
throwFromResponse(response.status, parsed);
|
|
332
|
-
return parsed;
|
|
333
|
-
}
|
|
334
|
-
async status() {
|
|
335
|
-
const body = await this.json("/v1/admin/status");
|
|
336
|
-
return AdminStatusSchema.parse(body);
|
|
337
|
-
}
|
|
338
|
-
/**
|
|
339
|
-
* The key of derived principal `principalId` on `tenantId`, from this client's admin key.
|
|
340
|
-
* Valid when the Host registered that principal (`NYLORUN_DERIVED_PRINCIPALS`, default
|
|
341
|
-
* `project`) on its Tenant.
|
|
342
|
-
*/
|
|
343
|
-
deriveTenantKey(tenantId, principalId) {
|
|
344
|
-
return deriveTenantKey(this.key, tenantId, principalId);
|
|
345
231
|
}
|
|
346
232
|
}
|
|
@@ -1,20 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Deterministic Studio key
|
|
3
|
-
*
|
|
4
|
-
*
|
|
2
|
+
* Deterministic Studio key, derived from the admin key: HMAC-SHA256 over
|
|
3
|
+
* `nylorun/studio/v2`. The same admin key always produces the same key. It names
|
|
4
|
+
* no Tenant (protocol 8): Studio derives it before it learns the Tenant's id, and
|
|
5
|
+
* an installation has one Tenant. Studio derives it in memory; the Tenant stores
|
|
6
|
+
* only its hash, as principal `studio`. It is the only derived key.
|
|
5
7
|
*/
|
|
6
|
-
export declare function deriveStudioToken(adminKey: string
|
|
7
|
-
/**
|
|
8
|
-
* The derived principal that Projects use. The Host registers it on its Tenant by default
|
|
9
|
-
* (`NYLORUN_DERIVED_PRINCIPALS`), and `nylorun start` derives its key from the Tenant's admin
|
|
10
|
-
* key into the Project's `.nylorun/credentials.json`.
|
|
11
|
-
*/
|
|
12
|
-
export declare const PROJECT_PRINCIPAL_ID = "project";
|
|
13
|
-
/**
|
|
14
|
-
* Deterministic key of a derived principal (`principalId`, e.g. `babai`) on one Tenant,
|
|
15
|
-
* derived from the admin key. The Tenant stores only its hash, registered when the Host creates
|
|
16
|
-
* its Tenant with the principals it is configured with (`NYLORUN_DERIVED_PRINCIPALS`, feature
|
|
17
|
-
* `derived-principals`); the client recomputes the key when it needs it and stores nothing.
|
|
18
|
-
* Rotating the admin key rotates every derived key.
|
|
19
|
-
*/
|
|
20
|
-
export declare function deriveTenantKey(adminKey: string, tenantId: string, principalId: string): string;
|
|
8
|
+
export declare function deriveStudioToken(adminKey: string): string;
|
|
@@ -1,40 +1,13 @@
|
|
|
1
1
|
import { createHmac } from "node:crypto";
|
|
2
2
|
/**
|
|
3
|
-
* Deterministic Studio key
|
|
4
|
-
*
|
|
5
|
-
*
|
|
3
|
+
* Deterministic Studio key, derived from the admin key: HMAC-SHA256 over
|
|
4
|
+
* `nylorun/studio/v2`. The same admin key always produces the same key. It names
|
|
5
|
+
* no Tenant (protocol 8): Studio derives it before it learns the Tenant's id, and
|
|
6
|
+
* an installation has one Tenant. Studio derives it in memory; the Tenant stores
|
|
7
|
+
* only its hash, as principal `studio`. It is the only derived key.
|
|
6
8
|
*/
|
|
7
|
-
export function deriveStudioToken(adminKey
|
|
8
|
-
const message = Buffer.
|
|
9
|
-
Buffer.from("nylorun/studio/v1", "utf8"),
|
|
10
|
-
Buffer.from([0]),
|
|
11
|
-
Buffer.from(tenantId, "utf8"),
|
|
12
|
-
]);
|
|
13
|
-
return createHmac("sha256", Buffer.from(adminKey, "utf8"))
|
|
14
|
-
.update(message)
|
|
15
|
-
.digest("hex");
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* The derived principal that Projects use. The Host registers it on its Tenant by default
|
|
19
|
-
* (`NYLORUN_DERIVED_PRINCIPALS`), and `nylorun start` derives its key from the Tenant's admin
|
|
20
|
-
* key into the Project's `.nylorun/credentials.json`.
|
|
21
|
-
*/
|
|
22
|
-
export const PROJECT_PRINCIPAL_ID = "project";
|
|
23
|
-
/**
|
|
24
|
-
* Deterministic key of a derived principal (`principalId`, e.g. `babai`) on one Tenant,
|
|
25
|
-
* derived from the admin key. The Tenant stores only its hash, registered when the Host creates
|
|
26
|
-
* its Tenant with the principals it is configured with (`NYLORUN_DERIVED_PRINCIPALS`, feature
|
|
27
|
-
* `derived-principals`); the client recomputes the key when it needs it and stores nothing.
|
|
28
|
-
* Rotating the admin key rotates every derived key.
|
|
29
|
-
*/
|
|
30
|
-
export function deriveTenantKey(adminKey, tenantId, principalId) {
|
|
31
|
-
const message = Buffer.concat([
|
|
32
|
-
Buffer.from("nylorun/principal/v1", "utf8"),
|
|
33
|
-
Buffer.from([0]),
|
|
34
|
-
Buffer.from(principalId, "utf8"),
|
|
35
|
-
Buffer.from([0]),
|
|
36
|
-
Buffer.from(tenantId, "utf8"),
|
|
37
|
-
]);
|
|
9
|
+
export function deriveStudioToken(adminKey) {
|
|
10
|
+
const message = Buffer.from("nylorun/studio/v2", "utf8");
|
|
38
11
|
return createHmac("sha256", Buffer.from(adminKey, "utf8"))
|
|
39
12
|
.update(message)
|
|
40
13
|
.digest("hex");
|
package/dist/index.d.ts
CHANGED
|
@@ -1,32 +1,27 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { HostTenant, OperatorKey, PutOperatorKeyResponse, TenantEnvelope } from "@nylorun/core/contracts";
|
|
2
2
|
import { ERROR_CODES, PROTOCOL_FEATURES, compareVersions, type ErrorCode } from "@nylorun/core/compatibility";
|
|
3
|
-
import { tenantHostRoot, type AdminConnectionOptions } from "./client.js";
|
|
4
|
-
import {
|
|
3
|
+
import { AdminClient, tenantHostRoot, type AdminConnectionOptions, type AdminSource } from "./client.js";
|
|
4
|
+
import { deriveStudioToken } from "./derived-credentials.js";
|
|
5
5
|
import { AdminError } from "./errors.js";
|
|
6
6
|
export { ERROR_CODES, PROTOCOL_FEATURES, compareVersions };
|
|
7
7
|
export type { ErrorCode };
|
|
8
8
|
export { AdminError };
|
|
9
|
-
export {
|
|
9
|
+
export { deriveStudioToken, tenantHostRoot };
|
|
10
10
|
export { mintStudioLoginToken } from "./studio-login.js";
|
|
11
|
-
export
|
|
12
|
-
/** The Host's Tenant API URL. */
|
|
13
|
-
readonly url: string;
|
|
14
|
-
/** Where Admin API requests go: the operator listener, or `url` on a single-port Host. */
|
|
15
|
-
readonly adminUrl: string;
|
|
16
|
-
readonly source: "options" | "environment" | "local-host";
|
|
17
|
-
/** The Host's status, with the one Tenant it serves (`status.tenant`). */
|
|
18
|
-
status(): Promise<AdminStatus>;
|
|
19
|
-
/**
|
|
20
|
-
* The key of a derived principal on the Host's Tenant, from this client's admin key. The
|
|
21
|
-
* Host registers the principals it is configured with (`NYLORUN_DERIVED_PRINCIPALS`, default
|
|
22
|
-
* `project`) when it creates its Tenant.
|
|
23
|
-
*/
|
|
24
|
-
deriveTenantKey(tenantId: string, principalId: string): string;
|
|
25
|
-
}
|
|
11
|
+
export { ManagementClient, createManagementClient, type ManagementClientOptions, type ManagementKeys, type ManagementModels, type ManagementSettings, type ManagementSigningKeys, type ManagementTenant, type ManagementVaults, } from "./management.js";
|
|
26
12
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* `
|
|
13
|
+
* The Management API client for the installation (`/v1/tenant/*`): `tenant`, `keys`
|
|
14
|
+
* (application keys), `models`, `vaults`, `signingKeys` and `settings`, with a management key.
|
|
15
|
+
* `source` says where the connection came from.
|
|
16
|
+
*/
|
|
17
|
+
export type Admin = AdminClient;
|
|
18
|
+
/**
|
|
19
|
+
* Resolution: explicit `url` + `key` (a management key) → `NYLORUN_RUNTIME_URL` +
|
|
20
|
+
* `NYLORUN_MANAGEMENT_KEY` → the local Host: the URL from `host.json` in its Host root
|
|
21
|
+
* (`home`, `NYLORUN_HOME`, or the Tenant named by `tenant`, `NYLORUN_TENANT` or the Project link
|
|
22
|
+
* under `cwd`: `~/.nylorun/tenants/<tenant>/`), and the management key from the linked
|
|
23
|
+
* Project's `.nylorun/credentials.json`, else the Host root's `project-credentials.json`, else
|
|
24
|
+
* its `cli-credentials.json`. The first request checks the Host's `/health` first.
|
|
30
25
|
*/
|
|
31
26
|
export declare function createAdmin(options?: AdminConnectionOptions): Admin;
|
|
32
|
-
export type { AdminConnectionOptions,
|
|
27
|
+
export type { AdminConnectionOptions, AdminSource, HostTenant, OperatorKey, PutOperatorKeyResponse, TenantEnvelope, };
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import { ERROR_CODES, PROTOCOL_FEATURES, compareVersions, } from "@nylorun/core/compatibility";
|
|
2
2
|
import { AdminClient, resolveAdminConnection, tenantHostRoot, } from "./client.js";
|
|
3
|
-
import {
|
|
3
|
+
import { deriveStudioToken } from "./derived-credentials.js";
|
|
4
4
|
import { AdminError } from "./errors.js";
|
|
5
5
|
export { ERROR_CODES, PROTOCOL_FEATURES, compareVersions };
|
|
6
6
|
export { AdminError };
|
|
7
|
-
export {
|
|
7
|
+
export { deriveStudioToken, tenantHostRoot };
|
|
8
8
|
export { mintStudioLoginToken } from "./studio-login.js";
|
|
9
|
+
export { ManagementClient, createManagementClient, } from "./management.js";
|
|
9
10
|
/**
|
|
10
|
-
* Resolution: explicit `url` + `key` → `
|
|
11
|
-
*
|
|
12
|
-
* `
|
|
11
|
+
* Resolution: explicit `url` + `key` (a management key) → `NYLORUN_RUNTIME_URL` +
|
|
12
|
+
* `NYLORUN_MANAGEMENT_KEY` → the local Host: the URL from `host.json` in its Host root
|
|
13
|
+
* (`home`, `NYLORUN_HOME`, or the Tenant named by `tenant`, `NYLORUN_TENANT` or the Project link
|
|
14
|
+
* under `cwd`: `~/.nylorun/tenants/<tenant>/`), and the management key from the linked
|
|
15
|
+
* Project's `.nylorun/credentials.json`, else the Host root's `project-credentials.json`, else
|
|
16
|
+
* its `cli-credentials.json`. The first request checks the Host's `/health` first.
|
|
13
17
|
*/
|
|
14
18
|
export function createAdmin(options) {
|
|
15
19
|
return new AdminClient(resolveAdminConnection(options));
|