alabid / flylatex

FlyLatex: A Realtime Collaborative Environment (with a concurrent editor) in node-js.
766 stars 85 forks source link

MongoDB authentication #11

Closed fiesh closed 11 years ago

fiesh commented 11 years ago

flylatex doesn't seem to allow using database authentication, but this is necessary if mongodb is supposed to be used for other projects too

alabid commented 11 years ago

I'd try to fix this soon. Thanks.

cocreature commented 11 years ago

I get an error which is most likely related to this. I used a url like this "mongodb://username:password@localhost:port/flydb". Probably the output helps you to fix this:

   info  - socket.io started
   warn  - error raised: Error: listen EADDRINUSE
Successfully created:  /home/javafant/flylatex/pdfs/

/home/javafant/flylatex/node_modules/connect-mongo/lib/connect-mongo.js:126
        throw new Error('Error connecting to database');
              ^
Error: Error connecting to database
    at module.exports.MongoStore.__proto__ (/home/javafant/flylatex/node_modules/connect-mongo/lib/connect-mongo.js:126:15)
    at Db.open (/home/javafant/flylatex/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:253:16)
    at Server.connect.connectionPool.on.server._serverState (/home/javafant/flylatex/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:408:7)
    at EventEmitter.emit (events.js:126:20)
    at connection.on.connectionStatus (/home/javafant/flylatex/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:93:15)
    at EventEmitter.emit (events.js:99:17)
    at Socket.errorHandler (/home/javafant/flylatex/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:382:10)
    at Socket.EventEmitter.emit (events.js:96:17)
    at Socket._destroy.self.errorEmitted (net.js:329:14)
    at process.startup.processNextTick.process._tickCallback (node.js:244:9)
alabid commented 11 years ago

First, @Javafant this should be in a different issue.

Second, did you run the mongo daemon like this: mongod --dbpath <some-dir> where is some directory to store the mongodb daemon output (like journals, et al)?

cocreature commented 11 years ago

The directory is in ~/mongodb. I looked at the log mongodb gives in the shell:

Wed Mar 27 22:49:27.963 [initandlisten] connection accepted from 127.0.0.1:54645 #25 (2 connections now open)
Wed Mar 27 22:49:27.978 [conn25] end connection 127.0.0.1:54645 (1 connection now open)

It doesn't seem that mongodb is trying to authenticate.

alabid commented 11 years ago

flylatex doesn't need to authenticate to use mongodb. Do you have 2 instances of either mongod or flylatex running? The line warn - error raised: Error: listen EADDRINUSE seems to suggest this.

alabid commented 11 years ago

Ooo... I just saw you had mongodb://username:password@localhost:port/flydb. I never used this. Just try the deafult: localhost:port/flydb.

cocreature commented 11 years ago

I have only one instance running. But it maybe related to port 5000 already being in use by another person as this is a shared host. However I didn't find out where to change the port. I tried the default which resulted in excatly the same error. Also mongodb has to run with --auth (shared host) so I need the password and the username.

alabid commented 11 years ago

I just created a new user in mongodb like: use flydb; db.addUser("dan", "rand").

I then used the url: "mongodb://dan:@localhost/flydb" and started the daemon using mongod --auth and seems to work fine for me. Is this exactly what you did too? Also what version of node/npm do you have?

cocreature commented 11 years ago

After the colon I have the passwort and after localhost a colon and the port. Otherweise this is exactly what I do. I have nodejs 0.8.19 and npm 1.2.10.

cocreature commented 11 years ago

Also I changed the port and the error message about the address being in use disappeared.

alabid commented 11 years ago

Your node version is a little bit old. You should update both to the latest and try again. Let me know what happens.

cocreature commented 11 years ago

I did some debugging now and installed it here on my local machine where I have nodejs v0.10.1 and npm 1.2.15. Surprisingle it ran fine, so I tried it again on my webspace with the default mongodb configuration and it worked fine as well. Then I went further and tried to change the port by appending :portnumber after localhost as it is decribed here. This resulted in the error described above. I can reproduce this here on my local machine, so it is definitly not related to the outdated nodejs version.

alabid commented 11 years ago

OK. Next thing we could try is to see if your mongodb connection port conflicts with any other mongodb instances via the command: lsof -iTCP -sTCP:LISTEN | grep mongo (you might need sudo). Make sure flylatex doesn't connect with the same port as any other mongodb instances.

If this still doesn't work, can you not use any portnumber?

cocreature commented 11 years ago

I thought the default mongodb port would already be in use on the webhost. However it isn't so this is no longer a problem. Now I think I get the same error as @fiesh . I can start flylatex but when I try to access the page I get "500 MongoError: Error: not authorized for query on user-auth.sessions".

alabid commented 11 years ago

I think @fiesh's problem is fixed. He just didn't know how to connect using a username/password which you've sort of figured out.

This error is more mongodb-centric and not flylatex-centric. Maybe the password you entered is incorrect/mistyped.

cocreature commented 11 years ago

I tried entering a false password and it didn't even start so I'm pretty sure my password is correct.

alabid commented 11 years ago

I think you haven't been granted to the db, according to http://www.mongodb.org/display/DOCS/Security+and+Authentication.

Also see http://stackoverflow.com/questions/10508975/cant-use-mongo-based-session-store. Similar problem.

cocreature commented 11 years ago

I tried deleting and creating users on my webhost and locally. However I can't get it to work. I can log in via the mongo shell and authenticate so I have access. Flylatex starts if my login credentials are correct but as soon as I try to access the webpage I get the 500 error. I could imagine that the issue is probably in connect-mongo or mongoose. The error message comes from connect-mongo. It seems to me that it doesn't authenticate properly. Here's the mongod output:

Thu Mar 28 01:12:29.928 [conn6] assertion 16550 not authorized for query on user-auth.sessions ns:user-auth.sessions query:{ _id: "longid" }
Thu Mar 28 01:12:29.928 [conn6]  ntoskip:0 ntoreturn:-1
limolitz commented 10 years ago

I think I ran into the very same problem as I am using the same shared hoster as Javafant.

The good news: I found the issue (and a workaround). The bad news is that I have never worked with node.js before, so all I can give you is this: If you add your config url in the file flylatex/node_modules/connect-mongo/lib/connect-mongo.js as options.url = "mongodb://flylatex:PASSWORD@localhost:PORT/flylatex"; for example at line 43 for my version, that is just before the if (options.url) { , Flylatex works flawlessy with authentication and a non-standard port. The problem is that options.url is empty when this function MongoStore is called. I cannot determine if this is a Flylatex problem or of any of the libraries used, but I suppose for you it is not that hard. If you need any additional information, I'll see if I can help.