<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Search results matching tags 'T-SQL', 'good practices', 'tips and tricks', and 'SQL Server 2008'</title><link>http://sqlblog.com/search/SearchResults.aspx?o=DateDescending&amp;tag=T-SQL,good+practices,tips+and+tricks,SQL+Server+2008&amp;orTags=0</link><description>Search results matching tags 'T-SQL', 'good practices', 'tips and tricks', and 'SQL Server 2008'</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Server level permissions for developers–why you should read books</title><link>http://sqlblog.com/blogs/piotr_rodak/archive/2012/01/01/server-level-permissions-for-developers-why-you-should-read-books.aspx</link><pubDate>Mon, 02 Jan 2012 03:05:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:40715</guid><dc:creator>rodak.p@gmail.com</dc:creator><description>&lt;p&gt;It is quite difficult recently for me to find some time to write a new post, so I don’t seem to be leading the rankings of the most frequent bloggers. I rarely recently have opportunity to lay my hands on the code too, so natural sources of ‘inspiration’ are less often. Hopefully this will change in 2012 and I will have more opportunities to write something useful.&lt;/p&gt;  &lt;p&gt;In a big enterprise the roles of server users are usually more distinguished than in smaller companies. You have DBAs, developers and business users. Each of these roles has generally different goals and different rights when it comes to connecting to the database servers. Large organizations tend to implement service oriented approach to processes, which means that various teams are responsible for executing various tasks ordered by other teams. In the case of developers and DBAs, the latter are responsible for all maintenance and management tasks on servers which are ‘owned’ by developers. Such organisation of work often results in sealing off developers from the maintenance related activities, such as creating databases or snapshots, running traces, viewing server state and troubleshooting blocks and deadlocks. This approach unfortunately, while imposes more order to the server utilization and prevents accidental failures, tends to slow down the development process, sometimes preventing any work from being done by significant number of developers for time anything from few minutes to few hours. This is a waste of time and money of course. In my case, development team wanted to have right to kill user sessions on development servers. You know, sometimes there is a process that takes a lot of time and locks, or there is an open transaction keeping schema locks all over the place and and the owner of it is gone for the day without committing it.&lt;/p&gt;  &lt;p&gt;Problem is, that to be able to kill a session, you have to be a sysadmin, processadmin, or have ALTER ANY CONNECTION right granted to you at server level. Obviously, in certain environments these permissions are not the ones DBAs want to give to &lt;strike&gt;random people&lt;/strike&gt; developers. If you GRANT ALTER ANY CONNECTION to a login, this login can kill some important sessions, not only the runaway ones. It may break audit, scheduled maintenance, backups, other teams ETL processes – just imagine your mailbox – it ain’t gonna look pretty. It would be much better to be able to grant login right to kill only spids belonging to certain users or working on certain databases. How can you eat the cake and have the cake?&lt;/p&gt;  &lt;p&gt;I remembered that I had read about this idea in one of the books, namely &lt;a target="_blank" href="http://www.amazon.com/exec/obidos/ASIN/159059729X/ref=nosim/apre-20"&gt;Expert SQL Server 2005 Development&lt;/a&gt; by &lt;a target="_blank" href="http://sqlblog.com/blogs/adam_machanic/"&gt;Adam Machanic&lt;/a&gt;, &lt;a target="_blank" href="http://sqlblog.com/blogs/lara_rubbelke/"&gt;Lara Rubbelke&lt;/a&gt; and &lt;a target="_blank" href="http://sqlblog.com/blogs/hugo_kornelis/"&gt;Hugo Kornelis&lt;/a&gt;. This is an excellent book, you should really read it if you haven’t yet done so. There is a chapter in the book, discussing the security features introduced in SQL Server 2005. These features allow architects to design more versatile and complex applications without jeopardizing security requirements. As you know, the security model in SQL Server 2005 has been completely revamped, for example the notion of the &lt;b&gt;&lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms190387%28v=SQL.90%29.aspx"&gt;schema&lt;/a&gt;&lt;/b&gt; was separated from the ownership and database user. There are more features, one of most important probably is the ability to sign stored procedures with certificates and to create logins and users from the certificates. The discussion about security architecture and certificates is huge and going beyond scope of this post. One of the starting points you can check out is the MSDN article "&lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/bb283235.aspx"&gt;Securing SQL Server&lt;/a&gt;”.&lt;/p&gt;  &lt;p&gt;In short, if you were to grant server wide elevated rights to certain groups of users, how would you do it? Here’s a little shopping list:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Create a certificate &lt;/li&gt;    &lt;li&gt;Create a login &lt;b&gt;from the certificate&lt;/b&gt; &lt;/li&gt;    &lt;li&gt;Grant ALTER ANY CONNECTION to the login &lt;/li&gt;    &lt;li&gt;If you want to have the procedure in database other than master, you must copy the certificate to this database. You do it with backup-restore technique.&lt;/li&gt;    &lt;li&gt;Create a user for the login &lt;/li&gt;    &lt;li&gt;Create stored procedure that will issue KILL command &lt;/li&gt;    &lt;li&gt;Sign the procedure with the certificate &lt;/li&gt;    &lt;li&gt;Grant EXECUTE on the procedure to developers. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;Here are some highlights of the solution. I attach the testing code at the end of the post as usual.&lt;/p&gt;  &lt;p&gt;Sample code for creating certificate and the login is as follows.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:f93dc5b1-ded6-4fc1-9dd3-f16da8d75066" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---create certificate that the procedure will be signed with&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;certificate&lt;/span&gt; kill_session_certificate&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;encryption&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;password&lt;/span&gt; &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'1410SomeReallyStrongPassword2011!'&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;subject&lt;/span&gt; &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'Enable KILL through procedure'&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;---create login that will be granted right to kill sessions&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;login&lt;/span&gt; kill_login &lt;span style="color:#0000ff;"&gt;from&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;certificate&lt;/span&gt; kill_session_certificate&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;go&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#008000;"&gt;--licence to kill&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;grant&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;ALTER&lt;/span&gt; &lt;span style="color:#808080;"&gt;ANY&lt;/span&gt; CONNECTION &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; kill_login&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;This is the stored procedure developers can call to kill sessions.&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:410bd5b7-2e04-4537-b97b-1b3c519ad81b" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:400px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2.5em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;create&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;procedure&lt;/span&gt; killsession&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@sessionid &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;as&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;begin&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#008000;"&gt;/* Piotr Rodak: procedure calls KILL command. Must be signed by certificate to work.*/&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;print&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'killsession: executing as '&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;suser_name&lt;/span&gt;&lt;span style="color:#808080;"&gt;()&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;declare&lt;/span&gt; @command &lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;300&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;set&lt;/span&gt; @command &lt;span style="color:#808080;"&gt;=&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'kill '&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @sessionid&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;print&lt;/span&gt; @command&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#008000;"&gt;---you can filter only sessions running on certain databases here.. &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;exec&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;@command&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/li&gt; &lt;li&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#008000;"&gt;---you can add logging here.. &lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;span style="color:#0000ff;"&gt;print&lt;/span&gt; &lt;span style="color:#ff0000;"&gt;'killed session '&lt;/span&gt; &lt;span style="color:#808080;"&gt;+&lt;/span&gt; &lt;span style="color:#ff00ff;"&gt;convert&lt;/span&gt;&lt;span style="color:#808080;"&gt;(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;varchar&lt;/span&gt;&lt;span style="color:#808080;"&gt;,&lt;/span&gt; @sessionid&lt;span style="color:#808080;"&gt;)&lt;/span&gt;&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;end&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;You can create the procedure in any database, but if you choose a database different than master you have to copy the certificate from master to the target database.&lt;/p&gt;  &lt;p&gt;And here’s code to sign the procedure:&lt;/p&gt;  &lt;div style="padding-bottom:0px;margin:0px;padding-left:0px;padding-right:0px;display:inline;float:none;padding-top:0px;" id="scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:56e9ab18-9eb8-4807-8ff2-563406307332" class="wlWriterEditableSmartContent"&gt; &lt;div style="border:#000080 1px solid;color:#000;font-family:'Courier New', Courier, Monospace;font-size:10pt;"&gt; &lt;div style="background:#ddd;max-height:500px;overflow:auto;"&gt; &lt;ol style="background:#ffffff;margin:0 0 0 2em;padding:0 0 0 5px;"&gt; &lt;li&gt;&lt;span style="color:#008000;"&gt;---sign the procedure with the certificate&lt;/span&gt;&lt;/li&gt; &lt;li style="background:#f3f3f3;"&gt;&lt;span style="color:#0000ff;"&gt;add&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;signature&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;to&lt;/span&gt; killsession &lt;span style="color:#0000ff;"&gt;by&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;certificate&lt;/span&gt; kill_session_certificate&lt;/li&gt; &lt;li&gt;&lt;span style="color:#0000ff;"&gt;with&lt;/span&gt; &lt;span style="color:#0000ff;"&gt;password&lt;/span&gt; &lt;span style="color:#808080;"&gt;=&lt;/span&gt;&lt;span style="color:#ff0000;"&gt;'1410SomeReallyStrongPassword2011!'&lt;/span&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt;  &lt;p&gt;Since you are executing the command wrapped into a stored procedure, you have potential to implement additional audit, like grab execution plan or some other performance indicators of the server at the moment when the command is issued. This can be extremely helpful for troubleshooting reasons. I think this is pretty cool.&lt;/p&gt;            &lt;p&gt;Here’s the testing &lt;a target="_blank" href="https://docs.google.com/open?id=0B5ayjYKh-6TxNjYxYWM3YzItMmM2ZC00ZTA2LTg3ZWEtNmU4MzYzNTg0NmRh"&gt;script&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Enjoy.&lt;/p&gt;</description></item></channel></rss>