From e4c1826d6aef0054cd489ea915e311825f8d7367 Mon Sep 17 00:00:00 2001 From: Gabriel20xx Date: Wed, 16 Sep 2026 19:52:57 +0200 Subject: [PATCH] Support pushed CarrierNode credentials --- backend/carriernodeclient/carriernodeclient.go | 14 ++++++++++++-- backend/coreconfig/coreconfig.go | 17 ++++++++++++++++- backend/coreconfig/coreconfig_test.go | 17 +++++++++++++++-- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/backend/carriernodeclient/carriernodeclient.go b/backend/carriernodeclient/carriernodeclient.go index 0ce5258..de21876 100644 --- a/backend/carriernodeclient/carriernodeclient.go +++ b/backend/carriernodeclient/carriernodeclient.go @@ -41,6 +41,12 @@ type Client struct { // calls from public traffic. Empty means no header is sent. InternalServiceSecret string + // InternalServiceSecretFunc, when set, resolves the current secret for + // each request. This lets callers use ProjectConfigurator's live + // per-service secret while retaining InternalServiceSecret as the + // bootstrap/fallback value. + InternalServiceSecretFunc func(ctx context.Context) string + // BaseURL resolves CarrierNode's current base URL for a request. It is a // function rather than a fixed string because CarrierNode's URL is // configured live through ProjectConfigurator's Projects workspace @@ -123,8 +129,12 @@ func (c *Client) newRequest(ctx context.Context, baseURL, method, path string, b if err != nil { return nil, err } - if c.InternalServiceSecret != "" { - request.Header.Set("X-Internal-Auth", c.InternalServiceSecret) + secret := c.InternalServiceSecret + if c.InternalServiceSecretFunc != nil { + secret = strings.TrimSpace(c.InternalServiceSecretFunc(ctx)) + } + if secret != "" { + request.Header.Set("X-Internal-Auth", secret) } return request, nil } diff --git a/backend/coreconfig/coreconfig.go b/backend/coreconfig/coreconfig.go index d76ca67..f3251e4 100644 --- a/backend/coreconfig/coreconfig.go +++ b/backend/coreconfig/coreconfig.go @@ -39,6 +39,11 @@ type Info struct { // discover files across every configured storage backend / fail over between replicas wire // this through carriernodeclient.NewMulti(secret, store.CarrierNodeURLs) instead. CarrierNodeURLs []string `json:"carrierNodeUrls"` + + // CarrierNodeSecret is the credential CarrierNode expects in X-Internal-Auth. + // ProjectConfigurator includes it so consumers with independent databases can + // authenticate storage requests without falling back to their own SECRET. + CarrierNodeSecret string `json:"carrierNodeSecret,omitempty"` } // Store holds the most recently pushed Info, defaulting to whatever fallback NewStore was given @@ -84,6 +89,9 @@ func (s *Store) CarrierNodeURL() string { return s.Get().CarrierNodeURL } // store.CarrierNodeURLs() }). 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 } + // 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 @@ -108,6 +116,9 @@ func (s *Store) Set(next Info) { if url := strings.TrimRight(strings.TrimSpace(next.ProjectConfiguratorURL), "/"); url != "" { s.info.ProjectConfiguratorURL = url } + if secret := strings.TrimSpace(next.CarrierNodeSecret); secret != "" { + s.info.CarrierNodeSecret = secret + } if len(next.CarrierNodeURLs) > 0 { s.info.CarrierNodeURLs = next.CarrierNodeURLs s.info.CarrierNodeURL = next.CarrierNodeURLs[0] @@ -162,7 +173,11 @@ func Handler(secret string, store *Store) http.HandlerFunc { return } store.Set(input) - writeJSON(w, http.StatusOK, map[string]any{"ok": true, "info": store.Get()}) + // Do not echo the CarrierNode credential to the caller. The push is + // authenticated, but the credential is needed only by the recipient. + info := store.Get() + info.CarrierNodeSecret = "" + writeJSON(w, http.StatusOK, map[string]any{"ok": true, "info": info}) } } diff --git a/backend/coreconfig/coreconfig_test.go b/backend/coreconfig/coreconfig_test.go index c435968..e5115de 100644 --- a/backend/coreconfig/coreconfig_test.go +++ b/backend/coreconfig/coreconfig_test.go @@ -32,6 +32,19 @@ func TestStoreSetComfyUIURL(t *testing.T) { } } +func TestStoreSetCarrierNodeSecret(t *testing.T) { + store := NewStore(Info{}) + store.Set(Info{CarrierNodeSecret: " carrier-secret "}) + + if got := store.CarrierNodeSecret(); got != "carrier-secret" { + t.Fatalf("CarrierNodeSecret = %q, want trimmed pushed secret", got) + } + store.Set(Info{}) + if got := store.CarrierNodeSecret(); got != "carrier-secret" { + t.Fatalf("blank push cleared CarrierNodeSecret to %q", got) + } +} + func TestStoreOnUpdateFiresWithNewInfo(t *testing.T) { store := NewStore(Info{}) var got Info @@ -114,7 +127,7 @@ func equalStrings(got, want []string) bool { 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"}) + body, _ := json.Marshal(Info{AuthGuardURL: "http://auth.example.com", CarrierNodeURL: "http://carrier.example.com", CarrierNodeSecret: "carrier-secret"}) req := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body)) req.Header.Set("X-Internal-Auth", "s3cret") @@ -125,7 +138,7 @@ func TestHandlerAppliesPushedInfo(t *testing.T) { 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" { + if got.AuthGuardURL != "http://auth.example.com" || got.CarrierNodeURL != "http://carrier.example.com" || got.CarrierNodeSecret != "carrier-secret" { t.Fatalf("store = %+v, want pushed values applied", got) } }