Source identity
| Field | Recorded value |
|---|---|
| Source type | GitHub source |
| Original record | Janady13/missionmatch / cmd/sync-tenant-identity/main.go |
| Date | Not specified in source |
| Repository | Janady13/missionmatch |
| Revision | 9bde0f9a74b89d01980fa658e5253acda7528d0c |
| Branch | per-tenant-routing |
| Path or archive member | cmd/sync-tenant-identity/main.go |
| Original SHA-256 | 519aec843537de078d754411af08749f4756fa8c7eec73628b3be8501abfba52 |
| Git blob SHA | d8d80b2e2fed8e9f754ef4d1b892b43be0b0b32e |
| Reading-copy SHA-256 | 519aec843537de078d754411af08749f4756fa8c7eec73628b3be8501abfba52 |
Every recorded occurrence
These 3 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/sync-tenant-identity/main.go | per-tenant-routing | 9bde0f9a74b89d01980fa658e5253acda7528d0c | Open original |
| Janady13/missionmatch / cmd/sync-tenant-identity/main.go | deploy-and-ops | bdacd9d0b4d5998d25fc74bf4007f4c3ec538c7a | Open original |
| Janady13/missionmatch / cmd/sync-tenant-identity/main.go | per-tenant-routing | 9bde0f9a74b89d01980fa658e5253acda7528d0c | Open original |
Recovered source transcript
Download complete source text ↓84 lines · 2.6 KB
// Command sync-tenant-identity copies an organization and its users from the control plane into that
// tenant's own database, so the tenant's business tables can reference its org and users. It is run at
// onboarding (after provision-tenant creates the database and the directory row is registered) and again
// whenever the org's users change (a new invite accepted). It is a migrator role operator tool and is
// idempotent.
//
// Usage:
//
// sync-tenant-identity -control-dsn postgres://missionmatch_migrator:...@host/controlplane -org <uuid>
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/missionworkforce/missionmatch/internal/platform/database"
"github.com/missionworkforce/missionmatch/internal/platform/directory"
"github.com/missionworkforce/missionmatch/internal/platform/identity"
)
func main() {
controlDSN := flag.String("control-dsn", os.Getenv("MM_MIGRATOR_DATABASE_URL"), "migrator DSN to the control plane database")
orgStr := flag.String("org", "", "organization id to sync")
flag.Parse()
if *controlDSN == "" || *orgStr == "" {
log.Fatal("sync-tenant-identity: -control-dsn (or MM_MIGRATOR_DATABASE_URL) and -org are required")
}
org, err := uuid.Parse(*orgStr)
if err != nil {
log.Fatalf("sync-tenant-identity: -org is not a uuid: %v", err)
}
ctx := context.Background()
control, err := pgxpool.New(ctx, *controlDSN)
if err != nil {
log.Fatalf("sync-tenant-identity: control plane pool: %v", err)
}
defer control.Close()
if err := database.AssertMigratorRole(ctx, control); err != nil {
log.Fatalf("sync-tenant-identity: %v", err)
}
tc, err := directory.New(control).Lookup(ctx, org)
if err != nil {
log.Fatalf("sync-tenant-identity: %v", err)
}
tenantDSN := swapDB(*controlDSN, tc.DBName)
tenant, err := pgxpool.New(ctx, tenantDSN)
if err != nil {
log.Fatalf("sync-tenant-identity: tenant pool: %v", err)
}
defer tenant.Close()
n, err := identity.SeedTenant(ctx, control, tenant, org)
if err != nil {
log.Fatalf("sync-tenant-identity: %v", err)
}
log.Printf("sync-tenant-identity: seeded org %s and %d user(s) into tenant database %q", org, n, tc.DBName)
}
// swapDB reuses a migrator DSN and swaps only the database name (mirrors cmd/provision-tenant).
func swapDB(dsn, name string) string {
cfg, err := pgx.ParseConfig(dsn)
if err != nil {
return dsn
}
out := fmt.Sprintf("host=%s port=%d user=%s dbname=%s", cfg.Host, cfg.Port, cfg.User, name)
if cfg.Password != "" {
out += " password=" + cfg.Password
}
if cfg.TLSConfig == nil {
out += " sslmode=disable"
}
return out
}