Files
tenant-registry/internal/config/config.go
T
sharang 209524e5ac
ci / shared (push) Successful in 14s
ci / test (push) Successful in 22m28s
ci / image (push) Successful in 28s
feat(erp): always-on ERP link, usage events, outbox, CLI licences (#25)
2026-09-12 08:35:13 +00:00

138 lines
4.6 KiB
Go

package config
import (
"fmt"
"os"
"strconv"
"time"
)
type Config struct {
Env string // dev | stage | prod
Addr string // listen address, e.g. ":8090"
KeycloakIssuer string // e.g. http://localhost:8080/realms/breakpilot-dev
DatabaseURL string // postgres DSN (unused in skeleton; in-memory store)
// Keycloak Admin API — only used if KeycloakAdminURL is set. Empty
// values disable the adapter and tenant-registry falls back to the
// Mock (dev convenience).
KeycloakAdminURL string
KeycloakRealm string
KeycloakClientID string
KeycloakClientSecret string
KeycloakTimeout time.Duration
// Inbound API auth (RBAC Phase 1). With AuthEnabled the server refuses
// to start unless OIDC discovery against KeycloakIssuer succeeds, and
// every non-health route requires a bearer token whose audience
// contains AuthAudience.
AuthEnabled bool
AuthAudience string
// Downstream product provisioning. When ProductAPIURL is set, tenant
// creation also provisions the tenant in the product's own database with
// the SAME registry UUID, so both anchors agree. Empty ⇒ no-op adapter.
ProductAPIURL string
ProductAPIPath string
ProductTimeout time.Duration
// Invite e-mail (execute-actions) client + redirect; per environment.
InviteClientID string
InviteRedirectURI string
// Per-client authorization rules (see server.ParseAuthzRules). Empty ⇒
// every authenticated client may call every route (dev only).
AuthzRules string
// ERP link (M-MIG-7). ErpBaseURL empty ⇒ outbox accumulates, reconcile
// idle (dev). Staging points at erpnext-mock-stage.
ErpBaseURL string
ErpAPIKey string
ErpAPISecret string
ErpLeadDoctype string
ErpWebhookSecret string // HMAC for POST /v1/internal/erp/webhook; empty ⇒ receiver disabled
ErpOutboxInterval time.Duration
ErpReconcileEvery time.Duration
// Ladder timing (days): warning → restricted, restricted → halted,
// halted → archived (export window).
RestrictAfterDays int
HaltAfterDays int
ExportWindowDays int
// CLI licences (decision 13). Empty key ⇒ ephemeral (dev only, logged).
LicenceSigningKey string
LicenceIssuer string
LicenceTTLDays int
}
func Load() (*Config, error) {
env := getenv("APP_ENV", "dev")
if env != "dev" && env != "stage" && env != "prod" {
return nil, fmt.Errorf("invalid APP_ENV %q", env)
}
return &Config{
Env: env,
// :8090 — Keycloak owns :8080 in the dev stack.
Addr: getenv("ADDR", ":8090"),
KeycloakIssuer: getenv("KEYCLOAK_ISSUER", "http://localhost:8080/realms/breakpilot-dev"),
DatabaseURL: os.Getenv("DATABASE_URL"),
KeycloakAdminURL: os.Getenv("KEYCLOAK_ADMIN_URL"),
KeycloakRealm: getenv("KEYCLOAK_REALM", "breakpilot-dev"),
KeycloakClientID: os.Getenv("KEYCLOAK_CLIENT_ID"),
KeycloakClientSecret: os.Getenv("KEYCLOAK_CLIENT_SECRET"),
KeycloakTimeout: 10 * time.Second,
AuthEnabled: getenv("AUTH_ENABLED", "false") == "true",
AuthAudience: getenv("AUTH_EXPECTED_AUDIENCE", "tenant-registry"),
ProductAPIURL: os.Getenv("PRODUCT_API_URL"),
ProductAPIPath: getenv("PRODUCT_API_TENANTS_PATH", "/sdk/v1/tenants"),
ProductTimeout: 10 * time.Second,
InviteClientID: getenv("INVITE_CLIENT_ID", "dev-portal"),
InviteRedirectURI: getenv("INVITE_REDIRECT_URI", "https://breakpilot.com/onboard"),
AuthzRules: os.Getenv("AUTHZ_RULES"),
ErpBaseURL: os.Getenv("ERP_BASE_URL"),
ErpAPIKey: os.Getenv("ERP_API_KEY"),
ErpAPISecret: os.Getenv("ERP_API_SECRET"),
ErpLeadDoctype: getenv("ERP_LEAD_DOCTYPE", "Lead"),
ErpWebhookSecret: os.Getenv("ERP_WEBHOOK_SECRET"),
ErpOutboxInterval: getduration("ERP_OUTBOX_INTERVAL", 15*time.Second),
ErpReconcileEvery: getduration("ERP_RECONCILE_INTERVAL", 15*time.Minute),
RestrictAfterDays: getint("ERP_RESTRICT_AFTER_DAYS", 14),
HaltAfterDays: getint("ERP_HALT_AFTER_DAYS", 30),
ExportWindowDays: getint("ERP_EXPORT_WINDOW_DAYS", 30),
LicenceSigningKey: os.Getenv("LICENCE_SIGNING_KEY"),
LicenceIssuer: getenv("LICENCE_ISSUER", "tenant-registry"),
LicenceTTLDays: getint("LICENCE_TTL_DAYS", 30),
}, nil
}
func getint(key string, fallback int) int {
if v := os.Getenv(key); v != "" {
if n, err := strconv.Atoi(v); err == nil {
return n
}
}
return fallback
}
func getduration(key string, fallback time.Duration) time.Duration {
if v := os.Getenv(key); v != "" {
if d, err := time.ParseDuration(v); err == nil {
return d
}
}
return fallback
}
func getenv(key, fallback string) string {
if v := os.Getenv(key); v != "" {
return v
}
return fallback
}