I am securing an ASP.NET/MVC web application running on top of MS SQL. As part of that I have different database connections to perform different things in the website.
EDIT - Of course it is up to the DBA and Ops people to decide how data is accessed and how the app is hosted, but they can't do much with an application that isn't granular enough to afford them the controls they need to lock things down.
The system uses only stored procedures with no dynamic SQL. Some stored procedures mutate data, so I call those using a conceptually different connection than the one used for other data access that only reads. No SQL user has rights to access any other database object than a stored procedure. Account authorization has a separate connection. Some parts of the web application shows billing information, other parts of the website direct billable work. There seems to be ways of putting bulkheads into place that separate conceptually different things.
I am looking for a way to integration test that I am not overly generous with the privileges chosen. I would like a mechanism that throws an exception on the build server if a dev tried to use the wrong access level in the wrong place in the code, even if the mistake is that the chosen account actually has too many privileges, which would give no error from the database.
I have been down the route of creating an implementation of IDbCommand that would act as a factory of IDbCommand instances that would basically issue the same command onto the backend using two different connections using different principals that have different access rights in SQL Server and then throw if the lower privileges would have been sufficient.
I have two problems with this approach
- It is hardly 'elegant' or obviously flawless. I have barely finished thinking about it and it is so much code already. I'd have to queue up all things happening to the lower connection and then replay that on the higher privileged connection which will not be realistic in how it works and may cause weird timing issues with the other code. I like to code, but I also like my tests to be trustworthy and easily understood and without introducing a new set of complexities that make the tests brittle in a different way than the code under test. Maybe if I fully duplicate all the work and all the data and run separate databases, but then the build server scripting will be a nightmare.
- It hardly qualifies as granular.
Surely this is a solved problem that only because of bad luck or bad google fu evaded my several hours of searching?