Agent Mesh
Go a2aa2a-protocolagent-meshagentic-aiinferencemcpmeshmesh-networknetwork-securitynetworking-aip2p-network
controlplane: one resolver per unresolved workload issuer across discovery passes
Antonio Ojea4dc2384b8ad8
diff --git a/internal/controlplane/server.go b/internal/controlplane/server.goindex 1343ffa6..7cbd245e 100644--- a/internal/controlplane/server.go+++ b/internal/controlplane/server.go@@ -137,7 +137,11 @@ type Server struct {meshMu sync.RWMutexmesh MeshAdapter- providersMu sync.RWMutex+ providersMu sync.RWMutex+ // resolving holds the workload issuers a background resolver is working+ // on, so that a second pass over the configuration (Init is exported for+ // embedders) starts no second resolver for the same issuer.+ resolving map[string]boolproviders map[string]*oidc.ProviderworkloadIssuers map[string]boolworkloadEmailSuffixes map[string][]string@@ -449,12 +453,17 @@ func (s *Server) discoverProviders() error {s.setProvider(iss, provider)}for _, iss := range deferred {+ if s.providerKnown(iss) {+ continue+ }workloadIssuerReady.WithLabelValues(iss).Set(0)provider, err := s.discoverProvider(iss)if err != nil {logger.Warnw("Workload issuer not resolved; tokens from it are refused until it answers", "issuer", iss, "error", err)- s.wg.Add(1)- go s.resolveWorkloadIssuer(iss)+ if s.markResolving(iss) {+ s.wg.Add(1)+ go s.resolveWorkloadIssuer(iss)+ }continue}s.setProvider(iss, provider)@@ -462,6 +471,29 @@ func (s *Server) discoverProviders() error {return nil}+// providerKnown reports whether iss is resolved or has a resolver running.+func (s *Server) providerKnown(iss string) bool {+ s.providersMu.RLock()+ defer s.providersMu.RUnlock()+ _, resolved := s.providers[iss]+ return resolved || s.resolving[iss]+}++// markResolving records that a resolver for iss is starting and reports+// whether the caller is the one that should start it.+func (s *Server) markResolving(iss string) bool {+ s.providersMu.Lock()+ defer s.providersMu.Unlock()+ if s.resolving == nil {+ s.resolving = make(map[string]bool)+ }+ if s.resolving[iss] {+ return false+ }+ s.resolving[iss] = true+ return true+}+// discoverProvider runs one bounded discovery of iss with the server's HTTP// settings.func (s *Server) discoverProvider(iss string) (*oidc.Provider, error) {@@ -482,6 +514,7 @@ func (s *Server) setProvider(iss string, provider *oidc.Provider) {s.providersMu.Lock()defer s.providersMu.Unlock()s.providers[iss] = provider+ delete(s.resolving, iss)if s.workloadIssuers[iss] || len(s.workloadEmailSuffixes[iss]) > 0 {workloadIssuerReady.WithLabelValues(iss).Set(1)}@@ -510,6 +543,9 @@ func (s *Server) resolveWorkloadIssuer(iss string) {select {case <-s.ctx.Done():timer.Stop()+ s.providersMu.Lock()+ delete(s.resolving, iss)+ s.providersMu.Unlock()returncase <-timer.C:}diff --git a/internal/controlplane/server_test.go b/internal/controlplane/server_test.goindex 28407831..2c4ff2bc 100644--- a/internal/controlplane/server_test.go+++ b/internal/controlplane/server_test.go@@ -4916,6 +4916,17 @@ func TestWorkloadIssuerResolvedInBackground(t *testing.T) {if _, ok := srv.getProviders()[workloadIss]; ok {t.Fatalf("provider for %s present although its discovery never answered", workloadIss)}+ // A second pass over the configuration (Init is exported for embedders)+ // must not start a second resolver for the same issuer.+ if err := srv.discoverProviders(); err != nil {+ t.Fatalf("second discoverProviders: %v", err)+ }+ srv.providersMu.RLock()+ resolving := len(srv.resolving)+ srv.providersMu.RUnlock()+ if resolving != 1 {+ t.Fatalf("%d resolvers after two discovery passes, want 1", resolving)+ }ctx := context.Background()if err := store.SavePolicyDocument(ctx,@@ -4980,6 +4991,12 @@ func TestWorkloadIssuerResolvedInBackground(t *testing.T) {if got := gaugeValue(t, workloadIssuerReady.WithLabelValues(workloadIss)); got != 1 {t.Fatalf("workload_issuer_ready{%s} = %v after resolution, want 1", workloadIss, got)}+ srv.providersMu.RLock()+ resolving = len(srv.resolving)+ srv.providersMu.RUnlock()+ if resolving != 0 {+ t.Fatalf("%d resolvers still recorded after resolution, want 0", resolving)+ }if code, body := exchange(); code != http.StatusOK {t.Fatalf("exchange after the issuer resolved returned %d %q, want 200", code, body)}