feat(MongoDB Node): Add support for TLS (#8266)

This commit is contained in:
Michael Kret 2024-01-10 15:02:05 +02:00 committed by GitHub
parent 94c9cd4080
commit e796e7f06d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 99 additions and 8 deletions

View file

@ -96,5 +96,67 @@ export class MongoDb implements ICredentialType {
},
default: 27017,
},
{
displayName: 'Use TLS',
name: 'tls',
type: 'boolean',
default: false,
},
{
displayName: 'CA Certificate',
name: 'ca',
type: 'string',
typeOptions: {
password: true,
},
displayOptions: {
show: {
tls: [true],
},
},
default: '',
},
{
displayName: 'Public Client Certificate',
name: 'cert',
type: 'string',
typeOptions: {
password: true,
},
displayOptions: {
show: {
tls: [true],
},
},
default: '',
},
{
displayName: 'Private Client Key',
name: 'key',
type: 'string',
typeOptions: {
password: true,
},
displayOptions: {
show: {
tls: [true],
},
},
default: '',
},
{
displayName: 'Passphrase',
name: 'passphrase',
type: 'string',
typeOptions: {
password: true,
},
displayOptions: {
show: {
tls: [true],
},
},
default: '',
},
];
}

View file

@ -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;
}

View file

@ -17,12 +17,13 @@ import type {
UpdateOptions,
Sort,
} from 'mongodb';
import { MongoClient, ObjectId } from 'mongodb';
import { ObjectId } from 'mongodb';
import { generatePairedItemData } from '../../utils/utilities';
import { nodeProperties } from './MongoDbProperties';
import {
buildParameterizedConnString,
connectMongoClient,
prepareFields,
prepareItems,
stringifyObjectIDs,
@ -74,7 +75,7 @@ export class MongoDb implements INodeType {
);
}
const client: MongoClient = await MongoClient.connect(connectionString);
const client = await connectMongoClient(connectionString, credentials);
const { databases } = await client.db().admin().listDatabases();
@ -100,12 +101,10 @@ export class MongoDb implements INodeType {
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const { database, connectionString } = validateAndResolveMongoCredentials(
this,
await this.getCredentials('mongoDb'),
);
const credentials = await this.getCredentials('mongoDb');
const { database, connectionString } = validateAndResolveMongoCredentials(this, credentials);
const client: MongoClient = await MongoClient.connect(connectionString);
const client = await connectMongoClient(connectionString, credentials);
const mdb = client.db(database);