-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add X-Permit-Consistent-Update header on facts proxy requests #306
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
omer9564
wants to merge
2
commits into
main
Choose a base branch
from
omer/per-14392-consistent-updates-pdp-header
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
| from horizon.facts.client import CONSISTENT_UPDATE_HEADER, FactsClient | ||
| from starlette.requests import Request as FastApiRequest | ||
|
|
||
|
|
||
| def _make_request(headers: dict[str, str] | None = None) -> FastApiRequest: | ||
| scope = { | ||
| "type": "http", | ||
| "method": "POST", | ||
| "path": "/facts/users", | ||
| "raw_path": b"/facts/users", | ||
| "query_string": b"", | ||
| "headers": [(k.lower().encode(), v.encode()) for k, v in (headers or {}).items()], | ||
| } | ||
|
|
||
| async def receive(): | ||
| return {"type": "http.request", "body": b"", "more_body": False} | ||
|
|
||
| return FastApiRequest(scope, receive) | ||
|
|
||
|
|
||
| def test_consistent_update_header_constant(): | ||
| assert CONSISTENT_UPDATE_HEADER == "X-Permit-Consistent-Update" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_build_forward_request_adds_header_when_consistent_update(): | ||
| """When is_consistent_update=True, request should carry the X-Permit-Consistent-Update header.""" | ||
| client = FactsClient() | ||
|
|
||
| mock_remote_config = MagicMock() | ||
| mock_remote_config.context = {"project_id": "proj1", "env_id": "env1"} | ||
|
|
||
| with ( | ||
| patch("horizon.facts.client.get_remote_config", return_value=mock_remote_config), | ||
| patch("horizon.facts.client.get_env_api_key", return_value="test_api_key"), | ||
| ): | ||
| request = _make_request(headers={"authorization": "Bearer user_token", "content-type": "application/json"}) | ||
| forward_request = await client.build_forward_request(request, "/users", is_consistent_update=True) | ||
|
|
||
| assert forward_request.headers.get(CONSISTENT_UPDATE_HEADER) == "true" | ||
|
|
||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_build_forward_request_omits_header_by_default(): | ||
| """By default (fallback proxy path), the request should NOT carry the consistent-update header.""" | ||
| client = FactsClient() | ||
|
|
||
| mock_remote_config = MagicMock() | ||
| mock_remote_config.context = {"project_id": "proj1", "env_id": "env1"} | ||
|
|
||
| with ( | ||
| patch("horizon.facts.client.get_remote_config", return_value=mock_remote_config), | ||
| patch("horizon.facts.client.get_env_api_key", return_value="test_api_key"), | ||
| ): | ||
| request = _make_request(headers={"authorization": "Bearer user_token", "content-type": "application/json"}) | ||
| forward_request = await client.build_forward_request(request, "/anything") | ||
|
|
||
| assert forward_request.headers.get(CONSISTENT_UPDATE_HEADER) is None |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting
is_consistent_update=Truehere will cause the backend (per PR description) to skip the control-plane delta publish for all these proxied writes, even when the local pubsub propagation fails or times out. In this codepathpublish_and_wait()can returnFalse(publish failure or wait timeout) and withTimeoutPolicy.IGNORE(the default via config) the request still returns success; after this change there would be no fallback update path, which can leave the PDP stale. Consider gatingis_consistent_updatebehind a stricter policy (e.g., only when timeout_policy==FAIL / when you will fail the request on local propagation failure) or otherwise ensuring a reliable fallback when local publish/wait does not succeed.