feat: first-class protected-secret support (DataProtectorExtensions) - #17
Merged
Conversation
…ttings fields Extracts the "protect a new value, or keep the existing ciphertext when the caller left the field blank" round trip that was hand-rolled near-identically in app-foundation's MartenEmailSettingsStore, customer-portal's MartenInvoiceNinjaSettingsStore, and finance-app's MartenCredentialStore. The extension takes the caller's own IDataProtector, so this library only depends on Microsoft.AspNetCore.DataProtection.Abstractions (interfaces only), not the full ASP.NET Core shared framework. Adds a new AndreGoepel.Marten.Configuration.Tests unit test project (no I/O, no Docker) alongside the existing IntegrationTests project.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #13
Adds
DataProtectorExtensions.ProtectOrKeepExisting, extracting the "protect a new value, or keep the existing ciphertext when the caller left the field blank" round trip that is currently hand-rolled near-identically in three places:app-foundation'sMartenEmailSettingsStore(SMTP password)customer-portal'sMartenInvoiceNinjaSettingsStore(API token)finance-app'sMartenCredentialStore(provider credential)Design decisions
Shape: an extension method on
IDataProtector, not a wrapper value type. I looked at what's actually identical across the three call sites versus what's app-specific:!string.IsNullOrEmpty(newPlaintext) ? protector.Protect(newPlaintext) : existingCiphertext.MartenEmailSettingsStorefails the save;MartenInvoiceNinjaSettingsStoretreats a blank token as a valid state and protectsstring.Empty;MartenCredentialStoredoesn't have this case at all — a secret is always required). SoProtectOrKeepExistingreturnsnullin that case and leaves the decision to the caller.IDataProtector.Unprotect(string)is already a one-line framework extension method (DataProtectionCommonExtensions). Wrapping a one-liner that already exists would only add indirection, not reduce duplication.No wrapper value type for the ciphertext. All three call sites store the ciphertext as a plain
stringfield on their document (ProtectedPassword,ProtectedApiToken,ProtectedPayload). Introducing aProtectedValuestruct would mean either changing those field types (out of scope — migration is tracked separately) or the primitive would go unused. Keeping it as a plainstringin/out means it can be adopted with a one-line change at each call site with no document/serialization migration.No new extension point on
ISettingsStore/MartenSettingsStorefor resolving anIDataProtector. I considered adding one (per-document purpose string →IDataProtector), butIDataProtectionProvider.CreateProtector(string purpose)already is that extension point — it's supplied by the framework and every call site already uses it directly. Adding another abstraction on top would be pure indirection with no reduction in duplication, so I left it out per "don't over-engineer."Package choice:
Microsoft.AspNetCore.DataProtection.Abstractions, notMicrosoft.AspNetCore.DataProtection. The extension method only operates on theIDataProtectorinterface — it never constructs a provider itself (callers get theirs from DI, as they already do). TheAbstractionspackage is a standalone NuGet package (no ASP.NET Core shared-framework dependency), consistent with this repo's "no ASP.NET Core dependency" rule and with howfinance-app'sAndreGoepel.FinanceApp.Domainproject already depends on it. I verified concretely:EphemeralDataProtectionProvider/DataProtectionProvider.Create(...)are not resolvable fromAbstractionsalone — they requireFrameworkReference Include="Microsoft.AspNetCore.App". That's fine, because this library never needs a concrete provider, only the interface.New test project. Added
AndreGoepel.Marten.Configuration.Tests(pure unit tests, no I/O) alongside the existingAndreGoepel.Marten.Configuration.IntegrationTests(Docker/Testcontainers), following the same split used inmarten-identity. It's the only project in the solution withFrameworkReference Include="Microsoft.AspNetCore.App"— test-only, to getEphemeralDataProtectionProviderfor in-memory round trips — so the packablesrcproject stays framework-free.Out of scope (tracked separately): migrating
app-foundation(app-foundation#155),customer-portal, andfinance-app(finance-app#68) onto this primitive. This PR ships the primitive only.Test plan
dotnet csharpier format .dotnet build— 0 warnings, 0 errorsdotnet test— all unit tests (6) and integration tests (4) passdotnet list package --vulnerable --include-transitive— clean