mesh/delivery merged: source.main moved to novox/mesh-delivery@cc9c681fa348
mesh/check being carried to 8f87dbe94d76 on cc9c681fa348 | from f1c33439292d
The teller read the router's record from KV_asks, a bucket no composed grant names, so every read was denied and its operator answers could not be read back. It now reads messenger_asks, as mesh-grants does. The integration bed grants the real subject and reads a record on the teller's credential, so it no longer agrees with the mistake. (issue 774) Claude-Session: https://claude.ai/code/session_01NcbgfJ9Gum47zkFKWRaovL
127 lines
4.6 KiB
Go
127 lines
4.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
"github.com/nats-io/nats.go/jetstream"
|
|
|
|
stdio "git.novox.be/novox/mesh-sdk/go"
|
|
"git.novox.be/novox/mesh-sdk/go/asks"
|
|
)
|
|
|
|
// The teller's three bus adapters for mesh-sdk go/asker, which in a bundle reaches the bus through the runtime: here the
|
|
// teller is a process on its own credential. What each may do is the controller's composed grant for a module that uses
|
|
// the seat operator-channel (mesh-control internal/broker seattraffic.go SeatTrafficOf): publish its own ask, cancel and
|
|
// condition, hear its own decided event, and read the router's record of its own asks.
|
|
|
|
// callTimeout bounds a read of the router's record.
|
|
const callTimeout = 30 * time.Second
|
|
|
|
// publishVia publishes an accept of the seat, de-duplicated by id: the same id twice is one message.
|
|
func publishVia(ctx context.Context, js jetstream.JetStream) func(subject string, body any, id string) error {
|
|
return func(subject string, body any, id string) error {
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var opts []jetstream.PublishOpt
|
|
if id != "" {
|
|
opts = append(opts, jetstream.WithMsgID(id))
|
|
}
|
|
_, err = js.Publish(ctx, subject, data, opts...)
|
|
return err
|
|
}
|
|
}
|
|
|
|
// messengerAsks is the router's records bucket as the seat operator-channel's manifest declares it (its `records`), the
|
|
// one the controller composes this account's direct get on (mesh-sdk buscompose SeatTrafficOf): never `asks`, a bucket
|
|
// no grant names (issue 774). The SDK names no constant for it.
|
|
const messengerAsks = "messenger_asks"
|
|
|
|
// recordVia reads the router's record of one of this module's asks: the key `<module>.<id>` of the router's bucket
|
|
// messenger_asks, by a direct get on the subject the controller grants for it,
|
|
// `$JS.API.DIRECT.GET.KV_messenger_asks.$KV.messenger_asks.<module>.>`. The record is {state, warrant}. An ask the
|
|
// router holds no record of is nil.
|
|
func recordVia(ctx context.Context, conn *nats.Conn, module string) func(id string) (*stdio.StateEntry, error) {
|
|
return func(id string) (*stdio.StateEntry, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, callTimeout)
|
|
defer cancel()
|
|
msg, err := conn.RequestWithContext(ctx, "$JS.API.DIRECT.GET.KV_"+messengerAsks+".$KV."+messengerAsks+"."+module+"."+id, nil)
|
|
if errors.Is(err, nats.ErrNoResponders) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if status := msg.Header.Get("Status"); status == "404" || len(msg.Data) == 0 {
|
|
return nil, nil
|
|
}
|
|
return &stdio.StateEntry{Key: id, Value: msg.Data}, nil
|
|
}
|
|
}
|
|
|
|
// decidedVia hears the warrants the router says to this module alone: its own `decided` event, a subject only the router
|
|
// publishes (the grant to subscribe it names the module). The handler is the asker's: pattern is the key it expects.
|
|
func decidedVia(conn *nats.Conn, module string, logf func(string, ...any)) func(pattern string, handle func(stdio.Envelope) error) error {
|
|
return func(pattern string, handle func(stdio.Envelope) error) error {
|
|
_, err := conn.Subscribe(asks.DecidedSubject(module), func(m *nats.Msg) {
|
|
if err := handle(stdio.Envelope{Key: pattern, Body: m.Data}); err != nil {
|
|
logf("a warrant could not be handled and is read again from the router's record at the next start: %v", err)
|
|
}
|
|
})
|
|
return err
|
|
}
|
|
}
|
|
|
|
// sayVia publishes a condition on the seat's one subject for conditions, under the module's own name.
|
|
func sayVia(ctx context.Context, js jetstream.JetStream, module string) func(c asks.Condition) error {
|
|
return func(c asks.Condition) error {
|
|
subject, err := asks.ConditionSubject(module)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
data, err := asks.EncodeCondition(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = js.Publish(ctx, subject, data)
|
|
return err
|
|
}
|
|
}
|
|
|
|
// watchAsks hands each key of the gate's bucket asks, with its revision, to put, and each key taken away to gone, until
|
|
// ctx ends or the watch fails. current is called once, when the watch has handed over every key the bucket held at its
|
|
// start (the watch's nil mark).
|
|
func watchAsks(ctx context.Context, kv jetstream.KeyValue, put func(key string, rev uint64, value []byte), gone func(key string), current func()) error {
|
|
w, err := kv.Watch(ctx, ">")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer w.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
case e, ok := <-w.Updates():
|
|
if !ok {
|
|
return errors.New("the watch ended")
|
|
}
|
|
switch {
|
|
case e == nil:
|
|
if current != nil {
|
|
current()
|
|
current = nil
|
|
}
|
|
case e.Operation() == jetstream.KeyValuePut:
|
|
put(e.Key(), e.Revision(), e.Value())
|
|
default:
|
|
gone(e.Key())
|
|
}
|
|
}
|
|
}
|
|
}
|