-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-admin.SignObject.sql
44 lines (40 loc) · 1.41 KB
/
02-admin.SignObject.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
CREATE OR ALTER PROCEDURE [admin].SignObject (
@SchemaName sysname = NULL,
@ObjectName sysname = NULL
)
AS
BEGIN
DECLARE @SigningStatement NVARCHAR(max) = (
SELECT CONCAT('add signature to ',
cte.FullName,
' by certificate ',
QUOTENAME(c.name), ';'
)
FROM (
SELECT CONCAT(QUOTENAME(o.SchemaName), '.', QUOTENAME(o.ObjectName)) AS FullName ,
o.CertificateName
FROM [admin].ObjectSignatures as o
WHERE (ObjectName = @ObjectName OR @ObjectName IS NULL)
AND (SchemaName = @SchemaName OR @SchemaName IS NULL)
) AS cte
-- only sign objects that exist
JOIN sys.objects as o
ON o.object_id = OBJECT_ID(cte.FullName)
-- if specified certificate doesn't exist, still try to sign the object
-- by it, but this will fail.
LEFT JOIN sys.certificates AS c
ON cte.CertificateName = c.name
-- if signature by specified certificate is missing, try to add it
LEFT JOIN sys.crypt_properties AS sigs
ON sigs.major_id = o.object_id
AND sigs.thumbprint = c.thumbprint
WHERE sigs.thumbprint IS NULL
FOR XML PATH('')
);
IF(@SigningStatement IS NOT NULL)
BEGIN
PRINT @SigningStatement;
EXEC(@SigningStatement);
END
END
GO