From fc2aa873bf8521dfb2748fdebd5f9343358e4fb4 Mon Sep 17 00:00:00 2001 From: Natan Mejirovsky Date: Thu, 26 Dec 2019 14:02:54 +0200 Subject: [PATCH 1/2] Support custom certificate validation #78 https://github.com/datalust/seq-api/issues/78 --- example/SeqCustomCertValidation/Program.cs | 39 +++++++++++++++++++ .../SeqCustomCertValidation.csproj | 17 ++++++++ seq-api.sln | 7 ++++ src/Seq.Api/Client/SeqApiClient.cs | 10 ++++- src/Seq.Api/SeqConnection.cs | 8 +++- 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 example/SeqCustomCertValidation/Program.cs create mode 100644 example/SeqCustomCertValidation/SeqCustomCertValidation.csproj diff --git a/example/SeqCustomCertValidation/Program.cs b/example/SeqCustomCertValidation/Program.cs new file mode 100644 index 0000000..58b18a9 --- /dev/null +++ b/example/SeqCustomCertValidation/Program.cs @@ -0,0 +1,39 @@ +using DocoptNet; +using Seq.Api; +using Seq.Api.Model.Settings; +using System; +using System.Net.Http; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; + +namespace SeqCustomCertValidation +{ + class Program + { + static void Main(string[] args) + { + const string Usage = @"seq-query: run Seq SQL queries from your console. + +Usage: + seq-cust-ssl.exe [--usecustssl=] + seq-cust-ssl.exe (-h | --help) + +Options: + -h --help Show this screen. + --usecustssl= Use custom ssl true/false. + "; + + var arguments = new Docopt().Apply(Usage, args, version: "Seq Custom Certificate Validation 0.1", exit: true); + string url = arguments[""].ToString(); + bool usecustssl = Convert.ToBoolean(arguments["--usecustssl"].Value); + + + var connection = new SeqConnection(url, serverCertificateCustomValidationCallback: !usecustssl? ((Func)null): (httpRequestMessage, cert, cetChain, policyErrors) => + { + return true; + }); + var limit = connection?.Settings.FindNamedAsync(SettingName.RawEventMaximumContentLength).GetAwaiter().GetResult(); + Console.WriteLine($"RawEventMaximumContentLength:{limit.Value}"); + } + } +} diff --git a/example/SeqCustomCertValidation/SeqCustomCertValidation.csproj b/example/SeqCustomCertValidation/SeqCustomCertValidation.csproj new file mode 100644 index 0000000..08fb2fa --- /dev/null +++ b/example/SeqCustomCertValidation/SeqCustomCertValidation.csproj @@ -0,0 +1,17 @@ + + + + Exe + net461;netcoreapp2.2 + seq-cust-ssl + + + + + + + + + + + diff --git a/seq-api.sln b/seq-api.sln index 0c8feca..1b26100 100644 --- a/seq-api.sln +++ b/seq-api.sln @@ -27,6 +27,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeqTail", "example\SeqTail\ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SignalCopy", "example\SignalCopy\SignalCopy.csproj", "{E8CDDE17-8E29-4EB4-A4BB-38BCE346A752}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeqCustomCertValidation", "example\SeqCustomCertValidation\SeqCustomCertValidation.csproj", "{1A7BB3A0-4B8F-4967-951A-724587A0F1B7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -53,6 +55,10 @@ Global {E8CDDE17-8E29-4EB4-A4BB-38BCE346A752}.Debug|Any CPU.Build.0 = Debug|Any CPU {E8CDDE17-8E29-4EB4-A4BB-38BCE346A752}.Release|Any CPU.ActiveCfg = Release|Any CPU {E8CDDE17-8E29-4EB4-A4BB-38BCE346A752}.Release|Any CPU.Build.0 = Release|Any CPU + {1A7BB3A0-4B8F-4967-951A-724587A0F1B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1A7BB3A0-4B8F-4967-951A-724587A0F1B7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1A7BB3A0-4B8F-4967-951A-724587A0F1B7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1A7BB3A0-4B8F-4967-951A-724587A0F1B7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -63,6 +69,7 @@ Global {34BBD428-8297-484E-B771-0B72C172C264} = {1C66E116-DC21-4C8F-833E-A4C2DDF58487} {42CEBFBA-208F-40F1-AC95-13F05F6D5412} = {1C66E116-DC21-4C8F-833E-A4C2DDF58487} {E8CDDE17-8E29-4EB4-A4BB-38BCE346A752} = {1C66E116-DC21-4C8F-833E-A4C2DDF58487} + {1A7BB3A0-4B8F-4967-951A-724587A0F1B7} = {1C66E116-DC21-4C8F-833E-A4C2DDF58487} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {20BAB483-FB94-4373-8E4C-0F846B6DBFFC} diff --git a/src/Seq.Api/Client/SeqApiClient.cs b/src/Seq.Api/Client/SeqApiClient.cs index f505450..27c4deb 100644 --- a/src/Seq.Api/Client/SeqApiClient.cs +++ b/src/Seq.Api/Client/SeqApiClient.cs @@ -28,6 +28,8 @@ using System.Threading; using Seq.Api.Streams; using System.Net.WebSockets; +using System.Security.Cryptography.X509Certificates; +using System.Net.Security; namespace Seq.Api.Client { @@ -56,7 +58,8 @@ public class SeqApiClient : IDisposable /// The base URL of the Seq server. /// An API key to use when making requests to the server, if required. /// Whether default credentials will be sent with HTTP requests; the default is true. - public SeqApiClient(string serverUrl, string apiKey = null, bool useDefaultCredentials = true) + /// Callback for custom certificate validation + public SeqApiClient(string serverUrl, string apiKey = null, bool useDefaultCredentials = true, Func serverCertificateCustomValidationCallback = null) { ServerUrl = serverUrl ?? throw new ArgumentNullException(nameof(serverUrl)); @@ -69,6 +72,11 @@ public SeqApiClient(string serverUrl, string apiKey = null, bool useDefaultCrede UseDefaultCredentials = useDefaultCredentials }; + if (serverCertificateCustomValidationCallback != null) + { + handler.ServerCertificateCustomValidationCallback = serverCertificateCustomValidationCallback; + } + var baseAddress = serverUrl; if (!baseAddress.EndsWith("/")) baseAddress += "/"; diff --git a/src/Seq.Api/SeqConnection.cs b/src/Seq.Api/SeqConnection.cs index 8388519..6415212 100644 --- a/src/Seq.Api/SeqConnection.cs +++ b/src/Seq.Api/SeqConnection.cs @@ -15,6 +15,9 @@ using System; using System.Collections.Generic; using System.Net; +using System.Net.Http; +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; using Seq.Api.Client; @@ -39,10 +42,11 @@ public class SeqConnection : ISeqConnection /// The base URL of the Seq server. /// An API key to use when making requests to the server, if required. /// Whether default credentials will be sent with HTTP requests; the default is true. - public SeqConnection(string serverUrl, string apiKey = null, bool useDefaultCredentials = true) + /// Callback for custom certificate validation + public SeqConnection(string serverUrl, string apiKey = null, bool useDefaultCredentials = true, Func serverCertificateCustomValidationCallback = null) { if (serverUrl == null) throw new ArgumentNullException(nameof(serverUrl)); - Client = new SeqApiClient(serverUrl, apiKey, useDefaultCredentials); + Client = new SeqApiClient(serverUrl, apiKey, useDefaultCredentials, serverCertificateCustomValidationCallback); } /// From 7a747cace9f0d223de6e4dbd6ef6472baf997781 Mon Sep 17 00:00:00 2001 From: nmkryon <41065597+nmkryon@users.noreply.github.com> Date: Sun, 29 Dec 2019 08:44:44 +0200 Subject: [PATCH 2/2] Revert "Support custom certificate validation" --- example/SeqCustomCertValidation/Program.cs | 39 ------------------- .../SeqCustomCertValidation.csproj | 17 -------- seq-api.sln | 7 ---- src/Seq.Api/Client/SeqApiClient.cs | 10 +---- src/Seq.Api/SeqConnection.cs | 8 +--- 5 files changed, 3 insertions(+), 78 deletions(-) delete mode 100644 example/SeqCustomCertValidation/Program.cs delete mode 100644 example/SeqCustomCertValidation/SeqCustomCertValidation.csproj diff --git a/example/SeqCustomCertValidation/Program.cs b/example/SeqCustomCertValidation/Program.cs deleted file mode 100644 index 58b18a9..0000000 --- a/example/SeqCustomCertValidation/Program.cs +++ /dev/null @@ -1,39 +0,0 @@ -using DocoptNet; -using Seq.Api; -using Seq.Api.Model.Settings; -using System; -using System.Net.Http; -using System.Net.Security; -using System.Security.Cryptography.X509Certificates; - -namespace SeqCustomCertValidation -{ - class Program - { - static void Main(string[] args) - { - const string Usage = @"seq-query: run Seq SQL queries from your console. - -Usage: - seq-cust-ssl.exe [--usecustssl=] - seq-cust-ssl.exe (-h | --help) - -Options: - -h --help Show this screen. - --usecustssl= Use custom ssl true/false. - "; - - var arguments = new Docopt().Apply(Usage, args, version: "Seq Custom Certificate Validation 0.1", exit: true); - string url = arguments[""].ToString(); - bool usecustssl = Convert.ToBoolean(arguments["--usecustssl"].Value); - - - var connection = new SeqConnection(url, serverCertificateCustomValidationCallback: !usecustssl? ((Func)null): (httpRequestMessage, cert, cetChain, policyErrors) => - { - return true; - }); - var limit = connection?.Settings.FindNamedAsync(SettingName.RawEventMaximumContentLength).GetAwaiter().GetResult(); - Console.WriteLine($"RawEventMaximumContentLength:{limit.Value}"); - } - } -} diff --git a/example/SeqCustomCertValidation/SeqCustomCertValidation.csproj b/example/SeqCustomCertValidation/SeqCustomCertValidation.csproj deleted file mode 100644 index 08fb2fa..0000000 --- a/example/SeqCustomCertValidation/SeqCustomCertValidation.csproj +++ /dev/null @@ -1,17 +0,0 @@ - - - - Exe - net461;netcoreapp2.2 - seq-cust-ssl - - - - - - - - - - - diff --git a/seq-api.sln b/seq-api.sln index 1b26100..0c8feca 100644 --- a/seq-api.sln +++ b/seq-api.sln @@ -27,8 +27,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeqTail", "example\SeqTail\ EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SignalCopy", "example\SignalCopy\SignalCopy.csproj", "{E8CDDE17-8E29-4EB4-A4BB-38BCE346A752}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeqCustomCertValidation", "example\SeqCustomCertValidation\SeqCustomCertValidation.csproj", "{1A7BB3A0-4B8F-4967-951A-724587A0F1B7}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -55,10 +53,6 @@ Global {E8CDDE17-8E29-4EB4-A4BB-38BCE346A752}.Debug|Any CPU.Build.0 = Debug|Any CPU {E8CDDE17-8E29-4EB4-A4BB-38BCE346A752}.Release|Any CPU.ActiveCfg = Release|Any CPU {E8CDDE17-8E29-4EB4-A4BB-38BCE346A752}.Release|Any CPU.Build.0 = Release|Any CPU - {1A7BB3A0-4B8F-4967-951A-724587A0F1B7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1A7BB3A0-4B8F-4967-951A-724587A0F1B7}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1A7BB3A0-4B8F-4967-951A-724587A0F1B7}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1A7BB3A0-4B8F-4967-951A-724587A0F1B7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -69,7 +63,6 @@ Global {34BBD428-8297-484E-B771-0B72C172C264} = {1C66E116-DC21-4C8F-833E-A4C2DDF58487} {42CEBFBA-208F-40F1-AC95-13F05F6D5412} = {1C66E116-DC21-4C8F-833E-A4C2DDF58487} {E8CDDE17-8E29-4EB4-A4BB-38BCE346A752} = {1C66E116-DC21-4C8F-833E-A4C2DDF58487} - {1A7BB3A0-4B8F-4967-951A-724587A0F1B7} = {1C66E116-DC21-4C8F-833E-A4C2DDF58487} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {20BAB483-FB94-4373-8E4C-0F846B6DBFFC} diff --git a/src/Seq.Api/Client/SeqApiClient.cs b/src/Seq.Api/Client/SeqApiClient.cs index 27c4deb..f505450 100644 --- a/src/Seq.Api/Client/SeqApiClient.cs +++ b/src/Seq.Api/Client/SeqApiClient.cs @@ -28,8 +28,6 @@ using System.Threading; using Seq.Api.Streams; using System.Net.WebSockets; -using System.Security.Cryptography.X509Certificates; -using System.Net.Security; namespace Seq.Api.Client { @@ -58,8 +56,7 @@ public class SeqApiClient : IDisposable /// The base URL of the Seq server. /// An API key to use when making requests to the server, if required. /// Whether default credentials will be sent with HTTP requests; the default is true. - /// Callback for custom certificate validation - public SeqApiClient(string serverUrl, string apiKey = null, bool useDefaultCredentials = true, Func serverCertificateCustomValidationCallback = null) + public SeqApiClient(string serverUrl, string apiKey = null, bool useDefaultCredentials = true) { ServerUrl = serverUrl ?? throw new ArgumentNullException(nameof(serverUrl)); @@ -72,11 +69,6 @@ public SeqApiClient(string serverUrl, string apiKey = null, bool useDefaultCrede UseDefaultCredentials = useDefaultCredentials }; - if (serverCertificateCustomValidationCallback != null) - { - handler.ServerCertificateCustomValidationCallback = serverCertificateCustomValidationCallback; - } - var baseAddress = serverUrl; if (!baseAddress.EndsWith("/")) baseAddress += "/"; diff --git a/src/Seq.Api/SeqConnection.cs b/src/Seq.Api/SeqConnection.cs index 6415212..8388519 100644 --- a/src/Seq.Api/SeqConnection.cs +++ b/src/Seq.Api/SeqConnection.cs @@ -15,9 +15,6 @@ using System; using System.Collections.Generic; using System.Net; -using System.Net.Http; -using System.Net.Security; -using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; using Seq.Api.Client; @@ -42,11 +39,10 @@ public class SeqConnection : ISeqConnection /// The base URL of the Seq server. /// An API key to use when making requests to the server, if required. /// Whether default credentials will be sent with HTTP requests; the default is true. - /// Callback for custom certificate validation - public SeqConnection(string serverUrl, string apiKey = null, bool useDefaultCredentials = true, Func serverCertificateCustomValidationCallback = null) + public SeqConnection(string serverUrl, string apiKey = null, bool useDefaultCredentials = true) { if (serverUrl == null) throw new ArgumentNullException(nameof(serverUrl)); - Client = new SeqApiClient(serverUrl, apiKey, useDefaultCredentials, serverCertificateCustomValidationCallback); + Client = new SeqApiClient(serverUrl, apiKey, useDefaultCredentials); } ///