Skip to content

[Templates] Switch to use a certificate file (port from master) #21496

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Components/Web.JS/dist/Release/blazor.server.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.Threading.Tasks;
using AngleSharp.Dom.Html;
using AngleSharp.Parser.Html;
using Microsoft.AspNetCore.Certificates.Generation;
using Microsoft.AspNetCore.Server.IntegrationTesting;
using Microsoft.Extensions.CommandLineUtils;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using OpenQA.Selenium;
using OpenQA.Selenium.Edge;
Expand All @@ -28,6 +31,10 @@ public class AspNetProcess : IDisposable
private readonly HttpClient _httpClient;
private readonly ITestOutputHelper _output;

private string _certificatePath;
private string _certificatePassword = Guid.NewGuid().ToString();
private string _certificateThumbprint;

internal readonly Uri ListeningUri;
internal ProcessEx Process { get; }

Expand All @@ -37,33 +44,61 @@ public AspNetProcess(
string dllPath,
IDictionary<string, string> environmentVariables,
bool published = true,
bool hasListeningUri = true)
bool hasListeningUri = true,
ILogger logger = null)
{
_certificatePath = Path.Combine(workingDirectory, $"{Guid.NewGuid()}.pfx");
EnsureDevelopmentCertificates();
_output = output;
_httpClient = new HttpClient(new HttpClientHandler()
{
AllowAutoRedirect = true,
UseCookies = true,
CookieContainer = new CookieContainer(),
ServerCertificateCustomValidationCallback = (m, c, ch, p) => true,
ServerCertificateCustomValidationCallback = (request, certificate, chain, errors) =>
{
output.WriteLine($"Server certificate thumbprint: '{certificate?.Thumbprint}'");
return (certificate.Subject != "CN=localhost" && errors == SslPolicyErrors.None) || certificate?.Thumbprint == _certificateThumbprint;
},
})
{
Timeout = TimeSpan.FromMinutes(2)
};

var now = DateTimeOffset.Now;
new CertificateManager().EnsureAspNetCoreHttpsDevelopmentCertificate(now, now.AddYears(1));

output.WriteLine($"Generated certificate thumbprint: '{_certificateThumbprint}'");
output.WriteLine("Running ASP.NET application...");

var arguments = published ? $"exec {dllPath}" : "run";
Process = ProcessEx.Run(output, workingDirectory, DotNetMuxer.MuxerPathOrDefault(), arguments, envVars: environmentVariables);

logger?.LogInformation($"AspNetProcess - process: {DotNetMuxer.MuxerPathOrDefault()} arguments: {arguments}");

var finalEnvironmentVariables = new Dictionary<string, string>(environmentVariables)
{
["ASPNETCORE_Kestrel__Certificates__Default__Path"] = _certificatePath,
["ASPNETCORE_Kestrel__Certificates__Default__Password"] = _certificatePassword
};

Process = ProcessEx.Run(output, workingDirectory, DotNetMuxer.MuxerPathOrDefault(), arguments, envVars: finalEnvironmentVariables);

logger?.LogInformation("AspNetProcess - process started");

if (hasListeningUri)
{
ListeningUri = GetListeningUri(output) ?? throw new InvalidOperationException("Couldn't find the listening URL.");
logger?.LogInformation("AspNetProcess - Getting listening uri");
ListeningUri = ResolveListeningUrl(output);
logger?.LogInformation($"AspNetProcess - Got {ListeningUri}");
}
}

internal void EnsureDevelopmentCertificates()
{
var now = DateTimeOffset.Now;
var manager = new CertificateManager();
var certificate = manager.CreateAspNetCoreHttpsDevelopmentCertificate(now, now.AddYears(1), "CN=localhost");
_certificateThumbprint = certificate.Thumbprint;
manager.ExportCertificate(certificate, path: _certificatePath, includePrivateKey: true, _certificatePassword);
}

public void VisitInBrowser(IWebDriver driver)
{
_output.WriteLine($"Opening browser at {ListeningUri}...");
Expand Down Expand Up @@ -104,13 +139,13 @@ public async Task AssertPagesOk(IEnumerable<Page> pages)

public async Task ContainsLinks(Page page)
{
var response = await RequestWithRetries(client =>
var response = await RetryHelper.RetryRequest(async () =>
{
var request = new HttpRequestMessage(
HttpMethod.Get,
new Uri(ListeningUri, page.Url));
return client.SendAsync(request);
}, _httpClient);
return await _httpClient.SendAsync(request);
}, logger: NullLogger.Instance);

Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var parser = new HtmlParser();
Expand Down Expand Up @@ -143,37 +178,15 @@ public async Task ContainsLinks(Page page)
Assert.True(string.Equals(anchor.Href, expectedLink), $"Expected next link to be {expectedLink} but it was {anchor.Href}.");
var result = await RetryHelper.RetryRequest(async () =>
{
return await RequestWithRetries(client => client.GetAsync(anchor.Href), _httpClient);
return await _httpClient.GetAsync(anchor.Href);
}, logger: NullLogger.Instance);

Assert.True(IsSuccessStatusCode(result), $"{anchor.Href} is a broken link!");
}
}
}

private async Task<T> RequestWithRetries<T>(Func<HttpClient, Task<T>> requester, HttpClient client, int retries = 3, TimeSpan initialDelay = default)
{
var currentDelay = initialDelay == default ? TimeSpan.FromSeconds(30) : initialDelay;
for (int i = 0; i <= retries; i++)
{
try
{
return await requester(client);
}
catch (Exception)
{
if (i == retries)
{
throw;
}
await Task.Delay(currentDelay);
currentDelay *= 2;
}
}
throw new InvalidOperationException("Max retries reached.");
}

private Uri GetListeningUri(ITestOutputHelper output)
private Uri ResolveListeningUrl(ITestOutputHelper output)
{
// Wait until the app is accepting HTTP requests
output.WriteLine("Waiting until ASP.NET application is accepting connections...");
Expand Down Expand Up @@ -202,21 +215,27 @@ private Uri GetListeningUri(ITestOutputHelper output)

private string GetListeningMessage()
{
var buffer = new List<string>();
try
{
return Process
// This will timeout at most after 5 minutes.
.OutputLinesAsEnumerable
.Where(line => line != null)
// This used to do StartsWith, but this is less strict and can prevent issues (very rare) where
// console logging interleaves with other console output in a bad way. For example:
// dbugNow listening on: http://127.0.0.1:12857
.FirstOrDefault(line => line.Trim().Contains(ListeningMessagePrefix, StringComparison.Ordinal));
foreach (var line in Process.OutputLinesAsEnumerable)
{
if (line != null)
{
buffer.Add(line);
if (line.Trim().Contains(ListeningMessagePrefix, StringComparison.Ordinal))
{
return line;
}
}
}
}
catch (OperationCanceledException)
{
return null;
}

throw new InvalidOperationException(@$"Couldn't find listening url:
{string.Join(Environment.NewLine, buffer)}");
}

private bool IsSuccessStatusCode(HttpResponseMessage response)
Expand All @@ -232,12 +251,13 @@ public Task AssertNotFound(string requestUrl)

internal Task<HttpResponseMessage> SendRequest(string path)
{
return RequestWithRetries(client => client.GetAsync(new Uri(ListeningUri, path)), _httpClient);
return RetryHelper.RetryRequest(async () => await _httpClient.GetAsync(new Uri(ListeningUri, path)), logger: NullLogger.Instance);
}

public async Task AssertStatusCode(string requestUrl, HttpStatusCode statusCode, string acceptContentType = null)
{
var response = await RequestWithRetries(client => {
var response = await RetryHelper.RetryRequest(async () =>
{
var request = new HttpRequestMessage(
HttpMethod.Get,
new Uri(ListeningUri, requestUrl));
Expand All @@ -247,8 +267,8 @@ public async Task AssertStatusCode(string requestUrl, HttpStatusCode statusCode,
request.Headers.Add("Accept", acceptContentType);
}

return client.SendAsync(request);
}, _httpClient);
return await _httpClient.SendAsync(request);
}, logger: NullLogger.Instance);
Assert.True(statusCode == response.StatusCode, $"Expected {requestUrl} to have status '{statusCode}' but it was '{response.StatusCode}'.");
}

Expand Down
5 changes: 3 additions & 2 deletions src/ProjectTemplates/test/ProjectTemplates.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<EmbeddedResource Include="template-baselines.json" />
<Compile Include="$(SharedSourceRoot)Process\*.cs" LinkBase="shared\Process" />
<Compile Include="$(SharedSourceRoot)CertificateGeneration\**\*.cs" LinkBase="shared\CertificateGeneration" />
<Compile Include="..\Shared\**" LinkBase="Helpers" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -63,7 +64,7 @@
<_Parameter2>true</_Parameter2>
</AssemblyAttribute>
</ItemGroup>

<Target Name="PrepareForTest" BeforeTargets="CoreCompile" Condition="$(DesignTimeBuild) != true">
<PropertyGroup>
<TestTemplateCreationFolder>$([MSBuild]::NormalizePath('$(OutputPath)$(TestTemplateCreationFolder)'))</TestTemplateCreationFolder>
Expand All @@ -81,7 +82,7 @@
<_Parameter1>ArtifactsLogDir</_Parameter1>
<_Parameter2>$([MSBuild]::NormalizeDirectory('$(ArtifactsDir)', 'log'))</_Parameter2>
</AssemblyAttribute>

<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
<_Parameter1>ArtifactsNonShippingPackagesDir</_Parameter1>
<_Parameter2>$(ArtifactsNonShippingPackagesDir)</_Parameter2>
Expand Down