@lssm/integration.runtime 0.0.0-canary-20251217060834 → 0.0.0-canary-20251217063201
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/dist/health.d.ts +21 -0
- package/dist/health.js +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +1 -0
- package/dist/runtime.d.ts +99 -0
- package/dist/runtime.js +1 -0
- package/dist/secrets/env-secret-provider.d.ts +31 -0
- package/dist/secrets/env-secret-provider.js +1 -0
- package/dist/secrets/gcp-secret-manager.d.ts +32 -0
- package/dist/secrets/gcp-secret-manager.js +1 -0
- package/dist/secrets/index.d.ts +5 -0
- package/dist/secrets/index.js +1 -0
- package/dist/secrets/manager.d.ts +47 -0
- package/dist/secrets/manager.js +1 -0
- package/dist/secrets/provider.d.ts +52 -0
- package/dist/secrets/provider.js +1 -0
- package/package.json +13 -13
- package/dist/index.d.mts +0 -267
- package/dist/index.mjs +0 -714
package/dist/index.mjs
DELETED
|
@@ -1,714 +0,0 @@
|
|
|
1
|
-
import { performance } from "node:perf_hooks";
|
|
2
|
-
import { Buffer as Buffer$1 } from "node:buffer";
|
|
3
|
-
import { SecretManagerServiceClient, protos } from "@google-cloud/secret-manager";
|
|
4
|
-
|
|
5
|
-
//#region src/runtime.ts
|
|
6
|
-
const DEFAULT_MAX_ATTEMPTS = 3;
|
|
7
|
-
const DEFAULT_BACKOFF_MS = 250;
|
|
8
|
-
var IntegrationCallGuard = class {
|
|
9
|
-
telemetry;
|
|
10
|
-
maxAttempts;
|
|
11
|
-
backoffMs;
|
|
12
|
-
shouldRetry;
|
|
13
|
-
sleep;
|
|
14
|
-
now;
|
|
15
|
-
constructor(secretProvider, options = {}) {
|
|
16
|
-
this.secretProvider = secretProvider;
|
|
17
|
-
this.telemetry = options.telemetry;
|
|
18
|
-
this.maxAttempts = Math.max(1, options.maxAttempts ?? DEFAULT_MAX_ATTEMPTS);
|
|
19
|
-
this.backoffMs = options.backoffMs ?? DEFAULT_BACKOFF_MS;
|
|
20
|
-
this.shouldRetry = options.shouldRetry ?? ((error) => typeof error === "object" && error !== null && "retryable" in error && Boolean(error.retryable));
|
|
21
|
-
this.sleep = options.sleep ?? ((ms) => ms <= 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, ms)));
|
|
22
|
-
this.now = options.now ?? (() => /* @__PURE__ */ new Date());
|
|
23
|
-
}
|
|
24
|
-
async executeWithGuards(slotId, operation, _input, resolvedConfig, executor) {
|
|
25
|
-
const integration = this.findIntegration(slotId, resolvedConfig);
|
|
26
|
-
if (!integration) return this.failure({
|
|
27
|
-
tenantId: resolvedConfig.tenantId,
|
|
28
|
-
appId: resolvedConfig.appId,
|
|
29
|
-
environment: resolvedConfig.environment,
|
|
30
|
-
blueprintName: resolvedConfig.blueprintName,
|
|
31
|
-
blueprintVersion: resolvedConfig.blueprintVersion,
|
|
32
|
-
configVersion: resolvedConfig.configVersion,
|
|
33
|
-
slotId,
|
|
34
|
-
operation
|
|
35
|
-
}, void 0, {
|
|
36
|
-
code: "SLOT_NOT_BOUND",
|
|
37
|
-
message: `Integration slot "${slotId}" is not bound for tenant "${resolvedConfig.tenantId}".`,
|
|
38
|
-
retryable: false
|
|
39
|
-
}, 0);
|
|
40
|
-
const status = integration.connection.status;
|
|
41
|
-
if (status === "disconnected" || status === "error") return this.failure(this.makeContext(slotId, operation, resolvedConfig), integration, {
|
|
42
|
-
code: "CONNECTION_NOT_READY",
|
|
43
|
-
message: `Integration connection "${integration.connection.meta.label}" is in status "${status}".`,
|
|
44
|
-
retryable: false
|
|
45
|
-
}, 0);
|
|
46
|
-
const secrets = await this.fetchSecrets(integration.connection);
|
|
47
|
-
let attempt = 0;
|
|
48
|
-
const started = performance.now();
|
|
49
|
-
while (attempt < this.maxAttempts) {
|
|
50
|
-
attempt += 1;
|
|
51
|
-
try {
|
|
52
|
-
const data = await executor(integration.connection, secrets);
|
|
53
|
-
const duration = performance.now() - started;
|
|
54
|
-
this.emitTelemetry(this.makeContext(slotId, operation, resolvedConfig), integration, "success", duration);
|
|
55
|
-
return {
|
|
56
|
-
success: true,
|
|
57
|
-
data,
|
|
58
|
-
metadata: {
|
|
59
|
-
latencyMs: duration,
|
|
60
|
-
connectionId: integration.connection.meta.id,
|
|
61
|
-
ownershipMode: integration.connection.ownershipMode,
|
|
62
|
-
attempts: attempt
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
} catch (error) {
|
|
66
|
-
const duration = performance.now() - started;
|
|
67
|
-
this.emitTelemetry(this.makeContext(slotId, operation, resolvedConfig), integration, "error", duration, this.errorCodeFor(error), error instanceof Error ? error.message : String(error));
|
|
68
|
-
const retryable = this.shouldRetry(error, attempt);
|
|
69
|
-
if (!retryable || attempt >= this.maxAttempts) return {
|
|
70
|
-
success: false,
|
|
71
|
-
error: {
|
|
72
|
-
code: this.errorCodeFor(error),
|
|
73
|
-
message: error instanceof Error ? error.message : String(error),
|
|
74
|
-
retryable,
|
|
75
|
-
cause: error
|
|
76
|
-
},
|
|
77
|
-
metadata: {
|
|
78
|
-
latencyMs: duration,
|
|
79
|
-
connectionId: integration.connection.meta.id,
|
|
80
|
-
ownershipMode: integration.connection.ownershipMode,
|
|
81
|
-
attempts: attempt
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
await this.sleep(this.backoffMs);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return {
|
|
88
|
-
success: false,
|
|
89
|
-
error: {
|
|
90
|
-
code: "UNKNOWN_ERROR",
|
|
91
|
-
message: "Integration call failed after retries.",
|
|
92
|
-
retryable: false
|
|
93
|
-
},
|
|
94
|
-
metadata: {
|
|
95
|
-
latencyMs: performance.now() - started,
|
|
96
|
-
connectionId: integration.connection.meta.id,
|
|
97
|
-
ownershipMode: integration.connection.ownershipMode,
|
|
98
|
-
attempts: this.maxAttempts
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
}
|
|
102
|
-
findIntegration(slotId, config) {
|
|
103
|
-
return config.integrations.find((integration) => integration.slot.slotId === slotId);
|
|
104
|
-
}
|
|
105
|
-
async fetchSecrets(connection) {
|
|
106
|
-
if (!this.secretProvider.canHandle(connection.secretRef)) throw new Error(`Secret provider "${this.secretProvider.id}" cannot handle reference "${connection.secretRef}".`);
|
|
107
|
-
const secret = await this.secretProvider.getSecret(connection.secretRef);
|
|
108
|
-
return this.parseSecret(secret);
|
|
109
|
-
}
|
|
110
|
-
parseSecret(secret) {
|
|
111
|
-
const text = new TextDecoder().decode(secret.data);
|
|
112
|
-
try {
|
|
113
|
-
const parsed = JSON.parse(text);
|
|
114
|
-
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
|
|
115
|
-
const entries = Object.entries(parsed).filter(([, value]) => typeof value === "string" || typeof value === "number" || typeof value === "boolean");
|
|
116
|
-
return Object.fromEntries(entries.map(([key, value]) => [key, String(value)]));
|
|
117
|
-
}
|
|
118
|
-
} catch {}
|
|
119
|
-
return { secret: text };
|
|
120
|
-
}
|
|
121
|
-
emitTelemetry(context, integration, status, durationMs, errorCode, errorMessage) {
|
|
122
|
-
if (!this.telemetry || !integration) return;
|
|
123
|
-
this.telemetry.record({
|
|
124
|
-
tenantId: context.tenantId,
|
|
125
|
-
appId: context.appId,
|
|
126
|
-
environment: context.environment,
|
|
127
|
-
slotId: context.slotId,
|
|
128
|
-
integrationKey: integration.connection.meta.integrationKey,
|
|
129
|
-
integrationVersion: integration.connection.meta.integrationVersion,
|
|
130
|
-
connectionId: integration.connection.meta.id,
|
|
131
|
-
status,
|
|
132
|
-
durationMs,
|
|
133
|
-
errorCode,
|
|
134
|
-
errorMessage,
|
|
135
|
-
occurredAt: this.now(),
|
|
136
|
-
metadata: {
|
|
137
|
-
blueprint: `${context.blueprintName}.v${context.blueprintVersion}`,
|
|
138
|
-
configVersion: context.configVersion,
|
|
139
|
-
operation: context.operation
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
failure(context, integration, error, attempts) {
|
|
144
|
-
if (integration) this.emitTelemetry(context, integration, "error", 0, error.code, error.message);
|
|
145
|
-
return {
|
|
146
|
-
success: false,
|
|
147
|
-
error,
|
|
148
|
-
metadata: {
|
|
149
|
-
latencyMs: 0,
|
|
150
|
-
connectionId: integration?.connection.meta.id ?? "unknown",
|
|
151
|
-
ownershipMode: integration?.connection.ownershipMode ?? "managed",
|
|
152
|
-
attempts
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
makeContext(slotId, operation, config) {
|
|
157
|
-
return {
|
|
158
|
-
tenantId: config.tenantId,
|
|
159
|
-
appId: config.appId,
|
|
160
|
-
environment: config.environment,
|
|
161
|
-
blueprintName: config.blueprintName,
|
|
162
|
-
blueprintVersion: config.blueprintVersion,
|
|
163
|
-
configVersion: config.configVersion,
|
|
164
|
-
slotId,
|
|
165
|
-
operation
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
errorCodeFor(error) {
|
|
169
|
-
if (typeof error === "object" && error !== null && "code" in error && typeof error.code === "string") return error.code;
|
|
170
|
-
return "PROVIDER_ERROR";
|
|
171
|
-
}
|
|
172
|
-
};
|
|
173
|
-
function ensureConnectionReady(integration) {
|
|
174
|
-
const status = integration.connection.status;
|
|
175
|
-
if (status === "disconnected" || status === "error") throw new Error(`Integration connection "${integration.connection.meta.label}" is in status "${status}".`);
|
|
176
|
-
}
|
|
177
|
-
function connectionStatusLabel(status) {
|
|
178
|
-
switch (status) {
|
|
179
|
-
case "connected": return "connected";
|
|
180
|
-
case "disconnected": return "disconnected";
|
|
181
|
-
case "error": return "error";
|
|
182
|
-
case "unknown":
|
|
183
|
-
default: return "unknown";
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
//#endregion
|
|
188
|
-
//#region src/health.ts
|
|
189
|
-
var IntegrationHealthService = class {
|
|
190
|
-
telemetry;
|
|
191
|
-
nowFn;
|
|
192
|
-
constructor(options = {}) {
|
|
193
|
-
this.telemetry = options.telemetry;
|
|
194
|
-
this.nowFn = options.now ?? (() => /* @__PURE__ */ new Date());
|
|
195
|
-
}
|
|
196
|
-
async check(context, executor) {
|
|
197
|
-
const start = this.nowFn();
|
|
198
|
-
try {
|
|
199
|
-
await executor(context);
|
|
200
|
-
const end = this.nowFn();
|
|
201
|
-
const result = {
|
|
202
|
-
status: "connected",
|
|
203
|
-
checkedAt: end,
|
|
204
|
-
latencyMs: end.getTime() - start.getTime()
|
|
205
|
-
};
|
|
206
|
-
this.emitTelemetry(context, result, "success");
|
|
207
|
-
return result;
|
|
208
|
-
} catch (error) {
|
|
209
|
-
const end = this.nowFn();
|
|
210
|
-
const message = error instanceof Error ? error.message : "Unknown error";
|
|
211
|
-
const code = extractErrorCode(error);
|
|
212
|
-
const result = {
|
|
213
|
-
status: "error",
|
|
214
|
-
checkedAt: end,
|
|
215
|
-
latencyMs: end.getTime() - start.getTime(),
|
|
216
|
-
errorMessage: message,
|
|
217
|
-
errorCode: code
|
|
218
|
-
};
|
|
219
|
-
this.emitTelemetry(context, result, "error", code, message);
|
|
220
|
-
return result;
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
emitTelemetry(context, result, status, errorCode, errorMessage) {
|
|
224
|
-
if (!this.telemetry) return;
|
|
225
|
-
this.telemetry.record({
|
|
226
|
-
tenantId: context.tenantId,
|
|
227
|
-
appId: context.appId,
|
|
228
|
-
environment: context.environment,
|
|
229
|
-
slotId: context.slotId,
|
|
230
|
-
integrationKey: context.spec.meta.key,
|
|
231
|
-
integrationVersion: context.spec.meta.version,
|
|
232
|
-
connectionId: context.connection.meta.id,
|
|
233
|
-
status,
|
|
234
|
-
durationMs: result.latencyMs,
|
|
235
|
-
errorCode,
|
|
236
|
-
errorMessage,
|
|
237
|
-
occurredAt: result.checkedAt ?? this.nowFn(),
|
|
238
|
-
metadata: {
|
|
239
|
-
...context.trace ? {
|
|
240
|
-
blueprint: `${context.trace.blueprintName}.v${context.trace.blueprintVersion}`,
|
|
241
|
-
configVersion: context.trace.configVersion
|
|
242
|
-
} : {},
|
|
243
|
-
status: result.status
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
function extractErrorCode(error) {
|
|
249
|
-
if (!error || typeof error !== "object") return void 0;
|
|
250
|
-
const candidate = error;
|
|
251
|
-
if (candidate.code == null) return void 0;
|
|
252
|
-
return String(candidate.code);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
//#endregion
|
|
256
|
-
//#region src/secrets/provider.ts
|
|
257
|
-
var SecretProviderError = class extends Error {
|
|
258
|
-
provider;
|
|
259
|
-
reference;
|
|
260
|
-
code;
|
|
261
|
-
cause;
|
|
262
|
-
constructor(params) {
|
|
263
|
-
super(params.message);
|
|
264
|
-
this.name = "SecretProviderError";
|
|
265
|
-
this.provider = params.provider;
|
|
266
|
-
this.reference = params.reference;
|
|
267
|
-
this.code = params.code ?? "UNKNOWN";
|
|
268
|
-
this.cause = params.cause;
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
function parseSecretUri(reference) {
|
|
272
|
-
if (!reference) throw new SecretProviderError({
|
|
273
|
-
message: "Secret reference cannot be empty",
|
|
274
|
-
provider: "unknown",
|
|
275
|
-
reference,
|
|
276
|
-
code: "INVALID"
|
|
277
|
-
});
|
|
278
|
-
const [scheme, rest] = reference.split("://");
|
|
279
|
-
if (!scheme || !rest) throw new SecretProviderError({
|
|
280
|
-
message: `Invalid secret reference: ${reference}`,
|
|
281
|
-
provider: "unknown",
|
|
282
|
-
reference,
|
|
283
|
-
code: "INVALID"
|
|
284
|
-
});
|
|
285
|
-
const queryIndex = rest.indexOf("?");
|
|
286
|
-
if (queryIndex === -1) return {
|
|
287
|
-
provider: scheme,
|
|
288
|
-
path: rest
|
|
289
|
-
};
|
|
290
|
-
const path = rest.slice(0, queryIndex);
|
|
291
|
-
const query = rest.slice(queryIndex + 1);
|
|
292
|
-
return {
|
|
293
|
-
provider: scheme,
|
|
294
|
-
path,
|
|
295
|
-
extras: Object.fromEntries(query.split("&").filter(Boolean).map((pair) => {
|
|
296
|
-
const [keyRaw, valueRaw] = pair.split("=");
|
|
297
|
-
const key = keyRaw ?? "";
|
|
298
|
-
const value = valueRaw ?? "";
|
|
299
|
-
return [decodeURIComponent(key), decodeURIComponent(value)];
|
|
300
|
-
}))
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
function normalizeSecretPayload(payload) {
|
|
304
|
-
if (payload.data instanceof Uint8Array) return payload.data;
|
|
305
|
-
if (payload.encoding === "base64") return Buffer$1.from(payload.data, "base64");
|
|
306
|
-
if (payload.encoding === "binary") return Buffer$1.from(payload.data, "binary");
|
|
307
|
-
return Buffer$1.from(payload.data, "utf-8");
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
//#endregion
|
|
311
|
-
//#region src/secrets/gcp-secret-manager.ts
|
|
312
|
-
const DEFAULT_REPLICATION = { automatic: {} };
|
|
313
|
-
var GcpSecretManagerProvider = class {
|
|
314
|
-
id = "gcp-secret-manager";
|
|
315
|
-
client;
|
|
316
|
-
explicitProjectId;
|
|
317
|
-
replication;
|
|
318
|
-
constructor(options = {}) {
|
|
319
|
-
this.client = options.client ?? new SecretManagerServiceClient(options.clientOptions ?? {});
|
|
320
|
-
this.explicitProjectId = options.projectId;
|
|
321
|
-
this.replication = options.defaultReplication ?? DEFAULT_REPLICATION;
|
|
322
|
-
}
|
|
323
|
-
canHandle(reference) {
|
|
324
|
-
try {
|
|
325
|
-
return parseSecretUri(reference).provider === "gcp";
|
|
326
|
-
} catch {
|
|
327
|
-
return false;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
async getSecret(reference, options, callOptions) {
|
|
331
|
-
const location = this.parseReference(reference);
|
|
332
|
-
const secretVersionName = this.buildVersionName(location, options?.version);
|
|
333
|
-
try {
|
|
334
|
-
const [result] = await this.client.accessSecretVersion({ name: secretVersionName }, callOptions ?? {});
|
|
335
|
-
const payload = result.payload;
|
|
336
|
-
if (!payload?.data) throw new SecretProviderError({
|
|
337
|
-
message: `Secret payload empty for ${secretVersionName}`,
|
|
338
|
-
provider: this.id,
|
|
339
|
-
reference,
|
|
340
|
-
code: "UNKNOWN"
|
|
341
|
-
});
|
|
342
|
-
const version = extractVersionFromName(result.name ?? secretVersionName);
|
|
343
|
-
return {
|
|
344
|
-
data: payload.data,
|
|
345
|
-
version,
|
|
346
|
-
metadata: payload.dataCrc32c ? { crc32c: payload.dataCrc32c.toString() } : void 0,
|
|
347
|
-
retrievedAt: /* @__PURE__ */ new Date()
|
|
348
|
-
};
|
|
349
|
-
} catch (error) {
|
|
350
|
-
throw toSecretProviderError({
|
|
351
|
-
error,
|
|
352
|
-
provider: this.id,
|
|
353
|
-
reference,
|
|
354
|
-
operation: "access"
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
async setSecret(reference, payload) {
|
|
359
|
-
const location = this.parseReference(reference);
|
|
360
|
-
const { secretName } = this.buildNames(location);
|
|
361
|
-
const data = normalizeSecretPayload(payload);
|
|
362
|
-
await this.ensureSecretExists(location, payload);
|
|
363
|
-
try {
|
|
364
|
-
const response = await this.client.addSecretVersion({
|
|
365
|
-
parent: secretName,
|
|
366
|
-
payload: { data }
|
|
367
|
-
});
|
|
368
|
-
if (!response) throw new SecretProviderError({
|
|
369
|
-
message: `No version returned when adding secret version for ${secretName}`,
|
|
370
|
-
provider: this.id,
|
|
371
|
-
reference,
|
|
372
|
-
code: "UNKNOWN"
|
|
373
|
-
});
|
|
374
|
-
const [version] = response;
|
|
375
|
-
const versionName = version?.name ?? `${secretName}/versions/latest`;
|
|
376
|
-
return {
|
|
377
|
-
reference: `gcp://${versionName}`,
|
|
378
|
-
version: extractVersionFromName(versionName) ?? "latest"
|
|
379
|
-
};
|
|
380
|
-
} catch (error) {
|
|
381
|
-
throw toSecretProviderError({
|
|
382
|
-
error,
|
|
383
|
-
provider: this.id,
|
|
384
|
-
reference,
|
|
385
|
-
operation: "addSecretVersion"
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
async rotateSecret(reference, payload) {
|
|
390
|
-
return this.setSecret(reference, payload);
|
|
391
|
-
}
|
|
392
|
-
async deleteSecret(reference) {
|
|
393
|
-
const location = this.parseReference(reference);
|
|
394
|
-
const { secretName } = this.buildNames(location);
|
|
395
|
-
try {
|
|
396
|
-
await this.client.deleteSecret({ name: secretName });
|
|
397
|
-
} catch (error) {
|
|
398
|
-
throw toSecretProviderError({
|
|
399
|
-
error,
|
|
400
|
-
provider: this.id,
|
|
401
|
-
reference,
|
|
402
|
-
operation: "delete"
|
|
403
|
-
});
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
parseReference(reference) {
|
|
407
|
-
const parsed = parseSecretUri(reference);
|
|
408
|
-
if (parsed.provider !== "gcp") throw new SecretProviderError({
|
|
409
|
-
message: `Unsupported secret provider: ${parsed.provider}`,
|
|
410
|
-
provider: this.id,
|
|
411
|
-
reference,
|
|
412
|
-
code: "INVALID"
|
|
413
|
-
});
|
|
414
|
-
const segments = parsed.path.split("/").filter(Boolean);
|
|
415
|
-
if (segments.length < 4 || segments[0] !== "projects") throw new SecretProviderError({
|
|
416
|
-
message: `Expected secret reference format gcp://projects/{project}/secrets/{secret}[(/versions/{version})] but received "${parsed.path}"`,
|
|
417
|
-
provider: this.id,
|
|
418
|
-
reference,
|
|
419
|
-
code: "INVALID"
|
|
420
|
-
});
|
|
421
|
-
const projectIdCandidate = segments[1] ?? this.explicitProjectId;
|
|
422
|
-
if (!projectIdCandidate) throw new SecretProviderError({
|
|
423
|
-
message: `Unable to resolve project or secret from reference "${parsed.path}"`,
|
|
424
|
-
provider: this.id,
|
|
425
|
-
reference,
|
|
426
|
-
code: "INVALID"
|
|
427
|
-
});
|
|
428
|
-
const indexOfSecrets = segments.indexOf("secrets");
|
|
429
|
-
if (indexOfSecrets === -1 || indexOfSecrets + 1 >= segments.length) throw new SecretProviderError({
|
|
430
|
-
message: `Unable to resolve project or secret from reference "${parsed.path}"`,
|
|
431
|
-
provider: this.id,
|
|
432
|
-
reference,
|
|
433
|
-
code: "INVALID"
|
|
434
|
-
});
|
|
435
|
-
const resolvedProjectId = projectIdCandidate;
|
|
436
|
-
const secretIdCandidate = segments[indexOfSecrets + 1];
|
|
437
|
-
if (!secretIdCandidate) throw new SecretProviderError({
|
|
438
|
-
message: `Unable to resolve secret ID from reference "${parsed.path}"`,
|
|
439
|
-
provider: this.id,
|
|
440
|
-
reference,
|
|
441
|
-
code: "INVALID"
|
|
442
|
-
});
|
|
443
|
-
const secretId = secretIdCandidate;
|
|
444
|
-
const indexOfVersions = segments.indexOf("versions");
|
|
445
|
-
return {
|
|
446
|
-
projectId: resolvedProjectId,
|
|
447
|
-
secretId,
|
|
448
|
-
version: parsed.extras?.version ?? (indexOfVersions !== -1 && indexOfVersions + 1 < segments.length ? segments[indexOfVersions + 1] : void 0)
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
buildNames(location) {
|
|
452
|
-
const projectId = location.projectId ?? this.explicitProjectId;
|
|
453
|
-
if (!projectId) throw new SecretProviderError({
|
|
454
|
-
message: "Project ID must be provided either in reference or provider configuration",
|
|
455
|
-
provider: this.id,
|
|
456
|
-
reference: `gcp://projects//secrets/${location.secretId}`,
|
|
457
|
-
code: "INVALID"
|
|
458
|
-
});
|
|
459
|
-
const projectParent = `projects/${projectId}`;
|
|
460
|
-
return {
|
|
461
|
-
projectParent,
|
|
462
|
-
secretName: `${projectParent}/secrets/${location.secretId}`
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
buildVersionName(location, explicitVersion) {
|
|
466
|
-
const { secretName } = this.buildNames(location);
|
|
467
|
-
return `${secretName}/versions/${explicitVersion ?? location.version ?? "latest"}`;
|
|
468
|
-
}
|
|
469
|
-
async ensureSecretExists(location, payload) {
|
|
470
|
-
const { secretName, projectParent } = this.buildNames(location);
|
|
471
|
-
try {
|
|
472
|
-
await this.client.getSecret({ name: secretName });
|
|
473
|
-
} catch (error) {
|
|
474
|
-
const providerError = toSecretProviderError({
|
|
475
|
-
error,
|
|
476
|
-
provider: this.id,
|
|
477
|
-
reference: `gcp://${secretName}`,
|
|
478
|
-
operation: "getSecret",
|
|
479
|
-
suppressThrow: true
|
|
480
|
-
});
|
|
481
|
-
if (!providerError || providerError.code !== "NOT_FOUND") {
|
|
482
|
-
if (providerError) throw providerError;
|
|
483
|
-
throw error;
|
|
484
|
-
}
|
|
485
|
-
try {
|
|
486
|
-
await this.client.createSecret({
|
|
487
|
-
parent: projectParent,
|
|
488
|
-
secretId: location.secretId,
|
|
489
|
-
secret: {
|
|
490
|
-
replication: this.replication,
|
|
491
|
-
labels: payload.labels
|
|
492
|
-
}
|
|
493
|
-
});
|
|
494
|
-
} catch (creationError) {
|
|
495
|
-
throw toSecretProviderError({
|
|
496
|
-
error: creationError,
|
|
497
|
-
provider: this.id,
|
|
498
|
-
reference: `gcp://${secretName}`,
|
|
499
|
-
operation: "createSecret"
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
};
|
|
505
|
-
function extractVersionFromName(name) {
|
|
506
|
-
const segments = name.split("/").filter(Boolean);
|
|
507
|
-
const index = segments.indexOf("versions");
|
|
508
|
-
if (index === -1 || index + 1 >= segments.length) return;
|
|
509
|
-
return segments[index + 1];
|
|
510
|
-
}
|
|
511
|
-
function toSecretProviderError(params) {
|
|
512
|
-
const { error, provider, reference, operation, suppressThrow } = params;
|
|
513
|
-
if (error instanceof SecretProviderError) return error;
|
|
514
|
-
const code = deriveErrorCode(error);
|
|
515
|
-
const providerError = new SecretProviderError({
|
|
516
|
-
message: error instanceof Error ? error.message : `Unknown error during ${operation}`,
|
|
517
|
-
provider,
|
|
518
|
-
reference,
|
|
519
|
-
code,
|
|
520
|
-
cause: error
|
|
521
|
-
});
|
|
522
|
-
if (suppressThrow) return providerError;
|
|
523
|
-
throw providerError;
|
|
524
|
-
}
|
|
525
|
-
function deriveErrorCode(error) {
|
|
526
|
-
if (typeof error !== "object" || error === null) return "UNKNOWN";
|
|
527
|
-
const code = error.code;
|
|
528
|
-
if (code === 5 || code === "NOT_FOUND") return "NOT_FOUND";
|
|
529
|
-
if (code === 6 || code === "ALREADY_EXISTS") return "INVALID";
|
|
530
|
-
if (code === 7 || code === "PERMISSION_DENIED" || code === 403) return "FORBIDDEN";
|
|
531
|
-
if (code === 3 || code === "INVALID_ARGUMENT") return "INVALID";
|
|
532
|
-
return "UNKNOWN";
|
|
533
|
-
}
|
|
534
|
-
|
|
535
|
-
//#endregion
|
|
536
|
-
//#region src/secrets/env-secret-provider.ts
|
|
537
|
-
/**
|
|
538
|
-
* Environment-variable backed secret provider. Read-only by design.
|
|
539
|
-
* Allows overriding other secret providers by deriving environment variable
|
|
540
|
-
* names from secret references (or by using explicit aliases).
|
|
541
|
-
*/
|
|
542
|
-
var EnvSecretProvider = class {
|
|
543
|
-
id = "env";
|
|
544
|
-
aliases;
|
|
545
|
-
constructor(options = {}) {
|
|
546
|
-
this.aliases = options.aliases ?? {};
|
|
547
|
-
}
|
|
548
|
-
canHandle(reference) {
|
|
549
|
-
const envKey = this.resolveEnvKey(reference);
|
|
550
|
-
return envKey !== void 0 && process.env[envKey] !== void 0;
|
|
551
|
-
}
|
|
552
|
-
async getSecret(reference) {
|
|
553
|
-
const envKey = this.resolveEnvKey(reference);
|
|
554
|
-
if (!envKey) throw new SecretProviderError({
|
|
555
|
-
message: `Unable to resolve environment variable for reference "${reference}".`,
|
|
556
|
-
provider: this.id,
|
|
557
|
-
reference,
|
|
558
|
-
code: "INVALID"
|
|
559
|
-
});
|
|
560
|
-
const value = process.env[envKey];
|
|
561
|
-
if (value === void 0) throw new SecretProviderError({
|
|
562
|
-
message: `Environment variable "${envKey}" not found for reference "${reference}".`,
|
|
563
|
-
provider: this.id,
|
|
564
|
-
reference,
|
|
565
|
-
code: "NOT_FOUND"
|
|
566
|
-
});
|
|
567
|
-
return {
|
|
568
|
-
data: Buffer.from(value, "utf-8"),
|
|
569
|
-
version: "current",
|
|
570
|
-
metadata: {
|
|
571
|
-
source: "env",
|
|
572
|
-
envKey
|
|
573
|
-
},
|
|
574
|
-
retrievedAt: /* @__PURE__ */ new Date()
|
|
575
|
-
};
|
|
576
|
-
}
|
|
577
|
-
async setSecret(reference, _payload) {
|
|
578
|
-
throw this.forbiddenError("setSecret", reference);
|
|
579
|
-
}
|
|
580
|
-
async rotateSecret(reference, _payload) {
|
|
581
|
-
throw this.forbiddenError("rotateSecret", reference);
|
|
582
|
-
}
|
|
583
|
-
async deleteSecret(reference) {
|
|
584
|
-
throw this.forbiddenError("deleteSecret", reference);
|
|
585
|
-
}
|
|
586
|
-
resolveEnvKey(reference) {
|
|
587
|
-
if (!reference) return;
|
|
588
|
-
if (this.aliases[reference]) return this.aliases[reference];
|
|
589
|
-
if (!reference.includes("://")) return reference;
|
|
590
|
-
try {
|
|
591
|
-
const parsed = parseSecretUri(reference);
|
|
592
|
-
if (parsed.provider === "env") return parsed.path;
|
|
593
|
-
if (parsed.extras?.env) return parsed.extras.env;
|
|
594
|
-
return this.deriveEnvKey(parsed.path);
|
|
595
|
-
} catch {
|
|
596
|
-
return reference;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
deriveEnvKey(path) {
|
|
600
|
-
if (!path) return void 0;
|
|
601
|
-
return path.split(/[\/:\-\.]/).filter(Boolean).map((segment) => segment.replace(/[^a-zA-Z0-9]/g, "_").replace(/_{2,}/g, "_").toUpperCase()).join("_");
|
|
602
|
-
}
|
|
603
|
-
forbiddenError(operation, reference) {
|
|
604
|
-
return new SecretProviderError({
|
|
605
|
-
message: `EnvSecretProvider is read-only. "${operation}" is not allowed for ${reference}.`,
|
|
606
|
-
provider: this.id,
|
|
607
|
-
reference,
|
|
608
|
-
code: "FORBIDDEN"
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
};
|
|
612
|
-
|
|
613
|
-
//#endregion
|
|
614
|
-
//#region src/secrets/manager.ts
|
|
615
|
-
/**
|
|
616
|
-
* Composite secret provider that delegates to registered providers.
|
|
617
|
-
* Providers are attempted in order of descending priority, respecting the
|
|
618
|
-
* registration order for ties. This enables privileged overrides (e.g.
|
|
619
|
-
* environment variables) while still supporting durable backends like GCP
|
|
620
|
-
* Secret Manager.
|
|
621
|
-
*/
|
|
622
|
-
var SecretProviderManager = class {
|
|
623
|
-
id;
|
|
624
|
-
providers = [];
|
|
625
|
-
registrationCounter = 0;
|
|
626
|
-
constructor(options = {}) {
|
|
627
|
-
this.id = options.id ?? "secret-provider-manager";
|
|
628
|
-
const initialProviders = options.providers ?? [];
|
|
629
|
-
for (const entry of initialProviders) this.register(entry.provider, { priority: entry.priority });
|
|
630
|
-
}
|
|
631
|
-
register(provider, options = {}) {
|
|
632
|
-
this.providers.push({
|
|
633
|
-
provider,
|
|
634
|
-
priority: options.priority ?? 0,
|
|
635
|
-
order: this.registrationCounter++
|
|
636
|
-
});
|
|
637
|
-
this.providers.sort((a, b) => {
|
|
638
|
-
if (a.priority !== b.priority) return b.priority - a.priority;
|
|
639
|
-
return a.order - b.order;
|
|
640
|
-
});
|
|
641
|
-
return this;
|
|
642
|
-
}
|
|
643
|
-
canHandle(reference) {
|
|
644
|
-
return this.providers.some(({ provider }) => safeCanHandle(provider, reference));
|
|
645
|
-
}
|
|
646
|
-
async getSecret(reference, options) {
|
|
647
|
-
const errors = [];
|
|
648
|
-
for (const { provider } of this.providers) {
|
|
649
|
-
if (!safeCanHandle(provider, reference)) continue;
|
|
650
|
-
try {
|
|
651
|
-
return await provider.getSecret(reference, options);
|
|
652
|
-
} catch (error) {
|
|
653
|
-
if (error instanceof SecretProviderError) {
|
|
654
|
-
errors.push(error);
|
|
655
|
-
if (error.code !== "NOT_FOUND") break;
|
|
656
|
-
continue;
|
|
657
|
-
}
|
|
658
|
-
throw error;
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
throw this.composeError("getSecret", reference, errors, options?.version);
|
|
662
|
-
}
|
|
663
|
-
async setSecret(reference, payload) {
|
|
664
|
-
return this.delegateToFirst("setSecret", reference, (provider) => provider.setSecret(reference, payload));
|
|
665
|
-
}
|
|
666
|
-
async rotateSecret(reference, payload) {
|
|
667
|
-
return this.delegateToFirst("rotateSecret", reference, (provider) => provider.rotateSecret(reference, payload));
|
|
668
|
-
}
|
|
669
|
-
async deleteSecret(reference) {
|
|
670
|
-
await this.delegateToFirst("deleteSecret", reference, (provider) => provider.deleteSecret(reference));
|
|
671
|
-
}
|
|
672
|
-
async delegateToFirst(operation, reference, invoker) {
|
|
673
|
-
const errors = [];
|
|
674
|
-
for (const { provider } of this.providers) {
|
|
675
|
-
if (!safeCanHandle(provider, reference)) continue;
|
|
676
|
-
try {
|
|
677
|
-
return await invoker(provider);
|
|
678
|
-
} catch (error) {
|
|
679
|
-
if (error instanceof SecretProviderError) {
|
|
680
|
-
errors.push(error);
|
|
681
|
-
continue;
|
|
682
|
-
}
|
|
683
|
-
throw error;
|
|
684
|
-
}
|
|
685
|
-
}
|
|
686
|
-
throw this.composeError(operation, reference, errors);
|
|
687
|
-
}
|
|
688
|
-
composeError(operation, reference, errors, version) {
|
|
689
|
-
if (errors.length === 1) {
|
|
690
|
-
const [singleError] = errors;
|
|
691
|
-
if (singleError) return singleError;
|
|
692
|
-
}
|
|
693
|
-
const messageParts = [`No registered secret provider could ${operation}`, `reference "${reference}"`];
|
|
694
|
-
if (version) messageParts.push(`(version: ${version})`);
|
|
695
|
-
if (errors.length > 1) messageParts.push(`Attempts: ${errors.map((error) => `${error.provider}:${error.code}`).join(", ")}`);
|
|
696
|
-
return new SecretProviderError({
|
|
697
|
-
message: messageParts.join(" "),
|
|
698
|
-
provider: this.id,
|
|
699
|
-
reference,
|
|
700
|
-
code: errors.length > 0 ? errors[errors.length - 1].code : "UNKNOWN",
|
|
701
|
-
cause: errors
|
|
702
|
-
});
|
|
703
|
-
}
|
|
704
|
-
};
|
|
705
|
-
function safeCanHandle(provider, reference) {
|
|
706
|
-
try {
|
|
707
|
-
return provider.canHandle(reference);
|
|
708
|
-
} catch {
|
|
709
|
-
return false;
|
|
710
|
-
}
|
|
711
|
-
}
|
|
712
|
-
|
|
713
|
-
//#endregion
|
|
714
|
-
export { EnvSecretProvider, GcpSecretManagerProvider, IntegrationCallGuard, IntegrationHealthService, SecretProviderError, SecretProviderManager, connectionStatusLabel, ensureConnectionReady, normalizeSecretPayload, parseSecretUri };
|