Skip to content

Commit

Permalink
feat(MongoDB Node): Add support for TLS (#8266)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency committed Jan 10, 2024
1 parent 94c9cd4 commit e796e7f
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 8 deletions.
62 changes: 62 additions & 0 deletions packages/nodes-base/credentials/MongoDb.credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
},
];
}
32 changes: 31 additions & 1 deletion packages/nodes-base/nodes/MongoDb/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}
13 changes: 6 additions & 7 deletions packages/nodes-base/nodes/MongoDb/MongoDb.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();

Expand All @@ -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);

Expand Down

0 comments on commit e796e7f

Please sign in to comment.