@capybara-hq/kernel 0.1.0-rc.148 → 0.1.0-rc.149
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/bootstrap.json +29 -29
- package/dist/admin/extensions.controller.d.ts +26 -2
- package/dist/admin/extensions.controller.js +52 -3
- package/dist/admin/extensions.schemas.d.ts +4 -0
- package/dist/admin/extensions.schemas.js +6 -0
- package/dist/catalog/cli.d.ts +3 -2
- package/dist/catalog/cli.js +19 -3
- package/dist/catalog/marketplaces.service.d.ts +1 -1
- package/dist/channels/channels.module.js +3 -0
- package/dist/channels/connection-summary.d.ts +32 -0
- package/dist/channels/connection-summary.js +67 -0
- package/dist/db/kernel-schema.d.ts +27 -0
- package/dist/erasure/org-catalog.js +4 -2
- package/dist/erasure/org-rules.js +1 -0
- package/dist/extensions/admin-ops.js +1 -0
- package/dist/extensions/admin-view.d.ts +5 -0
- package/dist/extensions/admin-view.js +19 -2
- package/dist/extensions/config-json-schema.d.ts +10 -6
- package/dist/extensions/config-json-schema.js +12 -8
- package/dist/extensions/deployment-config-store.d.ts +2 -0
- package/dist/extensions/deployment-config.d.ts +1 -1
- package/dist/extensions/deployment-config.js +5 -2
- package/dist/extensions/enabled.d.ts +1 -0
- package/dist/extensions/grant-epoch.d.ts +11 -2
- package/dist/extensions/grant-epoch.js +26 -3
- package/dist/extensions/host.d.ts +3 -0
- package/dist/extensions/host.js +5 -2
- package/dist/extensions/org-overrides.js +1 -1
- package/dist/extensions/permission-view.d.ts +5 -0
- package/dist/net/private-endpoint-preflight.js +1 -1
- package/dist/orgs/directory-sync-state.d.ts +125 -0
- package/dist/orgs/directory-sync-state.js +259 -0
- package/dist/orgs/directory-sync.controller.d.ts +16 -3
- package/dist/orgs/directory-sync.controller.js +29 -5
- package/dist/orgs/directory-sync.d.ts +18 -7
- package/dist/orgs/directory-sync.js +238 -106
- package/dist/orgs/directory-sync.schemas.d.ts +26 -0
- package/dist/orgs/directory-sync.schemas.js +50 -0
- package/dist/orgs/orgs.module.js +3 -0
- package/dist/platform/ops/extensions-admin.ops.d.ts +5 -1
- package/dist/platform/ops/extensions-admin.ops.js +43 -3
- package/dist/platform/ops/extensions.ops.d.ts +3 -1
- package/dist/platform/ops/extensions.ops.js +19 -2
- package/dist/platform/route-schemas.js +3 -0
- package/package.json +2 -2
- package/src/migrations/0240_directory_sync_state.sql +34 -0
|
@@ -11,6 +11,7 @@ import { randomUUID } from "node:crypto";
|
|
|
11
11
|
import { DIRECTORY_EXTENSION_POINT, } from "@capybara-hq/plugin-sdk";
|
|
12
12
|
import { getQueueToken } from "@nestjs/bullmq";
|
|
13
13
|
import { Inject, Injectable } from "@nestjs/common";
|
|
14
|
+
import { secretValues as secretConfigValues, secretScalars } from "../config/fields.js";
|
|
14
15
|
import { ApiError } from "../errors.js";
|
|
15
16
|
import { EffectiveConfigs } from "../extensions/effective-config.js";
|
|
16
17
|
import { enabledExtensionsByOrg } from "../extensions/enabled.js";
|
|
@@ -27,16 +28,15 @@ import { LockService } from "../infra/lock/lock.service.js";
|
|
|
27
28
|
import { LockedCron } from "../infra/schedule/locked-cron.decorator.js";
|
|
28
29
|
import { KERNEL_LOGGER, REDIS } from "../infra/tokens.js";
|
|
29
30
|
import { MAINTENANCE_QUEUE_NAME } from "../runtime/maintenance-queue.js";
|
|
31
|
+
import { DIRECTORY_SYNC_LOCK_TTL_MS, DirectorySyncStates, directorySyncLockKey, redactSyncError, } from "./directory-sync-state.js";
|
|
30
32
|
import { Offboarding } from "./offboarding.js";
|
|
31
33
|
import { OrgLock } from "./org-lock.js";
|
|
32
34
|
/** The hourly scan: cron job name (no `:`). */
|
|
33
35
|
export const DIRECTORY_SCAN_JOB = "directory-scan";
|
|
34
36
|
/** The job name of one (org, extension) sync on the maintenance queue. */
|
|
35
37
|
export const DIRECTORY_SYNC_JOB = "directory-sync";
|
|
36
|
-
|
|
37
|
-
export
|
|
38
|
-
/** The `LockService` key of one (org, extension) sync: one sync at a time. */
|
|
39
|
-
export const directorySyncLockKey = (orgId, plugin) => `directory-sync:${orgId}:${plugin}`;
|
|
38
|
+
// the lock constants and the trigger live with the state, which reads the lock too
|
|
39
|
+
export { DIRECTORY_SYNC_LOCK_TTL_MS, directorySyncLockKey };
|
|
40
40
|
/** 脏标记的保留时长(秒):锁被占时记下,持有者跑完后取走并补一轮。 */
|
|
41
41
|
export const DIRECTORY_SYNC_DIRTY_TTL_SECONDS = 3_600;
|
|
42
42
|
/** 脏标记(Redis 键):同步进行中又有同步请求被锁挡掉。 */
|
|
@@ -87,6 +87,14 @@ const silentSkip = (reason) => reason === "not_configured" || reason === "needs_
|
|
|
87
87
|
/** 组织已删除(同步途中):停止本轮。 */
|
|
88
88
|
class OrgGone extends Error {
|
|
89
89
|
}
|
|
90
|
+
/** The listing stopped for a reason the kernel names itself (page limit, cursor loop). */
|
|
91
|
+
class ListingStopped extends Error {
|
|
92
|
+
code;
|
|
93
|
+
constructor(code, message) {
|
|
94
|
+
super(message);
|
|
95
|
+
this.code = code;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
90
98
|
/**
|
|
91
99
|
* 通讯录同步(`directory@1`):把企业通讯录同步为组织成员。框架无关。
|
|
92
100
|
*
|
|
@@ -136,7 +144,8 @@ let DirectorySync = class DirectorySync {
|
|
|
136
144
|
queue;
|
|
137
145
|
lockService;
|
|
138
146
|
log;
|
|
139
|
-
|
|
147
|
+
states;
|
|
148
|
+
constructor(db, txHost, redis, registry, orgConfigs, effective, external, offboarding, orgLock, queue, lockService, log, states) {
|
|
140
149
|
this.db = db;
|
|
141
150
|
this.txHost = txHost;
|
|
142
151
|
this.redis = redis;
|
|
@@ -149,6 +158,7 @@ let DirectorySync = class DirectorySync {
|
|
|
149
158
|
this.queue = queue;
|
|
150
159
|
this.lockService = lockService;
|
|
151
160
|
this.log = log;
|
|
161
|
+
this.states = states;
|
|
152
162
|
}
|
|
153
163
|
/** A `directory-sync` job (dispatched by `MaintenanceProcessor`). */
|
|
154
164
|
async runSyncJob(data) {
|
|
@@ -348,7 +358,9 @@ let DirectorySync = class DirectorySync {
|
|
|
348
358
|
// 锁丢了(续租失败、过期后被他人取得):停止本轮,不再移除任何人
|
|
349
359
|
const signal = lease.lost;
|
|
350
360
|
try {
|
|
351
|
-
|
|
361
|
+
// intent first: the page shows the run from here; a crash leaves it to lapse with the lock
|
|
362
|
+
const startedAt = await this.states.markStarted(orgId, plugin, opts.trigger ?? "manual");
|
|
363
|
+
return await this.run(orgId, plugin, e.directory, e.config, signal, startedAt, opts);
|
|
352
364
|
}
|
|
353
365
|
finally {
|
|
354
366
|
await lease.release().catch(() => { });
|
|
@@ -370,7 +382,9 @@ let DirectorySync = class DirectorySync {
|
|
|
370
382
|
this.log.warn("directory sync: cannot enqueue follow-up sync", { orgId, plugin, err });
|
|
371
383
|
}
|
|
372
384
|
}
|
|
373
|
-
async run(orgId, plugin, directory, config, signal,
|
|
385
|
+
async run(orgId, plugin, directory, config, signal,
|
|
386
|
+
/** This run's start as recorded: the outcome is written only while no newer run started. */
|
|
387
|
+
startedAt, opts) {
|
|
374
388
|
const source = `directory:${plugin}`;
|
|
375
389
|
const counts = { added: 0, removed: 0, skippedLastOwner: 0, failed: 0, total: 0 };
|
|
376
390
|
/** 本轮仍在职的账号(只在本次任务内使用)。 */
|
|
@@ -378,18 +392,24 @@ let DirectorySync = class DirectorySync {
|
|
|
378
392
|
/** 列表里在职(active = true)的成员数(含处理失败的)。 */
|
|
379
393
|
let active = 0;
|
|
380
394
|
let listed = false;
|
|
395
|
+
/** Why the listing stopped, and the extension's message (redacted before it is stored). */
|
|
396
|
+
let listFailure;
|
|
397
|
+
/** The run lost its lock while listing: partial (Ruling A17 removals skipped), not a failure. */
|
|
398
|
+
let lockLost = false;
|
|
399
|
+
let pageTimeout;
|
|
381
400
|
try {
|
|
382
401
|
let cursor;
|
|
383
402
|
for (let page = 0;; page++) {
|
|
384
403
|
if (page >= DIRECTORY_MAX_PAGES) {
|
|
385
|
-
throw new
|
|
404
|
+
throw new ListingStopped("page_limit", `directory returned more than ${DIRECTORY_MAX_PAGES} pages`);
|
|
386
405
|
}
|
|
387
406
|
signal.throwIfAborted();
|
|
407
|
+
pageTimeout = AbortSignal.timeout(DIRECTORY_PAGE_TIMEOUT_MS);
|
|
388
408
|
const r = await directory.listMembers({
|
|
389
409
|
config,
|
|
390
410
|
orgId,
|
|
391
411
|
...(cursor !== undefined && { cursor }),
|
|
392
|
-
signal: AbortSignal.any([signal,
|
|
412
|
+
signal: AbortSignal.any([signal, pageTimeout]),
|
|
393
413
|
});
|
|
394
414
|
signal.throwIfAborted();
|
|
395
415
|
if (!Array.isArray(r?.members))
|
|
@@ -416,125 +436,233 @@ let DirectorySync = class DirectorySync {
|
|
|
416
436
|
const next = r.next;
|
|
417
437
|
if (typeof next !== "string" || next === "")
|
|
418
438
|
break;
|
|
419
|
-
if (next === cursor)
|
|
420
|
-
throw new
|
|
439
|
+
if (next === cursor) {
|
|
440
|
+
throw new ListingStopped("cursor_loop", "directory returned the same cursor twice");
|
|
441
|
+
}
|
|
421
442
|
cursor = next;
|
|
422
443
|
}
|
|
423
444
|
listed = true;
|
|
424
445
|
}
|
|
425
446
|
catch (err) {
|
|
426
447
|
this.log.warn("directory sync stopped before listing all members", { orgId, plugin, err });
|
|
448
|
+
if (signal.aborted)
|
|
449
|
+
lockLost = true;
|
|
450
|
+
else
|
|
451
|
+
listFailure = {
|
|
452
|
+
code: err instanceof ListingStopped
|
|
453
|
+
? err.code
|
|
454
|
+
: pageTimeout?.aborted === true && !signal.aborted
|
|
455
|
+
? "timeout"
|
|
456
|
+
: "list_failed",
|
|
457
|
+
message: err instanceof Error ? err.message : String(err),
|
|
458
|
+
};
|
|
427
459
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
460
|
+
try {
|
|
461
|
+
const trigger = opts.trigger ?? "manual";
|
|
462
|
+
let partial = !listed || counts.failed > 0;
|
|
463
|
+
/** Why a listed round is partial (the stored error code). */
|
|
464
|
+
let partialCode = lockLost
|
|
465
|
+
? "lock_lost"
|
|
466
|
+
: listed && counts.failed > 0
|
|
467
|
+
? "member_failures"
|
|
468
|
+
: undefined;
|
|
469
|
+
let removalBlocked = false;
|
|
470
|
+
if (!partial) {
|
|
471
|
+
// Re-check before removals: the plugin may have been switched off or the org's config may
|
|
472
|
+
// have become invalid during the run; the list is no longer trusted, so nobody is removed
|
|
473
|
+
const still = await this.eligible(orgId, plugin);
|
|
474
|
+
if (!still.ok) {
|
|
475
|
+
partial = true;
|
|
476
|
+
partialCode = "no_longer_eligible";
|
|
477
|
+
// The directory config was cleared during the run (Ruling A28): still no removals, no warning
|
|
478
|
+
if (!silentSkip(still.reason)) {
|
|
479
|
+
this.log.warn("directory sync: plugin no longer eligible before removals", {
|
|
480
|
+
orgId,
|
|
481
|
+
plugin,
|
|
482
|
+
reason: still.reason,
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
// Ruling A32: a change arrived while this run held the lock (another job found the lock taken
|
|
488
|
+
// and set the dirty flag). Pages already read may predate it, so the list is not trusted and
|
|
489
|
+
// this run removes nobody; at the end rerunIfDirty takes the flag and runs again, and that run
|
|
490
|
+
// removes. Only GET here: taking the flag is rerunIfDirty's.
|
|
491
|
+
let partialReason;
|
|
492
|
+
if (!partial) {
|
|
493
|
+
let dirty;
|
|
494
|
+
try {
|
|
495
|
+
dirty = await this.redis.get(directorySyncDirtyKey(orgId, plugin));
|
|
496
|
+
}
|
|
497
|
+
catch (err) {
|
|
498
|
+
this.log.warn("directory sync: cannot read the dirty flag; skipping removals", {
|
|
439
499
|
orgId,
|
|
440
500
|
plugin,
|
|
441
|
-
|
|
501
|
+
err,
|
|
442
502
|
});
|
|
503
|
+
dirty = "1";
|
|
504
|
+
}
|
|
505
|
+
if (dirty !== null) {
|
|
506
|
+
partial = true;
|
|
507
|
+
partialReason = "changed_during_sync";
|
|
508
|
+
partialCode = "changed_during_sync";
|
|
443
509
|
}
|
|
444
510
|
}
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
partial = true;
|
|
464
|
-
partialReason = "changed_during_sync";
|
|
465
|
-
}
|
|
466
|
-
}
|
|
467
|
-
if (!partial) {
|
|
468
|
-
const existing = await this.db
|
|
469
|
-
.selectFrom("memberships")
|
|
470
|
-
.select("account_id")
|
|
471
|
-
.where("org_id", "=", orgId)
|
|
472
|
-
.where("source", "=", source)
|
|
473
|
-
.orderBy("account_id")
|
|
474
|
-
.execute();
|
|
475
|
-
const planned = existing.filter((c) => !seen.has(c.account_id));
|
|
476
|
-
// Ruling A17:非手动触发的一轮要移除太多人(或通讯录忽然一个在职的人都没有)时,多半是插件或平台出错,先不删
|
|
477
|
-
const threshold = Math.max(DIRECTORY_REMOVAL_GUARD_MIN, Math.floor(DIRECTORY_REMOVAL_GUARD_RATIO * existing.length));
|
|
478
|
-
if (trigger !== "manual" &&
|
|
479
|
-
(planned.length > threshold || (active === 0 && existing.length > 0))) {
|
|
480
|
-
partial = true;
|
|
481
|
-
removalBlocked = true;
|
|
482
|
-
this.log.warn("directory sync: mass removal blocked", {
|
|
483
|
-
orgId,
|
|
484
|
-
plugin,
|
|
485
|
-
planned: planned.length,
|
|
486
|
-
existing: existing.length,
|
|
487
|
-
});
|
|
488
|
-
await audit(this.db.db, {
|
|
489
|
-
orgId,
|
|
490
|
-
action: "org.directory.removal_blocked",
|
|
491
|
-
target: plugin,
|
|
492
|
-
detail: {
|
|
511
|
+
if (!partial) {
|
|
512
|
+
const existing = await this.db
|
|
513
|
+
.selectFrom("memberships")
|
|
514
|
+
.select("account_id")
|
|
515
|
+
.where("org_id", "=", orgId)
|
|
516
|
+
.where("source", "=", source)
|
|
517
|
+
.orderBy("account_id")
|
|
518
|
+
.execute();
|
|
519
|
+
const planned = existing.filter((c) => !seen.has(c.account_id));
|
|
520
|
+
// Ruling A17: a run not started by hand that would remove too many people (or a directory
|
|
521
|
+
// suddenly without any active person) most likely hit a plugin or platform error: remove none
|
|
522
|
+
const threshold = Math.max(DIRECTORY_REMOVAL_GUARD_MIN, Math.floor(DIRECTORY_REMOVAL_GUARD_RATIO * existing.length));
|
|
523
|
+
if (trigger !== "manual" &&
|
|
524
|
+
(planned.length > threshold || (active === 0 && existing.length > 0))) {
|
|
525
|
+
partial = true;
|
|
526
|
+
removalBlocked = true;
|
|
527
|
+
this.log.warn("directory sync: mass removal blocked", {
|
|
528
|
+
orgId,
|
|
493
529
|
plugin,
|
|
494
|
-
trigger,
|
|
495
530
|
planned: planned.length,
|
|
496
531
|
existing: existing.length,
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
532
|
+
});
|
|
533
|
+
await audit(this.db.db, {
|
|
534
|
+
orgId,
|
|
535
|
+
action: "org.directory.removal_blocked",
|
|
536
|
+
target: plugin,
|
|
537
|
+
detail: {
|
|
538
|
+
plugin,
|
|
539
|
+
trigger,
|
|
540
|
+
planned: planned.length,
|
|
541
|
+
existing: existing.length,
|
|
542
|
+
active,
|
|
543
|
+
threshold,
|
|
544
|
+
},
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
for (const c of removalBlocked ? [] : planned) {
|
|
548
|
+
if (signal.aborted) {
|
|
549
|
+
// the lock lapsed: another run may hold it now, so this one stops removing
|
|
550
|
+
partial = true;
|
|
551
|
+
partialCode = "lock_lost";
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
554
|
+
const outcome = await this.offboardOne(orgId, plugin, source, c.account_id);
|
|
555
|
+
if (outcome === "removed")
|
|
556
|
+
counts.removed++;
|
|
557
|
+
else if (outcome === "last_owner")
|
|
558
|
+
counts.skippedLastOwner++;
|
|
559
|
+
else if (outcome === "failed")
|
|
560
|
+
counts.failed++;
|
|
506
561
|
}
|
|
507
|
-
const outcome = await this.offboardOne(orgId, plugin, source, c.account_id);
|
|
508
|
-
if (outcome === "removed")
|
|
509
|
-
counts.removed++;
|
|
510
|
-
else if (outcome === "last_owner")
|
|
511
|
-
counts.skippedLastOwner++;
|
|
512
|
-
else if (outcome === "failed")
|
|
513
|
-
counts.failed++;
|
|
514
562
|
}
|
|
563
|
+
const outcome = !listed
|
|
564
|
+
? lockLost
|
|
565
|
+
? "partial"
|
|
566
|
+
: "failed"
|
|
567
|
+
: removalBlocked
|
|
568
|
+
? "removal_blocked"
|
|
569
|
+
: partial
|
|
570
|
+
? "partial"
|
|
571
|
+
: "synced";
|
|
572
|
+
await this.finish(orgId, plugin, startedAt, {
|
|
573
|
+
audit: {
|
|
574
|
+
...(opts.requestedBy !== undefined && { actorAccountId: opts.requestedBy }),
|
|
575
|
+
...(opts.requestedByToken !== undefined && { actorTokenId: opts.requestedByToken }),
|
|
576
|
+
detail: {
|
|
577
|
+
plugin,
|
|
578
|
+
trigger,
|
|
579
|
+
...counts,
|
|
580
|
+
partial,
|
|
581
|
+
removalBlocked,
|
|
582
|
+
...(partialReason !== undefined && { partialReason }),
|
|
583
|
+
},
|
|
584
|
+
},
|
|
585
|
+
state: {
|
|
586
|
+
outcome,
|
|
587
|
+
counts: { ...counts },
|
|
588
|
+
...(listFailure !== undefined
|
|
589
|
+
? {
|
|
590
|
+
errorCode: listFailure.code,
|
|
591
|
+
error: redactSyncError(listFailure.message, this.secretValues(plugin, config)),
|
|
592
|
+
}
|
|
593
|
+
: outcome === "partial" && partialCode !== undefined
|
|
594
|
+
? { errorCode: partialCode }
|
|
595
|
+
: {}),
|
|
596
|
+
},
|
|
597
|
+
});
|
|
598
|
+
return {
|
|
599
|
+
status: "synced",
|
|
600
|
+
...counts,
|
|
601
|
+
partial,
|
|
602
|
+
removalBlocked,
|
|
603
|
+
...(partialReason !== undefined && { partialReason }),
|
|
604
|
+
};
|
|
515
605
|
}
|
|
606
|
+
catch (err) {
|
|
607
|
+
// a throw after the listing (a database error, the outcome write itself): record what is
|
|
608
|
+
// known in a separate transaction, since the failed one rolled back
|
|
609
|
+
await this.recordInterrupted(orgId, plugin, startedAt, config, {
|
|
610
|
+
outcome: listed || lockLost ? "partial" : "failed",
|
|
611
|
+
counts: { ...counts },
|
|
612
|
+
...(listFailure !== undefined && !listed
|
|
613
|
+
? { errorCode: listFailure.code }
|
|
614
|
+
: signal.aborted && { errorCode: "lock_lost" }),
|
|
615
|
+
message: err instanceof Error ? err.message : String(err),
|
|
616
|
+
});
|
|
617
|
+
throw err;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
/** The run's `org.directory.synced` audit row and its recorded state, in one transaction. */
|
|
621
|
+
async finish(orgId, plugin, startedAt, r) {
|
|
516
622
|
await audit(this.db.db, {
|
|
517
|
-
...
|
|
518
|
-
...(opts.requestedByToken !== undefined && { actorTokenId: opts.requestedByToken }),
|
|
623
|
+
...r.audit,
|
|
519
624
|
orgId,
|
|
520
625
|
action: "org.directory.synced",
|
|
521
626
|
target: plugin,
|
|
522
|
-
detail: {
|
|
523
|
-
plugin,
|
|
524
|
-
trigger,
|
|
525
|
-
...counts,
|
|
526
|
-
partial,
|
|
527
|
-
removalBlocked,
|
|
528
|
-
...(partialReason !== undefined && { partialReason }),
|
|
529
|
-
},
|
|
530
627
|
});
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
628
|
+
await this.states.markFinished(orgId, plugin, startedAt, r.state);
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* The outcome of a run that threw after its lock was taken, written on its own (never inside
|
|
632
|
+
* the transaction that failed); a failure to write it is only logged.
|
|
633
|
+
*/
|
|
634
|
+
async recordInterrupted(orgId, plugin, startedAt, config, r) {
|
|
635
|
+
try {
|
|
636
|
+
await this.states.markFinished(orgId, plugin, startedAt, {
|
|
637
|
+
outcome: r.outcome,
|
|
638
|
+
counts: r.counts,
|
|
639
|
+
...(r.errorCode !== undefined && { errorCode: r.errorCode }),
|
|
640
|
+
error: redactSyncError(r.message, this.secretValues(plugin, config)),
|
|
641
|
+
});
|
|
642
|
+
}
|
|
643
|
+
catch (err) {
|
|
644
|
+
this.log.warn("directory sync: cannot record the interrupted run", { orgId, plugin, err });
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* The secret values a stored error message masks, since an extension may echo a token in its
|
|
649
|
+
* error: those of the config the run used (the org's effective config) and those of the
|
|
650
|
+
* platform's running deployment config (an org with its own copy runs without the platform's
|
|
651
|
+
* overridable secrets, which the extension still holds in `ctx.config`).
|
|
652
|
+
*/
|
|
653
|
+
secretValues(plugin, config) {
|
|
654
|
+
const inst = this.registry.current(plugin);
|
|
655
|
+
const schema = inst?.manifest.config?.deployment;
|
|
656
|
+
const configs = [config, inst?.config];
|
|
657
|
+
if (schema === undefined)
|
|
658
|
+
return [];
|
|
659
|
+
try {
|
|
660
|
+
return configs.flatMap((c) => secretConfigValues(schema, c));
|
|
661
|
+
}
|
|
662
|
+
catch {
|
|
663
|
+
// a schema the walk cannot read: mask every scalar of both configs instead
|
|
664
|
+
return configs.flatMap((c) => secretScalars(c));
|
|
665
|
+
}
|
|
538
666
|
}
|
|
539
667
|
/**
|
|
540
668
|
* 一个在职成员:解析外部身份(不合并、不登录,新身份行 `linked_via = directory`),不是成员就以 member 加入
|
|
@@ -623,6 +751,9 @@ let DirectorySync = class DirectorySync {
|
|
|
623
751
|
__decorate([
|
|
624
752
|
Transactional()
|
|
625
753
|
], DirectorySync.prototype, "authorize", null);
|
|
754
|
+
__decorate([
|
|
755
|
+
Transactional()
|
|
756
|
+
], DirectorySync.prototype, "finish", null);
|
|
626
757
|
__decorate([
|
|
627
758
|
Transactional()
|
|
628
759
|
], DirectorySync.prototype, "addMemberIn", null);
|
|
@@ -642,7 +773,8 @@ DirectorySync = __decorate([
|
|
|
642
773
|
__param(8, Inject(OrgLock)),
|
|
643
774
|
__param(9, Inject(getQueueToken(MAINTENANCE_QUEUE_NAME))),
|
|
644
775
|
__param(10, Inject(LockService)),
|
|
645
|
-
__param(11, Inject(KERNEL_LOGGER))
|
|
776
|
+
__param(11, Inject(KERNEL_LOGGER)),
|
|
777
|
+
__param(12, Inject(DirectorySyncStates))
|
|
646
778
|
], DirectorySync);
|
|
647
779
|
export { DirectorySync };
|
|
648
780
|
/** The hourly directory scan ({@link DirectorySync.scan}), once per tick across replicas; a worker-role job (`WorkerModule`). */
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import type { RouteSchemas } from "../platform/route-schemas.js";
|
|
3
|
+
/** `:extension` of `GET /orgs/:orgId/extensions/:extension/directory`. */
|
|
4
|
+
export declare const OrgDirectoryParams: z.ZodObject<{
|
|
5
|
+
extension: z.ZodString;
|
|
6
|
+
}, z.core.$strip>;
|
|
7
|
+
/** `:name` of `GET /admin/extensions/:name/directory/orgs`. */
|
|
8
|
+
export declare const AdminDirectoryOrgsParams: z.ZodObject<{
|
|
9
|
+
name: z.ZodString;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
/** A page cursor of the platform's directory list: the last row's org name and id. */
|
|
12
|
+
export declare function encodeDirectoryOrgCursor(name: string, id: string): string;
|
|
13
|
+
/** The cursor's (name, id), or null when it is not one {@link encodeDirectoryOrgCursor} made. */
|
|
14
|
+
export declare function decodeDirectoryOrgCursor(token: string): [string, string] | null;
|
|
15
|
+
/**
|
|
16
|
+
* The query of the platform's directory list: `cursor` is the `next` of the previous page;
|
|
17
|
+
* `attention=1` keeps the orgs whose last run failed, was partial or was removal-blocked.
|
|
18
|
+
*/
|
|
19
|
+
export declare const AdminDirectoryOrgsQuery: z.ZodObject<{
|
|
20
|
+
cursor: z.ZodOptional<z.ZodString>;
|
|
21
|
+
attention: z.ZodOptional<z.ZodEnum<{
|
|
22
|
+
0: "0";
|
|
23
|
+
1: "1";
|
|
24
|
+
}>>;
|
|
25
|
+
}, z.core.$strict>;
|
|
26
|
+
export declare const DIRECTORY_SYNC_ROUTES: readonly RouteSchemas[];
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/** `:extension` of `GET /orgs/:orgId/extensions/:extension/directory`. */
|
|
3
|
+
export const OrgDirectoryParams = z.object({ extension: z.string().min(1).max(64) });
|
|
4
|
+
/** `:name` of `GET /admin/extensions/:name/directory/orgs`. */
|
|
5
|
+
export const AdminDirectoryOrgsParams = z.object({ name: z.string().min(1).max(64) });
|
|
6
|
+
/** A page cursor of the platform's directory list: the last row's org name and id. */
|
|
7
|
+
export function encodeDirectoryOrgCursor(name, id) {
|
|
8
|
+
return Buffer.from(JSON.stringify([name, id]), "utf8").toString("base64url");
|
|
9
|
+
}
|
|
10
|
+
/** A cursor part Postgres can compare: a string without NUL, which no text column holds. */
|
|
11
|
+
const cursorPart = (v) => typeof v === "string" && !v.includes("\u0000");
|
|
12
|
+
/** The cursor's (name, id), or null when it is not one {@link encodeDirectoryOrgCursor} made. */
|
|
13
|
+
export function decodeDirectoryOrgCursor(token) {
|
|
14
|
+
try {
|
|
15
|
+
const v = JSON.parse(Buffer.from(token, "base64url").toString("utf8"));
|
|
16
|
+
if (Array.isArray(v) && v.length === 2 && cursorPart(v[0]) && cursorPart(v[1])) {
|
|
17
|
+
return [v[0], v[1]];
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
// not a cursor
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* The query of the platform's directory list: `cursor` is the `next` of the previous page;
|
|
27
|
+
* `attention=1` keeps the orgs whose last run failed, was partial or was removal-blocked.
|
|
28
|
+
*/
|
|
29
|
+
export const AdminDirectoryOrgsQuery = z
|
|
30
|
+
.object({
|
|
31
|
+
cursor: z
|
|
32
|
+
.string()
|
|
33
|
+
.min(1)
|
|
34
|
+
.max(512)
|
|
35
|
+
.refine((c) => decodeDirectoryOrgCursor(c) !== null, "pass the next value of the previous page")
|
|
36
|
+
.optional(),
|
|
37
|
+
attention: z.enum(["0", "1"]).optional(),
|
|
38
|
+
})
|
|
39
|
+
.strict();
|
|
40
|
+
export const DIRECTORY_SYNC_ROUTES = [
|
|
41
|
+
{
|
|
42
|
+
route: "GET /api/v1/orgs/:orgId/extensions/:extension/directory",
|
|
43
|
+
params: OrgDirectoryParams,
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
route: "GET /api/v1/admin/extensions/:name/directory/orgs",
|
|
47
|
+
params: AdminDirectoryOrgsParams,
|
|
48
|
+
query: AdminDirectoryOrgsQuery,
|
|
49
|
+
},
|
|
50
|
+
];
|
package/dist/orgs/orgs.module.js
CHANGED
|
@@ -21,6 +21,7 @@ import { SandboxModule } from "../sandbox/sandbox.module.js";
|
|
|
21
21
|
import { StorageModule } from "../storage/storage.module.js";
|
|
22
22
|
import { ToolsModule } from "../tools/tools.module.js";
|
|
23
23
|
import { DirectorySync } from "./directory-sync.js";
|
|
24
|
+
import { DirectorySyncStates } from "./directory-sync-state.js";
|
|
24
25
|
import { InvitationsService } from "./invitations.service.js";
|
|
25
26
|
import { MembersService } from "./members.service.js";
|
|
26
27
|
import { OrgNetworkAllowancesModule } from "./network-allowances.service.js";
|
|
@@ -72,6 +73,7 @@ OrgsModule = __decorate([
|
|
|
72
73
|
InvitationsService,
|
|
73
74
|
Offboarding,
|
|
74
75
|
DirectorySync,
|
|
76
|
+
DirectorySyncStates,
|
|
75
77
|
OrgSecuritySettingsService,
|
|
76
78
|
],
|
|
77
79
|
exports: [
|
|
@@ -83,6 +85,7 @@ OrgsModule = __decorate([
|
|
|
83
85
|
InvitationsService,
|
|
84
86
|
Offboarding,
|
|
85
87
|
DirectorySync,
|
|
88
|
+
DirectorySyncStates,
|
|
86
89
|
OrgNetworkAllowancesModule,
|
|
87
90
|
],
|
|
88
91
|
})
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type OnModuleInit } from "@nestjs/common";
|
|
2
2
|
import { CatalogService } from "../../catalog/catalog.service.js";
|
|
3
|
+
import { ChannelConnectionSummaries } from "../../channels/connection-summary.js";
|
|
3
4
|
import { ExtensionAdminOps } from "../../extensions/admin-ops.js";
|
|
4
5
|
import { ExtensionAdmin } from "../../extensions/admin-view.js";
|
|
5
6
|
import { type DeploymentConfigView, ExtensionDeploymentConfigService } from "../../extensions/deployment-config.js";
|
|
@@ -8,6 +9,7 @@ import { PluginGuides, type RenderedSetupGuide } from "../../extensions/plugin-g
|
|
|
8
9
|
import { LoginSourcesService } from "../../identity/login-sources.js";
|
|
9
10
|
import { KernelConfigService } from "../../infra/config/kernel-config.service.js";
|
|
10
11
|
import { KernelDb } from "../../infra/database/kernel-db.js";
|
|
12
|
+
import { DirectorySyncStates } from "../../orgs/directory-sync-state.js";
|
|
11
13
|
import { SetupChecksService } from "../../setup/setup-checks.service.js";
|
|
12
14
|
import { type PersonFieldTarget } from "../person-fields.js";
|
|
13
15
|
import { PlatformOperations } from "../registry.js";
|
|
@@ -59,7 +61,9 @@ export declare class ExtensionsAdminOperations implements OnModuleInit {
|
|
|
59
61
|
private readonly guides;
|
|
60
62
|
private readonly checks;
|
|
61
63
|
private readonly loginSources;
|
|
62
|
-
|
|
64
|
+
private readonly directoryStates;
|
|
65
|
+
private readonly channelSummaries;
|
|
66
|
+
constructor(registry: PlatformOperations, admin: ExtensionAdmin, ops: ExtensionAdminOps, config: KernelConfigService, kdb: KernelDb, catalog: CatalogService, deploymentConfigs: ExtensionDeploymentConfigService, installs: ExtensionInstalls, guides: Pick<PluginGuides, "deploymentGuides">, checks: Pick<SetupChecksService, "deploymentStates" | "deploymentRun">, loginSources: Pick<LoginSourcesService, "providesIdentitySource">, directoryStates: Pick<DirectorySyncStates, "listForPlugin">, channelSummaries: Pick<ChannelConnectionSummaries, "forPlugin">);
|
|
63
67
|
/** The view of `name` for a precheck, or the refusal (unknown, startup-declared). */
|
|
64
68
|
private viewFor;
|
|
65
69
|
onModuleInit(): void;
|