forked from chroma-core/chroma
-
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.
feat: Working implementation and updated CIP
- Loading branch information
Showing
14 changed files
with
1,170 additions
and
99 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
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,78 @@ | ||
import logging | ||
from typing import Any, Dict, cast | ||
from overrides import override | ||
import yaml | ||
from chromadb.auth import ( | ||
AuthorizationContext, | ||
ServerAuthorizationConfigurationProvider, | ||
ServerAuthorizationProvider, | ||
) | ||
from chromadb.auth.registry import register_provider, resolve_provider | ||
from chromadb.config import System | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@register_provider("local_authz_config") | ||
class LocalUserConfigAuthorizationConfigurationProvider( | ||
ServerAuthorizationConfigurationProvider[Dict[str, Any]] | ||
): | ||
_config_file: str | ||
_config: Dict[str, Any] | ||
|
||
def __init__(self, system: System) -> None: | ||
super().__init__(system) | ||
self._settings = system.settings | ||
system.settings.require("chroma_server_authz_config_file") | ||
self._config_file = str( | ||
system.settings.chroma_server_authz_config_file) | ||
with open(self._config_file, "r") as f: | ||
self._config = yaml.safe_load(f) | ||
|
||
@override | ||
def get_configuration(self) -> Dict[str, Any]: | ||
return self._config | ||
|
||
|
||
@register_provider("simple_rbac") | ||
class SimpleRBACAuthorizationProvider(ServerAuthorizationProvider): | ||
_authz_config_provider: ServerAuthorizationConfigurationProvider[Dict[str, Any]] | ||
|
||
def __init__(self, system: System) -> None: | ||
super().__init__(system) | ||
self._settings = system.settings | ||
system.settings.require("chroma_server_authz_config_provider") | ||
if self._settings.chroma_server_authz_config_provider: | ||
_cls = resolve_provider( | ||
self._settings.chroma_server_authz_config_provider, | ||
ServerAuthorizationConfigurationProvider | ||
) | ||
self._authz_config_provider = cast( | ||
ServerAuthorizationConfigurationProvider, self.require(_cls)) | ||
_config = self._authz_config_provider.get_configuration() | ||
self._authz_tuples = [] | ||
for u in _config["users"]: | ||
_actions = _config["roles_mapping"][u["role"]]["actions"] | ||
for a in _actions: | ||
self._authz_tuples.append((u["id"], *a.split(":"))) | ||
logger.debug(f"Loaded {len(self._authz_tuples)} permissions for " | ||
f"({len(_config['users'])}) users") | ||
logger.info( | ||
"Authorization Provider SimpleRBACAuthorizationProvider initialized") | ||
|
||
@override | ||
def authorize(self, context: AuthorizationContext) \ | ||
-> bool: | ||
_authz_tuple = (context.user.id, | ||
context.resource.type, | ||
context.action.id) | ||
|
||
print(_authz_tuple) | ||
policy_decision = False | ||
if _authz_tuple in self._authz_tuples: | ||
policy_decision = True | ||
logger.debug(f"Authorization decision: Access " | ||
f"{'granted' if policy_decision else 'denied'} for " | ||
f"user [{context.user.id}] attempting to [{context.action.id}]" | ||
f" on [{context.resource}]") | ||
return policy_decision |
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.