mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-15 00:55:55 +08:00
Merge branch 'feat/plugin-install-scope-management' into deploy/dev
This commit is contained in:
commit
7636bc164d
@ -164,9 +164,23 @@ class PluginInstallTaskStartResponse(BaseModel):
|
|||||||
task_id: str = Field(description="The ID of the install task.")
|
task_id: str = Field(description="The ID of the install task.")
|
||||||
|
|
||||||
|
|
||||||
|
class PluginVerification(BaseModel):
|
||||||
|
"""
|
||||||
|
Verification of the plugin.
|
||||||
|
"""
|
||||||
|
|
||||||
|
class AuthorizedCategory(StrEnum):
|
||||||
|
Langgenius = "langgenius"
|
||||||
|
Partner = "partner"
|
||||||
|
Community = "community"
|
||||||
|
|
||||||
|
authorized_category: AuthorizedCategory = Field(description="The authorized category of the plugin.")
|
||||||
|
|
||||||
|
|
||||||
class PluginUploadResponse(BaseModel):
|
class PluginUploadResponse(BaseModel):
|
||||||
unique_identifier: str = Field(description="The unique identifier of the plugin.")
|
unique_identifier: str = Field(description="The unique identifier of the plugin.")
|
||||||
manifest: PluginDeclaration
|
manifest: PluginDeclaration
|
||||||
|
verification: Optional[PluginVerification] = Field(default=None, description="Basic verification information")
|
||||||
|
|
||||||
|
|
||||||
class PluginOAuthAuthorizationUrlResponse(BaseModel):
|
class PluginOAuthAuthorizationUrlResponse(BaseModel):
|
||||||
|
5
api/services/errors/plugin.py
Normal file
5
api/services/errors/plugin.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from services.errors.base import BaseServiceError
|
||||||
|
|
||||||
|
|
||||||
|
class PluginInstallationForbiddenError(BaseServiceError):
|
||||||
|
pass
|
@ -88,6 +88,26 @@ class WebAppAuthModel(BaseModel):
|
|||||||
allow_email_password_login: bool = False
|
allow_email_password_login: bool = False
|
||||||
|
|
||||||
|
|
||||||
|
class PluginInstallationScope(StrEnum):
|
||||||
|
NONE = "none"
|
||||||
|
OFFICIAL_ONLY = "official_only"
|
||||||
|
OFFICIAL_AND_SPECIFIC_PARTNERS = "official_and_specific_partners"
|
||||||
|
ALL = "all"
|
||||||
|
|
||||||
|
|
||||||
|
class PluginInstallationPermissionModel(BaseModel):
|
||||||
|
# Plugin installation scope – possible values:
|
||||||
|
# none: prohibit all plugin installations
|
||||||
|
# official_only: allow only Dify official plugins
|
||||||
|
# official_and_specific_partners: allow official and specific partner plugins
|
||||||
|
# all: allow installation of all plugins
|
||||||
|
plugin_installation_scope: PluginInstallationScope = PluginInstallationScope.ALL
|
||||||
|
|
||||||
|
# If True, restrict plugin installation to the marketplace only
|
||||||
|
# Equivalent to ForceEnablePluginVerification
|
||||||
|
restrict_to_marketplace_only: bool = False
|
||||||
|
|
||||||
|
|
||||||
class FeatureModel(BaseModel):
|
class FeatureModel(BaseModel):
|
||||||
billing: BillingModel = BillingModel()
|
billing: BillingModel = BillingModel()
|
||||||
education: EducationModel = EducationModel()
|
education: EducationModel = EducationModel()
|
||||||
@ -128,6 +148,7 @@ class SystemFeatureModel(BaseModel):
|
|||||||
license: LicenseModel = LicenseModel()
|
license: LicenseModel = LicenseModel()
|
||||||
branding: BrandingModel = BrandingModel()
|
branding: BrandingModel = BrandingModel()
|
||||||
webapp_auth: WebAppAuthModel = WebAppAuthModel()
|
webapp_auth: WebAppAuthModel = WebAppAuthModel()
|
||||||
|
plugin_installation_permission: PluginInstallationPermissionModel = PluginInstallationPermissionModel()
|
||||||
|
|
||||||
|
|
||||||
class FeatureService:
|
class FeatureService:
|
||||||
@ -291,3 +312,12 @@ class FeatureService:
|
|||||||
features.license.workspaces.enabled = license_info["workspaces"]["enabled"]
|
features.license.workspaces.enabled = license_info["workspaces"]["enabled"]
|
||||||
features.license.workspaces.limit = license_info["workspaces"]["limit"]
|
features.license.workspaces.limit = license_info["workspaces"]["limit"]
|
||||||
features.license.workspaces.size = license_info["workspaces"]["used"]
|
features.license.workspaces.size = license_info["workspaces"]["used"]
|
||||||
|
|
||||||
|
if "PluginInstallationPermission" in enterprise_info:
|
||||||
|
plugin_installation_info = enterprise_info["PluginInstallationPermission"]
|
||||||
|
features.plugin_installation_permission.plugin_installation_scope = plugin_installation_info[
|
||||||
|
"pluginInstallationScope"
|
||||||
|
]
|
||||||
|
features.plugin_installation_permission.restrict_to_marketplace_only = plugin_installation_info[
|
||||||
|
"restrictToMarketplaceOnly"
|
||||||
|
]
|
||||||
|
@ -17,11 +17,13 @@ from core.plugin.entities.plugin import (
|
|||||||
PluginInstallation,
|
PluginInstallation,
|
||||||
PluginInstallationSource,
|
PluginInstallationSource,
|
||||||
)
|
)
|
||||||
from core.plugin.entities.plugin_daemon import PluginInstallTask, PluginUploadResponse
|
from core.plugin.entities.plugin_daemon import PluginInstallTask, PluginUploadResponse, PluginVerification
|
||||||
from core.plugin.impl.asset import PluginAssetManager
|
from core.plugin.impl.asset import PluginAssetManager
|
||||||
from core.plugin.impl.debugging import PluginDebuggingClient
|
from core.plugin.impl.debugging import PluginDebuggingClient
|
||||||
from core.plugin.impl.plugin import PluginInstaller
|
from core.plugin.impl.plugin import PluginInstaller
|
||||||
from extensions.ext_redis import redis_client
|
from extensions.ext_redis import redis_client
|
||||||
|
from services.errors.plugin import PluginInstallationForbiddenError
|
||||||
|
from services.feature_service import FeatureService, PluginInstallationScope
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -86,6 +88,35 @@ class PluginService:
|
|||||||
logger.exception("failed to fetch latest plugin version")
|
logger.exception("failed to fetch latest plugin version")
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _check_plugin_installation_availability(plugin_verification: Optional[PluginVerification]):
|
||||||
|
"""
|
||||||
|
Check the verification of the plugin
|
||||||
|
"""
|
||||||
|
features = FeatureService.get_system_features()
|
||||||
|
|
||||||
|
if not plugin_verification:
|
||||||
|
if features.plugin_installation_permission.restrict_to_marketplace_only:
|
||||||
|
raise PluginInstallationForbiddenError("Plugin installation is restricted to marketplace only")
|
||||||
|
return
|
||||||
|
|
||||||
|
match features.plugin_installation_permission.plugin_installation_scope:
|
||||||
|
case PluginInstallationScope.OFFICIAL_ONLY:
|
||||||
|
if plugin_verification.authorized_category != PluginVerification.AuthorizedCategory.Langgenius:
|
||||||
|
raise PluginInstallationForbiddenError("Plugin installation is restricted to official only")
|
||||||
|
case PluginInstallationScope.OFFICIAL_AND_SPECIFIC_PARTNERS:
|
||||||
|
if plugin_verification.authorized_category not in [
|
||||||
|
PluginVerification.AuthorizedCategory.Langgenius,
|
||||||
|
PluginVerification.AuthorizedCategory.Partner,
|
||||||
|
]:
|
||||||
|
raise PluginInstallationForbiddenError(
|
||||||
|
"Plugin installation is restricted to official and specific partners"
|
||||||
|
)
|
||||||
|
case PluginInstallationScope.NONE:
|
||||||
|
raise PluginInstallationForbiddenError("Installing plugins is not allowed")
|
||||||
|
case PluginInstallationScope.ALL:
|
||||||
|
pass
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_debugging_key(tenant_id: str) -> str:
|
def get_debugging_key(tenant_id: str) -> str:
|
||||||
"""
|
"""
|
||||||
@ -199,6 +230,8 @@ class PluginService:
|
|||||||
# check if plugin pkg is already downloaded
|
# check if plugin pkg is already downloaded
|
||||||
manager = PluginInstaller()
|
manager = PluginInstaller()
|
||||||
|
|
||||||
|
features = FeatureService.get_system_features()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
manager.fetch_plugin_manifest(tenant_id, new_plugin_unique_identifier)
|
manager.fetch_plugin_manifest(tenant_id, new_plugin_unique_identifier)
|
||||||
# already downloaded, skip, and record install event
|
# already downloaded, skip, and record install event
|
||||||
@ -206,7 +239,14 @@ class PluginService:
|
|||||||
except Exception:
|
except Exception:
|
||||||
# plugin not installed, download and upload pkg
|
# plugin not installed, download and upload pkg
|
||||||
pkg = download_plugin_pkg(new_plugin_unique_identifier)
|
pkg = download_plugin_pkg(new_plugin_unique_identifier)
|
||||||
manager.upload_pkg(tenant_id, pkg, verify_signature=False)
|
response = manager.upload_pkg(
|
||||||
|
tenant_id,
|
||||||
|
pkg,
|
||||||
|
verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
|
||||||
|
)
|
||||||
|
|
||||||
|
# check if the plugin is available to install
|
||||||
|
PluginService._check_plugin_installation_availability(response.verification)
|
||||||
|
|
||||||
return manager.upgrade_plugin(
|
return manager.upgrade_plugin(
|
||||||
tenant_id,
|
tenant_id,
|
||||||
@ -251,7 +291,15 @@ class PluginService:
|
|||||||
returns: plugin_unique_identifier
|
returns: plugin_unique_identifier
|
||||||
"""
|
"""
|
||||||
manager = PluginInstaller()
|
manager = PluginInstaller()
|
||||||
return manager.upload_pkg(tenant_id, pkg, verify_signature)
|
features = FeatureService.get_system_features()
|
||||||
|
response = manager.upload_pkg(
|
||||||
|
tenant_id,
|
||||||
|
pkg,
|
||||||
|
verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
|
||||||
|
)
|
||||||
|
# check if the plugin is available to install
|
||||||
|
PluginService._check_plugin_installation_availability(response.verification)
|
||||||
|
return response
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def upload_pkg_from_github(
|
def upload_pkg_from_github(
|
||||||
@ -264,13 +312,17 @@ class PluginService:
|
|||||||
pkg = download_with_size_limit(
|
pkg = download_with_size_limit(
|
||||||
f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
|
f"https://github.com/{repo}/releases/download/{version}/{package}", dify_config.PLUGIN_MAX_PACKAGE_SIZE
|
||||||
)
|
)
|
||||||
|
features = FeatureService.get_system_features()
|
||||||
|
|
||||||
manager = PluginInstaller()
|
manager = PluginInstaller()
|
||||||
return manager.upload_pkg(
|
response = manager.upload_pkg(
|
||||||
tenant_id,
|
tenant_id,
|
||||||
pkg,
|
pkg,
|
||||||
verify_signature,
|
verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
|
||||||
)
|
)
|
||||||
|
# check if the plugin is available to install
|
||||||
|
PluginService._check_plugin_installation_availability(response.verification)
|
||||||
|
return response
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def upload_bundle(
|
def upload_bundle(
|
||||||
@ -313,28 +365,33 @@ class PluginService:
|
|||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def fetch_marketplace_pkg(
|
def fetch_marketplace_pkg(tenant_id: str, plugin_unique_identifier: str) -> PluginDeclaration:
|
||||||
tenant_id: str, plugin_unique_identifier: str, verify_signature: bool = False
|
|
||||||
) -> PluginDeclaration:
|
|
||||||
"""
|
"""
|
||||||
Fetch marketplace package
|
Fetch marketplace package
|
||||||
"""
|
"""
|
||||||
if not dify_config.MARKETPLACE_ENABLED:
|
if not dify_config.MARKETPLACE_ENABLED:
|
||||||
raise ValueError("marketplace is not enabled")
|
raise ValueError("marketplace is not enabled")
|
||||||
|
|
||||||
|
features = FeatureService.get_system_features()
|
||||||
|
|
||||||
manager = PluginInstaller()
|
manager = PluginInstaller()
|
||||||
try:
|
try:
|
||||||
declaration = manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
|
declaration = manager.fetch_plugin_manifest(tenant_id, plugin_unique_identifier)
|
||||||
except Exception:
|
except Exception:
|
||||||
pkg = download_plugin_pkg(plugin_unique_identifier)
|
pkg = download_plugin_pkg(plugin_unique_identifier)
|
||||||
declaration = manager.upload_pkg(tenant_id, pkg, verify_signature).manifest
|
response = manager.upload_pkg(
|
||||||
|
tenant_id,
|
||||||
|
pkg,
|
||||||
|
verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
|
||||||
|
)
|
||||||
|
# check if the plugin is available to install
|
||||||
|
PluginService._check_plugin_installation_availability(response.verification)
|
||||||
|
declaration = response.manifest
|
||||||
|
|
||||||
return declaration
|
return declaration
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def install_from_marketplace_pkg(
|
def install_from_marketplace_pkg(tenant_id: str, plugin_unique_identifiers: Sequence[str]):
|
||||||
tenant_id: str, plugin_unique_identifiers: Sequence[str], verify_signature: bool = False
|
|
||||||
):
|
|
||||||
"""
|
"""
|
||||||
Install plugin from marketplace package files,
|
Install plugin from marketplace package files,
|
||||||
returns installation task id
|
returns installation task id
|
||||||
@ -344,6 +401,8 @@ class PluginService:
|
|||||||
|
|
||||||
manager = PluginInstaller()
|
manager = PluginInstaller()
|
||||||
|
|
||||||
|
features = FeatureService.get_system_features()
|
||||||
|
|
||||||
# check if already downloaded
|
# check if already downloaded
|
||||||
for plugin_unique_identifier in plugin_unique_identifiers:
|
for plugin_unique_identifier in plugin_unique_identifiers:
|
||||||
try:
|
try:
|
||||||
@ -352,7 +411,13 @@ class PluginService:
|
|||||||
except Exception:
|
except Exception:
|
||||||
# plugin not installed, download and upload pkg
|
# plugin not installed, download and upload pkg
|
||||||
pkg = download_plugin_pkg(plugin_unique_identifier)
|
pkg = download_plugin_pkg(plugin_unique_identifier)
|
||||||
manager.upload_pkg(tenant_id, pkg, verify_signature)
|
response = manager.upload_pkg(
|
||||||
|
tenant_id,
|
||||||
|
pkg,
|
||||||
|
verify_signature=features.plugin_installation_permission.restrict_to_marketplace_only,
|
||||||
|
)
|
||||||
|
# check if the plugin is available to install
|
||||||
|
PluginService._check_plugin_installation_availability(response.verification)
|
||||||
|
|
||||||
return manager.install_from_identifiers(
|
return manager.install_from_identifiers(
|
||||||
tenant_id,
|
tenant_id,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user