-
Notifications
You must be signed in to change notification settings - Fork 9
Re-split OpenSearch indices per service #158
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
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fb3bb08
🔥(search) remove dead services parameter from search schema
StephanMeijer 6944b1f
♻️(backend) remove Service.services M2M and enforce name immutability
StephanMeijer 6881a57
✅(tests) rewrite cleanup_test_index fixture for per-service indices
StephanMeijer 20bba67
📝(documentation) per-service indices and services-cannot-search
StephanMeijer ab4813d
fixup! ♻️(backend) remove Service.services M2M and enforce name immut…
StephanMeijer 7f6eb7f
fixup! ✅(tests) rewrite cleanup_test_index fixture for per-service in…
StephanMeijer bb1bea1
fixup! 📝(documentation) per-service indices and services-cannot-search
StephanMeijer 5ffbfe1
amend! ♻️(backend) remove Service.services M2M and enforce name immut…
StephanMeijer 1fba769
fixup! 🔥(search) remove dead services parameter from search schema
StephanMeijer f3f103c
fixup! ♻️(backend) remove Service.services M2M and enforce name immut…
StephanMeijer c9e0d78
♻️(backend) introduce Service.slug as immutable index identifier
StephanMeijer 157f02b
fixup! ✅(tests) rewrite cleanup_test_index fixture for per-service in…
StephanMeijer f334ed0
fixup! ♻️(backend) remove Service.services M2M and enforce name immut…
StephanMeijer ce6ac3c
fixup! ♻️(backend) introduce Service.slug as immutable index identifier
StephanMeijer 37bfd7d
fixup! 📝(documentation) per-service indices and services-cannot-search
StephanMeijer bd77c86
fixup! ✅(tests) rewrite cleanup_test_index fixture for per-service in…
StephanMeijer 2769f27
fixup! 📝(documentation) per-service indices and services-cannot-search
StephanMeijer 600eea9
fixup! ♻️(backend) introduce Service.slug as immutable index identifier
StephanMeijer 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| # Contract: Services Cannot Search | ||
|
|
||
| Services registered in Find can index and delete documents, but they cannot search. Only users | ||
| authenticated via OIDC tokens can call `/search/`. | ||
|
|
||
| ## Authentication model | ||
|
|
||
| Find exposes three endpoints, each with a distinct authentication requirement: | ||
|
|
||
| | Endpoint | Auth class | Who can call it | | ||
| |-------------|-------------------------------|--------------------------| | ||
| | `/index/` | `ServiceTokenAuthentication` | Services (bearer token) | | ||
| | `/search/` | `ResourceServerAuthentication` + `ResourceServerMixin` | OIDC users only | | ||
| | `/delete/` | `ServiceTokenAuthentication` | Services (bearer token) | | ||
|
|
||
| `ServiceTokenAuthentication` validates the `Authorization: Bearer <token>` header against the | ||
| `Service.token` field in the database. It is wired to `IndexDocumentView` and | ||
| `DeleteDocumentsView`. | ||
|
|
||
| `ResourceServerAuthentication` (from `django-lasuite`) validates OIDC access tokens issued by | ||
| the configured identity provider. It is wired to `SearchDocumentView` via `ResourceServerMixin`. | ||
|
|
||
| ## What happens when a service tries to search | ||
|
|
||
| A service bearer token posted to `/search/` is rejected with a 4xx status (typically | ||
| **HTTP 400 Bad Request** from `django-lasuite`'s `SuspiciousOperation` when introspection | ||
| fails, or **HTTP 401/403** if the token never reaches introspection). | ||
|
|
||
| `ResourceServerAuthentication` does not recognise service bearer tokens. The request is rejected | ||
| before any OpenSearch query runs. | ||
|
|
||
| ## Why this contract exists | ||
|
|
||
| Services write to and delete from their own isolated index. They have no business reading across | ||
| other services' indices. Cross-service fan-out is a user-facing feature: when a user searches, | ||
| Find queries all active service indices and merges the results, filtered by the user's identity | ||
| (`sub` claim). | ||
|
|
||
| Allowing services to search would break the access-control model: a service could read documents | ||
| belonging to users it has no relationship with. | ||
|
|
||
| ## Test coverage | ||
|
|
||
| The following test scenarios lock this contract in: | ||
|
|
||
| - `test_api_documents_index.py` verifies that `/index/` rejects OIDC-style bearer tokens | ||
| (HTTP 403 `Invalid token.`). | ||
| - `test_api_documents_search.py` verifies that `/search/` rejects service bearer tokens and | ||
| anonymous requests (HTTP 4xx). | ||
| - `test_api_documents_delete.py` verifies that `/delete/` accepts service bearer tokens and rejects | ||
| OIDC user tokens. | ||
| - `test_integration_per_service_indices.py` verifies the full round-trip: services index and delete | ||
| documents from their own indices, users search and find their accessible documents. | ||
|
|
||
| If you add a new endpoint that should be user-only, wire `ResourceServerAuthentication` and | ||
| `ResourceServerMixin` to it. Do not add `ServiceTokenAuthentication` to search paths. | ||
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
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
73 changes: 73 additions & 0 deletions
73
src/backend/core/migrations/0003_service_slug_and_editable_name.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,73 @@ | ||
| import re | ||
|
|
||
| import django.core.validators | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| def backfill_slug_from_name(apps, schema_editor): | ||
| Service = apps.get_model("core", "Service") # noqa: N806 | ||
| seen = set() | ||
| for service in Service.objects.all(): | ||
| derived = re.sub(r"[^a-zA-Z0-9]", "", service.name or "").lower() | ||
| if not derived: | ||
| raise RuntimeError( | ||
| f"Cannot derive slug for Service id={service.pk!r} " | ||
| f"name={service.name!r}: name contains no alphanumeric characters." | ||
| ) | ||
| if derived in seen: | ||
| raise RuntimeError( | ||
| f"Slug collision while backfilling: name={service.name!r} -> " | ||
| f"slug={derived!r} already used by another service." | ||
| ) | ||
| seen.add(derived) | ||
| service.slug = derived | ||
| service.save(update_fields=["slug"]) | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("core", "0002_service_client_id_service_services"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="service", | ||
| name="slug", | ||
| field=models.CharField(max_length=20, null=True), | ||
| ), | ||
| migrations.RunPython( | ||
| backfill_slug_from_name, | ||
| reverse_code=migrations.RunPython.noop, | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="service", | ||
| name="slug", | ||
| field=models.SlugField( | ||
| editable=False, | ||
| help_text=( | ||
| "Stable identifier used in the OpenSearch index name. " | ||
| "Lowercase alphanumeric only. Set on creation, immutable thereafter." | ||
| ), | ||
| max_length=20, | ||
| unique=True, | ||
| validators=[ | ||
| django.core.validators.RegexValidator( | ||
| message="Slug must contain only lowercase letters and digits.", | ||
| regex="^[a-z0-9]+$", | ||
| ) | ||
| ], | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="service", | ||
| name="name", | ||
| field=models.CharField(max_length=255), | ||
| ), | ||
| migrations.AddConstraint( | ||
| model_name="service", | ||
| constraint=models.CheckConstraint( | ||
| condition=models.Q(("slug__regex", "^[a-z0-9]+$")), | ||
| name="slug_alphanumeric_only", | ||
| ), | ||
| ), | ||
| ] |
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.