Refuse to open the agent port without TLS unless agent_insecure says so

Without agent_tls the control plane served plaintext h2c on every
interface, and any machine reaching it could register as any node. The
server now refuses to start that way unless agent_insecure: true, and then
binds loopback when agent_listen names only a port.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Ilker ManapandClaude Opus 5.5 committed 2026-10-04 09:51:37 +02:00
1 parent 0bd5305e9a
commit c522e8b2aa
7 files changed
+97 -4

No files matched your search

+34 -2
View File
@@ -427,10 +427,20 @@ func newServerCmd() *cobra.Command {
// network only.
agentSrv.Handler = h2c.NewHandler(mux, &http2.Server{})
}
if agentLn, err = net.Listen("tcp", cfg.AgentListen); err != nil {
agentAddr, aerr := agentListenAddr(cfg)
if aerr != nil {
_ = apiLn.Close()
_ = adminLn.Close()
return fmt.Errorf("listening on %s: %w", cfg.AgentListen, err)
return aerr
}
if !cfg.AgentTLS.Enabled() {
log.Warn("the agent port has no TLS (agent_insecure): agents are not authenticated and "+
"traffic is plaintext; use only on one machine or a trusted network", "listen", agentAddr)
}
if agentLn, err = net.Listen("tcp", agentAddr); err != nil {
_ = apiLn.Close()
_ = adminLn.Close()
return fmt.Errorf("listening on %s: %w", agentAddr, err)
}
if cfg.AgentTLS.Enabled() {
tc, terr := cfg.AgentTLS.ServerConfig()
@@ -761,3 +771,25 @@ func servableCheck(llamaVersion string) func(context.Context, catalog.Model) err
return nil
}
}
// errAgentInsecure is a control plane asked to open its agent port with no
// TLS and not told that is meant.
var errAgentInsecure = errors.New("agent_tls is not set, so the agent port would be plaintext and anyone " +
"reaching it could register as a node: set agent_tls (llmgw server init writes it), or " +
"agent_insecure: true for one machine or a test")
// agentListenAddr is where the agent port listens: agent_listen with TLS;
// without it, refused unless agent_insecure says so, and then on loopback
// when agent_listen names only a port.
func agentListenAddr(cfg config.Server) (string, error) {
if cfg.AgentTLS.Enabled() {
return cfg.AgentListen, nil
}
if !cfg.AgentInsecure {
return "", errAgentInsecure
}
if host, port, err := net.SplitHostPort(cfg.AgentListen); err == nil && host == "" {
return net.JoinHostPort("127.0.0.1", port), nil
}
return cfg.AgentListen, nil
}
+29
View File
@@ -0,0 +1,29 @@
package cli
import (
"testing"
"gitea.mynodes.xyz/ilker/llmgw/internal/config"
"gitea.mynodes.xyz/ilker/llmgw/internal/tlsconf"
)
func TestTheAgentPortOpensWithoutTLSOnlyWhenToldAndThenOnLoopback(t *testing.T) {
cfg := config.Default()
if _, err := agentListenAddr(cfg); err == nil {
t.Error("a control plane with no agent_tls opened its agent port")
}
cfg.AgentInsecure = true
if a, _ := agentListenAddr(cfg); a != "127.0.0.1:7443" {
t.Errorf("insecure, port only: %s", a)
}
cfg.AgentListen = "10.0.0.5:7443"
if a, _ := agentListenAddr(cfg); a != "10.0.0.5:7443" {
t.Errorf("insecure, an address named: %s", a)
}
cfg.AgentInsecure = false
cfg.AgentListen = ":7443"
cfg.AgentTLS = tlsconf.Config{CA: "ca.pem", Cert: "s.pem", Key: "s-key.pem"}
if a, err := agentListenAddr(cfg); err != nil || a != ":7443" {
t.Errorf("with TLS: %s %v", a, err)
}
}