how to connect to mongodb server via ssh tunnel with Proxy Jump (Bastion Host)

7.1k views Asked by At

I have an ssh config file like this. I have a proxy jump to host1 from test2.

Host host1
  Hostname xxxxxx.us-east-1.elb.amazonaws.com
  Port 2222
  User xxxx
  IdentityFile ~/.ssh/cert
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  KeepAlive yes
  ServerAliveInterval 30
  ServerAliveCountMax 30

Host test2
  Hostname xx.xxx.xx.xxx
  ProxyCommand  ssh.exe host1  -q -W %h:%p host1
  User ubuntu
  IdentityFile ~/.ssh/cert
  KeepAlive yes
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null
  ServerAliveInterval 30
  ServerAliveCountMax 30

My mongo db host : xxx-nonprod.cluster-xx.us-east-1.docdb.amazonaws.com

I have to Use SSH Tunneling to Mongodb using Host test2, But it uses Proxy Jump using ProxyCommand

I want to connect to mongodb using SSH Tunneling with a Mongo DB Compass and also with node js mongoose.

How Can I connect using the Mongo DB Compass?

enter image description here

Here I don't have an option to enter ProxyCommand details.

How Can I connect using node js?

I am using tunnel-ssh, I have a reference code ,

var config = {
    username:'ubuntu',
    host:'xx.xxx.xx.xxx',
    agent : process.env.SSH_AUTH_SOCK,
    privateKey:require('fs').readFileSync('~/.ssh/cert'),
    port:22,
    dstPort:27017
};

var server = tunnel(config, function (error, server) {
  
});

Here as well how can I enter ProxyCommand details here? Or please suggest any node js package which solves this problem.

1

There are 1 answers

2
Pradeep Charan On BEST ANSWER

I am able to connect to the database now.

  1. Make a tunnel through Bastion to the Database from the terminal.

    ssh -L 27017:{mongodb-host}:27017 host1

  2. I am able to connect to database via the tunnel from localhost in another terminal.

    mongo --host 127.0.0.1:27017 --username {username} --password {password}

So I am able to connect through mongoose too using connection string. mongodb://dbadmin:{username}:{password}@localhost:27017