-
I'm not sure if it's me doing something wrong but I'm having a real hard time getting the value I may be doing this the wrong way so please let me know if I am. How am I meant to access fields like @timestamp from the post2 alert payload? I can't do I did come up with a workaround, shown below, but it's really smelly. So I'm hoping someone has a better way or maybe this could become an enhancement/bug? alert:
- post2:
http_post2_url: https://localhost/webhook
http_post2_headers:
authorization: $api_key$
http_post2_all_values: true
# won't work because http_post2_raw_fields doesn't expect a dict as a value
# http_post2_raw_fields:
# signal:
# original_time: "@timestamp"
http_post2_payload:
signal:
# using self._TemplateReference__context as a workaround to access dot notation or elastic @ variables
original_time: "{{ self._TemplateReference__context['@timestamp'] }}"
rule:
name: $name$
description: $description$
severity: low
kibana:
alert:
ancestors:
- id: '{{ _id }}'
index: '{{ _index }}' EDIT: I forgot to mention that I'm using the latest docker image jertel/elastalert2 |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
Try this:
|
Beta Was this translation helpful? Give feedback.
-
@jertel I've noticed that because the rule payload is serialized to a json string, double quotes get escaped. If you have the following payload http_post2_payload:
body: '{{ _data["key"] }}' It gets dumped to a json string as {"body":"{{ _data[\"key\"] }}"} Which causes jinja to raise a A |
Beta Was this translation helpful? Give feedback.
-
There's another option to handle escaped data in the post payload. I kept going back and forth with some ideas because I don't necessarily like the idea of requiring a specific subset of yaml escaping or quotes to account for json rest calls being made with the post2 plugin. This would be a big change but I think I can do it while maintaining existing functionality. Currently only YAML is supported in the http_post2_payload: |
{
"posted_name": "toto",
"args_name": "{{_data["some_field"]}}",
"another_name": "{{_data['some_field']}",
"no_data_field": "{{some_field}}",
"field_{{some_field}}": "some value"
} This would also still work, but would require the use of specific quotes: http_post2_payload:
posted_name: toto
args_name: "{{_data['some_field']}}"
no_data_field: "{{some_field}}"
field_{{some_field}}: some value
# cannot do the following as it will error
# another_name: '{{_data["some_field"]}}'
# likewise, these will fail as well
# another_name: "{{_data["some_field"]}}"
# another_name: "{{_data[\"some_field\"]}}" Errors from syntax issues can still be raised as well. It also handles escaping json values appropriately. What are your thoughts here? If you like this idea, would you like the same treatment of headers? EDIT: another benefit to this is the ability to use jinja control flow like multiline data {"dict_variable":{"field1":"value1","field2":"value2"}} rule http_post2_payload: |
{
"posted_name": "toto"
{% for k,v in dict_variable.items() %}
,"{{ k }}": "{{ v }}"
{% endfor %}
,"last_field": "value"
} output {"posted_name":"toto","field1":"value1","field2":"value2","last_field":"value"} I made a copy of every test in |
Beta Was this translation helpful? Give feedback.
There's another option to handle escaped data in the post payload. I kept going back and forth with some ideas because I don't necessarily like the idea of requiring a specific subset of yaml escaping or quotes to account for json rest calls being made with the post2 plugin. This would be a big change but I think I can do it while maintaining existing functionality.
Currently only YAML is supported in the
http_post2_payload
field but that field gets converted to json when sending. Json and Yaml are fairly interchangeable but in this case it causes some weirdness with needing to escape certain characters or even using different quotes to account for these oddities. What do you think about …