-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Containers: insecure registries: allow https (ignore cert errors), an…
…d accept config from envvar. (#41506)
- Loading branch information
Showing
10 changed files
with
419 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
85 changes: 85 additions & 0 deletions
85
src/Containers/Microsoft.NET.Build.Containers/FallbackToHttpMessageHandler.cs
This file contains 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,85 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Net; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.NET.Build.Containers.Resources; | ||
|
||
namespace Microsoft.NET.Build.Containers; | ||
|
||
/// <summary> | ||
/// A delegating handler that falls back from https to http for a specific hostname. | ||
/// </summary> | ||
internal sealed partial class FallbackToHttpMessageHandler : DelegatingHandler | ||
{ | ||
private readonly string _host; | ||
private readonly int _port; | ||
private readonly ILogger _logger; | ||
private bool _fallbackToHttp; | ||
|
||
public FallbackToHttpMessageHandler(string host, int port, HttpMessageHandler innerHandler, ILogger logger) : base(innerHandler) | ||
{ | ||
_host = host; | ||
_port = port; | ||
_logger = logger; | ||
} | ||
|
||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
if (request.RequestUri is null) | ||
{ | ||
throw new ArgumentException(Resource.GetString(nameof(Strings.NoRequestUriSpecified)), nameof(request)); | ||
} | ||
|
||
bool canFallback = request.RequestUri.Host == _host && request.RequestUri.Port == _port && request.RequestUri.Scheme == "https"; | ||
do | ||
{ | ||
try | ||
{ | ||
if (canFallback && _fallbackToHttp) | ||
{ | ||
FallbackToHttp(request); | ||
canFallback = false; | ||
} | ||
|
||
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false); | ||
} | ||
catch (HttpRequestException re) when (canFallback && ShouldAttemptFallbackToHttp(re)) | ||
{ | ||
string uri = request.RequestUri.ToString(); | ||
try | ||
{ | ||
// Try falling back. | ||
_logger.LogTrace("Attempt to fall back to http for {uri}.", uri); | ||
FallbackToHttp(request); | ||
HttpResponseMessage response = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); | ||
|
||
// Fall back was successful. Use http for all new requests. | ||
_logger.LogTrace("Fall back to http for {uri} was successful.", uri); | ||
_fallbackToHttp = true; | ||
|
||
return response; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_logger.LogInformation(ex, "Fall back to http for {uri} failed with message \"{message}\".", uri, ex.Message); | ||
} | ||
|
||
// Falling back didn't work, throw original exception. | ||
throw; | ||
} | ||
} while (true); | ||
} | ||
|
||
internal static bool ShouldAttemptFallbackToHttp(HttpRequestException exception) | ||
{ | ||
return exception.HttpRequestError == HttpRequestError.SecureConnectionError; | ||
} | ||
|
||
private static void FallbackToHttp(HttpRequestMessage request) | ||
{ | ||
var uriBuilder = new UriBuilder(request.RequestUri!); | ||
uriBuilder.Scheme = "http"; | ||
request.RequestUri = uriBuilder.Uri; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.