Skip to content

Commit 303107f

Browse files
Sebastian Wagnerwaldbauer-certat
authored andcommitted
MAINT+TST: refactor and fix tests of smtp output bot
1 parent 1a5f00a commit 303107f

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

intelmq/bots/outputs/smtp/output.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from email.mime.text import MIMEText
88

99
from intelmq.lib.bot import Bot
10+
from typing import Optional
1011

1112

1213
class SMTPOutputBot(Bot):
@@ -15,23 +16,26 @@ class SMTPOutputBot(Bot):
1516
mail_from: str = "cert@localhost"
1617
mail_to: str = "{ev[source.abuse_contact]}"
1718
smtp_host: str = "localhost"
18-
smtp_password: str = None
19-
smtp_port: int = None
20-
smtp_username: str = None
19+
smtp_password: Optional[str] = None
20+
smtp_port: int = 25
21+
smtp_username: Optional[str] = None
2122
ssl: bool = False
2223
starttls: bool = True
2324
subject: str = "Incident in your AS {ev[source.asn]}"
2425
text: str = "Dear network owner,\\n\\nWe have been informed that the following device might have security problems.\\n\\nYour localhost CERT"
2526

26-
username = None
27-
password = None
2827
http_verify_cert = True
2928

3029
def init(self):
3130
if self.ssl:
3231
self.smtp_class = smtplib.SMTP_SSL
3332
else:
3433
self.smtp_class = smtplib.SMTP
34+
if self.http_verify_cert and self.smtp_class is smtplib.SMTP_SSL:
35+
self.kwargs = {'context': ssl.create_default_context()}
36+
else:
37+
self.kwargs = {}
38+
3539
if isinstance(self.fieldnames, str):
3640
self.fieldnames = self.fieldnames.split(',')
3741

@@ -41,25 +45,20 @@ def process(self):
4145

4246
csvfile = io.StringIO()
4347
writer = csv.DictWriter(csvfile, fieldnames=self.fieldnames,
44-
quoting=csv.QUOTE_MINIMAL, delimiter=str(";"),
48+
quoting=csv.QUOTE_MINIMAL, delimiter=";",
4549
extrasaction='ignore', lineterminator='\n')
4650
writer.writeheader()
4751
writer.writerow(event)
4852
attachment = csvfile.getvalue()
4953

50-
if self.http_verify_cert and self.smtp_class == smtplib.SMTP_SSL:
51-
kwargs = {'context': ssl.create_default_context()}
52-
else:
53-
kwargs = {}
54-
55-
with self.smtp_class(self.smtp_host, self.smtp_port, **kwargs) as smtp:
54+
with self.smtp_class(self.smtp_host, self.smtp_port, **self.kwargs) as smtp:
5655
if self.starttls:
5756
if self.http_verify_cert:
5857
smtp.starttls(context=ssl.create_default_context())
5958
else:
6059
smtp.starttls()
61-
if self.username and self.password:
62-
smtp.login(user=self.username, password=self.password)
60+
if self.smtp_username and self.smtp_password:
61+
smtp.login(user=self.smtp_username, password=self.smtp_password)
6362
msg = MIMEMultipart()
6463
if self.text is not None:
6564
msg.attach(MIMEText(self.text.format(ev=event)))

intelmq/tests/bots/outputs/smtp/test_output.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def set_bot(cls):
2828
"text": 'foobar',
2929
"subject": "type: {ev[classification.type]}",
3030
"mail_from": "myself",
31-
"mail_to": "you,yourself"}
31+
"mail_to": "you,yourself",
32+
"starttls": False,
33+
}
3234

3335
def test_event(self):
3436
self.input_message = EVENT
@@ -67,5 +69,6 @@ def test_multiple_recipients_event(self):
6769
self.assertEqual({'from_addr': 'myself', 'to_addrs': ['[email protected]', '[email protected]']},
6870
SENT_MESSAGE[1])
6971

72+
7073
if __name__ == '__main__': # pragma: no cover
7174
unittest.main()

0 commit comments

Comments
 (0)