From 1fabdbdceda7c96b72acf62a774ded05858d2e33 Mon Sep 17 00:00:00 2001 From: David Browne Date: Tue, 12 Jul 2022 18:27:02 -0500 Subject: [PATCH 1/3] Making the deployment script portable Embedding the assembly bytes in the deployment script to make it portable, and support Managed Instance --- samples/features/sql-clr/Curl/SqlClrCurl.tt | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/samples/features/sql-clr/Curl/SqlClrCurl.tt b/samples/features/sql-clr/Curl/SqlClrCurl.tt index 6ff3e308..3dd3b704 100644 --- a/samples/features/sql-clr/Curl/SqlClrCurl.tt +++ b/samples/features/sql-clr/Curl/SqlClrCurl.tt @@ -32,19 +32,18 @@ DROP ASSEMBLY IF EXISTS SqlClrCurl; GO +DECLARE @assembly VARBINARY(MAX) = <#= "0x" + BitConverter.ToString(System.IO.File.ReadAllBytes(this.Host.ResolvePath("bin\\Release\\SqlClrCurl.dll"))).Replace("-","") #> DECLARE @hash VARBINARY(64); -SELECT @hash = HASHBYTES('SHA2_512', BulkColumn) -FROM OPENROWSET(BULK '<#= this.Host.ResolvePath("bin\\Release\\SqlClrCurl.dll") #>', SINGLE_BLOB) AS assembly_content +SELECT @hash = HASHBYTES('SHA2_512', @assembly) -IF(@hash IS NOT NULL) +IF(not exists (select * from sys.trusted_assemblies where hash = @hash)) EXEC sp_add_trusted_assembly @hash, N'SqlClrCurl' ELSE - PRINT 'Cannot create hash for assembly!' -GO + print 'Hash already exists' --Create the assembly CREATE ASSEMBLY SqlClrCurl -FROM '<#= this.Host.ResolvePath("bin\\Release\\SqlClrCurl.dll") #>' +FROM @assembly WITH PERMISSION_SET = EXTERNAL_ACCESS; GO @@ -60,4 +59,4 @@ GO CREATE PROCEDURE CURL.XPOST (@H NVARCHAR(MAX), @d NVARCHAR(MAX), @url NVARCHAR(4000)) AS EXTERNAL NAME SqlClrCurl.Curl.Post; -GO \ No newline at end of file +GO From 0eed8a78b6976918432619700f7db4b7620c99b8 Mon Sep 17 00:00:00 2001 From: dbrownems Date: Wed, 13 Jul 2022 10:43:48 -0500 Subject: [PATCH 2/3] Updating template to drop trusted assembly only if it exists, preventing error. --- samples/features/sql-clr/Curl/SqlClrCurl.tt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/features/sql-clr/Curl/SqlClrCurl.tt b/samples/features/sql-clr/Curl/SqlClrCurl.tt index 3dd3b704..94166667 100644 --- a/samples/features/sql-clr/Curl/SqlClrCurl.tt +++ b/samples/features/sql-clr/Curl/SqlClrCurl.tt @@ -24,14 +24,14 @@ SELECT @hash = hash FROM sys.trusted_assemblies ta WHERE ta.description = N'SqlClrCurl' -EXEC sp_drop_trusted_assembly @hash; +if (@hash is not null) + EXEC sp_drop_trusted_assembly @hash; GO --Drop the assembly if it already exists DROP ASSEMBLY IF EXISTS SqlClrCurl; GO - DECLARE @assembly VARBINARY(MAX) = <#= "0x" + BitConverter.ToString(System.IO.File.ReadAllBytes(this.Host.ResolvePath("bin\\Release\\SqlClrCurl.dll"))).Replace("-","") #> DECLARE @hash VARBINARY(64); SELECT @hash = HASHBYTES('SHA2_512', @assembly) From 4720aa94beae75701e50892a4a8fde55725f4d80 Mon Sep 17 00:00:00 2001 From: dbrownems Date: Wed, 13 Jul 2022 11:30:11 -0500 Subject: [PATCH 3/3] Updating targeting to 4.7.1 and forcing TLS 1.2 --- samples/features/sql-clr/Curl/Curl.cs | 8 ++++++++ samples/features/sql-clr/Curl/SqlClrCurl.csproj | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/samples/features/sql-clr/Curl/Curl.cs b/samples/features/sql-clr/Curl/Curl.cs index e2648a7e..d0f16779 100644 --- a/samples/features/sql-clr/Curl/Curl.cs +++ b/samples/features/sql-clr/Curl/Curl.cs @@ -9,10 +9,16 @@ using System.Threading; /// public partial class Curl { + static Curl() + { + ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; + } + [SqlFunction] [return: SqlFacet(MaxSize = -1)] public static SqlChars Get(SqlChars H, SqlChars url) { + var client = new WebClient(); AddHeader(H, client); return new SqlChars( @@ -24,6 +30,7 @@ public partial class Curl [SqlProcedure] public static void Post(SqlChars H, SqlChars d, SqlChars url) { + var client = new WebClient(); AddHeader(H, client); if (d.IsNull) @@ -39,6 +46,7 @@ public partial class Curl [SqlProcedure] public static void PostWithRetry(SqlChars H, SqlChars d, SqlChars url) { + var client = new WebClient(); AddHeader(H, client); if (d.IsNull) diff --git a/samples/features/sql-clr/Curl/SqlClrCurl.csproj b/samples/features/sql-clr/Curl/SqlClrCurl.csproj index af885557..d35f133f 100644 --- a/samples/features/sql-clr/Curl/SqlClrCurl.csproj +++ b/samples/features/sql-clr/Curl/SqlClrCurl.csproj @@ -9,7 +9,7 @@ Properties SqlClr SqlClrCurl - v4.5 + v4.7.2 512