From 69b1ac243eabb009f0d006caa6d7786f0fd01fa4 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 27 Jul 2024 17:45:04 +0000 Subject: [PATCH] fix: Ensure spark config init and login are only performed once As a static function that could be called from anywhere in the code (directly or indirectly), 'Configuration.Initialize' was vulnerable to race bugs. The "Configuration.get()" method, whose calls are ubiquitous in our code, would initialize config if it hadn't been done so already. Without a reference to the configServer param, this param would effectively be ignored/overridden with null. It's important that login only be performed once, for obvious reasons (our exes that connect to sparkd are generally designed to process data for a single user). Furthermore, not every routine that requires a successful login has a reference to the credentials needed to do it. To address this, we now pass the ISparkdLoginService singleton to these routines so they can wait until the singular login task has completed before they begin. --- .../spark_api/Services/SparkdLoginService.cs | 31 +++++++++----- .../spark_core/Services/SparkConfigLoader.cs | 29 ++++++++++--- src/client/spark_core/config/Configuration.cs | 41 ++++++++++++++++++- 3 files changed, 85 insertions(+), 16 deletions(-) diff --git a/src/client/spark_api/Services/SparkdLoginService.cs b/src/client/spark_api/Services/SparkdLoginService.cs index cf30c74f0..bf038dc81 100644 --- a/src/client/spark_api/Services/SparkdLoginService.cs +++ b/src/client/spark_api/Services/SparkdLoginService.cs @@ -7,6 +7,8 @@ using bts.api.core; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Options; +using Spark.Core.Services; + using System; using System.Threading; using System.Threading.Tasks; @@ -18,44 +20,53 @@ using System.Threading.Tasks; public class SparkdLoginService : IHostedService, ISparkdLoginService { readonly ServiceContext serviceContext; readonly SparkApiCredentials config; + readonly ISparkConfigLoader sparkConfigLoader; readonly object lockObj = new(); LoginResult? loginResult; Task? loginTask; - public SparkdLoginService(ServiceContext serviceContext, IOptions config) - : this(serviceContext, config.Value) { } + public SparkdLoginService(ServiceContext serviceContext, IOptions config, ISparkConfigLoader sparkConfigLoader) + : this(serviceContext, config.Value, sparkConfigLoader) { } - public SparkdLoginService(ServiceContext serviceContext, SparkApiCredentials config) { + public SparkdLoginService(ServiceContext serviceContext, SparkApiCredentials config, ISparkConfigLoader sparkConfigLoader) { this.serviceContext = serviceContext; this.config = config; + this.sparkConfigLoader = sparkConfigLoader; } - public ValueTask LoginAsync() { + public async ValueTask LoginAsync() { + Log.Gen.info("SparkdLoginService.LoginAsync"); + await sparkConfigLoader.InitializeAsync(CancellationToken.None); if (loginResult is not null) { - return ValueTask.FromResult(loginResult); + return loginResult; } if (string.IsNullOrEmpty(config.User)) { - return ValueTask.FromException(new InvalidOperationException("User is null/empty")); + Log.Gen.warn("SparkdLoginService.LoginAsync skipping because config.User is null"); + throw new InvalidOperationException("User is null/empty"); } lock (lockObj) { if (loginResult is not null) { - return ValueTask.FromResult(loginResult); + return loginResult; } loginTask ??= LoginImplAsync(); - return new(loginTask); } + return await loginTask; async Task LoginImplAsync() { + Log.Gen.warn($"SparkdLoginService.LoginAsync logging in now. user: {config.User}"); loginResult = await serviceContext.login(config.User ?? string.Empty, config.Password ?? string.Empty); if (!loginResult.success) { Log.Gen.error($"Failed to login to sparkd. reason: {loginResult.reason}"); } + Log.Gen.info($"SparkdLoginService.LoginAsync successfully logged. user: {config.User}"); return loginResult; } } - public Task StartAsync(CancellationToken cancellationToken) => - LoginAsync().AsTask(); + public Task StartAsync(CancellationToken cancellationToken) { + Log.Gen.info("SparkdLoginService.StartAsync"); + return LoginAsync().AsTask(); + } public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; diff --git a/src/client/spark_core/Services/SparkConfigLoader.cs b/src/client/spark_core/Services/SparkConfigLoader.cs index 64ae890c6..ed5a7e736 100644 --- a/src/client/spark_core/Services/SparkConfigLoader.cs +++ b/src/client/spark_core/Services/SparkConfigLoader.cs @@ -5,8 +5,10 @@ using bts; using mbts.config; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using System; using System.Threading; using System.Threading.Tasks; @@ -15,13 +17,30 @@ using System.Threading.Tasks; /// public class SparkConfigLoader : ISparkConfigLoader { readonly SparkConfigLoaderOpts config; + readonly ILogger? logger; - public SparkConfigLoader(IOptions config) - : this(config.Value) { } + public SparkConfigLoader(IOptions config, ILogger? logger = null) + : this(config.Value, logger) { } - public SparkConfigLoader(SparkConfigLoaderOpts config) => + public SparkConfigLoader(SparkConfigLoaderOpts config, ILogger? logger = null) { this.config = config; + this.logger = logger; + } - public async Task InitializeAsync(CancellationToken cancellationToken) => - Log.assert(await Configuration.InitializeAsync(config.ConfigServer, cancellationToken)); + public async Task InitializeAsync(CancellationToken cancellationToken) { + bool success = false; + logger?.LogInformation("Initializing Spark config. ConfigServer: {ConfigServer}", config.ConfigServer); + var task = Configuration.InitializeAsync(config.ConfigServer, cancellationToken); + if (task.IsCompleted) { + // logger?.LogWarning("Spark config already initialized."); + success = task.IsCompletedSuccessfully && task.Result; + } else { + try { + success = await task; + } catch (Exception ex) { + logger?.LogError("Failed to load Spark config. ConfigServer: {ConfigServer}; Exception:\n{Exception}", config.ConfigServer, ex); + } + } + Log.assert(success, $"Failed to load Spark config. ConfigServer: {config.ConfigServer}"); + } } diff --git a/src/client/spark_core/config/Configuration.cs b/src/client/spark_core/config/Configuration.cs index e2067ba70..588bfb940 100644 --- a/src/client/spark_core/config/Configuration.cs +++ b/src/client/spark_core/config/Configuration.cs @@ -53,6 +53,45 @@ namespace mbts.config { } } + public static ValueTask UntilInitializedAsync() { + var cts = new CancellationTokenSource(); + var task = UntilInitializedAsync(cts.Token); + cts.CancelAfter(TimeSpan.FromSeconds(5)); + return task; + } + + public static ValueTask UntilInitializedAsync(CancellationToken cancellationToken) { + switch (_state) { + case State.OK: + return ValueTask.FromResult(true); + case State.Error: + return ValueTask.FromResult(false); + default: + break; + } + return new(Task.Run(Impl)); + + bool Impl() { + const int MAX_ATTEMPTS = 100; + const int SLEEP_MS = 100; + for (int i = 0; i < MAX_ATTEMPTS; i++) { + switch (_state) { + case State.OK: + return true; + case State.Error: + return false; + default: + if (cancellationToken.IsCancellationRequested) { + return false; + } + break; + } + Thread.Sleep(SLEEP_MS); + } + return false; + } + } + public static ValueTask InitializeAsync(string? configSource = null) => InitializeAsync(configSource, CancellationToken.None); @@ -179,7 +218,7 @@ namespace mbts.config { static byte[]? _get_buffer(string key_name) { switch (_state) { case State.Uninitialized: - if (!InitializeAsync().GetAwaiter().GetResult()) { + if (!UntilInitializedAsync().AsTask().GetAwaiter().GetResult()) { return null; } break;