Tweaks Round #1

This commit is contained in:
AffectedArc07
2021-06-08 16:41:43 +01:00
parent c4cd4aace5
commit f1f1f0b312
7 changed files with 43 additions and 35 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
<Import Project="ControlPanelVersion.props" />
<PropertyGroup>
<TgsCoreVersion>4.12.0</TgsCoreVersion>
<TgsConfigVersion>3.0.1</TgsConfigVersion>
<TgsConfigVersion>3.1.0</TgsConfigVersion>
<TgsApiVersion>9.0.1</TgsApiVersion>
<TgsApiLibraryVersion>9.0.0</TgsApiLibraryVersion>
<TgsClientVersion>10.0.0</TgsClientVersion>
@@ -10,11 +10,6 @@
/// </summary>
public const string Section = "Elasticsearch";
/// <summary>
/// Default value of <see cref="Enable"/>.
/// </summary>
const bool DefaultEnable = false;
/// <summary>
/// Default value of <see cref="Host"/>.
/// </summary>
@@ -33,7 +28,7 @@
/// <summary>
/// Do we want to enable elasticsearch or not?.
/// </summary>
public bool Enable { get; set; } = DefaultEnable;
public bool Enable { get; set; }
/// <summary>
/// The host of the elasticsearch endpoint.
@@ -120,6 +120,7 @@ namespace Tgstation.Server.Host.Core
services.UseStandardConfig<UpdatesConfiguration>(Configuration);
services.UseStandardConfig<ControlPanelConfiguration>(Configuration);
services.UseStandardConfig<SwarmConfiguration>(Configuration);
services.UseStandardConfig<ElasticsearchConfiguration>(Configuration);
// enable options which give us config reloading
services.AddOptions();
@@ -151,7 +152,6 @@ namespace Tgstation.Server.Host.Core
config.MinimumLevel.Override("System.Net.Http.HttpClient", microsoftEventLevel.Value);
}
},
Configuration,
sinkConfig =>
{
if (postSetupServices.FileLoggingConfiguration.Disable)
@@ -179,7 +179,7 @@ namespace Tgstation.Server.Host.Core
flushToDiskInterval: TimeSpan.FromSeconds(2),
rollingInterval: RollingInterval.Day,
rollOnFileSizeLimit: true);
});
}, postSetupServices.ElasticsearchConfiguration);
// configure bearer token validation
services
@@ -3,13 +3,13 @@ using System.Diagnostics;
using System.Globalization;
using Elastic.CommonSchema.Serilog;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using Serilog.Configuration;
using Serilog.Sinks.Elasticsearch;
using Tgstation.Server.Host.Configuration;
namespace Tgstation.Server.Host.Extensions
@@ -59,14 +59,14 @@ namespace Tgstation.Server.Host.Extensions
/// </summary>
/// <param name="serviceCollection">The <see cref="IServiceCollection"/> to configure.</param>
/// <param name="configurationAction">Additional configuration for a given <see cref="LoggerConfiguration"/>.</param>
/// <param name="tgsConfiguration">The service configuration for a given <see cref="IConfiguration"/>.</param>
/// <param name="sinkConfigurationAction">Additional configuration for a given <see cref="LoggerSinkConfiguration"/>.</param>
/// <param name="esc">Configuration for a given <see cref="ElasticsearchConfiguration"/>.</param>
/// <returns>The updated <paramref name="serviceCollection"/>.</returns>
public static IServiceCollection SetupLogging(
this IServiceCollection serviceCollection,
Action<LoggerConfiguration> configurationAction,
IConfiguration tgsConfiguration,
Action<LoggerSinkConfiguration> sinkConfigurationAction = null)
Action<LoggerSinkConfiguration> sinkConfigurationAction = null,
ElasticsearchConfiguration esc = null)
=> serviceCollection.AddLogging(builder =>
{
builder.ClearProviders();
@@ -89,28 +89,24 @@ namespace Tgstation.Server.Host.Extensions
sinkConfigurationAction?.Invoke(sinkConfiguration);
});
string elasticsearchEndpoint = tgsConfiguration.GetSection("Elasticsearch").GetSection("Host").Value;
string elasticsearchUser = tgsConfiguration.GetSection("Elasticsearch").GetSection("Username").Value;
string elasticsearchPassword = tgsConfiguration.GetSection("Elasticsearch").GetSection("Password").Value;
string raw_bool = tgsConfiguration.GetSection("Elasticsearch").GetSection("Enable").Value;
bool elasticsearchEnabled = false;
if (!string.IsNullOrEmpty(raw_bool))
elasticsearchEnabled = bool.Parse(raw_bool);
if (elasticsearchEnabled)
if (esc != null)
{
if (elasticsearchEndpoint == null)
throw new InvalidOperationException("Elasticsearch endpoint is null!");
configuration.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(elasticsearchEndpoint))
if (esc.Enable)
{
ModifyConnectionSettings = x => (!string.IsNullOrEmpty(elasticsearchUser) && !string.IsNullOrEmpty(elasticsearchPassword)) ? x.BasicAuthentication(elasticsearchUser, elasticsearchPassword) : null,
CustomFormatter = new EcsTextFormatter(),
AutoRegisterTemplate = true,
AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
IndexFormat = "tgs4-logs",
});
if (esc.Host == null)
throw new InvalidOperationException("Elasticsearch endpoint is null!");
configuration.WriteTo.Elasticsearch(new ElasticsearchSinkOptions(new Uri(esc.Host))
{
// Yes I know this means they cannot use a self signed cert unless they also have authentication, but lets be real here
// No one is going to be doing one of thsoe but not the other
ModifyConnectionSettings = x => (!string.IsNullOrEmpty(esc.Username) && !string.IsNullOrEmpty(esc.Password)) ? x.BasicAuthentication(esc.Username, esc.Password).ServerCertificateValidationCallback((o, certificate, arg3, arg4) => { return true; }) : null,
CustomFormatter = new EcsTextFormatter(),
AutoRegisterTemplate = true,
AutoRegisterTemplateVersion = AutoRegisterTemplateVersion.ESv7,
IndexFormat = "tgs4-logs",
});
}
}
builder.AddSerilog(configuration.CreateLogger(), true);
@@ -28,6 +28,11 @@ namespace Tgstation.Server.Host.Setup
/// </summary>
FileLoggingConfiguration FileLoggingConfiguration { get; }
/// <summary>
/// The <see cref="Configuration.ElasticsearchConfiguration"/>.
/// </summary>
ElasticsearchConfiguration ElasticsearchConfiguration { get; }
/// <summary>
/// The <see cref="IPlatformIdentifier"/>.
/// </summary>
@@ -29,6 +29,9 @@ namespace Tgstation.Server.Host.Setup
/// <inheritdoc />
public ILogger<TLoggerType> Logger { get; }
/// <inheritdoc />
public ElasticsearchConfiguration ElasticsearchConfiguration => elasticsearchConfigurationOptions.Value;
/// <summary>
/// Backing <see cref="IOptions{TOptions}"/> for <see cref="GeneralConfiguration"/>.
/// </summary>
@@ -49,6 +52,11 @@ namespace Tgstation.Server.Host.Setup
/// </summary>
readonly IOptions<FileLoggingConfiguration> fileLoggingConfigurationOptions;
/// <summary>
/// Backing <see cref="IOptions{TOptions}"/> for <see cref="ElasticsearchConfiguration"/>.
/// </summary>
readonly IOptions<ElasticsearchConfiguration> elasticsearchConfigurationOptions;
/// <summary>
/// Initializes a new instance of the <see cref="PostSetupServices{TLoggerType}"/> class.
/// </summary>
@@ -58,13 +66,15 @@ namespace Tgstation.Server.Host.Setup
/// <param name="databaseConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="DatabaseConfiguration"/>.</param>
/// <param name="securityConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="SecurityConfiguration"/>.</param>
/// <param name="fileLoggingConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="FileLoggingConfiguration"/>.</param>
/// <param name="elasticsearchConfigurationOptions">The <see cref="IOptions{TOptions}"/> containing the value of <see cref="ElasticsearchConfiguration"/>.</param>
public PostSetupServices(
IPlatformIdentifier platformIdentifier,
ILoggerFactory loggerFactory,
IOptions<GeneralConfiguration> generalConfigurationOptions,
IOptions<DatabaseConfiguration> databaseConfigurationOptions,
IOptions<SecurityConfiguration> securityConfigurationOptions,
IOptions<FileLoggingConfiguration> fileLoggingConfigurationOptions)
IOptions<FileLoggingConfiguration> fileLoggingConfigurationOptions,
IOptions<ElasticsearchConfiguration> elasticsearchConfigurationOptions)
{
PlatformIdentifier = platformIdentifier ?? throw new ArgumentNullException(nameof(platformIdentifier));
if (loggerFactory == null)
@@ -75,6 +85,7 @@ namespace Tgstation.Server.Host.Setup
this.databaseConfigurationOptions = databaseConfigurationOptions ?? throw new ArgumentNullException(nameof(databaseConfigurationOptions));
this.securityConfigurationOptions = securityConfigurationOptions ?? throw new ArgumentNullException(nameof(securityConfigurationOptions));
this.fileLoggingConfigurationOptions = fileLoggingConfigurationOptions ?? throw new ArgumentNullException(nameof(fileLoggingConfigurationOptions));
this.elasticsearchConfigurationOptions = elasticsearchConfigurationOptions ?? throw new ArgumentNullException(nameof(elasticsearchConfigurationOptions));
}
}
}
@@ -52,7 +52,7 @@ namespace Tgstation.Server.Host.Setup
if (services == null)
throw new ArgumentNullException(nameof(services));
services.SetupLogging(config => config.MinimumLevel.Override("Microsoft", LogEventLevel.Warning), Configuration);
services.SetupLogging(config => config.MinimumLevel.Override("Microsoft", LogEventLevel.Warning));
services.AddSingleton(IOManager);
services.AddSingleton(AssemblyInformationProvider);
@@ -62,6 +62,7 @@ namespace Tgstation.Server.Host.Setup
services.AddSingleton<IPlatformIdentifier, PlatformIdentifier>();
services.AddSingleton<IAsyncDelayer, AsyncDelayer>();
// these configs are what's injected into PostSetupServices
services.UseStandardConfig<GeneralConfiguration>(Configuration);
services.UseStandardConfig<DatabaseConfiguration>(Configuration);
services.UseStandardConfig<SecurityConfiguration>(Configuration);