elsa-core/test/unit/Elsa.Persistence.VNext.UnitTests
Sipke Schoorstra ee40689ef9
feat(secrets)!: scope secrets to tenants (#7991)
* feat(secrets)!: scope secrets to tenants

Secret was the one user-facing entity with no notion of tenancy. It did not
derive from Entity, so it carried no TenantId and no query filter applied to
it: in a multi-tenant deployment every tenant could see and resolve every
other tenant's secrets. Permissions did not help, because secrets:view is
evaluated against the caller rather than against which tenant owns the
secret, so any caller holding it reached the whole set.

Secret now derives from Entity and is filtered like everything else. The
infrastructure was already in place -- SecretsElsaDbContext derives from
ElsaDbContextBase and the feature from PersistenceFeatureBase, which
registers SetTenantIdFilter -- and the handler was skipping secrets for one
reason: it only applies to Entity.

No backfill, deliberately. The column is added nullable and existing rows
keep a null tenant, because SetTenantIdFilter already treats null as the
default tenant through a clause written for exactly this case. Single-tenant
deployments see no change at all, since the filter is only installed when
multitenancy is enabled. Multi-tenant deployments find pre-existing secrets
invisible until assigned, which is a visible failure rather than continued
cross-tenant exposure.

Two things this needed that were not obvious:

Secret self-initialized its Id and nothing else ever assigned one -- there is
no identity generator on the create path -- while Entity.Id is null!. Simply
deriving would have produced a null id on every insert, which any test that
builds a Secret by hand would have missed. A constructor preserves it.

The unique index moves from NormalizedName to (TenantId, NormalizedName),
matching User, Role and Application in the same release. Leaving it global
would have made secret names a shared resource: the second tenant to want
"smtp-password" could not create one.

Elsa.Secrets.Persistence.VNext cannot support this. It keys documents by name
alone and Elsa.Persistence.VNext has no tenant concept to filter on, so it now
throws outside the default tenant rather than serving one tenant's secret to
another. Making it tenant-aware means changing the document id scheme, which
relocates existing documents and is a storage change to make deliberately.

Refs #7972

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(secrets): let the VNext repository resolve without multitenancy

The tenancy guard took ITenantAccessor as a required dependency. That
interface is registered by the tenants module, so a host that never added
multitenancy has none, and resolving ISecretRepository threw for exactly the
deployments the guard is meant to leave alone.

The accessor is now optional, and its absence means no tenancy, which is the
default tenant.

Found by review, and it is worth naming why the tests missed it: every case
in VNextSecretRepositoryTests constructs the repository directly with a stub
accessor, so none of them ever went through the container where the failure
lived. The new case resolves through a service collection that adds only the
document store and the module's own registration, which is what a
single-tenant host looks like. Reverting the fix makes it fail with the same
missing-service exception review reported.

Refs #7972

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 01:56:55 +02:00
..
DocumentDatabasePlanningTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
Elsa.Persistence.VNext.UnitTests.csproj Resolve vulnerable transitive persistence packages 2026-06-22 00:45:06 +02:00
MongoDbDocumentProviderTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
PersistenceVNextIntegrationTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
PhysicalizationPlanningTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
RelationalDocumentProviderTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
RuntimeEntityDefinitionValidatorTests.cs [codex] Add runtime entity validator coverage (#7697) 2026-06-06 04:14:12 +02:00
RuntimeEntityTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
SecretsSchemaPlanningTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
SqliteDocumentStoreTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
SqliteSchemaVersionRunnerTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
SqlServerSchemaPlanningTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00
VNextSecretRepositoryTests.cs feat(secrets)!: scope secrets to tenants (#7991) 2026-08-26 01:56:55 +02:00
WorkflowRuntimePersistenceEvaluatorTests.cs Add Persistence vNext provider-neutral POC 2026-06-02 21:05:21 +02:00