From 6545ae76b05c5e5ff9f268b8d8d60adde79fed10 Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Thu, 1 Nov 2018 11:42:49 -0400 Subject: [PATCH] Update setup wizard, don't inject CORS if not necessary, add additional logging --- src/Tgstation.Server.Host/Core/Application.cs | 29 +++++++------ src/Tgstation.Server.Host/Core/SetupWizard.cs | 42 ++++++++++++++++--- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/src/Tgstation.Server.Host/Core/Application.cs b/src/Tgstation.Server.Host/Core/Application.cs index 6e0e9e7fff..f9e3da3f18 100644 --- a/src/Tgstation.Server.Host/Core/Application.cs +++ b/src/Tgstation.Server.Host/Core/Application.cs @@ -114,6 +114,7 @@ namespace Tgstation.Server.Host.Core GeneralConfiguration generalConfiguration; DatabaseConfiguration databaseConfiguration; FileLoggingConfiguration fileLoggingConfiguration; + ControlPanelConfiguration controlPanelConfiguration; IIOManager ioManager; IPlatformIdentifier platformIdentifier; @@ -142,6 +143,9 @@ namespace Tgstation.Server.Host.Core var loggingOptions = provider.GetRequiredService>(); fileLoggingConfiguration = loggingOptions.Value; + var controlPanelOptions = provider.GetRequiredService>(); + controlPanelConfiguration = controlPanelOptions.Value; + ioManager = provider.GetRequiredService(); platformIdentifier = provider.GetRequiredService(); } @@ -223,8 +227,9 @@ namespace Tgstation.Server.Host.Core options.SerializerSettings.Converters = new[] { new VersionConverter() }; }); - //enable CORS - services.AddCors(); + //enable CORS if necessary + if (controlPanelConfiguration.AllowAnyOrigin || controlPanelConfiguration.AllowedOrigins?.Count > 0) + services.AddCors(); void AddTypedContext() where TContext : DatabaseContext { @@ -346,23 +351,21 @@ namespace Tgstation.Server.Host.Core //Set up CORS based on configuration if necessary Action corsBuilder = null; - - void AllowAnyOrginCors(CorsPolicyBuilder builder) => builder.AllowAnyOrigin(); - void AllowConfiguredOriginsCors(CorsPolicyBuilder builder) => builder.WithOrigins(); - if (controlPanelConfiguration.AllowAnyOrigin) - corsBuilder = AllowAnyOrginCors; + { + logger.LogTrace("Access-Control-Allow-Origin: *"); + corsBuilder = builder => builder.AllowAnyOrigin(); + } else if (controlPanelConfiguration.AllowedOrigins?.Count > 0) - corsBuilder = AllowConfiguredOriginsCors; + { + logger.LogTrace("Access-Control-Allow-Origin: ", String.Join(',', controlPanelConfiguration.AllowedOrigins)); + corsBuilder = builder => builder.WithOrigins(controlPanelConfiguration.AllowedOrigins.ToArray()); + } if (corsBuilder != null) { var originalBuilder = corsBuilder; - corsBuilder = builder => - { - originalBuilder(builder); - builder.AllowAnyHeader().AllowAnyMethod(); - }; + corsBuilder = builder => originalBuilder(builder.AllowAnyHeader().AllowAnyMethod()); applicationBuilder.UseCors(corsBuilder); } diff --git a/src/Tgstation.Server.Host/Core/SetupWizard.cs b/src/Tgstation.Server.Host/Core/SetupWizard.cs index d3009cdf66..c06d742b5d 100644 --- a/src/Tgstation.Server.Host/Core/SetupWizard.cs +++ b/src/Tgstation.Server.Host/Core/SetupWizard.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Data.Common; using System.Data.SqlClient; using System.Globalization; +using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -327,8 +328,6 @@ namespace Tgstation.Server.Host.Core SetupWizardMode = SetupWizardMode.Never }; - newGeneralConfiguration.UseWebControlPanel = await PromptYesNo("Enable the web control panel? (y/n): ", cancellationToken).ConfigureAwait(false); - do { await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); @@ -434,7 +433,7 @@ namespace Tgstation.Server.Host.Core { await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); await console.WriteAsync(question, true, cancellationToken).ConfigureAwait(false); - await console.WriteAsync(String.Format(CultureInfo.InvariantCulture, "Enter one of {0}/{1}/{2}/{3}/{4}/{5} (leave blank for default): ",nameof(LogLevel.Trace), nameof(LogLevel.Debug), nameof(LogLevel.Information), nameof(LogLevel.Warning), nameof(LogLevel.Error), nameof(LogLevel.Critical)), false, cancellationToken).ConfigureAwait(false); + await console.WriteAsync(String.Format(CultureInfo.InvariantCulture, "Enter one of {0}/{1}/{2}/{3}/{4}/{5} (leave blank for default): ", nameof(LogLevel.Trace), nameof(LogLevel.Debug), nameof(LogLevel.Information), nameof(LogLevel.Warning), nameof(LogLevel.Error), nameof(LogLevel.Critical)), false, cancellationToken).ConfigureAwait(false); var responseString = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false); if (String.IsNullOrWhiteSpace(responseString)) return null; @@ -450,6 +449,33 @@ namespace Tgstation.Server.Host.Core return fileLoggingConfiguration; } + /// + /// Prompts the user to create a + /// + /// The for the operation + /// A resulting in the new + async Task ConfigureControlPanel(CancellationToken cancellationToken) + { + var config = new ControlPanelConfiguration + { + Enable = await PromptYesNo("Enable the web control panel? (y/n): ", cancellationToken).ConfigureAwait(false), + AllowAnyOrigin = await PromptYesNo("Allow web control panels hosted elsewhere to access the server? (Access-Control-Allow-Origin: *) (y/n): ", cancellationToken).ConfigureAwait(false) + }; + + if (!config.AllowAnyOrigin) + { + await console.WriteAsync("Enter a comma seperated list of CORS allowed origins (optional): ", false, cancellationToken).ConfigureAwait(false); + var commaSeperatedOrigins = await console.ReadLineAsync(false, cancellationToken).ConfigureAwait(false); + if (!String.IsNullOrWhiteSpace(commaSeperatedOrigins)) + { + var splits = commaSeperatedOrigins.Split(','); + config.AllowedOrigins = new List(splits.Select(x => x.Trim())); + } + } + + return config; + } + /// /// Saves a given set to /// @@ -458,9 +484,10 @@ namespace Tgstation.Server.Host.Core /// The to save /// The to save /// The to save + /// The to save /// The for the operation /// A representing the running operation - async Task SaveConfiguration(string userConfigFileName, ushort? hostingPort, DatabaseConfiguration databaseConfiguration, GeneralConfiguration newGeneralConfiguration, FileLoggingConfiguration fileLoggingConfiguration, CancellationToken cancellationToken) + async Task SaveConfiguration(string userConfigFileName, ushort? hostingPort, DatabaseConfiguration databaseConfiguration, GeneralConfiguration newGeneralConfiguration, FileLoggingConfiguration fileLoggingConfiguration, ControlPanelConfiguration controlPanelConfiguration, CancellationToken cancellationToken) { await console.WriteAsync(String.Format(CultureInfo.InvariantCulture, "Configuration complete! Saving to {0}", userConfigFileName), true, cancellationToken).ConfigureAwait(false); @@ -468,7 +495,8 @@ namespace Tgstation.Server.Host.Core { { DatabaseConfiguration.Section, databaseConfiguration }, { GeneralConfiguration.Section, newGeneralConfiguration }, - { FileLoggingConfiguration.Section, fileLoggingConfiguration } + { FileLoggingConfiguration.Section, fileLoggingConfiguration }, + { ControlPanelConfiguration.Section, controlPanelConfiguration } }; if (hostingPort.HasValue) @@ -534,9 +562,11 @@ namespace Tgstation.Server.Host.Core var fileLoggingConfiguration = await ConfigureLogging(cancellationToken).ConfigureAwait(false); + var controlPanelConfiguration = await ConfigureControlPanel(cancellationToken).ConfigureAwait(false); + await console.WriteAsync(null, true, cancellationToken).ConfigureAwait(false); - await SaveConfiguration(userConfigFileName, hostingPort, databaseConfiguration, newGeneralConfiguration, fileLoggingConfiguration, cancellationToken).ConfigureAwait(false); + await SaveConfiguration(userConfigFileName, hostingPort, databaseConfiguration, newGeneralConfiguration, fileLoggingConfiguration, controlPanelConfiguration, cancellationToken).ConfigureAwait(false); } ///