| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Secrets.Repositories; |
| | | 7 | | |
| | 5 | 8 | | public class FileSecretRepository(IOptions<SecretsOptions> options, ILogger<FileSecretRepository>? logger = null) : ISec |
| | | 9 | | { |
| | 5 | 10 | | private readonly SemaphoreSlim _lock = new(1, 1); |
| | 5 | 11 | | private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web) |
| | 5 | 12 | | { |
| | 5 | 13 | | Converters = { new JsonStringEnumConverter() }, |
| | 5 | 14 | | WriteIndented = true |
| | 5 | 15 | | }; |
| | | 16 | | |
| | | 17 | | public async Task<Secret?> GetAsync(string normalizedName, CancellationToken cancellationToken = default) |
| | | 18 | | { |
| | 4 | 19 | | var secrets = await ReadAllAsync(cancellationToken); |
| | 8 | 20 | | return secrets.FirstOrDefault(x => string.Equals(x.Name, normalizedName, StringComparison.OrdinalIgnoreCase)); |
| | 4 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async Task<IReadOnlyCollection<Secret>> ListAsync(CancellationToken cancellationToken = default) |
| | | 24 | | { |
| | 1 | 25 | | return await ReadAllAsync(cancellationToken); |
| | 1 | 26 | | } |
| | | 27 | | |
| | | 28 | | public async Task AddAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 3 | 30 | | await _lock.WaitAsync(cancellationToken); |
| | | 31 | | try |
| | | 32 | | { |
| | 3 | 33 | | var secrets = await ReadAllUnsafeAsync(cancellationToken); |
| | 3 | 34 | | if (secrets.Any(x => string.Equals(x.Name, secret.Name, StringComparison.OrdinalIgnoreCase))) |
| | 0 | 35 | | throw new InvalidOperationException($"A secret named '{secret.Name}' already exists."); |
| | | 36 | | |
| | 3 | 37 | | secrets.Add(secret); |
| | 3 | 38 | | await WriteAllUnsafeAsync(secrets, cancellationToken); |
| | 3 | 39 | | } |
| | | 40 | | finally |
| | | 41 | | { |
| | 3 | 42 | | _lock.Release(); |
| | | 43 | | } |
| | 3 | 44 | | } |
| | | 45 | | |
| | | 46 | | public async Task<bool> TryAddOrReplaceDeletedAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 47 | | { |
| | 2 | 48 | | await _lock.WaitAsync(cancellationToken); |
| | | 49 | | try |
| | | 50 | | { |
| | 2 | 51 | | var secrets = await ReadAllUnsafeAsync(cancellationToken); |
| | 4 | 52 | | var index = secrets.FindIndex(x => string.Equals(x.Name, secret.Name, StringComparison.OrdinalIgnoreCase)); |
| | 2 | 53 | | if (index >= 0) |
| | | 54 | | { |
| | 2 | 55 | | if (secrets[index].Status != SecretStatus.Deleted) |
| | 1 | 56 | | return false; |
| | | 57 | | |
| | 1 | 58 | | secrets[index] = secret; |
| | | 59 | | } |
| | | 60 | | else |
| | | 61 | | { |
| | 0 | 62 | | secrets.Add(secret); |
| | | 63 | | } |
| | | 64 | | |
| | 1 | 65 | | await WriteAllUnsafeAsync(secrets, cancellationToken); |
| | 1 | 66 | | return true; |
| | | 67 | | } |
| | | 68 | | finally |
| | | 69 | | { |
| | 2 | 70 | | _lock.Release(); |
| | | 71 | | } |
| | 2 | 72 | | } |
| | | 73 | | |
| | | 74 | | public async Task SaveAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 75 | | { |
| | 3 | 76 | | await _lock.WaitAsync(cancellationToken); |
| | | 77 | | try |
| | | 78 | | { |
| | 3 | 79 | | var secrets = await ReadAllUnsafeAsync(cancellationToken); |
| | 5 | 80 | | var index = secrets.FindIndex(x => string.Equals(x.Name, secret.Name, StringComparison.OrdinalIgnoreCase)); |
| | 3 | 81 | | if (index < 0) |
| | 1 | 82 | | secrets.Add(secret); |
| | | 83 | | else |
| | 2 | 84 | | secrets[index] = secret; |
| | | 85 | | |
| | 3 | 86 | | await WriteAllUnsafeAsync(secrets, cancellationToken); |
| | 3 | 87 | | } |
| | | 88 | | finally |
| | | 89 | | { |
| | 3 | 90 | | _lock.Release(); |
| | | 91 | | } |
| | 3 | 92 | | } |
| | | 93 | | |
| | | 94 | | private async Task<List<Secret>> ReadAllAsync(CancellationToken cancellationToken) |
| | | 95 | | { |
| | 5 | 96 | | await _lock.WaitAsync(cancellationToken); |
| | | 97 | | try |
| | | 98 | | { |
| | 5 | 99 | | return await ReadAllUnsafeAsync(cancellationToken); |
| | | 100 | | } |
| | | 101 | | finally |
| | | 102 | | { |
| | 5 | 103 | | _lock.Release(); |
| | | 104 | | } |
| | 5 | 105 | | } |
| | | 106 | | |
| | | 107 | | private async Task<List<Secret>> ReadAllUnsafeAsync(CancellationToken cancellationToken) |
| | | 108 | | { |
| | 13 | 109 | | var path = GetPath(); |
| | 13 | 110 | | if (!File.Exists(path)) |
| | 3 | 111 | | return []; |
| | | 112 | | |
| | 10 | 113 | | await using var stream = File.OpenRead(path); |
| | | 114 | | try |
| | | 115 | | { |
| | 10 | 116 | | return await JsonSerializer.DeserializeAsync<List<Secret>>(stream, _jsonOptions, cancellationToken) ?? []; |
| | | 117 | | } |
| | 2 | 118 | | catch (JsonException e) |
| | | 119 | | { |
| | 2 | 120 | | logger?.LogError(e, "The secrets repository file '{Path}' could not be read because it contains invalid JSON |
| | 2 | 121 | | return []; |
| | | 122 | | } |
| | 13 | 123 | | } |
| | | 124 | | |
| | | 125 | | private async Task WriteAllUnsafeAsync(List<Secret> secrets, CancellationToken cancellationToken) |
| | | 126 | | { |
| | 7 | 127 | | var path = GetPath(); |
| | 7 | 128 | | Directory.CreateDirectory(Path.GetDirectoryName(path)!); |
| | | 129 | | |
| | 7 | 130 | | var temporaryPath = $"{path}.{Guid.NewGuid():N}.tmp"; |
| | | 131 | | try |
| | | 132 | | { |
| | 7 | 133 | | await using (var stream = File.Create(temporaryPath)) |
| | 7 | 134 | | await JsonSerializer.SerializeAsync(stream, secrets.OrderBy(x => x.Name).ToList(), _jsonOptions, cancell |
| | | 135 | | |
| | 7 | 136 | | File.Move(temporaryPath, path, true); |
| | 7 | 137 | | } |
| | | 138 | | finally |
| | | 139 | | { |
| | 7 | 140 | | if (File.Exists(temporaryPath)) |
| | 0 | 141 | | File.Delete(temporaryPath); |
| | | 142 | | } |
| | 7 | 143 | | } |
| | | 144 | | |
| | 20 | 145 | | private string GetPath() => options.Value.RepositoryFilePath ?? SecretsOptions.DefaultRepositoryFilePath; |
| | | 146 | | } |