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

✨♻️ Differentiate between TLS and STARTTLS in web-mailserver (⚠️ devops) #2965

Conversation

mrnicegyu11
Copy link
Member

@mrnicegyu11 mrnicegyu11 commented Apr 6, 2022

What do these changes do?

This enables the mail-sending functionality of the webserver to handle STARTTLS and TLS. Before, only STARTTLS was possible. Furthermore, there were naming ambigouitites , as the env-vars referred to TLS, which is different from STARTTLS

Bonus:

  • Adds settings-test ensuring that not both STARTTLS and TLS are simultaneously requested
  • Updates black-linter in precommit-hook, which would crash on my dev-PC in the old version

⚠️ devops:
Deprecates the Env-Var:
SMTP_TLS_ENABLED
Introduces the Env-Var:
SMTP_PROTOCOL
which can have the values

  • UNENCRYPTED
  • TLS
  • STARTTLS

Related issue/s

How to test

I did a dry-run test, but since we do not fully emulate an external mail-server in our tests this will have to be properly checked in master. The dry-run test involved running the send_mail(app: web.Application, msg: MIMEText) function vanilla in python, outside oSparc.

Checklist

  • Unit tests for the changes exist
  • Runs in the swarm
  • Documentation reflects the changes

@mrnicegyu11 mrnicegyu11 added t:enhancement Improvement or request on an existing feature bug buggy, it does not work as expected a:webserver issue related to the webserver service dependencies t:maintenance Some planned maintenance work security Pull requests that address a security vulnerability labels Apr 6, 2022
@mrnicegyu11 mrnicegyu11 self-assigned this Apr 6, 2022
@mrnicegyu11 mrnicegyu11 added this to the E.Shackleton milestone Apr 6, 2022
@codecov
Copy link

codecov bot commented Apr 6, 2022

Codecov Report

Merging #2965 (4fc0951) into master (075b043) will increase coverage by 0.8%.
The diff coverage is 50.0%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #2965     +/-   ##
========================================
+ Coverage    83.5%   84.4%   +0.8%     
========================================
  Files         848     647    -201     
  Lines       35763   28529   -7234     
  Branches      748     178    -570     
========================================
- Hits        29876   24079   -5797     
+ Misses       5697    4392   -1305     
+ Partials      190      58    -132     
Flag Coverage Δ
integrationtests 67.5% <21.4%> (-0.1%) ⬇️
unittests 80.6% <50.0%> (+0.1%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...re_service_webserver/application_settings_utils.py 97.7% <ø> (-0.1%) ⬇️
...erver/src/simcore_service_webserver/login/utils.py 73.6% <8.3%> (-3.9%) ⬇️
...ges/settings-library/src/settings_library/email.py 94.5% <90.0%> (-5.5%) ⬇️
...er/src/simcore_service_webserver/login/settings.py 100.0% <100.0%> (ø)
...tor_v2/modules/dynamic_sidecar/scheduler/_utils.py 92.0% <0.0%> (-5.0%) ⬇️
...rc/simcore_service_catalog/db/repositories/dags.py 44.4% <0.0%> (-2.8%) ⬇️
...simcore_service_director_v2/modules/node_rights.py 98.1% <0.0%> (-1.0%) ⬇️
...ector_v2/modules/dynamic_sidecar/scheduler/task.py 80.4% <0.0%> (-0.7%) ⬇️
...imcore_service_webserver/garbage_collector_core.py 69.8% <0.0%> (-0.7%) ⬇️
... and 204 more

@mrnicegyu11 mrnicegyu11 changed the title ✨ Differentiate between TLS and STARTTLS in web-mailserver ✨♻️ Differentiate between TLS and STARTTLS in web-mailserver Apr 6, 2022
@mrnicegyu11 mrnicegyu11 requested review from Surfict and removed request for Surfict April 6, 2022 09:48
@mrnicegyu11 mrnicegyu11 requested review from Surfict and mguidon April 6, 2022 12:28
@mrnicegyu11 mrnicegyu11 marked this pull request as ready for review April 6, 2022 12:30
@@ -22,6 +22,6 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 21.9b0
rev: 22.3.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure about bumping this here. @sanderegg @pcrespov ?

Copy link
Member

Choose a reason for hiding this comment

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

The pre-commit scripts run on their own virtualenv (with is different from our .venv) so bumping the version there is ok. Nonetheless there should be some level of compatibility with the tools used in vscode (which are installed in our .venv).

This number should be at least compatible (if possible identical) with the version we have in the requirements/_tool.txt (see screenshot below)... otherwise it is very confusing to format in one way with vscode tooling and then again with the pre-commit. Same applies for isort.

That said, as you can see we use 22.1.0 so that hsould be the version you put there.

image

log.info("Unencrypted connection attempt to mailserver ...")
await smtp.connect(use_tls=False, port=cfg.SMTP_PORT)
log.info("Starting STARTTLS ...")
log.warning("Certificates are not validated for STARTTLS on port 587!")
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure I understand the reason for this warning here

Copy link
Member Author

Choose a reason for hiding this comment

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

It is not really perfect to use encrypted communication but trust all certificates, the gain in security is marginal. I just thought the least I can do at this point (didnt wanna open up a whole big pandoras box and follow up on why it is coded like this now), was to add a warning.

But of course this log can be nerfed or removed

Copy link
Contributor

@GitHK GitHK Apr 7, 2022

Choose a reason for hiding this comment

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

I would suggest something like the following. Please use the actual port from the configuration.

log.warning(
    f"Certificates are not validated for STARTTLS (as it happens for TLS) on port {cfg.SMTP_PORT}!"
)

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure I understand the whole thing here. Is this a warning that no one will check even though we should tackle it? in which case please create an issue.
Is this workaround here because of the library? Is that the way it works? then I would not show any warning.
And if this is a security issue, should we really use starttls?

Copy link
Contributor

Choose a reason for hiding this comment

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

It's slightly better than unencrypted. In theory we should get a better mail server, since we run it, which supports TLS and be done with it.

Co-authored-by: Andrei Neagu <5694077+GitHK@users.noreply.github.com>
@mrnicegyu11 mrnicegyu11 requested a review from GitHK April 6, 2022 13:40
Copy link
Member

@pcrespov pcrespov left a comment

Choose a reason for hiding this comment

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

As discussed, check the possibility with Enums. IMO it iwll be simpler to maintain

@@ -22,6 +22,6 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 21.9b0
rev: 22.3.0
Copy link
Member

Choose a reason for hiding this comment

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

The pre-commit scripts run on their own virtualenv (with is different from our .venv) so bumping the version there is ok. Nonetheless there should be some level of compatibility with the tools used in vscode (which are installed in our .venv).

This number should be at least compatible (if possible identical) with the version we have in the requirements/_tool.txt (see screenshot below)... otherwise it is very confusing to format in one way with vscode tooling and then again with the pre-commit. Same applies for isort.

That said, as you can see we use 22.1.0 so that hsould be the version you put there.

image

@root_validator(pre=True)
@classmethod
def not_both_tls_and_starttls(cls, values):
if values.get("SMTP_TLS_ENABLED") and values.get("SMTP_STARTTLS_ENABLED"):
Copy link
Member

Choose a reason for hiding this comment

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

when you have multiple exclusive choices it is better if you can a single field (i.e. env variable) associated with an enum that has all the choice. Can you afford that?

Copy link
Member

@sanderegg sanderegg left a comment

Choose a reason for hiding this comment

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

all good. thanks for integrating the changes. Maybe just check the warning message if it makes sense like this.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@mrnicegyu11 mrnicegyu11 requested a review from pcrespov November 3, 2022 16:02
Copy link
Member

@pcrespov pcrespov left a comment

Choose a reason for hiding this comment

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

As agreed fix the smtp.connect and set the logs in info mode. thx 🎉

@mrnicegyu11 mrnicegyu11 requested a review from GitHK November 3, 2022 16:26
Copy link
Contributor

@GitHK GitHK left a comment

Choose a reason for hiding this comment

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

👍

@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 4, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@mrnicegyu11 mrnicegyu11 merged commit fd8c9d1 into ITISFoundation:master Nov 4, 2022
@mrnicegyu11 mrnicegyu11 deleted the enhancement/addSTARTTLSsupportForMailserver branch November 4, 2022 13:37
mrnicegyu11 pushed a commit to mrnicegyu11/osparc-ops-environments that referenced this pull request Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:webserver issue related to the webserver service bug buggy, it does not work as expected security Pull requests that address a security vulnerability t:enhancement Improvement or request on an existing feature t:maintenance Some planned maintenance work
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature: Enable possibilty to use STARTTLS for sending mails from oSparc Can't send email
6 participants