awx icon indicating copy to clipboard operation
awx copied to clipboard

webhook notification body is not valid anymore

Open damcav35 opened this issue 1 year ago • 6 comments

Please confirm the following

  • [X] I agree to follow this project's code of conduct.
  • [X] I have checked the current issues for duplicates.
  • [X] I understand that AWX is open source software provided for free and that I might not receive a timely response.
  • [X] I am NOT reporting a (potential) security vulnerability. (These should be emailed to [email protected] instead.)

Bug Summary

We are using notifications on start / fail / sucess of jobs. This notifications are of type webhook, where the body contains the message and some values used by the recipient.

Body ex: {"roomId": "my_room", "notificationText": "my_text"}

Since 23.9.0, those notifications does not work anymore, the logs show an http 400 response.

It fails to pass this block : https://github.com/ansible/awx/blob/23.9.0/awx/main/models/notifications.py#L501-L510

The body containing only 1 line, it is considered as invalid.

This was working fine in 23.5.1

AWX version

23.9.0

Select the relevant components

  • [ ] UI
  • [ ] UI (tech preview)
  • [X] API
  • [ ] Docs
  • [ ] Collection
  • [ ] CLI
  • [ ] Other

Installation method

kubernetes

Modifications

no

Ansible version

2.15.9

Operating system

centos

Web browser

No response

Steps to reproduce

  • configure a notification template of type webhook. Use this kind of body:

{"roomId": "my_room", "notificationText": "my_text"}

  • Add this notification on a playbook start
  • Start a playbook

Expected results

a notification is sent by AWX and received by the application at the other end.

Actual results

logs on awx-task:

Traceback (most recent call last): File "/var/lib/awx/venv/awx/lib64/python3.9/site-packages/awx/main/tasks/system.py", line 346, in send_notifications sent = notification.notification_template.send(notification.subject, notification.body) File "/var/lib/awx/venv/awx/lib64/python3.9/site-packages/awx/main/models/notifications.py", line 186, in send return backend_obj.send_messages([notification_obj]) File "/var/lib/awx/venv/awx/lib64/python3.9/site-packages/awx/main/notifications/webhook_backend.py", line 119, in send_messages raise Exception(err) Exception: Error sending webhook notification: 400

when adding some logs there : https://github.com/ansible/awx/blob/23.9.0/awx/main/models/notifications.py#L501-L510 we can see the body does not match the condition.

Additional information

No response

damcav35 avatar Mar 13 '24 13:03 damcav35

@dmzoneill looks like some fall out from the work you just did, mind taking a look?

thedoubl3j avatar Mar 13 '24 15:03 thedoubl3j

@damcav35

Thanks for your bug.

It would be very helpful to see/understand your previous output. Presumably you have an example of the body you previously used to receive?

This check just makes sure that the body wasn't blank when generated by jinja. Is your result all one line?

Furthermore, when you say it "fails to pass the block". What do you mean by it fails to pass the block? You get the error message? The server crashes? The message you receive says "The template rendering return a blank body."?

The exception lines you quoted are not related to the link you provided.

https://github.com/ansible/awx/blob/23.9.0/awx/main/models/notifications.py#L462 Always returns a body, only in the instance the jinja template was blank I provide the warning.

Can you please clarify your bug request :)

Many thanks

if len(body.strip().splitlines()) <= 2:
            # blank body
            body = '\r\n'.join(
                [
                    "The template rendering return a blank body.",
                    "Please check the template.",
                    "Refer to https://github.com/ansible/awx/issues/13983",
                    "for further information.",
                ]
            )

dmzoneill avatar Mar 14 '24 10:03 dmzoneill

Hi, sorry for the lack of input, here are more informations.

For a bit of context, the meaning of this webhook is to add text in a chat application. So, when we start a playbook, a message pops up on a specific room in the chat. I don't have an example of the body received, as i don't manage the application receiving the notification.

The example i've put in the description is like the one we are sending, and the format expected by the receiver: {"roomId": "my_room", "notificationText": "my_text"}

There are also some specific headers for authentication etc.

We configure the notifications in AWX with the help of a playbook using the awx collection, if it can help, here is how it is defined in the playbook

      notification_configuration:
         headers:
            Content-Type: "application/json; charset=utf-8"
         url: "https://my_application/messages"
       body_message_started:
         roomId: "my_room_id"
         notificationText: "test"

what i mean by "it fails to pass the block", is that the condition is not successfull ( <= 2), so yes, i get the message :

                "The template rendering return a blank body.",
                "Please check the template.",
                "Refer to https://github.com/ansible/awx/issues/13983",
                "for further information.",

Log example :

awx.main.notifications.webhook_backend : The template rendering return a blank body. Please check the template. Refer to https://github.com/ansible/awx/issues/13983 for further information.  

And here is a log example inside build_notification_message() with:

  • the body i sent
  • the result of len(body.strip().splitlines())
2024-03-13 08:11:21,185 INFO     [b0ed3a08] awx.main.models.notifications : build_notifications - body: {"roomId": "my_room", "notificationText": "test"}
2024-03-13 08:11:21,185 INFO     [b0ed3a08] awx.main.models.notifications : build_notifications - len body splitted: 1

Hope it helps to clarify the bug :-)

damcav35 avatar Mar 14 '24 15:03 damcav35

ah okay, so this is helpful. I'll let in a fix to allow for just 1 line, i presume that's what's happening here. I dont recall the magic number 2 in my testing, but it seems we should allow for just one line output. quick fix coming

dmzoneill avatar Mar 14 '24 17:03 dmzoneill

I'll get this merged ASAP.

https://github.com/ansible/awx/pull/14995

dmzoneill avatar Mar 14 '24 17:03 dmzoneill

Another point: when clicking "test," AWX will send something like {"body": "Test Notification 1 https://towerhost"}.

If the expected format is something like the following (telegram bot, for example):

{
  "chat_id": "your-chat-id",
  "text": "test message"
}

the test will fail. However, sending a custom message associated with the template that follows the JSON structure above will work.

igorferrati avatar Sep 26 '24 22:09 igorferrati