From e84e30ae46dfacbbdd15765699827518b29041bf Mon Sep 17 00:00:00 2001 From: Reinhold Rumberger Date: Thu, 30 Aug 2012 11:53:37 +0200 Subject: [PATCH 1/2] Added "IgnoreInvalidSSLCert" as a recognised parameter and the respective documentation string. Created an "SslCertificateValidator" and used it when the "IgnoreInvalidSSLCert" option is set. --- src/OpenRasta.Client/OpenRasta.Client.csproj | 1 + .../SslCertificateValidators.cs | 30 +++++++++++++++++++ .../CommandDocumentation.resx | 18 +++++++++++ .../Remote/AddRemoteCommand.cs | 10 +++++++ .../Remote/SetRemoteCommand.cs | 10 +++++++ src/OpenWrap.Commands/Wrap/AddWrapCommand.cs | 11 +++++++ src/OpenWrap.Commands/Wrap/ListWrapCommand.cs | 11 +++++++ src/OpenWrap.Commands/Wrap/NukeWrapCommand.cs | 11 +++++++ .../Wrap/PublishWrapCommand.cs | 12 +++++++- .../Wrap/UpdateWrapCommand.cs | 12 ++++++++ 10 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 src/OpenRasta.Client/SslCertificateValidators.cs diff --git a/src/OpenRasta.Client/OpenRasta.Client.csproj b/src/OpenRasta.Client/OpenRasta.Client.csproj index bdc120c8..92b93d3c 100644 --- a/src/OpenRasta.Client/OpenRasta.Client.csproj +++ b/src/OpenRasta.Client/OpenRasta.Client.csproj @@ -63,6 +63,7 @@ + diff --git a/src/OpenRasta.Client/SslCertificateValidators.cs b/src/OpenRasta.Client/SslCertificateValidators.cs new file mode 100644 index 00000000..dfd52b6e --- /dev/null +++ b/src/OpenRasta.Client/SslCertificateValidators.cs @@ -0,0 +1,30 @@ +using System.Net.Security; +using System.Security.Cryptography.X509Certificates; + +namespace OpenRasta.Client +{ + /// + /// Contains validation callbacks for + /// . + /// + public static class SslCertificateValidators + { + /// + /// Validates any SSL certificate, however invalid. + /// + /// An object that contains state information for this validation. + /// The certificate used to authenticate the remote party. + /// The chain of certificate authorities associated with the remote certificate. + /// One or more errors associated with the remote certificate. + /// true + public static bool ValidateAnyRemoteCertificate( + object sender, + X509Certificate certificate, + X509Chain chain, + SslPolicyErrors sslPolicyErrors) + { + // allow any SSL certificate (self-signed, expired, ...) + return true; + } + } +} diff --git a/src/OpenWrap.Commands/CommandDocumentation.resx b/src/OpenWrap.Commands/CommandDocumentation.resx index f76249fd..6d0778ed 100644 --- a/src/OpenWrap.Commands/CommandDocumentation.resx +++ b/src/OpenWrap.Commands/CommandDocumentation.resx @@ -481,4 +481,22 @@ Note that the NuPack support is only provided for backward compatibility with le Include pre-release packages for the specified dependency. + + Ignores invalid SSL certificates and keeps going despite them. + + + Ignores invalid SSL certificates and keeps going despite them. + + + Ignores invalid SSL certificates and keeps going despite them. + + + Ignores invalid SSL certificates and keeps going despite them. + + + Ignores invalid SSL certificates and keeps going despite them. + + + Ignores invalid SSL certificates and keeps going despite them. + \ No newline at end of file diff --git a/src/OpenWrap.Commands/Remote/AddRemoteCommand.cs b/src/OpenWrap.Commands/Remote/AddRemoteCommand.cs index c6dbd5df..58abc978 100644 --- a/src/OpenWrap.Commands/Remote/AddRemoteCommand.cs +++ b/src/OpenWrap.Commands/Remote/AddRemoteCommand.cs @@ -2,7 +2,9 @@ using System.Collections.Generic; using System.Linq; using System.Net; +using System.Net.Security; using System.Text.RegularExpressions; +using OpenRasta.Client; using OpenWrap.Collections; using OpenWrap.Commands.Messages; using OpenWrap.Commands.Remote.Messages; @@ -20,6 +22,9 @@ public class AddRemoteCommand : AbstractRemoteCommand [CommandInput(Position = 1)] public string Href { get; set; } + [CommandInput] + public bool IgnoreInvalidSSLCert { get; set; } + [CommandInput(Position = 0, IsRequired = true)] public string Name { get; set; } @@ -38,6 +43,11 @@ public class AddRemoteCommand : AbstractRemoteCommand protected override IEnumerable ExecuteCore() { var repositories = ConfigurationManager.Load(); + if (IgnoreInvalidSSLCert) + { + ServicePointManager.ServerCertificateValidationCallback + += new RemoteCertificateValidationCallback(SslCertificateValidators.ValidateAnyRemoteCertificate); + } return repositories.ContainsKey(Name) ? Append(repositories) diff --git a/src/OpenWrap.Commands/Remote/SetRemoteCommand.cs b/src/OpenWrap.Commands/Remote/SetRemoteCommand.cs index 57431cf2..a4160483 100644 --- a/src/OpenWrap.Commands/Remote/SetRemoteCommand.cs +++ b/src/OpenWrap.Commands/Remote/SetRemoteCommand.cs @@ -2,6 +2,8 @@ using System.Collections.Generic; using System.Linq; using System.Net; +using System.Net.Security; +using OpenRasta.Client; using OpenWrap.Collections; using OpenWrap.Commands.Errors; using OpenWrap.Commands.Messages; @@ -24,6 +26,9 @@ public class SetRemoteCommand : AbstractRemoteCommand [CommandInput] public string Href { get; set; } + [CommandInput] + public bool IgnoreInvalidSSLCert { get; set; } + [CommandInput(Position = 0, IsRequired = true)] public string Name { get; set; } @@ -45,6 +50,11 @@ public class SetRemoteCommand : AbstractRemoteCommand protected override IEnumerable ExecuteCore() { HandlePrioritySetting(_remotes, _targetRemote); + if (IgnoreInvalidSSLCert) + { + ServicePointManager.ServerCertificateValidationCallback + += new RemoteCertificateValidationCallback(SslCertificateValidators.ValidateAnyRemoteCertificate); + } if (NewName != null) _targetRemote.Name = NewName; diff --git a/src/OpenWrap.Commands/Wrap/AddWrapCommand.cs b/src/OpenWrap.Commands/Wrap/AddWrapCommand.cs index 7604afb9..a0f11f48 100644 --- a/src/OpenWrap.Commands/Wrap/AddWrapCommand.cs +++ b/src/OpenWrap.Commands/Wrap/AddWrapCommand.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Net.Security; +using OpenRasta.Client; using OpenWrap.Collections; using OpenWrap.PackageManagement; using OpenWrap.PackageModel; @@ -32,6 +35,9 @@ public class AddWrapCommand : WrapCommand public string From { get; set; } [CommandInput] + public bool IgnoreInvalidSSLCert { get; set; } + + [CommandInput] public string MaxVersion { get; set; } [CommandInput] @@ -113,6 +119,11 @@ protected override IEnumerable ExecuteCore() yield return VerifyProjectRepository(); yield return SetupEnvironmentForAdd(); + if (IgnoreInvalidSSLCert) + { + ServicePointManager.ServerCertificateValidationCallback + += new RemoteCertificateValidationCallback(SslCertificateValidators.ValidateAnyRemoteCertificate); + } var sourceRepositories = GetSourceRepositories().ToList(); var descriptor = new PackageDescriptor(_targetDescriptor.Value); diff --git a/src/OpenWrap.Commands/Wrap/ListWrapCommand.cs b/src/OpenWrap.Commands/Wrap/ListWrapCommand.cs index 55b0c7a2..da616acf 100644 --- a/src/OpenWrap.Commands/Wrap/ListWrapCommand.cs +++ b/src/OpenWrap.Commands/Wrap/ListWrapCommand.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Net.Security; +using OpenRasta.Client; using OpenWrap.Collections; using OpenWrap.Configuration; using OpenWrap.PackageManagement; @@ -14,6 +17,9 @@ public class ListWrapCommand : WrapCommand string _remote; bool _remoteSet; + [CommandInput] + public bool IgnoreInvalidSSLCert { get; set; } + [CommandInput(Position = 0)] public string Query { get; set; } @@ -68,6 +74,11 @@ protected override IEnumerable ExecuteCore() yield return m; yield break; } + if (IgnoreInvalidSSLCert) + { + ServicePointManager.ServerCertificateValidationCallback + += new RemoteCertificateValidationCallback(SslCertificateValidators.ValidateAnyRemoteCertificate); + } if (Project) { foreach(var descriptor in HostEnvironment.ScopedDescriptors diff --git a/src/OpenWrap.Commands/Wrap/NukeWrapCommand.cs b/src/OpenWrap.Commands/Wrap/NukeWrapCommand.cs index 31d51413..81b982e2 100644 --- a/src/OpenWrap.Commands/Wrap/NukeWrapCommand.cs +++ b/src/OpenWrap.Commands/Wrap/NukeWrapCommand.cs @@ -1,5 +1,8 @@ using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Net.Security; +using OpenRasta.Client; using OpenWrap.Commands.Errors; using OpenWrap.Repositories; @@ -8,6 +11,9 @@ namespace OpenWrap.Commands.Wrap [Command(Noun = "wrap", Verb = "nuke", Description = "Removes a wrap from a remote repository index.")] public class NukeWrapCommand : WrapCommand { + [CommandInput] + public bool IgnoreInvalidSSLCert { get; set; } + [CommandInput(IsRequired = true, Position = 1)] public string Name { get; set; } @@ -20,6 +26,11 @@ public class NukeWrapCommand : WrapCommand protected override IEnumerable ExecuteCore() { + if (IgnoreInvalidSSLCert) + { + ServicePointManager.ServerCertificateValidationCallback + += new RemoteCertificateValidationCallback(SslCertificateValidators.ValidateAnyRemoteCertificate); + } // TODO: HACK HACK HACK IPackageRepository repo = Remotes.PublishRepositories(Remote).SelectMany(_=>_).FirstOrDefault(); if (repo == null) diff --git a/src/OpenWrap.Commands/Wrap/PublishWrapCommand.cs b/src/OpenWrap.Commands/Wrap/PublishWrapCommand.cs index 5a8e7a87..e6729a60 100644 --- a/src/OpenWrap.Commands/Wrap/PublishWrapCommand.cs +++ b/src/OpenWrap.Commands/Wrap/PublishWrapCommand.cs @@ -3,6 +3,8 @@ using System.IO; using System.Linq; using System.Net; +using System.Net.Security; +using OpenRasta.Client; using OpenFileSystem.IO; using OpenWrap.Commands.Errors; using OpenWrap.Commands.Messages; @@ -24,6 +26,9 @@ public class PublishWrapCommand : WrapCommand NetworkCredential _credentials; [CommandInput] + public bool IgnoreInvalidSSLCert { get; set; } + + [CommandInput] public string Name { get; set; } [CommandInput(Position = 1)] @@ -42,7 +47,12 @@ protected override IEnumerable ExecuteCore() { yield return new Info(String.Format("Publishing package '{0}' to '{1}'", _packageFileName, Remote)); - var credentialCookie =_credentials != null + if (IgnoreInvalidSSLCert) + { + ServicePointManager.ServerCertificateValidationCallback + += new RemoteCertificateValidationCallback(SslCertificateValidators.ValidateAnyRemoteCertificate); + } + var credentialCookie = _credentials != null ? _authenticationSupport.WithCredentials(_credentials) : null; try diff --git a/src/OpenWrap.Commands/Wrap/UpdateWrapCommand.cs b/src/OpenWrap.Commands/Wrap/UpdateWrapCommand.cs index f2954830..28b3c02e 100644 --- a/src/OpenWrap.Commands/Wrap/UpdateWrapCommand.cs +++ b/src/OpenWrap.Commands/Wrap/UpdateWrapCommand.cs @@ -1,6 +1,9 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; +using System.Net.Security; +using OpenRasta.Client; using OpenWrap.PackageManagement; using OpenWrap.PackageManagement.DependencyResolvers; using OpenWrap.PackageModel; @@ -19,6 +22,9 @@ public class UpdateWrapCommand : WrapCommand [CommandInput] public string From { get; set; } + [CommandInput] + public bool IgnoreInvalidSSLCert { get; set; } + [CommandInput(Position = 0)] public string Name { get; set; } @@ -40,6 +46,12 @@ public bool System public string Scope { get; set; } protected override IEnumerable ExecuteCore() { + if (IgnoreInvalidSSLCert) + { + ServicePointManager.ServerCertificateValidationCallback + += new RemoteCertificateValidationCallback(SslCertificateValidators.ValidateAnyRemoteCertificate); + } + var update = Enumerable.Empty(); if (Project) update = update.Concat(UpdateProjectPackages()); From a814a1e5786e4b91b8fe7be49eb566f7c8aa9a00 Mon Sep 17 00:00:00 2001 From: Reinhold Rumberger Date: Wed, 10 Oct 2012 19:48:45 +0200 Subject: [PATCH 2/2] - some pretty-printing and spell-checking in MemoryHttpClient - fix issue #344 by not simply ignoring "Transfer-Encoding: chunked" answers and making ProgressStream able to handle streams where the total isn't known beforehand --- .../HttpWebResponseBasedResponse.cs | 3 ++- src/OpenRasta.Client/Memory/MemoryHttpClient.cs | 5 +++-- src/OpenRasta.Client/ProgressStream.cs | 15 +++++++++++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/OpenRasta.Client/HttpWebResponseBasedResponse.cs b/src/OpenRasta.Client/HttpWebResponseBasedResponse.cs index 13b31d26..8ebaf6c4 100644 --- a/src/OpenRasta.Client/HttpWebResponseBasedResponse.cs +++ b/src/OpenRasta.Client/HttpWebResponseBasedResponse.cs @@ -26,7 +26,8 @@ public HttpWebResponseBasedResponse(HttpWebRequestBasedRequest request, HttpWebR { Status = new HttpStatus((int)_response.StatusCode, _response.StatusDescription); RaiseStatusChanged("Connected."); - if (_response.ContentLength > 0) + if (_response.ContentLength > 0 + || (_response.Headers.Get("Transfer-Encoding") != null && _response.Headers.Get("Transfer-Encoding").Equals("chunked"))) { _entity = new HttpEntity(new ProgressStream(_response.ContentLength, RaiseProgress, _response.GetResponseStream())); } diff --git a/src/OpenRasta.Client/Memory/MemoryHttpClient.cs b/src/OpenRasta.Client/Memory/MemoryHttpClient.cs index b568621a..1572f351 100644 --- a/src/OpenRasta.Client/Memory/MemoryHttpClient.cs +++ b/src/OpenRasta.Client/Memory/MemoryHttpClient.cs @@ -42,7 +42,8 @@ public IClientResponse Send() { var response = RedirectIfNeeded(_client.Execute(this), this); - foreach (var handler in Handlers.Where(x => x.Key(response)).Select(x => x.Value)) handler(response); + foreach (var handler in Handlers.Where(x => x.Key(response)).Select(x => x.Value)) + handler(response); return response; } @@ -76,7 +77,7 @@ IClientResponse Execute(IClientRequest request) { return new MemoryResponse { - Status = new HttpStatus(404, "Nout found"), + Status = new HttpStatus(404, "Not found"), Headers = { { "Content-Length", "0" } } }; } diff --git a/src/OpenRasta.Client/ProgressStream.cs b/src/OpenRasta.Client/ProgressStream.cs index dd67d369..81275634 100644 --- a/src/OpenRasta.Client/ProgressStream.cs +++ b/src/OpenRasta.Client/ProgressStream.cs @@ -26,9 +26,20 @@ public override int Read(byte[] buffer, int offset, int count) void NotifyProgress(int amount) { - _total += amount; + if (_size > 0) + { + _total += amount; - _progressNotifier((int)(((double)_total / _size) * 100)); + _progressNotifier((int)(((double)_total / _size) * 100)); + } + else if (amount > 0) + { + _progressNotifier(50); + } + else + { + _progressNotifier(100); + } } public override void Write(byte[] buffer, int offset, int count)