Connecting from home Windows computer or Windows EC2 server to a Ubuntu MongoDB EC2 server via SSH and port forwarding?

Viewed 114

I have two AWS EC2 instances:

  1. Windows Server which will handle user requests for data.
  2. Ubuntu server with MongoDB database on it to be accessed and manipulated by Windows Server.

I am attempting to write a program in C# .NET that will be able to connect to the Ubuntu server and its MongoDB when run from my local computer or from the Windows Server.

I am using Renci SSH.NET and the standard MongoDB driver. The MongoDB driver operation is explained here. For networking, I found one old guide here which helped me get part way. I have so far:

string SSHServerUserName = "ubuntu";
string SSHServerHost = "x.xx.xxx.xxx"; //public IP of ubuntu server as per AWS
PrivateKeyFile keyFile = new PrivateKeyFile(@"H:\MongoDB.pem", "filePass"); //mongo server AWS keyfile
PrivateKeyAuthenticationMethod authenticationMethod = new PrivateKeyAuthenticationMethod(SSHServerUserName, keyFile);

ConnectionInfo connectionInfo = new ConnectionInfo(SSHServerHost, SSHServerUserName, authenticationMethod); //uses DefaultPort = 22

SshClient sshClient = new SshClient(connectionInfo);
//sshClient.ErrorOccurred += delegate { Debug.WriteLine("SSH ERROR OCCURRED"); };
//sshClient.HostKeyReceived += delegate { Debug.WriteLine("SSH HOST KEY RECEIVED"); };
sshClient.Connect();

if (sshClient.IsConnected) {

    string MongoDBHost = "xx.xx.xx.xx"; // **PRIVATE IP OF UBUNTU AWS EC2? 
    uint MongoDBPort = 27017;

    ForwardedPortLocal forwardedPortLocal = new ForwardedPortLocal("127.0.0.1", 5477, MongoDBHost, MongoDBPort);

    //forwardedPortLocal.Exception += delegate { Debug.WriteLine("FORWARDED PORT LOCAL EXCEPTION"); };
    //forwardedPortLocal.RequestReceived += delegate { Debug.WriteLine("FORWARDED PORT REQUEST RECEIVED"); };

    sshClient.AddForwardedPort(forwardedPortLocal);
    forwardedPortLocal.Start();

    MongoClientSettings mongoSettings = new MongoClientSettings();
    mongoSettings.Server = new MongoServerAddress("localhost", 5477); //IS THIS RIGHT?
    MongoClient mongoClient = new MongoClient(mongoSettings);

    var iMongoDatabase = mongoClient.GetDatabase("test");

    var profiles = iMongoDatabase.GetCollection<BsonDocument>("profiles");

    Debug.WriteLine("GOT THE COLLECTION:"); //debugs out okay...

    var document = new BsonDocument {
        { "name", "userName" },
        { "type", "newUser" },
        { "count", 1 },
        { "info", new BsonDocument
        {
            { "x", 203 },
            { "y", 102 }
        } }
    };

    profiles.InsertOne(document); //reports time out exception if using try/catch

    Debug.Write("ATTEMPTED TO INSERT ONE");
}

I am getting sshClient.IsConnected() as true. However, I do not know if my forwarded port is working correctly or to what extent it is or isn't connecting to the Mongo Database. I can see from MongoDB Compass (GUI database navigator) nothing is being inserted, and from a try catch statement I can receive the exception that the MongoDB driver times out on the InsertOne command.

Questions:

  • How do I test the Forwarded Local Port? I see FORWARDED PORT REQUEST RECEIVED outputs but don't know if they're working.
  • Is the MongoDB server code (IPs, ports, "localhost") correct for this configuration? How do I test to see if it has connected properly to the database?
  • What is going wrong in the above?

This is my first database and EC2 configuration. Thanks for any help.

0 Answers
Related