Skip to content

Commit bcccb63

Browse files
committed
fix(orchestrator): honor automatic proxy declarations and templates (review #14)
Review-Finding: 14 Original-Commit: 5c8d3df277e38b403be5d3ca06cf27bf88ced224 Original-Author: Josh Allmann <joshua.allmann@gmail.com> Original-Author-Date: 2026-07-22T08:49:22-07:00 Original-Commit-Date: 2026-07-22T08:49:22-07:00
1 parent 1733d8f commit bcccb63

5 files changed

Lines changed: 254 additions & 19 deletions

File tree

‎orchestrator/app.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func init() {
2929
}
3030

3131
type Params struct {
32+
ProxyURLTemplate string `name:"proxy-url-template" optional:"true" toml:"proxy_url_template" descr:"Generated proxy URL with {proxy} in a hostname label or final path segment"`
3233
ConfigFile string `name:"config" configfile:"true" file:"true" optional:"true" toml:"-" descr:"TOML configuration path"`
3334
Listen string `name:"listen" default:"127.0.0.1:8935" toml:"listen" descr:"Public HTTP listener"`
3435
MetricsListen string `name:"metrics-listen" default:"127.0.0.1:8936" toml:"metrics_listen" descr:"Loopback metrics listener"`
@@ -90,6 +91,9 @@ func (p Params) Validate() error {
9091
return errors.New("invalid payment RPC URL")
9192
}
9293
}
94+
if err := validateProxyTemplate(p.ProxyURLTemplate); err != nil {
95+
return err
96+
}
9397
listenHost, _, err := net.SplitHostPort(p.Listen)
9498
if err != nil {
9599
return errors.New("listen must be host:port")
@@ -251,6 +255,7 @@ func Serve(parent context.Context, p Params, logOut io.Writer) error {
251255
return err
252256
}
253257
registry := NewRegistry(p.BootstrapSecret, p.ServiceURL, p.HeartbeatInterval, p.HeartbeatTTL)
258+
registry.proxyTemplate = p.ProxyURLTemplate
254259
var engine *pm.Engine
255260
var paymentStore *pm.SQLiteStore
256261
var paymentChain eth.PaymentChain

‎orchestrator/http.go‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,24 @@ func NewServer(registry *Registry, runnerPolicy, proxyPolicy destination.Policy,
7070
s.mux.HandleFunc("/apps/{runner_id}/session/{session_id}/app/{app_path...}", s.proxySession)
7171
s.mux.HandleFunc("/apps/{runner_id}/app", s.proxySingleShot)
7272
s.mux.HandleFunc("/apps/{runner_id}/app/{app_path...}", s.proxySingleShot)
73+
s.mux.HandleFunc("/run/{proxy_id}", s.proxyGenerated)
74+
s.mux.HandleFunc("/run/{proxy_id}/{app_path...}", s.proxyGenerated)
7375
s.mux.HandleFunc("/proxy/{proxy_id}", s.proxyGenerated)
7476
s.mux.HandleFunc("/proxy/{proxy_id}/{app_path...}", s.proxyGenerated)
7577
s.mux.HandleFunc("/ai/trickle/{channel}", s.trickle)
7678
s.mux.HandleFunc("/ai/trickle/{channel}/{seq}", s.trickle)
7779
return s
7880
}
7981

80-
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.mux.ServeHTTP(w, r) }
82+
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
83+
if id, path := s.registry.matchProxy(r); id != "" {
84+
r.SetPathValue("proxy_id", id)
85+
r.SetPathValue("app_path", path)
86+
s.proxyGenerated(w, r)
87+
return
88+
}
89+
s.mux.ServeHTTP(w, r)
90+
}
8191

8292
func (s *Server) SetPayment(engine *pm.Engine) { s.payment = engine }
8393

@@ -319,13 +329,22 @@ func (s *Server) proxySingleShot(w http.ResponseWriter, r *http.Request) {
319329
}
320330

321331
func (s *Server) proxyGenerated(w http.ResponseWriter, r *http.Request) {
332+
if s.registry.singleShotProxy(r.PathValue("proxy_id")) {
333+
r.SetPathValue("runner_id", r.PathValue("proxy_id"))
334+
s.proxySingleShot(w, r)
335+
return
336+
}
322337
target, runnerID, sid, token, ok := s.registry.proxyTarget(r.PathValue("proxy_id"))
323338
if !ok {
324339
fail(w, http.StatusNotFound, "proxy not found")
325340
return
326341
}
327342
control := s.registry.service + "/runner/" + url.PathEscape(runnerID) + "/session/" + url.PathEscape(sid)
328-
s.proxy(w, r, s.proxyPolicy, target, r.PathValue("app_path"), runnerID, sid, token, control)
343+
policy := s.proxyPolicy
344+
if target.runner {
345+
policy = s.runnerPolicy
346+
}
347+
s.proxy(w, r, policy, target.target, r.PathValue("app_path"), runnerID, sid, token, control)
329348
}
330349

331350
func (s *Server) proxy(w http.ResponseWriter, r *http.Request, policy destination.Policy, target *url.URL, appPath, runnerID, sid, token, control string) {

‎orchestrator/proxy.go‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package orchestrator
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
"net/url"
7+
"strings"
8+
)
9+
10+
const proxyMarker = "livepeer-proxy-id"
11+
12+
func validateProxyTemplate(raw string) error {
13+
if raw == "" {
14+
return nil
15+
}
16+
u, err := url.Parse(strings.ReplaceAll(raw, "{proxy}", proxyMarker))
17+
if err != nil || strings.Count(raw, "{proxy}") != 1 || (u.Scheme != "https" && u.Scheme != "http") || u.Host == "" || u.User != nil || u.RawQuery != "" || u.Fragment != "" {
18+
return errors.New("proxy-url-template requires one {proxy} and an HTTP(S) URL without credentials, query or fragment")
19+
}
20+
host := strings.SplitSeq(u.Hostname(), ".")
21+
for label := range host {
22+
if label == proxyMarker && u.Path == "" {
23+
return nil
24+
}
25+
}
26+
if !strings.Contains(u.Host, proxyMarker) && strings.HasSuffix(u.Path, "/"+proxyMarker) {
27+
return nil
28+
}
29+
return errors.New("{proxy} must be a complete hostname label (with no path) or the final path segment")
30+
}
31+
32+
func (r *Registry) proxyURL(id string) string {
33+
if r.proxyTemplate != "" {
34+
return strings.Replace(r.proxyTemplate, "{proxy}", id, 1)
35+
}
36+
return r.service + "/run/" + id
37+
}
38+
39+
// The template is fixed before serving. Match the actual Host, never a
40+
// forwarded header supplied by a caller, and retain every application path.
41+
func (r *Registry) matchProxy(req *http.Request) (string, string) {
42+
if r.proxyTemplate == "" {
43+
return "", ""
44+
}
45+
u, _ := url.Parse(strings.Replace(r.proxyTemplate, "{proxy}", proxyMarker, 1))
46+
if before, after, ok := strings.Cut(strings.ToLower(u.Host), proxyMarker); ok {
47+
host := strings.ToLower(req.Host)
48+
if strings.HasPrefix(host, before) && strings.HasSuffix(host, after) && len(host) > len(before)+len(after) {
49+
id := host[len(before) : len(host)-len(after)]
50+
if validRouteID(id) {
51+
return id, strings.TrimPrefix(req.URL.Path, "/")
52+
}
53+
}
54+
} else if strings.EqualFold(req.Host, u.Host) {
55+
prefix := strings.TrimSuffix(u.Path, proxyMarker)
56+
if tail, ok := strings.CutPrefix(req.URL.Path, prefix); ok {
57+
id, path, _ := strings.Cut(tail, "/")
58+
if validRouteID(id) {
59+
return id, path
60+
}
61+
}
62+
}
63+
return "", ""
64+
}
65+
66+
func (r *Registry) discoveryURL(id string, item *runner) string {
67+
if item.Proxy && item.Mode == "single-shot" {
68+
return r.proxyURL(id)
69+
}
70+
return r.appURL(id, item.Mode, "")
71+
}
72+
73+
func (r *Registry) singleShotProxy(id string) bool {
74+
r.mu.Lock()
75+
defer r.mu.Unlock()
76+
item := r.runners[id]
77+
return item != nil && item.Proxy && item.Mode == "single-shot"
78+
}
79+
80+
// Single-shot discovery uses the runner ID as the proxy hostname label.
81+
func (r *Registry) validateProxyRunner(req heartbeatRequest) error {
82+
if !req.Proxy || req.Mode != "single-shot" || req.RunnerID == "" || r.proxyTemplate == "" {
83+
return nil
84+
}
85+
u, _ := url.Parse(strings.Replace(r.proxyTemplate, "{proxy}", proxyMarker, 1))
86+
if !strings.Contains(u.Host, proxyMarker) {
87+
return nil
88+
}
89+
id := req.RunnerID
90+
if len(id) > 63 || strings.HasPrefix(id, "-") || strings.HasSuffix(id, "-") || strings.ContainsAny(id, "_ABCDEFGHIJKLMNOPQRSTUVWXYZ") {
91+
return errors.New("single-shot domain proxy requires a lowercase DNS-label runner ID of at most 63 characters")
92+
}
93+
return nil
94+
}

‎orchestrator/proxy_test.go‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package orchestrator
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io"
7+
"log/slog"
8+
"net/http"
9+
"net/http/httptest"
10+
"net/url"
11+
"strings"
12+
"testing"
13+
"time"
14+
15+
"github.com/livepeer/node/destination"
16+
"github.com/stretchr/testify/require"
17+
)
18+
19+
func TestRunnerProxyRoutingMatrix(t *testing.T) {
20+
for _, static := range []bool{false, true} {
21+
for _, mode := range []string{"persistent", "single-shot"} {
22+
for _, proxy := range []bool{false, true} {
23+
for _, template := range []string{"", "https://{proxy}.apps.example", "https://orch.example/custom/{proxy}"} {
24+
t.Run(fmt.Sprintf("static=%t/%s/proxy=%t/%s", static, mode, proxy, template), func(t *testing.T) {
25+
upstream := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
26+
require.Equal(t, "/base/test/path", r.URL.Path)
27+
require.Equal(t, "x=1", r.URL.RawQuery)
28+
require.NotEmpty(t, r.Header.Get("Livepeer-Session-Id"))
29+
require.NotEqual(t, "forged", r.Header.Get("Livepeer-Session-Token"))
30+
_, _ = io.WriteString(w, "ok")
31+
}))
32+
defer upstream.Close()
33+
policy, err := destination.New("runner", []string{strings.TrimPrefix(upstream.URL, "http://")})
34+
require.NoError(t, err)
35+
denied, err := destination.New("session-proxy", nil)
36+
require.NoError(t, err)
37+
registry := NewRegistry("secret", "https://orch.example/deployment", time.Second, time.Minute)
38+
registry.proxyTemplate = template
39+
s := NewServer(registry, policy, denied, slog.New(slog.NewTextHandler(io.Discard, nil)))
40+
defer s.Close()
41+
if static {
42+
require.NoError(t, registry.AddStatic(StaticRunner{ID: "runner", RunnerURL: upstream.URL + "/base", App: "app", Mode: mode, Proxy: proxy}))
43+
} else {
44+
_, _, err := registry.Heartbeat(heartbeatRequest{RunnerID: "runner", RunnerURL: upstream.URL + "/base", App: "app", Mode: mode, Proxy: proxy}, "secret")
45+
require.NoError(t, err)
46+
}
47+
discovery := httptest.NewRecorder()
48+
s.ServeHTTP(discovery, httptest.NewRequest("GET", "https://orch.example/deployment/discovery", nil))
49+
require.Equal(t, 200, discovery.Code)
50+
var entries []discoveryEntry
51+
require.NoError(t, json.Unmarshal(discovery.Body.Bytes(), &entries))
52+
app := entries[0].Runners[0].URL
53+
var sid string
54+
if mode == "persistent" {
55+
var err error
56+
sid, app, _, _, err = registry.Reserve("runner")
57+
require.NoError(t, err)
58+
}
59+
if proxy {
60+
if template == "" {
61+
require.Contains(t, app, "/run/")
62+
} else if strings.Contains(template, ".apps.example") {
63+
u, err := url.Parse(app)
64+
require.NoError(t, err)
65+
require.True(t, strings.HasSuffix(u.Host, ".apps.example"))
66+
} else {
67+
require.Contains(t, app, "/custom/")
68+
}
69+
}
70+
req := httptest.NewRequest("POST", app+"/test/path?x=1", strings.NewReader("request"))
71+
req.Header.Set("Livepeer-Session-Token", "forged")
72+
w := httptest.NewRecorder()
73+
s.ServeHTTP(w, req)
74+
require.Equal(t, 200, w.Code, w.Body.String())
75+
require.Equal(t, "ok", w.Body.String())
76+
registry.ReleaseBySession(sid)
77+
_, sessions := registry.Counts()
78+
require.Zero(t, sessions)
79+
})
80+
}
81+
}
82+
}
83+
}
84+
}
85+
86+
func TestDomainProxyRunnerIDs(t *testing.T) {
87+
for _, id := range []string{"Uppercase", "under_score", strings.Repeat("a", 64), "-leading", "trailing-"} {
88+
reg := NewRegistry("bootstrap", "https://orch.example", time.Second, time.Minute)
89+
reg.proxyTemplate = "https://{proxy}.example"
90+
req := heartbeatRequest{RunnerID: id, RunnerURL: "https://runner.example", App: "test", Mode: "single-shot", Proxy: true}
91+
_, code, err := reg.Heartbeat(req, "bootstrap")
92+
require.Error(t, err)
93+
require.Equal(t, http.StatusBadRequest, code)
94+
require.Error(t, reg.AddStatic(StaticRunner{ID: id, RunnerURL: req.RunnerURL, App: req.App, Mode: req.Mode, Proxy: true}))
95+
}
96+
}

‎orchestrator/registry.go‎

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,21 @@ type session struct {
104104
cancel context.CancelFunc
105105
ID string
106106
Token string
107-
Proxies map[string]*url.URL
107+
Proxies map[string]sessionProxy
108108
Created time.Time
109109
}
110110

111111
type Registry struct {
112-
closed bool
113-
mu sync.Mutex
114-
runners map[string]*runner
115-
secret string
116-
service string
117-
interval time.Duration
118-
ttl time.Duration
119-
onEvent func(runnerID, event, sessionID string)
120-
weiPerUSD *big.Rat
112+
proxyTemplate string
113+
closed bool
114+
mu sync.Mutex
115+
runners map[string]*runner
116+
secret string
117+
service string
118+
interval time.Duration
119+
ttl time.Duration
120+
onEvent func(runnerID, event, sessionID string)
121+
weiPerUSD *big.Rat
121122
}
122123

123124
const maxRunners = 256
@@ -316,7 +317,7 @@ func (r *Registry) Discovery() []discoveryEntry {
316317
if available <= 0 {
317318
continue
318319
}
319-
result = append(result, discoveryRunner{URL: r.appURL(id, item.Mode, ""), App: item.App, Version: item.Version, Metadata: item.Metadata, GPU: item.GPU, Mode: item.Mode, Capacity: item.Capacity, CapacityUsed: len(item.Sessions), CapacityAvailable: available, PriceInfo: item.PriceInfo})
320+
result = append(result, discoveryRunner{URL: r.discoveryURL(id, item), App: item.App, Version: item.Version, Metadata: item.Metadata, GPU: item.GPU, Mode: item.Mode, Capacity: item.Capacity, CapacityUsed: len(item.Sessions), CapacityAvailable: available, PriceInfo: item.PriceInfo})
320321
}
321322
return []discoveryEntry{{Address: r.service, Runners: result}}
322323
}
@@ -368,11 +369,23 @@ func (r *Registry) ReserveWithID(id, requestedID string) (string, string, string
368369
return "", "", "", http.StatusInternalServerError, err
369370
}
370371
ctx, cancel := context.WithCancel(context.Background())
371-
item.Sessions[sessionID] = &session{ID: sessionID, Token: token, Created: time.Now(), Proxies: map[string]*url.URL{}, ctx: ctx, cancel: cancel, PriceInfo: item.PriceInfo}
372+
item.Sessions[sessionID] = &session{ID: sessionID, Token: token, Created: time.Now(), Proxies: map[string]sessionProxy{}, ctx: ctx, cancel: cancel, PriceInfo: item.PriceInfo}
373+
appURL := r.appURL(id, item.Mode, sessionID)
374+
if item.Proxy && item.Mode == "persistent" {
375+
target, _ := url.Parse(item.RunnerURL)
376+
proxyID, err := randomID()
377+
if err != nil {
378+
cancel()
379+
delete(item.Sessions, sessionID)
380+
return "", "", "", http.StatusInternalServerError, err
381+
}
382+
item.Sessions[sessionID].Proxies[proxyID] = sessionProxy{target: target, runner: true}
383+
appURL = r.proxyURL(proxyID)
384+
}
372385
if r.onEvent != nil {
373386
r.onEvent(id, "reserved", sessionID)
374387
}
375-
return sessionID, r.appURL(id, item.Mode, sessionID), r.service + "/apps/" + url.PathEscape(id) + "/session/" + url.PathEscape(sessionID), http.StatusOK, nil
388+
return sessionID, appURL, r.service + "/apps/" + url.PathEscape(id) + "/session/" + url.PathEscape(sessionID), http.StatusOK, nil
376389
}
377390

378391
func (r *Registry) PriceForRunner(id string) (priceInfo, int, error) {
@@ -476,24 +489,27 @@ func (r *Registry) addProxy(id, sid, token string, target *url.URL) (string, err
476489
if err != nil {
477490
return "", err
478491
}
479-
item.Sessions[sid].Proxies[proxyID] = target
492+
if len(item.Sessions[sid].Proxies) >= 25 {
493+
return "", errors.New("too many session proxies")
494+
}
495+
item.Sessions[sid].Proxies[proxyID] = sessionProxy{target: target}
480496
return proxyID, nil
481497
}
482498

483-
func (r *Registry) proxyTarget(proxyID string) (*url.URL, string, string, string, bool) {
499+
func (r *Registry) proxyTarget(proxyID string) (sessionProxy, string, string, string, bool) {
484500
r.mu.Lock()
485501
defer r.mu.Unlock()
486502
for runnerID, item := range r.runners {
487503
if !r.usable(item) {
488504
continue
489505
}
490506
for sid, sess := range item.Sessions {
491-
if target := sess.Proxies[proxyID]; target != nil {
507+
if target, ok := sess.Proxies[proxyID]; ok {
492508
return target, runnerID, sid, sess.Token, true
493509
}
494510
}
495511
}
496-
return nil, "", "", "", false
512+
return sessionProxy{}, "", "", "", false
497513
}
498514

499515
func (r *Registry) Expire() {
@@ -654,3 +670,8 @@ func (r *Registry) Close() {
654670
}
655671
}
656672
}
673+
674+
type sessionProxy struct {
675+
target *url.URL
676+
runner bool
677+
}

0 commit comments

Comments
 (0)