History

feat(amp): add hot-reload support for upstream URL and localhost restriction

hkfires committed 11 months ago

Commit 4a135f11 parent c4c02f44 files · +171 −49Browse files

Unified · Split

4 files
  1. internal/api/modules/amp/amp.go +62 −14
  2. internal/api/modules/amp/model_mapping_test.go +3 −3
  3. internal/api/modules/amp/routes.go +30 −21
  4. internal/api/modules/amp/routes_test.go +76 −11
internal/api/modules/amp/amp.go+62−14
@@ -27,12 +27,17 @@ type Option func(*AmpModule) · 26 unmodified lines
2727 type AmpModule struct {
2828 secretSource SecretSource
2929 proxy *httputil.ReverseProxy
30+ proxyMu sync.RWMutex // protects proxy for hot-reload
3031 accessManager *sdkaccess.Manager
3132 authMiddleware_ gin.HandlerFunc
3233 modelMapper *DefaultModelMapper
3334 enabled bool
3435 registerOnce sync.Once
3536
37+ // restrictToLocalhost controls localhost-only access for management routes (hot-reloadable)
38+ restrictToLocalhost bool
39+ restrictMu sync.RWMutex
40+
3641 // configMu protects lastConfig for partial reload comparison
3742 configMu sync.RWMutex
3843 lastConfig *config.AmpCode
@@ -115,6 +120,9 @@ func (m *AmpModule) Register(ctx modules.Context) error { · 76 unmodified lines
115120 settingsCopy := settings
116121 m.lastConfig = &settingsCopy
117122
123+ // Initialize localhost restriction setting (hot-reloadable)
124+ m.setRestrictToLocalhost(settings.RestrictManagementToLocalhost)
125+
118126 // Always register provider aliases - these work without an upstream
119127 m.registerProviderAliases(ctx.Engine, ctx.BaseHandler, auth)
120128
@@ -139,13 +147,12 @@ func (m *AmpModule) Register(ctx modules.Context) error { · 18 unmodified lines
139147 return
140148 }
141149
142− m.proxy = proxy
150+ m.setProxy(proxy)
143151 m.enabled = true
144152
145153 // Register management proxy routes (requires upstream)
146− // Restrict to localhost by default for security (prevents drive-by browser attacks)
147− handler := proxyHandler(proxy)
148− m.registerManagementRoutes(ctx.Engine, ctx.BaseHandler, handler, settings.RestrictManagementToLocalhost)
154+ // Uses dynamic middleware that checks m.IsRestrictedToLocalhost() for hot-reload support
155+ m.registerManagementRoutes(ctx.Engine, ctx.BaseHandler)
149156
150157 log.Infof("amp upstream proxy enabled for: %s", upstreamURL)
151158 log.Debug("amp provider alias routes registered")
@@ -172,7 +179,7 @@ func (m *AmpModule) getAuthMiddleware(ctx modules.Context) gin.HandlerFunc { · 20 unmodified lines
172179
173180 // OnConfigUpdated handles configuration updates with partial reload support.
174181 // Only updates components that have actually changed to avoid unnecessary work.
175−// URL changes still require restart (logged as warning).
182+// Supports hot-reload for: model-mappings, upstream-api-key, upstream-url, restrict-management-to-localhost.
176183 func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error {
177184 newSettings := cfg.AmpCode
178185
@@ -199,7 +206,7 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 20 unmodified lines
199206 }
200207
201208 if m.enabled {
202− // Check upstream URL change (requires restart)
209+ // Check upstream URL change - now supports hot-reload
203210 newUpstreamURL := strings.TrimSpace(newSettings.UpstreamURL)
204211 oldUpstreamURL := ""
205212 if oldSettings != nil {
@@ -207,10 +214,19 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 1 unmodified line
207214 }
208215
209216 if newUpstreamURL == "" && oldUpstreamURL != "" {
210− log.Warn("amp upstream URL removed from config, restart required to disable")
211− } else if newUpstreamURL != oldUpstreamURL {
212− changes = append(changes, "upstream-url(restart required)")
213− log.Warnf("amp config: upstream-url changed (%s -> %s), restart required", oldUpstreamURL, newUpstreamURL)
217+ log.Warn("amp upstream URL removed from config, proxy disabled until restart")
218+ m.setProxy(nil)
219+ changes = append(changes, "upstream-url(disabled)")
220+ } else if newUpstreamURL != oldUpstreamURL && newUpstreamURL != "" {
221+ // Recreate proxy with new URL
222+ proxy, err := createReverseProxy(newUpstreamURL, m.secretSource)
223+ if err != nil {
224+ log.Errorf("amp config: failed to create proxy for new upstream URL %s: %v", newUpstreamURL, err)
225+ } else {
226+ m.setProxy(proxy)
227+ changes = append(changes, "upstream-url")
228+ log.Infof("amp config partial reload: upstream URL updated (%s -> %s)", oldUpstreamURL, newUpstreamURL)
229+ }
214230 }
215231
216232 // Check API key change
@@ -226,11 +242,15 @@ func (m *AmpModule) OnConfigUpdated(cfg *config.Config) error { · 9 unmodified lines
226242 }
227243 }
228244
229− // Check restrict-management-to-localhost change (requires restart)
245+ // Check restrict-management-to-localhost change - now supports hot-reload
230246 if oldSettings != nil && oldSettings.RestrictManagementToLocalhost != newSettings.RestrictManagementToLocalhost {
231− changes = append(changes, "restrict-management-to-localhost(restart required)")
232− log.Warnf("amp config: restrict-management-to-localhost changed (%t -> %t), restart required",
233− oldSettings.RestrictManagementToLocalhost, newSettings.RestrictManagementToLocalhost)
247+ m.setRestrictToLocalhost(newSettings.RestrictManagementToLocalhost)
248+ changes = append(changes, "restrict-management-to-localhost")
249+ if newSettings.RestrictManagementToLocalhost {
250+ log.Infof("amp config partial reload: management routes now restricted to localhost")
251+ } else {
252+ log.Warnf("amp config partial reload: management routes now accessible from any IP - this is insecure!")
253+ }
234254 }
235255 }
236256
@@ -291,3 +311,31 @@ func (m *AmpModule) hasAPIKeyChanged(old *config.AmpCode, new *config.AmpCode) b · 54 unmodified lines
291311 func (m *AmpModule) GetModelMapper() *DefaultModelMapper {
292312 return m.modelMapper
293313 }
314+
315+// getProxy returns the current proxy instance (thread-safe for hot-reload).
316+func (m *AmpModule) getProxy() *httputil.ReverseProxy {
317+ m.proxyMu.RLock()
318+ defer m.proxyMu.RUnlock()
319+ return m.proxy
320+}
321+
322+// setProxy updates the proxy instance (thread-safe for hot-reload).
323+func (m *AmpModule) setProxy(proxy *httputil.ReverseProxy) {
324+ m.proxyMu.Lock()
325+ defer m.proxyMu.Unlock()
326+ m.proxy = proxy
327+}
328+
329+// IsRestrictedToLocalhost returns whether management routes are restricted to localhost.
330+func (m *AmpModule) IsRestrictedToLocalhost() bool {
331+ m.restrictMu.RLock()
332+ defer m.restrictMu.RUnlock()
333+ return m.restrictToLocalhost
334+}
335+
336+// setRestrictToLocalhost updates the localhost restriction setting.
337+func (m *AmpModule) setRestrictToLocalhost(restrict bool) {
338+ m.restrictMu.Lock()
339+ defer m.restrictMu.Unlock()
340+ m.restrictToLocalhost = restrict
341+}
internal/api/modules/amp/model_mapping_test.go+3−3
@@ -152,9 +152,9 @@ func TestModelMapper_UpdateMappings_SkipsInvalid(t *testing.T) { · 151 unmodified lines
152152 mapper := NewModelMapper(nil)
153153
154154 mapper.UpdateMappings([]config.AmpModelMapping{
155− {From: "", To: "model-b"}, // Invalid: empty from
156− {From: "model-a", To: ""}, // Invalid: empty to
157− {From: " ", To: "model-b"}, // Invalid: whitespace from
155+ {From: "", To: "model-b"}, // Invalid: empty from
156+ {From: "model-a", To: ""}, // Invalid: empty to
157+ {From: " ", To: "model-b"}, // Invalid: whitespace from
158158 {From: "model-c", To: "model-d"}, // Valid
159159 })
160160
internal/api/modules/amp/routes.go+30−21
@@ -14,15 +14,16 @@ import ( · 13 unmodified lines
1414 log "github.com/sirupsen/logrus"
1515 )
1616
17−// localhostOnlyMiddleware restricts access to localhost (127.0.0.1, ::1) only.
18−// Returns 403 Forbidden for non-localhost clients.
19−//
20−// Security: Uses RemoteAddr (actual TCP connection) instead of ClientIP() to prevent
21−// header spoofing attacks via X-Forwarded-For or similar headers. This means the
22−// middleware will not work correctly behind reverse proxies - users deploying behind
23−// nginx/Cloudflare should disable this feature and use firewall rules instead.
24−func localhostOnlyMiddleware() gin.HandlerFunc {
17+// localhostOnlyMiddleware returns a middleware that dynamically checks the module's
18+// localhost restriction setting. This allows hot-reload of the restriction without restarting.
19+func (m *AmpModule) localhostOnlyMiddleware() gin.HandlerFunc {
2520 return func(c *gin.Context) {
21+ // Check current setting (hot-reloadable)
22+ if !m.IsRestrictedToLocalhost() {
23+ c.Next()
24+ return
25+ }
26+
2627 // Use actual TCP connection address (RemoteAddr) to prevent header spoofing
2728 // This cannot be forged by X-Forwarded-For or other client-controlled headers
2829 remoteAddr := c.Request.RemoteAddr
@@ -79,21 +80,32 @@ func noCORSMiddleware() gin.HandlerFunc { · 50 unmodified lines
7980
8081 // registerManagementRoutes registers Amp management proxy routes
8182 // These routes proxy through to the Amp control plane for OAuth, user management, etc.
82−// If restrictToLocalhost is true, routes will only accept connections from 127.0.0.1/::1.
83−func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *handlers.BaseAPIHandler, proxyHandler gin.HandlerFunc, restrictToLocalhost bool) {
83+// Uses dynamic middleware and proxy getter for hot-reload support.
84+func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *handlers.BaseAPIHandler) {
8485 ampAPI := engine.Group("/api")
8586
8687 // Always disable CORS for management routes to prevent browser-based attacks
8788 ampAPI.Use(noCORSMiddleware())
8889
89− // Apply localhost-only restriction if configured
90− if restrictToLocalhost {
91− ampAPI.Use(localhostOnlyMiddleware())
90+ // Apply dynamic localhost-only restriction (hot-reloadable via m.IsRestrictedToLocalhost())
91+ ampAPI.Use(m.localhostOnlyMiddleware())
92+
93+ if m.IsRestrictedToLocalhost() {
9294 log.Info("amp management routes restricted to localhost only (CORS disabled)")
9395 } else {
9496 log.Warn("amp management routes are NOT restricted to localhost - this is insecure!")
9597 }
9698
99+ // Dynamic proxy handler that uses m.getProxy() for hot-reload support
100+ proxyHandler := func(c *gin.Context) {
101+ proxy := m.getProxy()
102+ if proxy == nil {
103+ c.JSON(503, gin.H{"error": "amp upstream proxy not available"})
104+ return
105+ }
106+ proxy.ServeHTTP(c.Writer, c.Request)
107+ }
108+
97109 // Management routes - these are proxied directly to Amp upstream
98110 ampAPI.Any("/internal", proxyHandler)
99111 ampAPI.Any("/internal/*path", proxyHandler)
@@ -114,11 +126,8 @@ func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *ha · 14 unmodified lines
114126 ampAPI.Any("/tab/*path", proxyHandler)
115127
116128 // Root-level routes that AMP CLI expects without /api prefix
117− // These need the same security middleware as the /api/* routes
118− rootMiddleware := []gin.HandlerFunc{noCORSMiddleware()}
119− if restrictToLocalhost {
120− rootMiddleware = append(rootMiddleware, localhostOnlyMiddleware())
121− }
129+ // These need the same security middleware as the /api/* routes (dynamic for hot-reload)
130+ rootMiddleware := []gin.HandlerFunc{noCORSMiddleware(), m.localhostOnlyMiddleware()}
122131 engine.GET("/threads.rss", append(rootMiddleware, proxyHandler)...)
123132
124133 // Root-level auth routes for CLI login flow
@@ -134,7 +143,7 @@ func (m *AmpModule) registerManagementRoutes(engine *gin.Engine, baseHandler *ha · 9 unmodified lines
134143 geminiHandlers := gemini.NewGeminiAPIHandler(baseHandler)
135144 geminiBridge := createGeminiBridgeHandler(geminiHandlers)
136145 geminiV1Beta1Fallback := NewFallbackHandler(func() *httputil.ReverseProxy {
137− return m.proxy
146+ return m.getProxy()
138147 })
139148 geminiV1Beta1Handler := geminiV1Beta1Fallback.WrapHandler(geminiBridge)
140149
@@ -177,10 +186,10 @@ func (m *AmpModule) registerProviderAliases(engine *gin.Engine, baseHandler *han · 36 unmodified lines
177186 openaiResponsesHandlers := openai.NewOpenAIResponsesAPIHandler(baseHandler)
178187
179188 // Create fallback handler wrapper that forwards to ampcode.com when provider not found
180− // Uses lazy evaluation to access proxy (which is created after routes are registered)
189+ // Uses m.getProxy() for hot-reload support (proxy can be updated at runtime)
181190 // Also includes model mapping support for routing unavailable models to alternatives
182191 fallbackHandler := NewFallbackHandlerWithMapper(func() *httputil.ReverseProxy {
183− return m.proxy
192+ return m.getProxy()
184193 }, m.modelMapper)
185194
186195 // Provider-specific routes under /api/provider/:provider
internal/api/modules/amp/routes_test.go+76−11
@@ -13,16 +13,26 @@ func TestRegisterManagementRoutes(t *testing.T) { · 12 unmodified lines
1313 gin.SetMode(gin.TestMode)
1414 r := gin.New()
1515
16− // Spy to track if proxy handler was called
16+ // Create module with proxy for testing
17+ m := &AmpModule{
18+ restrictToLocalhost: false, // disable localhost restriction for tests
19+ }
20+
21+ // Create a mock proxy that tracks calls
1722 proxyCalled := false
18− proxyHandler := func(c *gin.Context) {
23+ mockProxy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1924 proxyCalled = true
20− c.String(200, "proxied")
21− }
25+ w.WriteHeader(200)
26+ w.Write([]byte("proxied"))
27+ }))
28+ defer mockProxy.Close()
29+
30+ // Create real proxy to mock server
31+ proxy, _ := createReverseProxy(mockProxy.URL, NewStaticSecretSource(""))
32+ m.setProxy(proxy)
2233
23− m := &AmpModule{}
2434 base := &handlers.BaseAPIHandler{}
25− m.registerManagementRoutes(r, base, proxyHandler, false) // false = don't restrict to localhost in tests
35+ m.registerManagementRoutes(r, base)
2636
2737 managementPaths := []struct {
2838 path string
@@ -41,9 +51,9 @@ func TestRegisterManagementRoutes(t *testing.T) { · 12 unmodified lines
4151 {"/api/otel", http.MethodGet},
4252 {"/api/tab", http.MethodGet},
4353 {"/api/tab/some/path", http.MethodGet},
44− {"/auth", http.MethodGet}, // Root-level auth route
45− {"/auth/cli-login", http.MethodGet}, // CLI login flow
46− {"/auth/callback", http.MethodGet}, // OAuth callback
54+ {"/auth", http.MethodGet}, // Root-level auth route
55+ {"/auth/cli-login", http.MethodGet}, // CLI login flow
56+ {"/auth/callback", http.MethodGet}, // OAuth callback
4757 // Google v1beta1 bridge should still proxy non-model requests (GET) and allow POST
4858 {"/api/provider/google/v1beta1/models", http.MethodGet},
4959 {"/api/provider/google/v1beta1/models", http.MethodPost},
@@ -231,8 +241,13 @@ func TestLocalhostOnlyMiddleware_PreventsSpoofing(t *testing.T) { · 181 unmodified lines
231241 gin.SetMode(gin.TestMode)
232242 r := gin.New()
233243
234− // Apply localhost-only middleware
235− r.Use(localhostOnlyMiddleware())
244+ // Create module with localhost restriction enabled
245+ m := &AmpModule{
246+ restrictToLocalhost: true,
247+ }
248+
249+ // Apply dynamic localhost-only middleware
250+ r.Use(m.localhostOnlyMiddleware())
236251 r.GET("/test", func(c *gin.Context) {
237252 c.String(http.StatusOK, "ok")
238253 })
@@ -305,3 +320,53 @@ func TestLocalhostOnlyMiddleware_PreventsSpoofing(t *testing.T) { · 66 unmodified lines
305320 })
306321 }
307322 }
323+
324+func TestLocalhostOnlyMiddleware_HotReload(t *testing.T) {
325+ gin.SetMode(gin.TestMode)
326+ r := gin.New()
327+
328+ // Create module with localhost restriction initially enabled
329+ m := &AmpModule{
330+ restrictToLocalhost: true,
331+ }
332+
333+ // Apply dynamic localhost-only middleware
334+ r.Use(m.localhostOnlyMiddleware())
335+ r.GET("/test", func(c *gin.Context) {
336+ c.String(http.StatusOK, "ok")
337+ })
338+
339+ // Test 1: Remote IP should be blocked when restriction is enabled
340+ req := httptest.NewRequest(http.MethodGet, "/test", nil)
341+ req.RemoteAddr = "192.168.1.100:12345"
342+ w := httptest.NewRecorder()
343+ r.ServeHTTP(w, req)
344+
345+ if w.Code != http.StatusForbidden {
346+ t.Errorf("Expected 403 when restriction enabled, got %d", w.Code)
347+ }
348+
349+ // Test 2: Hot-reload - disable restriction
350+ m.setRestrictToLocalhost(false)
351+
352+ req = httptest.NewRequest(http.MethodGet, "/test", nil)
353+ req.RemoteAddr = "192.168.1.100:12345"
354+ w = httptest.NewRecorder()
355+ r.ServeHTTP(w, req)
356+
357+ if w.Code != http.StatusOK {
358+ t.Errorf("Expected 200 after disabling restriction, got %d", w.Code)
359+ }
360+
361+ // Test 3: Hot-reload - re-enable restriction
362+ m.setRestrictToLocalhost(true)
363+
364+ req = httptest.NewRequest(http.MethodGet, "/test", nil)
365+ req.RemoteAddr = "192.168.1.100:12345"
366+ w = httptest.NewRecorder()
367+ r.ServeHTTP(w, req)
368+
369+ if w.Code != http.StatusForbidden {
370+ t.Errorf("Expected 403 after re-enabling restriction, got %d", w.Code)
371+ }
372+}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t