-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added additional logging * disable account creation and file uploads temporarily * system now only allows user file upload if user is whitelisted and user has uploaded less than daily amount of bytes * hard limit on total number of users allowed to be created as another form of security * added USER_LIMIT to testing env file * added invoke task show-users-table * updated show-users-table invoke task * fix: user_limit -> users_limit * removed constant DAILY_UPLOAD_LIMIT_BYTES from operation_validator and instead retrieving value from get_settings() * wip * Polyfactory for Creating Mock Users (#59) * wip: trying to isolate users from one another in test scenarios. todo: generate mock users using polyfactory * upgraded packages * fixed all tests except test_user_creation_limit * added new factories.py file * updated mypy ignore comment * updated project version to 0.6.0 * updated requirements and added pyinvoke task for updating requirements.txt with the deps from uv's lockfile
- Loading branch information
Showing
19 changed files
with
814 additions
and
372 deletions.
There are no files selected for viewing
This file contains 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,4 +1,8 @@ | ||
#!/bin/bash | ||
|
||
source .venv/bin/activate | ||
hypercorn src.smolvault.main:app -b 0.0.0.0 --debug --log-config=logging.conf --log-level=DEBUG --access-logfile=hypercorn.access.log --error-logfile=hypercorn.error.log --keep-alive=120 --workers=2 | ||
hypercorn src.smolvault.main:app -b 0.0.0.0 --debug \ | ||
--log-config=logging.conf --log-level=DEBUG \ | ||
--access-logfile=hypercorn.access.log \ | ||
--error-logfile=hypercorn.error.log \ | ||
--keep-alive=120 --workers=2 |
This file contains 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 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 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 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 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 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,47 @@ | ||
import logging | ||
from datetime import datetime, timedelta | ||
|
||
from smolvault.clients.database import DatabaseClient | ||
from smolvault.config import get_settings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class UploadValidator: | ||
def __init__(self) -> None: | ||
self.settings = get_settings() | ||
self.daily_upload_limit_bytes = self.settings.daily_upload_limit_bytes | ||
self.whitelist = self.settings.user_whitelist.split(",") | ||
|
||
def upload_allowed(self, user_id: int, db_client: DatabaseClient) -> bool: | ||
valid = self._uploads_under_limit_prev_24h(user_id, db_client) and self._user_on_whitelist(user_id) | ||
logger.info("Upload allowed result for user %s: %s", user_id, valid) | ||
return valid | ||
|
||
def _uploads_under_limit_prev_24h(self, user_id: int, db_client: DatabaseClient) -> bool: | ||
logger.info("Checking upload limit for user %s", user_id) | ||
start_time = datetime.now() - timedelta(days=1) | ||
metadata = db_client.get_all_metadata(user_id, start_time=start_time) | ||
bytes_uploaded = sum([record.size for record in metadata]) | ||
logger.info( | ||
"User %s has uploaded %d bytes in the last 24 hours. DAILY_LIMIT: %d", | ||
user_id, | ||
bytes_uploaded, | ||
self.daily_upload_limit_bytes, | ||
) | ||
return bytes_uploaded < self.daily_upload_limit_bytes | ||
|
||
def _user_on_whitelist(self, user_id: int) -> bool: | ||
logger.info("Checking whitelist for user %s", user_id) | ||
return str(user_id) in self.whitelist | ||
|
||
|
||
class UserCreationValidator: | ||
def __init__(self) -> None: | ||
self.settings = get_settings() | ||
self.users_limit = self.settings.users_limit | ||
|
||
def user_creation_allowed(self, db_client: DatabaseClient) -> bool: | ||
users: int = db_client.get_user_count() | ||
logger.info("%d users currently in the system", users) | ||
return users < self.users_limit |
This file contains 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.