mirror of
https://github.com/clearml/clearml-server
synced 2025-02-25 13:43:32 +00:00
Add fixed user validation
Fix the way a fixed user id is generated
This commit is contained in:
parent
ed910d5f6a
commit
5d279c8c5a
@ -3,6 +3,8 @@ from os import getenv
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from version import __version__
|
from version import __version__
|
||||||
|
|
||||||
|
from config import config
|
||||||
|
|
||||||
root = Path(__file__).parent.parent
|
root = Path(__file__).parent.parent
|
||||||
|
|
||||||
|
|
||||||
@ -35,3 +37,7 @@ def get_commit_number():
|
|||||||
@lru_cache()
|
@lru_cache()
|
||||||
def get_deployment_type() -> str:
|
def get_deployment_type() -> str:
|
||||||
return _get("DEPLOY", env_suffix="DEPLOYMENT_TYPE", default="manual")
|
return _get("DEPLOY", env_suffix="DEPLOYMENT_TYPE", default="manual")
|
||||||
|
|
||||||
|
|
||||||
|
def get_default_company():
|
||||||
|
return config.get("apiserver.default_company")
|
||||||
|
@ -11,6 +11,7 @@ from semantic_version import Version
|
|||||||
import database.utils
|
import database.utils
|
||||||
from bll.queue import QueueBLL
|
from bll.queue import QueueBLL
|
||||||
from config import config
|
from config import config
|
||||||
|
from config.info import get_default_company
|
||||||
from database import Database
|
from database import Database
|
||||||
from database.model.auth import Role
|
from database.model.auth import Role
|
||||||
from database.model.auth import User as AuthUser, Credentials
|
from database.model.auth import User as AuthUser, Credentials
|
||||||
@ -49,7 +50,7 @@ def init_es_data():
|
|||||||
|
|
||||||
|
|
||||||
def _ensure_company():
|
def _ensure_company():
|
||||||
company_id = config.get("apiserver.default_company")
|
company_id = get_default_company()
|
||||||
company = Company.objects(id=company_id).only("id").first()
|
company = Company.objects(id=company_id).only("id").first()
|
||||||
if company:
|
if company:
|
||||||
return company_id
|
return company_id
|
||||||
@ -211,6 +212,7 @@ def init_mongo_data():
|
|||||||
|
|
||||||
if FixedUser.enabled():
|
if FixedUser.enabled():
|
||||||
log.info("Fixed users mode is enabled")
|
log.info("Fixed users mode is enabled")
|
||||||
|
FixedUser.validate()
|
||||||
for user in FixedUser.from_config():
|
for user in FixedUser.from_config():
|
||||||
try:
|
try:
|
||||||
_ensure_user(user, company_id)
|
_ensure_user(user, company_id)
|
||||||
|
@ -5,27 +5,45 @@ from typing import Sequence, TypeVar
|
|||||||
import attr
|
import attr
|
||||||
|
|
||||||
from config import config
|
from config import config
|
||||||
|
from config.info import get_default_company
|
||||||
|
|
||||||
T = TypeVar("T", bound="FixedUser")
|
T = TypeVar("T", bound="FixedUser")
|
||||||
|
|
||||||
|
|
||||||
|
class FixedUsersError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
@attr.s(auto_attribs=True)
|
@attr.s(auto_attribs=True)
|
||||||
class FixedUser:
|
class FixedUser:
|
||||||
username: str
|
username: str
|
||||||
password: str
|
password: str
|
||||||
name: str
|
name: str
|
||||||
|
company: str = get_default_company()
|
||||||
|
|
||||||
def __attrs_post_init__(self):
|
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
|
@classmethod
|
||||||
def enabled(cls):
|
def enabled(cls):
|
||||||
return config.get("apiserver.auth.fixed_users.enabled", False)
|
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
|
@classmethod
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
def from_config(cls) -> Sequence[T]:
|
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
|
@classmethod
|
||||||
@lru_cache()
|
@lru_cache()
|
||||||
|
Loading…
Reference in New Issue
Block a user