Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat: dynamic secret api changes for mongodb
=50fcf97a3643
diff --git a/backend/package-lock.json b/backend/package-lock.jsonindex cd521119c4..19f85472a3 100644--- a/backend/package-lock.json+++ b/backend/package-lock.json@@ -61,6 +61,7 @@"ldapjs": "^3.0.7","libsodium-wrappers": "^0.7.13","lodash.isequal": "^4.5.0",+ "mongodb": "^6.8.1","ms": "^2.1.3","mysql2": "^3.9.8","nanoid": "^3.3.4",@@ -4778,6 +4779,14 @@"url": "https://github.com/sponsors/isaacs"}},+ "node_modules/@mongodb-js/saslprep": {+ "version": "1.1.9",+ "resolved": "https://registry.npmjs.org/@mongodb-js/saslprep/-/saslprep-1.1.9.tgz",+ "integrity": "sha512-tVkljjeEaAhCqTzajSdgbQ6gE6f3oneVwa3iXR6csiEwXXOFsiC6Uh9iAjAhXPtqa/XMDHWjjeNH/77m/Yq2dw==",+ "dependencies": {+ "sparse-bitfield": "^3.0.3"+ }+ },"node_modules/@msgpackr-extract/msgpackr-extract-darwin-arm64": {"version": "3.0.2","resolved": "https://registry.npmjs.org/@msgpackr-extract/msgpackr-extract-darwin-arm64/-/msgpackr-extract-darwin-arm64-3.0.2.tgz",@@ -7214,6 +7223,19 @@"integrity": "sha512-WUtIVRUZ9i5dYXefDEAI7sh9/O7jGvHg7Df/5O/gtH3Yabe5odI3UWopVR1qbPXQtvOxWu3mM4XxlYeZtMWF4g==","dev": true},+ "node_modules/@types/webidl-conversions": {+ "version": "7.0.3",+ "resolved": "https://registry.npmjs.org/@types/webidl-conversions/-/webidl-conversions-7.0.3.tgz",+ "integrity": "sha512-CiJJvcRtIgzadHCYXw7dqEnMNRjhGZlYK05Mj9OyktqV8uVT8fD2BFOB7S1uwBE3Kj2Z+4UyPmFw/Ixgw/LAlA=="+ },+ "node_modules/@types/whatwg-url": {+ "version": "11.0.5",+ "resolved": "https://registry.npmjs.org/@types/whatwg-url/-/whatwg-url-11.0.5.tgz",+ "integrity": "sha512-coYR071JRaHa+xoEvvYqvnIHaVqaYrLPbsufM9BF63HkwI5Lgmy2QR8Q5K/lYDYo5AK82wOvSOS0UsLTpTG7uQ==",+ "dependencies": {+ "@types/webidl-conversions": "*"+ }+ },"node_modules/@types/xml-crypto": {"version": "1.4.6","resolved": "https://registry.npmjs.org/@types/xml-crypto/-/xml-crypto-1.4.6.tgz",@@ -8822,6 +8844,14 @@"node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7"}},+ "node_modules/bson": {+ "version": "6.8.0",+ "resolved": "https://registry.npmjs.org/bson/-/bson-6.8.0.tgz",+ "integrity": "sha512-iOJg8pr7wq2tg/zSlCCHMi3hMm5JTOxLTagf3zxhcenHsFp+c6uOs6K7W5UE7A4QIJGtqh/ZovFNMP4mOPJynQ==",+ "engines": {+ "node": ">=16.20.1"+ }+ },"node_modules/btoa-lite": {"version": "1.0.0","resolved": "https://registry.npmjs.org/btoa-lite/-/btoa-lite-1.0.0.tgz",@@ -11174,15 +11204,46 @@"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="},"node_modules/gcp-metadata": {- "version": "6.1.0",- "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-6.1.0.tgz",- "integrity": "sha512-Jh/AIwwgaxan+7ZUUmRLCjtchyDiqh4KjBJ5tW3plBZb5iL/BPcso8A5DlzeD9qlw0duCamnNdpFjxwaT0KyKg==",+ "version": "5.3.0",+ "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-5.3.0.tgz",+ "integrity": "sha512-FNTkdNEnBdlqF2oatizolQqNANMrcqJt6AAYt99B3y1aLLC8Hc5IOBb+ZnnzllodEEf6xMBp6wRcBbc16fa65w==",+ "optional": true,+ "peer": true,"dependencies": {- "gaxios": "^6.0.0",+ "gaxios": "^5.0.0","json-bigint": "^1.0.0"},"engines": {- "node": ">=14"+ "node": ">=12"+ }+ },+ "node_modules/gcp-metadata/node_modules/gaxios": {+ "version": "5.1.3",+ "resolved": "https://registry.npmjs.org/gaxios/-/gaxios-5.1.3.tgz",+ "integrity": "sha512-95hVgBRgEIRQQQHIbnxBXeHbW4TqFk4ZDJW7wmVtvYar72FdhRIo1UGOLS2eRAKCPEdPBWu+M7+A33D9CdX9rA==",+ "optional": true,+ "peer": true,+ "dependencies": {+ "extend": "^3.0.2",+ "https-proxy-agent": "^5.0.0",+ "is-stream": "^2.0.0",+ "node-fetch": "^2.6.9"+ },+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/gcp-metadata/node_modules/is-stream": {+ "version": "2.0.1",+ "resolved": "https://registry.npmjs.org/is-stream/-/is-stream-2.0.1.tgz",+ "integrity": "sha512-hFoiJiTl63nn+kstHGBtewWSKnQLpyb155KHheA1l39uvtO9nWIop1p3udqPcUd/xbF1VLMO4n7OI6p7RbngDg==",+ "optional": true,+ "peer": true,+ "engines": {+ "node": ">=8"+ },+ "funding": {+ "url": "https://github.com/sponsors/sindresorhus"}},"node_modules/generate-function": {@@ -11407,6 +11468,18 @@"node": ">=14"}},+ "node_modules/google-auth-library/node_modules/gcp-metadata": {+ "version": "6.1.0",+ "resolved": "https://registry.npmjs.org/gcp-metadata/-/gcp-metadata-6.1.0.tgz",+ "integrity": "sha512-Jh/AIwwgaxan+7ZUUmRLCjtchyDiqh4KjBJ5tW3plBZb5iL/BPcso8A5DlzeD9qlw0duCamnNdpFjxwaT0KyKg==",+ "dependencies": {+ "gaxios": "^6.0.0",+ "json-bigint": "^1.0.0"+ },+ "engines": {+ "node": ">=14"+ }+ },"node_modules/google-auth-library/node_modules/jwa": {"version": "2.0.0","resolved": "https://registry.npmjs.org/jwa/-/jwa-2.0.0.tgz",@@ -13118,6 +13191,11 @@"node": ">= 0.6"}},+ "node_modules/memory-pager": {+ "version": "1.5.0",+ "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz",+ "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg=="+ },"node_modules/merge-descriptors": {"version": "1.0.1","resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz",@@ -13308,6 +13386,91 @@"obliterator": "^2.0.1"}},+ "node_modules/mongodb": {+ "version": "6.8.1",+ "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-6.8.1.tgz",+ "integrity": "sha512-qsS+gl5EJb+VzJqUjXSZ5Y5rbuM/GZlZUEJ2OIVYP10L9rO9DQ0DGp+ceTzsmoADh6QYMWd9MSdG9IxRyYUkEA==",+ "dependencies": {+ "@mongodb-js/saslprep": "^1.1.5",+ "bson": "^6.7.0",+ "mongodb-connection-string-url": "^3.0.0"+ },+ "engines": {+ "node": ">=16.20.1"+ },+ "peerDependencies": {+ "@aws-sdk/credential-providers": "^3.188.0",+ "@mongodb-js/zstd": "^1.1.0",+ "gcp-metadata": "^5.2.0",+ "kerberos": "^2.0.1",+ "mongodb-client-encryption": ">=6.0.0 <7",+ "snappy": "^7.2.2",+ "socks": "^2.7.1"+ },+ "peerDependenciesMeta": {+ "@aws-sdk/credential-providers": {+ "optional": true+ },+ "@mongodb-js/zstd": {+ "optional": true+ },+ "gcp-metadata": {+ "optional": true+ },+ "kerberos": {+ "optional": true+ },+ "mongodb-client-encryption": {+ "optional": true+ },+ "snappy": {+ "optional": true+ },+ "socks": {+ "optional": true+ }+ }+ },+ "node_modules/mongodb-connection-string-url": {+ "version": "3.0.1",+ "resolved": "https://registry.npmjs.org/mongodb-connection-string-url/-/mongodb-connection-string-url-3.0.1.tgz",+ "integrity": "sha512-XqMGwRX0Lgn05TDB4PyG2h2kKO/FfWJyCzYQbIhXUxz7ETt0I/FqHjUeqj37irJ+Dl1ZtU82uYyj14u2XsZKfg==",+ "dependencies": {+ "@types/whatwg-url": "^11.0.2",+ "whatwg-url": "^13.0.0"+ }+ },+ "node_modules/mongodb-connection-string-url/node_modules/tr46": {+ "version": "4.1.1",+ "resolved": "https://registry.npmjs.org/tr46/-/tr46-4.1.1.tgz",+ "integrity": "sha512-2lv/66T7e5yNyhAAC4NaKe5nVavzuGJQVVtRYLyQ2OI8tsJ61PMLlelehb0wi2Hx6+hT/OJUWZcw8MjlSRnxvw==",+ "dependencies": {+ "punycode": "^2.3.0"+ },+ "engines": {+ "node": ">=14"+ }+ },+ "node_modules/mongodb-connection-string-url/node_modules/webidl-conversions": {+ "version": "7.0.0",+ "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",+ "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==",+ "engines": {+ "node": ">=12"+ }+ },+ "node_modules/mongodb-connection-string-url/node_modules/whatwg-url": {+ "version": "13.0.0",+ "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-13.0.0.tgz",+ "integrity": "sha512-9WWbymnqj57+XEuqADHrCJ2eSXzn8WXIW/YSGaZtb2WKAInQ6CHfaUUcTyyver0p8BDg5StLQq8h1vtZuwmOig==",+ "dependencies": {+ "tr46": "^4.1.1",+ "webidl-conversions": "^7.0.0"+ },+ "engines": {+ "node": ">=16"+ }+ },"node_modules/mri": {"version": "1.1.4","resolved": "https://registry.npmjs.org/mri/-/mri-1.1.4.tgz",@@ -15927,6 +16090,14 @@"node": ">=0.10.0"}},+ "node_modules/sparse-bitfield": {+ "version": "3.0.3",+ "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz",+ "integrity": "sha512-kvzhi7vqKTfkh0PZU+2D2PIllw2ymqJKujUcyPMd9Y75Nv4nPbGJZXNhxsgdQab2BmlDct1YnfQCguEvHr7VsQ==",+ "dependencies": {+ "memory-pager": "^1.0.2"+ }+ },"node_modules/split2": {"version": "4.2.0","resolved": "https://registry.npmjs.org/split2/-/split2-4.2.0.tgz",diff --git a/backend/package.json b/backend/package.jsonindex 91f64c53d3..f89843a00e 100644--- a/backend/package.json+++ b/backend/package.json@@ -158,6 +158,7 @@"ldapjs": "^3.0.7","libsodium-wrappers": "^0.7.13","lodash.isequal": "^4.5.0",+ "mongodb": "^6.8.1","ms": "^2.1.3","mysql2": "^3.9.8","nanoid": "^3.3.4",diff --git a/backend/src/ee/services/dynamic-secret/providers/index.ts b/backend/src/ee/services/dynamic-secret/providers/index.tsindex 23769df324..ad7de408f8 100644--- a/backend/src/ee/services/dynamic-secret/providers/index.ts+++ b/backend/src/ee/services/dynamic-secret/providers/index.ts@@ -4,6 +4,7 @@ import { CassandraProvider } from "./cassandra";import { ElasticSearchDatabaseProvider } from "./elastic-search";import { DynamicSecretProviders } from "./models";import { MongoAtlasProvider } from "./mongo-atlas";+import { MongoDBProvider } from "./mongo-db";import { RedisDatabaseProvider } from "./redis";import { SqlDatabaseProvider } from "./sql-database";@@ -14,5 +15,6 @@ export const buildDynamicSecretProviders = () => ({[DynamicSecretProviders.Redis]: RedisDatabaseProvider(),[DynamicSecretProviders.AwsElastiCache]: AwsElastiCacheDatabaseProvider(),[DynamicSecretProviders.MongoAtlas]: MongoAtlasProvider(),- [DynamicSecretProviders.ElasticSearch]: ElasticSearchDatabaseProvider()+ [DynamicSecretProviders.ElasticSearch]: ElasticSearchDatabaseProvider(),+ [DynamicSecretProviders.MongoDB]: MongoDBProvider()});diff --git a/backend/src/ee/services/dynamic-secret/providers/models.ts b/backend/src/ee/services/dynamic-secret/providers/models.tsindex 06069c981b..ae8f255813 100644--- a/backend/src/ee/services/dynamic-secret/providers/models.ts+++ b/backend/src/ee/services/dynamic-secret/providers/models.ts@@ -17,7 +17,6 @@ export const DynamicSecretRedisDBSchema = z.object({port: z.number(),username: z.string().trim(), // this is often "default".password: z.string().trim().optional(),-creationStatement: z.string().trim(),revocationStatement: z.string().trim(),renewStatement: z.string().trim().optional(),@@ -131,6 +130,22 @@ export const DynamicSecretMongoAtlasSchema = z.object({.array()});+export const DynamicSecretMongoDBSchema = z.object({+ host: z.string().min(1).trim().toLowerCase(),+ port: z.number().optional(),+ username: z.string().min(1).trim(),+ password: z.string().min(1).trim(),+ database: z.string().min(1).trim(),+ ca: z.string().min(1).optional(),+ roles: z+ .string()+ .array()+ .min(1)+ .describe(+ 'Enum: "atlasAdmin" "backup" "clusterMonitor" "dbAdmin" "dbAdminAnyDatabase" "enableSharding" "read" "readAnyDatabase" "readWrite" "readWriteAnyDatabase" "<a custom role name>".Human-readable label that identifies a group of privileges assigned to a database user. This value can either be a built-in role or a custom role.'+ )+});+export enum DynamicSecretProviders {SqlDatabase = "sql-database",Cassandra = "cassandra",@@ -138,7 +153,8 @@ export enum DynamicSecretProviders {Redis = "redis",AwsElastiCache = "aws-elasticache",MongoAtlas = "mongo-db-atlas",- ElasticSearch = "elastic-search"+ ElasticSearch = "elastic-search",+ MongoDB = "mongo-db"}export const DynamicSecretProviderSchema = z.discriminatedUnion("type", [@@ -148,7 +164,8 @@ export const DynamicSecretProviderSchema = z.discriminatedUnion("type", [z.object({ type: z.literal(DynamicSecretProviders.Redis), inputs: DynamicSecretRedisDBSchema }),z.object({ type: z.literal(DynamicSecretProviders.AwsElastiCache), inputs: DynamicSecretAwsElastiCacheSchema }),z.object({ type: z.literal(DynamicSecretProviders.MongoAtlas), inputs: DynamicSecretMongoAtlasSchema }),- z.object({ type: z.literal(DynamicSecretProviders.ElasticSearch), inputs: DynamicSecretElasticSearchSchema })+ z.object({ type: z.literal(DynamicSecretProviders.ElasticSearch), inputs: DynamicSecretElasticSearchSchema }),+ z.object({ type: z.literal(DynamicSecretProviders.MongoDB), inputs: DynamicSecretMongoDBSchema })]);export type TDynamicProviderFns = {diff --git a/backend/src/ee/services/dynamic-secret/providers/mongo-db.ts b/backend/src/ee/services/dynamic-secret/providers/mongo-db.tsnew file mode 100644index 0000000000..ea9430846e--- /dev/null+++ b/backend/src/ee/services/dynamic-secret/providers/mongo-db.ts@@ -0,0 +1,116 @@+import { MongoClient } from "mongodb";+import { customAlphabet } from "nanoid";+import { z } from "zod";++import { getConfig } from "@app/lib/config/env";+import { BadRequestError } from "@app/lib/errors";+import { alphaNumericNanoId } from "@app/lib/nanoid";++import { DynamicSecretMongoDBSchema, TDynamicProviderFns } from "./models";++const generatePassword = (size = 48) => {+ const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~!*$#";+ return customAlphabet(charset, 48)(size);+};++const generateUsername = () => {+ return alphaNumericNanoId(32);+};++export const MongoDBProvider = (): TDynamicProviderFns => {+ const validateProviderInputs = async (inputs: unknown) => {+ const appCfg = getConfig();+ const providerInputs = await DynamicSecretMongoDBSchema.parseAsync(inputs);+ if (+ appCfg.isCloud &&+ // localhost+ // internal ips+ (providerInputs.host === "host.docker.internal" ||+ providerInputs.host.match(/^10\.\d+\.\d+\.\d+/) ||+ providerInputs.host.match(/^192\.168\.\d+\.\d+/))+ )+ throw new BadRequestError({ message: "Invalid db host" });++ if (providerInputs.host === "localhost" || providerInputs.host === "127.0.0.1") {+ throw new BadRequestError({ message: "Invalid db host" });+ }++ return providerInputs;+ };++ const getClient = async (providerInputs: z.infer<typeof DynamicSecretMongoDBSchema>) => {+ const isSrv = !providerInputs.port;+ const uri = isSrv+ ? `mongodb+srv://${providerInputs.host}`+ : `mongodb://${providerInputs.host}:${providerInputs.port}`;++ const client = new MongoClient(uri, {+ auth: {+ username: providerInputs.username,+ password: providerInputs.password+ },+ directConnection: !isSrv,+ ca: providerInputs.ca+ });+ return client;+ };++ const validateConnection = async (inputs: unknown) => {+ const providerInputs = await validateProviderInputs(inputs);+ const client = await getClient(providerInputs);++ const isConnected = await client+ .db(providerInputs.database)+ .command({ ping: 1 })+ .then(() => true);++ await client.close();+ return isConnected;+ };++ const create = async (inputs: unknown) => {+ const providerInputs = await validateProviderInputs(inputs);+ const client = await getClient(providerInputs);++ const username = generateUsername();+ const password = generatePassword();++ const db = client.db(providerInputs.database);++ await db.command({+ createUser: username,+ pwd: password,+ roles: providerInputs.roles+ });+ await client.close();++ return { entityId: username, data: { DB_USERNAME: username, DB_PASSWORD: password } };+ };++ const revoke = async (inputs: unknown, entityId: string) => {+ const providerInputs = await validateProviderInputs(inputs);+ const client = await getClient(providerInputs);++ const username = entityId;++ const db = client.db(providerInputs.database);+ await db.command({+ dropUser: username+ });+ await client.close();++ return { entityId: username };+ };++ const renew = async (_inputs: unknown, entityId: string) => {+ return { entityId };+ };++ return {+ validateProviderInputs,+ validateConnection,+ create,+ revoke,+ renew+ };+};