This commit is contained in:
1 parent
df94fc6d8b
commit
a2a96f6a46
5 files changed
+434
-42
No files matched your search
@@ -1,7 +1,7 @@
|
||||
// Package coreconfig lets any service in this workspace receive AuthGuard's and CarrierNode's
|
||||
// current base URLs pushed from ProjectConfigurator, instead of each service hardcoding them in
|
||||
// current base URL(s) pushed from ProjectConfigurator, instead of each service hardcoding them in
|
||||
// its own env vars. ProjectConfigurator's Projects workspace is the operator-facing source of
|
||||
// truth for both (its authguard_url/carriernode_url settings); it pushes a fresh copy to every
|
||||
// truth for both (its authguard_url/carriernode_url(_N) settings); it pushes a fresh copy to every
|
||||
// project it has registered whenever either changes, and again on a periodic tick, so a service
|
||||
// that restarted (and lost its in-memory copy) converges again within a couple of minutes with no
|
||||
// manual step. See ProjectConfigurator/README.md's "Pushing core service info to other projects"
|
||||
@@ -16,10 +16,20 @@ import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Info is AuthGuard's and CarrierNode's current base URL, as pushed by ProjectConfigurator.
|
||||
// Info is AuthGuard's and CarrierNode's current base URL(s), as pushed by ProjectConfigurator.
|
||||
type Info struct {
|
||||
AuthGuardURL string `json:"authGuardUrl"`
|
||||
AuthGuardURL string `json:"authGuardUrl"`
|
||||
|
||||
// CarrierNodeURL is the primary CarrierNode instance's URL (CarrierNodeURLs[0], if any) - kept
|
||||
// for the services that only ever talk to one CarrierNode instance via
|
||||
// carriernodeclient.New(secret, store.CarrierNodeURL).
|
||||
CarrierNodeURL string `json:"carrierNodeUrl"`
|
||||
|
||||
// CarrierNodeURLs is every CarrierNode instance ProjectConfigurator has registered, in
|
||||
// priority order (the first entry is the same as CarrierNodeURL) - services that want to
|
||||
// discover files across every configured storage backend / fail over between replicas wire
|
||||
// this through carriernodeclient.NewMulti(secret, store.CarrierNodeURLs) instead.
|
||||
CarrierNodeURLs []string `json:"carrierNodeUrls"`
|
||||
}
|
||||
|
||||
// Store holds the most recently pushed Info, defaulting to whatever fallback NewStore was given
|
||||
@@ -34,7 +44,7 @@ type Store struct {
|
||||
|
||||
// NewStore returns a Store seeded with fallback.
|
||||
func NewStore(fallback Info) *Store {
|
||||
return &Store{info: fallback}
|
||||
return &Store{info: normalizeInfo(fallback)}
|
||||
}
|
||||
|
||||
// Get returns the current Info.
|
||||
@@ -49,10 +59,16 @@ func (s *Store) Get() Info {
|
||||
func (s *Store) AuthGuardURL() string { return s.Get().AuthGuardURL }
|
||||
|
||||
// CarrierNodeURL is a convenience accessor for Get().CarrierNodeURL - the natural closure to wire
|
||||
// as a carriernodeclient.Client's BaseURL: carriernodeclient.New(secret, func(ctx
|
||||
// as a single-instance carriernodeclient.Client's BaseURL: carriernodeclient.New(secret, func(ctx
|
||||
// context.Context) string { return store.CarrierNodeURL() }).
|
||||
func (s *Store) CarrierNodeURL() string { return s.Get().CarrierNodeURL }
|
||||
|
||||
// CarrierNodeURLs is a convenience accessor for Get().CarrierNodeURLs - the natural closure to
|
||||
// wire as a multi-instance carriernodeclient.Client's BaseURLs:
|
||||
// carriernodeclient.NewMulti(secret, func(ctx context.Context) []string { return
|
||||
// store.CarrierNodeURLs() }).
|
||||
func (s *Store) CarrierNodeURLs() []string { return s.Get().CarrierNodeURLs }
|
||||
|
||||
// OnUpdate registers fn to be called (synchronously, so keep it fast) with the new Info after
|
||||
// every Set - e.g. client.SetBaseURL for an authclient.Client that isn't already reading through
|
||||
// Store on every call. Only one callback is kept; a second registration replaces the first, which
|
||||
@@ -63,15 +79,17 @@ func (s *Store) OnUpdate(fn func(Info)) {
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
// Set replaces the current Info, field by field, ignoring a blank field so a partial push (e.g.
|
||||
// only carrierNodeUrl changed) never clobbers the other value with an empty string.
|
||||
// Set replaces the current Info, field by field, ignoring a blank/empty field so a partial push
|
||||
// (e.g. only carrierNodeUrl(s) changed) never clobbers the other value.
|
||||
func (s *Store) Set(next Info) {
|
||||
next = normalizeInfo(next)
|
||||
s.mu.Lock()
|
||||
if url := strings.TrimSpace(next.AuthGuardURL); url != "" {
|
||||
s.info.AuthGuardURL = url
|
||||
}
|
||||
if url := strings.TrimSpace(next.CarrierNodeURL); url != "" {
|
||||
s.info.CarrierNodeURL = url
|
||||
if len(next.CarrierNodeURLs) > 0 {
|
||||
s.info.CarrierNodeURLs = next.CarrierNodeURLs
|
||||
s.info.CarrierNodeURL = next.CarrierNodeURLs[0]
|
||||
}
|
||||
current := s.info
|
||||
callback := s.onUpdate
|
||||
@@ -81,6 +99,30 @@ func (s *Store) Set(next Info) {
|
||||
}
|
||||
}
|
||||
|
||||
// normalizeInfo trims/drops blank CarrierNodeURLs entries and fills in CarrierNodeURLs from a
|
||||
// lone legacy CarrierNodeURL (or vice versa), so every caller - old pushes that only ever set
|
||||
// CarrierNodeURL, and new ones that set CarrierNodeURLs - sees both fields populated consistently.
|
||||
func normalizeInfo(info Info) Info {
|
||||
var cleaned []string
|
||||
for _, url := range info.CarrierNodeURLs {
|
||||
if url = strings.TrimSpace(url); url != "" {
|
||||
cleaned = append(cleaned, url)
|
||||
}
|
||||
}
|
||||
if len(cleaned) == 0 {
|
||||
if url := strings.TrimSpace(info.CarrierNodeURL); url != "" {
|
||||
cleaned = []string{url}
|
||||
}
|
||||
}
|
||||
info.CarrierNodeURLs = cleaned
|
||||
if len(cleaned) > 0 {
|
||||
info.CarrierNodeURL = cleaned[0]
|
||||
} else {
|
||||
info.CarrierNodeURL = ""
|
||||
}
|
||||
return info
|
||||
}
|
||||
|
||||
// Handler is the X-Internal-Auth-gated PUT /api/core-services/internal endpoint
|
||||
// ProjectConfigurator calls to push a fresh Info: mount it at that path (see
|
||||
// ProjectConfigurator/README.md) behind a secret check for this service's own
|
||||
|
||||
@@ -63,6 +63,44 @@ func TestHandlerRejectsMissingOrWrongSecret(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreSetPopulatesCarrierNodeURLsFromLegacyField(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
|
||||
store.Set(Info{CarrierNodeURL: "http://carrier.example.com"})
|
||||
|
||||
got := store.Get()
|
||||
if want := []string{"http://carrier.example.com"}; !equalStrings(got.CarrierNodeURLs, want) {
|
||||
t.Fatalf("CarrierNodeURLs = %v, want %v", got.CarrierNodeURLs, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreSetCarrierNodeURLsAlsoUpdatesPrimary(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
|
||||
store.Set(Info{CarrierNodeURLs: []string{"http://primary.example.com", "http://replica.example.com"}})
|
||||
|
||||
got := store.Get()
|
||||
if got.CarrierNodeURL != "http://primary.example.com" {
|
||||
t.Fatalf("CarrierNodeURL = %q, want first CarrierNodeURLs entry", got.CarrierNodeURL)
|
||||
}
|
||||
want := []string{"http://primary.example.com", "http://replica.example.com"}
|
||||
if !equalStrings(got.CarrierNodeURLs, want) {
|
||||
t.Fatalf("CarrierNodeURLs = %v, want %v", got.CarrierNodeURLs, want)
|
||||
}
|
||||
}
|
||||
|
||||
func equalStrings(got, want []string) bool {
|
||||
if len(got) != len(want) {
|
||||
return false
|
||||
}
|
||||
for i := range got {
|
||||
if got[i] != want[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func TestHandlerAppliesPushedInfo(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
handler := Handler("s3cret", store)
|
||||
|
||||
Reference in new issue
Block a user