The firewall setting reaches the mirror machine and the store's members

The leading control plane tells the mirror over its API (PUT /v1/firewall,
kept as .firewall.json beside the models and applied by the same path unit
control planes use), and keeps the state in the store's cluster_state for
members that run no llmgw, which a root timer reads from the member's own
copy. The mirror also lets in the network of every node and control plane
the leader knows. Tried on the PoC: off, on and a network added, on every
kind of machine.

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-09 08:53:17 +02:00
1 parent 5be3da505b
commit 8df78a4d41
20 files changed
+772 -34

No files matched your search

+1
View File
@@ -40,6 +40,7 @@ func (a *API) Handler() http.Handler {
mux.HandleFunc("DELETE /v1/models/{model}/{version}", a.remove)
mux.HandleFunc("PUT /v1/models/{model}/{version}/{file}", a.upload)
mux.HandleFunc("GET /v1/health", a.health)
mux.HandleFunc("PUT /v1/firewall", a.putFirewall)
return mux
}
+65
View File
@@ -0,0 +1,65 @@
package mirror
import (
"bytes"
"context"
"encoding/json"
"net/http"
"os"
"path/filepath"
)
// The mirror's machine keeps llmgw's firewall as the cluster's settings
// say, as every other machine does. The mirror runs unprivileged and cannot
// change the ruleset: the control plane tells it what the settings say, it
// writes that beside its models, and a root path unit on the machine
// applies it with `llmgw firewall sync`.
// FirewallState is what the cluster's settings say of a machine's firewall.
type FirewallState struct {
Enabled bool `json:"enabled"`
// Networks are let in besides those the machine reaches others on.
Networks []string `json:"networks,omitempty"`
// Toward are the cluster's machines, by address: each one's network is
// let in, as the machine's route to it says.
Toward []string `json:"toward,omitempty"`
}
// FirewallFile is where, in the mirror's directory, the state is written.
// Hidden, so it is never taken for a model.
const FirewallFile = ".firewall.json"
func (a *API) putFirewall(w http.ResponseWriter, r *http.Request) {
var st FirewallState
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 1<<20)).Decode(&st); err != nil {
fail(w, http.StatusBadRequest, err)
return
}
b, _ := json.Marshal(st)
p := filepath.Join(a.m.root, FirewallFile)
changed := false
if old, err := os.ReadFile(p); err != nil || !bytes.Equal(old, b) {
// Renamed into place: the path unit sees one change, of a whole file.
tmp := p + ".new"
if err := os.WriteFile(tmp, b, 0o644); err != nil {
fail(w, http.StatusInternalServerError, err)
return
}
if err := os.Rename(tmp, p); err != nil {
fail(w, http.StatusInternalServerError, err)
return
}
changed = true
}
writeJSON(w, http.StatusOK, map[string]bool{"changed": changed})
}
// SetFirewall tells the mirror what the cluster's settings say of its
// machine's firewall; it answers whether that changed anything.
func (c *Client) SetFirewall(ctx context.Context, st FirewallState) (bool, error) {
var out struct {
Changed bool `json:"changed"`
}
err := c.do(ctx, http.MethodPut, "/v1/firewall", st, &out)
return out.Changed, err
}
+38
View File
@@ -0,0 +1,38 @@
package mirror
import (
"context"
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"testing"
"gitea.mynodes.xyz/ilker/llmgw/internal/tlsconf"
)
func TestTheMirrorKeepsWhatTheSettingsSayOfItsFirewall(t *testing.T) {
root := t.TempDir()
srv := httptest.NewServer(NewAPI(NewManager(root, discardLog()), "http://m:8081").Handler())
defer srv.Close()
c, err := NewClient(srv.URL, tlsconf.Config{}, 0)
if err != nil {
t.Fatal(err)
}
ctx := context.Background()
st := FirewallState{Enabled: true, Networks: []string{"192.168.5.0/24"}, Toward: []string{"10.0.0.147"}}
if changed, err := c.SetFirewall(ctx, st); err != nil || !changed {
t.Fatalf("first: %v %v", changed, err)
}
if changed, err := c.SetFirewall(ctx, st); err != nil || changed {
t.Errorf("the same again: %v %v", changed, err)
}
var back FirewallState
b, _ := os.ReadFile(filepath.Join(root, FirewallFile))
if err := json.Unmarshal(b, &back); err != nil || !back.Enabled || back.Toward[0] != "10.0.0.147" {
t.Errorf("kept %s", b)
}
if all, err := c.List(ctx); err != nil || len(all.Models) != 0 {
t.Errorf("the file is taken for a model: %+v %v", all, err)
}
}