snowflakedb / SnowAlert

Security Analytics Using The Snowflake Data Warehouse
Apache License 2.0
184 stars 57 forks source link

STMP handler error: "please run connect() first" #553

Open aninandaprojects opened 3 years ago

aninandaprojects commented 3 years ago

Hi There !

I am trying to send an email when a alert gets created in my snowalert.result.alerts table

For that i have the following setting in my env file: SA_SMTP_SERVER=smtp.sendgrid.net SA_SMTP_PORT=587 SA_SMTP_SENDER_EMAIL=sender@gmail.com SA_SMTP_USER=apikey SA_SMTP_PASSWORD=<my-api-key>

As part of my alert creation I have the following code block : ARRAY_CONSTRUCT( OBJECT_CONSTRUCT( 'type', 'smtp', 'sender_email', 'sender@gmail.com', 'recipient_email', 'receiver@gmail.com', 'subject', 'this is a test of the SnowAlert alerting system', 'text', 'this is only a test', 'html', 'this is <b>only a test</b>' ) ) AS handlers

I also see the following logs while running the snowalert alerts: [22] SNOWFLAKE_WAREHOUSE_CREDITS_ALERT_QUERY processing... [22] SNOWFLAKE_WAREHOUSE_CREDITS_ALERT_QUERY created 0, updated 1 rows. [22] SNOWFLAKE_WAREHOUSE_CREDITS_ALERT_QUERY metadata recorded. [22] SNOWFLAKE_WAREHOUSE_CREDITS_ALERT_QUERY done.

However when I see the handled column in the snowalert.results.alerts table, I see the following error message:

[ { "details": { "exception": "smtplib.SMTPServerDisconnected: please run connect() first", "exceptionArgs": [ "please run connect() first" ], "exceptionName": "SMTPServerDisconnected", "traceback": "Traceback (most recent call last):\n File \"/var/task/snowalert/venv/lib/python3.7/site-packages/runners/alert_dispatcher.py\", line 85, in main\n handler_module.handle, **handler_kwargs\n File \"/var/task/snowalert/venv/lib/python3.7/site-packages/runners/utils.py\", line 77, in apply_some\n return f(**defaults)\n File \"/var/task/snowalert/venv/lib/python3.7/site-packages/runners/handlers/smtp.py\", line 87, in handle\n smtpserver.starttls(context=context)\n File \"/usr/local/lib/python3.7/smtplib.py\", line 749, in starttls\n self.ehlo_or_helo_if_needed()\n File \"/usr/local/lib/python3.7/smtplib.py\", line 600, in ehlo_or_helo_if_needed\n if not (200 <= self.ehlo()[0] <= 299):\n File \"/usr/local/lib/python3.7/smtplib.py\", line 440, in ehlo\n self.putcmd(self.ehlo_msg, name or self.local_hostname)\n File \"/usr/local/lib/python3.7/smtplib.py\", line 367, in putcmd\n self.send(str)\n File \"/usr/local/lib/python3.7/smtplib.py\", line 359, in send\n raise SMTPServerDisconnected('please run connect() first')\nsmtplib.SMTPServerDisconnected: please run connect() first\n" }, "success": false } ]

Can some one advise what i could be missing here?

sfc-gh-afedorov commented 3 years ago
Traceback (most recent call last):
 File "/var/task/snowalert/venv/lib/python3.7/site-packages/runners/alert_dispatcher.py", line 85, in main
 handler_module.handle, **handler_kwargs
 File "/var/task/snowalert/venv/lib/python3.7/site-packages/runners/utils.py", line 77, in apply_some
 return f(**defaults)
 File "/var/task/snowalert/venv/lib/python3.7/site-packages/runners/handlers/smtp.py", line 87, in handle
 smtpserver.starttls(context=context)
 File "/usr/local/lib/python3.7/smtplib.py", line 749, in starttls
 self.ehlo_or_helo_if_needed()
 File "/usr/local/lib/python3.7/smtplib.py", line 600, in ehlo_or_helo_if_needed
 if not (200 <= self.ehlo()[0] <= 299):
 File "/usr/local/lib/python3.7/smtplib.py", line 440, in ehlo
 self.putcmd(self.ehlo_msg, name or self.local_hostname)
 File "/usr/local/lib/python3.7/smtplib.py", line 367, in putcmd
 self.send(str)
 File "/usr/local/lib/python3.7/smtplib.py", line 359, in send
 raise SMTPServerDisconnected('please run connect() first')
smtplib.SMTPServerDisconnected: please run connect() first

this looks like a bug in ./src/runners/handlers/smtp.py#L87 calling smtpserver.starttls before the connect() method was run. Would you be able to debug it, or should we slot it somewhere?

aninandaprojects commented 3 years ago

Hi @sfc-gh-afedorov can you please slot it. If so what could be the ETA around implementing a fix?

sfc-gh-afedorov commented 3 years ago

What kind of SMTP server are you using? we have some tests for a version of this code in GEFF that does not reproduce this error on SendGrid or Google mail server, where we've tested it.

aninandaprojects commented 3 years ago

hey @sfc-gh-afedorov apologies for the late reply....i have tested this using a Sendgrid SMTP server.