Archive for July, 2019

Moving from an execute as owner approach to a signed procedure approach

July 17, 2019

The following can be used as a template for signing procedures which removes the need for executing as owner and database chaining

–create a test login
CREATE LOGIN test WITH PASSWORD =’abc’, CHECK_POLICY = OFF

GO

–create a test proc

CREATE PROC test_proc
AS
SELECT * FROM sys.dm_exec_query_memory_grants

GO

GRANT EXEC ON test_proc TO PUBLIC

CREATE CERTIFICATE [CodeSigningCertificate]
ENCRYPTION BY PASSWORD = ‘Password123!’
WITH EXPIRY_DATE = ‘2099-01-01’,
SUBJECT = ‘Code Signing Cert’

CREATE LOGIN [CodeSigningLogin] FROM CERTIFICATE [CodeSigningCertificate]

GRANT VIEW SERVER STATE TO [CodeSigningLogin]
ADD SIGNATURE TO test_proc
BY CERTIFICATE [CodeSigningCertificate]
WITH PASSWORD = ‘Password123!’;

–Clean Up

/*
DROP PROC dbo.test_proc
DROP LOGIN [CodeSigningLogin]
DROP CERTIFICATE [CodeSigningCertificate]
DROP LOGIN test
*/

Advertisements