Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / cmd/api/main.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 4b8e7ce594c068621867f9c9deed0ec64ae6c553 |
| Branch | ws3-clearance |
| Path or archive member | cmd/api/main.go |
| Original SHA-256 | c95055a99055965eda7ed7830effcb222ae2e5c5d568cad1cc9c4b614ee26d65 |
| Git blob SHA | f4b5e295278ec0d41cd1d4a40f7a681a39a17fcb |
| Reading-copy SHA-256 | c95055a99055965eda7ed7830effcb222ae2e5c5d568cad1cc9c4b614ee26d65 |
Every recorded occurrence
These 9 records contain identical original source bytes. Their paths and revisions remain part of the archive.
| Original record | Branch or archive | Revision / identifier | Original location |
|---|---|---|---|
| Janady13/missionmatch / cmd/api/main.go | ws3-clearance | 4b8e7ce594c068621867f9c9deed0ec64ae6c553 | Open original |
| Janady13/missionmatch / cmd/api/main.go | ws7-federal-signal | 6a9f6ce9cfb13345ffb47770d4960e7c2d9c17c2 | Open original |
| Janady13/missionmatch / cmd/api/main.go | ws6-retrieval | 6c56e29231ec05735927fa265c97d517499dfeaf | Open original |
| Janady13/missionmatch / cmd/api/main.go | phase2-eval-fairness | d361af254701975fe239927656ababb56374655e | Open original |
| Janady13/missionmatch / cmd/api/main.go | ws2-alias-relationship | e4dada4601c6eb101830f9d223a9655dfa120616 | Open original |
| Janady13/missionmatch / cmd/api/main.go | ws6-retrieval | 6c56e29231ec05735927fa265c97d517499dfeaf | Open original |
| Janady13/missionmatch / cmd/api/main.go | ws3-clearance | 4b8e7ce594c068621867f9c9deed0ec64ae6c553 | Open original |
| Janady13/missionmatch / cmd/api/main.go | phase2-eval-fairness | d361af254701975fe239927656ababb56374655e | Open original |
| Janady13/missionmatch / cmd/api/main.go | ws7-federal-signal | 6a9f6ce9cfb13345ffb47770d4960e7c2d9c17c2 | Open original |
Recovered source transcript
Download complete source text ↓417 lines · 18.6 KB
// Command api is the missionMATCH HTTP server entrypoint.
//
// It wires configuration, the FIPS startup guard and self test, the Postgres pool (NOBYPASSRLS app
// role), the embedding provider, the authentication module, and the tenant scoped API. Cloud and
// provider selection are configuration (the portability mandate); no cloud SDK is imported here.
package main
import (
"context"
"crypto/fips140"
"crypto/pbkdf2"
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"
"log"
"net/http"
"os/signal"
"syscall"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/missionworkforce/missionmatch/internal/modules/ai"
"github.com/missionworkforce/missionmatch/internal/modules/auth"
"github.com/missionworkforce/missionmatch/internal/modules/awards"
"github.com/missionworkforce/missionmatch/internal/modules/commandcenter"
"github.com/missionworkforce/missionmatch/internal/modules/eeo"
"github.com/missionworkforce/missionmatch/internal/modules/governance"
"github.com/missionworkforce/missionmatch/internal/modules/matching"
"github.com/missionworkforce/missionmatch/internal/modules/missionmatch"
"github.com/missionworkforce/missionmatch/internal/modules/opportunities"
oppintel "github.com/missionworkforce/missionmatch/internal/modules/opportunity_intelligence"
"github.com/missionworkforce/missionmatch/internal/modules/screenmatch"
"github.com/missionworkforce/missionmatch/internal/modules/slatematch"
"github.com/missionworkforce/missionmatch/internal/modules/sourcing"
"github.com/missionworkforce/missionmatch/internal/modules/staffing"
"github.com/missionworkforce/missionmatch/internal/modules/wages"
"github.com/missionworkforce/missionmatch/internal/platform/audit"
"github.com/missionworkforce/missionmatch/internal/platform/config"
"github.com/missionworkforce/missionmatch/internal/platform/database"
"github.com/missionworkforce/missionmatch/internal/platform/extraction"
"github.com/missionworkforce/missionmatch/internal/platform/inference"
"github.com/missionworkforce/missionmatch/internal/platform/obs"
"github.com/missionworkforce/missionmatch/internal/platform/ratelimit"
"github.com/missionworkforce/missionmatch/internal/platform/security"
"github.com/missionworkforce/missionmatch/internal/platform/tenant"
)
func main() {
cfg, err := config.Load()
if err != nil {
log.Fatalf("config: %v", err)
}
// Structured JSON logging for the request path (debug level in dev, info in production): the obs
// middleware logs every request through slog. The one time boot and shutdown messages below stay on
// the standard logger, since they are operator facing startup output, not the high volume hot path.
obs.Setup(!cfg.IsProduction())
// FIPS startup guard: in a FIPS deployment, refuse to start with crypto outside the validated module.
if cfg.RequireFIPS && !fips140.Enabled() {
log.Fatalf("FIPS required but OFF, build with GOFIPS140=v1.0.0 and run with GODEBUG=fips140=on")
}
// FIPS self test: actually derive a PBKDF2 key at the configured cost under the active module and
// fail closed if it errors, so a broken crypto path is caught at boot, not at first login.
if err := fipsSelfTest(); err != nil {
log.Fatalf("FIPS self test (PBKDF2 HMAC SHA 256) failed: %v", err)
}
log.Printf("missionMATCH starting: env=%s cloud=%s fips_enabled=%v module=%s",
cfg.Env, cfg.Cloud, fips140.Enabled(), fips140.Version())
ctx := context.Background()
var db *tenant.DB
if cfg.AppDatabaseURL != "" {
pool, err := database.NewPool(ctx, cfg.AppDatabaseURL)
if err != nil {
log.Fatalf("db pool: %v", err)
}
// Fail closed unless this pool is the NOBYPASSRLS app role, so a swapped or over privileged DSN
// can never silently bypass RLS.
if err := database.AssertRole(ctx, pool, "missionmatch_app"); err != nil {
log.Fatalf("app pool role check: %v", err)
}
db = tenant.New(pool)
defer db.Close()
} else {
log.Printf("WARNING: MM_DATABASE_URL unset, DB backed routes return 503")
}
// The compliance pool authenticates as the compliance role and is the ONLY pool with eeo access.
// It drives adverse impact reporting; the app pool above can never read demographics.
var complianceDB *tenant.DB
if cfg.ComplianceDatabaseURL != "" {
pool, err := database.NewPool(ctx, cfg.ComplianceDatabaseURL)
if err != nil {
log.Fatalf("compliance db pool: %v", err)
}
// Fail closed unless this is the NOBYPASSRLS compliance role. A BYPASSRLS DSN here would leak
// every tenant's demographics cross org through the adverse impact join.
if err := database.AssertRole(ctx, pool, "missionmatch_compliance"); err != nil {
log.Fatalf("compliance pool role check: %v", err)
}
complianceDB = tenant.New(pool)
defer complianceDB.Close()
} else {
log.Printf("WARNING: MM_COMPLIANCE_DATABASE_URL unset, adverse impact reporting returns 503")
}
ext, err := extraction.New(cfg.Extraction)
if err != nil {
log.Fatalf("extraction: %v", err)
}
auditW := audit.New(db)
authSvc, err := buildAuthService(cfg, db, auditW)
if err != nil {
log.Fatalf("auth: %v", err)
}
csrf := security.NewCSRF(authKey(cfg.Auth.CSRFKey, 32, "MM_CSRF_KEY", cfg.IsProduction()))
authH := auth.NewHandler(authSvc, csrf, cfg.Auth.CookieSecure, cfg.AllowedOrigins, cfg.TrustedProxyHeader)
// The skill matcher reads the BACKFILLED skill embeddings (the HNSW indexed skills.embedding column)
// rather than embedding skill name strings at request time, so the vector layer is live in the scoring
// path and the fake default embedder is gone from it.
matcher := matching.NewChainedResolver(matching.NewDeterministicAliasResolver(db), matching.NewStoredSkillMatcher(db))
svc := matching.NewService(db, matcher, auditW).WithExtractor(ext)
h := matching.NewHandler(svc)
screenH := screenmatch.NewHandler(h)
slateH := slatematch.NewHandler(h)
ccH := commandcenter.NewHandler(commandcenter.NewServiceWithAudit(db, auditW))
missionH := missionmatch.NewHandler(h, ccH)
oppH := opportunities.NewHandler(opportunities.NewService(db))
oppIntelH := oppintel.NewHandler(oppintel.NewService(db))
awardsH := awards.NewHandler(awards.NewService(db))
wagesH := wages.NewHandler(wages.NewService(db))
sourcingH := sourcing.NewHandler(sourcing.NewService(db, auditW))
eeoH := eeo.NewHandler(eeo.NewService(complianceDB))
govH := governance.NewHandler(governance.NewService(db, auditW, cfg.DecisionRetentionDays))
// W3 staffing recommendations. It reuses the ONE engine path (the same skill matcher) and narrates only
// through the optional inference plane; a nil client means deterministic rationales, so the feature
// works even when the plane is down. Generating a run is a write capability; the deterministic engine
// stays the floor and a person decides every recommendation.
var inferClient *inference.Client
if cfg.Inference.URL != "" {
ic, ierr := inference.New(inference.Config{
BaseURL: cfg.Inference.URL, EmbedModel: cfg.Inference.EmbedModel, RerankModel: cfg.Inference.RerankModel,
GenerateModel: cfg.Inference.GenerateModel, Dimension: cfg.Inference.Dimension,
Timeout: time.Duration(cfg.Inference.TimeoutSeconds) * time.Second, MaxRetries: cfg.Inference.MaxRetries,
BreakerTrips: cfg.Inference.BreakerTrips, BreakerCool: time.Duration(cfg.Inference.BreakerCoolSec) * time.Second,
TeachingEnabled: cfg.Inference.TeachingEnabled,
})
if ierr != nil {
log.Fatalf("inference client: %v", ierr)
}
inferClient = ic
} else {
log.Printf("WARNING: MM_MEGAMIND_URL unset, staffing recommendation rationales are deterministic only")
}
staffingH := staffing.NewHandler(staffing.NewService(db, matcher, auditW, inferClient, staffing.Load()))
// The AI brain, mounted LIVE on the shared RLS database (per tenant pools are a sequenced follow on,
// out of scope here). It reuses the same inference client and audit writer and is memory augmented; a
// nil inference client makes every answer deterministic (the graceful no plane posture). The
// deterministic engine remains the floor: the brain narrates, it never scores.
brainH := ai.NewHandler(ai.NewOrchestrator(db, inferClient, auditW).WithMemory(ai.NewMemory(db, inferClient)))
// Fail the boot if any curated TAG intent is unsafe (a write, an unallowlisted table, or eeo), so an
// unsafe intent can never ship live, as the registry guarantee promises.
if err := ai.ValidateRegistry(); err != nil {
log.Fatalf("ai brain: unsafe TAG intent in the registry: %v", err)
}
// The enforced re embed gate. With a configured plane, /ai/query stays dark until a boot self check
// confirms the stored vectors were produced by the configured embed model (the all-minilm to bge
// corruption mode mis ranks every retrieval with no error). A proven mismatch keeps the brain route
// disabled with a typed 503 while the rest of the API serves normally; a boot time plane outage lets
// the gate self heal on the first query after the plane recovers. This is never a fatal boot error.
if inferClient != nil {
gate := ai.NewVectorGate(func(ctx context.Context) error {
return ai.VerifyVectorModel(ctx, db, inferClient, cfg.Inference.Dimension)
})
brainH = brainH.WithVectorGate(gate)
bootCtx, cancelBoot := context.WithTimeout(context.Background(), 30*time.Second)
switch err := gate.Check(bootCtx); {
case err == nil:
log.Printf("brain vector model check passed: stored vectors match %s", cfg.Inference.EmbedModel)
case errors.Is(err, ai.ErrVectorModelMismatch):
log.Printf("ERROR: brain vector model check FAILED, /ai/query is disabled until a re embed: %v", err)
default:
log.Printf("WARNING: brain vector model not yet verified at boot, /ai/query will retry on first use: %v", err)
}
cancelBoot()
}
// Resolve the tenant middleware. In a release build this is always the session resolver; the dev
// X-Org-ID path only exists in a dev tagged build. Assert that positively in production.
sessionMW, resolverKind := chooseResolver(cfg, authH)
if cfg.IsProduction() && resolverKind != "session" {
log.Fatalf("production must resolve the tenant from a validated session, got resolver %q", resolverKind)
}
log.Printf("tenant resolver: %s", resolverKind)
if !cfg.Auth.MFARequired {
log.Printf("WARNING: MM_MFA_REQUIRED is false, login is password only with no TOTP step (pilot only, this weakens the auth posture)")
}
csrfMW := csrf.Protect(authH.CSRFCookieName(), auth.CSRFHeader, cfg.AllowedOrigins)
// Metrics registry, fed the database pool gauges so /metrics reports connection saturation.
metrics := obs.NewRegistry()
metrics.SetPoolStats(func() map[string]float64 {
out := map[string]float64{}
if db != nil && db.Pool != nil {
s := db.Pool.Stat()
out["app_acquired"] = float64(s.AcquiredConns())
out["app_idle"] = float64(s.IdleConns())
out["app_total"] = float64(s.TotalConns())
out["app_max"] = float64(s.MaxConns())
}
if complianceDB != nil && complianceDB.Pool != nil {
out["compliance_total"] = float64(complianceDB.Pool.Stat().TotalConns())
}
return out
})
// Rate limiters: a generous per IP cap on the whole API, and a strict one on the public auth routes
// (login, CSRF, recovery) to blunt credential stuffing. Account lockout in the auth service is the
// deeper control; this is the transport layer defense in depth. Sweepers bound memory.
stopSweep := make(chan struct{})
defer close(stopSweep)
apiLimiter := ratelimit.New(50, 100)
apiLimiter.StartSweeper(stopSweep, time.Minute, 10*time.Minute)
authLimiter := ratelimit.New(1, 10)
authLimiter.StartSweeper(stopSweep, time.Minute, 10*time.Minute)
r := chi.NewRouter()
r.Use(middleware.RequestID)
// NOTE: middleware.RealIP is deliberately NOT used; it trusts spoofable client headers (deprecated
// in chi for that reason). The auth handler and the rate limiter derive the client IP from the TCP
// peer, or from MM_TRUSTED_PROXY_HEADER only when the app is behind a trusted reverse proxy.
r.Use(middleware.Recoverer)
r.Use(metrics.Middleware) // access log + metrics, outside the timeout so timed out requests still record
r.Use(middleware.Timeout(60 * time.Second))
// Liveness: the process is up. Cheap, never touches the database (a probe must not be coupled to the DB).
r.Get("/healthz", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
// Readiness: the process can serve, the database pools answer. A failing pool returns 503 so a load
// balancer stops routing to a node that cannot reach Postgres (the old /healthz reported healthy even
// with a dead DB).
r.Get("/readyz", func(w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithTimeout(req.Context(), 2*time.Second)
defer cancel()
if db != nil && db.Pool != nil {
if err := db.Pool.Ping(ctx); err != nil {
http.Error(w, "app database not ready", http.StatusServiceUnavailable)
return
}
}
if complianceDB != nil && complianceDB.Pool != nil {
if err := complianceDB.Pool.Ping(ctx); err != nil {
http.Error(w, "compliance database not ready", http.StatusServiceUnavailable)
return
}
}
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ready"))
})
r.Get("/healthz/fips", func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"fips_enabled": fips140.Enabled(),
"fips_enforced": fips140.Enforced(),
"module": fips140.Version(),
"pbkdf2_self_test": true, // a failure would have aborted startup
})
})
// Metrics are unauthenticated operational data carrying no PII; production firewalls this to the
// scraping network. It sits outside /api so the API rate limiter never throttles a scrape.
r.Handle("/metrics", metrics.Handler())
r.Route("/api", func(r chi.Router) {
r.Use(apiLimiter.Middleware(cfg.TrustedProxyHeader))
// Public pre tenant auth: runs before any org is known. Behind the strict auth rate limiter, the
// CSRF Origin allowlist, and the signed double submit, but NOT the session resolver (these operate
// on a pre MFA session or no session and read the cookie themselves).
r.Group(func(r chi.Router) {
r.Use(authLimiter.Middleware(cfg.TrustedProxyHeader))
r.Use(csrfMW)
authH.PublicRoutes(r)
})
// Tenant scoped: the session resolver feeds app.current_org_id, then CSRF, then per route RBAC.
r.Group(func(r chi.Router) {
r.Use(sessionMW)
r.Use(csrfMW)
// Flat alias surface: the pre split paths (POST /match, /slates/..., /candidates, ...) stay
// working so existing clients and the screenmatch.thatwebhostingguy.com redirect never break.
h.Routes(r)
// Canonical tool namespaces over the same shared core: screenMATCH, slateMATCH, missionMATCH.
r.Route("/screenmatch", screenH.Routes)
r.Route("/slatematch", slateH.Routes)
r.Route("/missionmatch", missionH.Routes)
ccH.Routes(r)
oppH.Routes(r)
oppIntelH.Routes(r)
awardsH.Routes(r)
wagesH.Routes(r)
sourcingH.Routes(r)
eeoH.Routes(r)
govH.Routes(r)
staffingH.Routes(r)
brainH.Routes(r)
authH.TenantRoutes(r)
})
})
srv := &http.Server{
Addr: cfg.HTTPAddr,
Handler: r,
ReadHeaderTimeout: 10 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 60 * time.Second,
IdleTimeout: 120 * time.Second,
}
// Graceful shutdown: on SIGINT or SIGTERM, stop accepting and let in flight requests (and their
// tenant transactions) drain before exit, so a redeploy never severs a write mid commit.
shutdownCtx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
go func() {
log.Printf("listening on %s", cfg.HTTPAddr)
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("server: %v", err)
}
}()
<-shutdownCtx.Done()
log.Printf("shutdown signal received, draining in flight requests")
drainCtx, cancel := context.WithTimeout(context.Background(), 25*time.Second)
defer cancel()
if err := srv.Shutdown(drainCtx); err != nil {
log.Printf("graceful shutdown error: %v", err)
}
log.Printf("stopped")
}
// fipsSelfTest derives one PBKDF2 key at the production iteration count under the active crypto module.
func fipsSelfTest() error {
_, err := pbkdf2.Key(sha256.New, "missionmatch fips self test", []byte("self test salt"), security.PBKDF2Iterations, 32)
return err
}
// buildAuthService assembles the auth service from config. In production every key is present
// (config.Validate enforces it). In development a missing key falls back to an ephemeral random key
// with a loud warning, so the server still starts for local work.
func buildAuthService(cfg config.Config, db *tenant.DB, auditW *audit.Writer) (*auth.Service, error) {
prod := cfg.IsProduction()
pepperKeys := map[int][]byte{
cfg.Auth.PepperVersion: authKey(cfg.Auth.Pepper, 32, "MM_CREDENTIAL_PEPPER", prod),
}
if len(cfg.Auth.PepperPrevious) > 0 {
pepperKeys[cfg.Auth.PepperVersion-1] = cfg.Auth.PepperPrevious
}
pepper, err := security.NewPepper(cfg.Auth.PepperVersion, pepperKeys)
if err != nil {
return nil, err
}
hasher, err := security.NewHasher(pepper, security.PBKDF2Iterations)
if err != nil {
return nil, err
}
tagger, err := security.NewTagger(authKey(cfg.Auth.RecoveryLookupKey, 32, "MM_RECOVERY_LOOKUP_KEY", prod))
if err != nil {
return nil, err
}
// The KEK map carries the active version plus an optional prior version so a rotated KEK still
// decrypts TOTP secrets sealed under the old one, mirroring the pepper rotation above.
totpKEKs := map[int][]byte{
cfg.Auth.TOTPKEKVersion: authKey(cfg.Auth.TOTPKEK, 32, "MM_TOTP_KEK", prod),
}
if len(cfg.Auth.TOTPKEKPrevious) > 0 {
totpKEKs[cfg.Auth.TOTPKEKVersion-1] = cfg.Auth.TOTPKEKPrevious
}
return auth.NewService(auth.Deps{
DB: db,
Audit: auditW,
Hasher: hasher,
Tagger: tagger,
TOTP: security.TOTP{},
TOTPKEKs: totpKEKs,
TOTPKEKActive: cfg.Auth.TOTPKEKVersion,
TOTPIssuer: cfg.Auth.TOTPIssuer,
IdleSeconds: cfg.Auth.SessionIdleSeconds,
AbsoluteSeconds: cfg.Auth.SessionAbsoluteSeconds,
PendingSeconds: cfg.Auth.PendingAbsoluteSeconds,
InviteSeconds: cfg.Auth.InviteExpirySeconds,
CookieSecure: cfg.Auth.CookieSecure,
MFADisabled: !cfg.Auth.MFARequired,
})
}
// authKey returns the configured key, or an ephemeral random key in development when it is unset.
func authKey(set []byte, size int, name string, prod bool) []byte {
if len(set) > 0 {
return set
}
if prod {
// config.Validate already fatals before here; this is a defensive backstop.
log.Fatalf("%s required in production", name)
}
b := make([]byte, size)
_, _ = rand.Read(b)
log.Printf("WARNING: %s unset, using an ephemeral dev key (sessions and credentials will not survive a restart; set %s for a stable dev database)", name, name)
return b
}