mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-11 12:57:29 -08:00
feat(MongoDB Node): Add support for TLS (#8266)
This commit is contained in:
parent
94c9cd4080
commit
e796e7f06d
|
@ -96,5 +96,67 @@ export class MongoDb implements ICredentialType {
|
||||||
},
|
},
|
||||||
default: 27017,
|
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: '',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,13 +8,16 @@ import { NodeOperationError } from 'n8n-workflow';
|
||||||
|
|
||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
import set from 'lodash/set';
|
import set from 'lodash/set';
|
||||||
import { ObjectId } from 'mongodb';
|
import { MongoClient, ObjectId } from 'mongodb';
|
||||||
import type {
|
import type {
|
||||||
IMongoCredentials,
|
IMongoCredentials,
|
||||||
IMongoCredentialsType,
|
IMongoCredentialsType,
|
||||||
IMongoParametricCredentials,
|
IMongoParametricCredentials,
|
||||||
} from './mongoDb.types';
|
} 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
|
* 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;
|
||||||
|
}
|
||||||
|
|
|
@ -17,12 +17,13 @@ import type {
|
||||||
UpdateOptions,
|
UpdateOptions,
|
||||||
Sort,
|
Sort,
|
||||||
} from 'mongodb';
|
} from 'mongodb';
|
||||||
import { MongoClient, ObjectId } from 'mongodb';
|
import { ObjectId } from 'mongodb';
|
||||||
import { generatePairedItemData } from '../../utils/utilities';
|
import { generatePairedItemData } from '../../utils/utilities';
|
||||||
import { nodeProperties } from './MongoDbProperties';
|
import { nodeProperties } from './MongoDbProperties';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
buildParameterizedConnString,
|
buildParameterizedConnString,
|
||||||
|
connectMongoClient,
|
||||||
prepareFields,
|
prepareFields,
|
||||||
prepareItems,
|
prepareItems,
|
||||||
stringifyObjectIDs,
|
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();
|
const { databases } = await client.db().admin().listDatabases();
|
||||||
|
|
||||||
|
@ -100,12 +101,10 @@ export class MongoDb implements INodeType {
|
||||||
};
|
};
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const { database, connectionString } = validateAndResolveMongoCredentials(
|
const credentials = await this.getCredentials('mongoDb');
|
||||||
this,
|
const { database, connectionString } = validateAndResolveMongoCredentials(this, credentials);
|
||||||
await this.getCredentials('mongoDb'),
|
|
||||||
);
|
|
||||||
|
|
||||||
const client: MongoClient = await MongoClient.connect(connectionString);
|
const client = await connectMongoClient(connectionString, credentials);
|
||||||
|
|
||||||
const mdb = client.db(database);
|
const mdb = client.db(database);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue