mirror of
https://github.com/lfnovo/open-notebook.git
synced 2026-04-28 19:40:50 +00:00
* feat: replace provider config with credential-based system (#477) Introduce a new credential management system replacing the old ProviderConfig singleton and standalone Models page. Each credential stores encrypted API keys and provider-specific configuration with full CRUD support via a unified settings UI. Backend: - Add Credential domain model with encrypted API key storage - Add credentials API router (CRUD, discovery, registration, testing) - Add encryption utilities for secure key storage - Add key_provider for DB-first env-var fallback provisioning - Add connection tester and model discovery services - Integrate ModelManager with credential-based config - Add provider name normalization for Esperanto compatibility - Add database migrations 11-12 for credential schema Frontend: - Rewrite settings/api-keys page with credential management UI - Add model discovery dialog with search and custom model support - Add compact default model assignments (primary/advanced layout) - Add inline model testing and credential connection testing - Add env-var migration banner - Update navigation to unified settings page - Remove standalone models page and old settings components i18n: - Update all 7 locale files with credential and model management keys Closes #477 Co-Authored-By: JFMD <git@jfmd.us> Co-Authored-By: OraCatQAQ <570768706@qq.com> * fix: address PR #540 review comments - Fix docs referencing removed Models page - Fix error-handler returning raw messages instead of i18n keys - Fix auth.py misleading docstring and missing no-password guard - Fix connection_tester using wrong env var for openai_compatible - Add provision_provider_keys before model discovery/sync - Update CLAUDE.md to reflect credential-based system - Fix missing closing brace in api-keys page useEffect * fix: add logging to credential migration and surface errors in UI - Add comprehensive logging to migrate-from-env and migrate-from-provider-config endpoints (start, per-provider progress, success/failure with stack traces, final summary) - Fix frontend migration hooks ignoring errors array from response - Show error toast when migration fails instead of "nothing to migrate" - Invalidate status/envStatus queries after migration so banner updates * docs: update CLAUDE.md files for credential system Replace stale ProviderConfig and /api-keys/ references across 8 CLAUDE.md files to reflect the new Credential-based system from PR #540. * docs: update user documentation for credential-based system Replace env var API key instructions with Settings UI credential workflow across all user-facing documentation. The new flow is: set OPEN_NOTEBOOK_ENCRYPTION_KEY → start services → add credential in Settings UI → test → discover models → register. - Rewrite ai-providers.md, api-configuration.md, environment-reference.md - Update all quick-start guides and installation docs - Update ollama.md, openai-compatible.md, local-tts/stt networking sections - Update reverse-proxy.md, development-setup.md, security.md - Fix broken links to non-existent docs/deployment/ paths - Add credentials endpoints to api-reference.md - Move all API key env vars to deprecated/legacy sections * chore: bump version to 1.7.0-rc1 Release candidate for credential-based provider management system. * fix: initialize provider before try block in test_credential Prevents UnboundLocalError when Credential.get() throws (e.g., invalid credential_id) before provider is assigned. * fix: reorder down migration to drop index before table Removes duplicate REMOVE FIELD statement and reorders so the index is dropped before the table, preventing rollback failures. * refactor: simplify encryption key to always derive via SHA-256 Remove the dual code path in _ensure_fernet_key() that detected native Fernet keys. Since the credential system is new, always deriving via SHA-256 removes unnecessary complexity. Also removes the generate_key() function and Fernet.generate_key() references from docs. * fix: correct mock patch targets in embedding tests and URL validation Fix embedding tests patching wrong module path for model_manager (was targeting open_notebook.utils.embedding.model_manager but it's imported locally from open_notebook.ai.models). Also fix URL validation to allow unresolvable hostnames since they may be valid in the deployment environment (e.g., Azure endpoints, internal DNS). * feat: add global setup banner for encryption and migration status Show a persistent banner in AppShell when encryption key is missing (red) or env var API keys can be migrated (amber), so users see these prompts on every page instead of only on Settings > API Keys. Includes a docs link for the encryption banner and i18n support across all 7 locales. * docs: several improvements to docker-compose e env examples * Update README.md Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> * docs: fix env var format in README and update model setup instructions Align the encryption key snippet in README Step 2 with the list format used in the compose file. Replace deprecated "Settings → Models" instructions with credential-based Discover Models flow. * fix: address credential system review issues - Fix SSRF bypass via IPv4-mapped IPv6 addresses (::ffff:169.254.x.x) - Fix TTS connection test missing config parameter - Add Azure-specific model discovery using api-key auth header - Add Vertex static model list for credential-based discovery - Fix PROVIDER_DISCOVERY_FUNCTIONS incorrect azure/vertex mapping - Extract business logic to api/credentials_service.py (service layer) - Move credential Pydantic schemas to api/models.py - Update tests to use new service imports and ValueError assertions * fix: sanitize error responses and migrate key_provider to Credential - Replace raw exception messages in all credential router 500 responses with generic error strings (internal details logged server-side only) - Refactor key_provider.py to use Credential.get_by_provider() instead of deprecated ProviderConfig.get_instance() - Remove unused functions (get_provider_configs, get_default_api_key, get_provider_config) that were dead code --------- Co-authored-by: JFMD <git@jfmd.us> Co-authored-by: OraCatQAQ <570768706@qq.com>
444 lines
16 KiB
Python
444 lines
16 KiB
Python
"""
|
|
Provider Configuration domain model for storing multiple credentials per provider.
|
|
|
|
This module provides the ProviderConfig singleton model that stores multiple
|
|
API key configurations per provider. Each ProviderCredential contains a complete
|
|
set of configuration options for a provider (api_key, base_url, model, etc.).
|
|
|
|
Encryption is enabled when OPEN_NOTEBOOK_ENCRYPTION_KEY environment variable
|
|
is set. If not set, keys are stored as plain text with a warning logged.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
from typing import ClassVar, Dict, List, Optional
|
|
|
|
from pydantic import Field, SecretStr, field_validator
|
|
|
|
from open_notebook.database.repository import ensure_record_id, repo_query, repo_upsert
|
|
from open_notebook.domain.base import RecordModel
|
|
from open_notebook.utils.encryption import decrypt_value, encrypt_value
|
|
|
|
|
|
class ProviderCredential:
|
|
"""
|
|
A single provider configuration item containing api_key and related settings.
|
|
|
|
This class represents one complete configuration for an AI provider.
|
|
Multiple configurations can exist for the same provider, allowing users
|
|
to have different credentials for different environments (dev, prod, etc.).
|
|
|
|
Attributes:
|
|
id: Unique identifier for this configuration
|
|
name: Human-readable name for this configuration
|
|
provider: Provider name (e.g., "openai", "anthropic")
|
|
is_default: Whether this is the default configuration for the provider
|
|
api_key: The API key (stored as SecretStr for in-memory protection)
|
|
base_url: Base URL for the provider API
|
|
model: Default model to use for this provider
|
|
api_version: API version string (for providers that need it)
|
|
endpoint: Generic endpoint URL
|
|
endpoint_llm: Endpoint URL for LLM service
|
|
endpoint_embedding: Endpoint URL for embedding service
|
|
endpoint_stt: Endpoint URL for speech-to-text service
|
|
endpoint_tts: Endpoint URL for text-to-speech service
|
|
project: Project ID (for Vertex AI)
|
|
location: Location/region (for Vertex AI)
|
|
credentials_path: Path to credentials file (for Vertex AI)
|
|
created: Timestamp when this config was created
|
|
updated: Timestamp when this config was last updated
|
|
"""
|
|
|
|
def __init__(
|
|
self,
|
|
id: str,
|
|
name: str,
|
|
provider: str,
|
|
is_default: bool = False,
|
|
api_key: Optional[SecretStr] = None,
|
|
base_url: Optional[str] = None,
|
|
model: Optional[str] = None,
|
|
api_version: Optional[str] = None,
|
|
endpoint: Optional[str] = None,
|
|
endpoint_llm: Optional[str] = None,
|
|
endpoint_embedding: Optional[str] = None,
|
|
endpoint_stt: Optional[str] = None,
|
|
endpoint_tts: Optional[str] = None,
|
|
project: Optional[str] = None,
|
|
location: Optional[str] = None,
|
|
credentials_path: Optional[str] = None,
|
|
created: Optional[str] = None,
|
|
updated: Optional[str] = None,
|
|
):
|
|
self.id = id
|
|
self.name = name
|
|
self.provider = provider
|
|
self.is_default = is_default
|
|
self.api_key = api_key
|
|
self.base_url = base_url
|
|
self.model = model
|
|
self.api_version = api_version
|
|
self.endpoint = endpoint
|
|
self.endpoint_llm = endpoint_llm
|
|
self.endpoint_embedding = endpoint_embedding
|
|
self.endpoint_stt = endpoint_stt
|
|
self.endpoint_tts = endpoint_tts
|
|
self.project = project
|
|
self.location = location
|
|
self.credentials_path = credentials_path
|
|
self.created = created or datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
self.updated = updated or datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
def to_dict(self, encrypted: bool = False) -> dict:
|
|
"""
|
|
Convert the credential to a dictionary for storage.
|
|
|
|
Args:
|
|
encrypted: If True, api_key is encrypted; otherwise it's a SecretStr
|
|
|
|
Returns:
|
|
Dictionary representation of the credential
|
|
"""
|
|
data = {
|
|
"id": self.id,
|
|
"name": self.name,
|
|
"provider": self.provider,
|
|
"is_default": self.is_default,
|
|
"base_url": self.base_url,
|
|
"model": self.model,
|
|
"api_version": self.api_version,
|
|
"endpoint": self.endpoint,
|
|
"endpoint_llm": self.endpoint_llm,
|
|
"endpoint_embedding": self.endpoint_embedding,
|
|
"endpoint_stt": self.endpoint_stt,
|
|
"endpoint_tts": self.endpoint_tts,
|
|
"project": self.project,
|
|
"location": self.location,
|
|
"credentials_path": self.credentials_path,
|
|
"created": self.created,
|
|
"updated": self.updated,
|
|
}
|
|
|
|
if self.api_key:
|
|
if encrypted:
|
|
data["api_key"] = encrypt_value(self.api_key.get_secret_value())
|
|
else:
|
|
data["api_key"] = self.api_key.get_secret_value()
|
|
|
|
return data
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict, decrypted: bool = False) -> "ProviderCredential":
|
|
"""
|
|
Create a ProviderCredential from a dictionary.
|
|
|
|
Args:
|
|
data: Dictionary containing credential data
|
|
decrypted: If True, api_key is already decrypted; otherwise wrap in SecretStr
|
|
|
|
Returns:
|
|
ProviderCredential instance
|
|
"""
|
|
api_key = None
|
|
if "api_key" in data and data["api_key"]:
|
|
if isinstance(data["api_key"], SecretStr):
|
|
# Already a SecretStr - use as-is
|
|
api_key = data["api_key"]
|
|
elif decrypted:
|
|
# Decrypted string from DB - wrap in SecretStr
|
|
api_key = SecretStr(data["api_key"])
|
|
else:
|
|
# Encrypted string from DB - wrap in SecretStr (will be decrypted later)
|
|
api_key = SecretStr(data["api_key"])
|
|
|
|
return cls(
|
|
id=data["id"],
|
|
name=data["name"],
|
|
provider=data["provider"],
|
|
is_default=data.get("is_default", False),
|
|
api_key=api_key,
|
|
base_url=data.get("base_url"),
|
|
model=data.get("model"),
|
|
api_version=data.get("api_version"),
|
|
endpoint=data.get("endpoint"),
|
|
endpoint_llm=data.get("endpoint_llm"),
|
|
endpoint_embedding=data.get("endpoint_embedding"),
|
|
endpoint_stt=data.get("endpoint_stt"),
|
|
endpoint_tts=data.get("endpoint_tts"),
|
|
project=data.get("project"),
|
|
location=data.get("location"),
|
|
credentials_path=data.get("credentials_path"),
|
|
created=data.get("created"),
|
|
updated=data.get("updated"),
|
|
)
|
|
|
|
|
|
class ProviderConfig(RecordModel):
|
|
"""
|
|
Singleton configuration for multiple provider credentials.
|
|
|
|
Uses RecordModel pattern with a fixed record_id. Stores a dictionary
|
|
of ProviderCredential objects organized by provider name.
|
|
|
|
Usage:
|
|
config = await ProviderConfig.get_instance()
|
|
credentials = config.credentials.get("openai", [])
|
|
default = config.get_default_config("openai")
|
|
"""
|
|
|
|
record_id: ClassVar[str] = "open_notebook:provider_configs"
|
|
|
|
# Store credentials organized by provider name
|
|
# Structure: {"openai": [ProviderCredential, ...], "anthropic": [...], ...}
|
|
credentials: Dict[str, List[ProviderCredential]] = Field(
|
|
default_factory=dict,
|
|
description="Provider credentials organized by provider name",
|
|
)
|
|
|
|
@classmethod
|
|
async def get_instance(cls) -> "ProviderConfig":
|
|
"""
|
|
Always fetch fresh configuration from database.
|
|
|
|
Overrides parent caching behavior to ensure we always get the latest
|
|
configuration values.
|
|
|
|
Returns:
|
|
ProviderConfig: Fresh instance with current database values
|
|
"""
|
|
result = await repo_query(
|
|
"SELECT * FROM ONLY $record_id",
|
|
{"record_id": ensure_record_id(cls.record_id)},
|
|
)
|
|
|
|
if result:
|
|
if isinstance(result, list) and len(result) > 0:
|
|
data = result[0]
|
|
elif isinstance(result, dict):
|
|
data = result
|
|
else:
|
|
data = {}
|
|
else:
|
|
data = {}
|
|
|
|
# Initialize credentials from database data
|
|
credentials: Dict[str, List[ProviderCredential]] = {}
|
|
creds_data = data.get("credentials")
|
|
if creds_data and isinstance(creds_data, dict):
|
|
for provider, provider_creds in creds_data.items():
|
|
if isinstance(provider_creds, list):
|
|
credentials[provider] = []
|
|
for cred_data in provider_creds:
|
|
try:
|
|
# Decrypt api_key if it's a string
|
|
api_key_val = cred_data.get("api_key")
|
|
if api_key_val and isinstance(api_key_val, str):
|
|
decrypted = decrypt_value(api_key_val)
|
|
cred_data["api_key"] = SecretStr(decrypted)
|
|
else:
|
|
# Keep as SecretStr or None
|
|
if api_key_val:
|
|
cred_data["api_key"] = SecretStr(api_key_val)
|
|
else:
|
|
cred_data["api_key"] = None
|
|
|
|
credentials[provider].append(
|
|
ProviderCredential(
|
|
id=cred_data.get("id", ""),
|
|
name=cred_data.get("name", "Default"),
|
|
provider=cred_data.get("provider", provider),
|
|
is_default=cred_data.get("is_default", False),
|
|
api_key=cred_data.get("api_key"),
|
|
base_url=cred_data.get("base_url"),
|
|
model=cred_data.get("model"),
|
|
api_version=cred_data.get("api_version"),
|
|
endpoint=cred_data.get("endpoint"),
|
|
endpoint_llm=cred_data.get("endpoint_llm"),
|
|
endpoint_embedding=cred_data.get(
|
|
"endpoint_embedding"
|
|
),
|
|
endpoint_stt=cred_data.get("endpoint_stt"),
|
|
endpoint_tts=cred_data.get("endpoint_tts"),
|
|
project=cred_data.get("project"),
|
|
location=cred_data.get("location"),
|
|
credentials_path=cred_data.get("credentials_path"),
|
|
created=cred_data.get("created"),
|
|
updated=cred_data.get("updated"),
|
|
)
|
|
)
|
|
except Exception:
|
|
# Skip invalid credentials
|
|
continue
|
|
|
|
# Create instance using model_validate to properly initialize Pydantic model
|
|
instance = cls.model_validate({"credentials": credentials})
|
|
|
|
# Mark as loaded from database
|
|
object.__setattr__(instance, "_db_loaded", True)
|
|
|
|
return instance
|
|
|
|
def get_default_config(self, provider: str) -> Optional[ProviderCredential]:
|
|
"""
|
|
Get the default configuration for a provider.
|
|
|
|
Args:
|
|
provider: Provider name (e.g., "openai", "anthropic")
|
|
|
|
Returns:
|
|
The default ProviderCredential, or None if not found
|
|
"""
|
|
provider_lower = provider.lower()
|
|
credentials = self.credentials.get(provider_lower, [])
|
|
|
|
# First, try to find explicitly marked default
|
|
for cred in credentials:
|
|
if cred.is_default:
|
|
return cred
|
|
|
|
# If no explicit default, return first config
|
|
if credentials:
|
|
return credentials[0]
|
|
|
|
return None
|
|
|
|
def get_config(
|
|
self, provider: str, config_id: str
|
|
) -> Optional[ProviderCredential]:
|
|
"""
|
|
Get a specific configuration by ID.
|
|
|
|
Args:
|
|
provider: Provider name
|
|
config_id: Configuration ID
|
|
|
|
Returns:
|
|
The ProviderCredential if found, None otherwise
|
|
"""
|
|
provider_lower = provider.lower()
|
|
credentials = self.credentials.get(provider_lower, [])
|
|
|
|
for cred in credentials:
|
|
if cred.id == config_id:
|
|
return cred
|
|
|
|
return None
|
|
|
|
def add_config(self, provider: str, credential: ProviderCredential) -> None:
|
|
"""
|
|
Add a new configuration for a provider.
|
|
|
|
If this is the first config for the provider, it becomes the default.
|
|
When adding a new config to an existing provider, the new config becomes
|
|
the default and previous default is unset.
|
|
|
|
Args:
|
|
provider: Provider name (normalized to lowercase)
|
|
credential: ProviderCredential to add
|
|
"""
|
|
provider_lower = provider.lower()
|
|
credential.provider = provider_lower
|
|
|
|
if provider_lower not in self.credentials:
|
|
self.credentials[provider_lower] = []
|
|
|
|
# When adding a new config to an existing provider, make it the default
|
|
# and unset the previous default
|
|
if self.credentials[provider_lower]:
|
|
for cred in self.credentials[provider_lower]:
|
|
cred.is_default = False
|
|
credential.is_default = True
|
|
|
|
# If this is the first config, make it default
|
|
if not self.credentials[provider_lower]:
|
|
credential.is_default = True
|
|
|
|
self.credentials[provider_lower].append(credential)
|
|
|
|
def delete_config(self, provider: str, config_id: str) -> bool:
|
|
"""
|
|
Delete a configuration.
|
|
|
|
Cannot delete the default configuration unless it's the only one.
|
|
|
|
Args:
|
|
provider: Provider name
|
|
config_id: Configuration ID to delete
|
|
|
|
Returns:
|
|
True if deleted, False if not found
|
|
"""
|
|
provider_lower = provider.lower()
|
|
credentials = self.credentials.get(provider_lower, [])
|
|
|
|
for i, cred in enumerate(credentials):
|
|
if cred.id == config_id:
|
|
# Cannot delete default if there are other configs
|
|
if cred.is_default and len(credentials) > 1:
|
|
return False
|
|
|
|
del credentials[i]
|
|
return True
|
|
|
|
return False
|
|
|
|
def set_default_config(self, provider: str, config_id: str) -> bool:
|
|
"""
|
|
Set a configuration as the default for a provider.
|
|
|
|
Args:
|
|
provider: Provider name
|
|
config_id: Configuration ID to make default
|
|
|
|
Returns:
|
|
True if successful, False if config not found
|
|
"""
|
|
provider_lower = provider.lower()
|
|
credentials = self.credentials.get(provider_lower, [])
|
|
|
|
for cred in credentials:
|
|
if cred.id == config_id:
|
|
# Unset all other defaults
|
|
for other in credentials:
|
|
other.is_default = False
|
|
|
|
# Set this one as default
|
|
cred.is_default = True
|
|
cred.updated = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
return True
|
|
|
|
return False
|
|
|
|
def _prepare_save_data(self) -> dict:
|
|
"""
|
|
Prepare data for database storage.
|
|
|
|
SecretStr values are extracted, encrypted, and stored as strings.
|
|
Encryption is performed using Fernet symmetric encryption if
|
|
OPEN_NOTEBOOK_ENCRYPTION_KEY is configured.
|
|
"""
|
|
data = {"credentials": {}}
|
|
|
|
for provider, credentials in self.credentials.items():
|
|
data["credentials"][provider] = []
|
|
for cred in credentials:
|
|
cred.updated = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
|
data["credentials"][provider].append(cred.to_dict(encrypted=True))
|
|
|
|
return data
|
|
|
|
async def save(self) -> "ProviderConfig":
|
|
"""
|
|
Save the configuration to the database.
|
|
|
|
Uses _prepare_save_data() to properly handle SecretStr conversion
|
|
and encryption.
|
|
"""
|
|
data = self._prepare_save_data()
|
|
await repo_upsert("open_notebook", self.record_id, data)
|
|
return self
|
|
|
|
@classmethod
|
|
def _clear_for_test(cls) -> None:
|
|
"""Clear the singleton instance for testing purposes."""
|
|
if cls.record_id in cls._instances:
|
|
del cls._instances[cls.record_id]
|