parent
d4845d89ae
commit
0dbc1ac7fd
@ -0,0 +1,8 @@
|
||||
using System.Collections.Immutable;
|
||||
|
||||
namespace TrashLib.Config.Secrets;
|
||||
|
||||
public interface ISecretsProvider
|
||||
{
|
||||
IImmutableDictionary<string, string> Secrets { get; }
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
using YamlDotNet.Core;
|
||||
using YamlDotNet.Core.Events;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace TrashLib.Config.Secrets;
|
||||
|
||||
public record SecretTag;
|
||||
|
||||
public class SecretsDeserializer : INodeDeserializer
|
||||
{
|
||||
private readonly ISecretsProvider _secrets;
|
||||
|
||||
public SecretsDeserializer(ISecretsProvider secrets)
|
||||
{
|
||||
_secrets = secrets;
|
||||
}
|
||||
|
||||
public bool Deserialize(IParser reader, Type expectedType, Func<IParser, Type, object?> nestedObjectDeserializer,
|
||||
out object? value)
|
||||
{
|
||||
// Only process items flagged as Secrets
|
||||
if (expectedType != typeof(SecretTag))
|
||||
{
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
var scalar = reader.Consume<Scalar>();
|
||||
if (!_secrets.Secrets.TryGetValue(scalar.Value, out var secretsValue))
|
||||
{
|
||||
throw new YamlException(scalar.Start, scalar.End, $"{scalar.Value} is not defined in secrets.yml.");
|
||||
}
|
||||
|
||||
value = secretsValue;
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
using System.Collections.Immutable;
|
||||
using TrashLib.Startup;
|
||||
using YamlDotNet.Serialization;
|
||||
|
||||
namespace TrashLib.Config.Secrets;
|
||||
|
||||
public class SecretsProvider : ISecretsProvider
|
||||
{
|
||||
public IImmutableDictionary<string, string> Secrets => _secrets.Value;
|
||||
|
||||
private readonly IAppPaths _paths;
|
||||
private readonly Lazy<IImmutableDictionary<string, string>> _secrets;
|
||||
|
||||
public SecretsProvider(IAppPaths paths)
|
||||
{
|
||||
_paths = paths;
|
||||
_secrets = new Lazy<IImmutableDictionary<string, string>>(LoadSecretsFile);
|
||||
}
|
||||
|
||||
private IImmutableDictionary<string, string> LoadSecretsFile()
|
||||
{
|
||||
var result = new Dictionary<string, string>();
|
||||
|
||||
if (_paths.SecretsPath.Exists)
|
||||
{
|
||||
using var stream = _paths.SecretsPath.OpenText();
|
||||
var deserializer = new DeserializerBuilder().Build();
|
||||
var dict = deserializer.Deserialize<Dictionary<string, string>?>(stream);
|
||||
if (dict is not null)
|
||||
{
|
||||
result = dict;
|
||||
}
|
||||
}
|
||||
|
||||
return result.ToImmutableDictionary();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue