carlos / platform Public

console: a reference code names its catalogue rule

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev b891a8b428f9950ed691c1d6611877f446c48fcd parent 703de09
2 files changed, +81 −3
  • internal/console/apiedgeref.go +18 −3
  • internal/console/apiedgeref_test.go +63 −0

CI failed — run details

diff --git a/internal/console/apiedgeref.go b/internal/console/apiedgeref.go
index afc8896b7..1fa06f8e5 100644
--- a/internal/console/apiedgeref.go
+++ b/internal/console/apiedgeref.go
@@ -15,13 +15,19 @@ import (
"time"
"github.com/carlosframework/platform/internal/applog"
+ "github.com/carlosframework/platform/internal/edgeguard"
"github.com/carlosframework/platform/internal/reqrec"
)
// apiEdgeRef is the wire shape of one explained code. The seal's fields
-// (code, verified, at, tier, rule_hash) are always there: they come from the
-// code itself, so they tell the owner which rule fired and when whatever
-// happened to the record. RecordStatus says what happened to it:
+// (code, sealed, verified, at, tier, rule_hash) are always there: they come
+// from the code itself, so they tell the owner which rule fired and when
+// whatever happened to the record. Sealed says whether the edge sealed the
+// code at all; an unsealed code is never verified, so the two together tell
+// "not checkable" from "checked and genuine". Rule and Group name the
+// catalogue rule behind rule_hash, and are left out when the catalogue has
+// no rule of that hash and tier (a tenant's own rule, or a rule from another
+// catalogue version). RecordStatus says what happened to the record:
//
// found Record is the edge record carrying this code
// absent the logs were read and hold no such record (sampled away,
@@ -30,10 +36,13 @@ import (
// asking again later may find it
type apiEdgeRef struct {
Code string `json:"code"`
+ Sealed bool `json:"sealed"`
Verified bool `json:"verified"`
At time.Time `json:"at"`
Tier string `json:"tier"`
RuleHash string `json:"rule_hash"`
+ Rule string `json:"rule,omitempty"`
+ Group string `json:"group,omitempty"`
RecordStatus string `json:"record_status"`
Record *applog.Record `json:"record"`
}
@@ -82,12 +91,18 @@ func (s *Server) handleAPIEdgeRef(w http.ResponseWriter, r *http.Request) {
}, 1000)
resp := apiEdgeRef{
Code: canon,
+ Sealed: ref.Sealed,
Verified: ref.Verified,
At: ref.At,
Tier: ref.Tier.String(),
RuleHash: fmt.Sprintf("%08x", ref.RuleHash),
RecordStatus: "absent",
}
+ // The hash is 32 bits, so a rule only counts as the code's when its tier
+ // is the code's tier too; a chance collision across tiers names nothing.
+ if rule, ok := edgeguard.RuleByHash(ref.RuleHash); ok && rule.Tier == ref.Tier {
+ resp.Rule, resp.Group = rule.ID, rule.Group
+ }
switch {
case err != nil:
// The code still explains itself; a failed read must not take that
diff --git a/internal/console/apiedgeref_test.go b/internal/console/apiedgeref_test.go
index 2a88a93a9..af360ac18 100644
--- a/internal/console/apiedgeref_test.go
+++ b/internal/console/apiedgeref_test.go
@@ -207,3 +207,66 @@ func TestEdgeRefRecordUnavailable(t *testing.T) {
t.Fatalf("got %+v", resp)
}
}
+
+// TestAPIEdgeRefNamesCatalogueRule: a sealed code for a catalogue rule comes
+// back with the rule's id and group, so the owner reads which rule fired
+// without decoding the hash.
+func TestAPIEdgeRefNamesCatalogueRule(t *testing.T) {
+ fx, token := edgeRefFixture(t, "code-edgeref-catalogue")
+ code := reqrec.NewRef(time.Now().UTC(), reqrec.TierHard, "hard.secrets.env", "box1", edgeRefTestKey)
+
+ resp := getEdgeRef(t, fx, token, code)
+ if resp.Rule != "hard.secrets.env" || resp.Group != "secrets" || !resp.Sealed || !resp.Verified {
+ t.Fatalf("rule/group/sealed/verified: %+v", resp)
+ }
+}
+
+// TestAPIEdgeRefUnknownRule: a rule the catalogue does not hold (a tenant's
+// own rule, or one from a catalogue version this console lacks) is named by
+// its hash alone, and the rule and group fields are left out.
+func TestAPIEdgeRefUnknownRule(t *testing.T) {
+ fx, token := edgeRefFixture(t, "code-edgeref-unknownrule")
+ code := reqrec.NewRef(time.Now().UTC(), reqrec.TierHard, "tenant-x", "box1", edgeRefTestKey)
+
+ status, _, body := rawReq(t, http.MethodGet, edgeRefURL(fx, fx.sqid, "titogo", code), token, "")
+ if status != http.StatusOK {
+ t.Fatalf("status = %d: %s", status, body)
+ }
+ var fields map[string]any
+ if err := json.Unmarshal([]byte(body), &fields); err != nil {
+ t.Fatal(err)
+ }
+ for _, k := range []string{"rule", "group"} {
+ if _, ok := fields[k]; ok {
+ t.Errorf("%s present for an unknown rule: %s", k, body)
+ }
+ }
+ if want := fmt.Sprintf("%08x", reqrec.RuleHash("tenant-x")); fields["rule_hash"] != want {
+ t.Fatalf("rule_hash = %v, want %s", fields["rule_hash"], want)
+ }
+}
+
+// TestAPIEdgeRefTierMismatch: a code whose hash matches a catalogue rule but
+// whose tier is not that rule's tier does not name the rule. The hash is only
+// 32 bits, so the tier is the cheap second check against a chance collision.
+func TestAPIEdgeRefTierMismatch(t *testing.T) {
+ fx, token := edgeRefFixture(t, "code-edgeref-tiermismatch")
+ code := reqrec.NewRef(time.Now().UTC(), reqrec.TierProbe, "hard.secrets.env", "box1", edgeRefTestKey)
+
+ resp := getEdgeRef(t, fx, token, code)
+ if resp.Rule != "" || resp.Group != "" {
+ t.Fatalf("named a rule of another tier: %+v", resp)
+ }
+}
+
+// TestAPIEdgeRefUnsealed: a code minted without a key still explains itself,
+// but says it was not sealed and so cannot be verified.
+func TestAPIEdgeRefUnsealed(t *testing.T) {
+ fx, token := edgeRefFixture(t, "code-edgeref-unsealed")
+ code := reqrec.NewRef(time.Now().UTC(), reqrec.TierHard, "hard.secrets.env", "box1", nil)
+
+ resp := getEdgeRef(t, fx, token, code)
+ if resp.Sealed || resp.Verified {
+ t.Fatalf("sealed/verified: %+v", resp)
+ }
+}