Sync shared snapshot from 8701874
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
01b18be1e4
commit
362cee26fd
20 files changed
+4908
-61
No files matched your search
@@ -78,6 +78,16 @@ type Info struct {
|
||||
// ProjectConfigurator includes it so consumers with independent databases can
|
||||
// authenticate storage requests without falling back to their own SECRET.
|
||||
CarrierNodeSecret string `json:"carrierNodeSecret,omitempty"`
|
||||
|
||||
// OmniLoggerURL is the OmniLogger instance that receives this service's log entries. Services
|
||||
// that ship logs use it with OmniLoggerIngestToken; both are optional, so a service that does not
|
||||
// ship logs ignores them.
|
||||
OmniLoggerURL string `json:"omniLoggerUrl,omitempty"`
|
||||
|
||||
// OmniLoggerIngestToken is the ingest token ProjectConfigurator minted for this receiving service.
|
||||
// OmniLogger binds it to the service's name, so it can only write this service's own entries.
|
||||
// It is a per-service credential, not a SECRET, and is never echoed back in the response.
|
||||
OmniLoggerIngestToken string `json:"omniLoggerIngestToken,omitempty"`
|
||||
}
|
||||
|
||||
// Store holds the most recently pushed Info, defaulting to whatever fallback NewStore was given
|
||||
@@ -85,9 +95,10 @@ type Info struct {
|
||||
// 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)
|
||||
mu sync.RWMutex
|
||||
info Info
|
||||
onUpdate func(Info)
|
||||
listeners []func(Info)
|
||||
}
|
||||
|
||||
// NewStore returns a Store seeded with fallback.
|
||||
@@ -132,6 +143,12 @@ func (s *Store) CarrierNodeURLs() []string { return s.Get().CarrierNodeURLs }
|
||||
// CarrierNodeSecret is a convenience accessor for Get().CarrierNodeSecret.
|
||||
func (s *Store) CarrierNodeSecret() string { return s.Get().CarrierNodeSecret }
|
||||
|
||||
// OmniLoggerURL is a convenience accessor for Get().OmniLoggerURL.
|
||||
func (s *Store) OmniLoggerURL() string { return s.Get().OmniLoggerURL }
|
||||
|
||||
// OmniLoggerIngestToken is a convenience accessor for Get().OmniLoggerIngestToken.
|
||||
func (s *Store) OmniLoggerIngestToken() string { return s.Get().OmniLoggerIngestToken }
|
||||
|
||||
// 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
|
||||
@@ -142,6 +159,16 @@ func (s *Store) OnUpdate(fn func(Info)) {
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
// Subscribe registers fn to be called (synchronously, so keep it fast) with the new Info after every
|
||||
// Set, in addition to OnUpdate's single callback, which it does not replace. Use it when a service
|
||||
// has more than one component that follows the pushed values, such as an authclient.Client and a
|
||||
// log shipper. Subscribers cannot be removed; they live as long as the Store.
|
||||
func (s *Store) Subscribe(fn func(Info)) {
|
||||
s.mu.Lock()
|
||||
s.listeners = append(s.listeners, fn)
|
||||
s.mu.Unlock()
|
||||
}
|
||||
|
||||
// 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) {
|
||||
@@ -165,16 +192,26 @@ func (s *Store) Set(next Info) {
|
||||
if secret := strings.TrimSpace(next.CarrierNodeSecret); secret != "" {
|
||||
s.info.CarrierNodeSecret = secret
|
||||
}
|
||||
if url := strings.TrimRight(strings.TrimSpace(next.OmniLoggerURL), "/"); url != "" {
|
||||
s.info.OmniLoggerURL = url
|
||||
}
|
||||
if token := strings.TrimSpace(next.OmniLoggerIngestToken); token != "" {
|
||||
s.info.OmniLoggerIngestToken = token
|
||||
}
|
||||
if len(next.CarrierNodeURLs) > 0 {
|
||||
s.info.CarrierNodeURLs = next.CarrierNodeURLs
|
||||
s.info.CarrierNodeURL = next.CarrierNodeURLs[0]
|
||||
}
|
||||
current := s.info
|
||||
callback := s.onUpdate
|
||||
listeners := append([]func(Info){}, s.listeners...)
|
||||
s.mu.Unlock()
|
||||
if callback != nil {
|
||||
callback(current)
|
||||
}
|
||||
for _, fn := range listeners {
|
||||
fn(current)
|
||||
}
|
||||
}
|
||||
|
||||
// normalizeInfo trims/drops blank CarrierNodeURLs entries and fills in CarrierNodeURLs from a
|
||||
@@ -228,6 +265,7 @@ func Handler(secret string, store *Store) http.HandlerFunc {
|
||||
info := store.Get()
|
||||
info.CarrierNodeSecret = ""
|
||||
info.ContentScannerSecret = ""
|
||||
info.OmniLoggerIngestToken = ""
|
||||
writeJSON(w, http.StatusOK, map[string]any{"ok": true, "info": info})
|
||||
}
|
||||
}
|
||||
@@ -235,7 +273,7 @@ func Handler(secret string, store *Store) http.HandlerFunc {
|
||||
// validateServiceURLs refuses a pushed URL that is not an http or https URL with a host, so a push
|
||||
// cannot point this service's clients at another scheme or at an empty host.
|
||||
func validateServiceURLs(info Info) error {
|
||||
urls := append([]string{info.AuthGuardURL, info.ComfyUIURL, info.ProjectConfiguratorURL, info.ContentScannerURL, info.CarrierNodeURL}, info.CarrierNodeURLs...)
|
||||
urls := append([]string{info.AuthGuardURL, info.ComfyUIURL, info.ProjectConfiguratorURL, info.ContentScannerURL, info.CarrierNodeURL, info.OmniLoggerURL}, info.CarrierNodeURLs...)
|
||||
for _, raw := range urls {
|
||||
value := strings.TrimSpace(raw)
|
||||
if value == "" {
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package coreconfig
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const omniTestSecret = "omnilogger-fields-test-secret-0123456789"
|
||||
|
||||
func TestOmniLoggerFieldsKeepTheirValueAcrossPartialPushes(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
store.Set(Info{OmniLoggerURL: "https://logs.example.com/", OmniLoggerIngestToken: "token-one"})
|
||||
if store.OmniLoggerURL() != "https://logs.example.com" || store.OmniLoggerIngestToken() != "token-one" {
|
||||
t.Fatalf("got url=%q token=%q", store.OmniLoggerURL(), store.OmniLoggerIngestToken())
|
||||
}
|
||||
store.Set(Info{AuthGuardURL: "https://auth.example.com"})
|
||||
if store.OmniLoggerIngestToken() != "token-one" {
|
||||
t.Fatal("a push without a token cleared the stored token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandlerNeverEchoesTheOmniLoggerIngestToken(t *testing.T) {
|
||||
store := NewStore(Info{})
|
||||
handler := Handler(omniTestSecret, store)
|
||||
request := httptest.NewRequest(http.MethodPut, "/api/core-services/internal",
|
||||
strings.NewReader(`{"omniLoggerUrl":"https://logs.example.com","omniLoggerIngestToken":"token-two"}`))
|
||||
request.Header.Set("X-Internal-Auth", omniTestSecret)
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, request)
|
||||
if response.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d: %s", response.Code, response.Body.String())
|
||||
}
|
||||
if strings.Contains(response.Body.String(), "token-two") {
|
||||
t.Fatalf("response echoed the ingest token: %s", response.Body.String())
|
||||
}
|
||||
if store.OmniLoggerIngestToken() != "token-two" {
|
||||
t.Fatal("the pushed token was not stored")
|
||||
}
|
||||
}
|
||||
|
||||
func TestOmniLoggerURLMustBeHTTPWithAHost(t *testing.T) {
|
||||
if err := validateServiceURLs(Info{OmniLoggerURL: "ftp://logs.example.com"}); err == nil {
|
||||
t.Fatal("accepted a non-http OmniLogger URL")
|
||||
}
|
||||
if err := validateServiceURLs(Info{OmniLoggerURL: "https://"}); err == nil {
|
||||
t.Fatal("accepted an OmniLogger URL without a host")
|
||||
}
|
||||
if err := validateServiceURLs(Info{OmniLoggerURL: "http://omnilogger:8080"}); err != nil {
|
||||
t.Fatalf("rejected a valid OmniLogger URL: %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user