Skip to content
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

[Azure] Sanitize message in case of malformed json #34874

Merged
merged 21 commits into from May 31, 2023

Conversation

lucian-ioan
Copy link
Contributor

@lucian-ioan lucian-ioan commented Mar 21, 2023

What does this PR do?

This PR adds the function sanitize which is used in parseMultipleMessages to clean up the message string before attempting to unmarshal the JSON.

It also adds the field SanitizeOptions which should be added manually for the datastreams that require sanitization in integrations.

Why is it important?

As pointed out by @zmoog, some logs from Azure can have issues with newlines, while others with single quotes.

This can lead to failure in processing the data in integrations (ex: the pipeline receiving one document with two records).

Filebeat sample configuration

eventhub: ""
connection_string: ""
storage_account: ""
storage_account_key: ""
sanitize_options:
  - NEW_LINES
  - SINGLE_QUOTES

Checklist

  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

Author's Checklist

  • Enhance testing with real world examples

Related issues

@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Mar 21, 2023
@lucian-ioan lucian-ioan requested a review from zmoog March 21, 2023 17:09
@elasticmachine
Copy link
Collaborator

elasticmachine commented Mar 21, 2023

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview preview

Expand to view the summary

Build stats

  • Start Time: 2023-05-29T11:31:09.436+0000

  • Duration: 71 min 41 sec

Test stats 🧪

Test Results
Failed 0
Passed 2920
Skipped 175
Total 3095

💚 Flaky test report

Tests succeeded.

🤖 GitHub comments

Expand to view the GitHub comments

To re-run your PR in the CI, just comment with:

  • /test : Re-trigger the build.

  • /package : Generate the packages and run the E2E tests.

  • /beats-tester : Run the installation tests with beats-tester.

  • run elasticsearch-ci/docs : Re-trigger the docs validation. (use unformatted text in the comment!)

@lucian-ioan lucian-ioan added the Team:Cloud-Monitoring Label for the Cloud Monitoring team label Mar 22, 2023
@botelastic botelastic bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Mar 22, 2023
@elastic elastic deleted a comment from mergify bot Mar 22, 2023
@lucian-ioan
Copy link
Contributor Author

The function parseMultipleMessages in input.go does not keep the original order of the json keys.

Therefore, the expected result has the json keys in different order (sanitization works fine).

x-pack/filebeat/input/azureeventhub/input.go Outdated Show resolved Hide resolved
// clean up the message for known issues producing a malformed JSON
if a.config.SanitizeOptions != nil {
for _, opt := range a.config.SanitizeOptions {
bMessage = sanitize(string(bMessage), opt)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if an unknown/invalid opt is passed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My idea is to handle this in the sanitize function. If an unknown option is passed, it should just return the JSON string as it was.

type sanitizationFunc func(jsonStr string) []byte

func getSanitizationFuncs() map[string]sanitizationFunc {
return map[string]sanitizationFunc{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't a switch/case be more readable here (it would also allow having a default behavior).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Should the default behaviour be for all sanitizations to occur for Azure?
  2. And how about exposing the sanitization options directly in Kibana for the end-user?

What's your opinion on this? @zmoog

Copy link
Contributor

@zmoog zmoog Apr 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my take:

  1. The default behavior must be no sanitization; these malformed JSON documents are edge cases.
  2. We need to make this option available in the agent template file and on the integration settings page.

For example, we can turn it on by default for FunctionApp logs but also give the option to turn it off as an escape hatch.

This triggers a question: we will probably release this change with stack version 8.8 and backport it to 8.7.2. How can we make this fix available without raising the minimum required version for the Azure Integration logs to 8.7.2? Today the min required version is ^7.16.0 || ^8.0.0.

It's a big bump to address and an edge case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we must bump the minimum version to 8.9.

x-pack/filebeat/input/azureeventhub/sanitization_test.go Outdated Show resolved Hide resolved
@lucian-ioan
Copy link
Contributor Author

lucian-ioan commented Apr 20, 2023

err := json.Unmarshal(bMessage, &mapObject)

Since unmrashalling happens via a map, I think we only need to worry about newlines in the key/values themselves.
(any other newlines are cut during the process)

@zmoog
Copy link
Contributor

zmoog commented Apr 21, 2023

@lucianpy, please make sure the settings are easy to use in the Agent template and in integration settings UI.

Set this PR ready for review when you're ready.

I will ask around what are our options to make this new configuration settings available to the integration without bumping the min version from 7.17.x to 8.7.2 for an edge case.

@lucian-ioan lucian-ioan changed the title Sanitize message in case of malformed json [Azure] Sanitize message in case of malformed json Apr 28, 2023
@lucian-ioan lucian-ioan assigned zmoog and unassigned zmoog May 2, 2023
@lucian-ioan lucian-ioan marked this pull request as ready for review May 4, 2023 09:03
Copy link
Contributor

@zmoog zmoog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @lucianpy, how do you plan to set up the integration settings UI to configure the sanitization options?

CHANGELOG.next.asciidoc Outdated Show resolved Hide resolved
x-pack/filebeat/input/azureeventhub/input.go Outdated Show resolved Hide resolved
@zmoog
Copy link
Contributor

zmoog commented May 17, 2023

@lucianpy, we're almost ready to go!

The last thing to try is setting up the new sanitization options in the integration settings UI to ensure we don't need to make any changes to the config struct.

lucian-ioan and others added 4 commits May 18, 2023 07:37
Co-authored-by: Maurizio Branca <maurizio.branca@elastic.co>
Co-authored-by: Maurizio Branca <maurizio.branca@elastic.co>
@lucian-ioan
Copy link
Contributor Author

lucian-ioan commented May 18, 2023

@zmoog I've set up the settings. They will be separate toggles for each sanitization option under 'Advanced options'.
The config under the policy created also looks fine.

Copy link
Contributor

@zmoog zmoog left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucianpy successfully tested we have everything we need to use this from the Agent integrations settings, so we're good to go!

@zmoog zmoog removed their assignment May 29, 2023
@zmoog
Copy link
Contributor

zmoog commented May 29, 2023

Hey, @elastic/elastic-agent-data-plane, @lucianpy also needs your review since you are the default owner in the Beats repo.

@cmacknz
Copy link
Member

cmacknz commented May 29, 2023

Looks like the data plane team is only being pinged because there is no owner set for the azureeventhub input. Rather than requiring us to review, change the codeowner for this input to the team that actually owns it and then I'll approve that. That way we aren't needlessly pinged every time this code changes.

@lalit-satapathy
Copy link
Contributor

Looks like the data plane team is only being pinged because there is no owner set for the azureeventhub input. Rather than requiring us to review, change the codeowner for this input to the team that actually owns it and then I'll approve that. That way we aren't needlessly pinged every time this code changes.

Classic example of wrong team being pinged because not-up-to-date CODEOWNER entry and hence delaying the PR review process.

FYI: @andresrc @ruflin

@zmoog
Copy link
Contributor

zmoog commented May 31, 2023

Looks like the data plane team is only being pinged because there is no owner set for the azureeventhub input. Rather than requiring us to review, change the codeowner for this input to the team that actually owns it and then I'll approve that. That way we aren't needlessly pinged every time this code changes.

You are totally right @cmacknz. My apologies for the noise.

I opened a PR to assign the ownership of this input to my team.

@zmoog zmoog merged commit 4096f9b into elastic:main May 31, 2023
21 checks passed
@zmoog zmoog added the backport-v8.8.0 Automated backport with mergify label May 31, 2023
mergify bot pushed a commit that referenced this pull request May 31, 2023
* Add sanitization function and test for azure input

(cherry picked from commit 4096f9b)
@zmoog zmoog added the backport-v7.17.0 Automated backport with mergify label May 31, 2023
mergify bot pushed a commit that referenced this pull request May 31, 2023
* Add sanitization function and test for azure input

(cherry picked from commit 4096f9b)
chrisberkhout pushed a commit that referenced this pull request Jun 1, 2023
* Add sanitization function and test for azure input
zmoog added a commit that referenced this pull request Jun 2, 2023
…json (#35622)

* [Azure] Sanitize message in case of malformed json (#34874)

* Add sanitization function and test for azure input

(cherry picked from commit 4096f9b)

* Remove extra changelog items from cherry-pick

---------

Co-authored-by: lucianpy <59661554+lucianpy@users.noreply.github.com>
Co-authored-by: Maurizio Branca <maurizio.branca@elastic.co>
rdner added a commit that referenced this pull request Jul 7, 2023
… json (#35623)

* [Azure] Sanitize message in case of malformed json (#34874)

* Add sanitization function and test for azure input

(cherry picked from commit 4096f9b)

* Fix logger import

* fix linter

* Fix changelog

---------

Co-authored-by: lucianpy <59661554+lucianpy@users.noreply.github.com>
Co-authored-by: lucian-ioan <lucian.pyc@gmail.com>
Co-authored-by: Denis <denis.rechkunov@elastic.co>
@reakaleek reakaleek mentioned this pull request Jul 19, 2023
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-v7.17.0 Automated backport with mergify backport-v8.8.0 Automated backport with mergify Team:Cloud-Monitoring Label for the Cloud Monitoring team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[azure-evenhub input] fail to process AppServicePlatformLogs due to unescaped newlines
6 participants