Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ssh tunnel functionality #344

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,20 @@ Edit the migrate-mongo-config.js file. An object or promise can be returned. Mak
// In this file you can configure migrate-mongo

module.exports = {
// configure ssh tunnel, see https://github.com/agebrock/tunnel-ssh#readme
sshTunnel: {
// username:'root',
// password:'secret',
// host:sshServer,
// port:22,
// dstHost:destinationServer,
// dstPort:27017,
// privateKey:require(fs).readFileSync('/path/to/key'),
// passphrase:'secret',
// localHost:'127.0.0.1',
// localPort: 27000
},

mongodb: {
// TODO Change (or review) the url to your MongoDB:
url: "mongodb://localhost:27017",
Expand All @@ -65,7 +79,8 @@ module.exports = {
databaseName: "YOURDATABASENAME",

options: {
useNewUrlParser: true // removes a deprecation warning when connecting
useNewUrlParser: true, // removes a deprecation warning when connecting
useUnifiedTopology: true, // removes a deprecating warning when connecting
// connectTimeoutMS: 3600000, // increase connection timeout to 1 hour
// socketTimeoutMS: 3600000, // increase socket timeout to 1 hour
}
Expand All @@ -78,7 +93,7 @@ module.exports = {
changelogCollectionName: "changelog",

// The file extension to create migrations and search for in migration dir
migrationFileExtension: ".js"
migrationFileExtension: ".js",

// Enable the algorithm to create a checksum of the file contents and use that in the comparison to determin
// if the file should be run. Requires that scripts are coded to be run multiple times.
Expand Down Expand Up @@ -323,6 +338,9 @@ Now the status will also include the file hash in the output

```

### Connecting to your database via SSH tunnel
If your database resides in a secured VPN or is privately accessible through an SSH tunnel, you can add configurations for your ssh tunnel to the `sshTunnel` option in the config file. If this option is defined, your migration scripts will automatically be run via the SSH tunnel. This feature uses the [tunnel-ssh](https://github.com/agebrock/tunnel-ssh#readme) package to wrap database connections, and borrows the same configuration options, therefore the docs for the repo are a good place to look for further clarifications for this functionality.

### Version
To know which version of migrate-mongo you're running, just pass the `version` option:

Expand Down
26 changes: 21 additions & 5 deletions lib/env/database.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { MongoClient } = require("mongodb");
const _ = require("lodash");
const tunnel = require("tunnel-ssh");
const config = require("./config");

module.exports = {
Expand All @@ -8,21 +9,36 @@ module.exports = {
const url = _.get(configContent, "mongodb.url");
const databaseName = _.get(configContent, "mongodb.databaseName");
const options = _.get(configContent, "mongodb.options");
const sshOptions = _.get(configContent, "sshTunnel");

if (!url) {
throw new Error("No `url` defined in config file!");
}

const client = await MongoClient.connect(
url,
options
);
if (sshOptions)
return new Promise((resolve) => {
tunnel(sshOptions, async (err) => {
if (err) throw err;
console.log("tunnel connected...");

const client = await MongoClient.connect(url, options);

const db = client.db(databaseName);
db.close = client.close;
resolve({
client,
db,
});
});
});

const client = await MongoClient.connect(url, options);

const db = client.db(databaseName);
db.close = client.close;
return {
client,
db,
};
}
},
};
124 changes: 117 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"fs-extra": "^9.1.0",
"lodash": "^4.17.21",
"mongodb": "^3.6.4",
"p-each-series": "^2.2.0"
"p-each-series": "^2.2.0",
"tunnel-ssh": "^4.1.4"
},
"devDependencies": {
"chai": "^4.3.3",
Expand Down
14 changes: 14 additions & 0 deletions samples/migrate-mongo-config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
// In this file you can configure migrate-mongo

const config = {
// configure ssh tunnel, see https://github.com/agebrock/tunnel-ssh#readme
sshTunnel: {
// username:'root',
// password:'secret',
// host:sshServer,
// port:22,
// dstHost:destinationServer,
// dstPort:27017,
// privateKey:require(fs).readFileSync('/path/to/key'),
// passphrase:'secret',
// localHost:'127.0.0.1',
// localPort: 27000
},

mongodb: {
// TODO Change (or review) the url to your MongoDB:
url: "mongodb://localhost:27017",
Expand Down