File size: 1,921 Bytes
ff8337e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Vault setup script — populates Supabase Vault with the service role key
at deployment / local-dev time instead of hardcoding it in migrations.

Usage:
    SUPABASE_URL=https://your-project.supabase.co \
    SUPABASE_SERVICE_KEY=eyJhbG... \
    python supabase/scripts/setup_vault.py

Requires: pip install supabase
"""

import os
import sys

SUPABASE_URL = os.environ.get("SUPABASE_URL")
SERVICE_KEY = os.environ.get("SUPABASE_SERVICE_KEY")

if not SUPABASE_URL or not SERVICE_KEY:
    print("ERROR: Both SUPABASE_URL and SUPABASE_SERVICE_KEY must be set.")
    sys.exit(1)


def main():
    try:
        from supabase import create_client
    except ImportError:
        print("ERROR: supabase package not installed. Run: pip install supabase")
        sys.exit(1)

    client = create_client(SUPABASE_URL, SERVICE_KEY)

    secret_name = "SUPABASE_SERVICE_ROLE_KEY"
    description = "Internal key for triggering edge functions from Postgres"

    response = client.table("vault.secrets").upsert(
        {
            "name": secret_name,
            "description": description,
            "secret": SERVICE_KEY,
        },
        on_conflict="name",
    ).execute()

    if hasattr(response, "error") and response.error:
        print(f"ERROR: Failed to upsert vault secret: {response.error}")
        sys.exit(1)

    print(f"vault.secrets '{secret_name}' upserted successfully.")

    # Also sync to internal_config.secrets for triggers that read from there
    try:
        response = client.table("internal_config.secrets").upsert(
            {
                "name": secret_name,
                "value": SERVICE_KEY,
            },
            on_conflict="name",
        ).execute()
        print(f"internal_config.secrets '{secret_name}' upserted successfully.")
    except Exception as e:
        print(f"Note: internal_config.secrets upsert skipped ({e})")


if __name__ == "__main__":
    main()