@wildo-ai/platform-infra-providers 1.1.2

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.
Files changed (62) hide show
  1. package/LICENSE +34 -0
  2. package/dist/esm/aws-s3/aws-s3.infra.module.d.ts +63 -0
  3. package/dist/esm/aws-s3/aws-s3.infra.module.d.ts.map +1 -0
  4. package/dist/esm/aws-s3/aws-s3.infra.module.js +110 -0
  5. package/dist/esm/aws-s3/aws-s3.infra.module.js.map +1 -0
  6. package/dist/esm/clamav-self/clamav-self.infra.module.d.ts +30 -0
  7. package/dist/esm/clamav-self/clamav-self.infra.module.d.ts.map +1 -0
  8. package/dist/esm/clamav-self/clamav-self.infra.module.js +61 -0
  9. package/dist/esm/clamav-self/clamav-self.infra.module.js.map +1 -0
  10. package/dist/esm/gotenberg-self/gotenberg-self.infra.module.d.ts +26 -0
  11. package/dist/esm/gotenberg-self/gotenberg-self.infra.module.d.ts.map +1 -0
  12. package/dist/esm/gotenberg-self/gotenberg-self.infra.module.js +54 -0
  13. package/dist/esm/gotenberg-self/gotenberg-self.infra.module.js.map +1 -0
  14. package/dist/esm/index.d.ts +88 -0
  15. package/dist/esm/index.d.ts.map +1 -0
  16. package/dist/esm/index.js +125 -0
  17. package/dist/esm/index.js.map +1 -0
  18. package/dist/esm/minio-self/minio-self.infra.module.d.ts +64 -0
  19. package/dist/esm/minio-self/minio-self.infra.module.d.ts.map +1 -0
  20. package/dist/esm/minio-self/minio-self.infra.module.js +105 -0
  21. package/dist/esm/minio-self/minio-self.infra.module.js.map +1 -0
  22. package/dist/esm/mongodb-atlas/mongodb-atlas.infra.module.d.ts +52 -0
  23. package/dist/esm/mongodb-atlas/mongodb-atlas.infra.module.d.ts.map +1 -0
  24. package/dist/esm/mongodb-atlas/mongodb-atlas.infra.module.js +201 -0
  25. package/dist/esm/mongodb-atlas/mongodb-atlas.infra.module.js.map +1 -0
  26. package/dist/esm/mongodb-self/mongodb-self.infra.module.d.ts +81 -0
  27. package/dist/esm/mongodb-self/mongodb-self.infra.module.d.ts.map +1 -0
  28. package/dist/esm/mongodb-self/mongodb-self.infra.module.js +311 -0
  29. package/dist/esm/mongodb-self/mongodb-self.infra.module.js.map +1 -0
  30. package/dist/esm/postgres-self/postgres-self.infra.module.d.ts +70 -0
  31. package/dist/esm/postgres-self/postgres-self.infra.module.d.ts.map +1 -0
  32. package/dist/esm/postgres-self/postgres-self.infra.module.js +328 -0
  33. package/dist/esm/postgres-self/postgres-self.infra.module.js.map +1 -0
  34. package/dist/esm/rabbitmq-self/rabbitmq-self.infra.module.d.ts +88 -0
  35. package/dist/esm/rabbitmq-self/rabbitmq-self.infra.module.d.ts.map +1 -0
  36. package/dist/esm/rabbitmq-self/rabbitmq-self.infra.module.js +473 -0
  37. package/dist/esm/rabbitmq-self/rabbitmq-self.infra.module.js.map +1 -0
  38. package/dist/esm/redis-self/redis-self.infra.module.d.ts +89 -0
  39. package/dist/esm/redis-self/redis-self.infra.module.d.ts.map +1 -0
  40. package/dist/esm/redis-self/redis-self.infra.module.js +331 -0
  41. package/dist/esm/redis-self/redis-self.infra.module.js.map +1 -0
  42. package/dist/tsconfig.build.tsbuildinfo +1 -0
  43. package/package.json +52 -0
  44. package/src/aws-s3/__tests__/aws-s3.infra.module.test.ts +67 -0
  45. package/src/aws-s3/aws-s3.infra.module.ts +120 -0
  46. package/src/clamav-self/__tests__/clamav-self.infra.module.test.ts +65 -0
  47. package/src/clamav-self/clamav-self.infra.module.ts +72 -0
  48. package/src/gotenberg-self/__tests__/gotenberg-self.infra.module.test.ts +65 -0
  49. package/src/gotenberg-self/gotenberg-self.infra.module.ts +65 -0
  50. package/src/index.ts +133 -0
  51. package/src/minio-self/__tests__/minio-self.infra.module.test.ts +70 -0
  52. package/src/minio-self/minio-self.infra.module.ts +116 -0
  53. package/src/mongodb-atlas/__tests__/mongodb-atlas.infra.module.test.ts +198 -0
  54. package/src/mongodb-atlas/mongodb-atlas.infra.module.ts +236 -0
  55. package/src/mongodb-self/__tests__/mongodb-self.infra.module.test.ts +419 -0
  56. package/src/mongodb-self/mongodb-self.infra.module.ts +384 -0
  57. package/src/postgres-self/__tests__/postgres-self.infra.module.test.ts +344 -0
  58. package/src/postgres-self/postgres-self.infra.module.ts +377 -0
  59. package/src/rabbitmq-self/__tests__/rabbitmq-self.infra.module.test.ts +499 -0
  60. package/src/rabbitmq-self/rabbitmq-self.infra.module.ts +531 -0
  61. package/src/redis-self/__tests__/redis-self.infra.module.test.ts +341 -0
  62. package/src/redis-self/redis-self.infra.module.ts +372 -0
@@ -0,0 +1,384 @@
1
+ /**
2
+ * `MongodbSelfInfraProviderModule` — fifth concrete `InfraProviderModule`
3
+ * (Slice 4.B.3 of `infra-provider-planner.md`, generalizing the
4
+ * pattern to self-managed MongoDB).
5
+ *
6
+ * Implements the `MONGODB_SELF` `InfraProvider` over the
7
+ * `IMongoSelfAdminClient` transport (admin-side mongoose connection,
8
+ * exposed by `MongoSelfManagerService` after the Slice 4.B.3 5C
9
+ * policy/transport split). Owns ALL per-app naming policy +
10
+ * URL-derivation policy — the per-app username convention
11
+ * (`<applicationNamespace>_user`, mirroring the Atlas variant) and
12
+ * the per-app SRV URL derivation by swapping admin credentials
13
+ * with per-app credentials in the authored
14
+ * `BASE_MONGO_SERVER_URL`-derived admin connect URL.
15
+ *
16
+ * **What this module reads from `AppCredentialMintingContext`**:
17
+ * - `applicationNamespace` — used as the source for the per-app
18
+ * username (legacy convention `${ns}_user`).
19
+ * - `secretInputs['database.mongodb.password']` — orchestrator-
20
+ * supplied password (module is upsert-only, never generator).
21
+ * - `applicationConfigInputs['databaseName']` — required.
22
+ * - `applicationConfigInputs['adminConnectUrl']` — required, the
23
+ * admin-scoped MongoDB connect URL the orchestrator reads from
24
+ * `MONGO_CONNECT_URL` env at app-init time.
25
+ * - `adminClient` — narrowed to `IMongoSelfAdminClient`.
26
+ *
27
+ * **Two audiences this module serves:**
28
+ *
29
+ * 1. **Audience-1 (platform-admin)**: cluster-wide Mongo admin
30
+ * connect URL (`MONGO_CONNECT_URL`). Consumers: `apps-manager`
31
+ * (mints per-app users via `mintAppCredentials`), `companion`
32
+ * (mirrors apps-manager admin role for local dev).
33
+ *
34
+ * 2. **Audience-3 (app-scoped, via `mintAppCredentials`)**:
35
+ * per-customer-app Mongo database user. Result threaded into
36
+ * `Wildo_Application_Secrets.database.mongodb.{username,password}`
37
+ * via `_merge_WildoApplicationSecrets`; the per-app connect URL
38
+ * returned via `runtimeArtifacts.connectionString` for
39
+ * transient app-init operations.
40
+ *
41
+ * **NOT served (D-4)**: audience-2 not applicable.
42
+ */
43
+
44
+ import {
45
+ AppCredentialMintingContext,
46
+ AppCredentialMintingResult,
47
+ IMongoSelfAdminClient,
48
+ InfraCapability,
49
+ InfraPlatformAdminConsumer,
50
+ InfraProvider,
51
+ InfraProviderModule,
52
+ InfraProviderPlanContribution,
53
+ ApplicationRuntimePrincipalKind,
54
+ resolveGrantStorageNames,
55
+ type ApplicationRuntimePrincipal,
56
+ } from '@wildo-ai/platform-config-lib';
57
+
58
+ const MONGODB_USERNAME_PATH = 'database.mongodb.username';
59
+ const MONGODB_PASSWORD_PATH = 'database.mongodb.password';
60
+ /**
61
+ * The paths this module mints PER RUNTIME PRINCIPAL — the single source for both consumers.
62
+ *
63
+ * Read by `InfraProviderModule.perPrincipalSecretBlobPaths` (statically, so apps-manager can strip
64
+ * these from the application blob before delivering a principal's own material) AND spread into
65
+ * `appRuntime.perAppSecretBlobPaths` inside `plan()`. Declaring the list twice is how the delivery
66
+ * path would silently stop covering a path this module still mints.
67
+ */
68
+ const PER_PRINCIPAL_SECRET_BLOB_PATHS: ReadonlyArray<string> = [MONGODB_USERNAME_PATH, MONGODB_PASSWORD_PATH];
69
+
70
+
71
+ const MONGODB_PASSWORD_INPUT_KEY = 'database.mongodb.password';
72
+
73
+ const DATABASE_NAME_INPUT_KEY = 'databaseName';
74
+ const ADMIN_CONNECT_URL_INPUT_KEY = 'adminConnectUrl';
75
+
76
+ const CONNECTION_STRING_ARTIFACT_KEY = 'connectionString';
77
+
78
+ /**
79
+ * Compute the per-app username. Mirrors the Atlas module's
80
+ * convention (`${ns}_user`) so the persistence shape is identical
81
+ * regardless of provider.
82
+ */
83
+ function buildMongodbUsername(applicationNamespace: string): string {
84
+ return `${applicationNamespace}_user`;
85
+ }
86
+
87
+ /**
88
+ * MongoDB privilege ACTIONS, per grant axis.
89
+ *
90
+ * Named here rather than inlined because these ARE the security semantics: what
91
+ * "read" and "write" mean once they reach the database. Mongo's action
92
+ * vocabulary is an external one, so it lives at this adapter — the module that
93
+ * speaks Mongo — and never leaks into the platform's grant model.
94
+ *
95
+ * `find` alone for read: no `listCollections`, no `collStats`, nothing that
96
+ * enumerates. A minion granted read on one resource should not be able to
97
+ * discover what else exists.
98
+ *
99
+ * `insert`/`update`/`remove` for write, and deliberately NOT `find`. The
100
+ * platform-level derivation refuses to widen write into read, and re-adding it
101
+ * here would defeat that decision at the last hop — see
102
+ * `deriveMinionResourceGrants`.
103
+ */
104
+ const MONGODB_READ_ACTIONS: readonly string[] = ['find'];
105
+ const MONGODB_WRITE_ACTIONS: readonly string[] = ['insert', 'update', 'remove'];
106
+
107
+ /** One `createRole`/`updateRole` privilege entry. */
108
+ export interface MongodbCollectionPrivilege {
109
+ readonly resource: { readonly db: string; readonly collection: string };
110
+ readonly actions: readonly string[];
111
+ }
112
+
113
+ /**
114
+ * Turn a minion's grants into collection-scoped MongoDB privileges (D14, R1).
115
+ *
116
+ * ## What this deliberately never produces
117
+ *
118
+ * No `dbAdmin` and no `readWrite` — the two roles the application backend
119
+ * currently holds on the whole database. `dbAdmin` is banned outright by D14;
120
+ * `readWrite` is database-WIDE, so granting it to a minion would make every
121
+ * collection reachable regardless of what `resourceAccess` declared, which is
122
+ * the containment this exists to create.
123
+ *
124
+ * No privilege on the database RESOURCE either (`{ db, collection: '' }`), which
125
+ * is Mongo's way of saying "every collection in this database". Every entry
126
+ * names one collection explicitly.
127
+ *
128
+ * ## Read and write are separate entries on purpose
129
+ *
130
+ * A collection granted both axes yields TWO privilege entries rather than one
131
+ * merged action list. Mongo unions them identically, but the emitted role then
132
+ * reads back as the declaration that produced it — which is what makes an
133
+ * operator inspecting `db.getRole()` able to check the grant against the config
134
+ * without re-deriving it.
135
+ */
136
+ export function buildMinionMongodbPrivileges(input: {
137
+ databaseName: string;
138
+ readCollections: readonly string[];
139
+ writeCollections: readonly string[];
140
+ }): MongodbCollectionPrivilege[] {
141
+ const privileges: MongodbCollectionPrivilege[] = [];
142
+ for (const collection of input.readCollections) {
143
+ privileges.push({ resource: { db: input.databaseName, collection }, actions: [...MONGODB_READ_ACTIONS] });
144
+ }
145
+ for (const collection of input.writeCollections) {
146
+ privileges.push({ resource: { db: input.databaseName, collection }, actions: [...MONGODB_WRITE_ACTIONS] });
147
+ }
148
+ return privileges;
149
+ }
150
+
151
+ /**
152
+ * Derive the per-app connect URL by swapping admin credentials and
153
+ * the database path in the authored `adminConnectUrl`. Mirrors the
154
+ * legacy logic at
155
+ * `wildo-application-lifecycle.service.ts:469-479`:
156
+ *
157
+ * const parsed = new URL(adminConnectUrl);
158
+ * parsed.username = perAppUsername;
159
+ * parsed.password = perAppPassword;
160
+ * parsed.pathname = '/' + databaseName;
161
+ *
162
+ * URL-encoding handled automatically by the WHATWG URL class.
163
+ */
164
+ function buildPerAppConnectUrl(
165
+ adminConnectUrl: string,
166
+ databaseName: string,
167
+ username: string,
168
+ password: string,
169
+ ): string {
170
+ const parsed = new URL(adminConnectUrl);
171
+ parsed.username = encodeURIComponent(username);
172
+ parsed.password = encodeURIComponent(password);
173
+ parsed.pathname = '/' + databaseName;
174
+ return parsed.toString();
175
+ }
176
+
177
+ /**
178
+ * Per-principal Mongo username — the SAME derivation the revoke path must use.
179
+ *
180
+ * The application backend keeps `<namespace>_user` VERBATIM so an existing
181
+ * deployment's user is never orphaned. Third time this shape appears (Redis,
182
+ * RabbitMQ, now Mongo): a revoke deriving it differently deletes the BACKEND's
183
+ * database user while believing it is removing a minion's.
184
+ */
185
+ function buildMongodbPrincipalUsername(
186
+ applicationNamespace: string,
187
+ principal: ApplicationRuntimePrincipal | undefined,
188
+ ): string {
189
+ if (!principal || principal.kind === ApplicationRuntimePrincipalKind.APPLICATION_BACKEND) {
190
+ return buildMongodbUsername(applicationNamespace);
191
+ }
192
+ const sanitizedName = (principal.name ?? '').replace(/[^a-zA-Z0-9]+/g, '_').replace(/^_+|_+$/g, '');
193
+ return `${applicationNamespace}_${principal.kind}_${sanitizedName}_user`;
194
+ }
195
+
196
+ export const MongodbSelfInfraProviderModule: InfraProviderModule = {
197
+ provider: InfraProvider.MONGODB_SELF,
198
+ supportedCapabilities: [InfraCapability.DOCUMENT_DATABASE],
199
+ perPrincipalSecretBlobPaths: PER_PRINCIPAL_SECRET_BLOB_PATHS,
200
+
201
+ plan(): InfraProviderPlanContribution {
202
+ return {
203
+ dockerComposeServices: ['mongodb'],
204
+ k8sResources: ['templates/infra/k8s/internal/mongodb/deployment.yaml.hbs'],
205
+ platformAdmin: {
206
+ envVars: {},
207
+ // The single MONGO_CONNECT_URL secret carries the admin
208
+ // credentials embedded; legacy field name preserved.
209
+ secretEnvVars: ['MONGO_CONNECT_URL'],
210
+ consumers: [
211
+ InfraPlatformAdminConsumer.APPS_MANAGER,
212
+ InfraPlatformAdminConsumer.COMPANION,
213
+ ],
214
+ },
215
+ appRuntime: {
216
+ // Same persistence shape as Atlas — per-app username +
217
+ // password go into secrets; the host/protocol/options come
218
+ // from `_merge_WildoApplicationConfig` which derives them
219
+ // from env-config (BASE_MONGO_SERVER_URL).
220
+ perAppSecretBlobPaths: [...PER_PRINCIPAL_SECRET_BLOB_PATHS],
221
+ },
222
+ };
223
+ },
224
+
225
+ async mintAppCredentials(
226
+ context: AppCredentialMintingContext,
227
+ ): Promise<AppCredentialMintingResult> {
228
+ const adminClient = context.adminClient as IMongoSelfAdminClient;
229
+ const password = context.secretInputs[MONGODB_PASSWORD_INPUT_KEY];
230
+ if (!password) {
231
+ throw new Error(
232
+ `MongodbSelfInfraProviderModule.mintAppCredentials: ` +
233
+ `secretInputs is missing '${MONGODB_PASSWORD_INPUT_KEY}'. ` +
234
+ `The orchestrator MUST supply the Mongo password — modules are upsert-only.`,
235
+ );
236
+ }
237
+ if (!context.applicationNamespace) {
238
+ throw new Error(
239
+ `MongodbSelfInfraProviderModule.mintAppCredentials: ` +
240
+ `applicationNamespace is empty. ` +
241
+ `The orchestrator MUST supply a non-empty namespace.`,
242
+ );
243
+ }
244
+ const databaseName = context.applicationConfigInputs?.[DATABASE_NAME_INPUT_KEY];
245
+ if (!databaseName) {
246
+ throw new Error(
247
+ `MongodbSelfInfraProviderModule.mintAppCredentials: ` +
248
+ `applicationConfigInputs is missing '${DATABASE_NAME_INPUT_KEY}'. ` +
249
+ `The orchestrator MUST supply the per-app database name.`,
250
+ );
251
+ }
252
+ const adminConnectUrl = context.applicationConfigInputs?.[ADMIN_CONNECT_URL_INPUT_KEY];
253
+ if (!adminConnectUrl) {
254
+ throw new Error(
255
+ `MongodbSelfInfraProviderModule.mintAppCredentials: ` +
256
+ `applicationConfigInputs is missing '${ADMIN_CONNECT_URL_INPUT_KEY}'. ` +
257
+ `The orchestrator MUST supply the admin Mongo connect URL.`,
258
+ );
259
+ }
260
+
261
+ const username = buildMongodbPrincipalUsername(context.applicationNamespace, context.principal);
262
+
263
+ if (context.principal && context.principal.kind !== ApplicationRuntimePrincipalKind.APPLICATION_BACKEND) {
264
+ // A named principal gets a COLLECTION-SCOPED user (Step 3 / R1 / D14):
265
+ // one custom role carrying exactly the privileges its `resourceAccess`
266
+ // derived, and nothing inherited.
267
+ //
268
+ // `resourceGrants` is REQUIRED here and its absence is an error rather
269
+ // than a default. Falling back to the app-wide upsert would hand a minion
270
+ // `readWrite` + `dbAdmin` on the whole database — the exact over-grant
271
+ // this path exists to replace, arriving silently through a code path that
272
+ // looks like it is doing the narrow thing.
273
+ const grants = context.resourceGrants;
274
+ if (!grants) {
275
+ throw new Error(
276
+ `MongodbSelfInfraProviderModule.mintAppCredentials: `
277
+ + `principal '${context.principal.kind}:${context.principal.name}' has no resourceGrants. `
278
+ + `Refusing to fall back to the database-wide application user — a minion with no declared `
279
+ + `access must get NO database user at all (D11/R3), which is the orchestrator's decision to make.`,
280
+ );
281
+ }
282
+ await adminClient.upsertCollectionScopedDatabaseUser({
283
+ adminConnectUrl,
284
+ databaseName,
285
+ username,
286
+ password,
287
+ roleName: `${username}_role`,
288
+ // Resource types → collection names HERE, at the adapter that speaks
289
+ // Mongo. The grant contract itself stays in the platform vocabulary.
290
+ privileges: buildMinionMongodbPrivileges({
291
+ databaseName,
292
+ readCollections: resolveGrantStorageNames(grants.read),
293
+ writeCollections: resolveGrantStorageNames(grants.write),
294
+ }),
295
+ });
296
+ } else {
297
+ // createUser-or-updateUser over an admin-scoped mongoose
298
+ // connection — idempotent upsert.
299
+ await adminClient.upsertApplicationDatabaseUser({
300
+ adminConnectUrl,
301
+ databaseName,
302
+ username,
303
+ password,
304
+ });
305
+ }
306
+
307
+ // Derive the per-app SRV URL by swapping credentials + db path
308
+ // in the authored admin URL. The orchestrator uses this for
309
+ // transient app-init seeding; not persisted.
310
+ const connectionString = buildPerAppConnectUrl(
311
+ adminConnectUrl,
312
+ databaseName,
313
+ username,
314
+ password,
315
+ );
316
+
317
+ return {
318
+ secretBlobValues: {
319
+ [MONGODB_USERNAME_PATH]: username,
320
+ [MONGODB_PASSWORD_PATH]: password,
321
+ },
322
+ runtimeArtifacts: {
323
+ [CONNECTION_STRING_ARTIFACT_KEY]: connectionString,
324
+ },
325
+ };
326
+ },
327
+
328
+ /**
329
+ * Tear down the per-app Mongo database user.
330
+ *
331
+ * **Caller status**: no caller wires this in Slices 1–4.B.3.
332
+ * Forward-compat seam.
333
+ */
334
+ async revokeAppCredentials(context: AppCredentialMintingContext): Promise<void> {
335
+ if (!context.applicationNamespace) {
336
+ throw new Error(
337
+ `MongodbSelfInfraProviderModule.revokeAppCredentials: ` +
338
+ `applicationNamespace is empty. ` +
339
+ `The orchestrator MUST supply a non-empty namespace.`,
340
+ );
341
+ }
342
+ const adminConnectUrl = context.applicationConfigInputs?.[ADMIN_CONNECT_URL_INPUT_KEY];
343
+ if (!adminConnectUrl) {
344
+ throw new Error(
345
+ `MongodbSelfInfraProviderModule.revokeAppCredentials: ` +
346
+ `applicationConfigInputs is missing '${ADMIN_CONNECT_URL_INPUT_KEY}'. ` +
347
+ `The orchestrator MUST supply the admin Mongo connect URL.`,
348
+ );
349
+ }
350
+ const adminClient = context.adminClient as IMongoSelfAdminClient;
351
+ // Derived through the SAME function the mint uses. Deriving it independently
352
+ // here — which is what this line did before named principals existed — meant
353
+ // revoking a minion left its credential live AND dropped the APPLICATION
354
+ // BACKEND's database user, because the backend's name is what
355
+ // `buildMongodbUsername` returns for every input.
356
+ const username = buildMongodbPrincipalUsername(context.applicationNamespace, context.principal);
357
+
358
+ if (context.principal && context.principal.kind !== ApplicationRuntimePrincipalKind.APPLICATION_BACKEND) {
359
+ // A scoped principal owns a user AND a custom role. Routing it through the
360
+ // user-only door would leave a privileged role behind that the next mint
361
+ // adopts without anybody declaring it.
362
+ const databaseName = context.applicationConfigInputs?.[DATABASE_NAME_INPUT_KEY];
363
+ if (!databaseName) {
364
+ throw new Error(
365
+ `MongodbSelfInfraProviderModule.revokeAppCredentials: `
366
+ + `applicationConfigInputs is missing '${DATABASE_NAME_INPUT_KEY}' for principal `
367
+ + `'${context.principal.kind}:${context.principal.name}'. The role to drop lives IN that `
368
+ + `database — revoking without it would drop the user and silently orphan its privileges.`,
369
+ );
370
+ }
371
+ await adminClient.dropCollectionScopedDatabaseUser({
372
+ adminConnectUrl,
373
+ databaseName,
374
+ username,
375
+ roleName: `${username}_role`,
376
+ });
377
+ return;
378
+ }
379
+
380
+ await adminClient.deleteApplicationDatabaseUser({ adminConnectUrl, username });
381
+ },
382
+
383
+ // No `mintPlatformServiceCredentials` (D-4).
384
+ };