Connect to MongoDB in EC2 from .Net application (With Test Case for Debugging)

721 views
Skip to first unread message

Ahmed Haque

unread,
Dec 4, 2014, 12:46:53 AM12/4/14
to mongodb...@googlegroups.com
Hey guys,

I know I've been posting a lot about this issue,  but here is one more thread I'd like to put out there.

As of now, I don't there isn't a clean way to have a local .Net application communicate to a MongoDB instance in an EC2 server -- because there is no way to relay SSH Authentication keys in the current c# driver.

This is a non-insignificant issue, because I am sure that many up and coming users of MongoDB will naturally think to deploy their Mongo Databases in the cloud using EC2. Being able to insert and obtain documents in local C# applications will be an apparent need then.

...

If anyone has any ideas on how to work around this, I'd love to hear it.

What I've done is created a dummy instance of MongoDB in an EC2 cloud for which I'm attaching the username / password / hostname / port -- so if anyone else wants to help create a solution they can. 


Test Case:

1. Connect to the EC2 instance MongoDB using PuTTy to insert a record through the resulting SSH shell.


Port:22

SSH Authentication Private Key File: Use Attached "AhmedsSensoKey.ppk"

Your PuTTY screens should look something like this:

 


2. Once the SSH Shell opens you can use the below parameters to work within the db. 

Username: devUser

Password: test

Db: testDb

Collection: testCollection

From here create a record of any sort inside the given Db and Collection. Your SSH shell should look something like this. 




Our/Your Challenge: 

Perform the same process in .Net / C# using the C# Driver (or any process you'd recommend). 

...

My attempt at the code looked like the below. The issue I ran into, however, was the error: 

"Additional information: Unable to connect to server bit...@ec2-54-85-204-7.compute-1.amazonaws.com:22: A non-recoverable error occurred during a database lookup."

I believe this error is due to the fact that we aren't incorporating the necessary SSH authentication key. But maybe I am wrong.

                // Create a metadataObj for upload.
                MetadataTemplate metaUpload = createMetadataObj();

                // Create Secure SSH Connection
                var keyFile = new PrivateKeyFile(@"C:\Users\ahaque89\Documents\sensodx_awsuploader\AmazonUploader\Resources\AhmedsSensoKeyOpenSSH.ppk");
                var keyFiles = new[] { keyFile };
                var username = "bitnami";
                var methods = new List<AuthenticationMethod>();
                methods.Add(new PrivateKeyAuthenticationMethod(username, keyFiles));
                var con = new ConnectionInfo(@"ec2-54-85-204-7.compute-1.amazonaws.com", 22, username, methods.ToArray());

                // Connect through SSH Client
                using (var client = new SshClient(con))
                {
                    client.Connect();
                    var cmd = client.RunCommand("ls");
                    var output = cmd.Result;
                    MessageBox.Show(output.ToString()); // This worked proving my SSH Client is functional

                    // Upload to MongoDB (ec2 server)
                    var connectionString = "mongodb://devUser:test@bitnami@ec2-54-85-204-7.compute-1.amazonaws.com:22";
                    var mongoEC2 = new MongoClient(connectionString);
                    var server = mongoEC2.GetServer();
                    var database = server.GetDatabase("testDb");
                    var collection = database.GetCollection<MetadataTemplate>("testCollection"); 

                    // Insert Record into DB
                    collection.Insert(metaUpload); // Program triggers error here.
                }

.........................................

Let me know what you guys come up with! 

Seriously, any help here would be ultra-helpful

I don't wan't to have to build a completely separate web application simply to insert my .Net generated JSON files into my database.

- Ahmed 


AhmedsSensoKey.ppk

Ahmed Haque

unread,
Dec 4, 2014, 12:50:26 AM12/4/14
to mongodb...@googlegroups.com
I should have said:

As of now, I don't there is a clean way to have a local .Net application communicate to a MongoDB instance in an EC2 server -- because there is no way to relay SSH Authentication keys in the current c# driver.

As in, right now I don't think there's a way to handle this issue.

On Wednesday, December 3, 2014 11:46:53 PM UTC-6, Ahmed Haque wrote:
Hey guys,

I know I've been posting a lot about this issue,  but here is one more thread I'd like to put out there.

As of now, I don't there isn't a clean way to have a local .Net application communicate to a MongoDB instance in an EC2 server -- because there is no way to relay SSH Authentication keys in the current c# driver.

This is a non-insignificant issue, because I am sure that many up and coming users of MongoDB will naturally think to deploy their Mongo Databases in the cloud using EC2. Being able to insert and obtain documents in local C# applications will be an apparent need then.

...

If anyone has any ideas on how to work around this, I'd love to hear it.

What I've done is created a dummy instance of MongoDB in an EC2 cloud for which I'm attaching the username / password / hostname / port -- so if anyone else wants to help create a solution they can. 


Test Case:

1. Connect to the EC2 instance MongoDB using PuTTy to insert a record through the resulting SSH shell.


Port:22

SSH Authentication Private Key File: Use Attached "AhmedsSensoKey.ppk"

Your PuTTY screens should look something like this:

 


2. Once the SSH Shell opens you can use the below parameters to work within the db. 

Username: devUser

Password: test

Db: testDb

Collection: testCollection

From here create a record of any sort inside the given Db and Collection. Your SSH shell should look something like this. 




Our/Your Challenge: 

Perform the same process in .Net / C# using the C# Driver (or any process you'd recommend). 

...

My attempt at the code looked like the below. The issue I ran into, however, was the error: 

"Additional information: Unable to connect to server bitnami@ec2-54-85-204-7.compute-1.amazonaws.com:22: A non-recoverable error occurred during a database lookup."

Craig Wilson

unread,
Dec 4, 2014, 8:59:54 AM12/4/14
to mongodb...@googlegroups.com
Hi Ahmed,

I've commented on CSHARP-768 (https://jira.mongodb.org/browse/CSHARP-768), which is the JIRA ticket for the feature request. As it is a feature request I opened 1.5 years ago and hasn't been implemented, then of course it doesn't work currently. As I mentioned in the ticket, we have an extension point for this exact type of thing, but will not implement the actual SSH client inside the driver due to dependencies. However, I've opened up a ticket (https://jira.mongodb.org/browse/CSHARP-1142) to remember to expose this extension point in the driver so you can create your own SshStreamFactory.

If you'd like to play with building an SshStreamFactory now, you can use the mongodb nuget feed here (https://www.myget.org/gallery/mongodb) and pull down the MongoDB.Driver-Build package.  Currently, you'll have to work directly at the core level until CSHARP-1142 is completed, but you should be able to follow this sample program to get up and running (https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver.Core.TestConsoleApplication/Program.cs).

Hope that helps.


On Wednesday, December 3, 2014 11:50:26 PM UTC-6, Ahmed Haque wrote:
I should have said:

As of now, I don't there is a clean way to have a local .Net application communicate to a MongoDB instance in an EC2 server -- because there is no way to relay SSH Authentication keys in the current c# driver.

As in, right now I don't think there's a way to handle this issue.

On Wednesday, December 3, 2014 11:46:53 PM UTC-6, Ahmed Haque wrote:
Hey guys,

I know I've been posting a lot about this issue,  but here is one more thread I'd like to put out there.

As of now, I don't there isn't a clean way to have a local .Net application communicate to a MongoDB instance in an EC2 server -- because there is no way to relay SSH Authentication keys in the current c# driver.

This is a non-insignificant issue, because I am sure that many up and coming users of MongoDB will naturally think to deploy their Mongo Databases in the cloud using EC2. Being able to insert and obtain documents in local C# applications will be an apparent need then.

...

If anyone has any ideas on how to work around this, I'd love to hear it.

What I've done is created a dummy instance of MongoDB in an EC2 cloud for which I'm attaching the username / password / hostname / port -- so if anyone else wants to help create a solution they can. 


Test Case:

1. Connect to the EC2 instance MongoDB using PuTTy to insert a record through the resulting SSH shell.

Ahmed Haque

unread,
Dec 4, 2014, 10:01:07 AM12/4/14
to mongodb...@googlegroups.com
Thanks a ton Craig! Both for all that you're doing on the driver and for steering me in the right direction.

I just asked because I wanted to make absolutely sure I wasn't missing an obvious workaround. 

- Ahmed 

Ahmet Artan

unread,
Jan 16, 2017, 7:46:42 AM1/16/17
to mongodb-csharp
Hi Ahmed

 To solve this problem ?

4 Aralık 2014 Perşembe 18:01:07 UTC+3 tarihinde Ahmed Haque yazdı:
Reply all
Reply to author
Forward
0 new messages