using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using StellaOps.Authority.Plugin.Standard.Storage; namespace StellaOps.Authority.Plugin.Standard.Bootstrap; internal sealed class StandardPluginBootstrapper : IHostedService { private readonly string pluginName; private readonly IServiceScopeFactory scopeFactory; private readonly ILogger logger; public StandardPluginBootstrapper( string pluginName, IServiceScopeFactory scopeFactory, ILogger logger) { this.pluginName = pluginName; this.scopeFactory = scopeFactory; this.logger = logger; } public async Task StartAsync(CancellationToken cancellationToken) { using var scope = scopeFactory.CreateScope(); var optionsMonitor = scope.ServiceProvider.GetRequiredService>(); var credentialStore = scope.ServiceProvider.GetRequiredService(); var options = optionsMonitor.Get(pluginName); if (options.BootstrapUser is null || !options.BootstrapUser.IsConfigured) { return; } logger.LogInformation("Standard Authority plugin '{PluginName}' ensuring bootstrap user.", pluginName); await credentialStore.EnsureBootstrapUserAsync(options.BootstrapUser, cancellationToken).ConfigureAwait(false); } public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; }