mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-02 01:48:08 +09:00
## Summary - Two-tier disaster recovery: GCP-native protections (PITR, versioning, deny policies) + cross-cloud AWS S3 backups - Nightly Cloud Function exports Firestore, BigQuery, and GCS job files to AWS S3 - Read-only Claude SA (`claude-readonly`) and break-glass SA for emergency access - IAM deny policy blocking `buckets.delete` and `datasets.delete` for all principals - External backup freshness monitor (AWS Lambda alerts if backups go stale >48h) ## Changes **Infrastructure (Pulumi):** - `modules/iam/claude_readonly_sa.py` — new read-only + break-glass SAs - `modules/backup.py` — backup SA, staging bucket, Cloud Function, Cloud Scheduler - `modules/database.py` — enable Firestore PITR - `modules/storage.py` — enable GCS versioning + soft delete lifecycle - `modules/secrets.py` — add `discord-alert-webhook` secret - `__main__.py` — wire new modules **Backup Cloud Function:** - `functions/backup_to_aws/` — Firestore export, BigQuery export (weekly/monthly), GCS delta sync, S3 streaming upload, Discord alerts **Documentation:** - `docs/DISASTER-RECOVERY.md` — restore runbook for accidental deletion + catastrophic GCP loss - `docs/archive/2026-03-27-business-continuity-design.md` — full design spec - `docs/archive/2026-03-29-business-continuity-plan.md` — implementation plan - `docs/archive/2026-03-29-external-services-config.md` — OAuth/DNS/external service reference ## Already deployed All infrastructure has been applied via `pulumi up`. First backup run completed successfully (Firestore + job files in S3). Nightly backups running at 1 AM ET. ## Test plan - [x] Pulumi preview — 30 creates, 2 updates, 0 deletes - [x] Pulumi up — all resources deployed - [x] Manual backup function trigger — Firestore export (1.4 GB) landed in S3 - [x] GCS job files sync — 161 files (2.9 GB) in S3 - [x] AWS Lambda freshness check — returned `ok` (3h age) - [x] IAM deny policy verified active via `gcloud iam policies get` @coderabbitai ignore 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
304 lines
13 KiB
Python
304 lines
13 KiB
Python
"""
|
|
Firestore database resources.
|
|
|
|
Manages the Firestore database, composite indexes, and TTL configuration.
|
|
"""
|
|
|
|
import pulumi
|
|
import pulumi_gcp as gcp
|
|
from pulumi_gcp import firestore
|
|
|
|
from config import PROJECT_ID
|
|
|
|
|
|
def create_database() -> dict:
|
|
"""
|
|
Create Firestore database and all composite indexes.
|
|
|
|
Returns:
|
|
dict: Dictionary containing all created resources for export.
|
|
"""
|
|
resources = {}
|
|
|
|
# ==================== Firestore Database ====================
|
|
firestore_db = firestore.Database(
|
|
"karaoke-firestore",
|
|
project=PROJECT_ID,
|
|
name="(default)",
|
|
location_id="us-central1",
|
|
type="FIRESTORE_NATIVE",
|
|
concurrency_mode="PESSIMISTIC",
|
|
app_engine_integration_mode="DISABLED",
|
|
point_in_time_recovery_enablement="POINT_IN_TIME_RECOVERY_ENABLED",
|
|
)
|
|
resources["firestore_db"] = firestore_db
|
|
|
|
# ==================== Firestore Composite Indexes ====================
|
|
# These are required for queries that filter/sort on multiple fields.
|
|
|
|
# Jobs: query by user_email, order by created_at (for user job lists)
|
|
resources["firestore_index_jobs_user_email"] = firestore.Index(
|
|
"firestore-index-jobs-user-email",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="jobs",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="user_email", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Jobs: query by status, order by created_at (for admin/system job lists)
|
|
resources["firestore_index_jobs_status"] = firestore.Index(
|
|
"firestore-index-jobs-status",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="jobs",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="status", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Jobs: query by tenant_id, order by created_at (for tenant portal job lists)
|
|
resources["firestore_index_jobs_tenant"] = firestore.Index(
|
|
"firestore-index-jobs-tenant",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="jobs",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="tenant_id", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Jobs: query by user_email + tenant_id, order by created_at (for tenant user job lists)
|
|
resources["firestore_index_jobs_user_tenant"] = firestore.Index(
|
|
"firestore-index-jobs-user-tenant",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="jobs",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="user_email", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="tenant_id", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Jobs: query by customer_email + made_for_you, order by created_at
|
|
# Used for finding existing made-for-you orders for a customer
|
|
resources["firestore_index_jobs_customer_mfy"] = firestore.Index(
|
|
"firestore-index-jobs-customer-mfy",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="jobs",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="customer_email", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="made_for_you", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Sessions: query by user_email + is_active, order by created_at
|
|
resources["firestore_index_sessions_active"] = firestore.Index(
|
|
"firestore-index-sessions-active",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="sessions",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="is_active", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="user_email", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Magic links: query by email, order by created_at
|
|
resources["firestore_index_magic_links"] = firestore.Index(
|
|
"firestore-index-magic-links",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="magic_links",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="email", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Gen Users: query by is_active, order by created_at
|
|
resources["firestore_index_gen_users_active_created"] = firestore.Index(
|
|
"firestore-index-gen-users-active-created",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="gen_users",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="is_active", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Gen Users: query by is_active, order by last_login_at
|
|
resources["firestore_index_gen_users_active_login"] = firestore.Index(
|
|
"firestore-index-gen-users-active-login",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="gen_users",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="is_active", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="last_login_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Gen Users: query by is_active, order by credits
|
|
resources["firestore_index_gen_users_active_credits"] = firestore.Index(
|
|
"firestore-index-gen-users-active-credits",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="gen_users",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="is_active", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="credits", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Gen Users: query by is_active, order by email
|
|
resources["firestore_index_gen_users_active_email"] = firestore.Index(
|
|
"firestore-index-gen-users-active-email",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="gen_users",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="is_active", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="email", order="ASCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Gen Users: query by signup_ip, order by created_at (anti-abuse investigation)
|
|
resources["firestore_index_gen_users_signup_ip"] = firestore.Index(
|
|
"firestore-index-gen-users-signup-ip",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="gen_users",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="signup_ip", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Gen Users: query by device_fingerprint, order by created_at (anti-abuse investigation)
|
|
resources["firestore_index_gen_users_device_fingerprint"] = firestore.Index(
|
|
"firestore-index-gen-users-device-fingerprint",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="gen_users",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="device_fingerprint", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Stripe Payments: query by customer_email, order by created_at
|
|
# Used for admin user detail page payment history
|
|
resources["firestore_index_stripe_payments_email"] = firestore.Index(
|
|
"firestore-index-stripe-payments-email",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="stripe_payments",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="customer_email", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="created_at", order="DESCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Audio separation jobs: query by status + updated_at (for cleanup of old jobs)
|
|
resources["firestore_index_audio_separation_jobs_cleanup"] = firestore.Index(
|
|
"firestore-index-audio-separation-jobs-cleanup",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="audio_separation_jobs",
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="status", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="updated_at", order="ASCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# ==================== Firestore TTL Field Configuration ====================
|
|
# TTL policies automatically delete documents after expiration.
|
|
|
|
# Worker logs TTL: logs subcollection documents expire after 30 days
|
|
resources["firestore_field_logs_ttl"] = firestore.Field(
|
|
"firestore-field-logs-ttl",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="logs", # Subcollection name (applies to all jobs/{job_id}/logs)
|
|
field="ttl_expiry",
|
|
ttl_config={}, # Empty block enables TTL based on ttl_expiry field
|
|
index_config={}, # Disable indexing on TTL field
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# Search sessions TTL: short-lived sessions from guided job creation flow expire after 30 minutes
|
|
# Sessions are created by POST /api/audio-search/search-standalone and consumed by
|
|
# POST /api/jobs/create-from-search. The TTL is a safety net for abandoned sessions.
|
|
resources["firestore_field_search_sessions_ttl"] = firestore.Field(
|
|
"firestore-field-search-sessions-ttl",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="search_sessions",
|
|
field="ttl_expiry",
|
|
ttl_config={}, # Empty block enables TTL based on ttl_expiry field
|
|
index_config={}, # Disable indexing on TTL field
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
# ==================== Review Sessions Collection Group Index ====================
|
|
|
|
# The cross-job session search uses a collection group query on
|
|
# review_sessions ordered by updated_at DESC. Firestore requires a
|
|
# single-field index exemption with COLLECTION_GROUP scope for this.
|
|
# Created via REST API (gcloud doesn't support query-scope for field exemptions):
|
|
#
|
|
# curl -X PATCH \
|
|
# "https://firestore.googleapis.com/v1/projects/nomadkaraoke/databases/(default)/collectionGroups/review_sessions/fields/updated_at" \
|
|
# -H "Authorization: Bearer $(gcloud auth print-access-token)" \
|
|
# -H "Content-Type: application/json" \
|
|
# -d '{"indexConfig":{"indexes":[
|
|
# {"queryScope":"COLLECTION","fields":[{"fieldPath":"updated_at","order":"ASCENDING"}]},
|
|
# {"queryScope":"COLLECTION","fields":[{"fieldPath":"updated_at","order":"DESCENDING"}]},
|
|
# {"queryScope":"COLLECTION_GROUP","fields":[{"fieldPath":"updated_at","order":"DESCENDING"}]}
|
|
# ]}}'
|
|
#
|
|
# Pulumi doesn't have a native resource for single-field index
|
|
# exemptions — manage via gcloud/REST if it needs recreation.
|
|
|
|
# ==================== Firestore Indexes for Logs Subcollection ====================
|
|
|
|
# Logs: query by worker, order by timestamp
|
|
resources["firestore_index_logs_worker_timestamp"] = firestore.Index(
|
|
"firestore-index-logs-worker-timestamp",
|
|
project=PROJECT_ID,
|
|
database=firestore_db.name,
|
|
collection="logs", # Subcollection name
|
|
fields=[
|
|
firestore.IndexFieldArgs(field_path="worker", order="ASCENDING"),
|
|
firestore.IndexFieldArgs(field_path="timestamp", order="ASCENDING"),
|
|
],
|
|
opts=pulumi.ResourceOptions(depends_on=[firestore_db]),
|
|
)
|
|
|
|
return resources
|