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

# Deploying Latchkey

> Choose between service and embedded mode, set up the database boundary, and see how Latchkey runs in Galaxy Cloud.

One Go module runs either as a standalone service or inside a product.
Products, annotations, the API and the data model are the same in both, and
the mode is a configuration choice. Neither mode is tied to Galaxy. Any team
can run the service for its own products, and any Go service can embed it.

## Service or embedded

| | Service mode | Embedded mode |
| - | - | - |
| Use for | Several products share one policy, or a team runs it as its own service, as Galaxy Cloud does | One Go product owns the policy, as standalone Filament does |
| Deploy | A separate binary, with 2 replicas in Galaxy Cloud and 1 elsewhere by default | Nothing extra, each product replica holds a snapshot |
| Check | A network call plus a short decision cache | A function call, and the snapshot is the cache |
| If Latchkey fails | Callers deny | It fails only with the product |
| Upgrades | Independent of products | Ship with product releases |
| Cleanup after a delete | The `PurgeResource` RPC after the delete | `postgres.PurgeResource` inside the product's delete transaction on Postgres, and a purge after the delete on SQLite |

On Postgres, moving from embedded to service mode is a dump and restore of
the `latchkey` schema, then switching the product's `LATCHKEY_MODE` to
`remote`. Embedded SQLite keeps its data in `latchkey.db`, which has no
schema to dump, so `latchkey export` copies it into the service's Postgres.

**1. Stop the product**

The export reads `latchkey.db` as the product left it, so nothing may
write to it.

**2. Prepare the database**

Run `install.sql` and migrate with the same Latchkey version the product
embeds. The export refuses a database at another migration, or one that
already holds a policy. A service may already run against it, with the
owners it wrote from `LATCHKEY_DOMAINS`, which the export keeps. Start it
without `LATCHKEY_BOOTSTRAP_TENANT`, since a tenant it creates makes the
export refuse.

**3. Export**

Connect as `latchkey_app`, with the password in `PGPASSWORD` rather than
the command line.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
PGPASSWORD=... latchkey export --from sqlite:data/latchkey.db --to postgres://latchkey_app@db/filament
```

It copies every domain, kind, tenant, subject, role, group, binding and
audit entry in one transaction, with their revisions and timestamps. A
running service picks it up by its next poll, which runs every 5 seconds.

**4. Switch the product**

Start the service, then set the product's `LATCHKEY_MODE` to `remote`
with `LATCHKEY_ADDR` pointing at it, and start the product.

## Server image

Each release publishes the server as `ghcr.io/galaxy-io/latchkey/server`,
tagged with the version, such as `0.1.0`, with `latest` and with the commit,
such as `sha-1a2b3c4`, for amd64 and arm64. The image holds one static
binary with the admin UI embedded and listens on port 8080. It runs as user 65532, not root, so a
volume for a SQLite database must be writable by that user. Run it with
`-migrate` to apply migrations and exit.

`GET /version` on the server answers with its version, commit and build
date, without a token.

## Kubernetes

Each release also publishes a Helm chart for service mode.

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
helm upgrade --install latchkey oci://ghcr.io/galaxy-io/charts/latchkey \
  --set existingSecret=latchkey-db \
  --set identity.serviceIssuer=https://oidc.eks.us-east-1.amazonaws.com/id/EXAMPLE \
  --set identity.oidc.audience=latchkey
```

It runs the server and a migration Job that runs before every install and
upgrade. The Job connects as `latchkey_owner` and the server as
`latchkey_app`, so the Secret holds both connection strings, as
`postgres-owner-dsn` and `postgres-dsn`. Only the Job reads the owner one.
Run `install.sql` once before the first install. The chart's README lists
every value.

## Galaxy Cloud

* Each tenant namespace runs one instance in service mode. It has two
  replicas with a PodDisruptionBudget of `maxUnavailable: 1`, and its database
  lives on the tenant's existing RDS. Filament and Alchemy call the instance
  in their own namespace.
* The edge validates the session and calls the Management Service's
  `ResolveCaller` for the caller and tenant. It forwards the request with a
  caller assertion it signs for the target service, and Latchkey verifies
  that assertion the same way products do. The edge never calls Latchkey.
* Network policy lets only the tenant's product pods and the Management
  Service reach the instance.
* Management puts the tenant id and the tenant creator's platform id in the
  deployment intent. Latchkey reads them from `LATCHKEY_BOOTSTRAP_TENANT` and
  `LATCHKEY_BOOTSTRAP_ADMIN`, and on first boot provisions the tenant and binds
  the creator to Admin. No provisioning step waits for Latchkey to come up.
* Identity, tenants and provisioning are specified in the Management Service
  proposal. Latchkey replaces that proposal's Permissions section, so
  Management stores no roles or grants and its Caller carries no grants.
* Galaxy Cloud sets resource requests for each instance, and a fleet upgrade
  rolls tenant by tenant. Migrations stay compatible with the previous
  release, so tenants can run mixed versions during the rollout.

## Where identities come from

In V1, users and groups arrive through directory sync from the tenant's
directory owner, which calls `DirectoryService`. In Galaxy Cloud that owner is
the Management Service, which takes SCIM from the IdP itself. A user can also
be created just in time the first time it is checked, and Admins can add
subjects by IdP id.

After V1, standalone installs get a SCIM 2.0 endpoint at `/scim/v2/Users` and
`/scim/v2/Groups` on the same HTTP server. It is plain REST, not Connect, and
it authenticates with its own bearer token, in embedded mode too.

## Database boundary

In both modes the Latchkey tables live in their own `latchkey` schema behind
their own Postgres roles, so product code cannot read or write them. The only
crossing is `latchkey.purge_resource()`.

| Role | Login | Privileges |
| - | - | - |
| `latchkey_owner` | Yes | Owns the `latchkey` schema and every object in it, and runs migrations and audit pruning |
| `latchkey_app` | Yes | The runtime role, with read and write on policy tables, read and insert on the audit log but never update or delete, and no DDL |
| `latchkey_purger` | No | `EXECUTE` on `latchkey.purge_resource` only, granted to each product role that shares the database, such as `filament_app` |

* Create the schema and roles once with `datastore/postgres/install.sql`.
* Migrations are goose files under `datastore/postgres/migrations`. The
  server applies them with `-migrate` and exits, run as a separate step such
  as a Helm pre-upgrade hook with owner credentials. A Postgres advisory
  lock keeps two runners from racing.
* The runtime connects as `latchkey_app`. It refuses to start if the schema
  is behind the version it expects. A newer schema is fine, since every
  migration stays compatible with the previous release.
* Every migration stays compatible with the previous release. Expand now and
  contract one release later, so old replicas keep running during an upgrade
  and `helm rollback` works. Never edit a shipped migration. Append a new
  numbered file.
* Simple installs such as Docker Compose can migrate at startup by giving the
  process owner credentials.
* Embedded installs follow the host's migration step. The library exposes
  `postgres.Migrate(ctx, ownerDB)`. Filament runs its Postgres migrations from a
  Helm post-upgrade job and calls it from there, never at boot.

### SQLite

Filament's standalone mode runs everything in one process over SQLite, so
embedded mode ships a SQLite store too.

* The Latchkey data lives in its own file, `latchkey.db`, next to the
  product's. SQLite has no schemas or roles, so the boundary is the separate
  file and the package API.
* Cleanup cannot share the delete's transaction, because commits across two
  SQLite files in WAL mode are not atomic. The interceptor purges after the
  delete commits. A periodic sweep that removes bindings whose resource no
  longer exists, for products that implement `ListResources`, is planned.
  Filament soft deletes rows and uses UUIDs, so a leftover binding never
  grants anything.
* Migrations are goose files under `datastore/sqlite/migrations`. The
  product calls `Migrate` at startup, since it owns the file. The audit log
  refuses updates, and with no partitions, pruning deletes rows older than
  the retention.


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