Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
feat: Add multi-tenant file encryption capability (#828)
baojun-zhang8cdecd916303
diff --git a/Cargo.lock b/Cargo.lockindex 7593e6ae9..ae50a74b9 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -866,6 +866,12 @@ version = "0.5.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"+[[package]]+name = "hex"+version = "0.4.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"+[[package]]name = "hmac"version = "0.12.1"@@ -1442,6 +1448,8 @@ dependencies = ["crossterm 0.28.1","dirs","futures",+ "getrandom 0.2.17",+ "hex","machine-uid","mime_guess","ratatui",diff --git a/crates/ov_cli/Cargo.toml b/crates/ov_cli/Cargo.tomlindex ea7bcd14e..c34ebdae7 100644--- a/crates/ov_cli/Cargo.toml+++ b/crates/ov_cli/Cargo.toml@@ -33,3 +33,5 @@ rustyline = "14.0"uuid = { version = "1.0", features = ["v4", "serde"] }machine-uid = "0.5"termimad = "0.34"+hex = "0.4"+getrandom = "0.2"diff --git a/crates/ov_cli/src/commands/crypto.rs b/crates/ov_cli/src/commands/crypto.rsnew file mode 100644index 000000000..9eeb15ba0--- /dev/null+++ b/crates/ov_cli/src/commands/crypto.rs@@ -0,0 +1,110 @@+//! Crypto commands for OpenViking CLI.+//!+//! This module provides commands for managing cryptographic keys,+//! including generating and initializing root keys.++use crate::error::{Error, Result};+use clap::Subcommand;+use dirs::home_dir;+use std::fs::OpenOptions;+use std::io::Write;+use std::path::PathBuf;++/// Crypto subcommands.+#[derive(Subcommand, Debug)]+pub enum CryptoCommands {+ /// Initialize a new root key.+ ///+ /// Generates a 32-byte random key and saves it to the specified file.+ /// If the file already exists, this command will fail.+ InitKey {+ /// Output file path for the root key.+ ///+ /// Defaults to `~/.openviking/master.key`.+ #[arg(long = "output-file", short = 'f')]+ output_file: Option<PathBuf>,+ },+}++/// Handle crypto commands.+pub async fn handle_crypto(cmd: CryptoCommands) -> Result<()> {+ match cmd {+ CryptoCommands::InitKey { output_file } => handle_init_key(output_file).await,+ }+}++/// Handle the init-key command.+async fn handle_init_key(output_file: Option<PathBuf>) -> Result<()> {+ let key_path = get_key_path(output_file)?;++ // Check if file already exists+ if key_path.exists() {+ return Err(Error::Client(format!(+ "Key file already exists at: {}\n\+ Use a different path or delete the existing file first.",+ key_path.display()+ )));+ }++ // Create parent directory if it doesn't exist+ if let Some(parent) = key_path.parent() {+ if !parent.exists() {+ std::fs::create_dir_all(parent)+ .map_err(|e| Error::Client(format!("Failed to create directory: {}", e)))?;+ }+ }++ // Generate 32-byte random key+ let mut key = vec![0u8; 32];+ getrandom::getrandom(&mut key)+ .map_err(|e| Error::Client(format!("Failed to generate random key: {}", e)))?;++ // Convert to hex string+ let hex_key = hex::encode(&key);++ // Write to file+ let mut file = OpenOptions::new()+ .write(true)+ .create_new(true)+ .open(&key_path)+ .map_err(|e| Error::Client(format!("Failed to create key file: {}", e)))?;++ file.write_all(hex_key.as_bytes())+ .map_err(|e| Error::Client(format!("Failed to write to key file: {}", e)))?;++ // Set permissions to 0600 (owner read/write only) on Unix platforms+ #[cfg(unix)]+ {+ use std::os::unix::fs::PermissionsExt;+ let mut perms = file+ .metadata()+ .map_err(|e| Error::Client(format!("Failed to get file metadata: {}", e)))?+ .permissions();+ perms.set_mode(0o600);+ file.set_permissions(perms)+ .map_err(|e| Error::Client(format!("Failed to set file permissions: {}", e)))?;+ }++ // On Windows, permissions are handled differently+ #[cfg(windows)]+ {+ // Windows doesn't support Unix-style permissions, skip setting mode+ }++ println!("Successfully generated root key at: {}", key_path.display());+ println!("Key permissions set to 0600 (owner read/write only)");++ Ok(())+}++/// Get the key path, using the default if not provided.+fn get_key_path(output_file: Option<PathBuf>) -> Result<PathBuf> {+ match output_file {+ Some(path) => Ok(path),+ None => {+ let home = home_dir()+ .ok_or_else(|| Error::Client("Failed to determine home directory".to_string()))?;+ Ok(home.join(".openviking").join("master.key"))+ }+ }+}diff --git a/crates/ov_cli/src/commands/mod.rs b/crates/ov_cli/src/commands/mod.rsindex 1e8d1bcda..e9786eb65 100644--- a/crates/ov_cli/src/commands/mod.rs+++ b/crates/ov_cli/src/commands/mod.rs@@ -1,6 +1,7 @@pub mod admin;pub mod chat;pub mod content;+pub mod crypto;pub mod search;pub mod filesystem;pub mod observer;diff --git a/crates/ov_cli/src/main.rs b/crates/ov_cli/src/main.rsindex 54d955d2b..7a1b8cdea 100644--- a/crates/ov_cli/src/main.rs+++ b/crates/ov_cli/src/main.rs@@ -393,6 +393,11 @@ enum SystemCommands {Status,/// Quick health checkHealth,+ /// Cryptographic key management commands+ Crypto {+ #[command(subcommand)]+ action: commands::crypto::CryptoCommands,+ },}#[derive(Subcommand)]@@ -822,6 +827,7 @@ async fn handle_system(cmd: SystemCommands, ctx: CliContext) -> Result<()> {commands::system::health(&client, ctx.output_format, ctx.compact).await?;Ok(())}+ SystemCommands::Crypto { action } => commands::crypto::handle_crypto(action).await,}}diff --git a/examples/ov.conf.example b/examples/ov.conf.exampleindex e2573abcd..18e69ded6 100644--- a/examples/ov.conf.example+++ b/examples/ov.conf.example@@ -161,4 +161,11 @@"preserve_line_breaks": false}}+ "encryption": {+ "enabled": true,+ "provider": "local",+ "local": {+ "key_file": "~/.openviking/master.key"+ }+ }}diff --git a/openviking/crypto/__init__.py b/openviking/crypto/__init__.pynew file mode 100644index 000000000..8de3defc4--- /dev/null+++ b/openviking/crypto/__init__.py@@ -0,0 +1,46 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+OpenViking Encryption Module++Provides multi-tenant encryption functionality, including:+- Envelope Encryption+- Multiple key providers (Local File, Vault, Volcengine KMS)+- API Key hashing storage (Argon2id)+"""++from openviking.crypto.config import (+ bootstrap_encryption,+ validate_encryption_config,+)+from openviking.crypto.encryptor import FileEncryptor+from openviking.crypto.exceptions import (+ AuthenticationFailedError,+ CorruptedCiphertextError,+ EncryptionError,+ InvalidMagicError,+ KeyMismatchError,+)+from openviking.crypto.providers import (+ LocalFileProvider,+ RootKeyProvider,+ VaultProvider,+ VolcengineKMSProvider,+ create_root_key_provider,+)++__all__ = [+ "RootKeyProvider",+ "LocalFileProvider",+ "VaultProvider",+ "VolcengineKMSProvider",+ "create_root_key_provider",+ "FileEncryptor",+ "validate_encryption_config",+ "bootstrap_encryption",+ "EncryptionError",+ "InvalidMagicError",+ "CorruptedCiphertextError",+ "AuthenticationFailedError",+ "KeyMismatchError",+]diff --git a/openviking/crypto/config.py b/openviking/crypto/config.pynew file mode 100644index 000000000..bba2067bc--- /dev/null+++ b/openviking/crypto/config.py@@ -0,0 +1,208 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+Encryption module configuration management.++Provides configuration validation and encryption module initialization.+"""++import os+from pathlib import Path+from typing import Any, Dict, List, Optional, Tuple++from openviking.crypto.encryptor import FileEncryptor+from openviking.crypto.exceptions import ConfigError+from openviking.crypto.providers import create_root_key_provider+from openviking_cli.utils.logger import get_logger++logger = get_logger(__name__)+++def validate_encryption_config(config: Dict[str, Any]) -> Tuple[bool, List[str]]:+ """+ Validate encryption configuration.++ Args:+ config: Configuration dictionary++ Returns:+ (is_valid, errors)+ """+ errors = []+ encryption_config = config.get("encryption", {})++ # Check enabled+ enabled = encryption_config.get("enabled", False)+ if not isinstance(enabled, bool):+ errors.append("encryption.enabled must be a boolean")++ if not enabled:+ return len(errors) == 0, errors++ # Check provider+ provider = encryption_config.get("provider", "local")+ supported_providers = ["local", "vault", "volcengine_kms"]+ if provider not in supported_providers:+ errors.append(f"Unsupported provider: {provider}")++ # Provider-specific validation+ if provider == "local":+ errors.extend(_validate_local_provider_config(encryption_config))+ elif provider == "vault":+ errors.extend(_validate_vault_provider_config(encryption_config))+ elif provider == "volcengine_kms":+ errors.extend(_validate_volcengine_provider_config(encryption_config))++ return len(errors) == 0, errors+++def _validate_local_provider_config(config: Dict[str, Any]) -> List[str]:+ """Validate Local Provider configuration."""+ errors = []+ local_config = config.get("local", {})+ key_file_path = local_config.get("key_file", "~/.openviking/master.key")++ if not key_file_path:+ errors.append("encryption.local.key_file is required")+ return errors++ # Check if file exists or can be created+ key_file = Path(key_file_path)+ if key_file.exists():+ # Check permissions+ if os.name != "nt": # Skip permission check on Windows+ if (key_file.stat().st_mode & 0o077) != 0:+ errors.append(f"Key file permissions too open: {key_file_path} (should be 0600)")+ else:+ # Check if parent directory is writable+ parent_dir = key_file.parent+ if not parent_dir.exists():+ try:+ parent_dir.mkdir(parents=True, exist_ok=True)+ except Exception as e:+ errors.append(f"Cannot create parent directory for key file: {e}")+ elif not os.access(parent_dir, os.W_OK):+ errors.append(f"Cannot create key file at: {key_file_path}")++ return errors+++def _validate_vault_provider_config(config: Dict[str, Any]) -> List[str]:+ """Validate Vault Provider configuration."""+ errors = []+ vault_config = config.get("vault", {})+ address = vault_config.get("address")+ token = vault_config.get("token")++ if not address:+ errors.append("encryption.vault.address is required")+ if not token:+ errors.append("encryption.vault.token is required")++ return errors+++def _validate_volcengine_provider_config(config: Dict[str, Any]) -> List[str]:+ """Validate Volcengine Provider configuration."""+ errors = []+ volc_config = config.get("volcengine_kms", {})+ region = volc_config.get("region")+ access_key = volc_config.get("access_key")+ secret_key = volc_config.get("secret_key")+ key_id = volc_config.get("key_id")++ if not region:+ errors.append("encryption.volcengine_kms.region is required")+ if not access_key:+ errors.append("encryption.volcengine_kms.access_key is required")+ if not secret_key:+ errors.append("encryption.volcengine_kms.secret_key is required")+ if not key_id:+ errors.append("encryption.volcengine_kms.key_id is required")++ return errors+++async def bootstrap_encryption(config: Dict[str, Any]) -> Optional[FileEncryptor]:+ """+ Initialize encryption module.++ Args:+ config: Configuration dictionary++ Returns:+ FileEncryptor instance, or None if encryption is not enabled+ """+ encryption_config = config.get("encryption", {})+ if not encryption_config.get("enabled", False):+ logger.info("Encryption is disabled")+ return None++ # Validate configuration+ is_valid, errors = validate_encryption_config(config)+ if not is_valid:+ error_msg = "; ".join(errors)+ raise ConfigError(f"Invalid encryption configuration: {error_msg}")++ # Create Provider+ provider_type = encryption_config.get("provider", "local")+ provider = create_root_key_provider(provider_type, encryption_config)++ # Create FileEncryptor+ encryptor = FileEncryptor(provider)+ logger.info("Encryption bootstrapped successfully with provider: %s", provider_type)++ return encryptor+++async def encryption_health_check(config: Dict[str, Any]) -> Dict[str, Any]:+ """+ Encryption module health check.++ Args:+ config: Configuration dictionary++ Returns:+ Health status dictionary+ """+ result = {"status": "healthy", "checks": {}}++ encryption_config = config.get("encryption", {})+ if not encryption_config.get("enabled", False):+ result["checks"]["encryption"] = "disabled"+ return result++ try:+ # 1. Configuration validation+ is_valid, errors = validate_encryption_config(config)+ if not is_valid:+ result["status"] = "unhealthy"+ result["checks"]["config"] = {"status": "failed", "errors": errors}+ return result+ result["checks"]["config"] = {"status": "passed"}++ # 2. Initialize encryption module+ encryptor = await bootstrap_encryption(config)+ if encryptor is None:+ result["status"] = "unhealthy"+ result["error"] = "Failed to bootstrap encryption"+ return result+ result["checks"]["bootstrap"] = {"status": "passed"}++ # 3. Test encrypt/decrypt+ test_account = "health-check-account"+ test_content = b"health check test"++ encrypted_content = await encryptor.encrypt(test_account, test_content)+ result["checks"]["encrypt"] = {"status": "passed"}++ decrypted_content = await encryptor.decrypt(test_account, encrypted_content)+ assert decrypted_content == test_content+ result["checks"]["decrypt"] = {"status": "passed"}++ except Exception as e:+ result["status"] = "unhealthy"+ result["error"] = str(e)+ logger.exception("Encryption health check failed")++ return resultdiff --git a/openviking/crypto/encryptor.py b/openviking/crypto/encryptor.pynew file mode 100644index 000000000..82943485b--- /dev/null+++ b/openviking/crypto/encryptor.py@@ -0,0 +1,265 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+File encryptor - envelope encryption implementation.++Implements Envelope Encryption pattern:+- Each file has independent random File Key+- File Key is encrypted with Account Key+- Account Key is derived from Root Key+"""++import secrets+import struct+from typing import Tuple++from openviking.crypto.exceptions import (+ AuthenticationFailedError,+ CorruptedCiphertextError,+ InvalidMagicError,+ KeyMismatchError,+)+from openviking.crypto.providers import (+ PROVIDER_LOCAL,+ PROVIDER_VAULT,+ PROVIDER_VOLCENGINE,+ RootKeyProvider,+)+from openviking_cli.utils.logger import get_logger++logger = get_logger(__name__)++# Magic number: OpenViking Encryption v1+MAGIC = b"OVE1"+MAGIC_LENGTH = len(MAGIC)++# Envelope format version+VERSION = 0x01+++class FileEncryptor:+ """File encryptor."""++ def __init__(self, provider: RootKeyProvider):+ """+ Initialize FileEncryptor.++ Args:+ provider: RootKeyProvider instance+ """+ self.provider = provider+ self._provider_type = self._detect_provider_type(provider)++ def _detect_provider_type(self, provider: RootKeyProvider) -> int:+ """Detect Provider type."""+ from openviking.crypto.providers import (+ LocalFileProvider,+ VaultProvider,+ VolcengineKMSProvider,+ )++ if isinstance(provider, LocalFileProvider):+ return PROVIDER_LOCAL+ elif isinstance(provider, VaultProvider):+ return PROVIDER_VAULT+ elif isinstance(provider, VolcengineKMSProvider):+ return PROVIDER_VOLCENGINE+ else:+ raise ValueError(f"Unknown provider type: {type(provider)}")++ async def encrypt(self, account_id: str, plaintext: bytes) -> bytes:+ """+ Encrypt file content.++ Args:+ account_id: Account ID+ plaintext: Plaintext content++ Returns:+ Encrypted content (Envelope format)+ """+ # 1. Generate random File Key+ file_key = secrets.token_bytes(32)++ # 2. Encrypt file content+ data_iv = secrets.token_bytes(12)+ encrypted_content = await self._aes_gcm_encrypt(file_key, data_iv, plaintext)++ # 3. Encrypt File Key+ key_iv = b""+ if self._provider_type == PROVIDER_LOCAL:+ encrypted_file_key, key_iv = await self.provider.encrypt_file_key(file_key, account_id)+ else:+ encrypted_file_key = await self.provider.encrypt_file_key(file_key, account_id)++ # 4. Build Envelope+ return self._build_envelope(+ self._provider_type,+ encrypted_file_key,+ key_iv,+ data_iv,+ encrypted_content,+ )++ async def decrypt(self, account_id: str, ciphertext: bytes) -> bytes:+ """+ Decrypt file content.++ Args:+ account_id: Account ID+ ciphertext: Ciphertext content++ Returns:+ Decrypted plaintext content+ """+ # 1. Check magic number+ if len(ciphertext) < MAGIC_LENGTH:+ raise InvalidMagicError("Ciphertext too short")++ if not ciphertext.startswith(MAGIC):+ # Unencrypted file, return directly+ return ciphertext++ try:+ # 2. Parse Envelope+ (+ provider_type,+ encrypted_file_key,+ key_iv,+ data_iv,+ encrypted_content,+ ) = self._parse_envelope(ciphertext)+ except Exception as e:+ raise CorruptedCiphertextError(f"Failed to parse envelope: {e}")++ try:+ # 3. Decrypt File Key+ if provider_type == PROVIDER_LOCAL:+ file_key = await self.provider.decrypt_file_key(+ encrypted_file_key, key_iv, account_id+ )+ else:+ file_key = await self.provider.decrypt_file_key(encrypted_file_key, account_id)+ except Exception as e:+ raise KeyMismatchError(f"Failed to decrypt file key: {e}")++ try:+ # 4. Decrypt file content+ return await self._aes_gcm_decrypt(file_key, data_iv, encrypted_content)+ except Exception as e:+ raise AuthenticationFailedError(f"Authentication failed: {e}")++ def _build_envelope(+ self,+ provider_type: int,+ encrypted_file_key: bytes,+ key_iv: bytes,+ data_iv: bytes,+ encrypted_content: bytes,+ ) -> bytes:+ """+ Build Envelope.++ Envelope format:+ - Magic (4B): b"OVE1"+ - Version (1B): 0x01+ - Provider Type (1B)+ - Encrypted File Key Length (2B, big-endian)+ - Key IV Length (2B, big-endian)+ - Data IV Length (2B, big-endian)+ - Encrypted File Key (variable)+ - Key IV (variable, only for Local Provider)+ - Data IV (variable)+ - Encrypted Content (variable)+ """+ # Calculate lengths of each part+ efk_len = len(encrypted_file_key)+ kiv_len = len(key_iv)+ div_len = len(data_iv)++ # Build header+ header = struct.pack(+ "!4sBBHHH",+ MAGIC,+ VERSION,+ provider_type,+ efk_len,+ kiv_len,+ div_len,+ )++ # Concatenate all parts+ return header + encrypted_file_key + key_iv + data_iv + encrypted_content++ def _parse_envelope(self, ciphertext: bytes) -> Tuple[int, bytes, bytes, bytes, bytes]:+ """+ Parse Envelope.++ Returns:+ (provider_type, encrypted_file_key, key_iv, data_iv, encrypted_content)+ """+ # Fixed header size: 4(magic) + 1(version) + 1(provider) + 2(efk_len) + 2(kiv_len) + 2(div_len) = 12 bytes+ HEADER_SIZE = 12++ if len(ciphertext) < HEADER_SIZE:+ raise CorruptedCiphertextError("Envelope too short")++ # Parse header+ (+ magic,+ version,+ provider_type,+ efk_len,+ kiv_len,+ div_len,+ ) = struct.unpack("!4sBBHHH", ciphertext[:HEADER_SIZE])++ # Verify magic and version+ if magic != MAGIC:+ raise InvalidMagicError(f"Invalid magic: {magic}")+ if version != VERSION:+ raise CorruptedCiphertextError(f"Unsupported version: {version}")++ # Calculate offsets for each part+ offset = HEADER_SIZE+ efk_end = offset + efk_len+ kiv_end = efk_end + kiv_len+ div_end = kiv_end + div_len++ # Verify length+ if len(ciphertext) < div_end:+ raise CorruptedCiphertextError("Incomplete envelope")++ # Extract each part+ encrypted_file_key = ciphertext[offset:efk_end]+ key_iv = ciphertext[efk_end:kiv_end]+ data_iv = ciphertext[kiv_end:div_end]+ encrypted_content = ciphertext[div_end:]++ return provider_type, encrypted_file_key, key_iv, data_iv, encrypted_content++ async def _aes_gcm_encrypt(self, key: bytes, iv: bytes, plaintext: bytes) -> bytes:+ """AES-GCM encryption."""+ try:+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM++ aesgcm = AESGCM(key)+ return aesgcm.encrypt(iv, plaintext, associated_data=None)+ except ImportError:+ from openviking.crypto.exceptions import ConfigError++ raise ConfigError("cryptography library is required for encryption")++ async def _aes_gcm_decrypt(self, key: bytes, iv: bytes, ciphertext: bytes) -> bytes:+ """AES-GCM decryption."""+ try:+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM++ aesgcm = AESGCM(key)+ return aesgcm.decrypt(iv, ciphertext, associated_data=None)+ except ImportError:+ from openviking.crypto.exceptions import ConfigError++ raise ConfigError("cryptography library is required for encryption")+ except Exception as e:+ raise AuthenticationFailedError(f"Decryption failed: {e}")diff --git a/openviking/crypto/exceptions.py b/openviking/crypto/exceptions.pynew file mode 100644index 000000000..e0e54afe0--- /dev/null+++ b/openviking/crypto/exceptions.py@@ -0,0 +1,53 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+Encryption module exception definitions.+"""+++class EncryptionError(Exception):+ """Base class for encryption-related errors."""++ pass+++class InvalidMagicError(EncryptionError):+ """Invalid magic number error."""++ pass+++class CorruptedCiphertextError(EncryptionError):+ """Corrupted ciphertext error."""++ pass+++class AuthenticationFailedError(EncryptionError):+ """Authentication tag verification failed error."""++ pass+++class KeyMismatchError(EncryptionError):+ """Key mismatch error."""++ pass+++class KeyNotFoundError(EncryptionError):+ """Key not found error."""++ pass+++class ConfigError(EncryptionError):+ """Configuration error."""++ pass+++class KMSError(EncryptionError):+ """KMS service error."""++ passdiff --git a/openviking/crypto/providers.py b/openviking/crypto/providers.pynew file mode 100644index 000000000..9a29f7318--- /dev/null+++ b/openviking/crypto/providers.py@@ -0,0 +1,596 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+Key provider abstractions and implementations.++Provides multiple key management methods:+- LocalFileProvider: Local file storage for Root Key+- VaultProvider: HashiCorp Vault+- VolcengineKMSProvider: Volcengine KMS+"""++import abc+import os+import secrets+from abc import ABC+from pathlib import Path+from typing import Any, Dict, Optional, Tuple++from openviking.crypto.exceptions import (+ AuthenticationFailedError,+ ConfigError,+)+from openviking_cli.utils.logger import get_logger++logger = get_logger(__name__)++# HKDF related constants+HKDF_SALT = b"openviking-kek-salt-v1"+HKDF_INFO_PREFIX = b"openviking:kek:v1:"++# Provider types+PROVIDER_LOCAL = 0x01+PROVIDER_VAULT = 0x02+PROVIDER_VOLCENGINE = 0x03+++class RootKeyProvider(ABC):+ """Root Key Provider abstract base class."""++ @abc.abstractmethod+ async def get_root_key(self) -> bytes:+ """Get Root Key (only used by Local Provider)."""+ pass++ @abc.abstractmethod+ async def derive_account_key(self, account_id: str) -> bytes:+ """Derive Account Key for the specified account."""+ pass++ @abc.abstractmethod+ async def encrypt_file_key(self, plaintext_key: bytes, account_id: str) -> Any:+ """Encrypt File Key."""+ pass++ @abc.abstractmethod+ async def decrypt_file_key(self, encrypted_key: Any, account_id: str) -> bytes:+ """Decrypt File Key."""+ pass+++class LocalFileProvider(RootKeyProvider):+ """Local file Root Key Provider."""++ def __init__(self, key_file: str):+ """+ Initialize LocalFileProvider.++ Args:+ key_file: Root Key file path+ """+ self.key_file = Path(key_file)+ self._root_key: Optional[bytes] = None++ async def get_root_key(self) -> bytes:+ """Get Root Key."""+ if self._root_key is None:+ self._root_key = await self._load_or_create_root_key()+ return self._root_key++ async def _load_or_create_root_key(self) -> bytes:+ """Load or create Root Key."""+ if self.key_file.exists():+ # Read existing key+ with open(self.key_file, "r") as f:+ hex_key = f.read().strip()+ try:+ return bytes.fromhex(hex_key)+ except ValueError:+ raise ConfigError(f"Invalid root key format in {self.key_file}")+ else:+ # Create new key+ root_key = secrets.token_bytes(32)+ # Ensure parent directory exists+ self.key_file.parent.mkdir(parents=True, exist_ok=True)+ # Write file with permissions 0600+ with open(self.key_file, "w") as f:+ f.write(root_key.hex())+ # Set file permissions+ os.chmod(self.key_file, 0o600)+ logger.info("Created new root key at %s", self.key_file)+ return root_key++ async def derive_account_key(self, account_id: str) -> bytes:+ """Derive Account Key from Root Key."""+ root_key = await self.get_root_key()+ return await self._hkdf_derive(root_key, account_id)++ async def _hkdf_derive(self, root_key: bytes, account_id: str) -> bytes:+ """Derive key using HKDF."""+ try:+ from cryptography.hazmat.primitives import hashes+ from cryptography.hazmat.primitives.kdf.hkdf import HKDF++ hkdf = HKDF(+ algorithm=hashes.SHA256(),+ length=32,+ salt=HKDF_SALT,+ info=HKDF_INFO_PREFIX + account_id.encode(),+ )+ return hkdf.derive(root_key)+ except ImportError:+ raise ConfigError("cryptography library is required for encryption")++ async def encrypt_file_key(self, plaintext_key: bytes, account_id: str) -> Tuple[bytes, bytes]:+ """+ Encrypt File Key.++ Returns:+ (encrypted_key, iv)+ """+ account_key = await self.derive_account_key(account_id)+ iv = secrets.token_bytes(12)+ encrypted_key = await self._aes_gcm_encrypt(account_key, iv, plaintext_key)+ return encrypted_key, iv++ async def decrypt_file_key(self, encrypted_key: bytes, iv: bytes, account_id: str) -> bytes:+ """Decrypt File Key."""+ account_key = await self.derive_account_key(account_id)+ return await self._aes_gcm_decrypt(account_key, iv, encrypted_key)++ async def _aes_gcm_encrypt(self, key: bytes, iv: bytes, plaintext: bytes) -> bytes:+ """AES-GCM encryption."""+ try:+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM++ aesgcm = AESGCM(key)+ return aesgcm.encrypt(iv, plaintext, associated_data=None)+ except ImportError:+ raise ConfigError("cryptography library is required for encryption")++ async def _aes_gcm_decrypt(self, key: bytes, iv: bytes, ciphertext: bytes) -> bytes:+ """AES-GCM decryption."""+ try:+ from cryptography.hazmat.primitives.ciphers.aead import AESGCM++ aesgcm = AESGCM(key)+ return aesgcm.decrypt(iv, ciphertext, associated_data=None)+ except ImportError:+ raise ConfigError("cryptography library is required for encryption")+ except Exception as e:+ raise AuthenticationFailedError(f"Decryption failed: {e}")+++class VaultProvider(RootKeyProvider):+ """HashiCorp Vault Key Provider.++ Uses HashiCorp Vault's transit secrets engine for key management.+ Core features:+ - Root key management: Vault transit secrets engine+ - Account Key derivation: HKDF-SHA256 (from root key)+ - File Key encryption: Vault transit encryption API+ """++ SALT = b"OpenViking_KDF_Salt"+ INFO_PREFIX = b"OpenViking_Account_"+ ROOT_KEY_NAME = "openviking-root-key"++ def __init__(self, addr: str, token: str, mount_path: str = "transit"):+ """+ Initialize VaultProvider.++ Args:+ addr: Vault server address (e.g., "http://127.0.0.1:8200")+ token: Vault authentication token+ mount_path: Transit secrets engine mount path (default: "transit")+ """+ self.addr = addr+ self.token = token+ self.mount_path = mount_path+ self._client = None+ self._root_key: Optional[bytes] = None++ async def _get_client(self):+ """+ Get or create Vault client.++ Returns:+ Vault client instance+ """+ if not self._client:+ try:+ import hvac+ except ImportError:+ raise ConfigError(+ "hvac library is required for Vault provider. Install with: pip install hvac"+ )++ self._client = hvac.Client(url=self.addr, token=self.token)++ # Verify Vault is accessible+ if not self._client.is_authenticated():+ raise AuthenticationFailedError("Failed to authenticate with Vault")++ # Ensure transit engine is enabled+ await self._ensure_transit_engine_enabled()++ # Ensure root key exists+ await self._ensure_root_key_exists()++ return self._client++ async def _ensure_transit_engine_enabled(self):+ """Ensure transit secrets engine is enabled."""+ try:+ # Check if transit engine is already enabled+ self._client.sys.list_mounted_secrets_engines()+ if f"{self.mount_path}/" not in self._client.sys.list_mounted_secrets_engines()["data"]:+ self._client.sys.enable_secrets_engine(backend_type="transit", path=self.mount_path)+ logger.info(f"Enabled transit secrets engine at {self.mount_path}")+ except Exception as e:+ logger.warning(f"Failed to check/enable transit engine: {e}")++ async def _ensure_root_key_exists(self):+ """Ensure root key exists in Vault transit engine."""+ try:+ # Try to read the key+ self._client.secrets.transit.read_key(+ name=self.ROOT_KEY_NAME, mount_point=self.mount_path+ )+ except Exception:+ # Key doesn't exist, create it+ self._client.secrets.transit.create_key(+ name=self.ROOT_KEY_NAME, key_type="aes256-gcm96", mount_point=self.mount_path+ )+ logger.info(f"Created root key {self.ROOT_KEY_NAME} in Vault")++ async def get_root_key(self) -> bytes:+ """+ Get root key from Vault.++ Note: Since we can't export the key directly from Vault transit,+ we use a fixed seed and derive the root key.++ Returns:+ Root key+ """+ if self._root_key is None:+ # Use a fixed seed for root key derivation+ self._root_key = b"OpenViking_Vault_Root_Seed_Key_v1"+ return self._root_key++ async def derive_account_key(self, account_id: str) -> bytes:+ """+ Derive Account Key using HKDF.++ Args:+ account_id: Account ID++ Returns:+ Derived Account Key+ """+ root_key = await self.get_root_key()+ return await self._hkdf_derive(root_key, account_id)++ async def _hkdf_derive(self, root_key: bytes, account_id: str) -> bytes:+ """+ Derive key using HKDF.++ Args:+ root_key: Root key+ account_id: Account ID++ Returns:+ Derived key+ """+ try:+ from cryptography.hazmat.primitives import hashes+ from cryptography.hazmat.primitives.kdf.hkdf import HKDF++ hkdf = HKDF(+ algorithm=hashes.SHA256(),+ length=32,+ salt=self.SALT,+ info=self.INFO_PREFIX + account_id.encode(),+ )+ return hkdf.derive(root_key)+ except ImportError:+ raise ConfigError("cryptography library is required for encryption")++ async def encrypt_file_key(self, plaintext_key: bytes, account_id: str) -> bytes:+ """+ Encrypt File Key using Vault transit.++ Args:+ plaintext_key: Plaintext File Key+ account_id: Account ID (not used in Vault mode)++ Returns:+ Encrypted File Key as bytes+ """+ client = await self._get_client()++ import base64++ plaintext_b64 = base64.b64encode(plaintext_key).decode("utf-8")++ response = client.secrets.transit.encrypt_data(+ name=self.ROOT_KEY_NAME, plaintext=plaintext_b64, mount_point=self.mount_path+ )++ ciphertext_str = response["data"]["ciphertext"]+ return ciphertext_str.encode("utf-8")++ async def decrypt_file_key(self, encrypted_key: bytes, account_id: str) -> bytes:+ """+ Decrypt File Key using Vault transit.++ Args:+ encrypted_key: Encrypted File Key (ciphertext bytes from Vault)+ account_id: Account ID (not used in Vault mode)++ Returns:+ Decrypted File Key+ """+ client = await self._get_client()++ ciphertext_str = encrypted_key.decode("utf-8")++ response = client.secrets.transit.decrypt_data(+ name=self.ROOT_KEY_NAME, ciphertext=ciphertext_str, mount_point=self.mount_path+ )++ import base64++ return base64.b64decode(response["data"]["plaintext"])+++class VolcengineKMSProvider(RootKeyProvider):+ """Volcengine KMS Key Provider.++ Suitable for production environments, using Volcengine KMS service for key management.+ Core features:+ - Root key storage: Volcengine KMS+ - Account Key derivation: HKDF-SHA256+ - File Key encryption: Volcengine KMS encryption API+ """++ SALT = b"OpenViking_KDF_Salt"+ INFO_PREFIX = b"OpenViking_Account_"++ def __init__(+ self,+ region: str,+ access_key_id: str,+ secret_access_key: str,+ key_id: str,+ endpoint: Optional[str] = None,+ ):+ """+ Initialize Volcengine KMS Provider.++ Args:+ region: Region (e.g., cn-beijing)+ access_key_id: Volcengine Access Key ID+ secret_access_key: Volcengine Secret Access Key+ key_id: Volcengine KMS Key ID (immutable system-generated identifier)+ endpoint: Custom KMS service endpoint (optional)+ """+ self.region = region+ self.access_key_id = access_key_id+ self.secret_access_key = secret_access_key+ self.key_id = key_id+ self.endpoint = endpoint or f"kms.{region}.volcengineapi.com"+ self._kms_client = None++ async def _get_kms_client(self):+ """+ Get Volcengine KMS client.++ Returns:+ KMS client instance+ """+ if not self._kms_client:+ try:+ import base64+ import json++ from volcengine.ApiInfo import ApiInfo+ from volcengine.base.Service import Service+ from volcengine.Credentials import Credentials+ from volcengine.ServiceInfo import ServiceInfo+ except ImportError:+ raise ConfigError(+ "volcengine is required for Volcengine KMS. "+ "Install with: pip install volcengine"+ )++ class KmsService(Service):+ def __init__(self, region, access_key_id, secret_access_key, endpoint):+ self.service_info = self.get_service_info(+ region, access_key_id, secret_access_key, endpoint+ )+ self.api_info = self.get_api_info()+ super(KmsService, self).__init__(self.service_info, self.api_info)++ @staticmethod+ def get_service_info(region, access_key_id, secret_access_key, endpoint):+ credentials = Credentials(access_key_id, secret_access_key, "kms", region)+ service_info = ServiceInfo(+ endpoint, {"Accept": "application/json"}, credentials, 30, 30, "https"+ )+ return service_info++ @staticmethod+ def get_api_info():+ api_info = {+ "Encrypt": ApiInfo(+ "POST", "/", {"Action": "Encrypt", "Version": "2021-02-18"}, {}, {}+ ),+ "Decrypt": ApiInfo(+ "POST", "/", {"Action": "Decrypt", "Version": "2021-02-18"}, {}, {}+ ),+ }+ return api_info++ def encrypt(self, key_id, plaintext):+ body = {+ "KeyID": key_id,+ "Plaintext": base64.b64encode(plaintext).decode("utf-8"),+ }++ res = self.json("Encrypt", {}, json.dumps(body))+ if res == "":+ raise Exception("empty response")+ res_json = json.loads(res)+ if "ResponseMetadata" in res_json and "Error" in res_json["ResponseMetadata"]:+ raise Exception(f"KMS Error: {res_json['ResponseMetadata']['Error']}")+ if "Result" in res_json and "CiphertextBlob" in res_json["Result"]:+ return base64.b64decode(res_json["Result"]["CiphertextBlob"])+ raise Exception(f"Unexpected response: {res_json}")++ def decrypt(self, ciphertext_blob, key_id):+ body = {+ "KeyID": key_id,+ "CiphertextBlob": base64.b64encode(ciphertext_blob).decode("utf-8"),+ }++ res = self.json("Decrypt", {}, json.dumps(body))+ if res == "":+ raise Exception("empty response")+ res_json = json.loads(res)+ if "ResponseMetadata" in res_json and "Error" in res_json["ResponseMetadata"]:+ raise Exception(f"KMS Error: {res_json['ResponseMetadata']['Error']}")+ if "Result" in res_json and "Plaintext" in res_json["Result"]:+ return base64.b64decode(res_json["Result"]["Plaintext"])+ raise Exception(f"Unexpected response: {res_json}")++ self._kms_client = KmsService(+ self.region, self.access_key_id, self.secret_access_key, self.endpoint+ )+ return self._kms_client++ async def get_root_key(self) -> bytes:+ """+ Get root key (only used for deriving Account Key).++ Note: In Volcengine KMS, the root key is managed by KMS service.+ Here we return the base key used for deriving Account Key.++ Returns:+ Base key+ """+ return b"OpenViking_Volcengine_KMS_Root_Seed"++ async def derive_account_key(self, account_id: str) -> bytes:+ """+ Derive Account Key using HKDF.++ Args:+ account_id: Account ID++ Returns:+ Derived Account Key+ """+ root_key = await self.get_root_key()+ return await self._hkdf_derive(root_key, account_id)++ async def _hkdf_derive(self, root_key: bytes, account_id: str) -> bytes:+ """+ Derive key using HKDF.++ Args:+ root_key: Root key+ account_id: Account ID++ Returns:+ Derived key+ """+ try:+ from cryptography.hazmat.primitives import hashes+ from cryptography.hazmat.primitives.kdf.hkdf import HKDF++ hkdf = HKDF(+ algorithm=hashes.SHA256(),+ length=32,+ salt=self.SALT,+ info=self.INFO_PREFIX + account_id.encode(),+ )+ return hkdf.derive(root_key)+ except ImportError:+ raise ConfigError("cryptography library is required for encryption")++ async def encrypt_file_key(self, plaintext_key: bytes, account_id: str) -> bytes:+ """+ Encrypt File Key using Volcengine KMS.++ Args:+ plaintext_key: Plaintext File Key+ account_id: Account ID (not used in Volcengine KMS mode)++ Returns:+ Encrypted File Key+ """+ client = await self._get_kms_client()+ return client.encrypt(self.key_id, plaintext_key)++ async def decrypt_file_key(self, encrypted_key: bytes, account_id: str) -> bytes:+ """+ Decrypt File Key using Volcengine KMS.++ Args:+ encrypted_key: Encrypted File Key+ account_id: Account ID (not used in Volcengine KMS mode)++ Returns:+ Decrypted File Key+ """+ client = await self._get_kms_client()+ return client.decrypt(encrypted_key, self.key_id)+++def create_root_key_provider(+ provider_type: str,+ config: Dict[str, Any],+) -> RootKeyProvider:+ """+ Create RootKeyProvider instance.++ Args:+ provider_type: Provider type ("local", "vault", "volcengine_kms")+ config: Configuration dictionary++ Returns:+ RootKeyProvider instance+ """+ if provider_type == "local":+ local_config = config.get("local", {})+ key_file_path = local_config.get("key_file", "~/.openviking/master.key")++ if not key_file_path:+ raise ConfigError("encryption.local.key_file is required")+ return LocalFileProvider(key_file_path)++ elif provider_type == "vault":+ vault_config = config.get("vault", {})+ address = vault_config.get("address")+ token = vault_config.get("token")+ mount_point = vault_config.get("mount_point", "transit")++ if not address or not token:+ raise ConfigError("vault.address and vault.token are required")+ return VaultProvider(address, token, mount_point)++ elif provider_type == "volcengine_kms":+ volc_config = config.get("volcengine_kms", {})+ region = volc_config.get("region")+ access_key = volc_config.get("access_key")+ secret_key = volc_config.get("secret_key")+ key_id = volc_config.get("key_id")++ if not all([region, access_key, secret_key, key_id]):+ raise ConfigError("volcengine_kms region, access_key, secret_key, key_id are required")+ return VolcengineKMSProvider(region, access_key, secret_key, key_id)++ else:+ raise ConfigError(f"Unsupported provider type: {provider_type}")diff --git a/openviking/server/api_keys.py b/openviking/server/api_keys.pyindex 8ee62e758..98b361d4a 100644--- a/openviking/server/api_keys.py+++ b/openviking/server/api_keys.py@@ -9,8 +9,11 @@ from dataclasses import dataclass, fieldfrom datetime import datetime, timezonefrom typing import Dict, Optional-from openviking.pyagfs import AGFSClient+from argon2 import PasswordHasher+from argon2.exceptions import VerifyMismatchError+from openviking.server.identity import ResolvedIdentity, Role+from openviking.storage.viking_fs import VikingFSfrom openviking_cli.exceptions import (AlreadyExistsError,NotFoundError,@@ -24,6 +27,13 @@ ACCOUNTS_PATH = "/local/_system/accounts.json"USERS_PATH_TEMPLATE = "/local/{account_id}/_system/users.json"+# Argon2id parameters+ARGON2_TIME_COST = 3+ARGON2_MEMORY_COST = 65536+ARGON2_PARALLELISM = 2+ARGON2_HASH_LENGTH = 32++@dataclassclass UserKeyEntry:"""In-memory index entry for a user key."""@@ -31,6 +41,8 @@ class UserKeyEntry:account_id: struser_id: strrole: Role+ key_or_hash: str+ is_hashed: bool@dataclass@@ -48,33 +60,37 @@ class APIKeyManager:- /_system/accounts.json: global workspace list- /{account_id}/_system/users.json: per-account user registry- In-memory index for O(1) key lookup at runtime.+ In-memory index for fast key lookup.+ Uses Argon2id for secure API key hashing."""- def __init__(self, root_key: str, agfs_client: AGFSClient):+ def __init__(self, root_key: str, viking_fs: VikingFS, encryption_enabled: bool = False):"""Initialize APIKeyManager.Args:root_key: Global root API key for administrative access.- agfs_client: AGFS client for persistent storage of user keys.+ viking_fs: VikingFS client for persistent storage of user keys.+ encryption_enabled: Whether API key hashing is enabled."""self._root_key = root_key- self._agfs = agfs_client+ self._viking_fs = viking_fs+ self._encryption_enabled = encryption_enabledself._accounts: Dict[str, AccountInfo] = {}- self._user_keys: Dict[str, UserKeyEntry] = {}+ # Prefix index: key_prefix -> list[UserKeyEntry]+ self._prefix_index: Dict[str, list[UserKeyEntry]] = {}async def load(self) -> None:- """Load accounts and user keys from AGFS into memory."""- accounts_data = self._read_json(ACCOUNTS_PATH)+ """Load accounts and user keys from VikingFS into memory."""+ accounts_data = await self._read_json(ACCOUNTS_PATH)if accounts_data is None:# First run: create default accountnow = datetime.now(timezone.utc).isoformat()accounts_data = {"accounts": {"default": {"created_at": now}}}- self._write_json(ACCOUNTS_PATH, accounts_data)+ await self._write_json(ACCOUNTS_PATH, accounts_data)for account_id, info in accounts_data.get("accounts", {}).items():users_path = USERS_PATH_TEMPLATE.format(account_id=account_id)- users_data = self._read_json(users_path)+ users_data = await self._read_json(users_path)users = users_data.get("users", {}) if users_data else {}self._accounts[account_id] = AccountInfo(@@ -83,18 +99,53 @@ class APIKeyManager:)for user_id, user_info in users.items():- key = user_info.get("key", "")- if key:- self._user_keys[key] = UserKeyEntry(+ key_or_hash = user_info.get("key", "")+ if key_or_hash:+ # Check if it's a hashed key+ if key_or_hash.startswith("$argon2"):+ # Already hashed+ stored_key = key_or_hash+ is_hashed = True+ key_prefix = user_info.get("key_prefix", "")+ else:+ # Plaintext key+ if self._encryption_enabled:+ # If encryption enabled, migrate to hashed+ stored_key = self._hash_api_key(key_or_hash)+ is_hashed = True+ key_prefix = self._get_key_prefix(key_or_hash)+ # Update storage+ user_info["key"] = stored_key+ user_info["key_prefix"] = key_prefix+ await self._save_users_json(account_id)+ logger.info(+ "Migrated API key for user %s in account %s", user_id, account_id+ )+ else:+ # If encryption not enabled, keep as plaintext+ stored_key = key_or_hash+ is_hashed = False+ # For plaintext keys, compute prefix on the fly for indexing+ key_prefix = self._get_key_prefix(key_or_hash)++ entry = UserKeyEntry(account_id=account_id,user_id=user_id,role=Role(user_info.get("role", "user")),+ key_or_hash=stored_key,+ is_hashed=is_hashed,)+ # Add to prefix index+ if key_prefix:+ if key_prefix not in self._prefix_index:+ self._prefix_index[key_prefix] = []+ self._prefix_index[key_prefix].append(entry)+logger.info("APIKeyManager loaded: %d accounts, %d user keys",len(self._accounts),- len(self._user_keys),+ sum(len(info.users) for info in self._accounts.values()),)def resolve(self, api_key: str) -> ResolvedIdentity:@@ -105,13 +156,27 @@ class APIKeyManager:if hmac.compare_digest(api_key, self._root_key):return ResolvedIdentity(role=Role.ROOT)- entry = self._user_keys.get(api_key)- if entry:- return ResolvedIdentity(- role=entry.role,- account_id=entry.account_id,- user_id=entry.user_id,- )+ # Use prefix index to quickly locate candidate keys+ key_prefix = self._get_key_prefix(api_key)+ candidates = self._prefix_index.get(key_prefix, [])++ for entry in candidates:+ if entry.is_hashed:+ # Verify hashed key+ if self._verify_api_key(api_key, entry.key_or_hash):+ return ResolvedIdentity(+ role=entry.role,+ account_id=entry.account_id,+ user_id=entry.user_id,+ )+ else:+ # Verify plaintext key+ if hmac.compare_digest(api_key, entry.key_or_hash):+ return ResolvedIdentity(+ role=entry.role,+ account_id=entry.account_id,+ user_id=entry.user_id,+ )raise UnauthenticatedError("Invalid API Key")@@ -124,20 +189,45 @@ class APIKeyManager:raise AlreadyExistsError(account_id, "account")now = datetime.now(timezone.utc).isoformat()- key = secrets.token_hex(32)+ key = self._generate_api_key()++ if self._encryption_enabled:+ stored_key = self._hash_api_key(key)+ is_hashed = True+ key_prefix = self._get_key_prefix(key)+ else:+ stored_key = key+ is_hashed = False+ key_prefix = self._get_key_prefix(key)++ user_info = {+ "role": "admin",+ "key": stored_key,+ }+ if self._encryption_enabled:+ user_info["key_prefix"] = key_prefixself._accounts[account_id] = AccountInfo(created_at=now,- users={admin_user_id: {"role": "admin", "key": key}},+ users={admin_user_id: user_info},)- self._user_keys[key] = UserKeyEntry(++ entry = UserKeyEntry(account_id=account_id,user_id=admin_user_id,role=Role.ADMIN,+ key_or_hash=stored_key,+ is_hashed=is_hashed,)- self._save_accounts_json()- self._save_users_json(account_id)+ # Add to prefix index+ if key_prefix:+ if key_prefix not in self._prefix_index:+ self._prefix_index[key_prefix] = []+ self._prefix_index[key_prefix].append(entry)++ await self._save_accounts_json()+ await self._save_users_json(account_id)return keyasync def delete_account(self, account_id: str) -> None:@@ -149,11 +239,26 @@ class APIKeyManager:raise NotFoundError(account_id, "account")account = self._accounts.pop(account_id)+ # Remove all keys for this account from prefix indexfor user_info in account.users.values():- key = user_info.get("key", "")- self._user_keys.pop(key, None)-- self._save_accounts_json()+ key_or_hash = user_info.get("key", "")+ if key_or_hash:+ # Get key_prefix - if not in user_info, compute from key+ key_prefix = user_info.get("key_prefix", "")+ if not key_prefix:+ key_prefix = self._get_key_prefix(key_or_hash)++ if key_prefix in self._prefix_index:+ self._prefix_index[key_prefix] = [+ entry+ for entry in self._prefix_index[key_prefix]+ if entry.account_id != account_id+ ]+ # Remove prefix if index is empty+ if not self._prefix_index[key_prefix]:+ del self._prefix_index[key_prefix]++ await self._save_accounts_json()async def register_user(self, account_id: str, user_id: str, role: str = "user") -> str:"""Register a new user in an account. Returns the user's API key."""@@ -163,15 +268,41 @@ class APIKeyManager:if user_id in account.users:raise AlreadyExistsError(user_id, "user")- key = secrets.token_hex(32)- account.users[user_id] = {"role": role, "key": key}- self._user_keys[key] = UserKeyEntry(+ key = self._generate_api_key()++ if self._encryption_enabled:+ stored_key = self._hash_api_key(key)+ is_hashed = True+ key_prefix = self._get_key_prefix(key)+ else:+ stored_key = key+ is_hashed = False+ key_prefix = self._get_key_prefix(key)++ user_info = {+ "role": role,+ "key": stored_key,+ }+ if self._encryption_enabled:+ user_info["key_prefix"] = key_prefix++ account.users[user_id] = user_info++ entry = UserKeyEntry(account_id=account_id,user_id=user_id,role=Role(role),+ key_or_hash=stored_key,+ is_hashed=is_hashed,)- self._save_users_json(account_id)+ # Add to prefix index+ if key_prefix:+ if key_prefix not in self._prefix_index:+ self._prefix_index[key_prefix] = []+ self._prefix_index[key_prefix].append(entry)++ await self._save_users_json(account_id)return keyasync def remove_user(self, account_id: str, user_id: str) -> None:@@ -183,10 +314,26 @@ class APIKeyManager:raise NotFoundError(user_id, "user")user_info = account.users.pop(user_id)- key = user_info.get("key", "")- self._user_keys.pop(key, None)-- self._save_users_json(account_id)+ key_or_hash = user_info.get("key", "")++ if key_or_hash:+ # Get key_prefix - if not in user_info, compute from key+ key_prefix = user_info.get("key_prefix", "")+ if not key_prefix:+ key_prefix = self._get_key_prefix(key_or_hash)++ # Remove from prefix index+ if key_prefix in self._prefix_index:+ self._prefix_index[key_prefix] = [+ entry+ for entry in self._prefix_index[key_prefix]+ if not (entry.account_id == account_id and entry.user_id == user_id)+ ]+ # Remove prefix if index is empty+ if not self._prefix_index[key_prefix]:+ del self._prefix_index[key_prefix]++ await self._save_users_json(account_id)async def regenerate_key(self, account_id: str, user_id: str) -> str:"""Regenerate a user's API key. Old key is immediately invalidated."""@@ -196,18 +343,60 @@ class APIKeyManager:if user_id not in account.users:raise NotFoundError(user_id, "user")- old_key = account.users[user_id].get("key", "")- self._user_keys.pop(old_key, None)-- new_key = secrets.token_hex(32)- account.users[user_id]["key"] = new_key- self._user_keys[new_key] = UserKeyEntry(+ old_user_info = account.users[user_id]+ old_key_or_hash = old_user_info.get("key", "")++ # Get old key_prefix - if not in user_info, compute from key+ old_key_prefix = old_user_info.get("key_prefix", "")+ if not old_key_prefix and old_key_or_hash:+ old_key_prefix = self._get_key_prefix(old_key_or_hash)++ # Remove old key from prefix index+ if old_key_prefix in self._prefix_index:+ self._prefix_index[old_key_prefix] = [+ entry+ for entry in self._prefix_index[old_key_prefix]+ if not (entry.account_id == account_id and entry.user_id == user_id)+ ]+ if not self._prefix_index[old_key_prefix]:+ del self._prefix_index[old_key_prefix]++ # Generate new key+ new_key = self._generate_api_key()++ if self._encryption_enabled:+ new_stored_key = self._hash_api_key(new_key)+ new_is_hashed = True+ new_key_prefix = self._get_key_prefix(new_key)+ else:+ new_stored_key = new_key+ new_is_hashed = False+ new_key_prefix = self._get_key_prefix(new_key)++ # Update user info+ account.users[user_id]["key"] = new_stored_key+ if self._encryption_enabled:+ account.users[user_id]["key_prefix"] = new_key_prefix+ else:+ # Remove key_prefix if encryption is disabled+ if "key_prefix" in account.users[user_id]:+ del account.users[user_id]["key_prefix"]++ # Add new key to prefix index+ entry = UserKeyEntry(account_id=account_id,user_id=user_id,role=Role(account.users[user_id]["role"]),+ key_or_hash=new_stored_key,+ is_hashed=new_is_hashed,)- self._save_users_json(account_id)+ if new_key_prefix:+ if new_key_prefix not in self._prefix_index:+ self._prefix_index[new_key_prefix] = []+ self._prefix_index[new_key_prefix].append(entry)++ await self._save_users_json(account_id)return new_keyasync def set_role(self, account_id: str, user_id: str, role: str) -> None:@@ -220,15 +409,22 @@ class APIKeyManager:account.users[user_id]["role"] = role- key = account.users[user_id].get("key", "")- if key in self._user_keys:- self._user_keys[key] = UserKeyEntry(- account_id=account_id,- user_id=user_id,- role=Role(role),- )+ # Update role in prefix index+ user_info = account.users[user_id]+ key_or_hash = user_info.get("key", "")+ if key_or_hash:+ # Get key_prefix - if not in user_info, compute from key+ key_prefix = user_info.get("key_prefix", "")+ if not key_prefix:+ key_prefix = self._get_key_prefix(key_or_hash)- self._save_users_json(account_id)+ if key_prefix in self._prefix_index:+ for entry in self._prefix_index[key_prefix]:+ if entry.account_id == account_id and entry.user_id == user_id:+ entry.role = Role(role)+ break++ await self._save_users_json(account_id)def get_accounts(self) -> list:"""List all accounts."""@@ -261,48 +457,105 @@ class APIKeyManager:# ---- internal helpers ----- def _read_json(self, path: str) -> Optional[dict]:- """Read a JSON file from AGFS. Returns None if not found."""+ def _generate_api_key(self) -> str:+ """Generate new API Key."""+ return secrets.token_hex(32)++ def _get_key_prefix(self, api_key: str) -> str:+ """Extract API Key prefix for indexing."""+ if api_key:+ # Take first 8 characters for indexing+ return api_key[:8]+ return ""++ def _hash_api_key(self, api_key: str) -> str:+ """Hash API Key using Argon2id."""+ ph = PasswordHasher(+ time_cost=ARGON2_TIME_COST,+ memory_cost=ARGON2_MEMORY_COST,+ parallelism=ARGON2_PARALLELISM,+ hash_len=ARGON2_HASH_LENGTH,+ )+ return ph.hash(api_key)++ def _verify_api_key(self, api_key: str, hashed_key: str) -> bool:+ """Verify if API Key matches the hash."""+ ph = PasswordHasher()try:- content = self._agfs.read(path)+ ph.verify(hashed_key, api_key)+ return True+ except VerifyMismatchError:+ return False++ async def _read_json(self, path: str) -> Optional[dict]:+ """Read a JSON file from AGFS with encryption support. Returns None if not found."""+ try:+ # Read file directly using AGFS+ content = self._viking_fs.agfs.read(path)if isinstance(content, bytes):- content = content.decode("utf-8")- return json.loads(content)+ raw = content+ else:+ raw = content.content if hasattr(content, "content") else b""++ # Decrypt content if encryption is enabled+ if self._viking_fs._encryptor:+ # Extract account ID from path+ parts = path.split("/")+ account_id = parts[2] if len(parts) >= 3 else "default"+ raw = await self._viking_fs._encryptor.decrypt(account_id, raw)++ text = raw.decode("utf-8") if isinstance(raw, bytes) else raw+ return json.loads(text)except Exception:return None- def _write_json(self, path: str, data: dict) -> None:- """Write a JSON file to AGFS, creating parent directories as needed."""+ async def _write_json(self, path: str, data: dict) -> None:+ """Write a JSON file to AGFS with encryption support."""content = json.dumps(data, ensure_ascii=False, indent=2)if isinstance(content, str):content = content.encode("utf-8")++ # Encrypt content if encryption is enabled+ if self._viking_fs._encryptor:+ # Extract account ID from path+ parts = path.split("/")+ account_id = parts[2] if len(parts) >= 3 else "default"+ content = await self._viking_fs._encryptor.encrypt(account_id, content)++ # Ensure parent directories existself._ensure_parent_dirs(path)- self._agfs.write(path, content)+ # Write file directly using AGFS+ self._viking_fs.agfs.write(path, content)def _ensure_parent_dirs(self, path: str) -> None:"""Recursively create all parent directories for a file path."""- parts = path.lstrip("/").split("/")- for i in range(1, len(parts)):- parent = "/" + "/".join(parts[:i])- try:- self._agfs.mkdir(parent)- except Exception:- pass-- def _save_accounts_json(self) -> None:+ # Handle direct AGFS paths+ if path.startswith("/local/"):+ # Extract path parts from /local/{account_id}/_system/...+ parts = path.lstrip("/").split("/")+ if parts:+ # Create directory for each parent path+ for i in range(1, len(parts)):+ parent = "/" + "/".join(parts[:i])+ try:+ self._viking_fs.agfs.mkdir(parent)+ except Exception:+ pass++ async def _save_accounts_json(self) -> None:"""Persist the global accounts list."""data = {"accounts": {aid: {"created_at": info.created_at} for aid, info in self._accounts.items()}}- self._write_json(ACCOUNTS_PATH, data)+ await self._write_json(ACCOUNTS_PATH, data)- def _save_users_json(self, account_id: str) -> None:+ async def _save_users_json(self, account_id: str) -> None:"""Persist a single account's user registry."""account = self._accounts.get(account_id)if account is None:returndata = {"users": account.users}path = USERS_PATH_TEMPLATE.format(account_id=account_id)- self._write_json(path, data)+ await self._write_json(path, data)diff --git a/openviking/server/app.py b/openviking/server/app.pyindex c553c6fff..d688bd0f7 100644--- a/openviking/server/app.py+++ b/openviking/server/app.py@@ -67,15 +67,18 @@ def create_app(set_service(service)- # Initialize APIKeyManager after service (needs AGFS)+ # Initialize APIKeyManager after service (needs VikingFS)if config.root_api_key:api_key_manager = APIKeyManager(root_key=config.root_api_key,- agfs_client=service._agfs,+ viking_fs=service.viking_fs,+ encryption_enabled=config.encryption_enabled,)await api_key_manager.load()app.state.api_key_manager = api_key_manager- logger.info("APIKeyManager initialized")+ logger.info(+ "APIKeyManager initialized with encryption_enabled=%s", config.encryption_enabled+ )else:app.state.api_key_manager = Nonelogger.warning(diff --git a/openviking/server/config.py b/openviking/server/config.pyindex 054c99603..7d8e8aafc 100644--- a/openviking/server/config.py+++ b/openviking/server/config.py@@ -32,6 +32,7 @@ class ServerConfig:cors_origins: List[str] = field(default_factory=lambda: ["*"])with_bot: bool = False # Enable Bot API proxy to Vikingbotbot_api_url: str = "http://localhost:18790" # Vikingbot OpenAPIChannel URL (default port)+ encryption_enabled: bool = False # Whether API key hashing is enableddef load_server_config(config_path: Optional[str] = None) -> ServerConfig:@@ -68,12 +69,16 @@ def load_server_config(config_path: Optional[str] = None) -> ServerConfig:data = load_json_config(path)server_data = data.get("server", {})+ # Get encryption enabled from config data directly (for test compatibility)+ encryption_enabled = data.get("encryption", {}).get("enabled", False)+config = ServerConfig(host=server_data.get("host", "127.0.0.1"),port=server_data.get("port", 1933),workers=server_data.get("workers", 1),root_api_key=server_data.get("root_api_key"),cors_origins=server_data.get("cors_origins", ["*"]),+ encryption_enabled=encryption_enabled,)return configdiff --git a/openviking/service/core.py b/openviking/service/core.pyindex 47bf3cb8c..16e1db153 100644--- a/openviking/service/core.py+++ b/openviking/service/core.py@@ -11,6 +11,7 @@ from typing import Any, Optionalfrom openviking.agfs_manager import AGFSManagerfrom openviking.core.directories import DirectoryInitializer+from openviking.crypto.config import bootstrap_encryptionfrom openviking.resource.watch_scheduler import WatchSchedulerfrom openviking.server.identity import RequestContext, Rolefrom openviking.service.debug_service import DebugService@@ -79,6 +80,7 @@ class OpenVikingService:self._lock_manager: Optional[LockManager] = Noneself._directory_initializer: Optional[DirectoryInitializer] = Noneself._watch_scheduler: Optional[WatchScheduler] = None+ self._encryptor: Optional[Any] = None# Sub-servicesself._fs_service = FSService()@@ -257,6 +259,14 @@ class OpenVikingService:config = get_openviking_config()+ # Initialize encryption module+ full_config = config.to_dict()+ self._encryptor = await bootstrap_encryption(full_config)+ if self._encryptor:+ logger.info("Encryption module initialized")+ else:+ logger.info("Encryption module not enabled")+# Initialize VikingFS and VikingDB with recorder if enabledenable_recorder = os.environ.get("OPENVIKING_ENABLE_RECORDER", "").lower() == "true"@@ -276,6 +286,7 @@ class OpenVikingService:rerank_config=config.rerank,vector_store=self._vikingdb_manager,enable_recorder=enable_recorder,+ encryptor=self._encryptor,)if enable_recorder:logger.info("VikingFS IO Recorder enabled")diff --git a/openviking/storage/viking_fs.py b/openviking/storage/viking_fs.pyindex c25c251b1..100adaf71 100644--- a/openviking/storage/viking_fs.py+++ b/openviking/storage/viking_fs.py@@ -16,6 +16,7 @@ import asyncioimport contextvarsimport hashlibimport json+import refrom contextlib import contextmanagerfrom dataclasses import dataclass, fieldfrom datetime import datetime@@ -74,6 +75,7 @@ def init_viking_fs(vector_store: Optional["VikingVectorIndexBackend"] = None,timeout: int = 10,enable_recorder: bool = False,+ encryptor: Optional[Any] = None,) -> "VikingFS":"""Initialize VikingFS singleton.@@ -84,6 +86,7 @@ def init_viking_fs(rerank_config: Rerank configurationvector_store: Vector store instanceenable_recorder: Whether to enable IO recording+ encryptor: FileEncryptor instance for encryption/decryption"""global _instance@@ -92,6 +95,7 @@ def init_viking_fs(query_embedder=query_embedder,rerank_config=rerank_config,vector_store=vector_store,+ encryptor=encryptor,)if enable_recorder:@@ -165,11 +169,13 @@ class VikingFS:rerank_config: Optional["RerankConfig"] = None,vector_store: Optional["VikingVectorIndexBackend"] = None,timeout: int = 10,+ encryptor: Optional[Any] = None,):self.agfs = agfsself.query_embedder = query_embedderself.rerank_config = rerank_configself.vector_store = vector_store+ self._encryptor = encryptorself._bound_ctx: contextvars.ContextVar[Optional[RequestContext]] = contextvars.ContextVar("vikingfs_bound_ctx", default=None)@@ -184,6 +190,20 @@ class VikingFS:bound = self._bound_ctx.get()return bound or self._default_ctx()+ async def _encrypt_content(self, content: bytes, ctx: Optional[RequestContext] = None) -> bytes:+ """Encrypt content if encryption is enabled."""+ if not self._encryptor:+ return content+ real_ctx = self._ctx_or_default(ctx)+ return await self._encryptor.encrypt(real_ctx.account_id, content)++ async def _decrypt_content(self, content: bytes, ctx: Optional[RequestContext] = None) -> bytes:+ """Decrypt content if encryption is enabled."""+ if not self._encryptor:+ return content+ real_ctx = self._ctx_or_default(ctx)+ return await self._encryptor.decrypt(real_ctx.account_id, content)+@contextmanagerdef bind_request_context(self, ctx: RequestContext):"""Temporarily bind ctx for legacy internal call paths without explicit ctx param."""@@ -240,11 +260,17 @@ class VikingFS:path = self._uri_to_path(uri, ctx=ctx)result = self.agfs.read(path, offset, size)if isinstance(result, bytes):- return result+ raw = resultelif result is not None and hasattr(result, "content"):- return result.content+ raw = result.contentelse:- return b""+ raw = b""++ # If encryption is enabled and not reading from offset, try to decrypt+ if self._encryptor and offset == 0 and size == -1:+ raw = await self._decrypt_content(raw, ctx=ctx)++ return rawasync def write(self,@@ -257,6 +283,8 @@ class VikingFS:path = self._uri_to_path(uri, ctx=ctx)if isinstance(data, str):data = data.encode("utf-8")++ data = await self._encrypt_content(data, ctx=ctx)return self.agfs.write(path, data)async def mkdir(@@ -369,9 +397,18 @@ class VikingFS:uris_to_move = await self._collect_uris(old_path, recursive=True, ctx=ctx)uris_to_move.append(target_uri)+ # Check if it's temp directory (files already encrypted)+ is_temp = old_uri.startswith("viking://temp/")+# Copy source to destination (source still intact)try:- agfs_cp(self.agfs, old_path, new_path, recursive=is_dir)+ if is_temp or not self._encryptor:+ agfs_cp(self.agfs, old_path, new_path, recursive=is_dir)+ else:+ if is_dir:+ await self._recursive_copy_dir_with_encryption(old_uri, new_uri, ctx=ctx)+ else:+ await self.move_file(old_uri, new_uri, ctx=ctx)except Exception as e:if "not found" in str(e).lower():await self._delete_from_vector_store(uris_to_move, ctx=ctx)@@ -379,7 +416,7 @@ class VikingFS:raise# Remove carried lock file from the copy (directory only)- if is_dir:+ if is_dir and (is_temp or not self._encryptor):carried_lock = new_path.rstrip("/") + "/.path.ovlock"try:self.agfs.rm(carried_lock)@@ -391,7 +428,10 @@ class VikingFS:await self._update_vector_store_uris(uris_to_move, old_uri, new_uri, ctx=ctx)except Exception:try:- self.agfs.rm(new_path, recursive=is_dir)+ if is_dir:+ self.agfs.rm(new_path, recursive=True)+ else:+ self.agfs.rm(new_path)except Exception:passraise@@ -400,6 +440,38 @@ class VikingFS:self.agfs.rm(old_path, recursive=is_dir)return {}+ async def _recursive_copy_dir_with_encryption(+ self,+ old_uri: str,+ new_uri: str,+ ctx: Optional[RequestContext] = None,+ ) -> None:+ """Recursively copy a directory, ensuring files are encrypted."""+ await self.mkdir(new_uri, exist_ok=True, ctx=ctx)++ max_iterations = 10+ iteration = 0++ while iteration < max_iterations:+ entries = await self.ls(old_uri, ctx=ctx)+ if not entries:+ break++ for entry in entries:+ name = entry.get("name", "")+ if not name or name in (".", ".."):+ continue+ old_child_uri = f"{old_uri.rstrip('/')}/{name}"+ new_child_uri = f"{new_uri.rstrip('/')}/{name}"+ if entry.get("isDir"):+ await self._recursive_copy_dir_with_encryption(+ old_child_uri, new_child_uri, ctx=ctx+ )+ else:+ await self.move_file(old_child_uri, new_child_uri, ctx=ctx)++ iteration += 1+async def grep(self,uri: str,@@ -408,24 +480,58 @@ class VikingFS:node_limit: Optional[int] = None,ctx: Optional[RequestContext] = None,) -> Dict:- """Content search by pattern or keywords."""+ """Content search by pattern or keywords.++ Grep search implemented at VikingFS layer, supports encrypted files.+ """self._ensure_access(uri, ctx)- path = self._uri_to_path(uri, ctx=ctx)- result = await asyncio.to_thread(- self.agfs.grep, path, pattern, True, case_insensitive, False, node_limit=node_limit- )- if result.get("matches", None) is None:- result["matches"] = []- new_matches = []- for match in result.get("matches", []):- new_match = {- "line": match.get("line"),- "uri": self._path_to_uri(match.get("file"), ctx=ctx),- "content": match.get("content"),- }- new_matches.append(new_match)- result["matches"] = new_matches- return result++ flags = re.IGNORECASE if case_insensitive else 0+ compiled_pattern = re.compile(pattern, flags)++ results = []++ async def search_recursive(current_uri: str):+ if node_limit and len(results) >= node_limit:+ return++ try:+ entries = await self.ls(current_uri, ctx=ctx)+ except Exception:+ return++ for entry in entries:+ if node_limit and len(results) >= node_limit:+ break++ entry_uri = f"{current_uri.rstrip('/')}/{entry['name']}"++ if entry.get("isDir"):+ await search_recursive(entry_uri)+ else:+ try:+ content = await self.read(entry_uri, ctx=ctx)+ if isinstance(content, bytes):+ content = content.decode("utf-8", errors="replace")++ lines = content.split("\n")+ for line_num, line in enumerate(lines, 1):+ if compiled_pattern.search(line):+ results.append(+ {+ "line": line_num,+ "uri": entry_uri,+ "content": line,+ }+ )+ if node_limit and len(results) >= node_limit:+ break+ except Exception as e:+ logger.debug(f"Failed to grep {entry_uri}: {e}")++ await search_recursive(uri)++ return {"matches": results}async def stat(self, uri: str, ctx: Optional[RequestContext] = None) -> Dict[str, Any]:"""@@ -642,8 +748,13 @@ class VikingFS:if not info.get("isDir"):raise ValueError(f"{uri} is not a directory")file_path = f"{path}/.abstract.md"- content = self.agfs.read(file_path)- return self._handle_agfs_content(content)+ content_bytes = self._handle_agfs_read(self.agfs.read(file_path))++ if self._encryptor:+ real_ctx = self._ctx_or_default(ctx)+ content_bytes = await self._encryptor.decrypt(real_ctx.account_id, content_bytes)++ return self._decode_bytes(content_bytes)async def overview(self,@@ -657,8 +768,13 @@ class VikingFS:if not info.get("isDir"):raise ValueError(f"{uri} is not a directory")file_path = f"{path}/.overview.md"- content = self.agfs.read(file_path)- return self._handle_agfs_content(content)+ content_bytes = self._handle_agfs_read(self.agfs.read(file_path))++ if self._encryptor:+ real_ctx = self._ctx_or_default(ctx)+ content_bytes = await self._encryptor.decrypt(real_ctx.account_id, content_bytes)++ return self._decode_bytes(content_bytes)async def relations(self,@@ -923,14 +1039,14 @@ class VikingFS:from_path = self._uri_to_path(from_uri, ctx=ctx)- entries = await self._read_relation_table(from_path)+ entries = await self._read_relation_table(from_path, ctx=ctx)existing_ids = {e.id for e in entries}link_id = next(f"link_{i}" for i in range(1, 10000) if f"link_{i}" not in existing_ids)entries.append(RelationEntry(id=link_id, uris=uris, reason=reason))- await self._write_relation_table(from_path, entries)+ await self._write_relation_table(from_path, entries, ctx=ctx)logger.debug(f"[VikingFS] Created link: {from_uri} -> {uris}")async def unlink(@@ -945,7 +1061,7 @@ class VikingFS:from_path = self._uri_to_path(from_uri, ctx=ctx)try:- entries = await self._read_relation_table(from_path)+ entries = await self._read_relation_table(from_path, ctx=ctx)entry_to_modify = Nonefor entry in entries:@@ -963,7 +1079,7 @@ class VikingFS:entries.remove(entry_to_modify)logger.debug(f"[VikingFS] Removed empty entry: {entry_to_modify.id}")- await self._write_relation_table(from_path, entries)+ await self._write_relation_table(from_path, entries, ctx=ctx)logger.debug(f"[VikingFS] Removed link: {from_uri} -> {uri}")except Exception as e:@@ -976,7 +1092,7 @@ class VikingFS:"""Get relation table."""self._ensure_access(uri, ctx)path = self._uri_to_path(uri, ctx=ctx)- return await self._read_relation_table(path)+ return await self._read_relation_table(path, ctx=ctx)# ========== URI Conversion ==========@@ -1326,11 +1442,14 @@ class VikingFS:# ========== Relation Table Internal Methods ==========- async def _read_relation_table(self, dir_path: str) -> List[RelationEntry]:+ async def _read_relation_table(+ self, dir_path: str, ctx: Optional[RequestContext] = None+ ) -> List[RelationEntry]:"""Read .relations.json."""table_path = f"{dir_path}/.relations.json"try:content = self._handle_agfs_read(self.agfs.read(table_path))+ content = await self._decrypt_content(content, ctx=ctx)data = json.loads(content.decode("utf-8"))except FileNotFoundError:return []@@ -1352,7 +1471,9 @@ class VikingFS:entries.append(RelationEntry.from_dict(entry_data))return entries- async def _write_relation_table(self, dir_path: str, entries: List[RelationEntry]) -> None:+ async def _write_relation_table(+ self, dir_path: str, entries: List[RelationEntry], ctx: Optional[RequestContext] = None+ ) -> None:"""Write .relations.json."""# Use flat list formatdata = [entry.to_dict() for entry in entries]@@ -1361,6 +1482,8 @@ class VikingFS:table_path = f"{dir_path}/.relations.json"if isinstance(content, str):content = content.encode("utf-8")++ content = await self._encrypt_content(content, ctx=ctx)self.agfs.write(table_path, content)# ========== Batch Read (backward compatible) ==========@@ -1397,6 +1520,8 @@ class VikingFS:if isinstance(content, str):content = content.encode("utf-8")++ content = await self._encrypt_content(content, ctx=ctx)self.agfs.write(path, content)async def read_file(@@ -1426,9 +1551,21 @@ class VikingFS:raise NotFoundError(uri, "file")try:content = self.agfs.read(path)+ if isinstance(content, bytes):+ raw = content+ elif content is not None and hasattr(content, "content"):+ raw = content.content+ else:+ raw = b""++ # If encryption is enabled and not reading from offset, try to decrypt+ if self._encryptor and offset == 0 and limit == -1:+ raw = await self._decrypt_content(raw, ctx=ctx)++ text = self._decode_bytes(raw)except Exception:raise NotFoundError(uri, "file")- text = self._handle_agfs_content(content)+if offset == 0 and limit == -1:return textlines = text.splitlines(keepends=True)@@ -1444,7 +1581,9 @@ class VikingFS:self._ensure_access(uri, ctx)path = self._uri_to_path(uri, ctx=ctx)try:- return self._handle_agfs_read(self.agfs.read(path))+ raw = self._handle_agfs_read(self.agfs.read(path))+ raw = await self._decrypt_content(raw, ctx=ctx)+ return rawexcept Exception:raise NotFoundError(uri, "file")@@ -1458,6 +1597,8 @@ class VikingFS:self._ensure_access(uri, ctx)path = self._uri_to_path(uri, ctx=ctx)await self._ensure_parent_dirs(path)++ content = await self._encrypt_content(content, ctx=ctx)self.agfs.write(path, content)async def append_file(@@ -1474,12 +1615,15 @@ class VikingFS:existing = ""try:existing_bytes = self._handle_agfs_read(self.agfs.read(path))+ existing_bytes = await self._decrypt_content(existing_bytes, ctx=ctx)existing = self._decode_bytes(existing_bytes)except Exception:passawait self._ensure_parent_dirs(path)- self.agfs.write(path, (existing + content).encode("utf-8"))+ final_content = (existing + content).encode("utf-8")+ final_content = await self._encrypt_content(final_content, ctx=ctx)+ self.agfs.write(path, final_content)except Exception as e:logger.error(f"[VikingFS] Failed to append to file {uri}: {e}")@@ -1608,10 +1752,9 @@ class VikingFS:self._ensure_access(from_uri, ctx)self._ensure_access(to_uri, ctx)from_path = self._uri_to_path(from_uri, ctx=ctx)- to_path = self._uri_to_path(to_uri, ctx=ctx)- content = self.agfs.read(from_path)- await self._ensure_parent_dirs(to_path)- self.agfs.write(to_path, content)++ content_bytes = await self.read_file_bytes(from_uri, ctx=ctx)+ await self.write_file(to_uri, content_bytes, ctx=ctx)self.agfs.rm(from_path)# ========== Temp File Operations (backward compatible) ==========@@ -1700,18 +1843,16 @@ class VikingFS:raiseif content:- content_path = f"{path}/{content_filename}"- if isinstance(content, str):- content = content.encode("utf-8")- self.agfs.write(content_path, content)+ content_uri = f"{uri}/{content_filename}"+ await self.write_file(content_uri, content, ctx=ctx)if abstract:- abstract_path = f"{path}/.abstract.md"- self.agfs.write(abstract_path, abstract.encode("utf-8"))+ abstract_uri = f"{uri}/.abstract.md"+ await self.write_file(abstract_uri, abstract, ctx=ctx)if overview:- overview_path = f"{path}/.overview.md"- self.agfs.write(overview_path, overview.encode("utf-8"))+ overview_uri = f"{uri}/.overview.md"+ await self.write_file(overview_uri, overview, ctx=ctx)except Exception as e:logger.error(f"[VikingFS] Failed to write {uri}: {e}")diff --git a/openviking_cli/utils/config/encryption_config.py b/openviking_cli/utils/config/encryption_config.pynew file mode 100644index 000000000..fe52b2b47--- /dev/null+++ b/openviking_cli/utils/config/encryption_config.py@@ -0,0 +1,117 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+from typing import Any, Dict, Optional++from pydantic import BaseModel, Field+++class LocalEncryptionProviderConfig(BaseModel):+ """Local file encryption provider configuration.++ Uses a local file to store the Root Key.+ Suitable for single-user or development environments.+ """++ key_file: str = Field(+ default="~/.openviking/master.key", description="Path to local root key file"+ )+++class VaultEncryptionProviderConfig(BaseModel):+ """HashiCorp Vault encryption provider configuration.++ Uses HashiCorp Vault Transit Secrets Engine for key management.+ Suitable for enterprise environments requiring centralized key management.+ """++ address: Optional[str] = Field(default=None, description="HashiCorp Vault address")+ token: Optional[str] = Field(default=None, description="HashiCorp Vault token")+ mount_point: str = Field(+ default="transit", description="HashiCorp Vault transit secrets engine mount point"+ )+ key_name: str = Field(default="openviking-root", description="HashiCorp Vault key name")+++class VolcengineKMSEncryptionProviderConfig(BaseModel):+ """Volcengine KMS encryption provider configuration.++ Uses Volcengine Key Management Service for key management.+ Suitable for production environments on Volcengine.+ """++ key_id: Optional[str] = Field(default=None, description="Volcengine KMS key ID")+ region: str = Field(default="cn-beijing", description="Volcengine KMS region")+ access_key: Optional[str] = Field(default=None, description="Volcengine access key ID")+ secret_key: Optional[str] = Field(default=None, description="Volcengine secret access key")+++class EncryptionConfig(BaseModel):+ """Configuration for encryption module.++ Provides configuration for multi-tenant encryption functionality including:+ - Envelope encryption with AES-256-GCM+ - Multiple key providers (Local File, Vault, Volcengine KMS)+ - API Key hashing with Argon2id++ Example configurations:+ # Local file provider+ {+ "enabled": true,+ "provider": "local",+ "local": {+ "key_file": "~/.openviking/master.key"+ }+ }++ # Vault provider+ {+ "enabled": true,+ "provider": "vault",+ "vault": {+ "address": "https://vault.example.com:8200",+ "token": "vault-token-xxx",+ "mount_point": "transit",+ "key_name": "openviking-root"+ }+ }+ """++ enabled: bool = Field(default=False, description="Whether encryption is enabled")++ provider: str = Field(+ default="local",+ description="Key provider type: 'local', 'vault', 'volcengine_kms'",+ )++ local: LocalEncryptionProviderConfig = Field(+ default_factory=lambda: LocalEncryptionProviderConfig(),+ description="Local provider configuration",+ )++ vault: VaultEncryptionProviderConfig = Field(+ default_factory=lambda: VaultEncryptionProviderConfig(),+ description="Vault provider configuration",+ )++ volcengine_kms: VolcengineKMSEncryptionProviderConfig = Field(+ default_factory=lambda: VolcengineKMSEncryptionProviderConfig(),+ description="Volcengine KMS provider configuration",+ )++ params: Dict[str, Any] = Field(+ default_factory=dict, description="Additional encryption-specific parameters"+ )++ model_config = {"extra": "forbid"}++ @classmethod+ def from_dict(cls, data: Dict[str, Any]) -> "EncryptionConfig":+ """Create configuration from dictionary.++ Args:+ data: Configuration dictionary++ Returns:+ EncryptionConfig instance+ """+ return cls(**data)diff --git a/openviking_cli/utils/config/open_viking_config.py b/openviking_cli/utils/config/open_viking_config.pyindex 503389290..b93889f7b 100644--- a/openviking_cli/utils/config/open_viking_config.py+++ b/openviking_cli/utils/config/open_viking_config.py@@ -17,6 +17,7 @@ from .consts import (SYSTEM_CONFIG_DIR,)from .embedding_config import EmbeddingConfig+from .encryption_config import EncryptionConfigfrom .log_config import LogConfigfrom .parser_config import (AudioConfig,@@ -58,6 +59,11 @@ class OpenVikingConfig(BaseModel):default_factory=lambda: RerankConfig(), description="Rerank configuration")+ # Encryption configuration+ encryption: EncryptionConfig = Field(+ default_factory=lambda: EncryptionConfig(), description="Encryption configuration"+ )+# Parser configurationspdf: PDFConfig = Field(default_factory=lambda: PDFConfig(), description="PDF parsing configuration"diff --git a/pyproject.toml b/pyproject.tomlindex 10b252261..226368bf8 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -70,6 +70,8 @@ dependencies = ["tree-sitter-go>=0.23.0","tree-sitter-c-sharp>=0.23.0","loguru>=0.7.3",+ "cryptography>=42.0.0",+ "argon2-cffi>=23.0.0",][tool.uv.sources]@@ -84,6 +86,7 @@ test = ["ragas>=0.1.0","datasets>=2.0.0","pandas>=2.0.0",+ "hvac>=2.0.0",]dev = ["mypy>=1.0.0",diff --git a/tests/integration/test_encryption_integration.py b/tests/integration/test_encryption_integration.pynew file mode 100644index 000000000..e5486c0b9--- /dev/null+++ b/tests/integration/test_encryption_integration.py@@ -0,0 +1,1334 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0++"""+Encryption integration tests++Tests encryption functionality integrated with VikingFS and OpenViking service.+"""++import os+import secrets+from pathlib import Path++import pytest+import pytest_asyncio++from openviking import AsyncOpenViking+from openviking.crypto.config import bootstrap_encryption+from openviking.crypto.encryptor import FileEncryptor+from openviking.crypto.providers import LocalFileProvider+from openviking.server.api_keys import APIKeyManager+from openviking.service.core import OpenVikingService+from openviking_cli.session.user_id import UserIdentifier+from openviking_cli.utils.config.open_viking_config import OpenVikingConfigSingleton+++@pytest_asyncio.fixture(scope="function")+async def encryption_config(tmp_path):+ """Fixture that provides encryption configuration with local provider"""+ # Create temporary key file+ key_file = tmp_path / "master.key"+ root_key = secrets.token_bytes(32)+ key_file.write_text(root_key.hex())+ os.chmod(key_file, 0o600)++ return {+ "encryption": {"enabled": True, "provider": "local", "local": {"key_file": str(key_file)}}+ }+++@pytest_asyncio.fixture(scope="function")+async def file_encryptor(tmp_path):+ """Fixture that provides a FileEncryptor instance for testing"""+ # Create temporary key file+ key_file = tmp_path / "master.key"+ root_key = secrets.token_bytes(32)+ key_file.write_text(root_key.hex())+ os.chmod(key_file, 0o600)++ provider = LocalFileProvider(key_file=str(key_file))+ return FileEncryptor(provider)+++@pytest_asyncio.fixture(scope="function")+async def openviking_client_with_encryption(test_data_dir: Path, encryption_config):+ """Fixture that provides an OpenViking client with encryption enabled"""+ await AsyncOpenViking.reset()+ OpenVikingConfigSingleton.reset_instance()++ # Clean data directory+ if test_data_dir.exists():+ import shutil++ shutil.rmtree(test_data_dir)+ test_data_dir.mkdir(parents=True, exist_ok=True)++ # Create config dict with encryption enabled+ config_dict = {}+ config_dict.update(encryption_config)+ config_dict["storage"] = {+ "workspace": str(test_data_dir / "workspace"),+ "vectordb": {"name": "test", "backend": "local", "project": "default"},+ }+ config_dict["embedding"] = {+ "dense": {"provider": "openai", "api_key": "fake", "model": "text-embedding-3-small"}+ }+ config_dict["vlm"] = {"provider": "openai", "api_key": "fake", "model": "gpt-4-vision-preview"}++ # Initialize config singleton+ OpenVikingConfigSingleton.initialize(config_dict=config_dict)++ client = AsyncOpenViking(path=str(test_data_dir))+ await client.initialize()++ yield client++ await client.close()+ await AsyncOpenViking.reset()+ OpenVikingConfigSingleton.reset_instance()+++class TestEncryptionBootstrap:+ """Tests for encryption module bootstrap"""++ async def test_bootstrap_encryption_enabled(self, encryption_config):+ """Test bootstrapping encryption with enabled configuration"""+ encryptor = await bootstrap_encryption(encryption_config)+ assert encryptor is not None+ assert isinstance(encryptor, FileEncryptor)++ async def test_bootstrap_encryption_disabled(self):+ """Test bootstrapping encryption with disabled configuration"""+ config = {"encryption": {"enabled": False}}+ encryptor = await bootstrap_encryption(config)+ assert encryptor is None+++class TestFileEncryptorIntegration:+ """Tests for FileEncryptor integration"""++ async def test_encrypt_decrypt_roundtrip(self, file_encryptor):+ """Test basic encryption/decryption roundtrip"""+ account_id = "test_account"+ plaintext = b"This is a test file content for encryption integration"++ # Encrypt+ ciphertext = await file_encryptor.encrypt(account_id, plaintext)+ assert ciphertext != plaintext+ assert ciphertext.startswith(b"OVE1")++ # Decrypt+ decrypted = await file_encryptor.decrypt(account_id, ciphertext)+ assert decrypted == plaintext++ async def test_different_accounts_isolation(self, file_encryptor):+ """Test that different accounts cannot decrypt each other's data"""+ account1 = "account_1"+ account2 = "account_2"+ plaintext = b"Sensitive data for account isolation test"++ # Encrypt with account1+ ciphertext = await file_encryptor.encrypt(account1, plaintext)++ # Decrypt with account1 should work+ decrypted1 = await file_encryptor.decrypt(account1, ciphertext)+ assert decrypted1 == plaintext++ # Decrypt with account2 should fail+ from openviking.crypto.exceptions import KeyMismatchError++ with pytest.raises(KeyMismatchError):+ await file_encryptor.decrypt(account2, ciphertext)++ async def test_encrypt_empty_data(self, file_encryptor):+ """Test encrypting and decrypting empty data"""+ account_id = "test_account"+ plaintext = b""++ ciphertext = await file_encryptor.encrypt(account_id, plaintext)+ assert ciphertext.startswith(b"OVE1")++ decrypted = await file_encryptor.decrypt(account_id, ciphertext)+ assert decrypted == b""+++class TestEncryptionDisabled:+ """Tests for behavior when encryption is disabled"""++ @pytest_asyncio.fixture(scope="function")+ async def openviking_client_without_encryption(self, test_data_dir: Path):+ """Fixture that provides an OpenViking client without encryption"""+ await AsyncOpenViking.reset()+ OpenVikingConfigSingleton.reset_instance()++ if test_data_dir.exists():+ import shutil++ shutil.rmtree(test_data_dir)+ test_data_dir.mkdir(parents=True, exist_ok=True)++ # Create config dict with encryption disabled+ config_dict = {+ "encryption": {"enabled": False},+ "storage": {+ "workspace": str(test_data_dir / "workspace"),+ "vectordb": {"name": "test", "backend": "local", "project": "default"},+ },+ "embedding": {+ "dense": {+ "provider": "openai",+ "api_key": "fake",+ "model": "text-embedding-3-small",+ }+ },+ "vlm": {"provider": "openai", "api_key": "fake", "model": "gpt-4-vision-preview"},+ }++ # Initialize config singleton+ OpenVikingConfigSingleton.initialize(config_dict=config_dict)++ client = AsyncOpenViking(path=str(test_data_dir))+ await client.initialize()++ yield client++ await client.close()+ await AsyncOpenViking.reset()+ OpenVikingConfigSingleton.reset_instance()++ async def test_read_write_without_encryption(+ self, openviking_client_without_encryption: AsyncOpenViking, tmp_path: Path+ ):+ """Test normal file operations when encryption is disabled"""+ client = openviking_client_without_encryption++ test_file = tmp_path / "normal_file.txt"+ test_content = "Normal content without encryption"+ test_file.write_text(test_content)++ result = await client.add_resource(+ path=str(test_file), reason="Normal operation test", wait=True+ )+ root_uri = result["root_uri"]++ # Get tree structure to find the actual file+ uris = await client.tree(root_uri)+ assert len(uris) > 0++ # Find the actual file (skip .abstract.md and .overview.md)+ found = False+ for data in uris:+ if not data["isDir"]:+ filename = data["name"]+ # Skip auto-generated files+ if filename not in [".abstract.md", ".overview.md"]:+ file_uri = data["uri"]+ content = await client.read(file_uri)+ assert content == test_content+ found = True+ break+ assert found, "Could not find the test file"+++class TestVikingFSEncryptionWithAccounts:+ """+ Complete VikingFS encryption tests, including account management, resource operations, skill operations, etc.+ Uses internal APIs directly instead of HTTP clients to improve test efficiency and accuracy.+ """++ ROOT_KEY = "test-root-key-for-encryption-tests-abcdef123456"++ @pytest_asyncio.fixture(scope="function")+ async def openviking_service_with_encryption(self, test_data_dir: Path, encryption_config):+ """+ Fixture that provides an OpenVikingService instance with encryption enabled.+ Also initializes APIKeyManager for account management.+ """+ # Clean data directory+ if test_data_dir.exists():+ import shutil++ shutil.rmtree(test_data_dir)+ test_data_dir.mkdir(parents=True, exist_ok=True)++ # Create config dict with encryption enabled and root_api_key set+ config_dict = {}+ config_dict.update(encryption_config)+ config_dict["storage"] = {+ "workspace": str(test_data_dir / "workspace"),+ "vectordb": {"name": "test", "backend": "local", "project": "default"},+ }+ config_dict["embedding"] = {+ "dense": {"provider": "openai", "api_key": "fake", "model": "text-embedding-3-small"}+ }+ config_dict["vlm"] = {+ "provider": "openai",+ "api_key": "fake",+ "model": "gpt-4-vision-preview",+ }+ config_dict["server"] = {"root_api_key": self.ROOT_KEY}++ # Initialize config singleton+ OpenVikingConfigSingleton.initialize(config_dict=config_dict)++ # Create OpenVikingService+ svc = OpenVikingService(+ path=str(test_data_dir / "viking"), user=UserIdentifier.the_default_user("test_user")+ )+ await svc.initialize()++ # Create APIKeyManager using VikingFS to ensure system file encryption+ api_key_manager = APIKeyManager(+ root_key=self.ROOT_KEY, viking_fs=svc.viking_fs, encryption_enabled=True+ )+ await api_key_manager.load()++ yield {"service": svc, "api_key_manager": api_key_manager, "test_data_dir": test_data_dir}++ await svc.close()+ await AsyncOpenViking.reset()+ OpenVikingConfigSingleton.reset_instance()++ def _is_file_encrypted(self, file_path: Path) -> bool:+ """+ Check if a file is encrypted (via file header "OVE1" marker).++ Args:+ file_path: File path++ Returns:+ True if file is encrypted, False otherwise+ """+ if not file_path.exists():+ return False+ try:+ with open(file_path, "rb") as f:+ header = f.read(4)+ return header == b"OVE1"+ except Exception:+ return False++ def _check_all_files_encrypted(self, svc, ctx, base_uri: str, print_paths: bool = True) -> None:+ """+ Recursively check if all files in a directory are encrypted.++ Args:+ svc: OpenVikingService instance+ ctx: RequestContext instance+ base_uri: Base URI to check+ print_paths: Whether to print encrypted file paths+ """+ agfs_client = svc._agfs_client++ async def _check_recursive(uri: str):+ try:+ entries = await svc.viking_fs.ls(uri, ctx=ctx)+ for entry in entries:+ entry_uri = entry["uri"]+ if entry["isDir"]:+ await _check_recursive(entry_uri)+ else:+ # Skip .relations.json files+ if ".relations.json" in entry.get("name", ""):+ continue+ try:+ agfs_path = svc.viking_fs._uri_to_path(entry_uri, ctx=ctx)+ raw_content = agfs_client.read(agfs_path)+ assert raw_content.startswith(b"OVE1"), (+ f"File not encrypted: {entry_uri}"+ )+ if print_paths:+ print(f"[ENCRYPTED] {entry_uri}")+ except Exception as e:+ if print_paths:+ print(f"[SKIP] Skip file {entry_uri}: {e}")+ except Exception as e:+ print(f"[WARNING] Error checking {uri}: {e}")++ import asyncio++ loop = asyncio.get_event_loop()+ loop.run_until_complete(_check_recursive(base_uri))++ def _generate_random_suffix(self) -> str:+ """Generate random suffix for test account/user names"""+ import secrets++ return secrets.token_hex(4)++ async def test_account_creation_and_encryption(self, openviking_service_with_encryption):+ """+ Test 1.1: Create account, verify system files (_system/accounts.json, _system/users.json) are encrypted+ """+ data = openviking_service_with_encryption+ api_key_manager = data["api_key_manager"]+ test_data_dir = data["test_data_dir"]++ # Create new account+ account_id = "test-account-encryption"+ admin_user_id = "admin"+ user_key = await api_key_manager.create_account(account_id, admin_user_id)++ # Verify account created successfully+ assert user_key is not None+ assert len(user_key) == 64++ # AGFS /local/... paths map to test_data_dir/viking/viking/...+ # because OpenVikingService path is test_data_dir/viking,+ # and AGFSManager vikingfs_path is data_path/viking+ agfs_data_root = test_data_dir / "viking" / "viking"++ # Verify global accounts.json file created and encrypted+ global_accounts_path = agfs_data_root / "_system" / "accounts.json"+ assert global_accounts_path.exists(), (+ f"Global accounts.json not created, path: {global_accounts_path}"+ )+ assert self._is_file_encrypted(global_accounts_path), "Global accounts.json not encrypted"++ # Verify users.json file in account directory created and encrypted+ account_users_path = agfs_data_root / account_id / "_system" / "users.json"+ assert account_users_path.exists(), (+ f"Account {account_id} users.json not created, path: {account_users_path}"+ )+ assert self._is_file_encrypted(account_users_path), (+ f"Account {account_id} users.json not encrypted"+ )++ # Verify identity can be resolved correctly via API Key+ identity = api_key_manager.resolve(user_key)+ assert identity.account_id == account_id+ assert identity.user_id == admin_user_id++ async def test_user_registration_and_encryption(self, openviking_service_with_encryption):+ """+ Test 1.2: Register new user in account, verify users.json remains encrypted after update+ """+ data = openviking_service_with_encryption+ api_key_manager = data["api_key_manager"]+ test_data_dir = data["test_data_dir"]++ # Create account first+ account_id = "test-account-users"+ await api_key_manager.create_account(account_id, "admin")++ # Register new user+ new_user_id = "user1"+ new_user_key = await api_key_manager.register_user(account_id, new_user_id, "user")++ # Verify user registered successfully+ assert new_user_key is not None+ assert len(new_user_key) == 64++ # AGFS /local/... paths map to test_data_dir/viking/viking/...+ agfs_data_root = test_data_dir / "viking" / "viking"++ # Verify users.json remains encrypted after update+ account_users_path = agfs_data_root / account_id / "_system" / "users.json"+ assert self._is_file_encrypted(account_users_path), "users.json not encrypted after update"++ # Verify identity can be resolved via new user's API Key+ identity = api_key_manager.resolve(new_user_key)+ assert identity.account_id == account_id+ assert identity.user_id == new_user_id++ async def test_resource_operations_with_encryption(+ self, openviking_service_with_encryption, tmp_path+ ):+ """+ Test 2.1: Write files directly via VikingFS, verify resource file encryption and disk storage encryption+ """+ data = openviking_service_with_encryption+ svc = data["service"]++ # Create request context+ from openviking.server.identity import RequestContext, Role+ from openviking_cli.session.user_id import UserIdentifier++ default_user = UserIdentifier("default", "default", "default")+ ctx = RequestContext(user=default_user, role=Role.ROOT)++ # Write test file directly via VikingFS+ test_content = "This is a test resource file content that needs encrypted storage"+ test_uri = "viking://default/test_encrypted.txt"++ await svc.viking_fs.write_file(test_uri, test_content, ctx=ctx)++ # Verify can read correctly via VikingFS (auto-decrypt)+ read_content = await svc.viking_fs.read_file(test_uri, ctx=ctx)+ assert read_content == test_content, "Read content does not match original content"++ # Verify encryption by reading raw file content directly via AGFS+ agfs_client = svc._agfs_client+ agfs_path = svc.viking_fs._uri_to_path(test_uri, ctx=ctx)+ raw_content = agfs_client.read(agfs_path)+ assert raw_content.startswith(b"OVE1"), (+ f"File not encrypted, raw content start: {raw_content[:10]}"+ )+ assert raw_content != test_content.encode("utf-8"), (+ "File content is plaintext, not encrypted"+ )++ async def test_multiple_accounts_isolation(self, openviking_service_with_encryption):+ """+ Test: Multiple account isolation, verify files from different accounts cannot decrypt each other+ """+ data = openviking_service_with_encryption+ api_key_manager = data["api_key_manager"]+ test_data_dir = data["test_data_dir"]++ # Create two different accounts+ account1_id = "test-account-1"+ account2_id = "test-account-2"++ key1 = await api_key_manager.create_account(account1_id, "admin1")+ key2 = await api_key_manager.create_account(account2_id, "admin2")++ # AGFS /local/... paths map to test_data_dir/viking/viking/...+ agfs_data_root = test_data_dir / "viking" / "viking"++ # Verify both accounts have their own encrypted system files+ account1_users_path = agfs_data_root / account1_id / "_system" / "users.json"+ account2_users_path = agfs_data_root / account2_id / "_system" / "users.json"++ assert self._is_file_encrypted(account1_users_path)+ assert self._is_file_encrypted(account2_users_path)++ # Verify respective API Keys can resolve correctly+ identity1 = api_key_manager.resolve(key1)+ identity2 = api_key_manager.resolve(key2)++ assert identity1.account_id == account1_id+ assert identity2.account_id == account2_id++ async def test_skill_operations_with_encryption(+ self, openviking_service_with_encryption, tmp_path+ ):+ """+ Test 2.2: Write skill files directly via VikingFS, verify skill file encryption+ """+ data = openviking_service_with_encryption+ svc = data["service"]++ # Create request context+ from openviking.server.identity import RequestContext, Role+ from openviking_cli.session.user_id import UserIdentifier++ default_user = UserIdentifier("default", "default", "default")+ ctx = RequestContext(user=default_user, role=Role.ROOT)++ # Create skill directory and files directly via VikingFS+ skill_uri = "viking://default/skill/test-skill"+ await svc.viking_fs.mkdir(skill_uri, ctx=ctx)++ # Create SKILL.md file with YAML frontmatter+ skill_md_content = """---+name: Test Skill+description: This is a test skill for verifying encryption functionality+version: 1.0.0+---++# Test Skill++This is a test skill for verifying encryption functionality.++## Features++- Test encryption+- Verify file storage+"""++ skill_md_uri = f"{skill_uri}/SKILL.md"+ await svc.viking_fs.write_file(skill_md_uri, skill_md_content, ctx=ctx)++ # Verify can read skill file via VikingFS+ skill_content = await svc.viking_fs.read_file(skill_md_uri, ctx=ctx)+ assert "Test Skill" in skill_content++ # Verify encryption by reading raw file content directly via AGFS+ agfs_client = svc._agfs_client+ skill_md_uri = f"{skill_uri}/SKILL.md"+ agfs_path = svc.viking_fs._uri_to_path(skill_md_uri, ctx=ctx)+ raw_content = agfs_client.read(agfs_path)+ assert raw_content.startswith(b"OVE1"), (+ f"Skill file not encrypted, raw content start: {raw_content[:10]}"+ )+ assert "Test Skill" not in raw_content.decode("utf-8", errors="ignore"), (+ "Skill file content is plaintext, not encrypted"+ )++ async def _check_all_files_encrypted(self, root_uri, ctx, svc, agfs_client, prefix=" "):+ """+ Recursively check if all files are encrypted++ Args:+ root_uri: Root URI+ ctx: Request context+ svc: OpenVikingService instance+ agfs_client: AGFS client+ prefix: Output prefix+ """+ try:+ # First try to get URI path to see if it's a file+ try:+ agfs_path = svc.viking_fs._uri_to_path(root_uri, ctx=ctx)+ if agfs_client.exists(agfs_path) and not agfs_client.isdir(agfs_path):+ # This is a file, check directly+ raw_content = agfs_client.read(agfs_path)+ assert raw_content.startswith(b"OVE1"), f"File not encrypted: {root_uri}"+ print(f"{prefix}✓ [ENCRYPTED] {root_uri}")+ return+ except Exception:+ pass # Not a file or doesn't exist, continue trying as directory++ # Try to handle as directory+ entries = await svc.viking_fs.ls(root_uri, ctx=ctx)+ for entry in entries:+ if entry["isDir"]:+ # Recursively check subdirectories+ await self._check_all_files_encrypted(+ entry["uri"], ctx, svc, agfs_client, prefix + " "+ )+ else:+ # Check file encryption+ agfs_path = svc.viking_fs._uri_to_path(entry["uri"], ctx=ctx)+ raw_content = agfs_client.read(agfs_path)+ assert raw_content.startswith(b"OVE1"), f"File not encrypted: {entry['uri']}"+ print(f"{prefix}✓ [ENCRYPTED] {entry['uri']}")+ except Exception as e:+ print(f"{prefix}[WARNING] Error checking {root_uri}: {e}")++ async def test_complete_encryption_workflow(self, openviking_service_with_encryption, tmp_path):+ """+ Complete encryption workflow test, implemented according to user plan:+ - Prerequisites: Create random account, user+ - Execute tests: resource, skill, memory, session, relation operations+ - Post operations: Cleanup+ """+ data = openviking_service_with_encryption+ svc = data["service"]+ api_key_manager = data["api_key_manager"]++ # ========== Prerequisites ==========+ print("\n" + "=" * 80)+ print("Prerequisites: Create test account and user")+ print("=" * 80)++ # 1. Create random test account+ random_suffix = self._generate_random_suffix()+ test_account_id = f"encrypt-test-account-{random_suffix}"+ test_admin_user_id = "admin"+ print(f"[1] Create test account: {test_account_id}")++ admin_user_key = await api_key_manager.create_account(test_account_id, test_admin_user_id)+ assert admin_user_key is not None+ assert len(admin_user_key) == 64++ # 2. Verify list-accounts operation (via accessing APIKeyManager internal data)+ print("[2] Verify list-accounts operation")+ assert test_account_id in api_key_manager._accounts, (+ f"Account {test_account_id} not in account list"+ )+ print(f" ✓ Account {test_account_id} exists in account list")++ # 3. Register new user in test account+ test_user_id = f"encrypt-test-user-{random_suffix}"+ print(f"[3] Register test user: {test_user_id}")+ test_user_key = await api_key_manager.register_user(test_account_id, test_user_id, "user")+ assert test_user_key is not None+ assert len(test_user_key) == 64++ # 4. Verify list-users operation+ print("[4] Verify list-users operation")+ account_info = api_key_manager._accounts.get(test_account_id)+ assert account_info is not None+ assert test_user_id in account_info.users, f"User {test_user_id} not in user list"+ print(f" ✓ User {test_user_id} exists in user list")++ # 5. Check all files in account directory are encrypted (recursive check)+ print("[5] Check all files in account directory are encrypted")++ from openviking.server.identity import RequestContext, Role+ from openviking_cli.session.user_id import UserIdentifier++ test_user = UserIdentifier(test_account_id, test_user_id, "default")+ ctx = RequestContext(user=test_user, role=Role.USER)+ root_ctx = RequestContext(user=test_user, role=Role.ROOT)++ agfs_client = svc._agfs_client+ account_root_uri = f"viking://{test_account_id}"++ await self._check_all_files_encrypted(account_root_uri, ctx, svc, agfs_client)++ # ========== Execute tests ==========+ print("\n" + "=" * 80)+ print("Execute tests: 1. Resource operations")+ print("=" * 80)++ # 1. Resource operations+ print(+ "[1.1] Create test resources directly using VikingFS (avoid waiting for semantic processing)"+ )+ test_resource_content = "This is test resource file content for verifying encryption functionality. Contains keyword OpenViking."++ # Create test file directly using VikingFS+ test_resource_uri = "viking://resources/test_encryption_file.txt"+ await svc.viking_fs.write_file(test_resource_uri, test_resource_content, ctx=ctx)+ print(f" ✓ Test file created successfully: {test_resource_uri}")++ # Create a test directory+ test_dir_uri = "viking://resources/test_encryption_dir"+ await svc.viking_fs.mkdir(test_dir_uri, ctx=ctx)+ test_file_in_dir_uri = f"{test_dir_uri}/nested_file.txt"+ await svc.viking_fs.write_file(test_file_in_dir_uri, "This is nested file content", ctx=ctx)+ print(f" ✓ Test directory created successfully: {test_dir_uri}")++ # Check all files in resources directory are encrypted+ print("[1.1] Check files in resources directory are encrypted")+ resources_dir_uri = "viking://resources"+ await self._check_all_files_encrypted(resources_dir_uri, ctx, svc, agfs_client)++ # 1.2 Execute various operations to verify returns unencrypted+ print("[1.2] Verify various operations return unencrypted content")++ # ls operation+ print(" Executing ls operation...")+ ls_entries = await svc.viking_fs.ls(resources_dir_uri, ctx=ctx)+ assert len(ls_entries) > 0+ print(" ✓ ls operation successful")++ # tree operation+ print(" Executing tree operation...")+ tree_entries = await svc.viking_fs.tree(resources_dir_uri, ctx=ctx)+ assert len(tree_entries) > 0+ print(" ✓ tree operation successful")++ # read operation+ print(f" Executing read operation, using URI: {test_resource_uri}")+ read_content = await svc.viking_fs.read_file(test_resource_uri, ctx=ctx)+ assert test_resource_content in read_content, "read operation returned incorrect content"+ print(" ✓ read operation returns unencrypted content")++ # read nested file+ print(f" Executing read nested file, using URI: {test_file_in_dir_uri}")+ nested_content = await svc.viking_fs.read_file(test_file_in_dir_uri, ctx=ctx)+ assert "nested file" in nested_content, "read nested file returned incorrect content"+ print(" ✓ read nested file returns unencrypted content")++ # get operation (using read_file)+ print(" Executing get operation...")+ get_content = await svc.viking_fs.read_file(test_resource_uri, ctx=ctx)+ assert test_resource_content in get_content, "get operation returned incorrect content"+ print(" ✓ get operation returns unencrypted content")++ # grep operation+ print(" Executing grep operation...")+ try:+ grep_result = await svc.viking_fs.grep(resources_dir_uri, "OpenViking", ctx=ctx)+ assert grep_result is not None+ print(" ✓ grep operation successful")+ except Exception as e:+ print(f" [WARNING] grep operation may not be supported: {e}")++ # abstract operation+ print(" Executing abstract operation...")+ try:+ abstract = await svc.viking_fs.abstract(test_resource_uri, ctx=ctx)+ assert abstract is not None+ assert "OVE1" not in abstract, "abstract returns encrypted content"+ print(" ✓ abstract operation successful, returns unencrypted content")+ except Exception as e:+ print(f" [WARNING] abstract operation may not be supported: {e}")++ # overview operation+ print(" Executing overview operation...")+ try:+ overview = await svc.viking_fs.overview(test_resource_uri, ctx=ctx)+ assert overview is not None+ assert "OVE1" not in overview, "overview returns encrypted content"+ print(" ✓ overview operation successful, returns unencrypted content")+ except Exception as e:+ print(f" [WARNING] overview operation may not be supported: {e}")++ # ========== 2. Skill operations ==========+ print("\n" + "=" * 80)+ print("Execute tests: 2. Skill operations")+ print("=" * 80)++ print(+ "[2.1] Create test skill directly using VikingFS (avoid waiting for semantic processing)"+ )+ skill_content = """---+name: Test Skill+description: This is a test skill for verifying encryption functionality+version: 1.0.0+tags:+ - test+ - encryption+---++# Test Skill++This is a test skill for verifying encryption functionality.++## Features++- Test encryption+- Verify file storage+"""++ # Create test file directly using VikingFS (using root_ctx to access agent directory)+ test_skill_uri = "viking://agent/test_encryption_skill/SKILL.md"+ test_skill_dir_uri = "viking://agent/test_encryption_skill"+ await svc.viking_fs.mkdir(test_skill_dir_uri, ctx=root_ctx)+ await svc.viking_fs.write_file(test_skill_uri, skill_content, ctx=root_ctx)+ print(f" ✓ Test skill created successfully: {test_skill_uri}")++ # Check all files in agent directory are encrypted+ print("[2.1] Check files in agent directory are encrypted")+ agent_dir_uri = "viking://agent"+ await self._check_all_files_encrypted(agent_dir_uri, root_ctx, svc, agfs_client)++ # 2.2 Verify various operations return unencrypted+ print("[2.2] Verify various operations return unencrypted content")++ # ls operation+ print(" Executing ls operation...")+ skill_ls_entries = await svc.viking_fs.ls(agent_dir_uri, ctx=root_ctx)+ assert len(skill_ls_entries) > 0+ print(" ✓ ls operation successful")++ # tree operation+ print(" Executing tree operation...")+ try:+ skill_tree_entries = await svc.viking_fs.tree(agent_dir_uri, ctx=root_ctx)+ assert len(skill_tree_entries) > 0+ print(" ✓ tree operation successful")+ except Exception as e:+ print(f" [WARNING] tree operation may not be supported: {e}")++ # read operation+ print(" Executing read operation...")+ skill_read_content = await svc.viking_fs.read_file(test_skill_uri, ctx=root_ctx)+ assert "Test Skill" in skill_read_content+ print(" ✓ read operation returns unencrypted content")++ # get operation+ print(" Executing get operation...")+ skill_get_content = await svc.viking_fs.read_file(test_skill_uri, ctx=root_ctx)+ assert "Test Skill" in skill_get_content+ print(" ✓ get operation returns unencrypted content")++ # grep operation+ print(" Executing grep operation...")+ try:+ skill_grep_result = await svc.viking_fs.grep(agent_dir_uri, "Test Skill", ctx=root_ctx)+ assert skill_grep_result is not None+ print(" ✓ grep operation successful")+ except Exception as e:+ print(f" [WARNING] grep operation may not be supported: {e}")++ # abstract operation+ print(" Executing abstract operation...")+ try:+ skill_abstract = await svc.viking_fs.abstract(test_skill_uri, ctx=root_ctx)+ assert skill_abstract is not None+ assert "OVE1" not in skill_abstract, "abstract returns encrypted content"+ print(" ✓ abstract operation successful, returns unencrypted content")+ except Exception as e:+ print(f" [WARNING] abstract operation may not be supported: {e}")++ # overview operation+ print(" Executing overview operation...")+ try:+ skill_overview = await svc.viking_fs.overview(test_skill_uri, ctx=root_ctx)+ assert skill_overview is not None+ assert "OVE1" not in skill_overview, "overview returns encrypted content"+ print(" ✓ overview operation successful, returns unencrypted content")+ except Exception as e:+ print(f" [WARNING] overview operation may not be supported: {e}")++ # ========== 3. Memory operations ==========+ print("\n" + "=" * 80)+ print("Execute tests: 3. Memory operations")+ print("=" * 80)++ print("[3.1] Add memory file")+ memory_dir_uri = f"viking://{test_account_id}/user/{test_user_id}/memories"++ # Create memories directory+ try:+ await svc.viking_fs.mkdir(memory_dir_uri, ctx=ctx)+ except Exception:+ pass # Directory may already exist++ # Create preferences subdirectory+ preferences_dir_uri = f"{memory_dir_uri}/preferences"+ try:+ await svc.viking_fs.mkdir(preferences_dir_uri, ctx=ctx)+ except Exception:+ pass++ # Write memory file+ memory_content = "# Test Preferences\n\nUser prefers dark theme, likes clean code style."+ memory_uri = f"{preferences_dir_uri}/theme.md"+ await svc.viking_fs.write_file(memory_uri, memory_content, ctx=ctx)+ print(f" ✓ Memory file added: {memory_uri}")++ # Check all files in user directory are encrypted+ print("[3.1] Check files in user directory are encrypted")+ user_dir_uri = f"viking://{test_account_id}/user/{test_user_id}"+ await self._check_all_files_encrypted(user_dir_uri, ctx, svc, agfs_client)++ # 3.2 Verify various operations return unencrypted+ print("[3.2] Verify various operations return unencrypted content")++ # ls operation+ print(" Executing ls operation...")+ memory_ls_entries = await svc.viking_fs.ls(user_dir_uri, ctx=ctx)+ assert len(memory_ls_entries) >= 0+ print(" ✓ ls operation successful")++ # tree operation+ print(" Executing tree operation...")+ try:+ memory_tree_entries = await svc.viking_fs.tree(user_dir_uri, ctx=ctx)+ assert len(memory_tree_entries) >= 0+ print(" ✓ tree operation successful")+ except Exception as e:+ print(f" [WARNING] tree operation may not be supported: {e}")++ # read operation+ print(" Executing read operation...")+ memory_read_content = await svc.viking_fs.read_file(memory_uri, ctx=ctx)+ assert "Test Preferences" in memory_read_content+ print(" ✓ read operation returns unencrypted content")++ # get operation+ print(" Executing get operation...")+ memory_get_content = await svc.viking_fs.read_file(memory_uri, ctx=ctx)+ assert "Test Preferences" in memory_get_content+ print(" ✓ get operation returns unencrypted content")++ # grep operation+ print(" Executing grep operation...")+ try:+ memory_grep_result = await svc.viking_fs.grep(user_dir_uri, "dark theme", ctx=ctx)+ assert memory_grep_result is not None+ print(" ✓ grep operation successful")+ except Exception as e:+ print(f" [WARNING] grep operation may not be supported: {e}")++ # abstract operation+ print(" Executing abstract operation...")+ try:+ memory_abstract = await svc.viking_fs.abstract(memory_uri, ctx=ctx)+ assert memory_abstract is not None+ assert "OVE1" not in memory_abstract, "abstract returns encrypted content"+ print(" ✓ abstract operation successful, returns unencrypted content")+ except Exception as e:+ print(f" [WARNING] abstract operation may not be supported: {e}")++ # overview operation+ print(" Executing overview operation...")+ try:+ memory_overview = await svc.viking_fs.overview(memory_uri, ctx=ctx)+ assert memory_overview is not None+ assert "OVE1" not in memory_overview, "overview returns encrypted content"+ print(" ✓ overview operation successful, returns unencrypted content")+ except Exception as e:+ print(f" [WARNING] overview operation may not be supported: {e}")++ # ========== 4. Session operations ==========+ print("\n" + "=" * 80)+ print("Execute tests: 4. Session operations")+ print("=" * 80)++ print("[4.1] Create new session")+ session = await svc.sessions.create(ctx)+ session_id = session.session_id+ print(f" ✓ Session created: {session_id}")++ # Check session directory files are encrypted+ print("[4.1] Check files in session directory are encrypted")+ session_dir_uri = f"viking://{test_account_id}/session"+ await self._check_all_files_encrypted(session_dir_uri, ctx, svc, agfs_client)++ # 4.2 Verify various operations return unencrypted+ print("[4.2] Verify various operations return unencrypted content")+ try:+ session_entries = await svc.viking_fs.ls(session_dir_uri, ctx=ctx)+ assert len(session_entries) >= 0+ print(" ✓ ls operation successful")++ # tree operation+ try:+ session_tree = await svc.viking_fs.tree(session_dir_uri, ctx=ctx)+ assert len(session_tree) >= 0+ print(" ✓ tree operation successful")+ except Exception as e:+ print(f" [WARNING] tree operation may not be supported: {e}")++ # get operation (via sessions.get)+ print(" Executing get operation...")+ reloaded_session = await svc.sessions.get(session_id, ctx)+ assert reloaded_session is not None+ print(" ✓ get operation returns unencrypted content")++ # grep operation+ try:+ grep_result = await svc.viking_fs.grep(session_dir_uri, "session", ctx=ctx)+ assert grep_result is not None+ print(" ✓ grep operation successful")+ except Exception as e:+ print(f" [WARNING] grep operation may not be supported: {e}")++ # abstract operation (on session directory)+ try:+ abstract = await svc.viking_fs.abstract(session_dir_uri, ctx=ctx)+ assert abstract is not None+ assert "OVE1" not in abstract, "abstract returns encrypted content"+ print(" ✓ abstract operation successful, returns unencrypted content")+ except Exception as e:+ print(f" [WARNING] abstract operation may not be supported: {e}")++ # overview operation+ try:+ overview = await svc.viking_fs.overview(session_dir_uri, ctx=ctx)+ assert overview is not None+ assert "OVE1" not in overview, "overview returns encrypted content"+ print(" ✓ overview operation successful, returns unencrypted content")+ except Exception as e:+ print(f" [WARNING] overview operation may not be supported: {e}")++ except Exception as e:+ print(f" [WARNING] Operation verification: {e}")++ # 4.3 Add message and check message.jsonl encryption+ print("[4.3] Add message to session")+ from openviking.message import TextPart++ test_message = "This is a test message for verifying session message.jsonl encryption"+ parts = [TextPart(text=test_message)]+ session.add_message(role="user", parts=parts)++ print(" ✓ Message added (add_message auto-saves)")++ # Check message.jsonl file encryption+ print("[4.3] Check messages file encryption")+ await self._check_all_files_encrypted(session_dir_uri, ctx, svc, agfs_client)++ # 4.4 Verify read operation returns unencrypted+ print("[4.4] Re-read session to verify message")+ reloaded_session2 = await svc.sessions.get(session_id, ctx)+ assert len(reloaded_session2.messages) == 1+ assert test_message in reloaded_session2.messages[0].content+ print(" ✓ read operation returns unencrypted message")++ # ========== 5. Relation operations ==========+ print("\n" + "=" * 80)+ print("Execute tests: 5. Relation operations")+ print("=" * 80)++ print("[5.1] Create two resource files directly using VikingFS")+ # Create relation_test directory+ relation_test_dir_uri = "viking://resources/relation_test"+ await svc.viking_fs.mkdir(relation_test_dir_uri, ctx=ctx)++ # Create resource A directory and file+ dir_a_uri = "viking://resources/relation_test/resource_a"+ await svc.viking_fs.mkdir(dir_a_uri, ctx=ctx)+ resource_a_content = "This is resource A content for testing relation functionality."+ resource_a_file_uri = f"{dir_a_uri}/resource_a.txt"+ await svc.viking_fs.write_file(resource_a_file_uri, resource_a_content, ctx=ctx)+ print(f" ✓ Resource A created: {dir_a_uri}")++ # Create resource B directory and file+ dir_b_uri = "viking://resources/relation_test/resource_b"+ await svc.viking_fs.mkdir(dir_b_uri, ctx=ctx)+ resource_b_content = "This is resource B content for testing relation functionality."+ resource_b_file_uri = f"{dir_b_uri}/resource_b.txt"+ await svc.viking_fs.write_file(resource_b_file_uri, resource_b_content, ctx=ctx)+ print(f" ✓ Resource B created: {dir_b_uri}")++ # 5.2 Create relation and check relation.json encryption+ print("[5.2] Create relation A -> B")+ test_reason = "Resource A and resource B are related test resources"+ await svc.relations.link(from_uri=dir_a_uri, uris=dir_b_uri, ctx=ctx, reason=test_reason)+ print(f" ✓ Relation created: {dir_a_uri} -> {dir_b_uri}")++ # Verify relation created successfully+ relations = await svc.relations.relations(dir_a_uri, ctx=ctx)+ assert len(relations) == 1+ assert relations[0]["uri"] == dir_b_uri+ print(" ✓ Relation created successfully")++ # Check relation file encryption+ print("[5.2] Check relation.json file encryption")+ try:+ relation_file_uri = f"{dir_a_uri}/.relations.json"+ agfs_path = svc.viking_fs._uri_to_path(relation_file_uri, ctx=ctx)+ raw_content = agfs_client.read(agfs_path)+ assert raw_content.startswith(b"OVE1"), (+ f"relation.json not encrypted: {relation_file_uri}"+ )+ print(f" ✓ [ENCRYPTED] {relation_file_uri}")+ except Exception as e:+ print(f" [WARNING] Error checking relation.json: {e}")++ # ========== Post operations ==========+ print("\n" + "=" * 80)+ print("Post operations: Clean up test account")+ print("=" * 80)++ # Delete test account+ await api_key_manager.delete_account(test_account_id)+ assert test_account_id not in api_key_manager._accounts, (+ f"Account {test_account_id} not deleted"+ )+ print(f" ✓ Test account deleted: {test_account_id}")++ print("\n" + "=" * 80)+ print("✅ All tests completed!")+ print("=" * 80)+++class TestAddResourceWithSemanticProcessing:+ """+ Test add-resource with semantic processing (.abstract.md generation)+ and ls operation decryption.+ """++ ROOT_KEY = "test-root-key-for-encryption-tests-abcdef123456"++ @pytest_asyncio.fixture(scope="function")+ async def openviking_service_with_encryption(self, test_data_dir: Path, encryption_config):+ """+ Fixture that provides an OpenVikingService instance with encryption enabled.+ Also initializes APIKeyManager for account management.+ """+ from openviking_cli.session.user_id import UserIdentifier++ # Clean data directory+ if test_data_dir.exists():+ import shutil++ shutil.rmtree(test_data_dir)+ test_data_dir.mkdir(parents=True, exist_ok=True)++ # Create config dict with encryption enabled and root_api_key set+ config_dict = {}+ config_dict.update(encryption_config)+ config_dict["storage"] = {+ "workspace": str(test_data_dir / "workspace"),+ "vectordb": {"name": "test", "backend": "local", "project": "default"},+ }+ config_dict["embedding"] = {+ "dense": {"provider": "openai", "api_key": "fake", "model": "text-embedding-3-small"}+ }+ config_dict["vlm"] = {+ "provider": "openai",+ "api_key": "fake",+ "model": "gpt-4-vision-preview",+ }+ config_dict["server"] = {"root_api_key": self.ROOT_KEY}++ # Initialize config singleton+ OpenVikingConfigSingleton.initialize(config_dict=config_dict)++ # Create OpenVikingService+ svc = OpenVikingService(+ path=str(test_data_dir / "viking"), user=UserIdentifier.the_default_user("test_user")+ )+ await svc.initialize()++ # Create APIKeyManager using VikingFS to ensure system file encryption+ api_key_manager = APIKeyManager(+ root_key=self.ROOT_KEY, viking_fs=svc.viking_fs, encryption_enabled=True+ )+ await api_key_manager.load()++ yield {+ "service": svc,+ "api_key_manager": api_key_manager,+ "test_data_dir": test_data_dir,+ }++ await svc.close()+ OpenVikingConfigSingleton.reset_instance()++ async def test_add_resource_and_ls_abstract(self, openviking_service_with_encryption, tmp_path):+ """+ Test complete add-resource workflow and verify ls returns decrypted abstract.+ This is the test that would have caught the double encryption bug!+ """+ data = openviking_service_with_encryption+ svc = data["service"]+ api_key_manager = data["api_key_manager"]++ print("\n" + "=" * 80)+ print("Test: add-resource complete workflow + ls abstract decryption")+ print("=" * 80)++ random_suffix = secrets.token_hex(4)+ test_account_id = f"test-abstract-account-{random_suffix}"+ test_admin_user_id = "admin"+ test_user_id = f"test-abstract-user-{random_suffix}"++ print(f"[1] Create test account: {test_account_id}")+ admin_user_key = await api_key_manager.create_account(test_account_id, test_admin_user_id)+ assert admin_user_key is not None+ assert len(admin_user_key) == 64++ print(f"[2] Register test user: {test_user_id}")+ test_user_key = await api_key_manager.register_user(test_account_id, test_user_id, "user")+ assert test_user_key is not None+ assert len(test_user_key) == 64++ from openviking.server.identity import RequestContext, Role+ from openviking_cli.session.user_id import UserIdentifier++ test_user = UserIdentifier(test_account_id, test_user_id, "default")+ ctx = RequestContext(user=test_user, role=Role.USER)++ agfs_client = svc._agfs_client++ print("[3] Create test resource files (including directory structure)")+ test_dir = tmp_path / "test_abstract_dir"+ test_dir.mkdir()++ (test_dir / "README.md").write_text("""# Test Directory++This is a test directory for verifying .abstract.md generation and decryption.++## Features++- Test semantic abstract generation+- Test ls operation in encryption mode+- Verify .abstract.md correctly decrypted and displayed+""")++ subdir1 = test_dir / "subdir1"+ subdir1.mkdir()+ (subdir1 / "file1.md").write_text("""# Subdir1 File++This is a file in subdir1.+""")++ subdir2 = test_dir / "subdir2"+ subdir2.mkdir()+ (subdir2 / "file2.md").write_text("""# Subdir2 File++This is a file in subdir2.+""")++ print(f" ✓ Test directory created: {test_dir}")++ print("[4] Execute add-resource operation, wait for completion")+ result = await svc.resources.add_resource(+ path=str(test_dir), reason="Test abstract generation and decryption", ctx=ctx, wait=True+ )+ root_uri = result["root_uri"]+ print(f" ✓ add-resource successful: {root_uri}")++ print("[5] Check all files are encrypted")++ async def check_encrypted_files(uri: str):+ try:+ entries = await svc.viking_fs.ls(uri, output="original", ctx=ctx)+ for entry in entries:+ entry_uri = entry["uri"]+ entry_name = entry.get("name", "")++ if entry["isDir"]:+ await check_encrypted_files(entry_uri)+ else:+ if ".relations.json" in entry_name:+ continue+ try:+ agfs_path = svc.viking_fs._uri_to_path(entry_uri, ctx=ctx)+ raw_content = agfs_client.read(agfs_path)+ assert raw_content.startswith(b"OVE1"), (+ f"File not encrypted: {entry_uri}"+ )+ print(f" ✓ [ENCRYPTED] {entry_uri}")+ except Exception as e:+ print(f" [SKIP] {entry_uri}: {e}")+ except Exception as e:+ print(f" [WARNING] Checking {uri}: {e}")++ await check_encrypted_files(root_uri)++ print("[6] Execute ls operation, verify abstract decrypted display")+ ls_entries = await svc.viking_fs.ls(root_uri, output="agent", abs_limit=1024, ctx=ctx)+ assert len(ls_entries) > 0++ print(" Directory list returned by ls:")+ found_abstract = False+ for entry in ls_entries:+ entry_name = entry.get("name", entry.get("uri", "unknown"))+ print(f" - {entry_name} (isDir={entry.get('isDir')})")+ if entry.get("isDir"):+ abstract = entry.get("abstract", "")+ print(f" Abstract: {abstract[:100]}...")+ assert "[.abstract.md is not ready]" not in abstract, (+ f"Abstract not ready for {entry_name}"+ )+ assert "OVE1" not in abstract, f"Abstract shows encrypted content for {entry_name}"+ assert "Test Directory" in abstract or "subdir" in abstract or abstract != "", (+ f"Abstract content missing for {entry_name}"+ )+ found_abstract = True++ assert found_abstract, "No directory with abstract found"+ print(" ✓ ls operation successful, abstract correctly decrypted and displayed")++ print("[7] Execute abstract operation, verify returns unencrypted content")+ for entry in ls_entries:+ if entry.get("isDir"):+ try:+ abstract = await svc.viking_fs.abstract(entry["uri"], ctx=ctx)+ assert abstract is not None+ assert "OVE1" not in abstract, (+ f"Abstract returns encrypted content for {entry['uri']}"+ )+ print(f" ✓ abstract operation successful: {entry['uri']}")+ except Exception as e:+ print(f" [WARNING] abstract operation {entry['uri']}: {e}")++ print("[8] Execute tree operation")+ tree_entries = await svc.viking_fs.tree(root_uri, output="agent", abs_limit=512, ctx=ctx)+ assert len(tree_entries) > 0+ print(f" ✓ tree operation successful, found {len(tree_entries)} nodes")++ print("[9] Verify .abstract.md file exists and is encrypted")+ for entry in tree_entries:+ if entry.get("isDir"):+ try:+ abstract_md_uri = f"{entry['uri']}/.abstract.md"+ agfs_path = svc.viking_fs._uri_to_path(abstract_md_uri, ctx=ctx)+ raw_content = agfs_client.read(agfs_path)+ assert raw_content.startswith(b"OVE1"), (+ f".abstract.md not encrypted: {abstract_md_uri}"+ )+ print(f" ✓ [ENCRYPTED] {abstract_md_uri}")+ except Exception as e:+ print(f" [SKIP] .abstract.md check for {entry['uri']}: {e}")++ print("\n" + "=" * 80)+ print("Post operations: Clean up test account")+ print("=" * 80)++ await api_key_manager.delete_account(test_account_id)+ assert test_account_id not in api_key_manager._accounts, (+ f"Account {test_account_id} not deleted"+ )+ print(f" ✓ Test account deleted: {test_account_id}")++ print("\n" + "=" * 80)+ print("✅ add-resource + ls abstract decryption test completed!")+ print("=" * 80)+++if __name__ == "__main__":+ pytest.main([__file__, "-v"])diff --git a/tests/server/test_admin_api.py b/tests/server/test_admin_api.pyindex 80e8d5e3b..725ad658d 100644--- a/tests/server/test_admin_api.py+++ b/tests/server/test_admin_api.py@@ -39,7 +39,7 @@ async def admin_app(admin_service):app = create_app(config=config, service=admin_service)set_service(admin_service)- manager = APIKeyManager(root_key=ROOT_KEY, agfs_client=admin_service._agfs)+ manager = APIKeyManager(root_key=ROOT_KEY, viking_fs=admin_service.viking_fs)await manager.load()app.state.api_key_manager = managerdiff --git a/tests/server/test_api_key_manager.py b/tests/server/test_api_key_manager.pyindex 364ebb2a1..22220c3fe 100644--- a/tests/server/test_api_key_manager.py+++ b/tests/server/test_api_key_manager.py@@ -37,7 +37,7 @@ async def manager_service(temp_dir):@pytest_asyncio.fixture(scope="function")async def manager(manager_service):"""Fresh APIKeyManager instance, loaded."""- mgr = APIKeyManager(root_key=ROOT_KEY, agfs_client=manager_service._agfs)+ mgr = APIKeyManager(root_key=ROOT_KEY, viking_fs=manager_service.viking_fs)await mgr.load()return mgr@@ -207,17 +207,215 @@ async def test_get_users(manager: APIKeyManager):async def test_persistence_across_reload(manager_service):"""Keys should survive manager reload from AGFS."""- mgr1 = APIKeyManager(root_key=ROOT_KEY, agfs_client=manager_service._agfs)+ mgr1 = APIKeyManager(root_key=ROOT_KEY, viking_fs=manager_service.viking_fs)await mgr1.load()acct = _uid()key = await mgr1.create_account(acct, "alice")# Create new manager instance and reload- mgr2 = APIKeyManager(root_key=ROOT_KEY, agfs_client=manager_service._agfs)+ mgr2 = APIKeyManager(root_key=ROOT_KEY, viking_fs=manager_service.viking_fs)await mgr2.load()identity = mgr2.resolve(key)assert identity.account_id == acctassert identity.user_id == "alice"assert identity.role == Role.ADMIN+++# ---- Encryption tests ----+++async def test_create_account_with_encryption_enabled(manager_service):+ """create_account with encryption_enabled=True should create hashed keys."""+ acct = _uid()+ mgr = APIKeyManager(+ root_key=ROOT_KEY, viking_fs=manager_service.viking_fs, encryption_enabled=True+ )+ await mgr.load()++ key = await mgr.create_account(acct, "alice")+ stored_hash = _get_stored_hash(mgr, acct, "alice")++ _print_api_key_info("创建账号", acct, "alice", key, stored_hash)++ assert isinstance(key, str)+ assert len(key) == 64 # hex(32)+ _assert_argon2_hash(stored_hash)++ identity = mgr.resolve(key)+ assert identity.role == Role.ADMIN+ assert identity.account_id == acct+ assert identity.user_id == "alice"+++async def test_register_user_with_encryption_enabled(manager_service):+ """register_user with encryption_enabled=True should create hashed keys."""+ acct = _uid()+ mgr = APIKeyManager(+ root_key=ROOT_KEY, viking_fs=manager_service.viking_fs, encryption_enabled=True+ )+ await mgr.load()++ await mgr.create_account(acct, "alice")+ key = await mgr.register_user(acct, "bob", "user")+ stored_hash = _get_stored_hash(mgr, acct, "bob")++ _print_api_key_info("注册用户", acct, "bob", key, stored_hash, role="user")+ _assert_argon2_hash(stored_hash)++ identity = mgr.resolve(key)+ assert identity.role == Role.USER+ assert identity.account_id == acct+ assert identity.user_id == "bob"+++async def test_regenerate_key_with_encryption_enabled(manager_service):+ """regenerate_key with encryption_enabled=True should create new hashed key."""+ acct = _uid()+ mgr = APIKeyManager(+ root_key=ROOT_KEY, viking_fs=manager_service.viking_fs, encryption_enabled=True+ )+ await mgr.load()++ await mgr.create_account(acct, "alice")+ old_key = await mgr.register_user(acct, "bob", "user")+ old_stored_hash = _get_stored_hash(mgr, acct, "bob")++ new_key = await mgr.regenerate_key(acct, "bob")+ new_stored_hash = _get_stored_hash(mgr, acct, "bob")++ _print_key_regeneration_info(+ "重新生成密钥", acct, "bob", old_key, old_stored_hash, new_key, new_stored_hash+ )++ assert new_key != old_key+ assert new_stored_hash != old_stored_hash+ _assert_argon2_hash(new_stored_hash)++ # Old key invalid+ with pytest.raises(UnauthenticatedError):+ mgr.resolve(old_key)++ # New key valid+ identity = mgr.resolve(new_key)+ assert identity.user_id == "bob"+ assert identity.account_id == acct+++async def test_migrate_plaintext_keys_to_encrypted(manager_service):+ """Keys created with encryption disabled should be migrated when encryption is enabled."""+ acct = _uid()++ # First, create a key with encryption disabled+ mgr1 = APIKeyManager(+ root_key=ROOT_KEY, viking_fs=manager_service.viking_fs, encryption_enabled=False+ )+ await mgr1.load()+ key = await mgr1.create_account(acct, "alice")++ # Now, reload with encryption enabled - should migrate the key+ mgr2 = APIKeyManager(+ root_key=ROOT_KEY, viking_fs=manager_service.viking_fs, encryption_enabled=True+ )+ await mgr2.load()++ # Key should still work+ identity = mgr2.resolve(key)+ assert identity.account_id == acct+ assert identity.user_id == "alice"+++async def test_persistence_with_encryption_enabled(manager_service):+ """Hashed keys should survive manager reload from AGFS."""+ mgr1 = APIKeyManager(+ root_key=ROOT_KEY, viking_fs=manager_service.viking_fs, encryption_enabled=True+ )+ await mgr1.load()++ acct = _uid()+ key = await mgr1.create_account(acct, "alice")+ stored_hash1 = _get_stored_hash(mgr1, acct, "alice")++ _print_api_key_info("持久化验证", acct, "alice", key, stored_hash1)+ print("正在重新加载管理器...\n")++ # Create new manager instance and reload+ mgr2 = APIKeyManager(+ root_key=ROOT_KEY, viking_fs=manager_service.viking_fs, encryption_enabled=True+ )+ await mgr2.load()++ stored_hash2 = _get_stored_hash(mgr2, acct, "alice")++ print(f"\n{'=' * 80}")+ print("[持久化验证 - 重新加载后]")+ print(f"重新加载后存储的 Argon2id 哈希值: {stored_hash2}")+ print(f"哈希值一致: {stored_hash1 == stored_hash2}")+ print(f"{'=' * 80}\n")++ assert stored_hash1 == stored_hash2+ _assert_argon2_hash(stored_hash2)++ identity = mgr2.resolve(key)+ assert identity.account_id == acct+ assert identity.user_id == "alice"+ assert identity.role == Role.ADMIN+++def _print_api_key_info(+ test_name: str,+ account_id: str,+ user_id: str,+ original_key: str,+ stored_hash: str,+ role: str = None,+) -> None:+ """打印 API Key 相关信息的辅助函数。"""+ print(f"\n{'=' * 80}")+ print(f"[加密测试 - {test_name}]")+ print(f"账号ID: {account_id}")+ print(f"用户名: {user_id}")+ if role:+ print(f"角色: {role}")+ print(f"原始 API Key (返回给用户): {original_key}")+ print(f"存储的 Argon2id 哈希值: {stored_hash}")+ print(f"原始 Key 长度: {len(original_key)}")+ print(f"哈希值长度: {len(stored_hash)}")+ print(f"{'=' * 80}\n")+++def _print_key_regeneration_info(+ test_name: str,+ account_id: str,+ user_id: str,+ old_key: str,+ old_hash: str,+ new_key: str,+ new_hash: str,+) -> None:+ """打印密钥重新生成信息的辅助函数。"""+ print(f"\n{'=' * 80}")+ print(f"[加密测试 - {test_name}]")+ print(f"账号ID: {account_id}")+ print(f"用户名: {user_id}")+ print(f"旧原始 API Key: {old_key}")+ print(f"旧存储的 Argon2id 哈希值: {old_hash}")+ print(f"新原始 API Key: {new_key}")+ print(f"新存储的 Argon2id 哈希值: {new_hash}")+ print(f"密钥已更换: {new_key != old_key}")+ print(f"哈希已更换: {new_hash != old_hash}")+ print(f"{'=' * 80}\n")+++def _assert_argon2_hash(stored_hash: str) -> None:+ """验证存储的哈希值是有效的 Argon2id 格式。"""+ assert stored_hash.startswith("$argon2"), "哈希值必须是 Argon2id 格式"+++def _get_stored_hash(mgr: APIKeyManager, account_id: str, user_id: str) -> str:+ """从管理器中获取用户存储的哈希值。"""+ account_info = mgr._accounts.get(account_id)+ assert account_info is not None, f"账号 {account_id} 不存在"+ assert user_id in account_info.users, f"用户 {user_id} 不存在"+ return account_info.users[user_id]["key"]diff --git a/tests/server/test_auth.py b/tests/server/test_auth.pyindex 559131225..78a02e83e 100644--- a/tests/server/test_auth.py+++ b/tests/server/test_auth.py@@ -139,7 +139,7 @@ async def auth_app(auth_service):set_service(auth_service)# Manually initialize APIKeyManager (lifespan not triggered in ASGI tests)- manager = APIKeyManager(root_key=ROOT_KEY, agfs_client=auth_service._agfs)+ manager = APIKeyManager(root_key=ROOT_KEY, viking_fs=auth_service.viking_fs)await manager.load()app.state.api_key_manager = managerdiff --git a/tests/unit/crypto/test_config.py b/tests/unit/crypto/test_config.pynew file mode 100644index 000000000..f754d80b5--- /dev/null+++ b/tests/unit/crypto/test_config.py@@ -0,0 +1,54 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+Unit tests for crypto configuration.+"""++import pytest++from openviking.crypto.config import bootstrap_encryption+++async def test_bootstrap_encryption_disabled():+ """Test bootstrap_encryption with encryption disabled."""+ config = {"encryption": {"enabled": False}}+ encryptor = await bootstrap_encryption(config)+ assert encryptor is None+++async def test_bootstrap_encryption_local_file(tmp_path):+ """Test bootstrap_encryption with local file provider."""+ # Create temporary key file with hex format and correct permissions+ import os+ import secrets++ key_file = tmp_path / "master.key"+ # Generate valid 32-byte key in hex format+ root_key = secrets.token_bytes(32)+ key_file.write_text(root_key.hex())+ # Set correct permissions (0o600)+ os.chmod(key_file, 0o600)++ config = {+ "encryption": {+ "enabled": True,+ "provider": "local",+ "local": {"key_file": str(key_file)},+ }+ }++ encryptor = await bootstrap_encryption(config)+ assert encryptor is not None+++async def test_bootstrap_encryption_invalid_provider():+ """Test bootstrap_encryption with invalid provider."""+ config = {"encryption": {"enabled": True, "provider": "invalid_provider"}}++ from openviking.crypto.exceptions import ConfigError++ with pytest.raises(ConfigError):+ await bootstrap_encryption(config)diff --git a/tests/unit/crypto/test_encryptor.py b/tests/unit/crypto/test_encryptor.pynew file mode 100644index 000000000..31f71a023--- /dev/null+++ b/tests/unit/crypto/test_encryptor.py@@ -0,0 +1,106 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+Unit tests for FileEncryptor.+"""++import os+import secrets+import tempfile++import pytest++from openviking.crypto.encryptor import FileEncryptor+from openviking.crypto.providers import LocalFileProvider+++async def encryptor():+ """Create a FileEncryptor instance with LocalFileProvider."""+ with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:+ # Generate valid 32-byte key in hex format+ root_key = secrets.token_bytes(32)+ f.write(root_key.hex())+ temp_path = f.name+ # Set correct permissions (0o600)+ os.chmod(temp_path, 0o600)++ try:+ provider = LocalFileProvider(key_file=temp_path)+ yield FileEncryptor(provider)+ finally:+ if os.path.exists(temp_path):+ os.unlink(temp_path)+++async def test_encrypt_decrypt_roundtrip(encryptor):+ """Test encryption and decryption roundtrip."""+ account_id = "test_account"+ plaintext = b"Hello, OpenViking!"++ # Encrypt+ ciphertext = await encryptor.encrypt(account_id, plaintext)+ assert ciphertext != plaintext+ assert ciphertext.startswith(b"OVE1")++ # Decrypt+ decrypted = await encryptor.decrypt(account_id, ciphertext)+ assert decrypted == plaintext+++async def test_decrypt_unencrypted_data(encryptor):+ """Test decrypting unencrypted data."""+ account_id = "test_account"+ plaintext = b"Unencrypted data"++ # Decrypt should return the same data+ decrypted = await encryptor.decrypt(account_id, plaintext)+ assert decrypted == plaintext+++async def test_decrypt_corrupted_ciphertext(encryptor):+ """Test decrypting corrupted ciphertext."""+ account_id = "test_account"+ corrupted = b"OVE1invalid"++ from openviking.crypto.exceptions import CorruptedCiphertextError++ with pytest.raises(CorruptedCiphertextError):+ await encryptor.decrypt(account_id, corrupted)+++async def test_encrypt_empty_data(encryptor):+ """Test encrypting empty data."""+ account_id = "test_account"+ plaintext = b""++ ciphertext = await encryptor.encrypt(account_id, plaintext)+ assert ciphertext.startswith(b"OVE1")++ decrypted = await encryptor.decrypt(account_id, ciphertext)+ assert decrypted == b""+++async def test_different_accounts(encryptor):+ """Test encryption/decryption across different accounts."""+ account1 = "account1"+ account2 = "account2"+ plaintext = b"Test data"++ # Encrypt with account1+ ciphertext = await encryptor.encrypt(account1, plaintext)++ # Decrypt with account1 should work+ decrypted1 = await encryptor.decrypt(account1, ciphertext)+ assert decrypted1 == plaintext++ from openviking.crypto.exceptions import KeyMismatchError++ # Decrypt with account2 should fail+ with pytest.raises(KeyMismatchError):+ await encryptor.decrypt(account2, ciphertext)diff --git a/tests/unit/crypto/test_exceptions.py b/tests/unit/crypto/test_exceptions.pynew file mode 100644index 000000000..246243ab3--- /dev/null+++ b/tests/unit/crypto/test_exceptions.py@@ -0,0 +1,54 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+Unit tests for crypto exception classes.+"""++from openviking.crypto.exceptions import (+ AuthenticationFailedError,+ ConfigError,+ CorruptedCiphertextError,+ EncryptionError,+ InvalidMagicError,+ KeyMismatchError,+ KeyNotFoundError,+ KMSError,+)+++def test_encryption_error_inheritance():+ """Test all exception classes inherit from EncryptionError."""+ assert issubclass(InvalidMagicError, EncryptionError)+ assert issubclass(CorruptedCiphertextError, EncryptionError)+ assert issubclass(AuthenticationFailedError, EncryptionError)+ assert issubclass(KeyMismatchError, EncryptionError)+ assert issubclass(KeyNotFoundError, EncryptionError)+ assert issubclass(ConfigError, EncryptionError)+ assert issubclass(KMSError, EncryptionError)+++def test_exception_instantiation():+ """Test exception classes can be instantiated correctly."""+ err = EncryptionError("test error")+ assert str(err) == "test error"++ err = InvalidMagicError("invalid magic")+ assert str(err) == "invalid magic"++ err = CorruptedCiphertextError("ciphertext corrupted")+ assert str(err) == "ciphertext corrupted"++ err = AuthenticationFailedError("auth failed")+ assert str(err) == "auth failed"++ err = KeyMismatchError("key mismatch")+ assert str(err) == "key mismatch"++ err = KeyNotFoundError("key not found")+ assert str(err) == "key not found"++ err = ConfigError("config error")+ assert str(err) == "config error"++ err = KMSError("kms error")+ assert str(err) == "kms error"diff --git a/tests/unit/crypto/test_providers.py b/tests/unit/crypto/test_providers.pynew file mode 100644index 000000000..636a60b4f--- /dev/null+++ b/tests/unit/crypto/test_providers.py@@ -0,0 +1,70 @@+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.+# SPDX-License-Identifier: Apache-2.0+"""+Unit tests for KeyProvider implementations.+"""++import os+import tempfile++import pytest++from openviking.crypto.providers import LocalFileProvider+++async def local_file_provider():+ """Create a LocalFileProvider instance with temporary file."""+ import secrets++ with tempfile.NamedTemporaryFile(mode="w", delete=False) as f:+ # Generate valid 32-byte key in hex format+ root_key = secrets.token_bytes(32)+ f.write(root_key.hex())+ temp_path = f.name+ # Set correct permissions (0o600)+ os.chmod(temp_path, 0o600)++ try:+ provider = LocalFileProvider(key_file=temp_path)+ yield provider+ finally:+ if os.path.exists(temp_path):+ os.unlink(temp_path)+++async def test_local_file_provider_encrypt_decrypt(local_file_provider):+ """Test LocalFileProvider encryption and decryption."""+ account_id = "test_account"+ plaintext_key = b"test_file_key"++ # Encrypt+ encrypted_key, iv = await local_file_provider.encrypt_file_key(plaintext_key, account_id)+ assert encrypted_key != plaintext_key++ # Decrypt+ decrypted = await local_file_provider.decrypt_file_key(encrypted_key, iv, account_id)+ assert decrypted == plaintext_key+++async def test_local_file_provider_different_accounts(local_file_provider):+ """Test LocalFileProvider with different accounts."""+ account1 = "account1"+ account2 = "account2"+ plaintext_key = b"test_file_key"++ # Encrypt for account1+ encrypted_key1, iv1 = await local_file_provider.encrypt_file_key(plaintext_key, account1)++ # Encrypt for account2 (should be different)+ encrypted_key2, iv2 = await local_file_provider.encrypt_file_key(plaintext_key, account2)+ assert encrypted_key1 != encrypted_key2++ # Decrypt with correct account+ decrypted1 = await local_file_provider.decrypt_file_key(encrypted_key1, iv1, account1)+ assert decrypted1 == plaintext_key++ decrypted2 = await local_file_provider.decrypt_file_key(encrypted_key2, iv2, account2)+ assert decrypted2 == plaintext_keydiff --git a/uv.lock b/uv.lockindex 863fd6d81..b5ca28fad 100644--- a/uv.lock+++ b/uv.lock@@ -244,6 +244,54 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/9f/64/2e54428beba8d9992aa478bb8f6de9e4ecaa5f8f513bcfd567ed7fb0262d/apscheduler-3.11.2-py3-none-any.whl", hash = "sha256:ce005177f741409db4e4dd40a7431b76feb856b9dd69d57e0da49d6715bfd26d", size = 64439, upload-time = "2025-12-22T00:39:33.303Z" },]+[[package]]+name = "argon2-cffi"+version = "25.1.0"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "argon2-cffi-bindings" },+]+sdist = { url = "https://files.pythonhosted.org/packages/0e/89/ce5af8a7d472a67cc819d5d998aa8c82c5d860608c4db9f46f1162d7dab9/argon2_cffi-25.1.0.tar.gz", hash = "sha256:694ae5cc8a42f4c4e2bf2ca0e64e51e23a040c6a517a85074683d3959e1346c1", size = 45706, upload-time = "2025-06-03T06:55:32.073Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/4f/d3/a8b22fa575b297cd6e3e3b0155c7e25db170edf1c74783d6a31a2490b8d9/argon2_cffi-25.1.0-py3-none-any.whl", hash = "sha256:fdc8b074db390fccb6eb4a3604ae7231f219aa669a2652e0f20e16ba513d5741", size = 14657, upload-time = "2025-06-03T06:55:30.804Z" },+]++[[package]]+name = "argon2-cffi-bindings"+version = "25.1.0"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "cffi" },+]+sdist = { url = "https://files.pythonhosted.org/packages/5c/2d/db8af0df73c1cf454f71b2bbe5e356b8c1f8041c979f505b3d3186e520a9/argon2_cffi_bindings-25.1.0.tar.gz", hash = "sha256:b957f3e6ea4d55d820e40ff76f450952807013d361a65d7f28acc0acbf29229d", size = 1783441, upload-time = "2025-07-30T10:02:05.147Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/60/97/3c0a35f46e52108d4707c44b95cfe2afcafc50800b5450c197454569b776/argon2_cffi_bindings-25.1.0-cp314-cp314t-macosx_10_13_universal2.whl", hash = "sha256:3d3f05610594151994ca9ccb3c771115bdb4daef161976a266f0dd8aa9996b8f", size = 54393, upload-time = "2025-07-30T10:01:40.97Z" },+ { url = "https://files.pythonhosted.org/packages/9d/f4/98bbd6ee89febd4f212696f13c03ca302b8552e7dbf9c8efa11ea4a388c3/argon2_cffi_bindings-25.1.0-cp314-cp314t-macosx_10_13_x86_64.whl", hash = "sha256:8b8efee945193e667a396cbc7b4fb7d357297d6234d30a489905d96caabde56b", size = 29328, upload-time = "2025-07-30T10:01:41.916Z" },+ { url = "https://files.pythonhosted.org/packages/43/24/90a01c0ef12ac91a6be05969f29944643bc1e5e461155ae6559befa8f00b/argon2_cffi_bindings-25.1.0-cp314-cp314t-macosx_11_0_arm64.whl", hash = "sha256:3c6702abc36bf3ccba3f802b799505def420a1b7039862014a65db3205967f5a", size = 31269, upload-time = "2025-07-30T10:01:42.716Z" },+ { url = "https://files.pythonhosted.org/packages/d4/d3/942aa10782b2697eee7af5e12eeff5ebb325ccfb86dd8abda54174e377e4/argon2_cffi_bindings-25.1.0-cp314-cp314t-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:a1c70058c6ab1e352304ac7e3b52554daadacd8d453c1752e547c76e9c99ac44", size = 86558, upload-time = "2025-07-30T10:01:43.943Z" },+ { url = "https://files.pythonhosted.org/packages/0d/82/b484f702fec5536e71836fc2dbc8c5267b3f6e78d2d539b4eaa6f0db8bf8/argon2_cffi_bindings-25.1.0-cp314-cp314t-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:e2fd3bfbff3c5d74fef31a722f729bf93500910db650c925c2d6ef879a7e51cb", size = 92364, upload-time = "2025-07-30T10:01:44.887Z" },+ { url = "https://files.pythonhosted.org/packages/c9/c1/a606ff83b3f1735f3759ad0f2cd9e038a0ad11a3de3b6c673aa41c24bb7b/argon2_cffi_bindings-25.1.0-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:c4f9665de60b1b0e99bcd6be4f17d90339698ce954cfd8d9cf4f91c995165a92", size = 85637, upload-time = "2025-07-30T10:01:46.225Z" },+ { url = "https://files.pythonhosted.org/packages/44/b4/678503f12aceb0262f84fa201f6027ed77d71c5019ae03b399b97caa2f19/argon2_cffi_bindings-25.1.0-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:ba92837e4a9aa6a508c8d2d7883ed5a8f6c308c89a4790e1e447a220deb79a85", size = 91934, upload-time = "2025-07-30T10:01:47.203Z" },+ { url = "https://files.pythonhosted.org/packages/f0/c7/f36bd08ef9bd9f0a9cff9428406651f5937ce27b6c5b07b92d41f91ae541/argon2_cffi_bindings-25.1.0-cp314-cp314t-win32.whl", hash = "sha256:84a461d4d84ae1295871329b346a97f68eade8c53b6ed9a7ca2d7467f3c8ff6f", size = 28158, upload-time = "2025-07-30T10:01:48.341Z" },+ { url = "https://files.pythonhosted.org/packages/b3/80/0106a7448abb24a2c467bf7d527fe5413b7fdfa4ad6d6a96a43a62ef3988/argon2_cffi_bindings-25.1.0-cp314-cp314t-win_amd64.whl", hash = "sha256:b55aec3565b65f56455eebc9b9f34130440404f27fe21c3b375bf1ea4d8fbae6", size = 32597, upload-time = "2025-07-30T10:01:49.112Z" },+ { url = "https://files.pythonhosted.org/packages/05/b8/d663c9caea07e9180b2cb662772865230715cbd573ba3b5e81793d580316/argon2_cffi_bindings-25.1.0-cp314-cp314t-win_arm64.whl", hash = "sha256:87c33a52407e4c41f3b70a9c2d3f6056d88b10dad7695be708c5021673f55623", size = 28231, upload-time = "2025-07-30T10:01:49.92Z" },+ { url = "https://files.pythonhosted.org/packages/1d/57/96b8b9f93166147826da5f90376e784a10582dd39a393c99bb62cfcf52f0/argon2_cffi_bindings-25.1.0-cp39-abi3-macosx_10_9_universal2.whl", hash = "sha256:aecba1723ae35330a008418a91ea6cfcedf6d31e5fbaa056a166462ff066d500", size = 54121, upload-time = "2025-07-30T10:01:50.815Z" },+ { url = "https://files.pythonhosted.org/packages/0a/08/a9bebdb2e0e602dde230bdde8021b29f71f7841bd54801bcfd514acb5dcf/argon2_cffi_bindings-25.1.0-cp39-abi3-macosx_10_9_x86_64.whl", hash = "sha256:2630b6240b495dfab90aebe159ff784d08ea999aa4b0d17efa734055a07d2f44", size = 29177, upload-time = "2025-07-30T10:01:51.681Z" },+ { url = "https://files.pythonhosted.org/packages/b6/02/d297943bcacf05e4f2a94ab6f462831dc20158614e5d067c35d4e63b9acb/argon2_cffi_bindings-25.1.0-cp39-abi3-macosx_11_0_arm64.whl", hash = "sha256:7aef0c91e2c0fbca6fc68e7555aa60ef7008a739cbe045541e438373bc54d2b0", size = 31090, upload-time = "2025-07-30T10:01:53.184Z" },+ { url = "https://files.pythonhosted.org/packages/c1/93/44365f3d75053e53893ec6d733e4a5e3147502663554b4d864587c7828a7/argon2_cffi_bindings-25.1.0-cp39-abi3-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:1e021e87faa76ae0d413b619fe2b65ab9a037f24c60a1e6cc43457ae20de6dc6", size = 81246, upload-time = "2025-07-30T10:01:54.145Z" },+ { url = "https://files.pythonhosted.org/packages/09/52/94108adfdd6e2ddf58be64f959a0b9c7d4ef2fa71086c38356d22dc501ea/argon2_cffi_bindings-25.1.0-cp39-abi3-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:d3e924cfc503018a714f94a49a149fdc0b644eaead5d1f089330399134fa028a", size = 87126, upload-time = "2025-07-30T10:01:55.074Z" },+ { url = "https://files.pythonhosted.org/packages/72/70/7a2993a12b0ffa2a9271259b79cc616e2389ed1a4d93842fac5a1f923ffd/argon2_cffi_bindings-25.1.0-cp39-abi3-musllinux_1_2_aarch64.whl", hash = "sha256:c87b72589133f0346a1cb8d5ecca4b933e3c9b64656c9d175270a000e73b288d", size = 80343, upload-time = "2025-07-30T10:01:56.007Z" },+ { url = "https://files.pythonhosted.org/packages/78/9a/4e5157d893ffc712b74dbd868c7f62365618266982b64accab26bab01edc/argon2_cffi_bindings-25.1.0-cp39-abi3-musllinux_1_2_x86_64.whl", hash = "sha256:1db89609c06afa1a214a69a462ea741cf735b29a57530478c06eb81dd403de99", size = 86777, upload-time = "2025-07-30T10:01:56.943Z" },+ { url = "https://files.pythonhosted.org/packages/74/cd/15777dfde1c29d96de7f18edf4cc94c385646852e7c7b0320aa91ccca583/argon2_cffi_bindings-25.1.0-cp39-abi3-win32.whl", hash = "sha256:473bcb5f82924b1becbb637b63303ec8d10e84c8d241119419897a26116515d2", size = 27180, upload-time = "2025-07-30T10:01:57.759Z" },+ { url = "https://files.pythonhosted.org/packages/e2/c6/a759ece8f1829d1f162261226fbfd2c6832b3ff7657384045286d2afa384/argon2_cffi_bindings-25.1.0-cp39-abi3-win_amd64.whl", hash = "sha256:a98cd7d17e9f7ce244c0803cad3c23a7d379c301ba618a5fa76a67d116618b98", size = 31715, upload-time = "2025-07-30T10:01:58.56Z" },+ { url = "https://files.pythonhosted.org/packages/42/b9/f8d6fa329ab25128b7e98fd83a3cb34d9db5b059a9847eddb840a0af45dd/argon2_cffi_bindings-25.1.0-cp39-abi3-win_arm64.whl", hash = "sha256:b0fdbcf513833809c882823f98dc2f931cf659d9a1429616ac3adebb49f5db94", size = 27149, upload-time = "2025-07-30T10:01:59.329Z" },+ { url = "https://files.pythonhosted.org/packages/11/2d/ba4e4ca8d149f8dcc0d952ac0967089e1d759c7e5fcf0865a317eb680fbb/argon2_cffi_bindings-25.1.0-pp310-pypy310_pp73-macosx_10_15_x86_64.whl", hash = "sha256:6dca33a9859abf613e22733131fc9194091c1fa7cb3e131c143056b4856aa47e", size = 24549, upload-time = "2025-07-30T10:02:00.101Z" },+ { url = "https://files.pythonhosted.org/packages/5c/82/9b2386cc75ac0bd3210e12a44bfc7fd1632065ed8b80d573036eecb10442/argon2_cffi_bindings-25.1.0-pp310-pypy310_pp73-macosx_11_0_arm64.whl", hash = "sha256:21378b40e1b8d1655dd5310c84a40fc19a9aa5e6366e835ceb8576bf0fea716d", size = 25539, upload-time = "2025-07-30T10:02:00.929Z" },+ { url = "https://files.pythonhosted.org/packages/31/db/740de99a37aa727623730c90d92c22c9e12585b3c98c54b7960f7810289f/argon2_cffi_bindings-25.1.0-pp310-pypy310_pp73-manylinux_2_26_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:5d588dec224e2a83edbdc785a5e6f3c6cd736f46bfd4b441bbb5aa1f5085e584", size = 28467, upload-time = "2025-07-30T10:02:02.08Z" },+ { url = "https://files.pythonhosted.org/packages/71/7a/47c4509ea18d755f44e2b92b7178914f0c113946d11e16e626df8eaa2b0b/argon2_cffi_bindings-25.1.0-pp310-pypy310_pp73-manylinux_2_26_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:5acb4e41090d53f17ca1110c3427f0a130f944b896fc8c83973219c97f57b690", size = 27355, upload-time = "2025-07-30T10:02:02.867Z" },+ { url = "https://files.pythonhosted.org/packages/ee/82/82745642d3c46e7cea25e1885b014b033f4693346ce46b7f47483cf5d448/argon2_cffi_bindings-25.1.0-pp310-pypy310_pp73-win_amd64.whl", hash = "sha256:da0c79c23a63723aa5d782250fbf51b768abca630285262fb5144ba5ae01e520", size = 29187, upload-time = "2025-07-30T10:02:03.674Z" },+]+[[package]]name = "async-timeout"version = "5.0.1"@@ -1493,7 +1541,6 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/38/3f/9859f655d11901e7b2996c6e3d33e0caa9a1d4572c3bc61ed0faa64b2f4c/greenlet-3.3.2-cp310-cp310-macosx_11_0_universal2.whl", hash = "sha256:9bc885b89709d901859cf95179ec9f6bb67a3d2bb1f0e88456461bd4b7f8fd0d", size = 277747, upload-time = "2026-02-20T20:16:21.325Z" },{ url = "https://files.pythonhosted.org/packages/fb/07/cb284a8b5c6498dbd7cba35d31380bb123d7dceaa7907f606c8ff5993cbf/greenlet-3.3.2-cp310-cp310-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:b568183cf65b94919be4438dc28416b234b678c608cafac8874dfeeb2a9bbe13", size = 579202, upload-time = "2026-02-20T20:47:28.955Z" },{ url = "https://files.pythonhosted.org/packages/ed/45/67922992b3a152f726163b19f890a85129a992f39607a2a53155de3448b8/greenlet-3.3.2-cp310-cp310-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:527fec58dc9f90efd594b9b700662ed3fb2493c2122067ac9c740d98080a620e", size = 590620, upload-time = "2026-02-20T20:55:55.581Z" },- { url = "https://files.pythonhosted.org/packages/03/5f/6e2a7d80c353587751ef3d44bb947f0565ec008a2e0927821c007e96d3a7/greenlet-3.3.2-cp310-cp310-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:508c7f01f1791fbc8e011bd508f6794cb95397fdb198a46cb6635eb5b78d85a7", size = 602132, upload-time = "2026-02-20T21:02:43.261Z" },{ url = "https://files.pythonhosted.org/packages/ad/55/9f1ebb5a825215fadcc0f7d5073f6e79e3007e3282b14b22d6aba7ca6cb8/greenlet-3.3.2-cp310-cp310-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:ad0c8917dd42a819fe77e6bdfcb84e3379c0de956469301d9fd36427a1ca501f", size = 591729, upload-time = "2026-02-20T20:20:58.395Z" },{ url = "https://files.pythonhosted.org/packages/24/b4/21f5455773d37f94b866eb3cf5caed88d6cea6dd2c6e1f9c34f463cba3ec/greenlet-3.3.2-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:97245cc10e5515dbc8c3104b2928f7f02b6813002770cfaffaf9a6e0fc2b94ef", size = 1551946, upload-time = "2026-02-20T20:49:31.102Z" },{ url = "https://files.pythonhosted.org/packages/00/68/91f061a926abead128fe1a87f0b453ccf07368666bd59ffa46016627a930/greenlet-3.3.2-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:8c1fdd7d1b309ff0da81d60a9688a8bd044ac4e18b250320a96fc68d31c209ca", size = 1618494, upload-time = "2026-02-20T20:21:06.541Z" },@@ -1501,7 +1548,6 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/f3/47/16400cb42d18d7a6bb46f0626852c1718612e35dcb0dffa16bbaffdf5dd2/greenlet-3.3.2-cp311-cp311-macosx_11_0_universal2.whl", hash = "sha256:c56692189a7d1c7606cb794be0a8381470d95c57ce5be03fb3d0ef57c7853b86", size = 278890, upload-time = "2026-02-20T20:19:39.263Z" },{ url = "https://files.pythonhosted.org/packages/a3/90/42762b77a5b6aa96cd8c0e80612663d39211e8ae8a6cd47c7f1249a66262/greenlet-3.3.2-cp311-cp311-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:1ebd458fa8285960f382841da585e02201b53a5ec2bac6b156fc623b5ce4499f", size = 581120, upload-time = "2026-02-20T20:47:30.161Z" },{ url = "https://files.pythonhosted.org/packages/bf/6f/f3d64f4fa0a9c7b5c5b3c810ff1df614540d5aa7d519261b53fba55d4df9/greenlet-3.3.2-cp311-cp311-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:a443358b33c4ec7b05b79a7c8b466f5d275025e750298be7340f8fc63dff2a55", size = 594363, upload-time = "2026-02-20T20:55:56.965Z" },- { url = "https://files.pythonhosted.org/packages/9c/8b/1430a04657735a3f23116c2e0d5eb10220928846e4537a938a41b350bed6/greenlet-3.3.2-cp311-cp311-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:4375a58e49522698d3e70cc0b801c19433021b5c37686f7ce9c65b0d5c8677d2", size = 605046, upload-time = "2026-02-20T21:02:45.234Z" },{ url = "https://files.pythonhosted.org/packages/72/83/3e06a52aca8128bdd4dcd67e932b809e76a96ab8c232a8b025b2850264c5/greenlet-3.3.2-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:8e2cd90d413acbf5e77ae41e5d3c9b3ac1d011a756d7284d7f3f2b806bbd6358", size = 594156, upload-time = "2026-02-20T20:20:59.955Z" },{ url = "https://files.pythonhosted.org/packages/70/79/0de5e62b873e08fe3cef7dbe84e5c4bc0e8ed0c7ff131bccb8405cd107c8/greenlet-3.3.2-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:442b6057453c8cb29b4fb36a2ac689382fc71112273726e2423f7f17dc73bf99", size = 1554649, upload-time = "2026-02-20T20:49:32.293Z" },{ url = "https://files.pythonhosted.org/packages/5a/00/32d30dee8389dc36d42170a9c66217757289e2afb0de59a3565260f38373/greenlet-3.3.2-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:45abe8eb6339518180d5a7fa47fa01945414d7cca5ecb745346fc6a87d2750be", size = 1619472, upload-time = "2026-02-20T20:21:07.966Z" },@@ -1510,7 +1556,6 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/ea/ab/1608e5a7578e62113506740b88066bf09888322a311cff602105e619bd87/greenlet-3.3.2-cp312-cp312-macosx_11_0_universal2.whl", hash = "sha256:ac8d61d4343b799d1e526db579833d72f23759c71e07181c2d2944e429eb09cd", size = 280358, upload-time = "2026-02-20T20:17:43.971Z" },{ url = "https://files.pythonhosted.org/packages/a5/23/0eae412a4ade4e6623ff7626e38998cb9b11e9ff1ebacaa021e4e108ec15/greenlet-3.3.2-cp312-cp312-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:3ceec72030dae6ac0c8ed7591b96b70410a8be370b6a477b1dbc072856ad02bd", size = 601217, upload-time = "2026-02-20T20:47:31.462Z" },{ url = "https://files.pythonhosted.org/packages/f8/16/5b1678a9c07098ecb9ab2dd159fafaf12e963293e61ee8d10ecb55273e5e/greenlet-3.3.2-cp312-cp312-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:a2a5be83a45ce6188c045bcc44b0ee037d6a518978de9a5d97438548b953a1ac", size = 611792, upload-time = "2026-02-20T20:55:58.423Z" },- { url = "https://files.pythonhosted.org/packages/5c/c5/cc09412a29e43406eba18d61c70baa936e299bc27e074e2be3806ed29098/greenlet-3.3.2-cp312-cp312-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:ae9e21c84035c490506c17002f5c8ab25f980205c3e61ddb3a2a2a2e6c411fcb", size = 626250, upload-time = "2026-02-20T21:02:46.596Z" },{ url = "https://files.pythonhosted.org/packages/50/1f/5155f55bd71cabd03765a4aac9ac446be129895271f73872c36ebd4b04b6/greenlet-3.3.2-cp312-cp312-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:43e99d1749147ac21dde49b99c9abffcbc1e2d55c67501465ef0930d6e78e070", size = 613875, upload-time = "2026-02-20T20:21:01.102Z" },{ url = "https://files.pythonhosted.org/packages/fc/dd/845f249c3fcd69e32df80cdab059b4be8b766ef5830a3d0aa9d6cad55beb/greenlet-3.3.2-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:4c956a19350e2c37f2c48b336a3afb4bff120b36076d9d7fb68cb44e05d95b79", size = 1571467, upload-time = "2026-02-20T20:49:33.495Z" },{ url = "https://files.pythonhosted.org/packages/2a/50/2649fe21fcc2b56659a452868e695634722a6655ba245d9f77f5656010bf/greenlet-3.3.2-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:6c6f8ba97d17a1e7d664151284cb3315fc5f8353e75221ed4324f84eb162b395", size = 1640001, upload-time = "2026-02-20T20:21:09.154Z" },@@ -1519,7 +1564,6 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/ac/48/f8b875fa7dea7dd9b33245e37f065af59df6a25af2f9561efa8d822fde51/greenlet-3.3.2-cp313-cp313-macosx_11_0_universal2.whl", hash = "sha256:aa6ac98bdfd716a749b84d4034486863fd81c3abde9aa3cf8eff9127981a4ae4", size = 279120, upload-time = "2026-02-20T20:19:01.9Z" },{ url = "https://files.pythonhosted.org/packages/49/8d/9771d03e7a8b1ee456511961e1b97a6d77ae1dea4a34a5b98eee706689d3/greenlet-3.3.2-cp313-cp313-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:ab0c7e7901a00bc0a7284907273dc165b32e0d109a6713babd04471327ff7986", size = 603238, upload-time = "2026-02-20T20:47:32.873Z" },{ url = "https://files.pythonhosted.org/packages/59/0e/4223c2bbb63cd5c97f28ffb2a8aee71bdfb30b323c35d409450f51b91e3e/greenlet-3.3.2-cp313-cp313-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:d248d8c23c67d2291ffd47af766e2a3aa9fa1c6703155c099feb11f526c63a92", size = 614219, upload-time = "2026-02-20T20:55:59.817Z" },- { url = "https://files.pythonhosted.org/packages/94/2b/4d012a69759ac9d77210b8bfb128bc621125f5b20fc398bce3940d036b1c/greenlet-3.3.2-cp313-cp313-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:ccd21bb86944ca9be6d967cf7691e658e43417782bce90b5d2faeda0ff78a7dd", size = 628268, upload-time = "2026-02-20T21:02:48.024Z" },{ url = "https://files.pythonhosted.org/packages/7a/34/259b28ea7a2a0c904b11cd36c79b8cef8019b26ee5dbe24e73b469dea347/greenlet-3.3.2-cp313-cp313-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b6997d360a4e6a4e936c0f9625b1c20416b8a0ea18a8e19cabbefc712e7397ab", size = 616774, upload-time = "2026-02-20T20:21:02.454Z" },{ url = "https://files.pythonhosted.org/packages/0a/03/996c2d1689d486a6e199cb0f1cf9e4aa940c500e01bdf201299d7d61fa69/greenlet-3.3.2-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:64970c33a50551c7c50491671265d8954046cb6e8e2999aacdd60e439b70418a", size = 1571277, upload-time = "2026-02-20T20:49:34.795Z" },{ url = "https://files.pythonhosted.org/packages/d9/c4/2570fc07f34a39f2caf0bf9f24b0a1a0a47bc2e8e465b2c2424821389dfc/greenlet-3.3.2-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:1a9172f5bf6bd88e6ba5a84e0a68afeac9dc7b6b412b245dd64f52d83c81e55b", size = 1640455, upload-time = "2026-02-20T20:21:10.261Z" },@@ -1528,7 +1572,6 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/3f/ae/8bffcbd373b57a5992cd077cbe8858fff39110480a9d50697091faea6f39/greenlet-3.3.2-cp314-cp314-macosx_11_0_universal2.whl", hash = "sha256:8d1658d7291f9859beed69a776c10822a0a799bc4bfe1bd4272bb60e62507dab", size = 279650, upload-time = "2026-02-20T20:18:00.783Z" },{ url = "https://files.pythonhosted.org/packages/d1/c0/45f93f348fa49abf32ac8439938726c480bd96b2a3c6f4d949ec0124b69f/greenlet-3.3.2-cp314-cp314-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:18cb1b7337bca281915b3c5d5ae19f4e76d35e1df80f4ad3c1a7be91fadf1082", size = 650295, upload-time = "2026-02-20T20:47:34.036Z" },{ url = "https://files.pythonhosted.org/packages/b3/de/dd7589b3f2b8372069ab3e4763ea5329940fc7ad9dcd3e272a37516d7c9b/greenlet-3.3.2-cp314-cp314-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:c2e47408e8ce1c6f1ceea0dffcdf6ebb85cc09e55c7af407c99f1112016e45e9", size = 662163, upload-time = "2026-02-20T20:56:01.295Z" },- { url = "https://files.pythonhosted.org/packages/cd/ac/85804f74f1ccea31ba518dcc8ee6f14c79f73fe36fa1beba38930806df09/greenlet-3.3.2-cp314-cp314-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:e3cb43ce200f59483eb82949bf1835a99cf43d7571e900d7c8d5c62cdf25d2f9", size = 675371, upload-time = "2026-02-20T21:02:49.664Z" },{ url = "https://files.pythonhosted.org/packages/d2/d8/09bfa816572a4d83bccd6750df1926f79158b1c36c5f73786e26dbe4ee38/greenlet-3.3.2-cp314-cp314-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:63d10328839d1973e5ba35e98cccbca71b232b14051fd957b6f8b6e8e80d0506", size = 664160, upload-time = "2026-02-20T20:21:04.015Z" },{ url = "https://files.pythonhosted.org/packages/48/cf/56832f0c8255d27f6c35d41b5ec91168d74ec721d85f01a12131eec6b93c/greenlet-3.3.2-cp314-cp314-musllinux_1_2_aarch64.whl", hash = "sha256:8e4ab3cfb02993c8cc248ea73d7dae6cec0253e9afa311c9b37e603ca9fad2ce", size = 1619181, upload-time = "2026-02-20T20:49:36.052Z" },{ url = "https://files.pythonhosted.org/packages/0a/23/b90b60a4aabb4cec0796e55f25ffbfb579a907c3898cd2905c8918acaa16/greenlet-3.3.2-cp314-cp314-musllinux_1_2_x86_64.whl", hash = "sha256:94ad81f0fd3c0c0681a018a976e5c2bd2ca2d9d94895f23e7bb1af4e8af4e2d5", size = 1687713, upload-time = "2026-02-20T20:21:11.684Z" },@@ -1537,7 +1580,6 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/98/6d/8f2ef704e614bcf58ed43cfb8d87afa1c285e98194ab2cfad351bf04f81e/greenlet-3.3.2-cp314-cp314t-macosx_11_0_universal2.whl", hash = "sha256:e26e72bec7ab387ac80caa7496e0f908ff954f31065b0ffc1f8ecb1338b11b54", size = 286617, upload-time = "2026-02-20T20:19:29.856Z" },{ url = "https://files.pythonhosted.org/packages/5e/0d/93894161d307c6ea237a43988f27eba0947b360b99ac5239ad3fe09f0b47/greenlet-3.3.2-cp314-cp314t-manylinux_2_24_aarch64.manylinux_2_28_aarch64.whl", hash = "sha256:8b466dff7a4ffda6ca975979bab80bdadde979e29fc947ac3be4451428d8b0e4", size = 655189, upload-time = "2026-02-20T20:47:35.742Z" },{ url = "https://files.pythonhosted.org/packages/f5/2c/d2d506ebd8abcb57386ec4f7ba20f4030cbe56eae541bc6fd6ef399c0b41/greenlet-3.3.2-cp314-cp314t-manylinux_2_24_ppc64le.manylinux_2_28_ppc64le.whl", hash = "sha256:b8bddc5b73c9720bea487b3bffdb1840fe4e3656fba3bd40aa1489e9f37877ff", size = 658225, upload-time = "2026-02-20T20:56:02.527Z" },- { url = "https://files.pythonhosted.org/packages/d1/67/8197b7e7e602150938049d8e7f30de1660cfb87e4c8ee349b42b67bdb2e1/greenlet-3.3.2-cp314-cp314t-manylinux_2_24_s390x.manylinux_2_28_s390x.whl", hash = "sha256:59b3e2c40f6706b05a9cd299c836c6aa2378cabe25d021acd80f13abf81181cf", size = 666581, upload-time = "2026-02-20T21:02:51.526Z" },{ url = "https://files.pythonhosted.org/packages/8e/30/3a09155fbf728673a1dea713572d2d31159f824a37c22da82127056c44e4/greenlet-3.3.2-cp314-cp314t-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl", hash = "sha256:b26b0f4428b871a751968285a1ac9648944cea09807177ac639b030bddebcea4", size = 657907, upload-time = "2026-02-20T20:21:05.259Z" },{ url = "https://files.pythonhosted.org/packages/f3/fd/d05a4b7acd0154ed758797f0a43b4c0962a843bedfe980115e842c5b2d08/greenlet-3.3.2-cp314-cp314t-musllinux_1_2_aarch64.whl", hash = "sha256:1fb39a11ee2e4d94be9a76671482be9398560955c9e568550de0224e41104727", size = 1618857, upload-time = "2026-02-20T20:49:37.309Z" },{ url = "https://files.pythonhosted.org/packages/6f/e1/50ee92a5db521de8f35075b5eff060dd43d39ebd46c2181a2042f7070385/greenlet-3.3.2-cp314-cp314t-musllinux_1_2_x86_64.whl", hash = "sha256:20154044d9085151bc309e7689d6f7ba10027f8f5a8c0676ad398b951913d89e", size = 1680010, upload-time = "2026-02-20T20:21:13.427Z" },@@ -1678,6 +1720,18 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/92/e3/e3a44f54c8e2f28983fcf07f13d4260b37bd6a0d3a081041bc60b91d230e/huggingface_hub-1.6.0-py3-none-any.whl", hash = "sha256:ef40e2d5cb85e48b2c067020fa5142168342d5108a1b267478ed384ecbf18961", size = 612874, upload-time = "2026-03-06T14:19:16.844Z" },]+[[package]]+name = "hvac"+version = "2.4.0"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "requests" },+]+sdist = { url = "https://files.pythonhosted.org/packages/e2/57/b46c397fb3842cfb02a44609aa834c887f38dd75f290c2fc5a34da4b2fee/hvac-2.4.0.tar.gz", hash = "sha256:e0056ad9064e7923e874e6769015b032580b639e29246f5ab1044f7959c1c7e0", size = 332543, upload-time = "2025-10-30T12:57:47.512Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/55/33/71e45a6bd6875f44a26f99da31c63b6840123e88bedf2c0b1ce429b8be12/hvac-2.4.0-py3-none-any.whl", hash = "sha256:008db5efd8c2f77bd37d2368ea5f713edceae1c65f11fd608393179478649e0f", size = 155921, upload-time = "2025-10-30T12:57:46.253Z" },+]+[[package]]name = "idna"version = "3.11"@@ -3283,6 +3337,8 @@ name = "openviking"source = { editable = "." }dependencies = [{ name = "apscheduler" },+ { name = "argon2-cffi" },+ { name = "cryptography" },{ name = "ebooklib" },{ name = "fastapi" },{ name = "httpx" },@@ -3342,6 +3398,7 @@ bot = [{ name = "readability-lxml" },{ name = "rich" },{ name = "socksio" },+ { name = "tavily-python" },{ name = "websocket-client" },{ name = "websockets" },]@@ -3379,6 +3436,7 @@ bot-full = [{ name = "rich" },{ name = "slack-sdk" },{ name = "socksio" },+ { name = "tavily-python" },{ name = "websocket-client" },{ name = "websockets" },]@@ -3441,6 +3499,7 @@ gemini-async = [test = [{ name = "boto3" },{ name = "datasets" },+ { name = "hvac" },{ name = "pandas", version = "2.3.3", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version < '3.11'" },{ name = "pandas", version = "3.0.1", source = { registry = "https://pypi.org/simple" }, marker = "python_full_version >= '3.11'" },{ name = "pytest" },@@ -3459,11 +3518,13 @@ requires-dist = [{ name = "agent-sandbox", marker = "extra == 'bot-sandbox'", specifier = ">=0.0.23" },{ name = "anyio", marker = "extra == 'gemini-async'", specifier = ">=4.0.0" },{ name = "apscheduler", specifier = ">=3.11.0" },+ { name = "argon2-cffi", specifier = ">=23.0.0" },{ name = "beautifulsoup4", marker = "extra == 'bot'", specifier = ">=4.12.0" },{ name = "boto3", marker = "extra == 'test'", specifier = ">=1.42.44" },{ name = "build", marker = "extra == 'build'" },{ name = "cmake", marker = "extra == 'build'", specifier = ">=3.15" },{ name = "croniter", marker = "extra == 'bot'", specifier = ">=2.0.0" },+ { name = "cryptography", specifier = ">=42.0.0" },{ name = "datasets", marker = "extra == 'eval'", specifier = ">=2.0.0" },{ name = "datasets", marker = "extra == 'test'", specifier = ">=2.0.0" },{ name = "ddgs", marker = "extra == 'bot'", specifier = ">=9.0.0" },@@ -3477,6 +3538,7 @@ requires-dist = [{ name = "html2text", marker = "extra == 'bot'", specifier = ">=2020.1.16" },{ name = "httpx", specifier = ">=0.25.0" },{ name = "httpx", extras = ["socks"], marker = "extra == 'bot'", specifier = ">=0.25.0" },+ { name = "hvac", marker = "extra == 'test'", specifier = ">=2.0.0" },{ name = "jinja2", specifier = ">=3.1.6" },{ name = "json-repair", specifier = ">=0.25.0" },{ name = "langfuse", marker = "extra == 'bot-langfuse'", specifier = ">=3.0.0" },@@ -3530,6 +3592,7 @@ requires-dist = [{ name = "sphinx", marker = "extra == 'doc'", specifier = ">=7.0.0" },{ name = "sphinx-rtd-theme", marker = "extra == 'doc'", specifier = ">=1.3.0" },{ name = "tabulate", specifier = ">=0.9.0" },+ { name = "tavily-python", marker = "extra == 'bot'", specifier = ">=0.5.0" },{ name = "tree-sitter", specifier = ">=0.23.0" },{ name = "tree-sitter-c-sharp", specifier = ">=0.23.0" },{ name = "tree-sitter-cpp", specifier = ">=0.23.0" },@@ -5601,6 +5664,20 @@ wheels = [{ url = "https://files.pythonhosted.org/packages/99/55/db07de81b5c630da5cbf5c7df646580ca26dfaefa593667fc6f2fe016d2e/tabulate-0.10.0-py3-none-any.whl", hash = "sha256:f0b0622e567335c8fabaaa659f1b33bcb6ddfe2e496071b743aa113f8774f2d3", size = 39814, upload-time = "2026-03-04T18:55:31.284Z" },]+[[package]]+name = "tavily-python"+version = "0.7.23"+source = { registry = "https://pypi.org/simple" }+dependencies = [+ { name = "httpx" },+ { name = "requests" },+ { name = "tiktoken" },+]+sdist = { url = "https://files.pythonhosted.org/packages/89/d1/197419d6133643848514e5e84e8f41886e825b73bf91ae235a1595c964f5/tavily_python-0.7.23.tar.gz", hash = "sha256:3b92232e0e29ab68898b765f281bb4f2c650b02210b64affbc48e15292e96161", size = 25968, upload-time = "2026-03-09T19:17:32.333Z" }+wheels = [+ { url = "https://files.pythonhosted.org/packages/64/27/f9c6e9249367be0772fb754849e03cbbc6ad8d80a479bf30ea8811828b2e/tavily_python-0.7.23-py3-none-any.whl", hash = "sha256:52ef85c44b926bce3f257570cd32bc1bd4db54666acf3105617f27411a59e188", size = 19079, upload-time = "2026-03-09T19:17:29.593Z" },+]+[[package]]name = "tenacity"version = "9.1.4"