Files
zeekay 73b7290e7f square: one webhook route and one verifier; secrets come from the host's KMS
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.
2026-10-19 09:12:56 -07:00

119 lines
4.2 KiB
Go

// Copyright (c) 2014-present Hanzo AI, Inc.
// Licensed under MIT OR Apache-2.0. See LICENSE-MIT and LICENSE-APACHE.
package secrets
import (
"context"
"errors"
"strings"
"testing"
"github.com/zap-proto/zip"
)
// store is a host secret plane that keeps cloud's tenancy rule (apps/kms admit): a
// ref naming orgs/<org>/… is served only to a call whose stated caller acts for
// that org. It records every ref it was asked for.
type store struct {
Values map[string]string
Asked []string
}
var errRefused = errors.New("kms: ref belongs to another org than the call acts for")
func (s *store) GetSecret(ctx context.Context, ref string) ([]byte, error) {
s.Asked = append(s.Asked, ref)
if rest, ok := strings.CutPrefix(ref, "orgs/"); ok {
org, _, _ := strings.Cut(rest, "/")
if zip.CallerOf(ctx).Org != org {
return nil, errRefused
}
}
if v, ok := s.Values[ref]; ok {
return []byte(v), nil
}
return nil, errors.New("no such secret")
}
func install(t *testing.T, s *store) {
t.Helper()
prev := Get()
Set(s)
t.Cleanup(func() { Set(prev) })
}
// Org reads orgs/<org>/<name> and states org on the call, so a store that serves
// a tenant's material only to that tenant answers it — even when the request
// behind ctx was sent by someone in another org, which is the payer paying a
// brand.
func TestOrgReadsAsTheOrg(t *testing.T) {
s := &store{Values: map[string]string{"orgs/hanzo/wire/WIRE_SWIFT": "BOFAUS3N"}}
install(t, s)
payer := zip.WithCaller(context.Background(), zip.Caller{Org: "acme", User: "alice", OrgAdmin: true})
if got := Org(payer, "hanzo", "wire/WIRE_SWIFT"); got != "BOFAUS3N" {
t.Fatalf("Org = %q, asked %v; want the brand's value read as the brand", got, s.Asked)
}
if len(s.Asked) != 1 || s.Asked[0] != "orgs/hanzo/wire/WIRE_SWIFT" {
t.Fatalf("asked %v, want exactly orgs/hanzo/wire/WIRE_SWIFT (no leading slash: a tenant ref)", s.Asked)
}
// The same store refuses the payer's own context, which is why Org states one.
if _, err := s.GetSecret(payer, "orgs/hanzo/wire/WIRE_SWIFT"); err == nil {
t.Fatal("the fake store does not keep the tenancy rule, so this test proves nothing")
}
}
// A caller from another org is re-pointed with zip.ActingAs, so the read names
// who is acting; a caller already in the org is left as it is; with no caller,
// nothing is read — cloud's KMS would answer an unstated call as its own brand.
func TestAsRecordsWhoIsActing(t *testing.T) {
payer := zip.WithCaller(context.Background(), zip.Caller{Org: "acme", User: "alice", Project: "p1"})
actx, err := as(payer, "hanzo")
if err != nil {
t.Fatal(err)
}
if who := zip.CallerOf(actx); who.Org != "hanzo" || who.ActedBy != "alice" || who.Project != "" {
t.Fatalf("caller = %+v, want org hanzo acted by alice, no project", who)
}
same := zip.WithCaller(context.Background(), zip.Caller{Org: "hanzo", User: "z"})
if actx, err := as(same, "hanzo"); err != nil || zip.CallerOf(actx).ActedBy != "" {
t.Fatalf("a caller already in the org was re-pointed: %+v, %v", zip.CallerOf(actx), err)
}
if _, err := as(context.Background(), "hanzo"); err == nil {
t.Fatal("a call with no caller was given an org to act for")
}
s := &store{Values: map[string]string{"orgs/hanzo/wire/WIRE_SWIFT": "BOFAUS3N"}}
install(t, s)
if got := Org(context.Background(), "hanzo", "wire/WIRE_SWIFT"); got != "" || len(s.Asked) != 0 {
t.Fatalf("a call with no caller read %q (asked %v)", got, s.Asked)
}
}
func TestOrgRefusesWhatCannotFormARef(t *testing.T) {
s := &store{Values: map[string]string{}}
install(t, s)
for _, c := range []struct{ org, name string }{
{"", "wire/X"}, {"hanzo", ""}, {"a/b", "wire/X"}, {"hanzo@prod", "wire/X"}, {"hanzo", "wire/X@prod"},
{" hanzo", "wire/X"}, {"hanzo ", "wire/X"}, {"Hanzo", "wire/X"}, {"team.a", "wire/X"}, {"hanzo", "/wire/X"},
} {
if got := Org(context.Background(), c.org, c.name); got != "" {
t.Errorf("Org(%q, %q) = %q, want refused", c.org, c.name, got)
}
}
if len(s.Asked) != 0 {
t.Fatalf("a malformed ref reached the store: %v", s.Asked)
}
}
func TestOrgWithNoReaderReadsNothing(t *testing.T) {
prev := Get()
Set(nil)
t.Cleanup(func() { Set(prev) })
if got := Org(context.Background(), "hanzo", "wire/X"); got != "" {
t.Fatalf("Org with no reader = %q, want empty", got)
}
}