-
Notifications
You must be signed in to change notification settings - Fork 2k
in_elasticsearch: respond to malformed bulk payloads #12445
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
+216
−23
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1b4aaae
in_elasticsearch: respond to malformed bulk payloads
edsiper b7a7674
tests: integration: cover elasticsearch invalid bulk
edsiper f85eb2c
in_elasticsearch: keep bulk ingestion failures retryable
edsiper 6b9ae6b
tests: cover elasticsearch bulk append failures
edsiper 112cffb
tests: integration: validate elasticsearch errors with shared memory …
edsiper c204e0b
in_elasticsearch: reject unconsumed bulk payload data
edsiper 24282ea
tests: integration: cover trailing elasticsearch bulk data
edsiper 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
120 changes: 120 additions & 0 deletions
120
...integration/scenarios/elasticsearch_invalid_bulk/tests/test_elasticsearch_invalid_bulk.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,120 @@ | ||
| """Reject malformed bulk payloads while keeping ingestion failures retryable.""" | ||
| import contextlib | ||
| import http.client | ||
| import json | ||
| from pathlib import Path | ||
| import socket | ||
| import time | ||
|
|
||
| import pytest | ||
|
|
||
| from utils.fluent_bit_manager import FluentBitManager | ||
|
|
||
|
|
||
| def wait_for(predicate, timeout=30): | ||
| deadline = time.monotonic() + timeout | ||
| while time.monotonic() < deadline: | ||
| if predicate(): | ||
| return | ||
| time.sleep(0.05) | ||
| assert predicate(), "Timed out waiting for Fluent Bit output" | ||
|
|
||
|
|
||
| @contextlib.contextmanager | ||
| def daemon(tmp_path, mode, input_options=None): | ||
| with socket.socket() as listener: | ||
| listener.bind(("127.0.0.1", 0)) | ||
| port = listener.getsockname()[1] | ||
| input_config = {"name": mode, "listen": "127.0.0.1", "port": port} | ||
| if input_options: | ||
| input_config.update(input_options) | ||
| config = { | ||
| "service": { | ||
| "flush": 0.1, | ||
| "grace": 1, | ||
| "http_server": "on", | ||
| "http_listen": "127.0.0.1", | ||
| "http_port": "${FLUENT_BIT_HTTP_MONITORING_PORT}", | ||
| }, | ||
| "pipeline": { | ||
| "inputs": [input_config], | ||
| "outputs": [{"name": "stdout", "match": "*", "format": "json_lines"}], | ||
| }, | ||
| } | ||
| config_path = tmp_path / "fluent-bit.yaml" | ||
| config_path.write_text(json.dumps(config)) | ||
| manager = FluentBitManager(str(config_path)) | ||
| try: | ||
| manager.start() | ||
| log = Path(manager.log_file) | ||
| yield ("127.0.0.1", port), manager.process, log | ||
| finally: | ||
| manager.stop() | ||
|
|
||
|
|
||
| def send(mode, address, payload): | ||
| conn = http.client.HTTPConnection(*address, timeout=10) | ||
| try: | ||
| payload = b'{"index":{}}\n' + payload + b"\n" | ||
| conn.request("POST", "/_bulk", payload, {"Content-Type": "application/json"}) | ||
| response = conn.getresponse() | ||
| response.read() | ||
| return response.status | ||
| finally: | ||
| conn.close() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("mode", ["elasticsearch"]) | ||
| @pytest.mark.parametrize("kind", ["array", "map", "mixed"]) | ||
| def test_nested_json_recovery(tmp_path, mode, kind): | ||
| # Bounded regression data, with no process-crash or exploit-chain behavior. | ||
| value = b"0" | ||
| for index in range(65): | ||
| value = (b'{"k":' + value + b"}") if kind == "map" or ( | ||
| kind == "mixed" and index % 2) else b"[" + value + b"]" | ||
| nested = b'{"nested":' + value + b"}" | ||
| with daemon(tmp_path, mode) as (address, process, log): | ||
| send(mode, address, b'{"marker":"before"}') | ||
| wait_for(lambda: '"marker":"before"' in log.read_text()) | ||
| assert send(mode, address, nested) == 400 | ||
| send(mode, address, b'{"marker":"after"}') | ||
| wait_for(lambda: '"marker":"after"' in log.read_text()) | ||
| assert process.poll() is None | ||
| assert '"nested":' not in log.read_text() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("payload", [b'{"broken":', b'not-json', b'[]', b'42']) | ||
| def test_malformed_bulk_recovery(tmp_path, payload): | ||
| with daemon(tmp_path, "elasticsearch") as (address, process, log): | ||
| assert send("elasticsearch", address, payload) == 400 | ||
| assert send("elasticsearch", address, b'{"marker":"after"}') == 200 | ||
| wait_for(lambda: '"marker":"after"' in log.read_text()) | ||
| assert process.poll() is None | ||
|
|
||
|
|
||
| def test_busy_ingress_is_retryable(tmp_path): | ||
| options = {"http_server.workers": 2, "http_server.ingress_queue_byte_limit": "128"} | ||
| with daemon(tmp_path, "elasticsearch", options) as (address, process, log): | ||
| assert send("elasticsearch", address, json.dumps({"large": "x" * 1024}).encode()) == 503 | ||
| assert send("elasticsearch", address, b'{"marker":"after"}') == 200 | ||
| wait_for(lambda: '"marker":"after"' in log.read_text()) | ||
| assert process.poll() is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("trailer", [b'{"index":', b'[', b'"unfinished', b'garbage']) | ||
| def test_trailing_data_rejected_before_ingestion(tmp_path, trailer): | ||
| with daemon(tmp_path, "elasticsearch") as (address, process, log): | ||
| payload = b'{"marker":"rejected"}\n' + trailer | ||
| assert send("elasticsearch", address, payload) == 400 | ||
| assert send("elasticsearch", address, b'{"marker":"after"}') == 200 | ||
| wait_for(lambda: '"marker":"after"' in log.read_text()) | ||
| assert '"marker":"rejected"' not in log.read_text() | ||
| assert process.poll() is None | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("trailer", [b'', b' \t\r\n \t']) | ||
| def test_trailing_whitespace_accepted(tmp_path, trailer): | ||
| with daemon(tmp_path, "elasticsearch") as (address, process, log): | ||
| assert send("elasticsearch", address, b'{"marker":"accepted"}' + trailer) == 200 | ||
| wait_for(lambda: '"marker":"accepted"' in log.read_text()) | ||
| assert process.poll() is None |
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.
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.