Skip to content

Agent Mesh

avoid race when using relays

Antonio Ojea9c12fe3530e6
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.go
index 369149de..fe1f252d 100644
--- a/cmd/sam-hub/main.go
+++ b/cmd/sam-hub/main.go
@@ -18,6 +18,7 @@ import (
"github.com/libp2p/go-libp2p/core/network"
"sync"
+
"github.com/libp2p/go-msgio"
"context"
@@ -106,36 +107,44 @@ var (
var logger = golog.Logger("sam-hub")
-
type relayACL struct {
hub *Hub
}
func (a *relayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {
- _, ok := a.hub.authenticatedPeers.Load(p)
- return ok
+ for i := 0; i < 20; i++ {
+ if _, ok := a.hub.authenticatedPeers.Load(p); ok {
+ return true
+ }
+ time.Sleep(250 * time.Millisecond)
+ }
+ return false
}
func (a *relayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {
- _, ok := a.hub.authenticatedPeers.Load(src)
- return ok
+ for i := 0; i < 20; i++ {
+ if _, ok := a.hub.authenticatedPeers.Load(src); ok {
+ return true
+ }
+ time.Sleep(250 * time.Millisecond)
+ }
+ return false
}
-
// Hub handles identity bridging and network discovery
type Hub struct {
- Host host.Host
- DHT *dht.IpfsDHT
- Providers map[string]*oidc.Provider
- KeyRing *KeyRing
- MeshID string
- PubSub *pubsub.PubSub
- EventTopic *pubsub.Topic
- Policy *api.PolicyConfig
- limiter *rate.Limiter
- ExternalAddrs []string
- AllowedAudiences []string
- AllowLoopback bool
+ Host host.Host
+ DHT *dht.IpfsDHT
+ Providers map[string]*oidc.Provider
+ KeyRing *KeyRing
+ MeshID string
+ PubSub *pubsub.PubSub
+ EventTopic *pubsub.Topic
+ Policy *api.PolicyConfig
+ limiter *rate.Limiter
+ ExternalAddrs []string
+ AllowedAudiences []string
+ AllowLoopback bool
authenticatedPeers sync.Map
}
@@ -260,9 +269,9 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig, allowLoopback bool) (
if err != nil {
return nil, err
}
-
+
h.SetStreamHandler(api.AuthProtocolID, hub.HandleAuthHandshake)
-
+
h.Network().Notify(&network.NotifyBundle{
DisconnectedF: func(n network.Network, c network.Conn) {
p := c.RemotePeer()
@@ -275,7 +284,6 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig, allowLoopback bool) (
return hub, nil
}
-
func (h *Hub) parseAndVerifyJWT(ctx context.Context, jwtStr string, allowedAudiences []string) (jwt.MapClaims, *oidc.IDToken, error) {
jwtParser := jwt.Parser{}
jwtToken, _, err := jwtParser.ParseUnverified(jwtStr, jwt.MapClaims{})
diff --git a/cmd/sam-node/node.go b/cmd/sam-node/node.go
index e8d73615..6c4fc47b 100644
--- a/cmd/sam-node/node.go
+++ b/cmd/sam-node/node.go
@@ -81,13 +81,23 @@ type nodeRelayACL struct {
}
func (a *nodeRelayACL) AllowReserve(p peer.ID, addr multiaddr.Multiaddr) bool {
- _, ok := a.node.authPeers.Load(p)
- return ok
+ for i := 0; i < 20; i++ {
+ if _, ok := a.node.authPeers.Load(p); ok {
+ return true
+ }
+ time.Sleep(250 * time.Millisecond)
+ }
+ return false
}
func (a *nodeRelayACL) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {
- _, ok := a.node.authPeers.Load(src)
- return ok
+ for i := 0; i < 20; i++ {
+ if _, ok := a.node.authPeers.Load(src); ok {
+ return true
+ }
+ time.Sleep(250 * time.Millisecond)
+ }
+ return false
}
type SamNode struct {
@@ -120,13 +130,13 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P
}
node := &SamNode{
- Store: store,
- trustedKeys: trustedKeys,
+ Store: store,
+ trustedKeys: trustedKeys,
peerLastEventTime: make(map[string]int64),
receivedMsgs: make(map[string][]string),
- topics: make(map[string]*pubsub.Topic),
- LocalPolicy: nodeConfig,
- AllowLoopback: allowLoopback,
+ topics: make(map[string]*pubsub.Topic),
+ LocalPolicy: nodeConfig,
+ AllowLoopback: allowLoopback,
}
var err error
@@ -246,7 +256,6 @@ func NewSamNode(ctx context.Context, privKey crypto.PrivKey, hubPubKey ed25519.P
}
}
-
// Initialize Gossipsub for Hub Events
ps, err := pubsub.NewGossipSub(ctx, h)
if err != nil {
@@ -492,11 +501,11 @@ func (n *SamNode) listenForHubEvents(ctx context.Context) {
}
// Freshness check: reject events older than the threshold to prevent replay attacks
- eventTime := time.UnixMilli(event.Timestamp)
- if time.Since(eventTime) > FreshnessThreshold || time.Until(eventTime) > FreshnessThreshold {
- logger.Warnf("[Mesh Event] Dropping stale or future event from %s (timestamp: %d)", msg.ReceivedFrom, event.Timestamp)
- continue
- }
+ eventTime := time.UnixMilli(event.Timestamp)
+ if time.Since(eventTime) > FreshnessThreshold || time.Until(eventTime) > FreshnessThreshold {
+ logger.Warnf("[Mesh Event] Dropping stale or future event from %s (timestamp: %d)", msg.ReceivedFrom, event.Timestamp)
+ continue
+ }
switch event.Type {
case api.MeshEvent_BANNED:
@@ -507,8 +516,6 @@ func (n *SamNode) listenForHubEvents(ctx context.Context) {
}
}
-
-
func (n *SamNode) handleBannedEvent(event *api.MeshEvent) {
n.mu.Lock()
if n.peerLastEventTime == nil {
@@ -939,7 +946,6 @@ func (n *SamNode) DiscoverRemoteServicesStream(ctx context.Context, serviceType
return out, nil
}
-
// discoverServicesByName: targeted DHT lookup, no fan-out.
func (n *SamNode) discoverServicesByName(ctx context.Context, serviceType api.ServiceType, typeStr, serviceName string) ([]*api.DiscoveredProvider, error) {
peers, err := n.FindProvidersByName(ctx, serviceType, serviceName)
@@ -1091,4 +1097,3 @@ func isLoopbackOrLinkLocal(addr multiaddr.Multiaddr) bool {
}
return false
}
-