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

Add EML attachments #26958

Merged
merged 25 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
68b59ba
Debugs
gal-forer Apr 30, 2023
073755a
Debugs
gal-forer May 7, 2023
3a60580
Working attachments
gal-forer May 23, 2023
70ba240
Working attachments
gal-forer May 23, 2023
2c4d6af
Release notes
gal-forer May 23, 2023
3495acf
Merge branch 'master' into test-eml-bug
gal-forer May 23, 2023
77b6efe
Added and fixed unit tests
gal-forer May 23, 2023
31e3555
Merge remote-tracking branch 'origin/test-eml-bug' into test-eml-bug
gal-forer May 23, 2023
30d929b
Added and fixed unit tests
gal-forer May 23, 2023
6a074be
Removed unstable test and formats
gal-forer May 24, 2023
f99e489
Removed unstable test and formats
gal-forer May 24, 2023
858a537
Merge branch 'master' into test-eml-bug
gal-forer May 28, 2023
9526112
Update Packs/MailListener/ReleaseNotes/1_0_37.md
gal-forer May 28, 2023
593ef6c
Merge branch 'master' into test-eml-bug
gal-forer May 28, 2023
550b70f
Update docker image
gal-forer May 28, 2023
4e311e1
Merge branch 'master' into test-eml-bug
gal-forer May 29, 2023
c821700
Added test data to solve the secrets validation timeout
gal-forer May 29, 2023
4e48af2
Merge branch 'master' into test-eml-bug
gal-forer May 29, 2023
2a277f6
Added test data to solve the secrets validation timeout
gal-forer May 29, 2023
d33ed00
Added test data to solve the secrets validation timeout
gal-forer May 29, 2023
848e630
Added test data to solve the secrets validation timeout
gal-forer May 29, 2023
4cdb99f
Added test data to solve the secrets validation timeout
gal-forer May 29, 2023
cd78cc5
Added test data to solve the secrets validation timeout
gal-forer May 29, 2023
85c4e29
Added test data to solve the secrets validation timeout
gal-forer May 29, 2023
29487cf
Added test data to solve the secrets validation timeout
gal-forer May 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions Packs/MailListener/Integrations/MailListenerV2/MailListenerV2.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ssl
import email
from datetime import timezone
from typing import Any, Dict, Tuple, List, Optional

Expand Down Expand Up @@ -31,12 +32,13 @@ def __init__(self, message_bytes: bytes, include_raw_body: bool, save_file: bool
email_object = parse_from_bytes(message_bytes)
except Exception:
email_object = parse_from_string(message_bytes.decode('ISO-8859-1'))

eml_attachments = self.get_eml_attachments(message_bytes)
self.id = id_
self.to = [mail_addresses for _, mail_addresses in email_object.to]
self.cc = [mail_addresses for _, mail_addresses in email_object.cc]
self.bcc = [mail_addresses for _, mail_addresses in email_object.bcc]
self.attachments = email_object.attachments
self.attachments.extend(eml_attachments)
self.from_ = [mail_addresses for _, mail_addresses in email_object.from_][0]
self.format = email_object.message.get_content_type()
self.html = email_object.text_html[0] if email_object.text_html else ''
Expand All @@ -51,6 +53,28 @@ def __init__(self, message_bytes: bytes, include_raw_body: bool, save_file: bool
self.labels = self._generate_labels()
self.message_id = email_object.message_id

@staticmethod
def get_eml_attachments(message_bytes: bytes) -> list:
eml_attachments = []
msg = email.message_from_bytes(message_bytes)
if msg:
for part in msg.walk():
if part.get_content_maintype() == "multipart" or part.get("Content-Disposition") is None:
continue

filename = part.get_filename()
if filename and filename.endswith('.eml'):
eml_attachments.append({
"filename": filename,
"payload": part.get_payload(decode=False)[0].as_bytes(),
"binary": False,
"mail_content_type": part.get_content_subtype(),
"content-id": part.get('content-id'),
"content-disposition": part.get('content-disposition'),
"charset": part.get_content_charset(),
"content_transfer_encoding": part.get_content_charset()})
return eml_attachments

@staticmethod
def handle_message_slashes(message_bytes: bytes) -> bytes:
"""
Expand Down Expand Up @@ -409,7 +433,7 @@ def generate_search_query(time_to_fetch_from: Optional[datetime],
return messages_query_list


def test_module(client: IMAPClient) -> str:
def script_test_module(client: IMAPClient) -> str:
Copy link
Contributor Author

@gal-forer gal-forer May 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed thew name because if it started with test_ the IDE will think it's a unit test

yesterday = parse('1 day UTC')
client.search(['SINCE', yesterday])
return 'ok'
Expand All @@ -420,7 +444,7 @@ def list_emails(client: IMAPClient,
with_headers: bool,
permitted_from_addresses: str,
permitted_from_domains: str,
_limit: int,) -> CommandResults:
_limit: int, ) -> CommandResults:
"""
Lists all emails that can be fetched with the given configuration and return a preview version of them.
Args:
Expand Down Expand Up @@ -503,7 +527,7 @@ def main():
client.login(username, password)
client.select_folder(folder)
if demisto.command() == 'test-module':
result = test_module(client)
result = script_test_module(client)
demisto.results(result)
elif demisto.command() == 'mail-listener-list-emails':
return_results(list_emails(client=client,
Expand Down
981 changes: 921 additions & 60 deletions Packs/MailListener/Integrations/MailListenerV2/MailListenerV2_test.py

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Packs/MailListener/ReleaseNotes/1_0_37.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#### Integrations

##### Mail Listener v2

- Added the ability to have EML files as attachments.
gal-forer marked this conversation as resolved.
Show resolved Hide resolved
- Updated the Docker image to: *demisto/py3-tools:1.0.0.54695*.
2 changes: 1 addition & 1 deletion Packs/MailListener/pack_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Mail Listener",
"description": "Listen to a mailbox, enable incident triggering via e-mail",
"support": "xsoar",
"currentVersion": "1.0.36",
"currentVersion": "1.0.37",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down