mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
feat(MongoDB Node): Add support for TLS (#8266)
This commit is contained in:
@@ -8,13 +8,16 @@ import { NodeOperationError } from 'n8n-workflow';
|
||||
|
||||
import get from 'lodash/get';
|
||||
import set from 'lodash/set';
|
||||
import { ObjectId } from 'mongodb';
|
||||
import { MongoClient, ObjectId } from 'mongodb';
|
||||
import type {
|
||||
IMongoCredentials,
|
||||
IMongoCredentialsType,
|
||||
IMongoParametricCredentials,
|
||||
} from './mongoDb.types';
|
||||
|
||||
import { createSecureContext } from 'tls';
|
||||
import { formatPrivateKey } from '../../utils/utilities';
|
||||
|
||||
/**
|
||||
* Standard way of building the MongoDB connection string, unless overridden with a provided string
|
||||
*
|
||||
@@ -140,3 +143,30 @@ export function stringifyObjectIDs(items: IDataObject[]) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function connectMongoClient(connectionString: string, credentials: IDataObject = {}) {
|
||||
let client: MongoClient;
|
||||
|
||||
if (credentials.tls) {
|
||||
const ca = credentials.ca ? formatPrivateKey(credentials.ca as string) : undefined;
|
||||
const cert = credentials.cert ? formatPrivateKey(credentials.cert as string) : undefined;
|
||||
const key = credentials.key ? formatPrivateKey(credentials.key as string) : undefined;
|
||||
const passphrase = (credentials.passphrase as string) || undefined;
|
||||
|
||||
const secureContext = createSecureContext({
|
||||
ca,
|
||||
cert,
|
||||
key,
|
||||
passphrase,
|
||||
});
|
||||
|
||||
client = await MongoClient.connect(connectionString, {
|
||||
tls: true,
|
||||
secureContext,
|
||||
});
|
||||
} else {
|
||||
client = await MongoClient.connect(connectionString);
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user