#!/usr/bin/env python3 """Test script to debug API key decryption issue.""" from src.core.database import SessionLocal from src.models.llm import LLMProvider from cryptography.fernet import Fernet import os # Get the encryption key key = os.getenv("ENCRYPTION_KEY", "ZcytYzi0iHIl4Ttr-GdAEk117aGRogkGvN3wiTxrPpE=").encode() print(f"Encryption key (first 20 chars): {key[:20]}") print(f"Encryption key length: {len(key)}") # Create Fernet instance fernet = Fernet(key) # Get provider from database db = SessionLocal() provider = db.query(LLMProvider).filter(LLMProvider.id == '6c899741-4108-4196-aea4-f38ad2f0150e').first() if provider: print(f"\nProvider found:") print(f" ID: {provider.id}") print(f" Name: {provider.name}") print(f" Encrypted API Key (first 50 chars): {provider.api_key[:50]}") print(f" Encrypted API Key Length: {len(provider.api_key)}") # Test decryption print(f"\nAttempting decryption...") try: decrypted = fernet.decrypt(provider.api_key.encode()).decode() print(f"Decryption successful!") print(f" Decrypted key length: {len(decrypted)}") print(f" Decrypted key (first 8 chars): {decrypted[:8]}") print(f" Decrypted key is empty: {len(decrypted) == 0}") except Exception as e: print(f"Decryption failed with error: {e}") print(f"Error type: {type(e).__name__}") import traceback traceback.print_exc() else: print("Provider not found") db.close()