Skip to content

Commit 49aae1a

Browse files
committed
Merge branch 'main' into 2889-drop-self-registration-and-review-user-creation-procedure
2 parents c7bfcaf + 5add0f8 commit 49aae1a

File tree

7 files changed

+17
-131
lines changed

7 files changed

+17
-131
lines changed

.github/workflows/oauth.yaml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,7 @@ jobs:
7474
FRACTAL_EMAIL_RECIPIENTS: [email protected],[email protected]
7575
FRACTAL_EMAIL_USE_STARTTLS: false
7676
FRACTAL_EMAIL_USE_LOGIN: true
77-
# FRACTAL_EMAIL_PASSWORD and FRACTAL_EMAIL_PASSWORD_KET are generated with the following command
78-
# `printf "fakepassword\n" | poetry run fractalctl encrypt-email-password`
79-
FRACTAL_EMAIL_PASSWORD: gAAAAABnoQUGHMsDgLkpDtwUtrKtf9T1so44ahEXExGRceAnf097mVY1EbNuMP5fjvkndvwCwBJM7lHoSgKQkZ4VbvO9t3PJZg==
80-
FRACTAL_EMAIL_PASSWORD_KEY: lp3j2FVDkzLd0Rklnzg1pHuV9ClCuDE0aGeJfTNCaW4=
81-
FRACTAL_HELP_URL: https://example.org/info
77+
FRACTAL_EMAIL_PASSWORD: fakepassword
8278
run: |
8379
fractalctl set-db
8480
fractalctl init-db-data --resource default --profile default --admin-email [email protected] --admin-pwd 1234 --admin-project-dir /fake

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ TBD
1111
\#2882
1212
\#2884
1313
\#2893
14+
\#2895 (remove email-password encryption)
15+
1416

1517
\#2890 (oauth self-registration)
1618

fractal_server/__main__.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,6 @@
9797
description="Apply data-migration script to an existing database.",
9898
)
9999

100-
# fractalctl encrypt-email-password
101-
encrypt_email_password_parser = subparsers.add_parser(
102-
"encrypt-email-password",
103-
description=(
104-
"Generate valid values for environment variables "
105-
"FRACTAL_EMAIL_PASSWORD and FRACTAL_EMAIL_PASSWORD_KEY."
106-
),
107-
)
108-
109100

110101
def save_openapi(dest="openapi.json"):
111102
from fractal_server.main import start_application
@@ -342,17 +333,6 @@ def _slugify_version(raw_version: str) -> str:
342333
current_update_db_data_module.fix_db()
343334

344335

345-
def print_encrypted_password():
346-
from cryptography.fernet import Fernet
347-
348-
password = input("Insert email password: ").encode("utf-8")
349-
key = Fernet.generate_key().decode("utf-8")
350-
encrypted_password = Fernet(key).encrypt(password).decode("utf-8")
351-
352-
print(f"\nFRACTAL_EMAIL_PASSWORD={encrypted_password}")
353-
print(f"FRACTAL_EMAIL_PASSWORD_KEY={key}")
354-
355-
356336
def run():
357337
args = parser.parse_args(sys.argv[1:])
358338

@@ -377,8 +357,6 @@ def run():
377357
port=args.port,
378358
reload=args.reload,
379359
)
380-
elif args.cmd == "encrypt-email-password":
381-
print_encrypted_password()
382360
else:
383361
sys.exit(f"Error: invalid command '{args.cmd}'.")
384362

fractal_server/app/security/signup_email.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from email.message import EmailMessage
33
from email.utils import formataddr
44

5-
from cryptography.fernet import Fernet
6-
75
from fractal_server.config import PublicEmailSettings
86
from fractal_server.logger import set_logger
97

@@ -50,14 +48,10 @@ def send_fractal_email_or_log_failure(
5048
server.starttls()
5149
server.ehlo()
5250
if email_settings.use_login:
53-
password = (
54-
Fernet(email_settings.encryption_key.get_secret_value())
55-
.decrypt(
56-
email_settings.encrypted_password.get_secret_value()
57-
)
58-
.decode("utf-8")
51+
server.login(
52+
user=email_settings.sender,
53+
password=email_settings.password.get_secret_value(),
5954
)
60-
server.login(user=email_settings.sender, password=password)
6155
server.sendmail(
6256
from_addr=email_settings.sender,
6357
to_addrs=email_settings.recipients,

fractal_server/config/_email.py

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from typing import Literal
22
from typing import Self
33

4-
from cryptography.fernet import Fernet
54
from pydantic import BaseModel
65
from pydantic import EmailStr
76
from pydantic import Field
@@ -32,8 +31,7 @@ class PublicEmailSettings(BaseModel):
3231
recipients: list[EmailStr] = Field(min_length=1)
3332
smtp_server: str
3433
port: int
35-
encrypted_password: SecretStr | None = None
36-
encryption_key: SecretStr | None = None
34+
password: SecretStr | None = None
3735
instance_name: str
3836
use_starttls: bool
3937
use_login: bool
@@ -54,10 +52,6 @@ class EmailSettings(BaseSettings):
5452
"""
5553
Password for the OAuth-signup email sender.
5654
"""
57-
FRACTAL_EMAIL_PASSWORD_KEY: SecretStr | None = None
58-
"""
59-
Key value for `cryptography.fernet` decrypt
60-
"""
6155
FRACTAL_EMAIL_SMTP_SERVER: str | None = None
6256
"""
6357
SMTP server for the OAuth-signup emails.
@@ -82,8 +76,7 @@ class EmailSettings(BaseSettings):
8276
FRACTAL_EMAIL_USE_LOGIN: Literal["true", "false"] = "true"
8377
"""
8478
Whether to use login when using the SMTP server.
85-
If 'true', FRACTAL_EMAIL_PASSWORD and FRACTAL_EMAIL_PASSWORD_KEY must be
86-
provided.
79+
If 'true', FRACTAL_EMAIL_PASSWORD must be provided.
8780
Accepted values: 'true', 'false'.
8881
"""
8982

@@ -120,49 +113,18 @@ def validate_email_settings(self: Self) -> Self:
120113
use_starttls = self.FRACTAL_EMAIL_USE_STARTTLS == "true"
121114
use_login = self.FRACTAL_EMAIL_USE_LOGIN == "true"
122115

123-
if use_login:
124-
if self.FRACTAL_EMAIL_PASSWORD is None:
125-
raise ValueError(
126-
"'FRACTAL_EMAIL_USE_LOGIN' is 'true' but "
127-
"'FRACTAL_EMAIL_PASSWORD' is not provided."
128-
)
129-
if self.FRACTAL_EMAIL_PASSWORD_KEY is None:
130-
raise ValueError(
131-
"'FRACTAL_EMAIL_USE_LOGIN' is 'true' but "
132-
"'FRACTAL_EMAIL_PASSWORD_KEY' is not provided."
133-
)
134-
try:
135-
(
136-
Fernet(
137-
self.FRACTAL_EMAIL_PASSWORD_KEY.get_secret_value()
138-
)
139-
.decrypt(
140-
self.FRACTAL_EMAIL_PASSWORD.get_secret_value()
141-
)
142-
.decode("utf-8")
143-
)
144-
except Exception as e:
145-
raise ValueError(
146-
"Invalid pair (FRACTAL_EMAIL_PASSWORD, "
147-
"FRACTAL_EMAIL_PASSWORD_KEY). "
148-
f"Original error: {str(e)}."
149-
)
150-
password = self.FRACTAL_EMAIL_PASSWORD.get_secret_value()
151-
else:
152-
password = None
153-
154-
if self.FRACTAL_EMAIL_PASSWORD_KEY is not None:
155-
key = self.FRACTAL_EMAIL_PASSWORD_KEY.get_secret_value()
156-
else:
157-
key = None
116+
if use_login and self.FRACTAL_EMAIL_PASSWORD is None:
117+
raise ValueError(
118+
"'FRACTAL_EMAIL_USE_LOGIN' is 'true' but "
119+
"'FRACTAL_EMAIL_PASSWORD' is not provided."
120+
)
158121

159122
self.public = PublicEmailSettings(
160123
sender=self.FRACTAL_EMAIL_SENDER,
161124
recipients=self.FRACTAL_EMAIL_RECIPIENTS.split(","),
162125
smtp_server=self.FRACTAL_EMAIL_SMTP_SERVER,
163126
port=self.FRACTAL_EMAIL_SMTP_PORT,
164-
encrypted_password=password,
165-
encryption_key=key,
127+
password=self.FRACTAL_EMAIL_PASSWORD,
166128
instance_name=self.FRACTAL_EMAIL_INSTANCE_NAME,
167129
use_starttls=use_starttls,
168130
use_login=use_login,

tests/no_version/test_commands.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,3 @@ def test_email_settings():
5656
)
5757
assert not res.stdout
5858
assert "usage" in res.stderr
59-
60-
cmd = (
61-
'printf "mypassword\n" | poetry run fractalctl encrypt-email-password'
62-
)
63-
res = subprocess.run(
64-
cmd,
65-
encoding="utf-8",
66-
capture_output=True,
67-
shell=True,
68-
)
69-
assert "FRACTAL_EMAIL_PASSWORD" in res.stdout
70-
assert "FRACTAL_EMAIL_PASSWORD_KEY" in res.stdout
71-
assert not res.stderr

tests/no_version/test_unit_config.py

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,6 @@ def test_get_oauth_router(override_oauth_settings_factory):
161161

162162

163163
def test_email_settings():
164-
from cryptography.fernet import Fernet
165-
166-
password = "password"
167-
FRACTAL_EMAIL_PASSWORD_KEY = Fernet.generate_key().decode("utf-8")
168-
FRACTAL_EMAIL_PASSWORD = (
169-
Fernet(FRACTAL_EMAIL_PASSWORD_KEY)
170-
.encrypt(password.encode("utf-8"))
171-
.decode("utf-8")
172-
)
173-
174164
required_mail_args = dict(
175165
FRACTAL_EMAIL_SENDER="[email protected]",
176166
FRACTAL_EMAIL_SMTP_SERVER="smtp_server",
@@ -186,23 +176,13 @@ def test_email_settings():
186176
EmailSettings(
187177
**required_mail_args,
188178
)
189-
# 3a: missing password
190-
with pytest.raises(ValidationError):
191-
EmailSettings(
192-
**required_mail_args,
193-
FRACTAL_EMAIL_PASSWORD_KEY=FRACTAL_EMAIL_PASSWORD_KEY,
194-
)
195-
# 3b missing password key
179+
# 3: missing password
196180
with pytest.raises(ValidationError):
197-
EmailSettings(
198-
**required_mail_args,
199-
FRACTAL_EMAIL_PASSWORD=FRACTAL_EMAIL_PASSWORD,
200-
)
181+
EmailSettings(**required_mail_args)
201182
# 4: ok
202183
email_settings = EmailSettings(
203184
**required_mail_args,
204-
FRACTAL_EMAIL_PASSWORD=FRACTAL_EMAIL_PASSWORD,
205-
FRACTAL_EMAIL_PASSWORD_KEY=FRACTAL_EMAIL_PASSWORD_KEY,
185+
FRACTAL_EMAIL_PASSWORD="password",
206186
)
207187
assert email_settings.public is not None
208188
assert len(email_settings.public.recipients) == 2
@@ -222,19 +202,6 @@ def test_email_settings():
222202
**{k: v for k, v in required_mail_args.items() if k != arg},
223203
FRACTAL_EMAIL_USE_LOGIN="false",
224204
)
225-
# 7a: fail with Fernet encryption
226-
with pytest.raises(ValidationError, match="FRACTAL_EMAIL_PASSWORD"):
227-
EmailSettings(
228-
**required_mail_args,
229-
FRACTAL_EMAIL_PASSWORD="invalid",
230-
FRACTAL_EMAIL_PASSWORD_KEY=FRACTAL_EMAIL_PASSWORD_KEY,
231-
)
232-
with pytest.raises(ValidationError, match="FRACTAL_EMAIL_PASSWORD"):
233-
EmailSettings(
234-
**required_mail_args,
235-
FRACTAL_EMAIL_PASSWORD=FRACTAL_EMAIL_PASSWORD,
236-
FRACTAL_EMAIL_PASSWORD_KEY="invalid",
237-
)
238205
# 8: fail with sender emails
239206
with pytest.raises(ValidationError):
240207
EmailSettings(

0 commit comments

Comments
 (0)