How to handle db authentication in multi tenant application with separate databases

database-designmultitenancy

In a multi tenant application with separated databases for each tenant, what would be the best way to authenticate connection to each of these separate databases. My goal is to:

  • Main security requirement is to avoid accidental or malicious data access of different tenant
  • Achieve this in MySQL.
  • For failure tolerance, being able to restore from backup is more than enough.

One approach I can think of is store tenant identity and authentication details in a table in a separate database and then fetch the db username and db password for a tenant and then use that to connect to its specific tenant.

I don't feel very secure about this approach as I will be quering to a database which pretty much has authentication of all the tenants and I am picking one from the list. If this was compromised somehow, having different database with individual username and password would not matter at all.

Any suggestions?

Best Answer

Don't put all the authentication data in the shared database.

Instead of storing tenant identity and authentication details in a separate database, you can store identity (username) and redirection details (server/instance.databasename). Then you can handle authentication at the tenant's database.

I'm assuming admins are going to send invitations to their users. All of the account information is predominantly handled in their own database. Just use the identification and redirection database to help in managing duplicate usernames. When a new account is created, you just need to create a record here with the database identify information (This should be part of the create user transaction.). There isn't anything highly sensitive in this shared part of your data. You could probably redirect a tenant to a specific application server as well. Internally, if you move a database (Maybe a huge client gets their own server.), just change the redirect records.

Salesforce.com doesn't allow duplicate user names across their entire system, so you should be able to do it as well.

Related Topic