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
@@ -23,6 +23,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -48,8 +49,12 @@ const DefaultCookieName = "auth_session"
|
||||
|
||||
// Client calls a centralized AuthServer instance over HTTP.
|
||||
type Client struct {
|
||||
// BaseURL is AuthServer's base URL, e.g. "https://auth.example.com"
|
||||
// (no trailing slash required).
|
||||
// baseURLMu guards baseURL so SetBaseURL can be called concurrently with
|
||||
// Validate/Middleware/the rest of the API surface - see SetBaseURL.
|
||||
baseURLMu sync.RWMutex
|
||||
// baseURL is AuthServer's base URL, e.g. "https://auth.example.com" (no
|
||||
// trailing slash required). Read/written only through getBaseURL/
|
||||
// SetBaseURL once construction is done.
|
||||
baseURL string
|
||||
// HTTPClient is used for every request; defaults to a client with a
|
||||
// short timeout suitable for a per-request auth check.
|
||||
@@ -95,6 +100,30 @@ func New(baseURL string, opts ...Option) *Client {
|
||||
return c
|
||||
}
|
||||
|
||||
// SetBaseURL atomically replaces the AuthServer base URL this Client calls,
|
||||
// normalizing it the same way New does. Safe to call concurrently with
|
||||
// Validate/Middleware/any other method on this Client - the intended use is
|
||||
// a service wiring this up as a coreconfig.Store's OnUpdate callback, so a
|
||||
// fresh authguard_url pushed from ProjectConfigurator takes effect on this
|
||||
// Client's very next call with no restart. A blank baseURL is ignored
|
||||
// (leaving the previous value in place) rather than leaving the client
|
||||
// unable to reach AuthServer at all.
|
||||
func (c *Client) SetBaseURL(baseURL string) {
|
||||
normalized := normalizeBaseURL(baseURL)
|
||||
if normalized == "" {
|
||||
return
|
||||
}
|
||||
c.baseURLMu.Lock()
|
||||
c.baseURL = normalized
|
||||
c.baseURLMu.Unlock()
|
||||
}
|
||||
|
||||
func (c *Client) getBaseURL() string {
|
||||
c.baseURLMu.RLock()
|
||||
defer c.baseURLMu.RUnlock()
|
||||
return c.baseURL
|
||||
}
|
||||
|
||||
func normalizeBaseURL(baseURL string) string {
|
||||
trimmed := strings.TrimRight(strings.TrimSpace(baseURL), "/")
|
||||
if trimmed == "" {
|
||||
@@ -140,7 +169,11 @@ func isPrivateHost(hostport string) bool {
|
||||
// ErrInvalidSession (wrapped) when AuthServer reports the session as not
|
||||
// valid, and a plain error for any transport/decoding failure.
|
||||
func (c *Client) Validate(ctx context.Context, token string) (*User, error) {
|
||||
if c == nil || c.baseURL == "" {
|
||||
if c == nil {
|
||||
return nil, errors.New("authclient: client has no base URL")
|
||||
}
|
||||
baseURL := c.getBaseURL()
|
||||
if baseURL == "" {
|
||||
return nil, errors.New("authclient: client has no base URL")
|
||||
}
|
||||
token = strings.TrimSpace(token)
|
||||
@@ -148,7 +181,7 @@ func (c *Client) Validate(ctx context.Context, token string) (*User, error) {
|
||||
return nil, ErrInvalidSession
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+"/api/session/validate", nil)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/api/session/validate", nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("authclient: build request: %w", err)
|
||||
}
|
||||
@@ -359,7 +392,11 @@ func ForwardCookies(w http.ResponseWriter, cookies []*http.Cookie) {
|
||||
// on a transport-level failure) is provided so callers can read Cookies();
|
||||
// its Body is always already fully read and closed.
|
||||
func (c *Client) call(ctx context.Context, method, path, token string, body, out any) (*http.Response, error) {
|
||||
if c == nil || c.baseURL == "" {
|
||||
if c == nil {
|
||||
return nil, errors.New("authclient: client has no base URL")
|
||||
}
|
||||
baseURL := c.getBaseURL()
|
||||
if baseURL == "" {
|
||||
return nil, errors.New("authclient: client has no base URL")
|
||||
}
|
||||
|
||||
@@ -371,7 +408,7 @@ func (c *Client) call(ctx context.Context, method, path, token string, body, out
|
||||
}
|
||||
reader = bytes.NewReader(data)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, method, c.baseURL+path, reader)
|
||||
req, err := http.NewRequestWithContext(ctx, method, baseURL+path, reader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("authclient: build request: %w", err)
|
||||
}
|
||||
|
||||
@@ -83,3 +83,30 @@ func TestSetupStatusPropagatesTransportError(t *testing.T) {
|
||||
t.Fatal("SetupStatus() expected an error against an unreachable server, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetBaseURLRetargetsSubsequentCalls(t *testing.T) {
|
||||
client := New("http://127.0.0.1:0")
|
||||
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]bool{"setup_required": true})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client.SetBaseURL(server.URL)
|
||||
got, err := client.SetupStatus(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("SetupStatus() after SetBaseURL error = %v", err)
|
||||
}
|
||||
if !got {
|
||||
t.Fatalf("SetupStatus() = %v, want true", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetBaseURLIgnoresBlank(t *testing.T) {
|
||||
client := New("https://auth.example.com")
|
||||
client.SetBaseURL(" ")
|
||||
if got := client.getBaseURL(); got != "https://auth.example.com" {
|
||||
t.Fatalf("SetBaseURL(blank) changed baseURL to %q, want unchanged", got)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user