pico-engine 1.6.1 → 1.6.4

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.
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isLevelDbOpenError = isLevelDbOpenError;
4
+ exports.openClassicLevelWithRecovery = openClassicLevelWithRecovery;
5
+ const classic_level_1 = require("classic-level");
6
+ const fs = require("fs/promises");
7
+ const makeDir = require("make-dir");
8
+ const path = require("path");
9
+ function errCode(err) {
10
+ if (err && typeof err === "object" && "code" in err) {
11
+ const code = err.code;
12
+ return typeof code === "string" ? code : undefined;
13
+ }
14
+ return undefined;
15
+ }
16
+ function errMessage(err) {
17
+ if (err instanceof Error) {
18
+ return err.message;
19
+ }
20
+ return String(err);
21
+ }
22
+ function isLevelDbOpenError(err) {
23
+ const code = errCode(err);
24
+ if (code === "LEVEL_LOCKED") {
25
+ return true;
26
+ }
27
+ const message = errMessage(err);
28
+ return /LOCK|corrupt|IO error|database/i.test(message);
29
+ }
30
+ async function removeStaleLockFile(location, log) {
31
+ const lockPath = path.join(location, "LOCK");
32
+ try {
33
+ await fs.unlink(lockPath);
34
+ log === null || log === void 0 ? void 0 : log.info("Removed stale LevelDB LOCK file", { location });
35
+ }
36
+ catch (err) {
37
+ if ((err === null || err === void 0 ? void 0 : err.code) !== "ENOENT") {
38
+ throw err;
39
+ }
40
+ }
41
+ }
42
+ async function closeClassicLevelQuietly(db) {
43
+ try {
44
+ await db.close();
45
+ }
46
+ catch (_a) {
47
+ // May never have opened.
48
+ }
49
+ }
50
+ async function openOnce(location, options) {
51
+ const db = new classic_level_1.ClassicLevel(location, options);
52
+ try {
53
+ await db.open();
54
+ return db;
55
+ }
56
+ catch (err) {
57
+ // A failed open can still leave a handle; if it is GC'd later it closes
58
+ // the underlying store and breaks a subsequent successful open on the same path.
59
+ await closeClassicLevelQuietly(db);
60
+ throw err;
61
+ }
62
+ }
63
+ /**
64
+ * Open a ClassicLevel database, recovering from stale locks or minor corruption
65
+ * left by an unclean shutdown (e.g. container SIGKILL).
66
+ *
67
+ * Recovery order (non-destructive first):
68
+ * 1. Normal open
69
+ * 2. Remove stale LOCK and retry
70
+ * 3. LevelDB repair and retry
71
+ */
72
+ async function openClassicLevelWithRecovery(location, options, log) {
73
+ await makeDir(location);
74
+ try {
75
+ return await openOnce(location, options);
76
+ }
77
+ catch (firstErr) {
78
+ if (!isLevelDbOpenError(firstErr)) {
79
+ throw firstErr;
80
+ }
81
+ log === null || log === void 0 ? void 0 : log.warn("LevelDB open failed; attempting recovery", {
82
+ location,
83
+ error: firstErr,
84
+ });
85
+ }
86
+ await removeStaleLockFile(location, log);
87
+ try {
88
+ return await openOnce(location, options);
89
+ }
90
+ catch (secondErr) {
91
+ if (!isLevelDbOpenError(secondErr)) {
92
+ throw secondErr;
93
+ }
94
+ log === null || log === void 0 ? void 0 : log.warn("LevelDB open failed after lock cleanup; running repair", {
95
+ location,
96
+ error: secondErr,
97
+ });
98
+ }
99
+ await classic_level_1.ClassicLevel.repair(location);
100
+ log === null || log === void 0 ? void 0 : log.info("LevelDB repair finished", { location });
101
+ return openOnce(location, options);
102
+ }
package/dist/logging.js CHANGED
@@ -7,6 +7,8 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
7
7
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
8
8
  };
9
9
  Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.resolveEngineLogFilePath = resolveEngineLogFilePath;
11
+ exports.makeEngineLogWriter = makeEngineLogWriter;
10
12
  exports.makeRotatingFileLogWriter = makeRotatingFileLogWriter;
11
13
  exports.getPicoLogs = getPicoLogs;
12
14
  const fs = require("fs");
@@ -15,21 +17,55 @@ const krl_stdlib_1 = require("krl-stdlib");
15
17
  const path = require("path");
16
18
  const rfs = require("rotating-file-stream");
17
19
  const logStreams = {};
20
+ function stdoutOnlyLogWriter() {
21
+ const isTest = process.env.NODE_ENV === "test";
22
+ return (line) => {
23
+ if (!isTest) {
24
+ process.stdout.write(line);
25
+ }
26
+ };
27
+ }
28
+ function attachSafeLogStreamHandlers(fileStream, filePath) {
29
+ fileStream.on("error", (err) => {
30
+ process.stderr.write(`pico-engine log file write failed (${filePath}): ${err.message}\n`);
31
+ });
32
+ }
18
33
  function getRotatingFileStream(filePath) {
19
34
  if (!logStreams[filePath]) {
20
35
  const filename = path.basename(filePath);
21
- logStreams[filePath] = rfs((time, index) => {
36
+ const fileStream = rfs((time, index) => {
22
37
  if (!time)
23
38
  return filename;
24
39
  return filename + "." + index;
25
40
  }, {
26
41
  path: path.dirname(filePath),
27
- size: "100M", // rotate every 10 MegaBytes written
42
+ size: "100M", // rotate every 100 MegaBytes written
28
43
  maxFiles: 12,
29
44
  });
45
+ attachSafeLogStreamHandlers(fileStream, filePath);
46
+ logStreams[filePath] = fileStream;
30
47
  }
31
48
  return logStreams[filePath];
32
49
  }
50
+ function resolveEngineLogFilePath(home) {
51
+ const configured = process.env.PICO_ENGINE_LOG_FILE;
52
+ if (configured === "0" ||
53
+ configured === "-" ||
54
+ configured === "stdout" ||
55
+ configured === "") {
56
+ return null;
57
+ }
58
+ if (typeof configured === "string" && configured.length > 0) {
59
+ return path.resolve(configured);
60
+ }
61
+ return path.resolve(home, "pico-engine.log");
62
+ }
63
+ function makeEngineLogWriter(filePath) {
64
+ if (!filePath) {
65
+ return stdoutOnlyLogWriter();
66
+ }
67
+ return makeRotatingFileLogWriter(filePath);
68
+ }
33
69
  function makeRotatingFileLogWriter(filePath) {
34
70
  const fileStream = getRotatingFileStream(filePath);
35
71
  const isTest = process.env.NODE_ENV === "test";
@@ -37,13 +73,20 @@ function makeRotatingFileLogWriter(filePath) {
37
73
  if (!isTest) {
38
74
  process.stdout.write(line);
39
75
  }
40
- fileStream.write(line);
76
+ fileStream.write(line, (err) => {
77
+ if (err) {
78
+ process.stderr.write(`pico-engine log file write failed (${filePath}): ${err.message}\n`);
79
+ }
80
+ });
41
81
  }
42
82
  return write;
43
83
  }
44
84
  async function getPicoLogs(filePath, picoId) {
45
85
  var _a, e_1, _b, _c;
46
86
  const output = [];
87
+ if (!(await fs.promises.stat(filePath).catch(() => null))) {
88
+ return output;
89
+ }
47
90
  const rl = readline.createInterface({
48
91
  input: fs.createReadStream(filePath),
49
92
  });
@@ -174,13 +174,16 @@ class OAuthService {
174
174
  channel.tags.includes("ingress")) {
175
175
  return false;
176
176
  }
177
+ if ((0, channelEligibility_1.isOAuthEligibleChannel)(channel)) {
178
+ return true;
179
+ }
180
+ if ((0, channelEligibility_1.isMeshOAuthExemptChannel)(channel)) {
181
+ return false;
182
+ }
177
183
  }
178
184
  catch (_e) {
179
185
  // channel not found — fall through
180
186
  }
181
- if (await this.channelRequiresBearer(eci)) {
182
- return true;
183
- }
184
187
  return this.meshRequiresOAuth(eci);
185
188
  }
186
189
  async validateBearerToken(token, channelEci) {
@@ -1,9 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OAUTH_WEBHOOK_TAG = void 0;
3
+ exports.MESH_OAUTH_EXEMPT_TAG = exports.OAUTH_WEBHOOK_TAG = void 0;
4
4
  exports.isSubscriptionChannel = isSubscriptionChannel;
5
5
  exports.isOAuthEligibleChannel = isOAuthEligibleChannel;
6
+ exports.isMeshOAuthExemptChannel = isMeshOAuthExemptChannel;
6
7
  exports.OAUTH_WEBHOOK_TAG = "oauth-webhook";
8
+ /** Opt out of mesh-wide /sky/* OAuth when the root has io.picolabs.oauth installed. */
9
+ exports.MESH_OAUTH_EXEMPT_TAG = "mesh-oauth-exempt";
7
10
  const SUBSCRIPTION_CHANNEL_TAGS = new Set([
8
11
  "wellknown_rx",
9
12
  "tx_rx",
@@ -24,3 +27,6 @@ function isOAuthEligibleChannel(channel) {
24
27
  }
25
28
  return channel.tags.includes(exports.OAUTH_WEBHOOK_TAG);
26
29
  }
30
+ function isMeshOAuthExemptChannel(channel) {
31
+ return channel.tags.some((tag) => tag.toLowerCase() === exports.MESH_OAUTH_EXEMPT_TAG);
32
+ }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.initOAuthModule = exports.isChannelUnderRoot = exports.rootPicoIdForChannelEci = exports.rootHasOAuthMeshRuleset = exports.meshRequiresOAuth = exports.OAUTH_MESH_RULESET_RID = exports.isSubscriptionChannel = exports.isOAuthEligibleChannel = exports.OAUTH_WEBHOOK_TAG = exports.renderConsentHtml = exports.parseAuthorizeQuery = exports.parseApproveBody = exports.OAuthError = exports.OAuthService = void 0;
3
+ exports.initOAuthModule = exports.isChannelUnderRoot = exports.rootPicoIdForChannelEci = exports.rootHasOAuthMeshRuleset = exports.meshRequiresOAuth = exports.OAUTH_MESH_RULESET_RID = exports.isSubscriptionChannel = exports.isMeshOAuthExemptChannel = exports.isOAuthEligibleChannel = exports.MESH_OAUTH_EXEMPT_TAG = exports.OAUTH_WEBHOOK_TAG = exports.renderConsentHtml = exports.parseAuthorizeQuery = exports.parseApproveBody = exports.OAuthError = exports.OAuthService = void 0;
4
4
  var OAuthService_1 = require("./OAuthService");
5
5
  Object.defineProperty(exports, "OAuthService", { enumerable: true, get: function () { return OAuthService_1.OAuthService; } });
6
6
  Object.defineProperty(exports, "OAuthError", { enumerable: true, get: function () { return OAuthService_1.OAuthError; } });
@@ -9,7 +9,9 @@ Object.defineProperty(exports, "parseAuthorizeQuery", { enumerable: true, get: f
9
9
  Object.defineProperty(exports, "renderConsentHtml", { enumerable: true, get: function () { return OAuthService_1.renderConsentHtml; } });
10
10
  var channelEligibility_1 = require("./channelEligibility");
11
11
  Object.defineProperty(exports, "OAUTH_WEBHOOK_TAG", { enumerable: true, get: function () { return channelEligibility_1.OAUTH_WEBHOOK_TAG; } });
12
+ Object.defineProperty(exports, "MESH_OAUTH_EXEMPT_TAG", { enumerable: true, get: function () { return channelEligibility_1.MESH_OAUTH_EXEMPT_TAG; } });
12
13
  Object.defineProperty(exports, "isOAuthEligibleChannel", { enumerable: true, get: function () { return channelEligibility_1.isOAuthEligibleChannel; } });
14
+ Object.defineProperty(exports, "isMeshOAuthExemptChannel", { enumerable: true, get: function () { return channelEligibility_1.isMeshOAuthExemptChannel; } });
13
15
  Object.defineProperty(exports, "isSubscriptionChannel", { enumerable: true, get: function () { return channelEligibility_1.isSubscriptionChannel; } });
14
16
  var meshOAuth_1 = require("./meshOAuth");
15
17
  Object.defineProperty(exports, "OAUTH_MESH_RULESET_RID", { enumerable: true, get: function () { return meshOAuth_1.OAUTH_MESH_RULESET_RID; } });
package/dist/server.js CHANGED
@@ -3,9 +3,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.server = server;
4
4
  const bodyParser = require("body-parser");
5
5
  const express = require("express");
6
+ const fs = require("fs");
6
7
  const helmet_1 = require("helmet");
7
8
  const _ = require("lodash");
8
9
  const path = require("path");
10
+ const bundledKrl_1 = require("./bundledKrl");
9
11
  const auth_1 = require("./auth");
10
12
  const oauth_1 = require("./oauth");
11
13
  const provisionRoot_1 = require("./provisionRoot");
@@ -212,6 +214,12 @@ function server(core, uiECI, auth, oauth, identity) {
212
214
  if (oauth) {
213
215
  registerOAuthRoutes(app, oauth, auth, requireAuthSession, parseCookies);
214
216
  }
217
+ app.get("/api/oauth/mesh-enabled", requireAuthSession, function (_req, res) {
218
+ const roots = pf.rootPicos();
219
+ const meshOAuthEnabled = roots.length > 0 &&
220
+ roots.some((root) => (0, oauth_1.rootHasOAuthMeshRuleset)(pf, root.id));
221
+ res.json({ meshOAuthEnabled });
222
+ });
215
223
  app.get("/api/mesh-context", function (req, res, next) {
216
224
  if (!oauth) {
217
225
  res.status(503).json({ error: "OAuth not enabled" });
@@ -252,6 +260,37 @@ function server(core, uiECI, auth, oauth, identity) {
252
260
  })
253
261
  .catch(next);
254
262
  });
263
+ app.get("/api/krl-sources", function (req, res, next) {
264
+ try {
265
+ let baseUrl = core.base_url;
266
+ if (!baseUrl) {
267
+ baseUrl = `${req.protocol}://${req.get("host") || "localhost"}`;
268
+ }
269
+ res.json({ sources: (0, bundledKrl_1.listEngineKrlSources)(baseUrl.replace(/\/$/, "")) });
270
+ }
271
+ catch (err) {
272
+ next(err);
273
+ }
274
+ });
275
+ app.get(/^\/krl\/(.+\.krl)$/, function (req, res, next) {
276
+ const suffix = req.params[0];
277
+ if (typeof suffix !== "string" || !suffix.endsWith(".krl")) {
278
+ res.status(404).end();
279
+ return;
280
+ }
281
+ const filePath = (0, bundledKrl_1.resolveEngineKrlFile)(suffix);
282
+ if (!filePath) {
283
+ res.status(404).end();
284
+ return;
285
+ }
286
+ fs.readFile(filePath, { encoding: "utf8" }, function (err, data) {
287
+ if (err) {
288
+ next(err);
289
+ return;
290
+ }
291
+ res.type("text/plain").send(data);
292
+ });
293
+ });
255
294
  app.get("/api/ui-context", function (req, res, next) {
256
295
  const hasRoots = pf.rootPicos().length > 0;
257
296
  const base = {
@@ -17,12 +17,18 @@ exports.UI_CHANNEL_EVENT_POLICY = {
17
17
  { domain: "engine_ui", name: "testing_eci" },
18
18
  { domain: "engine", name: "started" },
19
19
  { domain: "wrangler", name: "subscription" },
20
+ { domain: "wrangler", name: "relationship" },
20
21
  { domain: "wrangler", name: "pending_subscription_approval" },
22
+ { domain: "wrangler", name: "pending_relationship_approval" },
21
23
  { domain: "wrangler", name: "inbound_rejection" },
24
+ { domain: "wrangler", name: "inbound_relationship_rejection" },
22
25
  { domain: "wrangler", name: "outbound_cancellation" },
26
+ { domain: "wrangler", name: "outbound_relationship_cancellation" },
23
27
  { domain: "wrangler", name: "subscription_cancellation" },
28
+ { domain: "wrangler", name: "relationship_cancellation" },
24
29
  { domain: "wrangler", name: "set_public_intro" },
25
30
  { domain: "wrangler", name: "send_event_on_subs" },
31
+ { domain: "wrangler", name: "send_event_on_relationships" },
26
32
  ],
27
33
  deny: [],
28
34
  };
@@ -3,9 +3,10 @@ ruleset io.picolabs.oauth {
3
3
  name "OAuth Mesh"
4
4
  description <<
5
5
  Optional mesh-level OAuth. Install on the root pico to require Bearer tokens on external /sky/*
6
- for this mesh, and to hold per-mesh OAuth configuration (apps, consent policy, defaults).
7
- Webhook Client Credentials (per channel) and app Authorization Code grants coexist here.
8
- Not installed by default.
6
+ for this mesh (except channels tagged mesh-oauth-exempt or didcomm+ingress), and to hold
7
+ per-mesh OAuth configuration (apps, consent policy, defaults).
8
+ Webhook Client Credentials (per channel, oauth-webhook tag) and app Authorization Code grants
9
+ coexist here. Not installed by default.
9
10
 
10
11
  use module io.picolabs.oauth alias oauth
11
12
  >>
@@ -24,7 +25,7 @@ ruleset io.picolabs.oauth {
24
25
  true
25
26
  }
26
27
 
27
- /** True when external /sky/* for eci requires a Bearer token (mesh lock or oauth-webhook tag). */
28
+ /** True when the mesh root has io.picolabs.oauth (mesh lock may still be skipped via mesh-oauth-exempt on a channel). */
28
29
  meshRequiresOAuth = function(eci) {
29
30
  oauth:meshRequiresOAuth(eci)
30
31
  }
@@ -45,4 +46,10 @@ ruleset io.picolabs.oauth {
45
46
  oauth:revokeTokens(eci)
46
47
  }
47
48
  }
49
+
50
+ // Mesh OAuth is keyed off the root pico; installing here has no effect.
51
+ rule reject_non_root_install {
52
+ select when wrangler ruleset_installed where event:attr("rids") >< ctx:rid
53
+ if ctx:parent then ctx:uninstall(ctx:rid)
54
+ }
48
55
  }
@@ -143,12 +143,18 @@ ruleset io.picolabs.pico-engine-ui {
143
143
  { "domain": "engine_ui", "name": "testing_eci" },
144
144
  { "domain": "engine", "name": "started" },
145
145
  { "domain": "wrangler", "name": "subscription" },
146
+ { "domain": "wrangler", "name": "relationship" },
146
147
  { "domain": "wrangler", "name": "pending_subscription_approval" },
148
+ { "domain": "wrangler", "name": "pending_relationship_approval" },
147
149
  { "domain": "wrangler", "name": "inbound_rejection" },
150
+ { "domain": "wrangler", "name": "inbound_relationship_rejection" },
148
151
  { "domain": "wrangler", "name": "outbound_cancellation" },
152
+ { "domain": "wrangler", "name": "outbound_relationship_cancellation" },
149
153
  { "domain": "wrangler", "name": "subscription_cancellation" },
154
+ { "domain": "wrangler", "name": "relationship_cancellation" },
150
155
  { "domain": "wrangler", "name": "set_public_intro" },
151
- { "domain": "wrangler", "name": "send_event_on_subs" }
156
+ { "domain": "wrangler", "name": "send_event_on_subs" },
157
+ { "domain": "wrangler", "name": "send_event_on_relationships" }
152
158
  ],
153
159
  "deny": []
154
160
  }
@@ -309,7 +315,15 @@ ruleset io.picolabs.pico-engine-ui {
309
315
  }
310
316
  rule install {
311
317
  select when engine_ui install
312
- fired {
318
+ pre {
319
+ url = event:attrs{"url"} || ""
320
+ oauth_url = url.match(re#io\.picolabs\.oauth#)
321
+ // OAuth mesh ruleset is root-only; allow other URLs on any pico.
322
+ block_oauth = oauth_url && ctx:parent
323
+ }
324
+ // Action-block condition sets $fired; use notfired (or always) for the install path.
325
+ if block_oauth then noop()
326
+ notfired {
313
327
  raise wrangler event "install_ruleset_request" attributes event:attrs
314
328
  }
315
329
  }