prefect icon indicating copy to clipboard operation
prefect copied to clipboard

MattermostWebhook does not send message to https server

Open zepaz opened this issue 1 year ago • 1 comments

First check

  • [X] I added a descriptive title to this issue.
  • [X] I used the GitHub search to find a similar issue and didn't find it.
  • [X] I searched the Prefect documentation for this issue.
  • [X] I checked that this issue is related to Prefect and not one of its dependencies.

Bug summary

Loading in the prefect Block and trying to send a message results in a status code 400 because the message is attempted to be send over an http:// connection instead of the https:// connection.

Reproduction

Code:

from prefect.blocks.notifications import MattermostWebhook
mattermost_webhook_block = MattermostWebhook.load("mattermost")
mattermost_webhook_block.notify("Hello from Prefect!")

This is in my Block:

MattermostWebhook(notify_type='prefect_default',
hostname='my-mattermost-server', 
token=SecretStr('**********'), 
botname='Test', 
channels=None, 
include_image=False, 
path=None, 
port=443)

Error

Log:
`
| WARNING | apprise - Failed to send Mattermost notification: Bad Request - Unsupported Parameters., error=400.
`
Requests.response:

b'<html>\r\n<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>\r\n<body>\r\n<center><h1>400 Bad Request</h1></center>\r\n<center>The plain HTTP request was sent to HTTPS port</center>\r\n<hr><center>nginx/1.20.1</center>\r\n</body>\r\n</html>\r\n'


### Versions

```Text
Version:             2.16.4
API version:         0.8.4                    
Python version:      3.11.3                   
Git commit:          e3e7df9d                 
Built:               Thu, Mar 14, 2024 5:11 PM
OS/Arch:             win32/AMD64              
Profile:             dev                      
Server type:         server

Additional context

It looks like the problem is that when apprise gets initialized by

NotifyMattermost(
                token=self.token.get_secret_value(),
                fullpath=self.path,
                host=self.hostname,
                botname=self.botname,
                channels=self.channels,
                include_image=self.include_image,
                port=self.port,
)

it does not set a parameter for secure nor schema. If either secure or schema would get send, the URLBase class would have set self.secure = True and that would change the schema to https:// instead it sets self.secure = False and thus the schema used is http://

zepaz avatar May 22 '24 14:05 zepaz

for anyone reading along, https://github.com/PrefectHQ/prefect/discussions/8141#discussioncomment-4687562 has a workaround. In short; switch to slackwebhook, although not all Mattermost features are supported

zepaz avatar May 22 '24 15:05 zepaz

It is indeed a pity to use Slack webhook, which has less functionality.

For the record, the Mattermost block was added by #8341 (@zzstoatzz )

The support of the secure servers should be quite straightforward:

add

secure: bool = Field(
        default=False,
        description="Set True if the Mattermost server is running over https.",
    )

to https://github.com/PrefectHQ/prefect/blob/33339e14c52ff70427d4ad15d1c5d672c22804fa/src/prefect/blocks/notifications.py#L557

and

        url = SecretStr(
            NotifyMattermost(
                token=self.token.get_secret_value(),
                fullpath=self.path,
                host=self.hostname,
                botname=self.botname,
                channels=self.channels,
                include_image=self.include_image,
                port=self.port,
                secure=self.secure,
            ).url()

to

https://github.com/PrefectHQ/prefect/blob/33339e14c52ff70427d4ad15d1c5d672c22804fa/src/prefect/blocks/notifications.py#L571

Unfortunately, this is the maximum I can afford for this issue at the moment. I may eventually create the pull request; however, I would appreciate it if somebody else took it from here.

Anyway, many thanks for the Prefect project!

kripnerl avatar Dec 11 '24 14:12 kripnerl

FYI this is mostly sorted in PR https://github.com/PrefectHQ/prefect/pull/16884 which lets you set secure to True if you are sending HTTPs traffic.

But then I also found a bug in apprise so you might need https://github.com/caronc/apprise/pull/1293 (unmerged right now)

sochotnicky avatar Feb 25 '25 16:02 sochotnicky