caelum0x 5bb6bc92f2 fix(card-issuance): persist card metadata — a restart silently lost every issued card forever
cardsvc.Service's own `cards map[string]*cardissuancepb.Card` was the
only place this repo stores Card metadata (ledger-core's proto has no
Card message), and it was pure in-memory state with zero durability.
A card-issuance restart lost every issued card's id/cardholder/limit/
status while the ledger HOLD backing its limit lived on in ledger-core
forever, unreachable by GetCard/AdjustCardLimit/CloseCard ever again
(all three look the card up by id first). card_issuance.proto's own
service comment also claimed this service was "stateless... safe to
run as N replicas" — false given this in-memory map; corrected.

Adds a file-backed store (services/card-issuance/internal/cardsvc/
store.go): atomic write-then-fsync-then-rename JSON file, loaded on
startup via a new EnablePersistence(path) method (opt-in, so existing
unit tests that never call it are unaffected) and written after every
IssueCard/AdjustCardLimit/CloseCard. New RATION_CARD_ISSUANCE_DATA_DIR
env var (default ./data, same convention as RATION_AUDIT_LOG_DATA_DIR),
wired in main.go.

Verified live against the real bare-metal stack: issued a card,
confirmed data/cards.json held it, killed and restarted card-issuance,
and confirmed `ration card get`/`ration status` still found the card
(pre-fix this would have been NotFound). go build/vet/test clean for
card-issuance (0 regressions), full `make build` clean.

Does not make this service safe to run as N replicas (each replica
would still load/save its own independent copy of the file) — that
needs a real shared store, the same class of gap already named and
left open for services/auth's apikey.Store; documented, not attempted,
in this round.
2026-10-29 13:53:46 +03:00

188 lines
5.8 KiB
Go

// Package config loads and validates card-issuance's configuration from
// the environment. It fails fast (returns an error from Load, never
// panics) when AIRWALLEX_MODE=live and a required live-mode credential
// is missing, since a half-configured live client is worse than
// refusing to start.
//
// This is card-issuance's share of the gateway's former
// internal/config (docs/adr/0004): the Airwallex and bridge-policy
// settings that belonged to internal/card + internal/bridge move here
// verbatim; webhook-secret/HTTP-listener settings move to
// services/webhook-ingestion instead.
package config
import (
"fmt"
"os"
"strconv"
"time"
"github.com/caelum0x/ration/pkg/mtls"
)
// Mode is the Airwallex integration mode.
type Mode string
const (
ModeMock Mode = "mock"
ModeLive Mode = "live"
)
// Config holds every environment-derived setting card-issuance needs.
type Config struct {
AirwallexMode Mode
AirwallexClientID string
AirwallexAPIKey string
AirwallexBaseURL string
// DataDir is where this process persists its own card metadata
// (services/card-issuance/internal/cardsvc's file-backed store —
// see Service.EnablePersistence's doc comment for why this exists:
// without it, a restart loses every issued card while the ledger
// HOLD backing it lives on forever, unreachable). Same
// "RATION_<SERVICE>_DATA_DIR, default ./data" convention
// services/audit-log's RATION_AUDIT_LOG_DATA_DIR already
// established.
DataDir string
LedgerCoreAddr string
// AuthAddr is where card-issuance mints its own operator-scoped
// credential at startup (see internal/authclient.MintOperatorJWT)
// to present on every direct ledger-core call (ApplyEntry for
// bridge-funding HOLD/RELEASE and card-limit DEBIT/RELEASE) — see
// services/ledger-core/src/tenantauth.rs, docs/THREAT-MODEL.md
// item 1's "the biggest one" sub-gap.
AuthAddr string
GRPCAddr string
MetricsAddr string
BridgeMaxFraction float64
BridgeMinGrantMinorUnits int64
BridgeVelocityWindow time.Duration
// MTLS configures mutual TLS for card-issuance's own gRPC server and
// for dialing ledger-core (see pkg/mtls, infra/dev-ca/). Disabled by
// default.
MTLS mtls.Config
}
// Load reads configuration from the process environment and validates it.
func Load() (*Config, error) {
cfg := &Config{
AirwallexMode: Mode(getenvDefault("AIRWALLEX_MODE", string(ModeMock))),
AirwallexClientID: os.Getenv("AIRWALLEX_CLIENT_ID"),
AirwallexAPIKey: os.Getenv("AIRWALLEX_API_KEY"),
AirwallexBaseURL: getenvDefault("AIRWALLEX_BASE_URL", "https://api-demo.airwallex.com"),
DataDir: getenvDefault("RATION_CARD_ISSUANCE_DATA_DIR", "./data"),
LedgerCoreAddr: getenvDefault("LEDGER_CORE_ADDR", "localhost:7001"),
AuthAddr: getenvDefault("AUTH_GRPC_ADDR", "localhost:7010"),
// Ports 7010-7013 are reserved for the section 1 split
// (auth=7010, tenant-registry=7011, card-issuance=7012,
// webhook-ingestion=7013), distinct from ledger-core (7001),
// gateway (7002), audit-log (7003) and agent (no inbound port).
GRPCAddr: getenvDefault("CARD_ISSUANCE_GRPC_ADDR", "localhost:7012"),
MetricsAddr: getenvDefault("CARD_ISSUANCE_METRICS_ADDR", "localhost:9096"),
}
var err error
cfg.BridgeMaxFraction, err = getenvFloatDefault("RATION_BRIDGE_MAX_FRACTION", 0.6)
if err != nil {
return nil, err
}
cfg.BridgeMinGrantMinorUnits, err = getenvInt64Default("RATION_BRIDGE_MIN_GRANT_MINOR_UNITS", 500)
if err != nil {
return nil, err
}
velocityHours, err := getenvInt64Default("RATION_BRIDGE_VELOCITY_WINDOW_HOURS", 24)
if err != nil {
return nil, err
}
cfg.BridgeVelocityWindow = time.Duration(velocityHours) * time.Hour
cfg.MTLS, err = mtls.LoadFromEnv("CARD_ISSUANCE")
if err != nil {
return nil, err
}
if err := cfg.validate(); err != nil {
return nil, err
}
return cfg, nil
}
func (c *Config) validate() error {
switch c.AirwallexMode {
case ModeMock:
// No credentials required; the mock client is fully self-contained.
case ModeLive:
missing := []string{}
if c.AirwallexClientID == "" {
missing = append(missing, "AIRWALLEX_CLIENT_ID")
}
if c.AirwallexAPIKey == "" {
missing = append(missing, "AIRWALLEX_API_KEY")
}
if c.AirwallexBaseURL == "" {
missing = append(missing, "AIRWALLEX_BASE_URL")
}
if len(missing) > 0 {
return fmt.Errorf("config: AIRWALLEX_MODE=live requires env vars: %v", missing)
}
default:
return fmt.Errorf("config: invalid AIRWALLEX_MODE %q (want %q or %q)", c.AirwallexMode, ModeMock, ModeLive)
}
if c.BridgeMaxFraction <= 0 || c.BridgeMaxFraction > 1 {
return fmt.Errorf("config: RATION_BRIDGE_MAX_FRACTION must be in (0, 1], got %v", c.BridgeMaxFraction)
}
if c.BridgeMinGrantMinorUnits < 0 {
return fmt.Errorf("config: RATION_BRIDGE_MIN_GRANT_MINOR_UNITS must be >= 0, got %d", c.BridgeMinGrantMinorUnits)
}
if c.BridgeVelocityWindow <= 0 {
return fmt.Errorf("config: RATION_BRIDGE_VELOCITY_WINDOW_HOURS must be > 0")
}
if c.LedgerCoreAddr == "" {
return fmt.Errorf("config: LEDGER_CORE_ADDR must not be empty")
}
if c.AuthAddr == "" {
return fmt.Errorf("config: AUTH_GRPC_ADDR must not be empty")
}
if c.GRPCAddr == "" {
return fmt.Errorf("config: CARD_ISSUANCE_GRPC_ADDR must not be empty")
}
return nil
}
func getenvDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}
func getenvFloatDefault(key string, def float64) (float64, error) {
v := os.Getenv(key)
if v == "" {
return def, nil
}
f, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0, fmt.Errorf("config: invalid %s=%q: %w", key, v, err)
}
return f, nil
}
func getenvInt64Default(key string, def int64) (int64, error) {
v := os.Getenv(key)
if v == "" {
return def, nil
}
i, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return 0, fmt.Errorf("config: invalid %s=%q: %w", key, v, err)
}
return i, nil
}