From 98262025e01707d2f1a51358969f1c119482436f Mon Sep 17 00:00:00 2001 From: noahpodgurski Date: Fri, 23 Jan 2026 08:53:05 -0500 Subject: [PATCH] feat: add create and set virtual key script, update readme instructions --- README.md | 44 ++++++----------- scripts/create-and-set-virtual-key.py | 70 +++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 scripts/create-and-set-virtual-key.py diff --git a/README.md b/README.md index 6d0860d..04bd2f8 100644 --- a/README.md +++ b/README.md @@ -10,55 +10,43 @@ make setup This creates a virtual environment in `.venv/`, installs dependencies, and installs the tool locally in editable mode. -## Running MLPA locally with Docker +# Running MLPA locally with Docker -### Run LiteLLM +### Run LiteLLM and PostgreSQL -`docker compose -f litellm_docker_compose.yaml up -d` +1. `docker compose -f litellm_docker_compose.yaml up -d` ### Create and migrate appattest database -`sh ./scripts/create-app-attest-database.sh` -`alembic upgrade head` +2. `sh ./scripts/create-app-attest-database.sh` -### Set MLPA_DEBUG=true in the .env file -Verify FxA requests against staging URL +3. `alembic upgrade head` + +4. Set `MLPA_DEBUG=true` in the `config.py` or `.env` file + +### Create a virtual LiteLLM key + +5. Run `python scripts/create-and-set-virtual-key.py` (also sets the value in `.env`) ### Run MLPA -1. install it as a library +6. Install it as a library: ```bash pip install --no-cache-dir -e . ``` -2. Run the binary +7. Run the binary ```bash mlpa ``` -## Config (see [LiteLLM Documentation](https://docs.litellm.ai/docs/simple_proxy_old_doc) for more config options) - -`.env` (see `config.py` for all configuration variables) - -``` -MASTER_KEY="sk-1234..." -LITELLM_API_BASE="http://mlpa:4000" -DATABASE_URL=postgresql://... # required for direct user editing in SQL -CHALLENGE_EXPIRY_SECONDS=300 -PORT=8080 - -APP_BUNDLE_ID="org.example.app" -APP_DEVELOPMENT_TEAM="12BC943KDC" +Navigate to -CLIENT_ID="..." -CLIENT_SECRET="..." +## Config (see [LiteLLM Documentation](https://docs.litellm.ai/docs/simple_proxy_old_doc) for more config options) -MODEL_NAME="" -TEMPERATURE=0.1 -TOP_P=0.01 -``` +### See `config.py` for all configuration variables ### Also See `litellm_config.yaml` for litellm config diff --git a/scripts/create-and-set-virtual-key.py b/scripts/create-and-set-virtual-key.py new file mode 100644 index 0000000..218f0a7 --- /dev/null +++ b/scripts/create-and-set-virtual-key.py @@ -0,0 +1,70 @@ +""" +Run before testing MLPA locally +LiteLLM and Postgres must be configured and running +This creates a virtual API key for testing purposes. +""" + +import json +import os + +import requests +from dotenv import load_dotenv + +# Load environment variables from .env file if it exists +if os.path.exists(".env"): + load_dotenv(".env") + +# Set your authorization token here +AUTH_TOKEN = os.environ.get("MASTER_KEY") + +if not AUTH_TOKEN: + print("MASTER_KEY environment variable not set.") + exit(1) + +# Call the API and capture the response +url = "http://localhost:4000/key/generate" +headers = {"Content-Type": "application/json", "Authorization": "Bearer " + AUTH_TOKEN} +data = { + "user_id": "default_user_id", + "key_alias": "test-api", + "models": ["all-team-models"], +} + +response = requests.post(url, headers=headers, json=data) +if response.status_code != 200: + print(f"API call failed with status code {response.status_code}: {response.text}") + exit(1) + +try: + virtual_key = response.json().get("key") +except json.JSONDecodeError: + print("Failed to parse JSON response.") + exit(1) + +if not virtual_key: + print("Failed to extract key from response.") + exit(1) + +# Update or add MLPA_VIRTUAL_KEY in .env file +env_file = ".env" +lines = [] +found = False + +if os.path.exists(env_file): + with open(env_file, "r") as f: + for line in f: + if line.startswith("MLPA_VIRTUAL_KEY="): + lines.append(f"MLPA_VIRTUAL_KEY={virtual_key}\n") + found = True + else: + lines.append(line) +else: + lines = [] + +if not found: + lines.append(f"MLPA_VIRTUAL_KEY={virtual_key}\n") + +with open(env_file, "w") as f: + f.writelines(lines) + +print(f"MLPA_VIRTUAL_KEY set to {virtual_key} in {env_file}")