Support pushed CarrierNode credentials
This commit is contained in:
1 parent
4b0566ea46
commit
e4c1826d6a
3 files changed
+43
-5
No files matched your search
@@ -41,6 +41,12 @@ type Client struct {
|
|||||||
// calls from public traffic. Empty means no header is sent.
|
// calls from public traffic. Empty means no header is sent.
|
||||||
InternalServiceSecret string
|
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
|
// BaseURL resolves CarrierNode's current base URL for a request. It is a
|
||||||
// function rather than a fixed string because CarrierNode's URL is
|
// function rather than a fixed string because CarrierNode's URL is
|
||||||
// configured live through ProjectConfigurator's Projects workspace
|
// 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if c.InternalServiceSecret != "" {
|
secret := c.InternalServiceSecret
|
||||||
request.Header.Set("X-Internal-Auth", c.InternalServiceSecret)
|
if c.InternalServiceSecretFunc != nil {
|
||||||
|
secret = strings.TrimSpace(c.InternalServiceSecretFunc(ctx))
|
||||||
|
}
|
||||||
|
if secret != "" {
|
||||||
|
request.Header.Set("X-Internal-Auth", secret)
|
||||||
}
|
}
|
||||||
return request, nil
|
return request, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,11 @@ type Info struct {
|
|||||||
// discover files across every configured storage backend / fail over between replicas wire
|
// discover files across every configured storage backend / fail over between replicas wire
|
||||||
// this through carriernodeclient.NewMulti(secret, store.CarrierNodeURLs) instead.
|
// this through carriernodeclient.NewMulti(secret, store.CarrierNodeURLs) instead.
|
||||||
CarrierNodeURLs []string `json:"carrierNodeUrls"`
|
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
|
// 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() }).
|
// store.CarrierNodeURLs() }).
|
||||||
func (s *Store) CarrierNodeURLs() []string { return s.Get().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
|
// 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
|
// 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
|
// 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 != "" {
|
if url := strings.TrimRight(strings.TrimSpace(next.ProjectConfiguratorURL), "/"); url != "" {
|
||||||
s.info.ProjectConfiguratorURL = url
|
s.info.ProjectConfiguratorURL = url
|
||||||
}
|
}
|
||||||
|
if secret := strings.TrimSpace(next.CarrierNodeSecret); secret != "" {
|
||||||
|
s.info.CarrierNodeSecret = secret
|
||||||
|
}
|
||||||
if len(next.CarrierNodeURLs) > 0 {
|
if len(next.CarrierNodeURLs) > 0 {
|
||||||
s.info.CarrierNodeURLs = next.CarrierNodeURLs
|
s.info.CarrierNodeURLs = next.CarrierNodeURLs
|
||||||
s.info.CarrierNodeURL = next.CarrierNodeURLs[0]
|
s.info.CarrierNodeURL = next.CarrierNodeURLs[0]
|
||||||
@@ -162,7 +173,11 @@ func Handler(secret string, store *Store) http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
store.Set(input)
|
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})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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) {
|
func TestStoreOnUpdateFiresWithNewInfo(t *testing.T) {
|
||||||
store := NewStore(Info{})
|
store := NewStore(Info{})
|
||||||
var got Info
|
var got Info
|
||||||
@@ -114,7 +127,7 @@ func equalStrings(got, want []string) bool {
|
|||||||
func TestHandlerAppliesPushedInfo(t *testing.T) {
|
func TestHandlerAppliesPushedInfo(t *testing.T) {
|
||||||
store := NewStore(Info{})
|
store := NewStore(Info{})
|
||||||
handler := Handler("s3cret", store)
|
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 := httptest.NewRequest(http.MethodPut, "/api/core-services/internal", bytes.NewReader(body))
|
||||||
req.Header.Set("X-Internal-Auth", "s3cret")
|
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())
|
t.Fatalf("status = %d, want 200: %s", rec.Code, rec.Body.String())
|
||||||
}
|
}
|
||||||
got := store.Get()
|
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)
|
t.Fatalf("store = %+v, want pushed values applied", got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user