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

webhook notification body is not valid anymore #14985

Open
5 of 11 tasks
damcav35 opened this issue Mar 13, 2024 · 6 comments
Open
5 of 11 tasks

webhook notification body is not valid anymore #14985

damcav35 opened this issue Mar 13, 2024 · 6 comments

Comments

@damcav35
Copy link

Please confirm the following

  • I agree to follow this project's code of conduct.
  • I have checked the current issues for duplicates.
  • I understand that AWX is open source software provided for free and that I might not receive a timely response.
  • I am NOT reporting a (potential) security vulnerability. (These should be emailed to security@ansible.com 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)
  • 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

@thedoubl3j
Copy link
Member

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

@dmzoneill
Copy link
Member

dmzoneill commented Mar 14, 2024

@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.",
                ]
            )

@damcav35
Copy link
Author

damcav35 commented Mar 14, 2024

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 :-)

@dmzoneill
Copy link
Member

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
Copy link
Member

I'll get this merged ASAP.

#14995

dmzoneill pushed a commit that referenced this issue Mar 15, 2024
dmzoneill pushed a commit that referenced this issue Mar 25, 2024
dmzoneill pushed a commit that referenced this issue Mar 25, 2024
dmzoneill pushed a commit that referenced this issue Mar 29, 2024
djyasin pushed a commit to djyasin/awx that referenced this issue Sep 16, 2024
@igorferrati
Copy link

igorferrati commented Sep 26, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants