using System.Net; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using PlotLine.Controllers; using PlotLine.Data; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.DataProtection; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; internal static class DeploymentOperationsTests { public static void Run() { foreach(var address in new[]{"203.0.113.1","192.168.10.8"}) { var repository=new Probe();var controller=Controller(address,repository); if(controller.Read(default).GetAwaiter().GetResult() is not NotFoundResult || repository.Calls!=0) throw new Exception("External health request reached the database"); } foreach(var address in new[]{"127.0.0.1","::1"}) { var repository=new Probe();var controller=Controller(address,repository); if(controller.Read(default).GetAwaiter().GetResult() is not JsonResult || repository.Calls!=1) throw new Exception("Loopback health request did not verify the database"); } var unready=new Probe{Ready=false}; if(Controller("127.0.0.1",unready).Read(default).GetAwaiter().GetResult() is not ObjectResult {StatusCode:503}) throw new Exception("Uninitialised ledger passed health"); if(Controller("127.0.0.1",new Probe{Restricted=false}).Read(default).GetAwaiter().GetResult() is not ObjectResult {StatusCode:503}) throw new Exception("Privileged runtime credentials passed deployment health"); var failed=new Probe{Fail=true}; if(Controller("127.0.0.1",failed).Read(default).GetAwaiter().GetResult() is not ObjectResult {StatusCode:503}) throw new Exception("Database failure passed health"); DataProtectionIdentity(); } private static void DataProtectionIdentity() { var temporary=Directory.CreateTempSubdirectory("pd-protection-test-").FullName; try { var oldRoot=Path.Combine(temporary,"old");var newRoot=Path.Combine(temporary,"new");var keys=Path.Combine(temporary,"keys"); Directory.CreateDirectory(oldRoot);Directory.CreateDirectory(newRoot);Directory.CreateDirectory(keys); ServiceProvider Services(string root,string? applicationName=null) { var builder=WebApplication.CreateBuilder(new WebApplicationOptions{ContentRootPath=root,EnvironmentName="Qualification"}); var protection=builder.Services.AddDataProtection().PersistKeysToFileSystem(new DirectoryInfo(keys)); if(applicationName is not null)protection.SetApplicationName(applicationName); return builder.Services.BuildServiceProvider(); } using var original=Services(oldRoot); var identity=original.GetRequiredService>().Value.ApplicationDiscriminator; if(identity!=oldRoot+Path.DirectorySeparatorChar)throw new Exception("Hosting content-root identity differs from the reviewed assumption"); var purpose=new[]{"Microsoft.AspNetCore.Authentication.Cookies.CookieAuthenticationMiddleware","Cookies","v2"}; var protectedValue=original.GetRequiredService().CreateProtector(purpose).Protect("synthetic compatibility probe"); using var moved=Services(newRoot); var failed=false; try{moved.GetRequiredService().CreateProtector(purpose).Unprotect(protectedValue);} catch(System.Security.Cryptography.CryptographicException){failed=true;} if(!failed)throw new Exception("Different default roots unexpectedly shared an application identity"); using var pinned=Services(newRoot,identity); if(pinned.GetRequiredService().CreateProtector(purpose).Unprotect(protectedValue)!="synthetic compatibility probe") throw new Exception("Explicit stable Data Protection identity did not preserve the synthetic payload"); } finally{Directory.Delete(temporary,true);} } private static DeploymentHealthController Controller(string address,Probe repository) { var http=new DefaultHttpContext();http.Connection.RemoteIpAddress=IPAddress.Parse(address); return new DeploymentHealthController(repository,new TestEnvironment()){ControllerContext=new ControllerContext{HttpContext=http}}; } private sealed class TestEnvironment:Microsoft.AspNetCore.Hosting.IWebHostEnvironment { public string EnvironmentName {get;set;}="Production"; public string ApplicationName {get;set;}="PlotLine"; public string ContentRootPath {get;set;}="/unused"; public string WebRootPath {get;set;}="/unused/wwwroot"; public Microsoft.Extensions.FileProviders.IFileProvider ContentRootFileProvider {get;set;}=new Microsoft.Extensions.FileProviders.NullFileProvider(); public Microsoft.Extensions.FileProviders.IFileProvider WebRootFileProvider {get;set;}=new Microsoft.Extensions.FileProviders.NullFileProvider(); } private sealed class Probe:IDeploymentHealthRepository { public int Calls;public bool Ready=true;public bool Restricted=true;public bool Fail; public Task ReadAsync(CancellationToken cancellationToken) {Calls++;if(Fail)throw new InvalidOperationException("Private diagnostic must not reach HTTP");return Task.FromResult(new DeploymentDatabaseHealth("Isolated",Ready,Restricted));} } }