<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>nlsimmons.com &#187; Permissions</title>
	<atom:link href="http://nlsimmons.com/?cat=3&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://nlsimmons.com</link>
	<description>Professional Blog &#38; Online Resume</description>
	<lastBuildDate>Wed, 10 Jun 2015 15:02:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.2</generator>
	<item>
		<title>Signing CLR Assemblies</title>
		<link>http://nlsimmons.com/?p=181</link>
		<comments>http://nlsimmons.com/?p=181#comments</comments>
		<pubDate>Mon, 06 Dec 2010 18:04:40 +0000</pubDate>
		<dc:creator><![CDATA[]]></dc:creator>
				<category><![CDATA[Permissions]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[SQL CLR]]></category>

		<guid isPermaLink="false">http://nlsimmons.com/SQLScraps/?p=181</guid>
		<description><![CDATA[So you want to load a CLR assembly into SQL Server.  What&#8217;s that you say?  You DONT want to set&#8230;]]></description>
				<content:encoded><![CDATA[            <script type="text/javascript" src="http://nlsimmons.com/wp-content/plugins/wordpress-code-snippet/scripts/shBrushSql.js"></script>
<p>So you want to load a CLR assembly into SQL Server.  What&#8217;s that you say?  You DONT want to set your database to TRUSTWORTHY?  Well look no further, there is a much more secure alternative to the old TRUSTWORTHY fallback: sign your assemblies with a secure certificate.</p>
<p>While this sounds like a walk in the park, it&#8217;s not very straight-forward.  In this post I will walk you through the steps necessary to get up and running with a signed assembly.  First you&#8217;ll need some additional tools.  You&#8217;ll need the makecert.exe tool, which is part of the Microsoft Windows SDK.  Also needed are the signtool and pvk2pfx tool, both of which are also part of the SDK.  You can download the Windows SDK here: <a href="http://go.microsoft.com/fwlink/?linkid=84091">http://go.microsoft.com/fwlink/?linkid=84091</a>.</p>
<p>Once you&#8217;ve downloaded and installed the SDK, you&#8217;re ready to begin.  First, create the root authority certificate:<br />
<pre class="brush: sql">makecert -sv SignRoot.pvk -cy authority -r signroot.cer -n &quot;CN=Cert Authority&quot;</pre></p>
<p>Once you&#8217;ve created this certificate, use it to create an additional certificate and private key pair.  This is what will be loaded into SQL server.  It is also what you will use to sign compiled DLL files.<br />
<pre class="brush: sql">makecert -m 360 -n &quot;CN=Signing Cert&quot; -iv signroot.pvk -ic signroot.cer -cy end -pe -sv signcert.pvk signcert.cer
</pre></p>
<p>Now convert this certificate into a PFX file for signing assemblies:<br />
<pre class="brush: sql">pvk2pfx -pvk signcert.pvk -spc signcert.cer -pfx signcert.pfx</pre></p>
<p>Once you have the PFX file, you can use it to sign assemblies:<br />
<pre class="brush: sql">signtool sign /f signcert.pfx &quot;YOUR_DLL_HERE&quot;</pre></p>
<p>Load the certificate file created above into SQL Server. Be sure to use the .cer file:<br />
<pre class="brush: sql">USE [master];
GO

CREATE CERTIFICATE CLRCertificate
FROM FILE = 'signcert.cer'
WITH PRIVATE KEY (
	  FILE = 'signcert.pvk'
	, DECRYPTION BY PASSWORD = 'password'
	, ENCRYPTION BY PASSWORD = 'password'
)
</pre></p>
<p>Now that the certificate has been loaded into SQL Server, you need to create a login from that certificate.  This login will need to have the necessarey assembly permissions (unsafe, external_access).<br />
<pre class="brush: sql">CREATE LOGIN AssemblyLoader FROM CERTIFICATE CLRCertificate
REVOKE connect sql FROM AssemblyLoader 
GRANT external access assembly TO AssemblyLoader 
GRANT unsafe assembly TO AssemblyLoader 
</pre><br />
By creating a certificate-based login that has the unsafe assembly and external_access assembly permissions, you need not grant any other login these permissions.  When you go to load a signed assembly, SQL Server sees that there is a login associated with the signing certificate and that the login has the necessary permissions to load assemblies of any permission level.  </p>
<p>There you have it, the assembly is successfully loaded into SQL Server without compromising security by using the TRUSTWORTHY flag.  Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://nlsimmons.com/?feed=rss2&#038;p=181</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scripting Stored Procedure Permissions</title>
		<link>http://nlsimmons.com/?p=104</link>
		<comments>http://nlsimmons.com/?p=104#comments</comments>
		<pubDate>Wed, 06 Oct 2010 09:36:52 +0000</pubDate>
		<dc:creator><![CDATA[]]></dc:creator>
				<category><![CDATA[Permissions]]></category>
		<category><![CDATA[Stored Procedures]]></category>

		<guid isPermaLink="false">http://nlsimmons.com/SQLScraps/?p=7</guid>
		<description><![CDATA[Overview When scripting stored procedure code for deployment to several servers, there are a few things to keep in mind. &#8230;]]></description>
				<content:encoded><![CDATA[            <script type="text/javascript" src="http://nlsimmons.com/wp-content/plugins/wordpress-code-snippet/scripts/shBrushSql.js"></script>
<h4>Overview</h4>
<p>When scripting stored procedure code for deployment to several servers, there are a few things to keep in mind.  One of those things is the permissions associated with the procedure you&#8217;re deploying.  When deploying a brand new stored procedure, the process of assigning permissions is fairly straightforward.  If you need to deploy an update to an existing stored procedure, things get a bit more complicated.  If you simply issue a DROP PROCEDURE call followed by the CREATE PROCEDURE code, you will lose the permissions that are associated with the stored procedure.  Issuing an ALTER PROCEDURE command will preserve the permissions but will cause you trouble if there are places where the procedure doesn&#8217;t yet exist.  To solve this issue, you should script out the permissions on a procedure to a temp table before dropping the procedure.  If there is a chance that the batch will fail, use a persisted table or a global temp table instead.  Once the proc is recreated, run through the permissions and apply them to the proc again.  This way, you&#8217;ve preserved the permissions on the proc.</p>
<p>Now, there are some pitfalls to this approach.  If the proc didn&#8217;t already exist when you run the deploy script, there will obviously be no permissions to script out to a temp table.  So, you might run into the problem where the same proc on two different databases has two sets of permissions.  This might be an issue, but considering the proc didn&#8217;t exist in the first place, it&#8217;s doubtful that it would be an issue.  Nevertheless it is something to keep in mind.</p>
<p>The way to get around this pitfall is to include desired the permissions in the SQL script used to create the procedure.  This way, the newly created proc will have the permissions it had before, in addition to any included in the deployment script.</p>
<h4>The Code</h4>
<p>Here is some sample SQL for scripting the permissions into a temp table:</p>
<p><pre class="brush: sql">IF EXISTS (SELECT * FROM sys.objects WHERE name = 'SampleProc')
BEGIN
	IF OBJECT_ID('tempdb..#tmp_PermissionsWork') IS NOT NULL DROP TABLE #tmp_PermissionsWork
	CREATE TABLE #tmp_PermissionsWork (
		  ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL 
		, Permission NVARCHAR(MAX) NULL
	)
	
	INSERT #tmp_PermissionsWork (Permission)
	SELECT state_desc + ' ' + permission_name + ' ON ' + OBJECT_NAME(major_id) + ' TO ' + QUOTENAME(USER_NAME(grantee_principal_id)) + ';' FROM sys.database_permissions 
	WHERE major_id = OBJECT_ID('SampleProc')

	DROP PROCEDURE SampleProc
END
GO

CREATE PROCEDURE SampleProc
AS
...
GO

DECLARE @cmd NVARCHAR(MAX)

WHILE EXISTS(SELECT 1 FROM #tmp_PermissionsWork)
BEGIN
	SELECT TOP 1 @cmd = Permission
	FROM #tmp_PermissionsWork
	
	EXEC sp_executesql @cmd;
	
	DELETE FROM #tmp_PermissionsWork 
	WHERE Permission = @cmd
END
GO</pre></p>
<pre>
IF EXISTS (SELECT * FROM sys.objects WHERE name = 'SampleProc')
BEGIN
	IF OBJECT_ID('tempdb..#tmp_PermissionsWork') IS NOT NULL DROP TABLE #tmp_PermissionsWork
	CREATE TABLE #tmp_PermissionsWork (
		  ID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED NOT NULL 
		, Permission NVARCHAR(MAX) NULL
	)
	
	INSERT #tmp_PermissionsWork (Permission)
	SELECT state_desc + ' ' + permission_name + ' ON ' + OBJECT_NAME(major_id) + ' TO ' + QUOTENAME(USER_NAME(grantee_principal_id)) + ';' FROM sys.database_permissions 
	WHERE major_id = OBJECT_ID('SampleProc')

	DROP PROCEDURE SampleProc
END
GO

CREATE PROCEDURE SampleProc
AS
...
GO

DECLARE @cmd NVARCHAR(MAX)

WHILE EXISTS(SELECT 1 FROM #tmp_PermissionsWork)
BEGIN
	SELECT TOP 1 @cmd = Permission
	FROM #tmp_PermissionsWork
	
	EXEC sp_executesql @cmd;
	
	DELETE FROM #tmp_PermissionsWork 
	WHERE Permission = @cmd
END
GO
</pre>
<p>To use this code, swap out &#8216;SampleProc&#8217; with your stored procedure and put your procedure code in place of lines 17 &#8211; 20.</p>
<p><font face=arial size=1>*This code is provided &#8220;AS-IS&#8221; with no warranty of any kind.</font></p>
]]></content:encoded>
			<wfw:commentRss>http://nlsimmons.com/?feed=rss2&#038;p=104</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
