-
Notifications
You must be signed in to change notification settings - Fork 16
Add config updates via SSE (behind FF) #667
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
44fc973
Add config updates via SSE
timokoessler cfabb9d
Extend test coverage
timokoessler 024c641
Fix Codequality comments
timokoessler 63de88a
Send X-Agent headers to realtime
timokoessler ae7bec3
Use event scheduler and fix error handling
timokoessler 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
66 changes: 66 additions & 0 deletions
66
aikido_zen/background_process/realtime/listen_for_config_updates.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,66 @@ | ||
| """ | ||
| Mainly exports `listen_for_config_updates` | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| from aikido_zen.helpers.token import Token | ||
| from aikido_zen.helpers.logging import logger | ||
| import aikido_zen.background_process.realtime as realtime | ||
| from .sse_client import connect_to_sse | ||
|
|
||
|
|
||
| def listen_for_config_updates(connection_manager, event_scheduler): | ||
| """ | ||
| Connects to the realtime SSE endpoint and fetches the new config whenever | ||
| the server signals, through a "config-updated" event, that it has changed. | ||
| """ | ||
| if not isinstance(connection_manager.token, Token): | ||
| logger.info("No token provided, not listening for config updates") | ||
| return | ||
| if connection_manager.serverless: | ||
| logger.info( | ||
| "Running in serverless environment, not listening for config updates" | ||
| ) | ||
| return | ||
|
|
||
| token = connection_manager.token | ||
| last_updated_at = connection_manager.conf.last_updated_at | ||
|
|
||
| def on_event(event): | ||
| nonlocal last_updated_at | ||
|
|
||
| logger.debug("SSE event received: %s", event.event) | ||
| if event.event != "config-updated": | ||
| return | ||
|
|
||
| try: | ||
| payload = json.loads(event.data) | ||
| config_updated_at = payload["configUpdatedAt"] | ||
| if config_updated_at <= last_updated_at: | ||
| return | ||
| except (ValueError, KeyError, TypeError): | ||
| logger.debug("SSE config-updated event has invalid payload: %s", event.data) | ||
| return | ||
|
|
||
| logger.debug("SSE config-updated event, fetching new config") | ||
|
|
||
| try: | ||
| config = realtime.get_config(token) | ||
| logger.debug( | ||
| "SSE config fetched, configUpdatedAt: %s", config.get("configUpdatedAt") | ||
| ) | ||
| last_updated_at = config.get("configUpdatedAt", config_updated_at) | ||
| except Exception as e: | ||
| logger.error("Failed to fetch config after SSE event : %s", e) | ||
| return | ||
|
|
||
| def apply_config(): | ||
| # Runs on the scheduler/reporting thread, so config updates stay | ||
| # on a single writer thread | ||
| connection_manager.update_service_config({**config, "success": True}) | ||
|
timokoessler marked this conversation as resolved.
|
||
| connection_manager.update_firewall_lists() | ||
|
|
||
| event_scheduler.enter(0, 1, apply_config) | ||
|
|
||
| connect_to_sse(token=token, on_event=on_event) | ||
202 changes: 202 additions & 0 deletions
202
aikido_zen/background_process/realtime/listen_for_config_updates_test.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,202 @@ | ||
| import json | ||
| import logging | ||
| from types import SimpleNamespace | ||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import pytest | ||
|
|
||
| from aikido_zen.helpers.token import Token | ||
| from .listen_for_config_updates import listen_for_config_updates | ||
|
|
||
|
|
||
| def make_event(event="config-updated", data=None): | ||
| return SimpleNamespace( | ||
| event=event, data=json.dumps(data) if data is not None else "" | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def connection_manager(): | ||
| return MagicMock( | ||
| token=Token("123"), | ||
| serverless=None, | ||
| conf=MagicMock(last_updated_at=0), | ||
| ) | ||
|
|
||
|
|
||
| def test_no_token(caplog): | ||
| with patch( | ||
| "aikido_zen.background_process.realtime.listen_for_config_updates.connect_to_sse" | ||
| ) as mock_connect: | ||
| listen_for_config_updates( | ||
| connection_manager=MagicMock(token=None, serverless=None), | ||
| event_scheduler=MagicMock(), | ||
| ) | ||
|
|
||
| assert "No token provided, not listening for config updates" in caplog.text | ||
| mock_connect.assert_not_called() | ||
|
|
||
|
|
||
| def test_serverless_environment(caplog): | ||
| with patch( | ||
| "aikido_zen.background_process.realtime.listen_for_config_updates.connect_to_sse" | ||
| ) as mock_connect: | ||
| listen_for_config_updates( | ||
| connection_manager=MagicMock(token=Token("123"), serverless=True), | ||
| event_scheduler=MagicMock(), | ||
| ) | ||
|
|
||
| assert ( | ||
| "Running in serverless environment, not listening for config updates" | ||
| in caplog.text | ||
| ) | ||
| mock_connect.assert_not_called() | ||
|
|
||
|
|
||
| def test_connects_to_sse_with_token(connection_manager): | ||
| with patch( | ||
| "aikido_zen.background_process.realtime.listen_for_config_updates.connect_to_sse" | ||
| ) as mock_connect: | ||
| listen_for_config_updates( | ||
| connection_manager=connection_manager, event_scheduler=MagicMock() | ||
| ) | ||
|
|
||
| mock_connect.assert_called_once() | ||
| assert mock_connect.call_args.kwargs["token"] is connection_manager.token | ||
| assert callable(mock_connect.call_args.kwargs["on_event"]) | ||
|
|
||
|
|
||
| def get_on_event(connection_manager, event_scheduler=None): | ||
| if event_scheduler is None: | ||
| event_scheduler = MagicMock() | ||
| with patch( | ||
| "aikido_zen.background_process.realtime.listen_for_config_updates.connect_to_sse" | ||
| ) as mock_connect: | ||
| listen_for_config_updates( | ||
| connection_manager=connection_manager, event_scheduler=event_scheduler | ||
| ) | ||
| return mock_connect.call_args.kwargs["on_event"] | ||
|
|
||
|
|
||
| def make_inline_scheduler(): | ||
| scheduler = MagicMock() | ||
| scheduler.enter.side_effect = lambda delay, priority, action, argument=(): action( | ||
| *argument | ||
| ) | ||
| return scheduler | ||
|
|
||
|
|
||
| def test_ignores_events_that_are_not_config_updated(connection_manager): | ||
| on_event = get_on_event(connection_manager) | ||
|
|
||
| with patch("aikido_zen.background_process.realtime.get_config") as mock_get_config: | ||
| on_event(make_event(event="ping")) | ||
|
|
||
| mock_get_config.assert_not_called() | ||
| connection_manager.update_service_config.assert_not_called() | ||
|
|
||
|
|
||
| def test_ignores_config_updated_event_with_invalid_json(connection_manager, caplog): | ||
| caplog.set_level(logging.DEBUG, logger="Zen") | ||
| on_event = get_on_event(connection_manager) | ||
|
|
||
| with patch("aikido_zen.background_process.realtime.get_config") as mock_get_config: | ||
| on_event(SimpleNamespace(event="config-updated", data="not json")) | ||
|
|
||
| mock_get_config.assert_not_called() | ||
| connection_manager.update_service_config.assert_not_called() | ||
| assert "SSE config-updated event has invalid payload" in caplog.text | ||
|
|
||
|
|
||
| def test_ignores_config_updated_event_missing_config_updated_at(connection_manager): | ||
| on_event = get_on_event(connection_manager) | ||
|
|
||
| with patch("aikido_zen.background_process.realtime.get_config") as mock_get_config: | ||
| on_event(make_event(data={"foo": "bar"})) | ||
|
|
||
| mock_get_config.assert_not_called() | ||
| connection_manager.update_service_config.assert_not_called() | ||
|
|
||
|
|
||
| def test_ignores_config_updated_event_that_is_not_newer(connection_manager): | ||
| connection_manager.conf.last_updated_at = 100 | ||
| on_event = get_on_event(connection_manager) | ||
|
|
||
| with patch("aikido_zen.background_process.realtime.get_config") as mock_get_config: | ||
| on_event(make_event(data={"configUpdatedAt": 100})) | ||
|
|
||
| mock_get_config.assert_not_called() | ||
| connection_manager.update_service_config.assert_not_called() | ||
|
|
||
|
|
||
| def test_fetches_and_applies_new_config_on_newer_event(connection_manager): | ||
| connection_manager.conf.last_updated_at = 100 | ||
| event_scheduler = make_inline_scheduler() | ||
| on_event = get_on_event(connection_manager, event_scheduler) | ||
|
|
||
| new_config = {"endpoints": [], "configUpdatedAt": 200} | ||
| with patch( | ||
| "aikido_zen.background_process.realtime.get_config", return_value=new_config | ||
| ) as mock_get_config: | ||
| on_event(make_event(data={"configUpdatedAt": 200})) | ||
|
|
||
| mock_get_config.assert_called_once_with(connection_manager.token) | ||
| connection_manager.update_service_config.assert_called_once_with( | ||
| {**new_config, "success": True} | ||
| ) | ||
| connection_manager.update_firewall_lists.assert_called_once() | ||
|
|
||
|
|
||
| def test_applying_the_new_config_is_scheduled_on_the_event_scheduler( | ||
| connection_manager, | ||
| ): | ||
| connection_manager.conf.last_updated_at = 100 | ||
| event_scheduler = MagicMock() | ||
| on_event = get_on_event(connection_manager, event_scheduler) | ||
|
|
||
| new_config = {"endpoints": [], "configUpdatedAt": 200} | ||
| with patch( | ||
| "aikido_zen.background_process.realtime.get_config", return_value=new_config | ||
| ): | ||
| on_event(make_event(data={"configUpdatedAt": 200})) | ||
|
|
||
| connection_manager.update_service_config.assert_not_called() | ||
| connection_manager.update_firewall_lists.assert_not_called() | ||
| event_scheduler.enter.assert_called_once() | ||
| args, _kwargs = event_scheduler.enter.call_args | ||
| assert args[0] == 0 # run as soon as possible | ||
| assert callable(args[2]) | ||
|
|
||
|
|
||
| def test_updates_last_updated_at_so_a_second_stale_event_is_ignored( | ||
| connection_manager, | ||
| ): | ||
| connection_manager.conf.last_updated_at = 100 | ||
| event_scheduler = make_inline_scheduler() | ||
| on_event = get_on_event(connection_manager, event_scheduler) | ||
|
|
||
| new_config = {"endpoints": [], "configUpdatedAt": 200} | ||
| with patch( | ||
| "aikido_zen.background_process.realtime.get_config", return_value=new_config | ||
| ) as mock_get_config: | ||
| on_event(make_event(data={"configUpdatedAt": 200})) | ||
| # A second event claiming to be newer than the original lastUpdatedAt | ||
| # (100), but not newer than what we just fetched (200), should now be | ||
| # ignored without fetching again. | ||
| on_event(make_event(data={"configUpdatedAt": 150})) | ||
|
|
||
| mock_get_config.assert_called_once() | ||
|
|
||
|
|
||
| def test_handles_get_config_failure_gracefully(connection_manager, caplog): | ||
| connection_manager.conf.last_updated_at = 100 | ||
| on_event = get_on_event(connection_manager) | ||
|
|
||
| with patch( | ||
| "aikido_zen.background_process.realtime.get_config", | ||
| side_effect=ValueError("Request timed out"), | ||
| ): | ||
| on_event(make_event(data={"configUpdatedAt": 200})) | ||
|
|
||
| connection_manager.update_service_config.assert_not_called() | ||
| assert "Failed to fetch config after SSE event" in caplog.text |
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.