Repository navigation
Send the common agent headers on all cloud requests #751
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
4 commits
Select commit
Hold shift + click to select a range
3c30bb5
Send the common agent headers on all cloud requests
iacobdaniel 732c2a2
Keep the original request construction in make_request
iacobdaniel e8bca90
Harden the agent headers and add X-Agent-Library
iacobdaniel 989fd50
Record the agent headers alongside each event in the mock core
iacobdaniel 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
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,24 @@ | ||
| import aikido_zen.config as config | ||
| from aikido_zen.helpers.get_agent_session_id import get_agent_session_id | ||
| from aikido_zen.helpers.get_hostname import get_hostname | ||
| from aikido_zen.helpers.get_machine_ip import get_ip | ||
|
|
||
|
|
||
| def get_common_agent_headers(): | ||
| return { | ||
| "X-Agent-Platform": "python", | ||
| "X-Agent-Library": config.LIBRARY_NAME, | ||
| "X-Agent-Version": config.PKG_VERSION, | ||
| "X-Agent-Hostname": _header_value(get_hostname()), | ||
| "X-Agent-IP-Address": _header_value(get_ip()), | ||
| "X-Agent-Session-Id": get_agent_session_id(), | ||
| } | ||
|
|
||
|
|
||
| def _header_value(value): | ||
| # Header values are latin-1, and one that cannot be encoded fails the request. | ||
| try: | ||
| value.encode("latin-1") | ||
| except UnicodeEncodeError: | ||
| return "unknown" | ||
| return value or "unknown" |
77 changes: 77 additions & 0 deletions
77
aikido_zen/background_process/get_common_agent_headers_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,77 @@ | ||
| import socket | ||
| from unittest.mock import MagicMock | ||
|
|
||
| import pytest | ||
|
|
||
| import aikido_zen.config as config | ||
| import aikido_zen.background_process.get_common_agent_headers as headers_module | ||
| from aikido_zen.background_process.get_common_agent_headers import ( | ||
| get_common_agent_headers, | ||
| ) | ||
| from aikido_zen.helpers.get_agent_session_id import get_agent_session_id | ||
| from aikido_zen.helpers.get_hostname import get_hostname | ||
| from aikido_zen.helpers.get_machine_ip import get_ip | ||
| from aikido_zen.background_process.cloud_connection_manager.get_manager_info import ( | ||
| get_manager_info, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture(autouse=True) | ||
| def reset_cached_instance_values(): | ||
| get_ip.cache_clear() | ||
| get_hostname.cache_clear() | ||
|
|
||
|
|
||
| def test_common_agent_headers(monkeypatch): | ||
| monkeypatch.setattr(headers_module, "get_ip", lambda: "10.0.0.1") | ||
|
|
||
| assert get_common_agent_headers() == { | ||
| "X-Agent-Platform": "python", | ||
| "X-Agent-Library": config.LIBRARY_NAME, | ||
| "X-Agent-Version": config.PKG_VERSION, | ||
| "X-Agent-Hostname": get_hostname() or "unknown", | ||
| "X-Agent-IP-Address": "10.0.0.1", | ||
| "X-Agent-Session-Id": get_agent_session_id(), | ||
| } | ||
|
|
||
|
|
||
| def test_common_agent_headers_keep_the_same_session_id(): | ||
| assert ( | ||
| get_common_agent_headers()["X-Agent-Session-Id"] | ||
| == get_common_agent_headers()["X-Agent-Session-Id"] | ||
| ) | ||
|
|
||
|
|
||
| def test_hostname_and_ip_match_the_event_payload(monkeypatch): | ||
| monkeypatch.setattr(socket, "gethostname", lambda: "instance-app-7c9d") | ||
| monkeypatch.setattr(socket, "gethostbyname", lambda _hostname: "10.0.1.42") | ||
| manager_info = get_manager_info(MagicMock()) | ||
| headers = get_common_agent_headers() | ||
|
|
||
| assert headers["X-Agent-Hostname"] == manager_info["hostname"] | ||
| assert headers["X-Agent-IP-Address"] == manager_info["ipAddress"] | ||
|
|
||
|
|
||
| def test_hostname_that_cannot_be_a_header_is_unknown(monkeypatch): | ||
| monkeypatch.setattr(socket, "gethostname", lambda: "p\u0101yments") | ||
|
|
||
| assert get_common_agent_headers()["X-Agent-Hostname"] == "unknown" | ||
|
|
||
|
|
||
| def test_missing_hostname_is_unknown(monkeypatch): | ||
| monkeypatch.setattr(socket, "gethostname", lambda: "") | ||
|
|
||
| assert get_common_agent_headers()["X-Agent-Hostname"] == "unknown" | ||
|
|
||
|
|
||
| def test_unresolved_ip_address_is_unknown(monkeypatch): | ||
| monkeypatch.setattr(headers_module, "get_ip", lambda: "") | ||
|
|
||
| assert get_common_agent_headers()["X-Agent-IP-Address"] == "unknown" | ||
|
|
||
|
|
||
| def test_library_matches_the_event_payload(): | ||
| assert ( | ||
| get_common_agent_headers()["X-Agent-Library"] | ||
| == get_manager_info(MagicMock())["library"] | ||
| ) |
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
71 changes: 71 additions & 0 deletions
71
aikido_zen/background_process/requests/make_request_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,71 @@ | ||
| import http.server | ||
| import threading | ||
| from unittest.mock import patch | ||
|
|
||
| from aikido_zen.background_process.get_common_agent_headers import ( | ||
| get_common_agent_headers, | ||
| ) | ||
| from aikido_zen.background_process.requests.make_request import make_request | ||
|
|
||
|
|
||
| class _Response: | ||
| def __enter__(self): | ||
| return self | ||
|
|
||
| def __exit__(self, *args): | ||
| return False | ||
|
|
||
| def getcode(self): | ||
| return 200 | ||
|
|
||
| def info(self): | ||
| return {} | ||
|
|
||
| def read(self): | ||
| return b"" | ||
|
|
||
|
|
||
| def test_make_request_sends_agent_headers(): | ||
| captured = {} | ||
|
|
||
| def fake_urlopen(request, timeout): | ||
| captured["request"] = request | ||
| return _Response() | ||
|
|
||
| expected = get_common_agent_headers() | ||
| with patch("urllib.request.urlopen", side_effect=fake_urlopen): | ||
| make_request( | ||
| "GET", | ||
| "https://example.test/api", | ||
| 1, | ||
| headers={"Authorization": "token"}, | ||
| ) | ||
|
|
||
| headers = { | ||
| name.lower(): value for name, value in captured["request"].header_items() | ||
| } | ||
| assert headers["authorization"] == "token" | ||
| for name, value in expected.items(): | ||
| assert headers[name.lower()] == value | ||
|
|
||
|
|
||
| def test_make_request_with_a_hostname_that_cannot_be_a_header(): | ||
| class Handler(http.server.BaseHTTPRequestHandler): | ||
| def do_GET(self): | ||
| self.send_response(200) | ||
| self.end_headers() | ||
|
|
||
| def log_message(self, *args): | ||
| pass | ||
|
|
||
| server = http.server.HTTPServer(("127.0.0.1", 0), Handler) | ||
| threading.Thread(target=server.serve_forever, daemon=True).start() | ||
| try: | ||
| with patch("socket.gethostname", return_value="p\u0101yments"): | ||
| response = make_request( | ||
| "GET", "http://127.0.0.1:%d/" % server.server_address[1], 5 | ||
| ) | ||
| finally: | ||
| server.shutdown() | ||
|
|
||
| assert response.status_code == 200 |
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 |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| """Contains package versions""" | ||
|
|
||
| PKG_VERSION = "1.0-REPLACE-VERSION" | ||
| LIBRARY_NAME = "firewall-python" | ||
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.
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.
a constant for this feels a bit overkill sometimes but fine to me! ^^