-
Notifications
You must be signed in to change notification settings - Fork 77
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
257 user model refactor the config handling #433
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -8,7 +8,7 @@ | |||||||||||
import random | ||||||||||||
import string | ||||||||||||
from redis import StrictRedis | ||||||||||||
from enum import Enum | ||||||||||||
from enum import Enum, auto | ||||||||||||
from rq import Queue # type: ignore | ||||||||||||
from sqlmodel import ( | ||||||||||||
Session, | ||||||||||||
|
@@ -40,10 +40,67 @@ class TaskType(Enum): | |||||||||||
COMMAND = "command" | ||||||||||||
|
||||||||||||
|
||||||||||||
# See docs/users.md for documentation on the permission model. | ||||||||||||
# Enum values are meaningless and may change. Make sure to not store them | ||||||||||||
# anywhere (for storing/transfer use role names instead). | ||||||||||||
class UserRole(Enum): | ||||||||||||
# "role groups", used to grant a collection of "action roles" | ||||||||||||
nobody = auto() # no permissions granted | ||||||||||||
user = auto() # can run yara queries and read the state | ||||||||||||
admin = auto() # can manage the system (and do everything else) | ||||||||||||
|
||||||||||||
# "action roles", used to give permission to a specific thing | ||||||||||||
can_manage_all_queries = auto() | ||||||||||||
can_manage_queries = auto() | ||||||||||||
can_list_all_queries = auto() | ||||||||||||
can_list_queries = auto() | ||||||||||||
can_view_queries = auto() | ||||||||||||
can_download_files = auto() | ||||||||||||
|
||||||||||||
|
||||||||||||
# Type alias for Job ids | ||||||||||||
JobId = str | ||||||||||||
|
||||||||||||
|
||||||||||||
class UserModelConfig: | ||||||||||||
def __init__(self, db_instance): | ||||||||||||
self.db = db_instance | ||||||||||||
|
||||||||||||
@property | ||||||||||||
def auth_default_roles(self) -> List[UserRole]: | ||||||||||||
auth_default_roles = self.db.get_mquery_config_key( | ||||||||||||
"auth_default_roles" | ||||||||||||
) | ||||||||||||
if auth_default_roles is None: | ||||||||||||
auth_default_roles = "admin" | ||||||||||||
return [ | ||||||||||||
UserRole[role.strip()] for role in auth_default_roles.split(",") | ||||||||||||
] | ||||||||||||
|
||||||||||||
@property | ||||||||||||
def openid_client_id(self) -> str: | ||||||||||||
return self.db.get_mquery_config_key("openid_client_id") | ||||||||||||
|
||||||||||||
@property | ||||||||||||
def query_allow_slow(self) -> bool: | ||||||||||||
return self.db.get_mquery_config_key("query_allow_slow") == "true" | ||||||||||||
|
||||||||||||
@property | ||||||||||||
def auth_enabled(self) -> bool: | ||||||||||||
auth_enabled = self.db.get_mquery_config_key("auth_enabled") | ||||||||||||
if not auth_enabled or auth_enabled == "false": | ||||||||||||
return False | ||||||||||||
return True | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Tiny chance in behaviour, but consistent with query_allow_slow (and works the same way for "true" and "false") There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. brilliant :) |
||||||||||||
|
||||||||||||
@property | ||||||||||||
def openid_url(self) -> str: | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||||||||||||
return self.db.get_mquery_config_key("openid_url") | ||||||||||||
|
||||||||||||
@property | ||||||||||||
def openid_secret(self) -> str | None: | ||||||||||||
return self.db.get_mquery_config_key("openid_secret") | ||||||||||||
|
||||||||||||
|
||||||||||||
class Database: | ||||||||||||
def __init__(self, redis_host: str, redis_port: int) -> None: | ||||||||||||
self.redis: Any = StrictRedis( | ||||||||||||
|
@@ -57,6 +114,10 @@ def __schedule(self, agent: str, task: Any, *args: Any) -> None: | |||||||||||
task, *args, job_timeout=app_config.rq.job_timeout | ||||||||||||
) | ||||||||||||
|
||||||||||||
@property | ||||||||||||
def config(self): | ||||||||||||
return UserModelConfig(self) | ||||||||||||
|
||||||||||||
@contextmanager | ||||||||||||
def session(self): | ||||||||||||
with Session(self.engine) as session: | ||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -101,7 +101,7 @@ class BackendStatusDatasetsSchema(BaseModel): | |
|
||
class ServerSchema(BaseModel): | ||
version: str | ||
auth_enabled: Optional[str] | ||
auth_enabled: Optional[bool] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this will cause issues on the frontend (but you can check). Unfortunately, you should probably cast to str when creating ServerSchema. |
||
openid_url: Optional[str] | ||
openid_client_id: Optional[str] | ||
about: str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(if this causes type errors somewhere else, callers should be fixed to handle the situation where this is null)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done