-
Notifications
You must be signed in to change notification settings - Fork 5
fix(identity)!: enforce tenant and credential isolation #1001
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bokelley
wants to merge
1
commit into
main
Choose a base branch
from
codex/security-identity-isolation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
examples/v3_reference_seller/alembic/versions/0003_unique_buyer_api_key.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| """require globally unique buyer-agent bearer identifiers | ||
|
|
||
| Revision ID: 0003 | ||
| Revises: 0002 | ||
| Create Date: 2026-07-29 | ||
|
|
||
| ``api_key_id`` is a bearer credential, so it must identify exactly one | ||
| commercial identity and tenant. The preflight deliberately stops the | ||
| migration before changing indexes when legacy duplicates exist; operators | ||
| must rotate or remove duplicates rather than letting the database choose an | ||
| arbitrary owner. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
|
|
||
| import sqlalchemy as sa | ||
| from alembic import context, op | ||
|
|
||
| revision: str = "0003" | ||
| down_revision: str | None = "0002" | ||
|
bokelley marked this conversation as resolved.
|
||
| branch_labels: str | Sequence[str] | None = None | ||
|
bokelley marked this conversation as resolved.
|
||
| depends_on: str | Sequence[str] | None = None | ||
|
bokelley marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def upgrade() -> None: | ||
| if not context.is_offline_mode(): | ||
| connection = op.get_bind() | ||
| duplicate_count = connection.execute( | ||
| sa.text( | ||
| "SELECT COUNT(*) FROM (" | ||
| "SELECT api_key_id FROM buyer_agents " | ||
| "WHERE api_key_id IS NOT NULL " | ||
| "GROUP BY api_key_id HAVING COUNT(*) > 1" | ||
| ") AS duplicate_credentials" | ||
| ) | ||
| ).scalar_one() | ||
| if duplicate_count: | ||
| raise RuntimeError( | ||
| "Cannot enforce buyer_agents.api_key_id uniqueness: " | ||
| f"found {duplicate_count} duplicated credential identifier(s). " | ||
| "Rotate or remove duplicate bearer credentials, then rerun the migration." | ||
| ) | ||
|
|
||
| # Create first so a concurrent/legacy duplicate makes the migration fail | ||
| # while the old lookup index remains available. PostgreSQL then rolls the | ||
| # transaction back; offline SQL retains the same safe ordering. | ||
| op.create_index( | ||
| "buyer_agents_api_key_uidx", | ||
| "buyer_agents", | ||
| ["api_key_id"], | ||
| unique=True, | ||
| postgresql_where=sa.text("api_key_id IS NOT NULL"), | ||
| sqlite_where=sa.text("api_key_id IS NOT NULL"), | ||
| ) | ||
| op.drop_index("buyer_agents_api_key_idx", table_name="buyer_agents") | ||
|
|
||
|
|
||
| def downgrade() -> None: | ||
| op.drop_index("buyer_agents_api_key_uidx", table_name="buyer_agents") | ||
| op.create_index( | ||
| "buyer_agents_api_key_idx", | ||
| "buyer_agents", | ||
| ["api_key_id"], | ||
| unique=False, | ||
| postgresql_where=sa.text("api_key_id IS NOT NULL"), | ||
| sqlite_where=sa.text("api_key_id IS NOT NULL"), | ||
| ) | ||
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.