mirror of
https://github.com/clearml/clearml-server
synced 2025-02-01 03:16:44 +00:00
5d279c8c5a
Fix the way a fixed user id is generated
57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
import hashlib
|
|
from functools import lru_cache
|
|
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.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", [])
|
|
]
|
|
|
|
@classmethod
|
|
@lru_cache()
|
|
def get_by_username(cls, username) -> T:
|
|
return next(
|
|
(user for user in cls.from_config() if user.username == username), None
|
|
)
|
|
|
|
def __hash__(self):
|
|
return hash(self.user_id)
|