AshVault.Verifiers.VerifyVault (AshVault v0.2.0)

Copy Markdown View Source

Post-compile checks on the ash_vault section.

Verifiers run after the resource module is compiled, so cross-module checks — such as "does the configured vault actually implement the callbacks?" — cost no compile-time dependency on the vault.

Checks:

  • a plain-module vault exports encrypt!/2, decrypt!/2, rotate!/1 and destroy!/1 (the fun/2 and MFA forms are resolved at runtime, so they are skipped)

  • the resource's key scope agrees with the vault's own scope module. The effective encryption scope is the vault's — AshVault.Vault.Runtime resolves it from the vault's :scope option — while the resource's scope is what the key_lifecycle actions rotate and destroy. Letting the two disagree would act on a different scope than the one the data was encrypted under.

    This is deliberately stricter than EXTENSION_SPEC §1: spark materializes schema defaults into the DSL state, so Spark.Dsl.Extension.fetch_opt/3 cannot tell a written scope :tenant from the default one. Rather than let a silent mismatch through, AshVault requires a resource to restate a non-default scope. Both defaults are AshVault.Scopes.AshTenant, so the common case never trips; a vault built with scope: MyApp.CustomScope needs scope MyApp.CustomScope on the resource too.

  • decrypt_by_default names only encrypted fields

  • backfill_from names an attribute that still exists

  • key_lifecycle is only configured on a scope_owner? true resource

  • no duplicate encrypt entries

  • nothing else on the resource still points at the plaintext attribute in a way that the encrypted form cannot honour — see Constructs an encrypted attribute voids below

use Spark.Dsl.Extension auto-prepends Spark.Dsl.Verifiers.VerifyEntityUniqueness and VerifySectionSingletonEntities to the verifier list, so entity-level uniqueness is already covered; the duplicate check here catches sugar/entity collisions.

Constructs an encrypted attribute voids

AshVault.Transformers.SetupEncryption removes the plaintext attribute and replaces it with a filterable?: false, sortable?: false calculation. Anything elsewhere on the resource that was declared against that attribute is therefore now declared against something that is not a column, and most of those constructs fail silently rather than loudly.

Ash and AshPostgres already catch most of them, and this verifier deliberately does not duplicate their errors:

  • a relationship's source_attribute — deps/ash/lib/ash/resource/verifiers/validate_relationship_attributes.ex:33-40 raises "expects source attribute email to be defined"; the other side is caught on the other resource at :75-83 of the same file.
  • a postgres check_constraints entry — deps/ash_postgres/lib/verifiers/validate_check_constraints.ex:17-30 raises.

The multitenancy attribute is rejected earlier by AshVault.Transformers.SetupEncryption, before removing any attributes. AshPostgres needs that column while persisting key metadata, before Ash's multitenancy verifier runs.

Two do not, and this verifier covers them:

  • an identity whose keys include the encrypted field. Ash's own deps/ash/lib/ash/resource/verifiers/verify_identities.ex:16 accepts a key that is either an attribute or a calculation — and the encrypted field is now exactly that, a calculation. So identity :unique_email, [:email] compiles with no complaint whatsoever on a Postgres-backed resource, the migration generator has no column to index, and duplicates start landing while the developer believes the field is unique. (ETS-backed resources happen to fail via Ash.DataLayer.Verifiers.RequirePreCheckWith, but that is an ETS-only accident: Postgres enforces identities with a real index and needs no pre-check, so the realistic configuration is the unguarded one.)

  • a postgres custom_indexes entry naming the encrypted field. Nothing in AshPostgres validates those field names — AshPostgres.DataLayer.Info.custom_indexes/1 hands them straight to the migration generator — so the index is generated against a column that does not exist.