Learn AuthGuard/CarrierNode URLs from ProjectConfigurator's push
Adds the receiving end of ProjectConfigurator's core-services push (PUT /api/core-services/internal, X-Internal-Auth-gated) via the shared coreconfig package, so this service's AUTHGUARD_URL/CARRIERNODE_URL env vars become just the bootstrap/fallback value instead of the permanent source of truth. authclient.Client gains a concurrency-safe SetBaseURL so a later push retargets it in place with no restart. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
20e9461d46
commit
7c4a903f59
7 files changed
+323
-11
No files matched your search
@@ -0,0 +1,114 @@
|
||||
// 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
|
||||
// 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
|
||||
// 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"
|
||||
// section for the wire contract this package implements both ends of (Store/Handler on the
|
||||
// receiving side; ProjectConfigurator's own settings.go on the sending side).
|
||||
package coreconfig
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Info is AuthGuard's and CarrierNode's current base URL, as pushed by ProjectConfigurator.
|
||||
type Info struct {
|
||||
AuthGuardURL string `json:"authGuardUrl"`
|
||||
CarrierNodeURL string `json:"carrierNodeUrl"`
|
||||
}
|
||||
|
||||
// Store holds the most recently pushed Info, defaulting to whatever fallback NewStore was given
|
||||
// (typically this service's own AUTHGUARD_URL/CARRIERNODE_URL env vars) until ProjectConfigurator's
|
||||
// first push arrives - or forever, if this deployment isn't registered with a ProjectConfigurator
|
||||
// instance at all, in which case those env vars keep working exactly as before.
|
||||
type Store struct {
|
||||
mu sync.RWMutex
|
||||
info Info
|
||||
onUpdate func(Info)
|
||||
}
|
||||
|
||||
// NewStore returns a Store seeded with fallback.
|
||||
func NewStore(fallback Info) *Store {
|
||||
return &Store{info: fallback}
|
||||
}
|
||||
|
||||
// Get returns the current Info.
|
||||
func (s *Store) Get() Info {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
return s.info
|
||||
}
|
||||
|
||||
// AuthGuardURL is a convenience accessor for Get().AuthGuardURL - the natural value to feed an
|
||||
// authclient.Client's SetBaseURL, or (via OnUpdate) to keep one continuously up to date.
|
||||
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
|
||||
// context.Context) string { return store.CarrierNodeURL() }).
|
||||
func (s *Store) CarrierNodeURL() string { return s.Get().CarrierNodeURL }
|
||||
|
||||
// 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
|
||||
// is enough for every current caller (one authclient.Client per service).
|
||||
func (s *Store) OnUpdate(fn func(Info)) {
|
||||
s.mu.Lock()
|
||||
s.onUpdate = fn
|
||||
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.
|
||||
func (s *Store) Set(next Info) {
|
||||
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
|
||||
}
|
||||
current := s.info
|
||||
callback := s.onUpdate
|
||||
s.mu.Unlock()
|
||||
if callback != nil {
|
||||
callback(current)
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
// INTERNAL_SERVICE_SECRET (the same value ProjectConfigurator and CarrierNode were configured
|
||||
// with - an empty secret refuses every caller, the same fail-closed default ProjectConfigurator's
|
||||
// own internal-auth-gated routes use).
|
||||
func Handler(secret string, store *Store) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if secret == "" || r.Header.Get("X-Internal-Auth") != secret {
|
||||
writeError(w, http.StatusUnauthorized, "internal service authentication required")
|
||||
return
|
||||
}
|
||||
var input Info
|
||||
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4<<10)).Decode(&input); err != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
store.Set(input)
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "info": store.Get()})
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, message string) {
|
||||
writeJSON(w, status, map[string]string{"error": message})
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package coreconfig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestStoreSetIgnoresBlankFields(t *testing.T) {
|
||||
store := NewStore(Info{AuthGuardURL: "http://auth.example.com", CarrierNodeURL: "http://carrier.example.com"})
|
||||
|
||||
store.Set(Info{CarrierNodeURL: "http://carrier2.example.com"})
|
||||
|
||||
got := store.Get()
|
||||
if got.AuthGuardURL != "http://auth.example.com" {
|
||||
t.Fatalf("AuthGuardURL = %q, want unchanged", got.AuthGuardURL)
|
||||
}
|
||||
if got.CarrierNodeURL != "http://carrier2.example.com" {
|
||||
t.Fatalf("CarrierNodeURL = %q, want updated", got.CarrierNodeURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoreOnUpdateFiresWithNewInfo(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
var got Info
|
||||
calls := 0
|
||||
store.OnUpdate(func(info Info) {
|
||||
calls++
|
||||
got = info
|
||||
})
|
||||
|
||||
store.Set(Info{AuthGuardURL: "http://auth.example.com"})
|
||||
|
||||
if calls != 1 {
|
||||
t.Fatalf("OnUpdate called %d times, want 1", calls)
|
||||
}
|
||||
if got.AuthGuardURL != "http://auth.example.com" {
|
||||
t.Fatalf("OnUpdate saw AuthGuardURL = %q, want http://auth.example.com", got.AuthGuardURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerRejectsMissingOrWrongSecret(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
handler := Handler("s3cret", store)
|
||||
body, _ := json.Marshal(Info{AuthGuardURL: "http://auth.example.com"})
|
||||
|
||||
cases := []string{"", "wrong"}
|
||||
for _, header := range cases {
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
|
||||
if header != "" {
|
||||
req.Header.Set("X-Internal-Auth", header)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
handler(rec, req)
|
||||
if rec.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want 401 for X-Internal-Auth=%q", rec.Code, header)
|
||||
}
|
||||
}
|
||||
if got := store.Get(); got.AuthGuardURL != "" {
|
||||
t.Fatalf("store was updated despite auth failure: %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerAppliesPushedInfo(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
handler := Handler("s3cret", store)
|
||||
body, _ := json.Marshal(Info{AuthGuardURL: "http://auth.example.com", CarrierNodeURL: "http://carrier.example.com"})
|
||||
|
||||
req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
|
||||
req.Header.Set("X-Internal-Auth", "s3cret")
|
||||
rec := httptest.NewRecorder()
|
||||
handler(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200: %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
got := store.Get()
|
||||
if got.AuthGuardURL != "http://auth.example.com" || got.CarrierNodeURL != "http://carrier.example.com" {
|
||||
t.Fatalf("store = %+v, want pushed values applied", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user