fix(trigger): enhance credential encryption handling in TriggerProviderService

- Introduced conditional initialization of credential_encrypter based on credential_type to prevent errors when unauthorized.
- Updated the encryption logic to handle cases where credential_encrypter may be None, ensuring robustness in credential processing.
This commit is contained in:
Harry 2025-10-16 15:06:55 +08:00
parent 56abca1f41
commit beaeb30dcc

View File

@ -10,7 +10,7 @@ from sqlalchemy.orm import Session
from configs import dify_config from configs import dify_config
from constants import HIDDEN_VALUE, UNKNOWN_VALUE from constants import HIDDEN_VALUE, UNKNOWN_VALUE
from core.helper.provider_cache import NoOpProviderCredentialCache from core.helper.provider_cache import NoOpProviderCredentialCache
from core.helper.provider_encryption import create_provider_encrypter from core.helper.provider_encryption import ProviderConfigEncrypter, create_provider_encrypter
from core.plugin.entities.plugin_daemon import CredentialType from core.plugin.entities.plugin_daemon import CredentialType
from core.plugin.impl.oauth import OAuthHandler from core.plugin.impl.oauth import OAuthHandler
from core.tools.utils.system_oauth_encryption import decrypt_system_oauth_params from core.tools.utils.system_oauth_encryption import decrypt_system_oauth_params
@ -154,11 +154,13 @@ class TriggerProviderService:
if existing: if existing:
raise ValueError(f"Credential name '{name}' already exists for this provider") raise ValueError(f"Credential name '{name}' already exists for this provider")
credential_encrypter, _ = create_provider_encrypter( credential_encrypter: ProviderConfigEncrypter | None = None
tenant_id=tenant_id, if credential_type != CredentialType.UNAUTHORIZED:
config=provider_controller.get_credential_schema_config(credential_type), credential_encrypter, _ = create_provider_encrypter(
cache=NoOpProviderCredentialCache(), tenant_id=tenant_id,
) config=provider_controller.get_credential_schema_config(credential_type),
cache=NoOpProviderCredentialCache(),
)
properties_encrypter, _ = create_provider_encrypter( properties_encrypter, _ = create_provider_encrypter(
tenant_id=tenant_id, tenant_id=tenant_id,
@ -176,7 +178,7 @@ class TriggerProviderService:
provider_id=str(provider_id), provider_id=str(provider_id),
parameters=parameters, parameters=parameters,
properties=properties_encrypter.encrypt(dict(properties)), properties=properties_encrypter.encrypt(dict(properties)),
credentials=credential_encrypter.encrypt(dict(credentials)), credentials=credential_encrypter.encrypt(dict(credentials)) if credential_encrypter else {},
credential_type=credential_type.value, credential_type=credential_type.value,
credential_expires_at=credential_expires_at, credential_expires_at=credential_expires_at,
expires_at=expires_at, expires_at=expires_at,