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

Auth support for useDb() #8116

Closed
vkarpov15 opened this issue Aug 31, 2019 · 10 comments
Closed

Auth support for useDb() #8116

vkarpov15 opened this issue Aug 31, 2019 · 10 comments

Comments

@vkarpov15
Copy link
Collaborator

Re: #2874, useDb() should have the option to provide auth credentials in case the currently logged in user doesn't have access to the new database.

https://mongoosejs.com/docs/api/connection.html#connection_Connection-useDb

@vkarpov15 vkarpov15 added this to the 5.x Unprioritized milestone Aug 31, 2019
@vkarpov15 vkarpov15 modified the milestones: 5.x Unprioritized, 5.11 Aug 16, 2020
@linuscarlstrom
Copy link

We are a handful of developers working on a multi-tenancy solution, and this has quickly become an issue. Each tenant has their own database with a specific user.

Creating new connections or establishing and saving a connection for each request feels expensive. Is there another workaround for this until it's added in the mongoose library?

Thanks in advance!

@vkarpov15
Copy link
Collaborator Author

Creating new connections for each tenant is generally the way to go. Switching on each request using useDb() is not a good way to implement multi-tenant: you may switch databases in the middle of a multi-request operation like populate(). That makes useDb() risky for multi-tenant unless you implement queuing.

@linuscarlstrom
Copy link

Cool cool, we'll keep our approach as is then, thanks for the response 👍

@vkarpov15
Copy link
Collaborator Author

I took a closer look and unfortunately this is not possible. The MongoDB Node.js driver deprecated support for additional authenticate() calls in 2017. So there's no way for Mongoose to support different auth credentials on the same connection pool.

@vkarpov15 vkarpov15 removed this from the 5.11 milestone Nov 6, 2020
@vkarpov15
Copy link
Collaborator Author

In order to connect to different databases with different authentication requirements, you need to use multiple connections.

@darinrogers
Copy link

@vkarpov15 What if you connect to one database with one set of credentials, but then try to useDb to another database which happens to have a user with same credentials? I believe that even with the same credentials, they are different users. At the command line, I need to call the auth method. But it seems like there's no way to do this from Mongoose? Is a multi-tenant architecture only possible if you don't use authentication?

@vkarpov15
Copy link
Collaborator Author

@darinrogers Mongoose's docs on multi-tenant connections describe 2 patterns for multi tenancy: 1) shared connection pool using useDb(), 2) separate connection pool per tenant. Pattern (1) is not possible when you need to authenticate as different users for different databases, because the MongoDB server does not support authenticating as multiple users on the same connection pool. But there's no problem with using pattern (2).

@darinrogers
Copy link

@vkarpov15 The problem we have is we've got many services, each of which talks to many (and an increasing number of) tenants. And our cluster only allows so many connections, so we quickly run out. I think our long-term solution will be an architectural change, where all or most tenant data goes through one or a few services. But that's beyond the scope of this thread. I was just curious if there were alternatives to username/password that would work with useDb, but it sounds like not. It sounds like any authentication switches are disallowed.

@vkarpov15
Copy link
Collaborator Author

Yeah, unfortunately there's no way to make useDb() work with multiple authentications, and there's nothing Mongoose can do about it unless the MongoDB server adds support for multiple auth on the same connection.

One potential alternative is to cap the number of sockets per connection: the MongoDB Node driver allows up to 100 sockets per connection by default. You can reduce that with the maxPoolSize option. Would that help?

@darinrogers
Copy link

That's what we're doing and it does help. It prevents us from hitting a connection limit. But now, each service/tenant has a small number of sockets per connection and it's noticeably slowing things down. And this doesn't scale well. So I think an architectural change is in order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants