| | | 1 | | using System.Text.Json; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Secrets.Contracts; |
| | | 5 | | using Elsa.Secrets.Services; |
| | | 6 | | using Elsa.Tenants.Options; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using Microsoft.Extensions.Options; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Secrets.Repositories; |
| | | 11 | | |
| | | 12 | | public class FileSecretRepository : ISecretRepository |
| | | 13 | | { |
| | | 14 | | private readonly IOptions<SecretsOptions> options; |
| | | 15 | | private readonly ILogger<FileSecretRepository>? logger; |
| | | 16 | | private readonly ITenantAccessor? tenantAccessor; |
| | | 17 | | private readonly bool tenancyEnabled; |
| | | 18 | | private readonly ISecretNameValidator nameValidator; |
| | | 19 | | |
| | | 20 | | public FileSecretRepository( |
| | | 21 | | IOptions<SecretsOptions> options, |
| | | 22 | | ILogger<FileSecretRepository>? logger) |
| | 1 | 23 | | : this(options, logger, null) |
| | | 24 | | { |
| | 1 | 25 | | } |
| | | 26 | | |
| | | 27 | | public FileSecretRepository( |
| | | 28 | | IOptions<SecretsOptions> options, |
| | | 29 | | ILogger<FileSecretRepository>? logger = null, |
| | | 30 | | ITenantAccessor? tenantAccessor = null) |
| | 27 | 31 | | : this(options, logger, tenantAccessor, tenantAccessor is not null, new DefaultSecretNameValidator()) |
| | | 32 | | { |
| | 27 | 33 | | } |
| | | 34 | | |
| | | 35 | | public FileSecretRepository( |
| | | 36 | | IOptions<SecretsOptions> options, |
| | | 37 | | IOptions<TenantsOptions> tenantsOptions, |
| | | 38 | | ISecretNameValidator nameValidator, |
| | | 39 | | ILogger<FileSecretRepository>? logger = null, |
| | | 40 | | ITenantAccessor? tenantAccessor = null) |
| | 3 | 41 | | : this(options, logger, tenantAccessor, tenantsOptions.Value.IsEnabled, nameValidator) |
| | | 42 | | { |
| | 3 | 43 | | } |
| | | 44 | | |
| | 30 | 45 | | private FileSecretRepository( |
| | 30 | 46 | | IOptions<SecretsOptions> options, |
| | 30 | 47 | | ILogger<FileSecretRepository>? logger, |
| | 30 | 48 | | ITenantAccessor? tenantAccessor, |
| | 30 | 49 | | bool tenancyEnabled, |
| | 30 | 50 | | ISecretNameValidator nameValidator) |
| | | 51 | | { |
| | 30 | 52 | | this.options = options; |
| | 30 | 53 | | this.logger = logger; |
| | 30 | 54 | | this.tenantAccessor = tenantAccessor; |
| | 30 | 55 | | this.tenancyEnabled = tenancyEnabled; |
| | 30 | 56 | | this.nameValidator = nameValidator; |
| | 30 | 57 | | } |
| | | 58 | | |
| | 30 | 59 | | private readonly SemaphoreSlim _lock = new(1, 1); |
| | 30 | 60 | | private readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web) |
| | 30 | 61 | | { |
| | 30 | 62 | | Converters = { new JsonStringEnumConverter() }, |
| | 30 | 63 | | WriteIndented = true |
| | 30 | 64 | | }; |
| | | 65 | | |
| | | 66 | | public async Task<Secret?> GetAsync(string normalizedName, CancellationToken cancellationToken = default) |
| | | 67 | | { |
| | 37 | 68 | | var secrets = await ReadAllAsync(cancellationToken); |
| | 80 | 69 | | return secrets.FirstOrDefault(x => SecretRepositoryTenant.IsVisible(x, tenantAccessor, tenancyEnabled) && Secret |
| | 37 | 70 | | } |
| | | 71 | | |
| | | 72 | | public async Task<IReadOnlyCollection<Secret>> ListAsync(CancellationToken cancellationToken = default) |
| | | 73 | | { |
| | 25 | 74 | | return (await ReadAllAsync(cancellationToken)).Where(x => SecretRepositoryTenant.IsVisible(x, tenantAccessor, te |
| | 9 | 75 | | } |
| | | 76 | | |
| | | 77 | | public async Task AddAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 78 | | { |
| | 39 | 79 | | SecretRepositoryTenant.Stamp(secret, tenantAccessor, tenancyEnabled); |
| | | 80 | | |
| | 39 | 81 | | await _lock.WaitAsync(cancellationToken); |
| | | 82 | | try |
| | | 83 | | { |
| | 39 | 84 | | var secrets = await ReadAllUnsafeAsync(cancellationToken); |
| | 58 | 85 | | if (secrets.Any(x => SecretRepositoryTenant.IsVisible(x, tenantAccessor, tenancyEnabled) && SecretRepository |
| | 6 | 86 | | throw new InvalidOperationException($"A secret named '{secret.Name}' already exists."); |
| | | 87 | | |
| | 46 | 88 | | if (secrets.Any(x => SecretRepositoryTenant.HasSameTenantName(x, secret, nameValidator, tenancyEnabled))) |
| | 2 | 89 | | throw new InvalidOperationException($"A secret named '{secret.Name}' already exists."); |
| | | 90 | | |
| | 42 | 91 | | if (secrets.Any(x => x.Id == secret.Id)) |
| | 0 | 92 | | throw new InvalidOperationException($"A secret with ID '{secret.Id}' already exists."); |
| | | 93 | | |
| | 31 | 94 | | secrets.Add(secret); |
| | 31 | 95 | | await WriteAllUnsafeAsync(secrets, cancellationToken); |
| | 31 | 96 | | } |
| | | 97 | | finally |
| | | 98 | | { |
| | 39 | 99 | | _lock.Release(); |
| | | 100 | | } |
| | 31 | 101 | | } |
| | | 102 | | |
| | | 103 | | public async Task<bool> TryAddOrReplaceDeletedAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 104 | | { |
| | 13 | 105 | | SecretRepositoryTenant.Stamp(secret, tenantAccessor, tenancyEnabled); |
| | | 106 | | |
| | 13 | 107 | | await _lock.WaitAsync(cancellationToken); |
| | | 108 | | try |
| | | 109 | | { |
| | 13 | 110 | | var secrets = await ReadAllUnsafeAsync(cancellationToken); |
| | 27 | 111 | | var index = secrets.FindIndex(x => SecretRepositoryTenant.IsVisible(x, tenantAccessor, tenancyEnabled) && Se |
| | 13 | 112 | | if (index >= 0) |
| | | 113 | | { |
| | 8 | 114 | | if (secrets[index].Status != SecretStatus.Deleted) |
| | 3 | 115 | | return false; |
| | | 116 | | |
| | 5 | 117 | | if (secrets[index].IsLifecycleManaged) |
| | | 118 | | { |
| | 0 | 119 | | return false; |
| | | 120 | | } |
| | | 121 | | |
| | 5 | 122 | | if (!SecretRepositoryTenant.CanReplace(secrets[index], secret, tenantAccessor, tenancyEnabled)) |
| | 0 | 123 | | return false; |
| | | 124 | | |
| | 13 | 125 | | if (secrets.Where((_, i) => i != index).Any(x => x.Id == secret.Id)) |
| | 1 | 126 | | return false; |
| | | 127 | | |
| | 4 | 128 | | secrets[index] = ReplaceTenantOwnedSecret(secrets[index], secret, tenancyEnabled); |
| | | 129 | | } |
| | | 130 | | else |
| | | 131 | | { |
| | 10 | 132 | | if (secrets.Any(x => SecretRepositoryTenant.HasSameTenantName(x, secret, nameValidator, tenancyEnabled)) |
| | 2 | 133 | | return false; |
| | | 134 | | |
| | 6 | 135 | | if (secrets.Any(x => x.Id == secret.Id)) |
| | 1 | 136 | | return false; |
| | | 137 | | |
| | 2 | 138 | | secrets.Add(secret); |
| | | 139 | | } |
| | | 140 | | |
| | 6 | 141 | | await WriteAllUnsafeAsync(secrets, cancellationToken); |
| | 6 | 142 | | return true; |
| | | 143 | | } |
| | | 144 | | finally |
| | | 145 | | { |
| | 13 | 146 | | _lock.Release(); |
| | | 147 | | } |
| | 13 | 148 | | } |
| | | 149 | | |
| | | 150 | | public async Task SaveAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 151 | | { |
| | 12 | 152 | | SecretRepositoryTenant.Stamp(secret, tenantAccessor, tenancyEnabled); |
| | | 153 | | |
| | 12 | 154 | | await _lock.WaitAsync(cancellationToken); |
| | | 155 | | try |
| | | 156 | | { |
| | 12 | 157 | | var secrets = await ReadAllUnsafeAsync(cancellationToken); |
| | 21 | 158 | | var index = secrets.FindIndex(x => SecretRepositoryTenant.IsVisible(x, tenantAccessor, tenancyEnabled) && Se |
| | 12 | 159 | | if (index < 0) |
| | | 160 | | { |
| | 9 | 161 | | if (secrets.Any(x => SecretRepositoryTenant.HasSameTenantName(x, secret, nameValidator, tenancyEnabled)) |
| | 2 | 162 | | throw new InvalidOperationException($"A secret named '{secret.Name}' already exists."); |
| | | 163 | | |
| | 5 | 164 | | if (secrets.Any(x => x.Id == secret.Id)) |
| | 0 | 165 | | throw new InvalidOperationException($"A secret with ID '{secret.Id}' already exists."); |
| | | 166 | | |
| | 4 | 167 | | secrets.Add(secret); |
| | | 168 | | } |
| | | 169 | | else |
| | | 170 | | { |
| | 6 | 171 | | if (!SecretRepositoryTenant.CanReplace(secrets[index], secret, tenantAccessor, tenancyEnabled)) |
| | 1 | 172 | | throw new InvalidOperationException($"A secret named '{secret.Name}' belongs to another tenant."); |
| | | 173 | | |
| | 5 | 174 | | secrets[index] = ReplaceIdentityAndTenant(secrets[index], secret, tenancyEnabled); |
| | | 175 | | } |
| | | 176 | | |
| | 9 | 177 | | await WriteAllUnsafeAsync(secrets, cancellationToken); |
| | 9 | 178 | | } |
| | | 179 | | finally |
| | | 180 | | { |
| | 12 | 181 | | _lock.Release(); |
| | | 182 | | } |
| | 9 | 183 | | } |
| | | 184 | | |
| | | 185 | | private async Task<List<Secret>> ReadAllAsync(CancellationToken cancellationToken) |
| | | 186 | | { |
| | 46 | 187 | | await _lock.WaitAsync(cancellationToken); |
| | | 188 | | try |
| | | 189 | | { |
| | 46 | 190 | | return await ReadAllUnsafeAsync(cancellationToken); |
| | | 191 | | } |
| | | 192 | | finally |
| | | 193 | | { |
| | 46 | 194 | | _lock.Release(); |
| | | 195 | | } |
| | 46 | 196 | | } |
| | | 197 | | |
| | | 198 | | private async Task<List<Secret>> ReadAllUnsafeAsync(CancellationToken cancellationToken) |
| | | 199 | | { |
| | 110 | 200 | | var path = GetPath(); |
| | 110 | 201 | | if (!File.Exists(path)) |
| | 23 | 202 | | return []; |
| | | 203 | | |
| | 87 | 204 | | await using var stream = File.OpenRead(path); |
| | | 205 | | try |
| | | 206 | | { |
| | 87 | 207 | | return await JsonSerializer.DeserializeAsync<List<Secret>>(stream, _jsonOptions, cancellationToken) ?? []; |
| | | 208 | | } |
| | 2 | 209 | | catch (JsonException e) |
| | | 210 | | { |
| | 2 | 211 | | logger?.LogError(e, "The secrets repository file '{Path}' could not be read because it contains invalid JSON |
| | 2 | 212 | | return []; |
| | | 213 | | } |
| | 110 | 214 | | } |
| | | 215 | | |
| | | 216 | | private async Task WriteAllUnsafeAsync(List<Secret> secrets, CancellationToken cancellationToken) |
| | | 217 | | { |
| | 46 | 218 | | var path = GetPath(); |
| | 46 | 219 | | Directory.CreateDirectory(Path.GetDirectoryName(path)!); |
| | | 220 | | |
| | 46 | 221 | | var temporaryPath = $"{path}.{Guid.NewGuid():N}.tmp"; |
| | | 222 | | try |
| | | 223 | | { |
| | 46 | 224 | | await using (var stream = File.Create(temporaryPath)) |
| | 75 | 225 | | await JsonSerializer.SerializeAsync(stream, secrets.OrderBy(x => x.Name).ToList(), _jsonOptions, cancell |
| | | 226 | | |
| | 46 | 227 | | File.Move(temporaryPath, path, true); |
| | 46 | 228 | | } |
| | | 229 | | finally |
| | | 230 | | { |
| | 46 | 231 | | if (File.Exists(temporaryPath)) |
| | 0 | 232 | | File.Delete(temporaryPath); |
| | | 233 | | } |
| | 46 | 234 | | } |
| | | 235 | | |
| | | 236 | | /// <summary> |
| | | 237 | | /// Updates the aggregate payload while retaining the row identity and, when enabled, tenant ownership. |
| | | 238 | | /// </summary> |
| | | 239 | | private static Secret ReplaceTenantOwnedSecret(Secret existing, Secret incoming, bool tenancyEnabled) |
| | | 240 | | { |
| | 4 | 241 | | incoming.ManagedOwnerId = existing.ManagedOwnerId; |
| | 4 | 242 | | incoming.ManagedGenerationId = existing.ManagedGenerationId; |
| | | 243 | | |
| | 4 | 244 | | if (tenancyEnabled) |
| | 2 | 245 | | incoming.TenantId = existing.TenantId; |
| | | 246 | | |
| | 4 | 247 | | return incoming; |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | private static Secret ReplaceIdentityAndTenant(Secret existing, Secret incoming, bool tenancyEnabled) |
| | | 251 | | { |
| | 5 | 252 | | incoming.Id = existing.Id; |
| | 5 | 253 | | incoming.ManagedOwnerId = existing.ManagedOwnerId; |
| | 5 | 254 | | incoming.ManagedGenerationId = existing.ManagedGenerationId; |
| | | 255 | | |
| | 5 | 256 | | if (tenancyEnabled) |
| | 2 | 257 | | incoming.TenantId = existing.TenantId; |
| | | 258 | | |
| | 5 | 259 | | return incoming; |
| | | 260 | | } |
| | | 261 | | |
| | 156 | 262 | | private string GetPath() => options.Value.RepositoryFilePath ?? SecretsOptions.DefaultRepositoryFilePath; |
| | | 263 | | } |