diff options
-rw-r--r-- | BackEnd/Timeline/Configs/ApplicationConfiguration.cs | 39 | ||||
-rw-r--r-- | BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs | 11 | ||||
-rw-r--r-- | BackEnd/Timeline/Startup.cs | 34 | ||||
-rw-r--r-- | Dockerfile | 1 |
4 files changed, 80 insertions, 5 deletions
diff --git a/BackEnd/Timeline/Configs/ApplicationConfiguration.cs b/BackEnd/Timeline/Configs/ApplicationConfiguration.cs index c7c679d1..c808e946 100644 --- a/BackEnd/Timeline/Configs/ApplicationConfiguration.cs +++ b/BackEnd/Timeline/Configs/ApplicationConfiguration.cs @@ -1,4 +1,8 @@ -namespace Timeline.Configs
+using System;
+using System.Collections.Generic;
+using Microsoft.Extensions.Configuration;
+
+namespace Timeline.Configs
{
public static class ApplicationConfiguration
{
@@ -7,5 +11,38 @@ public const string DatabaseFileName = "timeline.db";
public const string DatabaseBackupDirectoryName = "backup";
public const string FrontEndKey = "FrontEnd";
+ public const string DisableAutoBackupKey = "DisableAutoBackup";
+ public const string EnableForwardedHeadersKey = "EnableForwardedHeaders";
+ public const string ForwardedHeadersAllowedProxyHostsKey = "ForwardedHeadersAllowedProxyHosts";
+
+ public static bool CheckIsValidBoolString(string? value, string configPath, Boolean defaultValue)
+ {
+ if (value is null)
+ {
+ return defaultValue;
+ }
+
+ var true_strings = new List<string> { "true", "1", "y", "yes", "on" };
+ var false_strings = new List<string> { "false", "0", "n", "no", "off" };
+
+ if (true_strings.Contains(value.ToLowerInvariant()))
+ {
+ return true;
+ }
+ else if (false_strings.Contains(value.ToLowerInvariant()))
+ {
+ return false;
+ }
+ else
+ {
+ throw new Exception($"Invalid boolean value {value} in config {configPath}.");
+ }
+ }
+
+ public static bool GetBoolConfig(IConfiguration configuration, string configPath, bool defaultValue)
+ {
+ var value = configuration.GetValue<string?>(configPath);
+ return CheckIsValidBoolString(value, configPath, defaultValue);
+ }
}
}
diff --git a/BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs b/BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs index c3d9ac4e..a4814069 100644 --- a/BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs +++ b/BackEnd/Timeline/Services/DatabaseManagement/DatabaseManagementService.cs @@ -1,9 +1,11 @@ using Microsoft.EntityFrameworkCore;
+using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
+using Timeline.Configs;
using Timeline.Entities;
namespace Timeline.Services.DatabaseManagement
@@ -11,10 +13,12 @@ namespace Timeline.Services.DatabaseManagement public class DatabaseManagementService : IHostedService
{
private readonly IServiceProvider _serviceProvider;
+ private readonly bool _disableAutoBackup;
- public DatabaseManagementService(IServiceProvider serviceProvider)
+ public DatabaseManagementService(IServiceProvider serviceProvider, IConfiguration configuration)
{
_serviceProvider = serviceProvider;
+ _disableAutoBackup = ApplicationConfiguration.GetBoolConfig(configuration, ApplicationConfiguration.DisableAutoBackupKey, false);
}
public async Task StartAsync(CancellationToken cancellationToken = default)
@@ -27,7 +31,10 @@ namespace Timeline.Services.DatabaseManagement var customMigrator = provider.GetRequiredService<IDatabaseCustomMigrator>();
- await backupService.BackupAsync(cancellationToken);
+ if (!_disableAutoBackup)
+ {
+ await backupService.BackupAsync(cancellationToken);
+ }
await database.Database.MigrateAsync(cancellationToken);
await customMigrator.MigrateAsync(cancellationToken);
}
diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index 3f94f8a9..fa4968d7 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -33,6 +33,8 @@ namespace Timeline {
public class Startup
{
+ private readonly bool _enableForwardedHeaders;
+ private readonly string? _forwardedHeadersAllowedProxyHosts;
private readonly FrontEndMode _frontEndMode;
public Startup(IConfiguration configuration, IWebHostEnvironment environment)
@@ -51,9 +53,12 @@ namespace Timeline if (!Enum.TryParse(frontEndModeString, true, out _frontEndMode))
{
_frontEndMode = FrontEndMode.Normal;
- Console.WriteLine("Unknown FrontEnd configuaration value '{0}', fallback to normal.", frontEndModeString);
+ Console.WriteLine("Unknown FrontEnd configuration value '{0}', fallback to normal.", frontEndModeString);
}
}
+
+ _enableForwardedHeaders = ApplicationConfiguration.GetBoolConfig(configuration, ApplicationConfiguration.EnableForwardedHeadersKey, false);
+ _forwardedHeadersAllowedProxyHosts = Configuration.GetValue<string?>(ApplicationConfiguration.ForwardedHeadersAllowedProxyHostsKey);
}
public IWebHostEnvironment Environment { get; }
@@ -135,6 +140,28 @@ namespace Timeline config.RootPath = "ClientApp";
});
}
+
+ if (_enableForwardedHeaders)
+ {
+ services.Configure<ForwardedHeadersOptions>(options =>
+ {
+ options.ForwardedHeaders = Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedFor | Microsoft.AspNetCore.HttpOverrides.ForwardedHeaders.XForwardedProto;
+ if (_forwardedHeadersAllowedProxyHosts is not null)
+ {
+ options.KnownNetworks.Clear();
+ options.KnownProxies.Clear();
+ foreach (var host in _forwardedHeadersAllowedProxyHosts.Split(new char[] { ';', ',' }))
+ {
+ // Resolve host to ip
+ var ips = System.Net.Dns.GetHostAddresses(host);
+ foreach (var ip in ips)
+ {
+ options.KnownProxies.Add(ip);
+ }
+ }
+ }
+ });
+ }
}
@@ -151,6 +178,11 @@ namespace Timeline });
}
+ if (_enableForwardedHeaders)
+ {
+ app.UseForwardedHeaders();
+ }
+
app.UseOpenApi();
app.UseReDoc();
@@ -13,6 +13,5 @@ RUN dotnet publish Timeline/Timeline.csproj --configuration Release --output ./T FROM mcr.microsoft.com/dotnet/aspnet:6.0
WORKDIR /app
-ENV ASPNETCORE_FORWARDEDHEADERS_ENABLED true
COPY --from=back-build /timeline-app/Timeline/publish .
ENTRYPOINT ["dotnet", "Timeline.dll"]
|