Restrinct for insert update and delete from a specific table

  • hello experts,

    In my existing database one table we are going to create like Sql_Audit_table.

    I want to restrict each and every one to insert update and delete in this table except "SA".

    how can i implement this ?? please advice.

    as i have seen there are so many logins and domain groups avaialbale in sysadmin role prperties.

    and in database itself many users having dbo and datawriter role assigned.

    please advice how can i accomplish my goal to restrict every one to modify data from that table.

    please advice.

    thanks in advance

  • The simple truth of the matter is that if the users are members of SYSADMIN role, then they own your database. Your organzation doesn't have one or two DBA's, it now has 100 DBAs. They can delete tables, drop tables, add more users, take down the server, or anything else they choose, and there is not a damn thing you can do about it, unless you remove them from the sysadmin role. You should be thakful that they havn't decided to drop all the databases or remove you from the sysadmin role, in which case you'd be out of a job!

    First, remove these users from sysadmin server role and all other server and database roles so that they are reduced down to 'public'. Public is the default role when a user is first added to a database in SQL Server 2005, and at this point they have access to basically nothing.

    Next, create a new 'application role' that will define permission for the user group. Add that role as a member of the builtin database roles 'db_datareader' and 'db_datawriter', which will allow them to read / write any table. Ideally you would only grant the role select, insert, update, etc. permission as needed on specific tables.

    Next, under the 'Securables' tab of the Applicaion Role dialog, DENY all permissions on the audit tables.

    Next, add each user to the application role and you're done. If you need to adjust permissions, then do that within the application role, not at the user level. If different groups of users need different levels of permissions, then add another application role and re-assign users.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • hi Eric,

    thanks for such a good explaination, actually there are already multiple running applications and it is not possible to remove logins from sysadmin role or cant modify any users permission because that may imapct of other areas of application and client will not accept it. requirement is only to restrict everyone except SA to update that audit table data.

  • Zeal-DBA (7/25/2013)


    hi Eric,

    thanks for such a good explaination, actually there are already multiple running applications and it is not possible to remove logins from sysadmin role or cant modify any users permission because that may imapct of other areas of application and client will not accept it. requirement is only to restrict everyone except SA to update that audit table data.

    'SA' is not a role, it is just an account that itself is a member of sysadmin. All members of sysadmin are equal, a sysadmin can't be denied permission. If the client doesn't want to remove users from sysadmin, then the client will just have accept the fact that users can do whatever they want in your database.

    Really, one of the users could drop you from sysadmin role and call himself the new DBA.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • that mean the login id which has sysadmin role, that cant be restricted to access an indivisual table in any way?

  • Sounds like you guys have kind of painted yourselves into a corner. There is a way to do this but it is not exactly best practice. Basically what you are trying to do is make a table readonly unless the current is xxx.

    You could create instead of triggers on this one table and check SUSER_NAME(). If it is not the user that can modify the table you could either raise an error or simply exit.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (7/25/2013)


    Sounds like you guys have kind of painted yourselves into a corner. There is a way to do this but it is not exactly best practice. Basically what you are trying to do is make a table readonly unless the current is xxx.

    You could create instead of triggers on this one table and check SUSER_NAME(). If it is not the user that can modify the table you could either raise an error or simply exit.

    Yeah, recently someone have a presentation at our local mssql user group meeting on how he uses logon triggers, etc. in an attempt to block accounts with sysadmin membershop from harming his production database. It seems to be a daily struggle, and he obviously put a lot of thought and effort into it. The sad thing about it is that one of these rogue sysadmins can simply drop the trigger, if they were intent on doing some damage.

    Really, if the DBA would just drop the accounts from sysadmin role, and add them as db_datareader / db_datawriter, that would cover anything that an application account would need to do. It's not as if a user will call up help desk and complain because they can't change the MAXDOP setting or truncate the transaction log. Even if they did, then that's obviously something that the DBA would want routed through his department anyhow.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • Eric M Russell (7/25/2013)


    Sean Lange (7/25/2013)


    Sounds like you guys have kind of painted yourselves into a corner. There is a way to do this but it is not exactly best practice. Basically what you are trying to do is make a table readonly unless the current is xxx.

    You could create instead of triggers on this one table and check SUSER_NAME(). If it is not the user that can modify the table you could either raise an error or simply exit.

    Yeah, recently someone have a presentation at our local mssql user group meeting on how he uses logon triggers, etc. in an attempt to block accounts with sysadmin membershop from harming his production database. It seems to be a daily struggle, and he obviously put a lot of thought and effort into it. The sad thing about it is that one of these rogue sysadmins can simply drop the trigger, if they were intent on doing some damage.

    Really, if the DBA would just drop the accounts from sysadmin role, and add them as db_datareader / db_datawriter, that would cover anything that an application account would need to do. It's not as if a user will call up help desk and complain because they can't change the MAXDOP setting or truncate the transaction log. Even if they did, then that's obviously something that the DBA would want routed through his department anyhow.

    Agreed. It sounds like security has really gotten away from there here. It is not a difficult thing to manage if you have a plan. Giving sysadmin to applications is definitely not the best approach. It is really difficult to back track in these cases and reel everything back in. It is always met with backlash and complaining. The other side of that is what this person is facing, everybody has full access to everything and now we need to prevent it for some users in some situations. :w00t:

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • This situation smells like Entity Framework. I'll bet it's developers telling management they need sysadmin membership in production or else the application won't work.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • hi Sean,

    please find attached image , as i have written trigger, after executing it prints message what i have mentioned in code but it is allowing to insert record as well. how to restrict

  • another possibility is to create the user with the "right" limited permissions, with the same password as 'sa' currently;

    that should be easy to do , since everyone is logging in as SA, anyway, so everyone knows the password as it is.

    After that is all set up then rename the 'sa' login, and then rename that new user to be 'sa';

    an additional problem of course, if you need to upgrade to 2008, because i seem remember a thread Gail mentioned were in place upgrades fail on renamed sa, i think.

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • Lowell (7/25/2013)


    another possibility is to create the user with the "right" limited permissions, with the same password as 'sa' currently;

    that should be easy to do , since everyone is logging in as SA, anyway, so everyone knows the password as it is.

    After that is all set up then rename the 'sa' login, and then rename that new user to be 'sa';

    an additional problem of course, if you need to upgrade to 2008, because i seem remember a thread Gail mentioned were in place upgrades fail on renamed sa, i think.

    It is possible to drop the 'SA' account and re-create with limited permissions. In the past there have been BI / EF developers who insisted on having the 'SA' password. So, I gave them one, only with nothing but reader/writer access... and never heard a complaint back. Honestly most of these guys don't really know what they're asking for and wouldn't know the difference.

    However, in this specific case, it sounds like the users arn't logging in as 'SA', they are logging in as themselves and have been granted membership in SYSADMIN server role at some point in past.

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho

  • Zeal-DBA (7/25/2013)


    hi Sean,

    please find attached image , as i have written trigger, after executing it prints message what i have mentioned in code but it is allowing to insert record as well. how to restrict

    It would be far better if you could post the code instead of a screen shot. The reason here is because you did not create this as an INSTEAD OF trigger. Replace the word FOR with INSTEAD OF.

    Then you should read up on how triggers work.

    http://msdn.microsoft.com/en-us/library/ms189799.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply