@lssm/integration.runtime 0.0.0-canary-20251213172311
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/README.md +7 -0
- package/dist/index.d.mts +267 -0
- package/dist/index.mjs +714 -0
- package/package.json +61 -0
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { SecretManagerServiceClient, protos } from "@google-cloud/secret-manager";
|
|
2
|
+
import { ResolvedAppConfig, ResolvedIntegration } from "@lssm/lib.contracts/app-config/runtime";
|
|
3
|
+
import { ConnectionStatus, IntegrationConnection, IntegrationConnectionHealth } from "@lssm/lib.contracts/integrations/connection";
|
|
4
|
+
import { IntegrationSpec } from "@lssm/lib.contracts/integrations/spec";
|
|
5
|
+
import { CallOptions } from "google-gax";
|
|
6
|
+
|
|
7
|
+
//#region src/secrets/provider.d.ts
|
|
8
|
+
type SecretReference = string;
|
|
9
|
+
interface SecretValue {
|
|
10
|
+
data: Uint8Array;
|
|
11
|
+
version?: string;
|
|
12
|
+
metadata?: Record<string, string>;
|
|
13
|
+
retrievedAt: Date;
|
|
14
|
+
}
|
|
15
|
+
interface SecretFetchOptions {
|
|
16
|
+
version?: string;
|
|
17
|
+
}
|
|
18
|
+
type SecretPayloadEncoding = 'utf-8' | 'base64' | 'binary';
|
|
19
|
+
interface SecretWritePayload {
|
|
20
|
+
data: string | Uint8Array;
|
|
21
|
+
encoding?: SecretPayloadEncoding;
|
|
22
|
+
contentType?: string;
|
|
23
|
+
labels?: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
interface SecretRotationResult {
|
|
26
|
+
reference: SecretReference;
|
|
27
|
+
version: string;
|
|
28
|
+
}
|
|
29
|
+
interface SecretProvider {
|
|
30
|
+
readonly id: string;
|
|
31
|
+
canHandle(reference: SecretReference): boolean;
|
|
32
|
+
getSecret(reference: SecretReference, options?: SecretFetchOptions): Promise<SecretValue>;
|
|
33
|
+
setSecret(reference: SecretReference, payload: SecretWritePayload): Promise<SecretRotationResult>;
|
|
34
|
+
rotateSecret(reference: SecretReference, payload: SecretWritePayload): Promise<SecretRotationResult>;
|
|
35
|
+
deleteSecret(reference: SecretReference): Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
interface ParsedSecretUri {
|
|
38
|
+
provider: string;
|
|
39
|
+
path: string;
|
|
40
|
+
extras?: Record<string, string>;
|
|
41
|
+
}
|
|
42
|
+
declare class SecretProviderError extends Error {
|
|
43
|
+
readonly provider: string;
|
|
44
|
+
readonly reference: SecretReference;
|
|
45
|
+
readonly code: 'NOT_FOUND' | 'FORBIDDEN' | 'INVALID' | 'UNKNOWN';
|
|
46
|
+
readonly cause?: unknown;
|
|
47
|
+
constructor(params: {
|
|
48
|
+
message: string;
|
|
49
|
+
provider: string;
|
|
50
|
+
reference: SecretReference;
|
|
51
|
+
code?: SecretProviderError['code'];
|
|
52
|
+
cause?: unknown;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
declare function parseSecretUri(reference: SecretReference): ParsedSecretUri;
|
|
56
|
+
declare function normalizeSecretPayload(payload: SecretWritePayload): Uint8Array;
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/runtime.d.ts
|
|
59
|
+
interface IntegrationTraceMetadata {
|
|
60
|
+
blueprintName: string;
|
|
61
|
+
blueprintVersion: number;
|
|
62
|
+
configVersion: number;
|
|
63
|
+
}
|
|
64
|
+
interface IntegrationTelemetryEvent {
|
|
65
|
+
tenantId: string;
|
|
66
|
+
appId: string;
|
|
67
|
+
environment?: string;
|
|
68
|
+
slotId?: string;
|
|
69
|
+
integrationKey: string;
|
|
70
|
+
integrationVersion: number;
|
|
71
|
+
connectionId: string;
|
|
72
|
+
status: 'success' | 'error';
|
|
73
|
+
durationMs?: number;
|
|
74
|
+
errorCode?: string;
|
|
75
|
+
errorMessage?: string;
|
|
76
|
+
occurredAt: Date;
|
|
77
|
+
metadata?: Record<string, string | number | boolean>;
|
|
78
|
+
}
|
|
79
|
+
interface IntegrationTelemetryEmitter {
|
|
80
|
+
record(event: IntegrationTelemetryEvent): Promise<void> | void;
|
|
81
|
+
}
|
|
82
|
+
type IntegrationInvocationStatus = 'success' | 'error';
|
|
83
|
+
interface IntegrationContext {
|
|
84
|
+
tenantId: string;
|
|
85
|
+
appId: string;
|
|
86
|
+
environment?: string;
|
|
87
|
+
slotId?: string;
|
|
88
|
+
spec: IntegrationSpec;
|
|
89
|
+
connection: IntegrationConnection;
|
|
90
|
+
secretProvider: SecretProvider;
|
|
91
|
+
secretReference: string;
|
|
92
|
+
trace: IntegrationTraceMetadata;
|
|
93
|
+
config?: Record<string, unknown>;
|
|
94
|
+
}
|
|
95
|
+
interface IntegrationCallContext {
|
|
96
|
+
tenantId: string;
|
|
97
|
+
appId: string;
|
|
98
|
+
environment?: string;
|
|
99
|
+
blueprintName: string;
|
|
100
|
+
blueprintVersion: number;
|
|
101
|
+
configVersion: number;
|
|
102
|
+
slotId: string;
|
|
103
|
+
operation: string;
|
|
104
|
+
}
|
|
105
|
+
interface IntegrationCallError {
|
|
106
|
+
code: string;
|
|
107
|
+
message: string;
|
|
108
|
+
retryable: boolean;
|
|
109
|
+
cause?: unknown;
|
|
110
|
+
}
|
|
111
|
+
interface IntegrationCallResult<T> {
|
|
112
|
+
success: boolean;
|
|
113
|
+
data?: T;
|
|
114
|
+
error?: IntegrationCallError;
|
|
115
|
+
metadata: {
|
|
116
|
+
latencyMs: number;
|
|
117
|
+
connectionId: string;
|
|
118
|
+
ownershipMode: IntegrationConnection['ownershipMode'];
|
|
119
|
+
attempts: number;
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
interface IntegrationCallGuardOptions {
|
|
123
|
+
telemetry?: IntegrationTelemetryEmitter;
|
|
124
|
+
maxAttempts?: number;
|
|
125
|
+
backoffMs?: number;
|
|
126
|
+
shouldRetry?: (error: unknown, attempt: number) => boolean;
|
|
127
|
+
sleep?: (ms: number) => Promise<void>;
|
|
128
|
+
now?: () => Date;
|
|
129
|
+
}
|
|
130
|
+
declare class IntegrationCallGuard {
|
|
131
|
+
private readonly secretProvider;
|
|
132
|
+
private readonly telemetry?;
|
|
133
|
+
private readonly maxAttempts;
|
|
134
|
+
private readonly backoffMs;
|
|
135
|
+
private readonly shouldRetry;
|
|
136
|
+
private readonly sleep;
|
|
137
|
+
private readonly now;
|
|
138
|
+
constructor(secretProvider: SecretProvider, options?: IntegrationCallGuardOptions);
|
|
139
|
+
executeWithGuards<T>(slotId: string, operation: string, _input: unknown, resolvedConfig: ResolvedAppConfig, executor: (connection: IntegrationConnection, secrets: Record<string, string>) => Promise<T>): Promise<IntegrationCallResult<T>>;
|
|
140
|
+
private findIntegration;
|
|
141
|
+
private fetchSecrets;
|
|
142
|
+
private parseSecret;
|
|
143
|
+
private emitTelemetry;
|
|
144
|
+
private failure;
|
|
145
|
+
private makeContext;
|
|
146
|
+
private errorCodeFor;
|
|
147
|
+
}
|
|
148
|
+
declare function ensureConnectionReady(integration: ResolvedIntegration): void;
|
|
149
|
+
declare function connectionStatusLabel(status: ConnectionStatus): string;
|
|
150
|
+
//#endregion
|
|
151
|
+
//#region src/health.d.ts
|
|
152
|
+
interface IntegrationHealthCheckResult extends IntegrationConnectionHealth {
|
|
153
|
+
metadata?: Record<string, string>;
|
|
154
|
+
}
|
|
155
|
+
type IntegrationHealthCheckExecutor = (context: IntegrationContext) => Promise<void>;
|
|
156
|
+
interface IntegrationHealthServiceOptions {
|
|
157
|
+
telemetry?: IntegrationTelemetryEmitter;
|
|
158
|
+
now?: () => Date;
|
|
159
|
+
}
|
|
160
|
+
declare class IntegrationHealthService {
|
|
161
|
+
private readonly telemetry?;
|
|
162
|
+
private readonly nowFn;
|
|
163
|
+
constructor(options?: IntegrationHealthServiceOptions);
|
|
164
|
+
check(context: IntegrationContext, executor: IntegrationHealthCheckExecutor): Promise<IntegrationHealthCheckResult>;
|
|
165
|
+
private emitTelemetry;
|
|
166
|
+
}
|
|
167
|
+
//#endregion
|
|
168
|
+
//#region src/secrets/gcp-secret-manager.d.ts
|
|
169
|
+
type SecretManagerClient = SecretManagerServiceClient;
|
|
170
|
+
interface GcpSecretManagerProviderOptions {
|
|
171
|
+
projectId?: string;
|
|
172
|
+
client?: SecretManagerClient;
|
|
173
|
+
clientOptions?: ConstructorParameters<typeof SecretManagerServiceClient>[0];
|
|
174
|
+
defaultReplication?: protos.google.cloud.secretmanager.v1.IReplication;
|
|
175
|
+
}
|
|
176
|
+
declare class GcpSecretManagerProvider implements SecretProvider {
|
|
177
|
+
readonly id = "gcp-secret-manager";
|
|
178
|
+
private readonly client;
|
|
179
|
+
private readonly explicitProjectId?;
|
|
180
|
+
private readonly replication;
|
|
181
|
+
constructor(options?: GcpSecretManagerProviderOptions);
|
|
182
|
+
canHandle(reference: SecretReference): boolean;
|
|
183
|
+
getSecret(reference: SecretReference, options?: {
|
|
184
|
+
version?: string;
|
|
185
|
+
}, callOptions?: CallOptions): Promise<SecretValue>;
|
|
186
|
+
setSecret(reference: SecretReference, payload: SecretWritePayload): Promise<SecretRotationResult>;
|
|
187
|
+
rotateSecret(reference: SecretReference, payload: SecretWritePayload): Promise<SecretRotationResult>;
|
|
188
|
+
deleteSecret(reference: SecretReference): Promise<void>;
|
|
189
|
+
private parseReference;
|
|
190
|
+
private buildNames;
|
|
191
|
+
private buildVersionName;
|
|
192
|
+
private ensureSecretExists;
|
|
193
|
+
}
|
|
194
|
+
//#endregion
|
|
195
|
+
//#region src/secrets/env-secret-provider.d.ts
|
|
196
|
+
interface EnvSecretProviderOptions {
|
|
197
|
+
/**
|
|
198
|
+
* Optional map to alias secret references to environment variable names.
|
|
199
|
+
* Useful when referencing secrets from other providers (e.g. gcp://...)
|
|
200
|
+
* while still allowing local overrides.
|
|
201
|
+
*/
|
|
202
|
+
aliases?: Record<string, string>;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Environment-variable backed secret provider. Read-only by design.
|
|
206
|
+
* Allows overriding other secret providers by deriving environment variable
|
|
207
|
+
* names from secret references (or by using explicit aliases).
|
|
208
|
+
*/
|
|
209
|
+
declare class EnvSecretProvider implements SecretProvider {
|
|
210
|
+
readonly id = "env";
|
|
211
|
+
private readonly aliases;
|
|
212
|
+
constructor(options?: EnvSecretProviderOptions);
|
|
213
|
+
canHandle(reference: SecretReference): boolean;
|
|
214
|
+
getSecret(reference: SecretReference): Promise<SecretValue>;
|
|
215
|
+
setSecret(reference: SecretReference, _payload: SecretWritePayload): Promise<SecretRotationResult>;
|
|
216
|
+
rotateSecret(reference: SecretReference, _payload: SecretWritePayload): Promise<SecretRotationResult>;
|
|
217
|
+
deleteSecret(reference: SecretReference): Promise<void>;
|
|
218
|
+
private resolveEnvKey;
|
|
219
|
+
private deriveEnvKey;
|
|
220
|
+
private forbiddenError;
|
|
221
|
+
}
|
|
222
|
+
//#endregion
|
|
223
|
+
//#region src/secrets/manager.d.ts
|
|
224
|
+
interface RegisterOptions {
|
|
225
|
+
/**
|
|
226
|
+
* Larger priority values are attempted first. Defaults to 0.
|
|
227
|
+
*/
|
|
228
|
+
priority?: number;
|
|
229
|
+
}
|
|
230
|
+
interface SecretProviderManagerOptions {
|
|
231
|
+
/**
|
|
232
|
+
* Override manager identifier. Defaults to "secret-provider-manager".
|
|
233
|
+
*/
|
|
234
|
+
id?: string;
|
|
235
|
+
/**
|
|
236
|
+
* Providers to pre-register. They are registered in array order with
|
|
237
|
+
* descending priority (first entry wins ties).
|
|
238
|
+
*/
|
|
239
|
+
providers?: {
|
|
240
|
+
provider: SecretProvider;
|
|
241
|
+
priority?: number;
|
|
242
|
+
}[];
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Composite secret provider that delegates to registered providers.
|
|
246
|
+
* Providers are attempted in order of descending priority, respecting the
|
|
247
|
+
* registration order for ties. This enables privileged overrides (e.g.
|
|
248
|
+
* environment variables) while still supporting durable backends like GCP
|
|
249
|
+
* Secret Manager.
|
|
250
|
+
*/
|
|
251
|
+
declare class SecretProviderManager implements SecretProvider {
|
|
252
|
+
readonly id: string;
|
|
253
|
+
private readonly providers;
|
|
254
|
+
private registrationCounter;
|
|
255
|
+
constructor(options?: SecretProviderManagerOptions);
|
|
256
|
+
register(provider: SecretProvider, options?: RegisterOptions): this;
|
|
257
|
+
canHandle(reference: SecretReference): boolean;
|
|
258
|
+
getSecret(reference: SecretReference, options?: SecretFetchOptions$1): Promise<SecretValue>;
|
|
259
|
+
setSecret(reference: SecretReference, payload: SecretWritePayload): Promise<SecretRotationResult>;
|
|
260
|
+
rotateSecret(reference: SecretReference, payload: SecretWritePayload): Promise<SecretRotationResult>;
|
|
261
|
+
deleteSecret(reference: SecretReference): Promise<void>;
|
|
262
|
+
private delegateToFirst;
|
|
263
|
+
private composeError;
|
|
264
|
+
}
|
|
265
|
+
type SecretFetchOptions$1 = Parameters<SecretProvider['getSecret']>[1];
|
|
266
|
+
//#endregion
|
|
267
|
+
export { EnvSecretProvider, GcpSecretManagerProvider, IntegrationCallContext, IntegrationCallError, IntegrationCallGuard, IntegrationCallGuardOptions, IntegrationCallResult, IntegrationContext, IntegrationHealthCheckExecutor, IntegrationHealthCheckResult, IntegrationHealthService, IntegrationHealthServiceOptions, IntegrationInvocationStatus, IntegrationTelemetryEmitter, IntegrationTelemetryEvent, IntegrationTraceMetadata, ParsedSecretUri, SecretFetchOptions, SecretPayloadEncoding, SecretProvider, SecretProviderError, SecretProviderManager, SecretProviderManagerOptions, SecretReference, SecretRotationResult, SecretValue, SecretWritePayload, connectionStatusLabel, ensureConnectionReady, normalizeSecretPayload, parseSecretUri };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,714 @@
|
|
|
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 };
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@lssm/integration.runtime",
|
|
3
|
+
"version": "0.0.0-canary-20251213172311",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"publish:pkg": "bun publish --tolerate-republish --ignore-scripts --verbose",
|
|
14
|
+
"build": "bun build:bundle && bun build:types",
|
|
15
|
+
"build:bundle": "tsdown",
|
|
16
|
+
"build:types": "tsc --noEmit",
|
|
17
|
+
"dev": "bun build:bundle --watch",
|
|
18
|
+
"clean": "rimraf dist .turbo",
|
|
19
|
+
"lint": "bun lint:fix",
|
|
20
|
+
"lint:fix": "eslint src --fix",
|
|
21
|
+
"lint:check": "eslint src",
|
|
22
|
+
"test": "bun test"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@lssm/lib.contracts": "workspace:*",
|
|
26
|
+
"@lssm/lib.logger": "workspace:*",
|
|
27
|
+
"@google-cloud/secret-manager": "^6.1.1",
|
|
28
|
+
"google-gax": "^5.0.0"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@lssm/tool.tsdown": "workspace:*",
|
|
32
|
+
"@lssm/tool.typescript": "workspace:*",
|
|
33
|
+
"tsdown": "^0.17.0",
|
|
34
|
+
"typescript": "^5.9.3"
|
|
35
|
+
},
|
|
36
|
+
"exports": {
|
|
37
|
+
".": "./src/index.ts",
|
|
38
|
+
"./health": "./src/health.ts",
|
|
39
|
+
"./runtime": "./src/runtime.ts",
|
|
40
|
+
"./secrets": "./src/secrets/index.ts",
|
|
41
|
+
"./secrets/env-secret-provider": "./src/secrets/env-secret-provider.ts",
|
|
42
|
+
"./secrets/gcp-secret-manager": "./src/secrets/gcp-secret-manager.ts",
|
|
43
|
+
"./secrets/manager": "./src/secrets/manager.ts",
|
|
44
|
+
"./secrets/provider": "./src/secrets/provider.ts",
|
|
45
|
+
"./*": "./*"
|
|
46
|
+
},
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public",
|
|
49
|
+
"exports": {
|
|
50
|
+
".": "./dist/index.js",
|
|
51
|
+
"./health": "./dist/health.js",
|
|
52
|
+
"./runtime": "./dist/runtime.js",
|
|
53
|
+
"./secrets": "./dist/secrets/index.js",
|
|
54
|
+
"./secrets/env-secret-provider": "./dist/secrets/env-secret-provider.js",
|
|
55
|
+
"./secrets/gcp-secret-manager": "./dist/secrets/gcp-secret-manager.js",
|
|
56
|
+
"./secrets/manager": "./dist/secrets/manager.js",
|
|
57
|
+
"./secrets/provider": "./dist/secrets/provider.js",
|
|
58
|
+
"./*": "./*"
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|