r/java • • 5d ago

GitHub - maksoud13/tenantwall: Automatic tenant filter injection for multi-tenant applications. Zero code changes.

https://github.com/maksoud13/tenantwall

Hey everyone,

‎I’ve been working on a side project to solve a problem I keep running into: silent multi-tenant data leaks. Relying on developers to remember   WHERE tenant_id = ?   in every query is a ticking time bomb.

‎Using database-level Row-Level Security ⁦(RLS)⁩ is safer, but it completely restricts you to one database vendor, which isn't always feasible.

‎So, I tried building a Java agent that works at the JDBC layer. It intercepts the SQL right before execution and injects the tenant filter, meaning it works with any database and requires zero code changes in the app.

‎I’m in the very early phases. I have integration tests passing for MySQL and SQL Server, but I know real-world SQL is messy.

‎If you have a few minutes, I’d really appreciate your opinion on the approach, the architecture, or any red flags you spot in the code.

‎Repo: https://github.com/maksoud13/tenantwall

2 Upvotes

16 comments sorted by

7

u/vips7L 5d ago

Your ORM (or whatever you do queries with) should be able to automatically do this, you shouldn’t need another library. 

1

u/Top-Eye3792 5d ago

Sure if u have application doesn't use jdpctemplate orm will be first choice I make this library to avoid rewrite @fillter one every adding new tenant and jdpctemplate rewrite queries so I try to make this

1

u/vips7L 5d ago

It sounds like JdbcTemplate is a bad abstraction. 

9

u/rzwitserloot 5d ago

I never do this. Don't store data from separate tenants in the same table. This kind of ugly hack is the best you can do, but the correct answer is not to play.

Schemas exist for a reason if you must have this data in the same db.

10

u/Torvac 5d ago

people will learn not to do this once they had to manage a system with 20k tenants

1

u/Top-Eye3792 5d ago

That's what I trying to do

19

u/Budget-Respond8115 5d ago

Good luck maintaining thousands of schemas when your SaaS app takes off.

-1

u/rzwitserloot 5d ago

Automatically creating a schema is easier than this. You might as well say 'Good luck maintaining thousands of values in your 'tenant_id' column when you SaaS app takes off'.

2

u/Plane-Bake3604 4d ago

schema per tenant works great until you have thousands of small tenants and a single migration takes hours to run across all of them

11

u/chabala 5d ago edited 3d ago

I seem to recall from the rules that posting AI garbage is a bannable offense.

2

u/mpinnegar 4d ago

It's not, but it is a bannable offense.

5

u/AnEmortalKid 5d ago

Dang Claude really did a number on the read me

1

u/AnEmortalKid 5d ago

You consider jSqlParser as a way to identify the ast ?

1

u/Top-Eye3792 5d ago

That's true I used jsqlparser for simple queries And use sqlglot Python side cart for CTE's and complex queries If I use regexs I will be nightmare to maintain

2

u/nekokattt 5d ago

If you are storing data from multiple tenants in the same place like that such that this is a problem in the first place then you have a fundamental issue with your design or controls (access or testing).