> ## Documentation Index
> Fetch the complete documentation index at: https://docs.lumenwipe.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Restore API keys

> Back up the API key store, restore it into a separate database, prove a staging service reads it, and check the infrastructure after a migration.

> [!NOTE]
>
> The point-in-time path was rehearsed on the production project on 2026-11-17, and the times below
> are from that drill. Restoring from the daily export is not yet rehearsed.

Self-serve API keys are stored only as hashes in the `apiKeys` Firestore collection, so a raw key can never be read back or reissued from the hash. If the collection is lost or corrupted, every self-serve key stops working at once and the only way back is a backup. Static keys in `API_KEYS` are not in Firestore and keep working ([Firestore outage](https://docs.lumenwipe.com/runbooks/firestore-outage.md) has the full blast radius).

The API reads the database named by `FIRESTORE_DATABASE_ID` in the project named by `FIRESTORE_PROJECT_ID`. Unset means `(default)`. An invalid id refuses the boot, so a typo leaves the previous revision serving, and each revision logs `Firestore database: <id>` at boot. A restore is always made into a new, separately named database and checked from a staging service before production is pointed at it.

## Recovery objective

| Path | Point-in-time window | Elapsed time (restore to key resolving) | Rehearsed on |
| - | - | - | - |
| Point-in-time clone | Last 7 days, by the minute | About 10 minutes (2026-11-17), of which the clone operation took 7 min 45 s | 2026-11-17 |
| Import from the daily export | Daily, kept 30 days | Not yet measured | Not yet |

The 2026-11-17 time includes one failed staging boot, from before the staging command below passed the mainnet endpoints.

## Backup configuration

Run once per project, and again after any project move. Placeholders: `<project>`, `<location>` (the location of the `(default)` database, from `gcloud firestore databases describe --database='(default)' --format='value(locationId)'`), `<export bucket>` and `<export account>` (a service account used only by the scheduled export).

1. **Turn on point-in-time recovery** (seven days of versions, readable by the minute):

   ```bash theme={null}
   gcloud firestore databases update --database='(default)' --enable-pitr --project=<project>
   gcloud firestore databases describe --database='(default)' --project=<project> --format='value(pointInTimeRecoveryEnablement,versionRetentionPeriod)'
   ```

   Expected: `POINT_IN_TIME_RECOVERY_ENABLED` and `604800s`. The oldest readable minute is `earliestVersionTime`, from the same `describe` with `--format='value(earliestVersionTime)'`; on 2026-11-17 it was seven days back.

2. **Create the export bucket**, private and in the same project, so only the project's own Firestore service agent writes to it:

   ```bash theme={null}
   gcloud storage buckets create gs://<export bucket> --project=<project> --location=<location> \
     --uniform-bucket-level-access --public-access-prevention
   echo '{"rule":[{"action":{"type":"Delete"},"condition":{"age":30}}]}' > lifecycle.json
   gcloud storage buckets update gs://<export bucket> --lifecycle-file=lifecycle.json
   ```

   On gcloud 586.0.0 (2026-11-17), both `gcloud storage buckets` commands crashed with `module 'ssl' has no attribute 'PROTOCOL_TLSv1'`. The same bucket with `gsutil`:

   ```bash theme={null}
   gsutil mb -p <project> -l <location> -b on --pap enforced gs://<export bucket>
   gsutil lifecycle set lifecycle.json gs://<export bucket>
   ```

   Grant nobody else access to the bucket. Expected from `gcloud storage buckets describe gs://<export bucket>`: `public_access_prevention: enforced`, `uniform_bucket_level_access: true`, and the 30 day delete rule.

3. **Schedule the daily export** of the `apiKeys` collection:

   ```bash theme={null}
   gcloud projects add-iam-policy-binding <project> \
     --member=serviceAccount:<export account> --role=roles/datastore.importExportAdmin
   gcloud scheduler jobs create http firestore-export-api-keys --project=<project> --location=<location> \
     --schedule='0 3 * * *' --time-zone=Etc/UTC --http-method=POST \
     --uri='https://firestore.googleapis.com/v1/projects/<project>/databases/(default):exportDocuments' \
     --message-body='{"outputUriPrefix":"gs://<export bucket>","collectionIds":["apiKeys"]}' \
     --oauth-service-account-email=<export account>
   ```

   Run it once by hand with `gcloud scheduler jobs run firestore-export-api-keys --location=<location>`. Expected after a minute: `gcloud storage ls gs://<export bucket>` lists a folder named by the export's start time.

4. **Confirm audit logging** for Firestore: in the project's audit log settings, the Cloud Firestore API has Data Write (and Admin Read) on. Expected from `gcloud projects get-iam-policy <project> --format=json | jq '.auditConfigs'`: an entry for `datastore.googleapis.com` listing `DATA_WRITE`.

### Service account permissions

The API's runtime service account holds only what the API needs:

| Role | Granted on | Why |
| - | - | - |
| `roles/datastore.user` | The project | Read and write keys, usage and the counter |
| `roles/secretmanager.secretAccessor` | Each secret the service mounts only | Read its own secrets at boot, never others |

No other project role. The `service-account` check of the [migration check](#migration-check) fails when the runtime account holds any other project role.

## Restore

Pick a new database id for the restore, such as `keys-restore-<yyyymmdd>`: 4 to 63 lowercase letters, digits or hyphens, starting with a letter. Never restore over `(default)`. Start a timer: the elapsed time until a key resolves is the recovery time to record.

### From point-in-time recovery

Use this when the loss happened in the last seven days. Pick the last whole minute before the loss, in UTC:

```bash theme={null}
gcloud firestore databases clone --project=<project> \
  --source-database='projects/<project>/databases/(default)' \
  --snapshot-time=<yyyy-mm-ddThh:mm:00Z> --destination-database=<restore id>
```

The clone holds every collection as of that minute: keys, usage and the counter. To restore only the keys, export them as of that minute and import them as below:

```bash theme={null}
gcloud firestore export gs://<export bucket>/pitr-<yyyymmdd> --project=<project> \
  --database='(default)' --snapshot-time=<yyyy-mm-ddThh:mm:00Z> --collection-ids=apiKeys
```

### From the daily export

Use this when the loss is older than seven days or point-in-time recovery is unavailable. Pick the newest export folder from before the loss:

```bash theme={null}
gcloud storage ls gs://<export bucket>
gcloud firestore databases create --project=<project> --database=<restore id> \
  --location=<location> --type=firestore-native
gcloud firestore import gs://<export bucket>/<export folder> --project=<project> \
  --database=<restore id> --collection-ids=apiKeys
```

Expected: the import operation ends `SUCCESSFUL`. Only `apiKeys` is in the export, so usage and the counter start empty in this database.

### Point a staging service at it

A separate service, so production's configuration and traffic are not touched. Use the image production serves now and the same runtime service account. A production boot refuses to start without explicit mainnet endpoints (`STELLAR_RPC_MAINNET is not set ... A production boot requires an explicit mainnet endpoint.`), so the staging service gets production's `STELLAR_RPC_MAINNET` and `PATH_ROUTING_API_MAINNET`. They go through a file, not `--set-env-vars`, because `STELLAR_RPC_MAINNET` can be a comma-separated list. Their URLs can carry provider keys: create the file readable only by you, never paste its contents anywhere, and delete it after the deploy:

```bash theme={null}
gcloud run services describe lumenwipe-api --region us-central1 --format='value(spec.template.spec.containers[0].image,spec.template.spec.serviceAccountName)'
umask 077
{
  echo 'FIRESTORE_PROJECT_ID: "<project>"'
  echo 'FIRESTORE_DATABASE_ID: "<restore id>"'
  gcloud run services describe lumenwipe-api --region us-central1 --format=json \
    | jq -r '.spec.template.spec.containers[0].env[] | select(.name == "STELLAR_RPC_MAINNET" or .name == "PATH_ROUTING_API_MAINNET") | "\(.name): \(.value | @json)"'
} > restore-env.yaml
grep -c MAINNET restore-env.yaml
gcloud run deploy lumenwipe-api-restore --region us-central1 --image <image> \
  --service-account <runtime account> --no-allow-unauthenticated --max-instances=1 \
  --env-vars-file restore-env.yaml --set-secrets API_KEYS=<API_KEYS secret>:latest
rm restore-env.yaml
gcloud run services proxy lumenwipe-api-restore --region us-central1 --port 8081
```

Expected: `grep` prints `2` (if it prints less, production binds the endpoint from a secret: pass it with `--set-secrets` instead), and the deploy ends with the service ready.

The proof that staging reads the restore, not production's database, is a pair of testnet test keys, never an integrator's: `$KEY_BEFORE` created before the restore point and `$KEY_AFTER` created after it. In a second terminal:

```bash theme={null}
curl -s -H "Authorization: Bearer $KEY_BEFORE" http://localhost:8081/v1/testnet/close/status
curl -s -o /dev/null -w '%{http_code}\n' -H "Authorization: Bearer $KEY_AFTER" http://localhost:8081/v1/testnet/close/status
gcloud logging read 'resource.type="cloud_run_revision" AND resource.labels.service_name="lumenwipe-api-restore" AND jsonPayload.message:"Firestore database"' --freshness=15m --limit=1 --format='value(jsonPayload.message)'
```

Expected: `{"error":{"code":"invalid_source",...}}` for the key from before, which the API only answers once the key has resolved, and `401` for the key from after, which still resolves on production. The boot log line is `Firestore database: <restore id>`; the API writes it as JSON, so the filter reads `jsonPayload.message`, not `textPayload`. Stop the timer and record the elapsed time and the date in the [recovery objective](#recovery-objective).

### Point production at it

Only after the staging check passes, during a real loss:

```bash theme={null}
gcloud run services update lumenwipe-api --region us-central1 --update-env-vars FIRESTORE_DATABASE_ID=<restore id>
```

If traffic is pinned to one revision, this does not reach users until traffic follows the latest revision again ([Runbooks, conventions](https://docs.lumenwipe.com/runbooks/overview.md#conventions)). Expected: the boot log names `<restore id>`, and a self-serve key resolves against `$API`.

### Clean up after a drill

The clone inherits delete protection from `(default)`, so a delete alone fails with `FAILED_PRECONDITION ... DELETE_PROTECTION_ENABLED`. Turn it off on the restore database only, then delete:

```bash theme={null}
gcloud run services delete lumenwipe-api-restore --region us-central1
gcloud firestore databases update --database=<restore id> --no-delete-protection --project=<project>
gcloud firestore databases delete --database=<restore id> --project=<project>
```

## Migration check

`scripts/migration-check.sh` is the required step after any project or organization move, and after any IAM, policy or service change. It is read-only: it only describes, lists and reads (`gcloud` describe, list and get-iam-policy, `gh run list`, `gh api` GET, `curl` GET). It prints PASS, FAIL or SKIPPED per check and exits non-zero on any failure.

| Check | Passes when |
| - | - |
| `health` | `/health` answers on the platform URL and the custom domain, and `/health/deep` on the domain |
| `ingress-invoker` | The service's ingress and its `roles/run.invoker` binding are the expected ones |
| `deploy-pipeline` | The latest manual run of the deploy workflow succeeded (its date is printed), and the trust condition names this repo |
| `revision` | The latest revision booted (it read its secrets) and the service is ready |
| `service-account` | The runtime account holds exactly the expected project roles |
| `test-key` | A testnet test key resolves, which reads Firestore |
| `domain` | The custom domain mapping is ready; the certificate expiry is printed |
| `billing` | Billing is enabled and a budget alerts at 70 percent of its amount ([provider quotas](https://docs.lumenwipe.com/runbooks/provider-quotas.md)) |
| `logging` | The `_Default` bucket keeps logs long enough and the `_Required` and `_Default` sinks are on |
| `alerting` | The on-chain monitors workflow, which carries the alerts, is active and its latest scheduled run succeeded within 24 hours; any Cloud Monitoring alert policy is enabled and routed (none is fine); uptime checks are counted |
| `org-policies` | The project's parent and the organization policy constraints in force can be read; both are listed |

Install the beta component once with `gcloud components install beta` (the `domain` check needs it; the script disables prompts, so a missing component fails that check and names the command). Then run it signed in to the project, after a manual run of the deploy workflow:

```bash theme={null}
GCP_PROJECT_ID=<project> GCP_REGION=us-central1 SERVICE=lumenwipe-api API_DOMAIN=<api domain> \
  GITHUB_REPOSITORY=LumenWipe/lumenwipe WIF_PROVIDER=<provider resource name> \
  CHECK_API_KEY=<testnet test key> ALERT_CHANNEL=<notification channel> \
  scripts/migration-check.sh
```

Expected: `11 passed, 0 failed, 0 skipped`. A check whose input is unset is SKIPPED. The header of the script lists every input and its default, and how to add a check. To prove the gate fails, run it with `DRILL=fail-check`: `ingress-invoker` expects a wrong member, fails, and the run exits `1`.

The **Migration check** workflow runs the same script by hand from GitHub Actions, as the deploy identity. Checks that identity is not allowed to read are reported as SKIPPED; run the script as an operator for the full list.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.