Skip to content

fix(tenancy): harden device ownership resolution - #27

Merged
KilianBoute merged 2 commits into
mainfrom
fix/device-key-ambiguity
Aug 24, 2026
Merged

KilianBoute merged 2 commits into
mainfrom
fix/device-key-ambiguity

Conversation

@KilianBoute

@KilianBoute KilianBoute commented Aug 24, 2026 •

Copy link
Copy Markdown
Member

Description

This change hardens tenancy ownership resolution by tightening device lookups and removing unsupported organisation ownership fallbacks that could hide data integrity issues and degrade query performance.

LoadDevice now enforces that a device key resolves to exactly one persisted device. Instead of relying on FindOne, it performs a bounded query with a limit of two results so the resolver can explicitly reject ambiguous keys while still failing correctly for missing devices. This prevents ownership resolution from silently operating on inconsistent data and makes failure modes deterministic.

The organisation ownership lookup has also been simplified to use only the canonical ownerId field. Organisations never persisted the legacy owner_id variant, so the previous $or condition introduced unnecessary complexity and prevented MongoDB from fully using the ownerId_1 index. Restricting the query to the canonical indexed field avoids collection scans and aligns the resolver with the actual persistence model.

The accompanying tests strengthen coverage around:

  • ambiguous device keys
  • missing devices
  • missing persisted identities
  • canonical organisation owner filtering
  • query limits and configured collection usage

Overall, this improves correctness, fail-closed behaviour, and query efficiency in tenancy resolution.

Read at most two device records by key and fail closed when a key resolves to multiple persisted devices. Preserve the existing no-document error contract.
Query organisations by canonical ownerId only. Organisation documents never used owner_id, and removing that unindexed OR arm preserves use of the ownerId_1 index.
@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@KilianBoute
KilianBoute merged commit 0f32ecb into main Aug 24, 2026
6 checks passed
@KilianBoute
KilianBoute deleted the fix/device-key-ambiguity branch August 24, 2026 13:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant