@capybara-hq/kernel 0.1.0-rc.147 → 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/isolated/admission.js +3 -1
- package/dist/extensions/org-overrides.js +1 -1
- package/dist/extensions/package.js +1 -3
- package/dist/extensions/permission-set.d.ts +12 -3
- package/dist/extensions/permission-set.js +5 -5
- package/dist/extensions/permission-view.d.ts +17 -5
- package/dist/extensions/permission-view.js +23 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- 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
|
@@ -37,7 +37,7 @@ export const BROKERED_ENDPOINT_PINS = [
|
|
|
37
37
|
alias: "object-store",
|
|
38
38
|
methods: ["GET", "HEAD", "PUT", "POST", "DELETE"],
|
|
39
39
|
target: s3Target,
|
|
40
|
-
ctxNetFrom:
|
|
40
|
+
ctxNetFrom: "0.1.0-rc.15",
|
|
41
41
|
},
|
|
42
42
|
];
|
|
43
43
|
export const PRIVATE_ENDPOINTS_DOC = "docs/deploy/upgrade.md#private-endpoints-for-plugin-egress";
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { Redis } from "ioredis";
|
|
2
|
+
import type { DirectorySyncCountsJson } from "../db/kernel-schema.js";
|
|
3
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
4
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
5
|
+
/** The run lock's TTL (renewed while held); a crashed holder's lock expires within it. */
|
|
6
|
+
export declare const DIRECTORY_SYNC_LOCK_TTL_MS = 30000;
|
|
7
|
+
/** The `LockService` key of one (org, extension) sync: one sync at a time. */
|
|
8
|
+
export declare const directorySyncLockKey: (orgId: string, plugin: string) => string;
|
|
9
|
+
/** What started a sync: the hourly scan, an inbound `directory_changed` hint, an admin. */
|
|
10
|
+
export type DirectorySyncTrigger = "schedule" | "changed" | "manual";
|
|
11
|
+
/**
|
|
12
|
+
* How a run ended: every member listed and processed (`synced`); listed but not complete enough to
|
|
13
|
+
* remove anyone (`partial`: member failures, a change during the run, the extension no longer
|
|
14
|
+
* eligible, the lock lost before the removals); stopped by the mass-removal guard
|
|
15
|
+
* (`removal_blocked`, Ruling A17); the listing did not finish (`failed`).
|
|
16
|
+
*/
|
|
17
|
+
export type DirectorySyncOutcome = "synced" | "partial" | "removal_blocked" | "failed";
|
|
18
|
+
/** The kernel's word for why a run was `failed` or `partial`. */
|
|
19
|
+
export type DirectorySyncErrorCode = "list_failed" | "page_limit" | "cursor_loop" | "timeout" | "member_failures" | "changed_during_sync" | "no_longer_eligible" | "lock_lost";
|
|
20
|
+
export type DirectorySyncCounts = DirectorySyncCountsJson;
|
|
21
|
+
/** The directory sync state of one (org, extension), as the org's page reads it. */
|
|
22
|
+
export interface DirectorySyncState {
|
|
23
|
+
orgId: string;
|
|
24
|
+
plugin: string;
|
|
25
|
+
/** When the current run took its lock; null when no run is recorded as running. */
|
|
26
|
+
runningSince: string | null;
|
|
27
|
+
/** A run holds the lock now (a crashed run's stale `runningSince` reads as false). */
|
|
28
|
+
running: boolean;
|
|
29
|
+
lastStartedAt: string | null;
|
|
30
|
+
lastFinishedAt: string | null;
|
|
31
|
+
lastTrigger: DirectorySyncTrigger | null;
|
|
32
|
+
lastOutcome: DirectorySyncOutcome | null;
|
|
33
|
+
lastCounts: DirectorySyncCounts | null;
|
|
34
|
+
lastErrorCode: DirectorySyncErrorCode | null;
|
|
35
|
+
/** The extension's message, truncated, with the org's secret config values masked. */
|
|
36
|
+
lastError: string | null;
|
|
37
|
+
/** The end of the last `synced` run. */
|
|
38
|
+
lastSuccessAt: string | null;
|
|
39
|
+
/** `failed` runs in a row (0 after any other outcome). */
|
|
40
|
+
consecutiveFailures: number;
|
|
41
|
+
/** The org's members this directory added (live count). */
|
|
42
|
+
people: number;
|
|
43
|
+
}
|
|
44
|
+
/** One org of the platform's directory list of an extension. */
|
|
45
|
+
export interface DirectorySyncOrgRow {
|
|
46
|
+
orgId: string;
|
|
47
|
+
orgName: string;
|
|
48
|
+
/** Whether the org follows the platform's config or keeps its own copy (since when). */
|
|
49
|
+
config: {
|
|
50
|
+
kind: "platform";
|
|
51
|
+
} | {
|
|
52
|
+
kind: "own";
|
|
53
|
+
since: string;
|
|
54
|
+
};
|
|
55
|
+
people: number;
|
|
56
|
+
lastFinishedAt: string | null;
|
|
57
|
+
lastOutcome: DirectorySyncOutcome | null;
|
|
58
|
+
lastErrorCode: DirectorySyncErrorCode | null;
|
|
59
|
+
lastError: string | null;
|
|
60
|
+
}
|
|
61
|
+
/** The outcomes the platform list's `attention` filter keeps. */
|
|
62
|
+
export declare const ATTENTION_OUTCOMES: readonly DirectorySyncOutcome[];
|
|
63
|
+
/** Rows per page of the platform list. */
|
|
64
|
+
export declare const DIRECTORY_ORG_PAGE_SIZE = 50;
|
|
65
|
+
/** Characters of a stored error message. */
|
|
66
|
+
export declare const SYNC_ERROR_MAX_CHARS = 500;
|
|
67
|
+
/**
|
|
68
|
+
* An extension's error message as stored: every secret value replaced by `***` (longest first, so
|
|
69
|
+
* a secret that contains another is masked whole), then truncated to {@link SYNC_ERROR_MAX_CHARS}
|
|
70
|
+
* characters. Masking runs before truncation so a cut can never leave half a secret.
|
|
71
|
+
*/
|
|
72
|
+
export declare function redactSyncError(message: string, secrets: readonly string[]): string;
|
|
73
|
+
/**
|
|
74
|
+
* Directory sync state per (org, directory extension) (`capybara.directory_sync_state`, RLS):
|
|
75
|
+
* `DirectorySync` marks a run started when it takes the lock and records the outcome in the
|
|
76
|
+
* transaction of its `org.directory.synced` audit row; the org's page and the platform list read
|
|
77
|
+
* it. No process state: a crashed run leaves `running_since`, which reads as not running once the
|
|
78
|
+
* run lock is gone.
|
|
79
|
+
*/
|
|
80
|
+
export declare class DirectorySyncStates {
|
|
81
|
+
private readonly db;
|
|
82
|
+
readonly txHost: TransactionHost;
|
|
83
|
+
private readonly redis;
|
|
84
|
+
constructor(db: KernelDb, txHost: TransactionHost, redis: Pick<Redis, "exists">);
|
|
85
|
+
/**
|
|
86
|
+
* A run took the lock (intent first: visible as running until it ends or the lock lapses).
|
|
87
|
+
* Returns the recorded start, which the run passes to {@link markFinished}.
|
|
88
|
+
*/
|
|
89
|
+
markStarted(orgId: string, plugin: string, trigger: DirectorySyncTrigger, now?: Date): Promise<Date>;
|
|
90
|
+
/**
|
|
91
|
+
* A run ended: its outcome, counts and (for `failed` / `partial`) the reason. Joins the caller's
|
|
92
|
+
* transaction, the one that writes the run's `org.directory.synced` audit row. `error` must
|
|
93
|
+
* already be redacted ({@link redactSyncError}); it is truncated again here as a guard.
|
|
94
|
+
* Written only while the row's start is still `startedAt`: a run that lost its lock never
|
|
95
|
+
* overwrites the state of a newer run that took it since. Returns whether it was written.
|
|
96
|
+
*/
|
|
97
|
+
markFinished(orgId: string, plugin: string, startedAt: Date, result: {
|
|
98
|
+
outcome: DirectorySyncOutcome;
|
|
99
|
+
counts: DirectorySyncCounts;
|
|
100
|
+
errorCode?: DirectorySyncErrorCode | undefined;
|
|
101
|
+
error?: string | undefined;
|
|
102
|
+
}, now?: Date): Promise<boolean>;
|
|
103
|
+
/**
|
|
104
|
+
* The org's state for `plugin`, or null when no run was ever recorded. Runs under the caller's
|
|
105
|
+
* org scope. `running`: `runningSince` is set and either younger than the lock TTL or the run
|
|
106
|
+
* lock is still held (a long run renews it); a crashed run's row reads as not running once its
|
|
107
|
+
* lock lapsed.
|
|
108
|
+
*/
|
|
109
|
+
get(orgId: string, plugin: string, now?: Date): Promise<DirectorySyncState | null>;
|
|
110
|
+
private isRunning;
|
|
111
|
+
/**
|
|
112
|
+
* The orgs where `plugin` is enabled, with their directory state, by org name: whether each
|
|
113
|
+
* follows the platform's config or keeps its own copy, its directory members, the last run.
|
|
114
|
+
* `attention` keeps the orgs whose last run was `failed`, `partial` or `removal_blocked`.
|
|
115
|
+
* Deployment-wide (super admin).
|
|
116
|
+
*/
|
|
117
|
+
listForPlugin(plugin: string, opts?: {
|
|
118
|
+
cursor?: string | undefined;
|
|
119
|
+
attention?: boolean;
|
|
120
|
+
limit?: number;
|
|
121
|
+
}): Promise<{
|
|
122
|
+
items: DirectorySyncOrgRow[];
|
|
123
|
+
next: string | null;
|
|
124
|
+
}>;
|
|
125
|
+
}
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
|
+
};
|
|
10
|
+
import { Inject, Injectable } from "@nestjs/common";
|
|
11
|
+
import { sql } from "kysely";
|
|
12
|
+
import { enabledExtensionsByOrg } from "../extensions/enabled.js";
|
|
13
|
+
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
14
|
+
import { asPlatform } from "../infra/database/org-scope.js";
|
|
15
|
+
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
16
|
+
import { Transactional } from "../infra/database/transactional.decorator.js";
|
|
17
|
+
import { prefixed } from "../infra/lock/lock.service.js";
|
|
18
|
+
import { REDIS } from "../infra/tokens.js";
|
|
19
|
+
import { decodeDirectoryOrgCursor, encodeDirectoryOrgCursor } from "./directory-sync.schemas.js";
|
|
20
|
+
/** The run lock's TTL (renewed while held); a crashed holder's lock expires within it. */
|
|
21
|
+
export const DIRECTORY_SYNC_LOCK_TTL_MS = 30_000;
|
|
22
|
+
/** The `LockService` key of one (org, extension) sync: one sync at a time. */
|
|
23
|
+
export const directorySyncLockKey = (orgId, plugin) => `directory-sync:${orgId}:${plugin}`;
|
|
24
|
+
/** The outcomes the platform list's `attention` filter keeps. */
|
|
25
|
+
export const ATTENTION_OUTCOMES = [
|
|
26
|
+
"failed",
|
|
27
|
+
"partial",
|
|
28
|
+
"removal_blocked",
|
|
29
|
+
];
|
|
30
|
+
/** Rows per page of the platform list. */
|
|
31
|
+
export const DIRECTORY_ORG_PAGE_SIZE = 50;
|
|
32
|
+
/** Characters of a stored error message. */
|
|
33
|
+
export const SYNC_ERROR_MAX_CHARS = 500;
|
|
34
|
+
/** Secret values shorter than this are not masked (they would mask ordinary words). */
|
|
35
|
+
const MIN_SECRET_CHARS = 4;
|
|
36
|
+
/**
|
|
37
|
+
* An extension's error message as stored: every secret value replaced by `***` (longest first, so
|
|
38
|
+
* a secret that contains another is masked whole), then truncated to {@link SYNC_ERROR_MAX_CHARS}
|
|
39
|
+
* characters. Masking runs before truncation so a cut can never leave half a secret.
|
|
40
|
+
*/
|
|
41
|
+
export function redactSyncError(message, secrets) {
|
|
42
|
+
let out = message;
|
|
43
|
+
const values = [...new Set(secrets.filter((s) => s.length >= MIN_SECRET_CHARS))].sort((a, b) => b.length - a.length);
|
|
44
|
+
for (const s of values)
|
|
45
|
+
out = out.split(s).join("***");
|
|
46
|
+
return out.length > SYNC_ERROR_MAX_CHARS ? `${out.slice(0, SYNC_ERROR_MAX_CHARS - 1)}…` : out;
|
|
47
|
+
}
|
|
48
|
+
const iso = (v) => v === null ? null : new Date(v).toISOString();
|
|
49
|
+
/**
|
|
50
|
+
* Directory sync state per (org, directory extension) (`capybara.directory_sync_state`, RLS):
|
|
51
|
+
* `DirectorySync` marks a run started when it takes the lock and records the outcome in the
|
|
52
|
+
* transaction of its `org.directory.synced` audit row; the org's page and the platform list read
|
|
53
|
+
* it. No process state: a crashed run leaves `running_since`, which reads as not running once the
|
|
54
|
+
* run lock is gone.
|
|
55
|
+
*/
|
|
56
|
+
let DirectorySyncStates = class DirectorySyncStates {
|
|
57
|
+
db;
|
|
58
|
+
txHost;
|
|
59
|
+
redis;
|
|
60
|
+
constructor(db, txHost, redis) {
|
|
61
|
+
this.db = db;
|
|
62
|
+
this.txHost = txHost;
|
|
63
|
+
this.redis = redis;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* A run took the lock (intent first: visible as running until it ends or the lock lapses).
|
|
67
|
+
* Returns the recorded start, which the run passes to {@link markFinished}.
|
|
68
|
+
*/
|
|
69
|
+
async markStarted(orgId, plugin, trigger, now = new Date()) {
|
|
70
|
+
await this.db
|
|
71
|
+
.insertInto("directory_sync_state")
|
|
72
|
+
.values({
|
|
73
|
+
org_id: orgId,
|
|
74
|
+
plugin,
|
|
75
|
+
running_since: now,
|
|
76
|
+
last_started_at: now,
|
|
77
|
+
last_trigger: trigger,
|
|
78
|
+
})
|
|
79
|
+
.onConflict((oc) => oc.columns(["org_id", "plugin"]).doUpdateSet({
|
|
80
|
+
running_since: now,
|
|
81
|
+
last_started_at: now,
|
|
82
|
+
last_trigger: trigger,
|
|
83
|
+
}))
|
|
84
|
+
.execute();
|
|
85
|
+
return now;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* A run ended: its outcome, counts and (for `failed` / `partial`) the reason. Joins the caller's
|
|
89
|
+
* transaction, the one that writes the run's `org.directory.synced` audit row. `error` must
|
|
90
|
+
* already be redacted ({@link redactSyncError}); it is truncated again here as a guard.
|
|
91
|
+
* Written only while the row's start is still `startedAt`: a run that lost its lock never
|
|
92
|
+
* overwrites the state of a newer run that took it since. Returns whether it was written.
|
|
93
|
+
*/
|
|
94
|
+
async markFinished(orgId, plugin, startedAt, result, now = new Date()) {
|
|
95
|
+
const failed = result.outcome === "failed";
|
|
96
|
+
const success = result.outcome === "synced";
|
|
97
|
+
const error = result.error === undefined ? null : redactSyncError(result.error, []);
|
|
98
|
+
const counts = JSON.stringify(result.counts);
|
|
99
|
+
const r = await this.db
|
|
100
|
+
.insertInto("directory_sync_state")
|
|
101
|
+
.values({
|
|
102
|
+
org_id: orgId,
|
|
103
|
+
plugin,
|
|
104
|
+
running_since: null,
|
|
105
|
+
last_started_at: startedAt,
|
|
106
|
+
last_finished_at: now,
|
|
107
|
+
last_outcome: result.outcome,
|
|
108
|
+
last_counts: counts,
|
|
109
|
+
last_error_code: result.errorCode ?? null,
|
|
110
|
+
last_error: error,
|
|
111
|
+
last_success_at: success ? now : null,
|
|
112
|
+
consecutive_failures: failed ? 1 : 0,
|
|
113
|
+
})
|
|
114
|
+
.onConflict((oc) => oc
|
|
115
|
+
.columns(["org_id", "plugin"])
|
|
116
|
+
.doUpdateSet((eb) => ({
|
|
117
|
+
running_since: null,
|
|
118
|
+
last_finished_at: now,
|
|
119
|
+
last_outcome: result.outcome,
|
|
120
|
+
last_counts: counts,
|
|
121
|
+
last_error_code: result.errorCode ?? null,
|
|
122
|
+
last_error: error,
|
|
123
|
+
...(success && { last_success_at: now }),
|
|
124
|
+
consecutive_failures: failed
|
|
125
|
+
? sql `${eb.ref("directory_sync_state.consecutive_failures")} + 1`
|
|
126
|
+
: 0,
|
|
127
|
+
}))
|
|
128
|
+
.where("directory_sync_state.last_started_at", "=", startedAt))
|
|
129
|
+
.executeTakeFirst();
|
|
130
|
+
return Number(r.numInsertedOrUpdatedRows ?? 0) > 0;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The org's state for `plugin`, or null when no run was ever recorded. Runs under the caller's
|
|
134
|
+
* org scope. `running`: `runningSince` is set and either younger than the lock TTL or the run
|
|
135
|
+
* lock is still held (a long run renews it); a crashed run's row reads as not running once its
|
|
136
|
+
* lock lapsed.
|
|
137
|
+
*/
|
|
138
|
+
async get(orgId, plugin, now = new Date()) {
|
|
139
|
+
const row = await this.db
|
|
140
|
+
.selectFrom("directory_sync_state")
|
|
141
|
+
.selectAll()
|
|
142
|
+
.where("org_id", "=", orgId)
|
|
143
|
+
.where("plugin", "=", plugin)
|
|
144
|
+
.executeTakeFirst();
|
|
145
|
+
if (!row)
|
|
146
|
+
return null;
|
|
147
|
+
const people = await this.db
|
|
148
|
+
.selectFrom("memberships")
|
|
149
|
+
.select((eb) => eb.fn.countAll().as("n"))
|
|
150
|
+
.where("org_id", "=", orgId)
|
|
151
|
+
.where("source", "=", `directory:${plugin}`)
|
|
152
|
+
.executeTakeFirstOrThrow();
|
|
153
|
+
const runningSince = iso(row.running_since);
|
|
154
|
+
return {
|
|
155
|
+
orgId,
|
|
156
|
+
plugin,
|
|
157
|
+
runningSince,
|
|
158
|
+
running: runningSince !== null && (await this.isRunning(orgId, plugin, runningSince, now)),
|
|
159
|
+
lastStartedAt: iso(row.last_started_at),
|
|
160
|
+
lastFinishedAt: iso(row.last_finished_at),
|
|
161
|
+
lastTrigger: row.last_trigger,
|
|
162
|
+
lastOutcome: row.last_outcome,
|
|
163
|
+
lastCounts: row.last_counts,
|
|
164
|
+
lastErrorCode: row.last_error_code,
|
|
165
|
+
lastError: row.last_error,
|
|
166
|
+
lastSuccessAt: iso(row.last_success_at),
|
|
167
|
+
consecutiveFailures: row.consecutive_failures,
|
|
168
|
+
people: Number(people.n),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
async isRunning(orgId, plugin, runningSince, now) {
|
|
172
|
+
if (now.getTime() - Date.parse(runningSince) < DIRECTORY_SYNC_LOCK_TTL_MS)
|
|
173
|
+
return true;
|
|
174
|
+
try {
|
|
175
|
+
return (await this.redis.exists(prefixed(directorySyncLockKey(orgId, plugin)))) === 1;
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
// Redis unreadable: past the TTL the age rule alone decides
|
|
179
|
+
return false;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* The orgs where `plugin` is enabled, with their directory state, by org name: whether each
|
|
184
|
+
* follows the platform's config or keeps its own copy, its directory members, the last run.
|
|
185
|
+
* `attention` keeps the orgs whose last run was `failed`, `partial` or `removal_blocked`.
|
|
186
|
+
* Deployment-wide (super admin).
|
|
187
|
+
*/
|
|
188
|
+
async listForPlugin(plugin, opts = {}) {
|
|
189
|
+
const limit = opts.limit ?? DIRECTORY_ORG_PAGE_SIZE;
|
|
190
|
+
const after = opts.cursor === undefined ? null : decodeDirectoryOrgCursor(opts.cursor);
|
|
191
|
+
const source = `directory:${plugin}`;
|
|
192
|
+
// org-filter: deployment-wide directory status of one extension across orgs (super admin list)
|
|
193
|
+
const rows = await asPlatform("directory sync status across orgs", () => {
|
|
194
|
+
const db = this.db.db;
|
|
195
|
+
let q = db
|
|
196
|
+
.selectFrom(enabledExtensionsByOrg(db, [plugin]).as("en"))
|
|
197
|
+
.innerJoin("orgs as o", "o.id", "en.org_id")
|
|
198
|
+
.leftJoin("directory_sync_state as s", (j) => j.onRef("s.org_id", "=", "en.org_id").on("s.plugin", "=", plugin))
|
|
199
|
+
.leftJoin("extension_org_overrides as ov", (j) => j.onRef("ov.org_id", "=", "en.org_id").on("ov.plugin", "=", plugin))
|
|
200
|
+
.select((eb) => [
|
|
201
|
+
"o.id as orgId",
|
|
202
|
+
"o.name as orgName",
|
|
203
|
+
"ov.created_at as copySince",
|
|
204
|
+
"s.last_finished_at as lastFinishedAt",
|
|
205
|
+
"s.last_outcome as lastOutcome",
|
|
206
|
+
"s.last_error_code as lastErrorCode",
|
|
207
|
+
"s.last_error as lastError",
|
|
208
|
+
eb
|
|
209
|
+
.selectFrom("memberships as m")
|
|
210
|
+
.select((m) => m.fn.countAll().as("n"))
|
|
211
|
+
.whereRef("m.org_id", "=", "en.org_id")
|
|
212
|
+
.where("m.source", "=", source)
|
|
213
|
+
.as("people"),
|
|
214
|
+
]);
|
|
215
|
+
if (opts.attention)
|
|
216
|
+
q = q.where("s.last_outcome", "in", [...ATTENTION_OUTCOMES]);
|
|
217
|
+
if (after !== null) {
|
|
218
|
+
q = q.where((eb) => eb(eb.refTuple("o.name", "o.id"), ">", eb.tuple(after[0], after[1])));
|
|
219
|
+
}
|
|
220
|
+
return q
|
|
221
|
+
.orderBy("o.name")
|
|
222
|
+
.orderBy("o.id")
|
|
223
|
+
.limit(limit + 1)
|
|
224
|
+
.execute();
|
|
225
|
+
});
|
|
226
|
+
const page = rows.slice(0, limit);
|
|
227
|
+
const last = page.at(-1);
|
|
228
|
+
return {
|
|
229
|
+
items: page.map((r) => ({
|
|
230
|
+
orgId: r.orgId,
|
|
231
|
+
orgName: r.orgName,
|
|
232
|
+
config: r.copySince === null
|
|
233
|
+
? { kind: "platform" }
|
|
234
|
+
: { kind: "own", since: new Date(r.copySince).toISOString() },
|
|
235
|
+
people: Number(r.people ?? 0),
|
|
236
|
+
lastFinishedAt: iso(r.lastFinishedAt),
|
|
237
|
+
lastOutcome: r.lastOutcome,
|
|
238
|
+
lastErrorCode: r.lastErrorCode,
|
|
239
|
+
lastError: r.lastError,
|
|
240
|
+
})),
|
|
241
|
+
next: rows.length > limit && last !== undefined
|
|
242
|
+
? encodeDirectoryOrgCursor(last.orgName, last.orgId)
|
|
243
|
+
: null,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
};
|
|
247
|
+
__decorate([
|
|
248
|
+
Transactional()
|
|
249
|
+
], DirectorySyncStates.prototype, "markStarted", null);
|
|
250
|
+
__decorate([
|
|
251
|
+
Transactional()
|
|
252
|
+
], DirectorySyncStates.prototype, "markFinished", null);
|
|
253
|
+
DirectorySyncStates = __decorate([
|
|
254
|
+
Injectable(),
|
|
255
|
+
__param(0, Inject(KernelDb)),
|
|
256
|
+
__param(1, Inject(TransactionHost)),
|
|
257
|
+
__param(2, Inject(REDIS))
|
|
258
|
+
], DirectorySyncStates);
|
|
259
|
+
export { DirectorySyncStates };
|
|
@@ -1,13 +1,26 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
1
2
|
import { type Principal } from "../identity/principal.js";
|
|
2
3
|
import { DirectorySync } from "./directory-sync.js";
|
|
4
|
+
import { OrgDirectoryParams } from "./directory-sync.schemas.js";
|
|
5
|
+
import { type DirectorySyncState, DirectorySyncStates } from "./directory-sync-state.js";
|
|
3
6
|
import { type OrgContext } from "./org.guard.js";
|
|
4
7
|
/**
|
|
5
|
-
*
|
|
6
|
-
* removes members, so only an admin and only
|
|
8
|
+
* An org's directory of one extension (`/api/v1/orgs/:orgId/extensions/:extension/directory`):
|
|
9
|
+
* its sync state, and the manual sync, which adds and removes members, so only an admin and only
|
|
10
|
+
* a human may start it.
|
|
7
11
|
*/
|
|
8
12
|
export declare class DirectorySyncController {
|
|
9
13
|
private readonly directory;
|
|
10
|
-
|
|
14
|
+
private readonly states;
|
|
15
|
+
constructor(directory: DirectorySync, states: DirectorySyncStates);
|
|
16
|
+
/**
|
|
17
|
+
* The org's directory sync state of the extension (last run, its outcome and reason, people,
|
|
18
|
+
* whether a run holds the lock now) as `state`; null before the first recorded run. The
|
|
19
|
+
* same role as the manual sync reads it.
|
|
20
|
+
*/
|
|
21
|
+
state(org: OrgContext, { extension }: z.infer<typeof OrgDirectoryParams>): Promise<{
|
|
22
|
+
state: DirectorySyncState | null;
|
|
23
|
+
}>;
|
|
11
24
|
sync(principal: Principal, org: OrgContext, extension: string): Promise<{
|
|
12
25
|
jobId: string;
|
|
13
26
|
}>;
|
|
@@ -7,24 +7,47 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
8
8
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
9
9
|
};
|
|
10
|
-
import { Controller, HttpCode, Inject, Param, Post } from "@nestjs/common";
|
|
10
|
+
import { Controller, Get, HttpCode, Inject, Param, Post } from "@nestjs/common";
|
|
11
11
|
import { CurrentPrincipal, patTokenId, requireUser, } from "../identity/principal.js";
|
|
12
|
+
import { createZodDto } from "../infra/validation/create-zod-dto.js";
|
|
13
|
+
import { ZParam } from "../infra/validation/zod-params.js";
|
|
12
14
|
import { DirectorySync } from "./directory-sync.js";
|
|
15
|
+
import { OrgDirectoryParams } from "./directory-sync.schemas.js";
|
|
16
|
+
import { DirectorySyncStates } from "./directory-sync-state.js";
|
|
13
17
|
import { CurrentOrg, OrgAction } from "./org.guard.js";
|
|
18
|
+
class OrgDirectoryParamsDto extends createZodDto(OrgDirectoryParams, "OrgDirectoryParams") {
|
|
19
|
+
}
|
|
14
20
|
/**
|
|
15
|
-
*
|
|
16
|
-
* removes members, so only an admin and only
|
|
21
|
+
* An org's directory of one extension (`/api/v1/orgs/:orgId/extensions/:extension/directory`):
|
|
22
|
+
* its sync state, and the manual sync, which adds and removes members, so only an admin and only
|
|
23
|
+
* a human may start it.
|
|
17
24
|
*/
|
|
18
25
|
let DirectorySyncController = class DirectorySyncController {
|
|
19
26
|
directory;
|
|
20
|
-
|
|
27
|
+
states;
|
|
28
|
+
constructor(directory, states) {
|
|
21
29
|
this.directory = directory;
|
|
30
|
+
this.states = states;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The org's directory sync state of the extension (last run, its outcome and reason, people,
|
|
34
|
+
* whether a run holds the lock now) as `state`; null before the first recorded run. The
|
|
35
|
+
* same role as the manual sync reads it.
|
|
36
|
+
*/
|
|
37
|
+
async state(org, { extension }) {
|
|
38
|
+
return { state: await this.states.get(org.orgId, extension) };
|
|
22
39
|
}
|
|
23
40
|
async sync(principal, org, extension) {
|
|
24
41
|
const p = requireUser(principal);
|
|
25
42
|
return this.directory.request(org.orgId, extension, p.account.id, patTokenId(p));
|
|
26
43
|
}
|
|
27
44
|
};
|
|
45
|
+
__decorate([
|
|
46
|
+
Get(),
|
|
47
|
+
OrgAction("org.members.manage"),
|
|
48
|
+
__param(0, CurrentOrg()),
|
|
49
|
+
__param(1, ZParam(OrgDirectoryParamsDto))
|
|
50
|
+
], DirectorySyncController.prototype, "state", null);
|
|
28
51
|
__decorate([
|
|
29
52
|
Post("sync"),
|
|
30
53
|
HttpCode(202),
|
|
@@ -35,6 +58,7 @@ __decorate([
|
|
|
35
58
|
], DirectorySyncController.prototype, "sync", null);
|
|
36
59
|
DirectorySyncController = __decorate([
|
|
37
60
|
Controller("api/v1/orgs/:orgId/extensions/:extension/directory"),
|
|
38
|
-
__param(0, Inject(DirectorySync))
|
|
61
|
+
__param(0, Inject(DirectorySync)),
|
|
62
|
+
__param(1, Inject(DirectorySyncStates))
|
|
39
63
|
], DirectorySyncController);
|
|
40
64
|
export { DirectorySyncController };
|
|
@@ -8,16 +8,14 @@ import { KernelDb } from "../infra/database/kernel-db.js";
|
|
|
8
8
|
import { TransactionHost } from "../infra/database/transaction-host.js";
|
|
9
9
|
import { LockService } from "../infra/lock/lock.service.js";
|
|
10
10
|
import type { KernelLogger } from "../log.js";
|
|
11
|
+
import { DIRECTORY_SYNC_LOCK_TTL_MS, DirectorySyncStates, type DirectorySyncTrigger, directorySyncLockKey } from "./directory-sync-state.js";
|
|
11
12
|
import { Offboarding } from "./offboarding.js";
|
|
12
13
|
import { OrgLock } from "./org-lock.js";
|
|
13
14
|
/** The hourly scan: cron job name (no `:`). */
|
|
14
15
|
export declare const DIRECTORY_SCAN_JOB = "directory-scan";
|
|
15
16
|
/** The job name of one (org, extension) sync on the maintenance queue. */
|
|
16
17
|
export declare const DIRECTORY_SYNC_JOB = "directory-sync";
|
|
17
|
-
|
|
18
|
-
export declare const DIRECTORY_SYNC_LOCK_TTL_MS = 30000;
|
|
19
|
-
/** The `LockService` key of one (org, extension) sync: one sync at a time. */
|
|
20
|
-
export declare const directorySyncLockKey: (orgId: string, plugin: string) => string;
|
|
18
|
+
export { DIRECTORY_SYNC_LOCK_TTL_MS, type DirectorySyncTrigger, directorySyncLockKey };
|
|
21
19
|
/** 脏标记的保留时长(秒):锁被占时记下,持有者跑完后取走并补一轮。 */
|
|
22
20
|
export declare const DIRECTORY_SYNC_DIRTY_TTL_SECONDS = 3600;
|
|
23
21
|
/** 脏标记(Redis 键):同步进行中又有同步请求被锁挡掉。 */
|
|
@@ -32,8 +30,6 @@ export declare const DIRECTORY_REMOVAL_GUARD_RATIO = 0.2;
|
|
|
32
30
|
export declare const DIRECTORY_PAGE_TIMEOUT_MS = 60000;
|
|
33
31
|
/** 一轮同步最多翻的页数:防止插件反复返回游标让任务无限长(超出按中途失败处理)。 */
|
|
34
32
|
export declare const DIRECTORY_MAX_PAGES = 10000;
|
|
35
|
-
/** 触发来源:每小时扫描、入站 `directory_changed` 提示、管理员手动。 */
|
|
36
|
-
export type DirectorySyncTrigger = "schedule" | "changed" | "manual";
|
|
37
33
|
/**
|
|
38
34
|
* Why a sync does not run: a stale job (the extension is unloaded or no longer enabled), the org was
|
|
39
35
|
* deleted, another sync holds the lock. `state_unreadable`: whether the extension is enabled in the
|
|
@@ -139,7 +135,8 @@ export declare class DirectorySync {
|
|
|
139
135
|
private readonly queue;
|
|
140
136
|
readonly lockService: LockService;
|
|
141
137
|
readonly log: KernelLogger;
|
|
142
|
-
|
|
138
|
+
private readonly states;
|
|
139
|
+
constructor(db: KernelDb, txHost: TransactionHost, redis: Redis, registry: Pick<ExtensionRegistry, "list" | "current">, orgConfigs: Pick<OrgExtensionConfigs, "get">, effective: Pick<EffectiveConfigs, "get">, external: Pick<ExternalIdentities, "resolve">, offboarding: Pick<Offboarding, "offboard" | "afterOffboard">, orgLock: OrgLock, queue: Pick<Queue, "add">, lockService: LockService, log: KernelLogger, states: Pick<DirectorySyncStates, "markStarted" | "markFinished">);
|
|
143
140
|
/** A `directory-sync` job (dispatched by `MaintenanceProcessor`). */
|
|
144
141
|
runSyncJob(data: SyncJobData): Promise<void>;
|
|
145
142
|
/** 插件提供的 `directory@1`;未加载或不提供时 undefined。 */
|
|
@@ -186,6 +183,20 @@ export declare class DirectorySync {
|
|
|
186
183
|
*/
|
|
187
184
|
private rerunIfDirty;
|
|
188
185
|
private run;
|
|
186
|
+
/** The run's `org.directory.synced` audit row and its recorded state, in one transaction. */
|
|
187
|
+
private finish;
|
|
188
|
+
/**
|
|
189
|
+
* The outcome of a run that threw after its lock was taken, written on its own (never inside
|
|
190
|
+
* the transaction that failed); a failure to write it is only logged.
|
|
191
|
+
*/
|
|
192
|
+
private recordInterrupted;
|
|
193
|
+
/**
|
|
194
|
+
* The secret values a stored error message masks, since an extension may echo a token in its
|
|
195
|
+
* error: those of the config the run used (the org's effective config) and those of the
|
|
196
|
+
* platform's running deployment config (an org with its own copy runs without the platform's
|
|
197
|
+
* overridable secrets, which the extension still holds in `ctx.config`).
|
|
198
|
+
*/
|
|
199
|
+
private secretValues;
|
|
189
200
|
/**
|
|
190
201
|
* 一个在职成员:解析外部身份(不合并、不登录,新身份行 `linked_via = directory`),不是成员就以 member 加入
|
|
191
202
|
* (source = 本通讯录);此人在该组织有本通讯录的排除(被手动移出 / 本人离开过)时不加入。
|