CoolFace
Apppublic

malepati/custom_template_working

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
sync_secrets_to_config.py92 linesDownload Raw Back to root
1import json2import os3import sys4 5# Add the current directory to sys.path to import retrieve_secret6current_dir = os.path.dirname(os.path.abspath(__file__))7sys.path.append(current_dir)8 9try:10    from retrieve_secret import get_secret11except ImportError:12    print("Error: Could not import retrieve_secret. Make sure retrieve_secret.py is in the same directory.")13    sys.exit(1)14 15def update_user_config():16    print("Fetching secrets...")17    secret_name = "Demo/MR/skeys"18    region_name = "us-east-1"19    20    secrets = get_secret(secret_name, region_name)21    22    if not secrets:23        print("Failed to retrieve secrets.")24        return25 26    # Unpack secrets (retrieve_secret.py returns a tuple)27    (28        decrypted_data,29        CONNECTIONS_HOST,30        CONNECTIONS_DB,31        CONNECTIONS_USER,32        CONNECTIONS_PASS,33        OPENAI_API_KEY,34        OPENAI_MODEL_NAME,35        OPENAI_MODEL_TURBO,36        REDIS_HOST,37        REDIS_PORT,38        REDIS_DB,39        REDIS_PASSWORD,40        REDIS_USER,41        QDRANT_HOST,42        QDRANT_PORT,43        QDRANT_COLLECTION_NAME,44        QDRANT_URL,45        QDRANT_API_KEY,46        REDIS_URL,47        BUCKET_NAME,48        BUCKET_UNSTRUCTURED_STORAGE,49        AWS_S3_CREDS_KEY_ID,50        AWS_S3_CREDS_SECRET_KEY,51        ALLOWED_HOSTS,52        GEMINI_API_KEY,53        QDRANT_HOST_AWS54    ) = secrets55 56    # Path to user_config.json57    config_path = os.path.join(current_dir, "servers", "nextjs", "app_data", "user_config.json")58    59    # Ensure directory exists60    os.makedirs(os.path.dirname(config_path), exist_ok=True)61 62    config_data = {}63    if os.path.exists(config_path):64        try:65            with open(config_path, 'r') as f:66                config_data = json.load(f)67        except json.JSONDecodeError:68            print("Warning: Existing user_config.json is invalid. Starting fresh.")69 70    # Update keys71    print(f"Injecting OPENAI_API_KEY: {OPENAI_API_KEY[:5]}...")72    config_data["OPENAI_API_KEY"] = OPENAI_API_KEY73    74    # Check if GOOGLE_API_KEY exists in secrets (checking decrypted_data dict as not all unpacked vars explicitly cover it)75    if "GOOGLE_API_KEY" in decrypted_data:76         print(f"Injecting GOOGLE_API_KEY: {decrypted_data['GOOGLE_API_KEY'][:5]}...")77         config_data["GOOGLE_API_KEY"] = decrypted_data["GOOGLE_API_KEY"]78    79    if GEMINI_API_KEY:80         print(f"Injecting GEMINI_API_KEY (as GOOGLE_API_KEY fallback): {GEMINI_API_KEY[:5]}...")81         if "GOOGLE_API_KEY" not in config_data or not config_data["GOOGLE_API_KEY"]:82             config_data["GOOGLE_API_KEY"] = GEMINI_API_KEY83 84    # Write back85    with open(config_path, 'w') as f:86        json.dump(config_data, f, indent=2)87 88    print(f"Successfully updated {config_path}")89 90if __name__ == "__main__":91    update_user_config()92