58 lines
2.4 KiB
C#
58 lines
2.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using StellaOps.Cryptography;
|
|
using StellaOps.Cryptography.PluginLoader;
|
|
|
|
namespace StellaOps.Cryptography.DependencyInjection;
|
|
|
|
internal sealed class CryptoPluginConfigurationRegistry : ICryptoProviderRegistry
|
|
{
|
|
private readonly CryptoProviderRegistry _registry;
|
|
|
|
public CryptoPluginConfigurationRegistry(
|
|
IReadOnlyList<ICryptoProvider> providers,
|
|
IOptions<CryptoPluginConfiguration> configuration,
|
|
ILogger<CryptoPluginConfigurationRegistry>? logger = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(providers);
|
|
ArgumentNullException.ThrowIfNull(configuration);
|
|
|
|
var config = configuration.Value;
|
|
var preferredProviderNames = providers
|
|
.OrderByDescending(provider => GetProviderPriority(provider, config))
|
|
.Select(provider => provider.Name)
|
|
.ToList();
|
|
|
|
logger?.LogInformation(
|
|
"Loaded {Count} crypto provider(s) with preferred order: {Providers}",
|
|
providers.Count,
|
|
string.Join(", ", preferredProviderNames));
|
|
|
|
_registry = new CryptoProviderRegistry(providers, preferredProviderNames);
|
|
}
|
|
|
|
public IReadOnlyCollection<ICryptoProvider> Providers => _registry.Providers;
|
|
public bool TryResolve(string preferredProvider, out ICryptoProvider provider)
|
|
=> _registry.TryResolve(preferredProvider, out provider);
|
|
public ICryptoProvider ResolveOrThrow(CryptoCapability capability, string algorithmId)
|
|
=> _registry.ResolveOrThrow(capability, algorithmId);
|
|
public CryptoSignerResolution ResolveSigner(
|
|
CryptoCapability capability,
|
|
string algorithmId,
|
|
CryptoKeyReference keyReference,
|
|
string? preferredProvider = null)
|
|
=> _registry.ResolveSigner(capability, algorithmId, keyReference, preferredProvider);
|
|
public CryptoHasherResolution ResolveHasher(string algorithmId, string? preferredProvider = null)
|
|
=> _registry.ResolveHasher(algorithmId, preferredProvider);
|
|
|
|
private static int GetProviderPriority(ICryptoProvider provider, CryptoPluginConfiguration config)
|
|
{
|
|
var enabledEntry = config.Enabled.FirstOrDefault(entry =>
|
|
entry.Id.Equals(provider.Name, StringComparison.OrdinalIgnoreCase));
|
|
return enabledEntry?.Priority ?? 50;
|
|
}
|
|
}
|