mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Square keeps an event's created_at across every retry, and the receiver refused anything older than five minutes, so every retry of an event first sent while cloud was down (Recreate, ten rollouts a day) was refused until Square gave up. The verifier now lives in payment/providers/square and needs only the key and the URL; thirdparty/square is the API client and no longer registers itself or verifies anything, so every registry holds one Square type. Replay is stopped by the event id instead: each verified event is recorded once for the whole deployment, keyed on the verifying processor and the event id with an insert-if-absent swap, so a repeat is a duplicate under any path or org. The path names the one processor that verifies, and only one that holds its webhook secret may: an unknown name, or a verifier that needs no secret, is refused rather than tried. The books an event lands in are the verifying account's, else the deployment's brand — never a header or session, which on a replay are the replayer's. An effect that did not land (a store or the ledger did not answer) leaves the delivery open with a 503, so the provider redelivers; it used to be acknowledged and never retried. A settlement reads the payment index before the org's books, because an in-session charge's receipt is in the payer's org. POST /v1/commerce/webhooks/:provider is declared once, in setupRoutes, so standalone and embedded commerce serve the same address; the /v1/billing/webhooks spelling is gone. thirdparty/kms read /v1/kms/orgs/<org>/secrets/..., which cloud no longer serves, and was off in production anyway, so per-org payment credentials never loaded. It is deleted. secrets.Org reads orgs/<org>/<name> through the host's reader, the caller re-pointed at that org with zip.ActingAs so the KMS tenancy check admits it, and payment.Hydrate loads an org's credentials where the org receives the money: storefront checkout, storefront subscriptions and wire, and the brand's bank for the billing wire rail. Hanzo's own billing no longer reads the paying org's credentials; it charges the deployment's account, as it did. The standalone binary reads through hanzoai/kms/sdk/go.
103 lines
3.6 KiB
Go
103 lines
3.6 KiB
Go
// Package secrets is commerce's read side of the HOST's secret plane.
|
|
//
|
|
// One value, set once at embed time, read by whichever handler needs a
|
|
// deployment secret. It exists so a plugin reads secrets the way it reads
|
|
// everything else — through the host it is embedded in — instead of through an
|
|
// env fan-out (KMS → a k8s Secret → a pod env var → os.Getenv), which is three
|
|
// places for one value to go stale and a restart to pick up a rotation.
|
|
//
|
|
// An org's own material lives at orgs/<org>/<name> in the default environment —
|
|
// the address cloud's KMS gives every org (apps/kms OrgPath) and the one every
|
|
// other in-process reader uses. [Org] is the one way commerce reads it.
|
|
//
|
|
// nil is a supported state: a commerce with no reader installed reads nothing,
|
|
// and every caller treats that as "not configured".
|
|
package secrets
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/hanzoai/namespace"
|
|
"github.com/zap-proto/zip"
|
|
)
|
|
|
|
// Reader is the narrowest thing commerce needs: read one secret by reference.
|
|
// Commerce never writes, never lists, and never learns the transport — a host
|
|
// satisfies this in-process, over UDS, or from a map in a test.
|
|
type Reader interface {
|
|
GetSecret(ctx context.Context, ref string) ([]byte, error)
|
|
}
|
|
|
|
var (
|
|
mu sync.RWMutex
|
|
r Reader
|
|
)
|
|
|
|
// Set installs the host's reader. Called once, before routes register.
|
|
func Set(x Reader) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
r = x
|
|
}
|
|
|
|
// Get returns the reader, or nil when commerce runs standalone.
|
|
func Get() Reader {
|
|
mu.RLock()
|
|
defer mu.RUnlock()
|
|
return r
|
|
}
|
|
|
|
// String reads a secret and trims it, answering "" for every failure mode —
|
|
// no reader, no such secret, unreadable. Callers treat absence and error the
|
|
// same way (fall back), so collapsing them here keeps that decision in one
|
|
// place instead of at each call site.
|
|
func String(ctx context.Context, ref string) string {
|
|
rd := Get()
|
|
if rd == nil {
|
|
return ""
|
|
}
|
|
b, err := rd.GetSecret(ctx, ref)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(string(b))
|
|
}
|
|
|
|
// Org reads one of org's own secrets, orgs/<org>/<name>, and asks AS org.
|
|
//
|
|
// The host's store serves a ref naming a tenant only to a call acting for that
|
|
// same tenant (cloud apps/kms admit), and the org a call acts for rides its
|
|
// caller, never the ref. A request's caller is whoever sent it — the PAYER, when a
|
|
// customer pays a brand — so the read re-points that caller at org with
|
|
// zip.ActingAs, which names who is acting on the call. A call with no caller reads
|
|
// nothing: cloud's KMS answers an unstated call as its own brand, so "no caller"
|
|
// must not reach it. org is always one commerce resolved itself (the charging
|
|
// org, or the brand that owns the host), never a value a request supplied as a
|
|
// ref.
|
|
//
|
|
// "" for every failure: no reader, an org that is not already a clean namespace
|
|
// slug (namespace.Sanitize, the fold cloud keys every org's store by), a name
|
|
// that cannot form a ref, no such secret, an unreadable one.
|
|
func Org(ctx context.Context, org, name string) string {
|
|
if org == "" || namespace.Sanitize(org) != org || name == "" || name != strings.Trim(name, "/ ") || strings.Contains(name, "@") {
|
|
return ""
|
|
}
|
|
actx, err := as(ctx, org)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return String(actx, "orgs/"+org+"/"+name)
|
|
}
|
|
|
|
// as is ctx acting for org: unchanged when its caller already acts for org, and
|
|
// re-pointed with zip.ActingAs when it acts for another. ActingAs refuses a
|
|
// context with no authenticated caller, which is the refusal wanted here.
|
|
func as(ctx context.Context, org string) (context.Context, error) {
|
|
if zip.CallerOf(ctx).Org == org {
|
|
return ctx, nil
|
|
}
|
|
return zip.ActingAs(ctx, org)
|
|
}
|