feat: forward optional FastAPI control token
This commit is contained in:
1 parent
4b9c2da9f7
commit
a88bfad1d4
5 files changed
+50
-3
No files matched your search
+1
-1
@@ -36,7 +36,7 @@ func main() {
|
|||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
Addr: cfg.Addr,
|
Addr: cfg.Addr,
|
||||||
Handler: api.New(cfg, st, ml.New(cfg.FastAPIURL, cfg.MLTimeout), hub).Handler(),
|
Handler: api.New(cfg, st, ml.NewWithToken(cfg.FastAPIURL, cfg.MLTimeout, cfg.ControlToken), hub).Handler(),
|
||||||
ReadHeaderTimeout: 5 * time.Second,
|
ReadHeaderTimeout: 5 * time.Second,
|
||||||
IdleTimeout: 120 * time.Second,
|
IdleTimeout: 120 * time.Second,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,13 +14,15 @@ type Config struct {
|
|||||||
FastAPIURL string
|
FastAPIURL string
|
||||||
ControlUser string
|
ControlUser string
|
||||||
ControlPass string
|
ControlPass string
|
||||||
|
ControlToken string
|
||||||
Heartbeat time.Duration
|
Heartbeat time.Duration
|
||||||
MLTimeout time.Duration
|
MLTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load reads configuration from env with local-development defaults.
|
// Load reads configuration from env with local-development defaults.
|
||||||
// CONTROL_USER / CONTROL_PASS have no defaults: POST /api/scenario fails
|
// CONTROL_USER / CONTROL_PASS have no defaults: POST /api/scenario fails
|
||||||
// closed when they are unset.
|
// closed when they are unset. CONTROL_TOKEN is optional and, when set, is
|
||||||
|
// forwarded to FastAPI's internal /train endpoints as a Bearer token.
|
||||||
func Load() Config {
|
func Load() Config {
|
||||||
port := getenv("PORT", "8080")
|
port := getenv("PORT", "8080")
|
||||||
return Config{
|
return Config{
|
||||||
@@ -29,6 +31,7 @@ func Load() Config {
|
|||||||
FastAPIURL: getenv("FASTAPI_URL", "http://localhost:8000"),
|
FastAPIURL: getenv("FASTAPI_URL", "http://localhost:8000"),
|
||||||
ControlUser: os.Getenv("CONTROL_USER"),
|
ControlUser: os.Getenv("CONTROL_USER"),
|
||||||
ControlPass: os.Getenv("CONTROL_PASS"),
|
ControlPass: os.Getenv("CONTROL_PASS"),
|
||||||
|
ControlToken: os.Getenv("CONTROL_TOKEN"),
|
||||||
Heartbeat: durationEnv("SSE_HEARTBEAT", 15*time.Second),
|
Heartbeat: durationEnv("SSE_HEARTBEAT", 15*time.Second),
|
||||||
MLTimeout: durationEnv("ML_TIMEOUT", 1500*time.Millisecond),
|
MLTimeout: durationEnv("ML_TIMEOUT", 1500*time.Millisecond),
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadDefaults(t *testing.T) {
|
func TestLoadDefaults(t *testing.T) {
|
||||||
for _, key := range []string{"PORT", "DATA_DIR", "FASTAPI_URL", "CONTROL_USER", "CONTROL_PASS", "SSE_HEARTBEAT", "ML_TIMEOUT"} {
|
for _, key := range []string{"PORT", "DATA_DIR", "FASTAPI_URL", "CONTROL_USER", "CONTROL_PASS", "CONTROL_TOKEN", "SSE_HEARTBEAT", "ML_TIMEOUT"} {
|
||||||
t.Setenv(key, "")
|
t.Setenv(key, "")
|
||||||
}
|
}
|
||||||
cfg := Load()
|
cfg := Load()
|
||||||
@@ -25,6 +25,9 @@ func TestLoadDefaults(t *testing.T) {
|
|||||||
if cfg.ControlAuthConfigured() {
|
if cfg.ControlAuthConfigured() {
|
||||||
t.Error("control auth should be unconfigured by default")
|
t.Error("control auth should be unconfigured by default")
|
||||||
}
|
}
|
||||||
|
if cfg.ControlToken != "" {
|
||||||
|
t.Errorf("ControlToken=%q, want empty by default", cfg.ControlToken)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestLoadFromEnv(t *testing.T) {
|
func TestLoadFromEnv(t *testing.T) {
|
||||||
@@ -33,6 +36,7 @@ func TestLoadFromEnv(t *testing.T) {
|
|||||||
t.Setenv("FASTAPI_URL", "http://ml:8000")
|
t.Setenv("FASTAPI_URL", "http://ml:8000")
|
||||||
t.Setenv("CONTROL_USER", "user")
|
t.Setenv("CONTROL_USER", "user")
|
||||||
t.Setenv("CONTROL_PASS", "pass")
|
t.Setenv("CONTROL_PASS", "pass")
|
||||||
|
t.Setenv("CONTROL_TOKEN", "token-123")
|
||||||
t.Setenv("SSE_HEARTBEAT", "5s")
|
t.Setenv("SSE_HEARTBEAT", "5s")
|
||||||
t.Setenv("ML_TIMEOUT", "2s")
|
t.Setenv("ML_TIMEOUT", "2s")
|
||||||
|
|
||||||
@@ -40,6 +44,9 @@ func TestLoadFromEnv(t *testing.T) {
|
|||||||
if cfg.Addr != ":9999" || cfg.DataDir != "/data" || cfg.FastAPIURL != "http://ml:8000" {
|
if cfg.Addr != ":9999" || cfg.DataDir != "/data" || cfg.FastAPIURL != "http://ml:8000" {
|
||||||
t.Fatalf("cfg=%+v", cfg)
|
t.Fatalf("cfg=%+v", cfg)
|
||||||
}
|
}
|
||||||
|
if cfg.ControlToken != "token-123" {
|
||||||
|
t.Fatalf("ControlToken=%q, want token-123", cfg.ControlToken)
|
||||||
|
}
|
||||||
if cfg.Heartbeat != 5*time.Second || cfg.MLTimeout != 2*time.Second {
|
if cfg.Heartbeat != 5*time.Second || cfg.MLTimeout != 2*time.Second {
|
||||||
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
t.Fatalf("timeouts: heartbeat=%s ml=%s", cfg.Heartbeat, cfg.MLTimeout)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,10 +28,17 @@ type Client struct {
|
|||||||
baseURL string
|
baseURL string
|
||||||
http *http.Client
|
http *http.Client
|
||||||
control *http.Client
|
control *http.Client
|
||||||
|
controlToken string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a client for baseURL (for example http://localhost:8000).
|
// New returns a client for baseURL (for example http://localhost:8000).
|
||||||
func New(baseURL string, timeout time.Duration) *Client {
|
func New(baseURL string, timeout time.Duration) *Client {
|
||||||
|
return NewWithToken(baseURL, timeout, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewWithToken is New plus the Bearer token FastAPI's internal /train
|
||||||
|
// endpoints expect. An empty token sends no Authorization header.
|
||||||
|
func NewWithToken(baseURL string, timeout time.Duration, controlToken string) *Client {
|
||||||
if timeout <= 0 {
|
if timeout <= 0 {
|
||||||
timeout = DefaultTimeout
|
timeout = DefaultTimeout
|
||||||
}
|
}
|
||||||
@@ -39,6 +46,7 @@ func New(baseURL string, timeout time.Duration) *Client {
|
|||||||
baseURL: strings.TrimRight(baseURL, "/"),
|
baseURL: strings.TrimRight(baseURL, "/"),
|
||||||
http: &http.Client{Timeout: timeout},
|
http: &http.Client{Timeout: timeout},
|
||||||
control: &http.Client{Timeout: controlTimeout},
|
control: &http.Client{Timeout: controlTimeout},
|
||||||
|
controlToken: controlToken,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,6 +147,9 @@ func (c *Client) controlRequest(ctx context.Context, method, path, contentType s
|
|||||||
req.Header.Set("Content-Type", contentType)
|
req.Header.Set("Content-Type", contentType)
|
||||||
}
|
}
|
||||||
req.Header.Set("Accept", "application/json")
|
req.Header.Set("Accept", "application/json")
|
||||||
|
if c.controlToken != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+c.controlToken)
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := c.control.Do(req)
|
resp, err := c.control.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -160,6 +160,32 @@ func TestTrainForwardsBodyContentTypeAndStatus(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestControlRequestsForwardBearerToken(t *testing.T) {
|
||||||
|
var got []string
|
||||||
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
got = append(got, r.Header.Get("Authorization"))
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
if r.Method == http.MethodPost {
|
||||||
|
w.WriteHeader(http.StatusAccepted)
|
||||||
|
}
|
||||||
|
_, _ = w.Write([]byte(`{"job_id":"job-1","status":"queued"}`))
|
||||||
|
}))
|
||||||
|
defer srv.Close()
|
||||||
|
|
||||||
|
client := NewWithToken(srv.URL, time.Second, "token-123")
|
||||||
|
if _, _, err := client.Train(context.Background(), "application/json", strings.NewReader(`{"path":"/tmp/x.csv"}`)); err != nil {
|
||||||
|
t.Fatalf("Train: %v", err)
|
||||||
|
}
|
||||||
|
if _, _, err := client.TrainStatus(context.Background(), "job-1"); err != nil {
|
||||||
|
t.Fatalf("TrainStatus: %v", err)
|
||||||
|
}
|
||||||
|
for i, auth := range got {
|
||||||
|
if auth != "Bearer token-123" {
|
||||||
|
t.Errorf("request %d Authorization=%q, want Bearer token-123", i, auth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTrainStatusForwardsJobIDAndUpstreamError(t *testing.T) {
|
func TestTrainStatusForwardsJobIDAndUpstreamError(t *testing.T) {
|
||||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodGet {
|
if r.Method != http.MethodGet {
|
||||||
|
|||||||
Reference in new issue
Block a user