-
Notifications
You must be signed in to change notification settings - Fork 92
feat(backend): add external KB permission resolver extension point #1025
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
Open
cocowh
wants to merge
12
commits into
wecode-ai:main
Choose a base branch
from
cocowh:weagent/kb-permission-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2db0887
feat(backend): add external KB permission resolver extension point
cocowh aa81e98
fix(backend): add exc_info to extension loading error log
cocowh 62c3b9b
Merge branch 'upstream/main' into weagent/kb-permission-extension
cocowh 8e39967
feat(frontend): add KB permission extension point for external permis…
cocowh e70657f
feat(frontend): add KB permission extension point for external permis…
cocowh edb81eb
feat(backend): use Python entry points for KB permission resolver
cocowh 4fb0790
feat(backend): use Python entry points for KB permission resolver (#29)
cocowh 64d2d17
fix(i18n): add missing document.permission.personal translation
cocowh e6ef759
fix(i18n): add missing document.permission.personal translation
cocowh a6dc78d
Merge pull request #30 from cocowh/weagent/kb-entry-points
cocowh bea37e6
style(backend): format code with black and isort
cocowh b0ba65c
docs: update kb-permission-extension.md for entry points mechanism
cocowh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,216 @@ | ||
| # SPDX-FileCopyrightText: 2025 Wegent, Inc. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """ | ||
| Knowledge base external permission resolver extension point. | ||
|
|
||
| Loaded via Python entry points mechanism. | ||
|
|
||
| Usage (in extension package, e.g. myext/kb_permissions.py): | ||
|
|
||
| from app.services.readers.kb_permissions import IKbPermissionResolver | ||
|
|
||
| class MyResolver(IKbPermissionResolver): | ||
| def __init__(self, base: IKbPermissionResolver): | ||
| self._base = base | ||
|
|
||
| def resolve(self, db, kb_id, user_id, kb): | ||
| # return a role string or None | ||
| ... | ||
|
|
||
| def get_accessible_kb_ids(self, db, user_id): | ||
| # return list of kb_ids the user can access | ||
| ... | ||
|
|
||
| Register in pyproject.toml: | ||
|
|
||
| [project.entry-points."wegent.kb_permissions"] | ||
| my_resolver = "myext.kb_permissions:MyResolver" | ||
| """ | ||
|
|
||
| import importlib.metadata | ||
| import logging | ||
| import threading | ||
| from abc import ABC, abstractmethod | ||
| from typing import Optional | ||
|
|
||
| from sqlalchemy.orm import Session | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Entry point group for KB permission resolvers | ||
| ENTRY_POINT_GROUP = "wegent.kb_permissions" | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Interface | ||
| # ============================================================================= | ||
|
|
||
|
|
||
| class IKbPermissionResolver(ABC): | ||
| """ | ||
| Abstract interface for external knowledge base permission resolution. | ||
|
|
||
| Implementations return a role string when the external system grants | ||
| access, or None to fall through to the built-in permission logic. | ||
| """ | ||
|
|
||
| @abstractmethod | ||
| def resolve( | ||
| self, | ||
| db: Session, | ||
| kb_id: int, | ||
| user_id: int, | ||
| kb: object, | ||
| ) -> Optional[str]: | ||
| """ | ||
| Resolve permission for a single knowledge base access check. | ||
|
|
||
| Called after all built-in checks have returned False. | ||
|
|
||
| Args: | ||
| db: Database session | ||
| kb_id: Knowledge base ID | ||
| user_id: Requesting user ID | ||
| kb: Kind object (knowledge base record) | ||
|
|
||
| Returns: | ||
| Role string ("Owner"/"Maintainer"/"Developer"/"Reporter") if the | ||
| external system grants access, None to continue with built-in | ||
| denial. | ||
| """ | ||
| pass | ||
|
|
||
| @abstractmethod | ||
| def get_accessible_kb_ids(self, db: Session, user_id: int) -> list[int]: | ||
| """ | ||
| Return knowledge base IDs accessible to the user via external rules. | ||
|
|
||
| Called during list queries to extend the OR conditions. Return an | ||
| empty list when there are no additional IDs to include. | ||
|
|
||
| Args: | ||
| db: Database session | ||
| user_id: Requesting user ID | ||
|
|
||
| Returns: | ||
| List of knowledge base IDs (may be empty). | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Default Implementation | ||
| # ============================================================================= | ||
|
|
||
|
|
||
| class DefaultKbPermissionResolver(IKbPermissionResolver): | ||
| """ | ||
| No-op resolver used when no extension is configured. | ||
|
|
||
| Always returns None / [] so no extra permissions are granted. | ||
| """ | ||
|
|
||
| def resolve( | ||
| self, | ||
| db: Session, | ||
| kb_id: int, | ||
| user_id: int, | ||
| kb: object, | ||
| ) -> Optional[str]: | ||
| return None | ||
|
|
||
| def get_accessible_kb_ids(self, db: Session, user_id: int) -> list[int]: | ||
| return [] | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Loader | ||
| # ============================================================================= | ||
|
|
||
|
|
||
| def _load_from_entry_points( | ||
| base: IKbPermissionResolver, | ||
| ) -> Optional[IKbPermissionResolver]: | ||
| """ | ||
| Load resolver from entry points. | ||
|
|
||
| Args: | ||
| base: The base resolver to pass to the loaded resolver's constructor. | ||
|
|
||
| Returns: | ||
| The loaded resolver instance, or None if no valid entry point found. | ||
| """ | ||
| try: | ||
| entry_points = importlib.metadata.entry_points(group=ENTRY_POINT_GROUP) | ||
| except TypeError: | ||
| # Python < 3.10 compatibility | ||
| all_eps = importlib.metadata.entry_points() | ||
| entry_points = all_eps.get(ENTRY_POINT_GROUP, []) | ||
|
|
||
| if not entry_points: | ||
| return None | ||
|
|
||
| # Use the first entry point | ||
| ep = next(iter(entry_points)) | ||
|
|
||
| try: | ||
| resolver_class = ep.load() | ||
|
|
||
| if not issubclass(resolver_class, IKbPermissionResolver): | ||
| logger.error( | ||
| f"Entry point {ep.name} ({resolver_class}) does not implement IKbPermissionResolver" | ||
| ) | ||
| return None | ||
|
|
||
| result = resolver_class(base) | ||
| logger.info(f"KB permission resolver loaded from entry point: {ep.name}") | ||
| return result | ||
|
|
||
| except Exception as e: | ||
| logger.warning(f"Failed to load entry point {ep.name}: {e}", exc_info=True) | ||
| return None | ||
|
|
||
|
|
||
| def _create_resolver() -> IKbPermissionResolver: | ||
| """Create resolver, loading from entry points if available.""" | ||
| base: IKbPermissionResolver = DefaultKbPermissionResolver() | ||
|
|
||
| # Try to load from entry points | ||
| loaded = _load_from_entry_points(base) | ||
| if loaded is not None: | ||
| return loaded | ||
|
|
||
| return base | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Lazy Singleton | ||
| # ============================================================================= | ||
|
|
||
|
|
||
| class _LazyReader: | ||
| """Lazy-loaded resolver proxy that delegates to the actual resolver instance.""" | ||
|
|
||
| _instance: IKbPermissionResolver | None = None | ||
| _init_lock: threading.Lock = threading.Lock() | ||
|
|
||
| def _get(self) -> IKbPermissionResolver: | ||
| if self._instance is None: | ||
| with self._init_lock: | ||
| # Double-checked locking to ensure only one resolver is created | ||
| # under concurrent access. | ||
| if self._instance is None: | ||
| self._instance = _create_resolver() | ||
| return self._instance | ||
|
|
||
| def __getattr__(self, name: str): | ||
| return getattr(self._get(), name) | ||
|
|
||
|
|
||
| # ============================================================================= | ||
| # Export | ||
| # ============================================================================= | ||
|
|
||
| kb_permission_resolver: IKbPermissionResolver = _LazyReader() # type: ignore |
This file contains hidden or 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
Empty file.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.