mirror of
https://github.com/clearml/clearml-server
synced 2025-06-26 23:15:47 +00:00
Add fixed user validation
Fix the way a fixed user id is generated
This commit is contained in:
@@ -5,27 +5,45 @@ from typing import Sequence, TypeVar
|
||||
import attr
|
||||
|
||||
from config import config
|
||||
from config.info import get_default_company
|
||||
|
||||
T = TypeVar("T", bound="FixedUser")
|
||||
|
||||
|
||||
class FixedUsersError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@attr.s(auto_attribs=True)
|
||||
class FixedUser:
|
||||
username: str
|
||||
password: str
|
||||
name: str
|
||||
company: str = get_default_company()
|
||||
|
||||
def __attrs_post_init__(self):
|
||||
self.user_id = hashlib.md5(f"{self.username}:{self.password}".encode()).hexdigest()
|
||||
self.user_id = hashlib.md5(f"{self.company}:{self.username}".encode()).hexdigest()
|
||||
|
||||
@classmethod
|
||||
def enabled(cls):
|
||||
return config.get("apiserver.auth.fixed_users.enabled", False)
|
||||
|
||||
@classmethod
|
||||
def validate(cls):
|
||||
if not cls.enabled():
|
||||
return
|
||||
users = cls.from_config()
|
||||
if len({user.username for user in users}) < len(users):
|
||||
raise FixedUsersError(
|
||||
"Duplicate user names found in fixed users configuration"
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@lru_cache()
|
||||
def from_config(cls) -> Sequence[T]:
|
||||
return [cls(**user) for user in config.get("apiserver.auth.fixed_users.users", [])]
|
||||
return [
|
||||
cls(**user) for user in config.get("apiserver.auth.fixed_users.users", [])
|
||||
]
|
||||
|
||||
@classmethod
|
||||
@lru_cache()
|
||||
|
||||
Reference in New Issue
Block a user