| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | |
| | | 3 | | namespace Elsa.Secrets.Repositories; |
| | | 4 | | |
| | | 5 | | public class InMemorySecretRepository : ISecretRepository |
| | | 6 | | { |
| | 32 | 7 | | private readonly ConcurrentDictionary<string, Secret> _secrets = new(StringComparer.OrdinalIgnoreCase); |
| | | 8 | | |
| | | 9 | | public Task<Secret?> GetAsync(string normalizedName, CancellationToken cancellationToken = default) |
| | | 10 | | { |
| | 26 | 11 | | _secrets.TryGetValue(normalizedName, out var secret); |
| | 26 | 12 | | return Task.FromResult(secret == null ? null : Clone(secret)); |
| | | 13 | | } |
| | | 14 | | |
| | | 15 | | public Task<IReadOnlyCollection<Secret>> ListAsync(CancellationToken cancellationToken = default) |
| | | 16 | | { |
| | 3 | 17 | | return Task.FromResult<IReadOnlyCollection<Secret>>(_secrets.Values.Select(Clone).ToList()); |
| | | 18 | | } |
| | | 19 | | |
| | | 20 | | public Task AddAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 21 | | { |
| | 1 | 22 | | if (!_secrets.TryAdd(secret.Name, Clone(secret))) |
| | 0 | 23 | | throw new InvalidOperationException($"A secret named '{secret.Name}' already exists."); |
| | | 24 | | |
| | 1 | 25 | | return Task.CompletedTask; |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public Task<bool> TryAddOrReplaceDeletedAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 28 | 30 | | var secretClone = Clone(secret); |
| | | 31 | | |
| | | 32 | | while (true) |
| | | 33 | | { |
| | 28 | 34 | | if (!_secrets.TryGetValue(secret.Name, out var existingSecret)) |
| | 24 | 35 | | return Task.FromResult(_secrets.TryAdd(secret.Name, secretClone)); |
| | | 36 | | |
| | 4 | 37 | | if (existingSecret.Status != SecretStatus.Deleted) |
| | 2 | 38 | | return Task.FromResult(false); |
| | | 39 | | |
| | 2 | 40 | | if (_secrets.TryUpdate(secret.Name, secretClone, existingSecret)) |
| | 2 | 41 | | return Task.FromResult(true); |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | public Task SaveAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 46 | | { |
| | 10 | 47 | | _secrets[secret.Name] = Clone(secret); |
| | 10 | 48 | | return Task.CompletedTask; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | private static Secret Clone(Secret secret) |
| | | 52 | | { |
| | 74 | 53 | | return new Secret |
| | 74 | 54 | | { |
| | 74 | 55 | | Id = secret.Id, |
| | 74 | 56 | | Name = secret.Name, |
| | 74 | 57 | | DisplayName = secret.DisplayName, |
| | 74 | 58 | | Description = secret.Description, |
| | 74 | 59 | | TypeName = secret.TypeName, |
| | 74 | 60 | | StoreName = secret.StoreName, |
| | 74 | 61 | | Scope = secret.Scope, |
| | 74 | 62 | | Tags = secret.Tags.ToHashSet(StringComparer.OrdinalIgnoreCase), |
| | 74 | 63 | | Status = secret.Status, |
| | 74 | 64 | | CreatedAt = secret.CreatedAt, |
| | 74 | 65 | | UpdatedAt = secret.UpdatedAt, |
| | 74 | 66 | | Versions = secret.Versions.Select(Clone).ToList() |
| | 74 | 67 | | }; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | private static SecretVersion Clone(SecretVersion version) |
| | | 71 | | { |
| | 79 | 72 | | return new SecretVersion |
| | 79 | 73 | | { |
| | 79 | 74 | | Version = version.Version, |
| | 79 | 75 | | Status = version.Status, |
| | 79 | 76 | | CreatedAt = version.CreatedAt, |
| | 79 | 77 | | ExpiresAt = version.ExpiresAt, |
| | 79 | 78 | | Payload = new SecretPayload |
| | 79 | 79 | | { |
| | 79 | 80 | | Value = version.Payload.Value, |
| | 79 | 81 | | Metadata = new Dictionary<string, string>(version.Payload.Metadata, StringComparer.OrdinalIgnoreCase) |
| | 79 | 82 | | } |
| | 79 | 83 | | }; |
| | | 84 | | } |
| | | 85 | | } |