-
-
Notifications
You must be signed in to change notification settings - Fork 67
protect small servers by rejecting new addresses under resource constraints / capping smtp/imap connection limits #1019
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Detect whether the system is at its limits.""" | ||
|
|
||
| import logging | ||
|
|
||
| import psutil | ||
|
|
||
| MB = 1024 * 1024 | ||
|
|
||
|
|
||
| def read_value(getter): | ||
| try: | ||
| return getter() | ||
| except Exception as e: | ||
| logging.warning("ignoring unreadable system limit: %s", e) | ||
| return None | ||
|
|
||
|
|
||
| def has_sufficient_resources(config): | ||
| """Return False if load, memory or disk exceeds a configured limit.""" | ||
| load = read_value(lambda: psutil.getloadavg()[0]) | ||
| mem = read_value(lambda: psutil.virtual_memory().available // MB) | ||
| disk = read_value(lambda: psutil.disk_usage(str(config.mailboxes_dir)).free // MB) | ||
| if load is not None and load > config.max_load_1m: | ||
| msg = f"load avg {load:.2f} > {config.max_load_1m:.2f}" | ||
| elif mem is not None and mem < config.min_available_memory_mb: | ||
| msg = f"available memory {mem}MB < {config.min_available_memory_mb}MB" | ||
| elif disk is not None and disk < config.min_free_disk_space_mb: | ||
| msg = f"free disk {disk}MB < {config.min_free_disk_space_mb}MB" | ||
| else: | ||
| return True | ||
| logging.warning("registration rejected: %s", msg) | ||
| return False |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import shutil | ||
|
|
||
| import psutil | ||
|
|
||
| from chatmaild.syslimits import has_sufficient_resources | ||
|
|
||
| PERMISSIVE = { | ||
| "max_load_1m": "99999", | ||
| "min_available_memory": "0", | ||
| "min_free_disk_space": "0", | ||
| } | ||
|
|
||
|
|
||
| def test_rejects_constrained_system(make_config, caplog): | ||
| assert has_sufficient_resources(make_config("chat.example.org", PERMISSIVE)) | ||
| for settings in ( | ||
| {"max_load_1m": "-1.0"}, | ||
| {"min_available_memory": "99999999G"}, | ||
| {"min_free_disk_space": "99999999G"}, | ||
| ): | ||
| config = make_config("chat.example.org", PERMISSIVE | settings) | ||
| caplog.clear() | ||
| assert not has_sufficient_resources(config), settings | ||
| assert "registration rejected" in caplog.text | ||
|
|
||
|
|
||
| def test_unreadable_disk_does_not_reject(make_config, caplog): | ||
| config = make_config( | ||
| "chat.example.org", PERMISSIVE | {"min_free_disk_space": "99999999G"} | ||
| ) | ||
| shutil.rmtree(config.mailboxes_dir) | ||
| assert has_sufficient_resources(config) | ||
| assert "ignoring" in caplog.text | ||
|
|
||
|
|
||
| def test_one_unreadable_value_keeps_other_checks(make_config, monkeypatch, caplog): | ||
| def raise_error(*args): | ||
| raise psutil.Error("dud") | ||
|
|
||
| monkeypatch.setattr(psutil, "getloadavg", raise_error) | ||
| config = make_config( | ||
| "chat.example.org", PERMISSIVE | {"min_free_disk_space": "99999999G"} | ||
| ) | ||
| assert not has_sufficient_resources(config) | ||
| assert "ignoring" in caplog.text |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.