using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using StellaOps.Scanner.Storage; namespace StellaOps.Scanner.WebService.Options; internal sealed class ScannerStorageOptionsPostConfigurator : IPostConfigureOptions { private readonly IOptionsMonitor _webOptions; private readonly ILogger _logger; public ScannerStorageOptionsPostConfigurator( IOptionsMonitor webOptions, ILogger logger) { _webOptions = webOptions ?? throw new ArgumentNullException(nameof(webOptions)); _logger = logger ?? throw new ArgumentNullException(nameof(logger)); } public void PostConfigure(string? name, ScannerStorageOptions options) { ArgumentNullException.ThrowIfNull(options); var source = _webOptions.CurrentValue?.ArtifactStore; if (source is null) { return; } var target = options.ObjectStore ??= new ObjectStoreOptions(); if (!string.IsNullOrWhiteSpace(source.Driver)) { target.Driver = source.Driver; } if (!string.IsNullOrWhiteSpace(source.Region)) { target.Region = source.Region!; } if (!string.IsNullOrWhiteSpace(source.Bucket)) { target.BucketName = source.Bucket!; } if (!string.IsNullOrWhiteSpace(source.RootPrefix)) { target.RootPrefix = source.RootPrefix; } if (!string.IsNullOrWhiteSpace(source.Endpoint)) { if (target.IsRustFsDriver()) { target.RustFs ??= new RustFsOptions(); target.RustFs.BaseUrl = source.Endpoint; } else { target.ServiceUrl = source.Endpoint; } } if (target.IsRustFsDriver()) { if (target.RustFs is null) { target.RustFs = new RustFsOptions(); } target.RustFs.AllowInsecureTls = source.AllowInsecureTls; if (!string.IsNullOrWhiteSpace(source.ApiKeyHeader)) { target.RustFs.ApiKeyHeader = source.ApiKeyHeader!; } if (!string.IsNullOrWhiteSpace(source.ApiKey)) { target.RustFs.ApiKey = source.ApiKey; } if (!string.IsNullOrWhiteSpace(source.Endpoint)) { target.RustFs.BaseUrl = source.Endpoint!; } } if (!string.IsNullOrWhiteSpace(source.AccessKey)) { target.AccessKeyId = source.AccessKey; } if (!string.IsNullOrWhiteSpace(source.SecretKey)) { target.SecretAccessKey = source.SecretKey; } if (source.Headers is { Count: > 0 }) { foreach (var (key, value) in source.Headers) { if (string.IsNullOrWhiteSpace(key) || string.IsNullOrWhiteSpace(value)) { continue; } target.Headers[key] = value; } } _logger.LogDebug( "Mirrored artifact store settings into scanner storage options (driver: {Driver}, bucket: {Bucket}).", target.Driver, target.BucketName); } }