| | | 1 | | using Elsa.Common.Multitenancy; |
| | | 2 | | using Elsa.Persistence.EFCore; |
| | | 3 | | using Elsa.Secrets.Contracts; |
| | | 4 | | using Elsa.Secrets.Models; |
| | | 5 | | using Elsa.Tenants.Options; |
| | | 6 | | using Microsoft.EntityFrameworkCore; |
| | | 7 | | using Microsoft.Extensions.Options; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Secrets.Persistence.EFCore.Repositories; |
| | | 10 | | |
| | 0 | 11 | | public class EFCoreSecretRepository( |
| | 0 | 12 | | Store<SecretsElsaDbContext, Secret> store, |
| | 0 | 13 | | ISecretNameValidator secretNameValidator, |
| | 0 | 14 | | IOptions<TenantsOptions>? tenantsOptions = null) : ISecretRepository |
| | | 15 | | { |
| | 0 | 16 | | private readonly bool? _tenancyEnabled = tenantsOptions?.Value.IsEnabled; |
| | | 17 | | |
| | | 18 | | // Keep the pre-tenancy constructor in the public binary surface. |
| | | 19 | | public EFCoreSecretRepository(Store<SecretsElsaDbContext, Secret> store, ISecretNameValidator secretNameValidator) |
| | 0 | 20 | | : this(store, secretNameValidator, null) |
| | | 21 | | { |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task<Secret?> GetAsync(string normalizedName, CancellationToken cancellationToken = default) |
| | | 25 | | { |
| | 0 | 26 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 0 | 27 | | var secret = await FindByNameAsync(dbContext, normalizedName, cancellationToken); |
| | 0 | 28 | | SecretSerialization.LoadSerializedProperties(dbContext, secret); |
| | 0 | 29 | | return secret; |
| | 0 | 30 | | } |
| | | 31 | | |
| | | 32 | | public async Task<IReadOnlyCollection<Secret>> ListAsync(CancellationToken cancellationToken = default) |
| | | 33 | | { |
| | 0 | 34 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 0 | 35 | | var secrets = await dbContext.Secrets.ToListAsync(cancellationToken); |
| | | 36 | | |
| | 0 | 37 | | foreach (var secret in secrets) |
| | 0 | 38 | | SecretSerialization.LoadSerializedProperties(dbContext, secret); |
| | | 39 | | |
| | 0 | 40 | | return secrets; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public async Task AddAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 44 | | { |
| | 0 | 45 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 0 | 46 | | AssignDefaultTenantId(secret, dbContext); |
| | 0 | 47 | | EnsureNewSecretTenantIsOwnedByWriter(secret, dbContext, IsTenancyEnabled(dbContext)); |
| | 0 | 48 | | var normalizedName = secretNameValidator.Normalize(secret.Name); |
| | 0 | 49 | | if (await ExistsByNormalizedNameAsync(dbContext, normalizedName, cancellationToken)) |
| | 0 | 50 | | throw new InvalidOperationException($"A secret named '{secret.Name}' already exists."); |
| | | 51 | | |
| | 0 | 52 | | await dbContext.Secrets.AddAsync(secret, cancellationToken); |
| | 0 | 53 | | SetNormalizedName(dbContext, secret); |
| | 0 | 54 | | SecretSerialization.StoreSerializedProperties(dbContext, secret); |
| | 0 | 55 | | await SaveChangesAsync(dbContext, secret.Name, cancellationToken); |
| | 0 | 56 | | } |
| | | 57 | | |
| | | 58 | | public async Task<bool> TryAddOrReplaceDeletedAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 59 | | { |
| | 0 | 60 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 0 | 61 | | AssignDefaultTenantId(secret, dbContext); |
| | 0 | 62 | | var tenancyEnabled = IsTenancyEnabled(dbContext); |
| | 0 | 63 | | var existingSecret = await FindByNameAsync(dbContext, secret.Name, cancellationToken); |
| | | 64 | | |
| | 0 | 65 | | if (existingSecret == null) |
| | | 66 | | { |
| | 0 | 67 | | if (!IsNewSecretTenantOwnedByWriter(secret, dbContext, tenancyEnabled)) |
| | 0 | 68 | | return false; |
| | | 69 | | |
| | 0 | 70 | | await dbContext.Secrets.AddAsync(secret, cancellationToken); |
| | 0 | 71 | | SetNormalizedName(dbContext, secret); |
| | 0 | 72 | | SecretSerialization.StoreSerializedProperties(dbContext, secret); |
| | 0 | 73 | | return await TrySaveChangesAsync(dbContext, secret.Name, cancellationToken); |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | if (existingSecret.IsLifecycleManaged || existingSecret.Status != SecretStatus.Deleted) |
| | | 77 | | { |
| | 0 | 78 | | return false; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | var incomingTenantId = secret.TenantId ?? dbContext.TenantId; |
| | 0 | 82 | | if (tenancyEnabled && !TenantVisibility.CanReplaceOwnedRow(existingSecret.TenantId, incomingTenantId, dbContext. |
| | 0 | 83 | | return false; |
| | | 84 | | |
| | 0 | 85 | | if (tenancyEnabled) |
| | 0 | 86 | | secret.TenantId = existingSecret.TenantId; |
| | 0 | 87 | | await using var transaction = await dbContext.Database.BeginTransactionAsync(cancellationToken); |
| | 0 | 88 | | dbContext.Secrets.Remove(existingSecret); |
| | 0 | 89 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 0 | 90 | | await dbContext.Secrets.AddAsync(secret, cancellationToken); |
| | 0 | 91 | | SetNormalizedName(dbContext, secret); |
| | 0 | 92 | | SecretSerialization.StoreSerializedProperties(dbContext, secret); |
| | | 93 | | |
| | 0 | 94 | | if (!await TrySaveChangesAsync(dbContext, secret.Name, cancellationToken)) |
| | 0 | 95 | | return false; |
| | | 96 | | |
| | 0 | 97 | | await transaction.CommitAsync(cancellationToken); |
| | 0 | 98 | | return true; |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | public async Task SaveAsync(Secret secret, CancellationToken cancellationToken = default) |
| | | 102 | | { |
| | 0 | 103 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 0 | 104 | | AssignDefaultTenantId(secret, dbContext); |
| | 0 | 105 | | var tenancyEnabled = IsTenancyEnabled(dbContext); |
| | 0 | 106 | | var existingSecret = await FindByNameAsync(dbContext, secret.Name, cancellationToken); |
| | | 107 | | |
| | 0 | 108 | | if (existingSecret == null) |
| | | 109 | | { |
| | 0 | 110 | | EnsureNewSecretTenantIsOwnedByWriter(secret, dbContext, tenancyEnabled); |
| | 0 | 111 | | await dbContext.Secrets.AddAsync(secret, cancellationToken); |
| | 0 | 112 | | SetNormalizedName(dbContext, secret); |
| | 0 | 113 | | SecretSerialization.StoreSerializedProperties(dbContext, secret); |
| | | 114 | | } |
| | | 115 | | else |
| | | 116 | | { |
| | 0 | 117 | | var incomingTenantId = secret.TenantId ?? dbContext.TenantId; |
| | 0 | 118 | | if (tenancyEnabled && !TenantVisibility.CanReplaceOwnedRow(existingSecret.TenantId, incomingTenantId, dbCont |
| | 0 | 119 | | throw new InvalidOperationException($"A secret named '{secret.Name}' belongs to another tenant."); |
| | | 120 | | |
| | 0 | 121 | | Copy(secret, existingSecret, tenancyEnabled); |
| | 0 | 122 | | SetNormalizedName(dbContext, existingSecret); |
| | 0 | 123 | | SecretSerialization.StoreSerializedProperties(dbContext, existingSecret); |
| | | 124 | | } |
| | | 125 | | |
| | 0 | 126 | | await SaveChangesAsync(dbContext, secret.Name, cancellationToken); |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | private static void Copy(Secret source, Secret target, bool tenancyEnabled) |
| | | 130 | | { |
| | 0 | 131 | | if (!tenancyEnabled) |
| | 0 | 132 | | target.TenantId = source.TenantId; |
| | | 133 | | |
| | 0 | 134 | | target.Name = source.Name; |
| | 0 | 135 | | target.DisplayName = source.DisplayName; |
| | 0 | 136 | | target.Description = source.Description; |
| | 0 | 137 | | target.TypeName = source.TypeName; |
| | 0 | 138 | | target.StoreName = source.StoreName; |
| | 0 | 139 | | target.Scope = source.Scope; |
| | 0 | 140 | | target.Status = source.Status; |
| | 0 | 141 | | target.CreatedAt = source.CreatedAt; |
| | 0 | 142 | | target.UpdatedAt = source.UpdatedAt; |
| | 0 | 143 | | target.Tags = source.Tags.ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | 0 | 144 | | target.Versions = source.Versions.ToList(); |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | private Task<Secret?> FindByNameAsync(SecretsElsaDbContext dbContext, string name, CancellationToken cancellationTok |
| | | 148 | | { |
| | 0 | 149 | | var normalizedName = secretNameValidator.Normalize(name); |
| | 0 | 150 | | return dbContext.Secrets.FirstOrDefaultAsync(x => EF.Property<string>(x, SecretShadowPropertyNames.NormalizedNam |
| | | 151 | | } |
| | | 152 | | |
| | | 153 | | private bool IsTenancyEnabled(SecretsElsaDbContext dbContext) |
| | | 154 | | { |
| | 0 | 155 | | if (_tenancyEnabled.HasValue) |
| | 0 | 156 | | return _tenancyEnabled.Value; |
| | | 157 | | |
| | 0 | 158 | | return dbContext.IsTenantFilteringEnabled; |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | private static bool IsNewSecretTenantOwnedByWriter(Secret secret, SecretsElsaDbContext dbContext, bool tenancyEnable |
| | 0 | 162 | | !tenancyEnabled || secret.TenantId is null || secret.TenantId == (dbContext.TenantId ?? Tenant.DefaultTenantId); |
| | | 163 | | |
| | | 164 | | private static void EnsureNewSecretTenantIsOwnedByWriter(Secret secret, SecretsElsaDbContext dbContext, bool tenancy |
| | | 165 | | { |
| | 0 | 166 | | if (!IsNewSecretTenantOwnedByWriter(secret, dbContext, tenancyEnabled)) |
| | 0 | 167 | | throw new InvalidOperationException($"A secret named '{secret.Name}' belongs to another tenant."); |
| | 0 | 168 | | } |
| | | 169 | | |
| | | 170 | | private static Task<bool> ExistsByNormalizedNameAsync(SecretsElsaDbContext dbContext, string normalizedName, Cancell |
| | | 171 | | { |
| | 0 | 172 | | return dbContext.Secrets.AnyAsync(x => EF.Property<string>(x, SecretShadowPropertyNames.NormalizedName) == norma |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | // The DbUpdateException-to-name-conflict translation below relies on the (TenantId, NormalizedName) |
| | | 176 | | // unique index. Default-tenant writes are stamped with an empty TenantId before saving so the index |
| | | 177 | | // provides the same concurrency backstop when multitenancy is disabled. See doc/migrations/secrets-tenancy.md. |
| | | 178 | | private async Task SaveChangesAsync(SecretsElsaDbContext dbContext, string name, CancellationToken cancellationToken |
| | | 179 | | { |
| | | 180 | | try |
| | | 181 | | { |
| | 0 | 182 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 0 | 183 | | } |
| | 0 | 184 | | catch (DbUpdateException e) |
| | | 185 | | { |
| | 0 | 186 | | if (await IsNameConflictAsync(name, cancellationToken)) |
| | 0 | 187 | | throw new InvalidOperationException($"A secret named '{name}' already exists.", e); |
| | | 188 | | |
| | 0 | 189 | | throw; |
| | 0 | 190 | | } |
| | 0 | 191 | | } |
| | | 192 | | |
| | | 193 | | private async Task<bool> TrySaveChangesAsync(SecretsElsaDbContext dbContext, string name, CancellationToken cancella |
| | | 194 | | { |
| | | 195 | | try |
| | | 196 | | { |
| | 0 | 197 | | await dbContext.SaveChangesAsync(cancellationToken); |
| | 0 | 198 | | return true; |
| | | 199 | | } |
| | 0 | 200 | | catch (DbUpdateException) |
| | | 201 | | { |
| | 0 | 202 | | if (await IsNameConflictAsync(name, cancellationToken)) |
| | 0 | 203 | | return false; |
| | | 204 | | |
| | 0 | 205 | | throw; |
| | | 206 | | } |
| | 0 | 207 | | } |
| | | 208 | | |
| | | 209 | | private async Task<bool> IsNameConflictAsync(string name, CancellationToken cancellationToken) |
| | | 210 | | { |
| | 0 | 211 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 0 | 212 | | return await ExistsByNormalizedNameAsync(dbContext, secretNameValidator.Normalize(name), cancellationToken); |
| | 0 | 213 | | } |
| | | 214 | | |
| | | 215 | | private void SetNormalizedName(SecretsElsaDbContext dbContext, Secret secret) |
| | | 216 | | { |
| | 0 | 217 | | dbContext.Entry(secret).Property(SecretShadowPropertyNames.NormalizedName).CurrentValue = secretNameValidator.No |
| | 0 | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Default-tenant uniqueness uses <see cref="Tenant.DefaultTenantId"/> (<c>""</c>), not null. |
| | | 222 | | /// Named and agnostic ambient tenants are left for <c>ApplyTenantId</c>. |
| | | 223 | | /// </summary> |
| | | 224 | | private static void AssignDefaultTenantId(Secret secret, SecretsElsaDbContext dbContext) |
| | | 225 | | { |
| | 0 | 226 | | if (secret.TenantId is not null) |
| | 0 | 227 | | return; |
| | | 228 | | |
| | 0 | 229 | | if (!string.IsNullOrEmpty(dbContext.TenantId) && dbContext.TenantId != Tenant.DefaultTenantId) |
| | 0 | 230 | | return; |
| | | 231 | | |
| | 0 | 232 | | secret.TenantId = Tenant.DefaultTenantId; |
| | 0 | 233 | | } |
| | | 234 | | } |