carlos / platform Public

apppolicy, registry: the deferred verification and Upsert tests (#328, #329)

Deferred minors from Tasks 3 and 4, folded into the final fix wave:
correctly signed records with an invalid shape or signing fields are
unverifiable to Verify, VerifyRevision and OpenKey; an unknown field is
accepted and ignored; and Upsert replaces the policy columns with
non-empty values, field by field for the same tenant and exactly for a
changed one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev 8c6904eab46a2035cf8a4bd2f14d207e7d8091dd parent 60c3375
2 files changed, +154 −0
  • internal/apppolicy/signed_test.go +96 −0
  • internal/registry/policycols_test.go +58 −0
diff --git a/internal/apppolicy/signed_test.go b/internal/apppolicy/signed_test.go
index cd76c5f4a..b031e50b9 100644
--- a/internal/apppolicy/signed_test.go
+++ b/internal/apppolicy/signed_test.go
@@ -6,6 +6,7 @@ import (
"encoding/base64"
"encoding/json"
"errors"
+ "fmt"
"reflect"
"testing"
"time"
@@ -232,3 +233,98 @@ func TestVerifyKeyBindsTheRecordToItsKey(t *testing.T) {
t.Fatalf("OpenKey of a tampered wire: %v, want ErrUnverifiable", err)
}
}
+
+// signedRaw is a wire object whose payload is raw, correctly signed under the
+// policy domain, so a test can sign what Sign would refuse to produce.
+func signedRaw(t *testing.T, priv ed25519.PrivateKey, raw []byte) []byte {
+ t.Helper()
+ b64 := base64.StdEncoding.EncodeToString(raw)
+ wire, err := json.Marshal(signedWrapper{Payload: b64,
+ Sig: base64.StdEncoding.EncodeToString(ed25519.Sign(priv, []byte(domain+b64)))})
+ if err != nil {
+ t.Fatal(err)
+ }
+ return wire
+}
+
+// Deferred T3 minor: a record signed by the right key whose shape is invalid
+// is unverifiable to every reader, so a signing bug or a key misuse can never
+// hand a box a policy Validate or the signing fields refuse.
+func TestVerifyRefusesACorrectlySignedInvalidRecord(t *testing.T) {
+ pub, priv := newKey(t)
+ many := make([]string, 9)
+ for i := range many {
+ many[i] = fmt.Sprintf("10.0.5.%d", 20+i)
+ }
+ cases := map[string]func(*Policy){
+ "version 2": func(p *Policy) { p.V = 2 },
+ "unknown default size": func(p *Policy) { p.DefaultSize = "huge" },
+ "unknown override size": func(p *Policy) {
+ p.Sizes = map[string]SizeOverride{"shop-a.example.com": {Size: "huge", CreatedAt: testNow}}
+ },
+ "override without creation": func(p *Policy) { p.Sizes = map[string]SizeOverride{"shop-a.example.com": {Size: "large"}} },
+ "upper-case override host": func(p *Policy) {
+ p.Sizes = map[string]SizeOverride{"Shop-a.example.com": {Size: "large", CreatedAt: testNow}}
+ },
+ "allow out of order": func(p *Policy) { p.EgressAllow = []string{"10.0.5.21", "10.0.5.20"} },
+ "allow repeated": func(p *Policy) { p.EgressAllow = []string{"10.0.5.20", "10.0.5.20"} },
+ "allow not canonical": func(p *Policy) { p.EgressAllow = []string{"fd00:5:0::20"} },
+ "allow not an address": func(p *Policy) { p.EgressAllow = []string{"shop-a.example.com"} },
+ "too many allows": func(p *Policy) { p.EgressAllow = many },
+ "revision 0": func(p *Policy) { p.Revision = 0 },
+ "no signed_at": func(p *Policy) { p.SignedAt = time.Time{} },
+ "invalid account": func(p *Policy) { p.Account = "AB!" },
+ "invalid app": func(p *Policy) { p.App = "Shop!" },
+ }
+ for name, mutate := range cases {
+ t.Run(name, func(t *testing.T) {
+ p := p1(testNow)
+ mutate(&p)
+ raw, err := json.Marshal(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ wire := signedRaw(t, priv, raw)
+ if _, err := Verify(pub, wire, testNow); !errors.Is(err, ErrUnverifiable) {
+ t.Errorf("Verify: %v, want ErrUnverifiable", err)
+ }
+ if rev, err := VerifyRevision(pub, wire); !errors.Is(err, ErrUnverifiable) {
+ t.Errorf("VerifyRevision = %d, %v; want ErrUnverifiable", rev, err)
+ }
+ if _, err := OpenKey(pub, wire, p.Account, p.App); !errors.Is(err, ErrUnverifiable) {
+ t.Errorf("OpenKey: %v, want ErrUnverifiable", err)
+ }
+ })
+ }
+}
+
+// Deferred T3 minor: a field this build does not know is accepted and
+// ignored, so a later plan can add one without older boxes refusing every
+// record (additive compatibility).
+func TestVerifyAcceptsAnUnknownField(t *testing.T) {
+ pub, priv := newKey(t)
+ p := p1(testNow)
+ p.DefaultSize = "large"
+ raw, err := json.Marshal(p)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var fields map[string]any
+ if err := json.Unmarshal(raw, &fields); err != nil {
+ t.Fatal(err)
+ }
+ fields["future_field"] = map[string]any{"cpu_weight": 200}
+ raw, err = json.Marshal(fields)
+ if err != nil {
+ t.Fatal(err)
+ }
+ wire := signedRaw(t, priv, raw)
+ got, err := VerifyKey(pub, wire, testNow, "abc", "shop")
+ if err != nil {
+ t.Fatalf("VerifyKey: %v", err)
+ }
+ samePolicy(t, got, p)
+ if rev, err := VerifyRevision(pub, wire); err != nil || rev != 1 {
+ t.Fatalf("VerifyRevision = %d, %v; want 1", rev, err)
+ }
+}
diff --git a/internal/registry/policycols_test.go b/internal/registry/policycols_test.go
index 4416f31fe..4078c5931 100644
--- a/internal/registry/policycols_test.go
+++ b/internal/registry/policycols_test.go
@@ -165,3 +165,61 @@ func TestRemoteSetPolicy(t *testing.T) {
t.Fatal("remote SetPolicy on an unknown host succeeded")
}
}
+
+// Deferred T4 minor: an Upsert that carries policy values replaces the row's.
+// The same tenant's values win field by field (an empty one keeps the row's);
+// another tenant's row takes exactly the values it carries, so nothing of the
+// old tenant's policy survives even where the new one sends a value for only
+// some fields.
+func TestUpsertReplacesThePolicyWithNonEmptyValues(t *testing.T) {
+ reg := openTestRegistry(t)
+ base := Route{Host: policyHost, Kind: "instance", Account: "abc", App: "shop"}
+ if err := reg.Upsert(base); err != nil {
+ t.Fatal(err)
+ }
+ if err := reg.SetPolicy(policyHost, "10.0.5.20", "xlarge", "1900000000"); err != nil {
+ t.Fatal(err)
+ }
+
+ // The same tenant, every field: all replaced.
+ same := base
+ same.EgressAllow, same.Size, same.PolicyExpiresAt = "10.0.5.21 fd00:5::20", "small", "1900043200"
+ if err := reg.Upsert(same); err != nil {
+ t.Fatal(err)
+ }
+ wantPolicy(t, policyRow(t, reg), "10.0.5.21 fd00:5::20", "small", "1900043200")
+
+ // The same tenant, one field: that one replaced, the others kept.
+ one := base
+ one.Size = "standard"
+ if err := reg.Upsert(one); err != nil {
+ t.Fatal(err)
+ }
+ wantPolicy(t, policyRow(t, reg), "10.0.5.21 fd00:5::20", "standard", "1900043200")
+
+ // Another app of the same account, with values: exactly its own.
+ other := Route{Host: policyHost, Kind: "instance", Account: "abc", App: "other",
+ EgressAllow: "10.0.5.22", Size: "large", PolicyExpiresAt: "1900086400"}
+ if err := reg.Upsert(other); err != nil {
+ t.Fatal(err)
+ }
+ wantPolicy(t, policyRow(t, reg), "10.0.5.22", "large", "1900086400")
+
+ // Another app, with a size only: no allow or expiry carries over.
+ blog := Route{Host: policyHost, Kind: "instance", Account: "abc", App: "blog", Size: "small"}
+ if err := reg.Upsert(blog); err != nil {
+ t.Fatal(err)
+ }
+ wantPolicy(t, policyRow(t, reg), "", "small", "")
+
+ // Another account, with values: exactly its own.
+ if err := reg.SetPolicy(policyHost, "10.0.5.20", "xlarge", "1900000000"); err != nil {
+ t.Fatal(err)
+ }
+ foreign := Route{Host: policyHost, Kind: "instance", Account: "zzz", App: "blog",
+ EgressAllow: "fd00:5::20", Size: "standard", PolicyExpiresAt: "1900129600"}
+ if err := reg.Upsert(foreign); err != nil {
+ t.Fatal(err)
+ }
+ wantPolicy(t, policyRow(t, reg), "fd00:5::20", "standard", "1900129600")
+}