Skip to content

Added Email Alert Functionality - #129

Merged
ManofWax merged 7 commits into
certego:developfrom
kunalsz:alert_email
Mar 7, 2025
Merged

ManofWax merged 7 commits into
certego:developfrom
kunalsz:alert_email

Conversation

@kunalsz

@kunalsz kunalsz commented Feb 22, 2025

Copy link
Copy Markdown
Contributor

I had already implemented the email alerting functionality but given the new Alert classes ,I have reimplemented it.
In reference to the issue #126

Changes made

  • Google's SMTP service has been implemented. This requires an app password and your email address to send emails
  • A new file inside impossible_travel/alerting is created email_alerting.py to send the emails to a specific address using Django's send_email functionality
  • tests are created to check if the email is sent correctly or not

Outcomes

  • emails are generated successfully when tests are ran
  • test is getting failed even when the email is being recieved in the reciever's mailbox. The issue can be with the @patch functionality implemented in the tests/test_alert_email.py. Will work on this.

placeholders are left in the place of actual email addresses and app password

Looking forward for your insights and advices !
@ManofWax

@ManofWax

ManofWax commented Feb 24, 2025

Copy link
Copy Markdown
Contributor

Looks good to me. Tommorrow We are going to fix the github action linter bug (it is on our side, I've already checked) and merge the PR

@ManofWax

Copy link
Copy Markdown
Contributor

Hi @kunalsz, can you run linters and formatters as documented here? https://github.com/certego/BuffaLogs/blob/main/CONTRIBUTING.md

@kunalsz

kunalsz commented Feb 25, 2025

Copy link
Copy Markdown
Contributor Author

@ManofWax sure, I'll do that and update you.

@kunalsz

kunalsz commented Feb 27, 2025

Copy link
Copy Markdown
Contributor Author

Hi @ManofWax , there were some problems in linting on my side. I have fixed them and linted code. Please review it.

@Lorygold
Lorygold self-requested a review February 28, 2025 14:00
@Lorygold Lorygold linked an issue Feb 28, 2025 that may be closed by this pull request
@Lorygold Lorygold removed a link to an issue Feb 28, 2025
@kunalsz
kunalsz changed the base branch from main to develop February 28, 2025 17:13
@Lorygold

Lorygold commented Mar 3, 2025

Copy link
Copy Markdown
Contributor

Hi @kunalsz , linters have been fixed in the PR #146. So, please align your feature branch with the develop one and resolve the conflicts with your code

@ManofWax ManofWax linked an issue Mar 3, 2025 that may be closed by this pull request
@kunalsz

kunalsz commented Mar 4, 2025

Copy link
Copy Markdown
Contributor Author

@Lorygold I will align my code with the updates soon.

kunalsz added 3 commits March 4, 2025 23:54
Signed-off-by: kunalsz <kunalavengers@gmail.com>
Signed-off-by: kunalsz <kunalavengers@gmail.com>
Signed-off-by: kunalsz <kunalavengers@gmail.com>
Signed-off-by: kunalsz <kunalavengers@gmail.com>
@Lorygold
Lorygold requested a review from ManofWax March 6, 2025 08:56
@Lorygold

Lorygold commented Mar 6, 2025

Copy link
Copy Markdown
Contributor

Hi @kunalsz, there's an error in your test:

  File "/home/runner/work/BuffaLogs/BuffaLogs/buffalogs/impossible_travel/tests/test_alert_email.py", line 42, in test_email_args
    self.assertEqual(email.from_email, "BuffaLogs Alerts SENDER_EMAIL_ADDRESS")
AssertionError: 'BuffaLogs Alerts SENDER_EMAIL_ADRESS' != 'BuffaLogs Alerts SENDER_EMAIL_ADDRESS'
- BuffaLogs Alerts SENDER_EMAIL_ADRESS
+ BuffaLogs Alerts SENDER_EMAIL_ADDRESS

Before committing, you can run tests locally launching > ./manage.py test in order to check if the tests pass.

Signed-off-by: kunalsz <kunalavengers@gmail.com>
@kunalsz

kunalsz commented Mar 7, 2025

Copy link
Copy Markdown
Contributor Author

@Lorygold I have fixed that error but I am facing this error which is not originating from my code

ERROR: test_process_logs_loop (impossible_travel.tests.test_tasks.TestTasks.test_process_logs_loop)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/kali/Desktop/gsoc/BuffaLogs/buffalogs/impossible_travel/tests/test_tasks.py", line 292, in test_process_logs_loop
    tasks.process_logs()
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/celery/local.py", line 182, in __call__
    return self._get_current_object()(*a, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/celery/app/task.py", line 411, in __call__
    return self.run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/buffalogs/impossible_travel/tasks.py", line 243, in process_logs
    exec_process_logs(start_date, end_date)
  File "/home/kali/Desktop/gsoc/BuffaLogs/buffalogs/impossible_travel/tasks.py", line 280, in exec_process_logs
    response = s.execute()
               ^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch_dsl/_sync/search.py", line 88, in execute
    es.search(index=self._index, body=self.to_dict(), **self._params)
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch/_sync/client/utils.py", line 455, in wrapped
    return api(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch/_sync/client/__init__.py", line 5061, in search
    return self.perform_request(  # type: ignore[return-value]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch/_sync/client/_base.py", line 271, in perform_request
    response = self._perform_request(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch/_sync/client/_base.py", line 352, in _perform_request
    raise HTTP_EXCEPTIONS.get(meta.status, ApiError)(
elasticsearch.BadRequestError: BadRequestError(400, 'search_phase_execution_exception', 'Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [user.name] in order to load field data by uninverting the inverted index. Note that this can use significant memory.')

This can be fixed by updating this code in buffalogs/impossible_travel/tasks.py

s.aggs.bucket("login_user", "terms", field="user.name", size=10000)
TO
s.aggs.bucket("login_user", "terms", field="user.name.keyword", size=10000)

Should I proceed with that ?

Signed-off-by: kunalsz <kunalavengers@gmail.com>
@Lorygold

Lorygold commented Mar 7, 2025

Copy link
Copy Markdown
Contributor

@Lorygold I have fixed that error but I am facing this error which is not originating from my code

ERROR: test_process_logs_loop (impossible_travel.tests.test_tasks.TestTasks.test_process_logs_loop)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/kali/Desktop/gsoc/BuffaLogs/buffalogs/impossible_travel/tests/test_tasks.py", line 292, in test_process_logs_loop
    tasks.process_logs()
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/celery/local.py", line 182, in __call__
    return self._get_current_object()(*a, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/celery/app/task.py", line 411, in __call__
    return self.run(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/buffalogs/impossible_travel/tasks.py", line 243, in process_logs
    exec_process_logs(start_date, end_date)
  File "/home/kali/Desktop/gsoc/BuffaLogs/buffalogs/impossible_travel/tasks.py", line 280, in exec_process_logs
    response = s.execute()
               ^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch_dsl/_sync/search.py", line 88, in execute
    es.search(index=self._index, body=self.to_dict(), **self._params)
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch/_sync/client/utils.py", line 455, in wrapped
    return api(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch/_sync/client/__init__.py", line 5061, in search
    return self.perform_request(  # type: ignore[return-value]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch/_sync/client/_base.py", line 271, in perform_request
    response = self._perform_request(
               ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/kali/Desktop/gsoc/BuffaLogs/venv/lib/python3.12/site-packages/elasticsearch/_sync/client/_base.py", line 352, in _perform_request
    raise HTTP_EXCEPTIONS.get(meta.status, ApiError)(
elasticsearch.BadRequestError: BadRequestError(400, 'search_phase_execution_exception', 'Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default. Please use a keyword field instead. Alternatively, set fielddata=true on [user.name] in order to load field data by uninverting the inverted index. Note that this can use significant memory.')

This can be fixed by updating this code in buffalogs/impossible_travel/tasks.py

s.aggs.bucket("login_user", "terms", field="user.name", size=10000)
TO
s.aggs.bucket("login_user", "terms", field="user.name.keyword", size=10000)

Should I proceed with that ?

It's an error that is present only in your workspace, so probably I think that you didn't load the template on Elasticsearch, running:

> cd config/elasticsearch/
> ./load_templates.sh

Please, commit again to: s.aggs.bucket("login_user", "terms", field="user.name", size=10000)

Comment thread buffalogs/impossible_travel/tasks.py
Signed-off-by: kunalsz <kunalavengers@gmail.com>
@ManofWax
ManofWax merged commit 7e38a14 into certego:develop Mar 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alerter: add email alerter

3 participants