-   Notifications  
You must be signed in to change notification settings  - Fork 509
 
o365: tolerate string JSON encodings of objects in Actions field #14944
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
Conversation
O365 may send the objects in the Actions field as their JSON encoding, so conditionally parse elements of the list when they are strings. This requires working around the absence of an 'if' for iteration processors in the foreach processor and the absence of a JSON parser in Painless.
 🚀 Benchmarks reportTo see the full report comment with   |  
|   Pinging @elastic/security-service-integrations (Team:Security-Service Integrations)  |  
| # Ensure that o365.audit.Actions is an array of Map. | ||
| - script: | ||
| description: 'This is the first half of an implementation of `if: _ingest._value instanceof String` for the foreach below.' | ||
| if: ctx.o365audit?.Actions != null | ||
| tag: script_select_string_actions | ||
| source: |- | ||
| ctx._tmp = [:]; | ||
| def actions = []; | ||
| ctx._tmp.action_strings = []; | ||
| if (!(ctx.o365audit.Actions instanceof List)) { | ||
| ctx.o365audit.Actions = [ctx.o365audit.Actions]; | ||
| } | ||
| for (def e: ctx.o365audit.Actions) { | ||
| if (e instanceof Map) { | ||
| actions.add(e); | ||
| } else if (e instanceof String) { | ||
| ctx._tmp.action_strings.add(e); | ||
| } | ||
| } | ||
| if (actions.length == ctx.o365audit.Actions.length) { | ||
| ctx._tmp.remove("action_strings"); | ||
| return | ||
| } | ||
| ctx.o365audit.Actions = actions; | ||
| - foreach: | ||
| tag: parse_string_actions_to_json | ||
| field: _tmp.action_strings | ||
| if: ctx._tmp?.action_strings != null | ||
| processor: | ||
| json: | ||
| field: _ingest._value | ||
| on_failure: | ||
| - append: | ||
| field: error.message | ||
| value: 'Processor {{{_ingest.on_failure_processor_type}}} with tag {{{_ingest.on_failure_processor_tag}}} in pipeline {{{_ingest.pipeline}}} failed with message: {{{_ingest.on_failure_message}}}' | ||
| - script: | ||
| description: 'This is the second half of an implementation of `if: _ingest._value instanceof String` for the foreach above.' | ||
| if: ctx._tmp?.action_strings != null | ||
| tag: script_select_string_actions | ||
| source: |- | ||
| ctx.o365audit = ctx.o365audit ?: [:]; | ||
| ctx.o365audit.Actions = ctx.o365audit.Actions ?: []; | ||
| for (def e: ctx._tmp.action_strings) { | ||
| ctx.o365audit.Actions.add(e); | ||
| } | ||
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.
I played around with using a stream to partition. Not necessary to use.
 # Ensure that o365.audit.Actions is an array of Map. - script: description: 'This is the first half of an implementation of `if: _ingest._value instanceof String` for the foreach below.' if: ctx.o365audit?.Actions != null tag: script_select_string_actions source: |- if (!(ctx.o365audit.Actions instanceof List)) { ctx.o365audit.Actions = [ctx.o365audit.Actions]; } Map parts = ctx.o365audit?.Actions.stream().collect(Collectors.partitioningBy(x -> x instanceof String)); ctx._tmp_action_strings = parts.get(true); ctx.o365audit.Actions = parts.get(false) ?: []; - foreach: tag: parse_string_actions_to_json field: _tmp_action_strings if: ctx._tmp_action_strings != null processor: json: field: _ingest._value on_failure: - append: field: error.message value: 'Processor {{{_ingest.on_failure_processor_type}}} with tag {{{_ingest.on_failure_processor_tag}}} in pipeline {{{_ingest.pipeline}}} failed with message: {{{_ingest.on_failure_message}}}' - script: description: 'This is the second half of an implementation of `if: _ingest._value instanceof String` for the foreach above.' if: ctx._tmp_action_strings != null tag: script_select_string_actions source: |- ctx.o365audit.Actions.addAll(ctx._tmp_action_strings); - remove: field: _tmp_action_strings ignore_missing: true 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.
I've taken the finaliser, but I prefer the setup that exists.
 💚 Build Succeeded
 History
 cc @efd6  |  
   |  
|   Package o365 - 2.24.0 containing this change is available at https://epr.elastic.co/package/o365/2.24.0/  |  




Proposed commit message
Note
Obviously, this is awful… the whole way down.
Checklist
changelog.ymlfile.Author's Checklist
How to test this PR locally
Related issues
Screenshots