Repository navigation
XS✔ ◾ fix(healthchecks): scope TLS validation bypass to local endpoints #902
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
...agnostics.HealthChecks.UnitTests/HealthCheckHttpClientServiceCollectionExtensionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,156 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Net.Http; | ||
| using System.Net.Security; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
| using Microsoft.Extensions.Http; | ||
| using Microsoft.Extensions.Options; | ||
| using Microsoft.Omex.Extensions.Testing.Helpers; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using Moq; | ||
|
|
||
| namespace Microsoft.Omex.Extensions.Diagnostics.HealthChecks.UnitTests | ||
| { | ||
| [TestClass] | ||
| public class HealthCheckHttpClientServiceCollectionExtensionsTests | ||
| { | ||
| private const string ClientName = "TestHealthCheckClient"; | ||
|
|
||
| [TestMethod] | ||
| [DataRow("https://localhost:8080/health")] | ||
| [DataRow("https://127.0.0.1:8080/health")] | ||
| [DataRow("https://[::1]:8080/health")] | ||
| [DataRow("https://10.0.0.4:8080/health")] | ||
| [DataRow("https://[fd00::1]:8080/health")] | ||
| public void AddHealthCheckHttpClient_ForLocalOrIpEndpoint_IgnoresCertificateErrors(string url) | ||
| { | ||
| Assert.IsTrue( | ||
| Validate(CertificateCallback(Environments.Production, validated: false), url, SslPolicyErrors.RemoteCertificateNameMismatch), | ||
| FormattableString.Invariant($"Certificate errors should be ignored for local endpoint '{url}'")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow("https://service.contoso.com/health")] | ||
| [DataRow("https://my-service.westus.cloudapp.azure.com/health")] | ||
| public void AddHealthCheckHttpClient_ForDnsEndpoint_ValidatesCertificate(string url) | ||
| { | ||
| Func<HttpRequestMessage, SslPolicyErrors, bool> callback = CertificateCallback(Environments.Production, validated: false); | ||
|
|
||
| Assert.IsFalse( | ||
| Validate(callback, url, SslPolicyErrors.RemoteCertificateNameMismatch), | ||
| FormattableString.Invariant($"Certificate errors should be rejected for DNS endpoint '{url}'")); | ||
|
|
||
| Assert.IsTrue( | ||
| Validate(callback, url, SslPolicyErrors.None), | ||
| FormattableString.Invariant($"A valid certificate should be accepted for DNS endpoint '{url}'")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow("https://service.contoso.com/health")] | ||
| [DataRow("https://localhost:8080/health")] | ||
| public void AddHealthCheckHttpClient_InDevelopment_IgnoresCertificateErrors(string url) | ||
| { | ||
| Assert.IsTrue( | ||
| Validate(CertificateCallback(Environments.Development, validated: false), url, SslPolicyErrors.RemoteCertificateChainErrors), | ||
| FormattableString.Invariant($"Certificate errors should be ignored in Development for '{url}'")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow("Production")] | ||
| [DataRow("Staging")] | ||
| [DataRow("EDog")] | ||
| [DataRow("Int")] | ||
| [DataRow(null)] | ||
| public void AddValidatedHealthCheckHttpClient_OutsideDevelopment_ValidatesLocalEndpoints(string? environmentName) | ||
| { | ||
| Func<HttpRequestMessage, SslPolicyErrors, bool> callback = CertificateCallback(environmentName, validated: true); | ||
|
|
||
| Assert.IsFalse( | ||
| Validate(callback, "https://localhost:8080/health", SslPolicyErrors.RemoteCertificateNameMismatch), | ||
| FormattableString.Invariant($"Local certificate errors should be rejected in '{environmentName ?? "<unregistered>"}'")); | ||
|
|
||
| Assert.IsFalse( | ||
| Validate(callback, "https://10.0.0.4:8080/health", SslPolicyErrors.RemoteCertificateNameMismatch), | ||
| FormattableString.Invariant($"IP certificate errors should be rejected in '{environmentName ?? "<unregistered>"}'")); | ||
|
|
||
| Assert.IsTrue( | ||
| Validate(callback, "https://localhost:8080/health", SslPolicyErrors.None), | ||
| "A valid certificate should always be accepted"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void AddValidatedHealthCheckHttpClient_InDevelopment_IgnoresCertificateErrors() => | ||
| Assert.IsTrue( | ||
| Validate(CertificateCallback(Environments.Development, validated: true), "https://localhost:8080/health", SslPolicyErrors.RemoteCertificateChainErrors), | ||
| "Certificate errors should be ignored in Development"); | ||
|
|
||
| private static bool Validate(Func<HttpRequestMessage, SslPolicyErrors, bool> callback, string url, SslPolicyErrors errors) | ||
| { | ||
| using HttpRequestMessage request = new(HttpMethod.Get, url); | ||
| return callback(request, errors); | ||
| } | ||
|
|
||
| private static Func<HttpRequestMessage, SslPolicyErrors, bool> CertificateCallback(string? environmentName, bool validated) | ||
| { | ||
| ServiceCollection services = new(); | ||
|
|
||
| if (environmentName != null) | ||
| { | ||
| Mock<IHostEnvironment> environmentMock = new(); | ||
| environmentMock.SetupGet(e => e.EnvironmentName).Returns(environmentName); | ||
| services.AddSingleton(environmentMock.Object); | ||
| } | ||
|
|
||
| if (validated) | ||
| { | ||
| services.AddValidatedHealthCheckHttpClient(ClientName); | ||
| } | ||
| else | ||
| { | ||
| services.AddHealthCheckHttpClient(ClientName); | ||
| } | ||
|
|
||
| IServiceProvider provider = services.BuildServiceProvider(); | ||
|
|
||
| HttpClientFactoryOptions options = provider | ||
| .GetRequiredService<IOptionsMonitor<HttpClientFactoryOptions>>() | ||
| .Get(ClientName); | ||
|
|
||
| TestHttpMessageHandlerBuilder builder = new(ClientName, provider); | ||
| foreach (Action<HttpMessageHandlerBuilder> action in options.HttpMessageHandlerBuilderActions) | ||
| { | ||
| action(builder); | ||
| } | ||
|
|
||
| Assert.IsInstanceOfType(builder.PrimaryHandler, typeof(HttpClientHandler)); | ||
| HttpClientHandler handler = (HttpClientHandler)builder.PrimaryHandler; | ||
|
|
||
| NullableAssert.IsNotNull(handler.ServerCertificateCustomValidationCallback, "A validation callback should be configured"); | ||
|
|
||
| return (request, errors) => handler.ServerCertificateCustomValidationCallback(request, null, null, errors); | ||
| } | ||
|
|
||
| private sealed class TestHttpMessageHandlerBuilder : HttpMessageHandlerBuilder | ||
| { | ||
| public TestHttpMessageHandlerBuilder(string name, IServiceProvider services) | ||
| { | ||
| Name = name; | ||
| Services = services; | ||
| } | ||
|
|
||
| public override string? Name { get; set; } | ||
|
|
||
| public override HttpMessageHandler PrimaryHandler { get; set; } = new HttpClientHandler(); | ||
|
|
||
| public override IList<DelegatingHandler> AdditionalHandlers { get; } = new List<DelegatingHandler>(); | ||
|
|
||
| public override IServiceProvider Services { get; } | ||
|
|
||
| public override HttpMessageHandler Build() => PrimaryHandler; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.