From 5ea1ae0400d6410e03fcc8ca45ffa0676c5394a2 Mon Sep 17 00:00:00 2001 From: areller Date: Fri, 17 Jul 2020 12:19:32 -0400 Subject: [PATCH] Fix static file loading when using ingress with host (#591) * fix static files loading when using host in ingress * formatting * formatting * fixing failing tests * fix tests --- src/Microsoft.Tye.Hosting/HttpProxyService.cs | 12 ++----- test/E2ETest/TyeRunTests.cs | 30 ++++++++++++++++ .../ApplicationC-UI/ApplicationC-UI.csproj | 7 ++++ .../ApplicationC-UI/Program.cs | 26 ++++++++++++++ .../Properties/launchSettings.json | 27 +++++++++++++++ .../ApplicationC-UI/Startup.cs | 34 +++++++++++++++++++ .../appsettings.Development.json | 9 +++++ .../ApplicationC-UI/appsettings.json | 10 ++++++ .../ApplicationC-UI/wwwroot/index.html | 1 + .../projects/apps-with-ingress/tye-ui.yaml | 19 +++++++++++ .../projects/apps-with-ingress/tye.yaml | 4 +-- 11 files changed, 167 insertions(+), 12 deletions(-) create mode 100644 test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/ApplicationC-UI.csproj create mode 100644 test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Program.cs create mode 100644 test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Properties/launchSettings.json create mode 100644 test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Startup.cs create mode 100644 test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/appsettings.Development.json create mode 100644 test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/appsettings.json create mode 100644 test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/wwwroot/index.html create mode 100644 test/E2ETest/testassets/projects/apps-with-ingress/tye-ui.yaml diff --git a/src/Microsoft.Tye.Hosting/HttpProxyService.cs b/src/Microsoft.Tye.Hosting/HttpProxyService.cs index 12d2291d..62696fe3 100644 --- a/src/Microsoft.Tye.Hosting/HttpProxyService.cs +++ b/src/Microsoft.Tye.Hosting/HttpProxyService.cs @@ -161,16 +161,8 @@ namespace Microsoft.Tye.Hosting await context.ProxyRequest(invoker, uri.Uri); }; - IEndpointConventionBuilder conventions = null!; - - if (rule.Path != null) - { - conventions = ((IEndpointRouteBuilder)webApp).Map(rule.Path.TrimEnd('/') + "/{**path}", del); - } - else - { - conventions = webApp.MapFallback(del); - } + IEndpointConventionBuilder conventions = + ((IEndpointRouteBuilder)webApp).Map((rule.Path?.TrimEnd('/') ?? "") + "/{**path}", del); if (rule.Host != null) { diff --git a/test/E2ETest/TyeRunTests.cs b/test/E2ETest/TyeRunTests.cs index 270b90d9..0ec26db1 100644 --- a/test/E2ETest/TyeRunTests.cs +++ b/test/E2ETest/TyeRunTests.cs @@ -648,6 +648,36 @@ services: }); } + [Fact] + public async Task IngressStaticFilesTest() + { + using var projectDirectory = CopyTestProjectDirectory("apps-with-ingress"); + + var projectFile = new FileInfo(Path.Combine(projectDirectory.DirectoryPath, "tye-ui.yaml")); + var outputContext = new OutputContext(_sink, Verbosity.Debug); + var application = await ApplicationFactory.CreateAsync(outputContext, projectFile); + + var handler = new HttpClientHandler + { + ServerCertificateCustomValidationCallback = (a, b, c, d) => true, + AllowAutoRedirect = true + }; + + var client = new HttpClient(new RetryHandler(handler)); + + await RunHostingApplication(application, new HostOptions(), async (app, uri) => + { + var ingressUri = await GetServiceUrl(client, uri, "ingress"); + + var htmlRequest = new HttpRequestMessage(HttpMethod.Get, + ingressUri + "/index.html"); + htmlRequest.Headers.Host = "ui.example.com"; + + var htmlResponse = await client.SendAsync(htmlRequest); + htmlResponse.EnsureSuccessStatusCode(); + }); + } + [ConditionalFact] [SkipIfDockerNotRunning] public async Task NginxIngressTest() diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/ApplicationC-UI.csproj b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/ApplicationC-UI.csproj new file mode 100644 index 00000000..92605c5a --- /dev/null +++ b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/ApplicationC-UI.csproj @@ -0,0 +1,7 @@ + + + + netcoreapp3.1 + + + diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Program.cs b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Program.cs new file mode 100644 index 00000000..2da3ef28 --- /dev/null +++ b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Program.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace ApplicationC_UI +{ + public class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + webBuilder.UseStartup(); + }); + } +} diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Properties/launchSettings.json b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Properties/launchSettings.json new file mode 100644 index 00000000..2dc78ee3 --- /dev/null +++ b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Properties/launchSettings.json @@ -0,0 +1,27 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:28611", + "sslPort": 44364 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "ApplicationC_UI": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "https://localhost:5001;http://localhost:5000", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Startup.cs b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Startup.cs new file mode 100644 index 00000000..d4260bbd --- /dev/null +++ b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/Startup.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + +namespace ApplicationC_UI +{ + public class Startup + { + // This method gets called by the runtime. Use this method to add services to the container. + // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 + public void ConfigureServices(IServiceCollection services) + { + } + + // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. + public void Configure(IApplicationBuilder app, IWebHostEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + + app.UseRouting(); + + app.UseStaticFiles(); + } + } +} diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/appsettings.Development.json b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/appsettings.Development.json new file mode 100644 index 00000000..8983e0fc --- /dev/null +++ b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/appsettings.json b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/appsettings.json new file mode 100644 index 00000000..d9d9a9bf --- /dev/null +++ b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/appsettings.json @@ -0,0 +1,10 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft": "Warning", + "Microsoft.Hosting.Lifetime": "Information" + } + }, + "AllowedHosts": "*" +} diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/wwwroot/index.html b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/wwwroot/index.html new file mode 100644 index 00000000..70c379b6 --- /dev/null +++ b/test/E2ETest/testassets/projects/apps-with-ingress/ApplicationC-UI/wwwroot/index.html @@ -0,0 +1 @@ +Hello world \ No newline at end of file diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/tye-ui.yaml b/test/E2ETest/testassets/projects/apps-with-ingress/tye-ui.yaml new file mode 100644 index 00000000..3506452c --- /dev/null +++ b/test/E2ETest/testassets/projects/apps-with-ingress/tye-ui.yaml @@ -0,0 +1,19 @@ +# tye application configuration file +# read all about it at https://github.com/dotnet/tye +# +# when you've given us a try, we'd love to know what you think: +# https://aka.ms/AA7q20u +# +name: apps-with-ingress-ui +ingress: + - name: ingress + bindings: + - port: 8080 + rules: + - host: ui.example.com + service: appC-ui + +services: + - name: appC-ui + project: ApplicationC-UI/ApplicationC-UI.csproj + replicas: 2 \ No newline at end of file diff --git a/test/E2ETest/testassets/projects/apps-with-ingress/tye.yaml b/test/E2ETest/testassets/projects/apps-with-ingress/tye.yaml index 304a2177..b334617b 100644 --- a/test/E2ETest/testassets/projects/apps-with-ingress/tye.yaml +++ b/test/E2ETest/testassets/projects/apps-with-ingress/tye.yaml @@ -17,7 +17,7 @@ ingress: - host: a.example.com service: appA - host: b.example.com - service: appB + service: appB services: - name: appA @@ -25,4 +25,4 @@ services: replicas: 2 - name: appB project: ApplicationB/ApplicationB.csproj - replicas: 2 + replicas: 2 \ No newline at end of file