Keysmith

MongoDB Store

MongoDB store for document-oriented and horizontally-scalable deployments.

The store/mongo package implements Keysmith's store.Store interface using the grove ORM with the MongoDB driver. It stores keys, policies, scopes, usage records, and rotations as documents, making it a natural fit for deployments that already run MongoDB.

Usage

import (
    "github.com/xraph/grove"
    "github.com/xraph/grove/drivers/mongodriver"
    "github.com/xraph/keysmith/store/mongo"
)

db, err := grove.Open(mongodriver.Open("mongodb://localhost:27017", "keysmith"))
if err != nil {
    log.Fatal(err)
}

s := mongo.New(db)
if err := s.Migrate(ctx); err != nil {
    log.Fatal(err)
}

eng, err := keysmith.NewEngine(keysmith.WithStore(s))

Migrations

The store creates collections with JSON Schema validation (auto-generated from Grove model structs) and applies indexes on all Keysmith collections. Run them on startup:

if err := s.Migrate(ctx); err != nil {
    log.Fatal(err)
}

This creates indexes across seven collections:

CollectionDescription
keysmith_keysAPI keys with hash, state, and metadata
keysmith_policiesPolicy definitions
keysmith_scopesScope definitions
keysmith_key_scopesKey-scope junction table
keysmith_usagePer-request usage records
keysmith_usage_aggAggregated usage (daily/monthly)
keysmith_rotationsRotation history records

Migrations are idempotent and safe to run on every startup.

Internals

AspectDetail
Drivergrove ORM + mongodriver
MigrationsGrove migrations with JSON Schema validation + indexes
TransactionsMongoDB sessions (replica-set required for multi-doc txns)
ConcurrencyMongoDB native concurrency model
Subsystemskeys, policies, scopes, usages, rotations

Health checks

if err := s.Ping(ctx); err != nil {
    log.Fatal("database unreachable:", err)
}

Usage with the engine

eng, err := keysmith.NewEngine(keysmith.WithStore(s))

Grove Migrations

The store exports a Migrations group for use with Grove's migration orchestrator. This enables tracked, versioned migrations across all stores in your application:

import mongostore "github.com/xraph/keysmith/store/mongo"

// mongostore.Migrations is a migrate.Group for the keysmith mongo store.
// Register it with the grove migration orchestrator for coordinated migrations.

When to use

ScenarioRecommended
Document-oriented workloadsYes
Horizontally-scaled environmentsYes
Teams already running MongoDBYes
Single-node developmentPossible -- SQLite may be simpler
Maximum SQL compatibilityNo -- use PostgreSQL

On this page