From c29657c5620523f7c41085b8ab9309805ed0153a Mon Sep 17 00:00:00 2001 From: Tom Elliott Date: Tue, 17 Nov 2026 06:52:09 -0600 Subject: [PATCH] Use https:// when the agent is given an http:// server address The server knows an agent only by its TLS client certificate, so no call after enrollment works over plain HTTP. An agent installed with an http:// address enrolled, got a certificate it could never present, and failed every poll (forum topic 18320). A FOG install serves HTTPS whether or not it redirects HTTP to it. - enroll.HTTPSAddress moves http:// with no port or port 80 to https:// on the same host. Another port is kept and still fails, with the reason in the log. - Applied to --server (setup, enroll, run), to a config.json that holds http:// (enroll.Load), and to the `ca probe` wizard address. - The move is logged once per process, with userinfo redacted, and saved to config.json. The enrollment, key and host id are kept. - A machine whose polls are already refused cannot self-update to this release; it needs the MSI run again by hand. - protocol-v1.md, 0002-trust-model.md and install-windows.md say so. Co-Authored-By: Claude Opus 5.5 --- cmd/fog-agent/main.go | 61 ++++++++++++- cmd/fog-agent/serverurl_test.go | 144 ++++++++++++++++++++++++++++++ docs/design/0002-trust-model.md | 5 +- docs/design/protocol-v1.md | 15 +++- docs/install-windows.md | 6 ++ internal/enroll/caprobe.go | 4 + internal/enroll/serverurl.go | 37 ++++++++ internal/enroll/serverurl_test.go | 27 ++++++ internal/enroll/state.go | 10 +++ 9 files changed, 301 insertions(+), 8 deletions(-) create mode 100644 cmd/fog-agent/serverurl_test.go create mode 100644 internal/enroll/serverurl.go create mode 100644 internal/enroll/serverurl_test.go diff --git a/cmd/fog-agent/main.go b/cmd/fog-agent/main.go index 5878315..32a0614 100644 --- a/cmd/fog-agent/main.go +++ b/cmd/fog-agent/main.go @@ -13,6 +13,7 @@ import ( "flag" "fmt" "io" + "net/url" "os" "os/signal" "path/filepath" @@ -165,9 +166,21 @@ func addCommonFlags(fs *flag.FlagSet) commonFlags { } // repairConfig logs and finishes a recovery when Load had to start from -// config.json.prev. The service start, setup, renew and update paths call it +// config.json.prev. It also logs and saves an http:// server address that +// Load moved to https://, so this call can write config.json. The service start, setup, renew and update paths call it // right after Load, before anything saves config.json or copies it to .prev. func repairConfig(st *enroll.State, out *sayer) error { + if st.ServerURLMoved != "" { + // Load moved an http:// address to https://. Say so once and save + // it, so the next start does not move it again. + sayMoved(st.ServerURLMoved, st.Config.ServerURL, out) + st.ServerURLMoved = "" + if st.ConfigRecovered == "" { + if err := st.SaveConfig(); err != nil { + return fmt.Errorf("could not save the https:// server address to config.json in %s; check that the state directory is writable: %w", st.Dir, err) + } + } + } if st.ConfigRecovered == "" { return nil } @@ -175,6 +188,47 @@ func repairConfig(st *enroll.State, out *sayer) error { return st.RepairConfig() } +// movedServerLine is the one line for an http:// address the agent moved. +// Both addresses are redacted: one with user:password@ in it must not put +// the password in the log. +func movedServerLine(from, to string) string { + from, to = redactedURL(from), redactedURL(to) + return "the server address " + from + " is not https://; using " + to + + ". The agent's certificate travels only over HTTPS. If this fails, check that the FOG server answers on " + to +} + +func redactedURL(s string) string { + u, err := url.Parse(s) + if err != nil { + return s + } + return u.Redacted() +} + +// movedSaid is the last move line logged by this process. Setup can meet the +// same move twice: config.json and --server both http://, or the restore +// path opening the state a second time. Each sayer is fresh, so the sayer's +// own repeat check cannot catch it. +var movedSaid string + +func sayMoved(from, to string, out *sayer) { + line := movedServerLine(from, to) + if line != movedSaid { + movedSaid = line + out.say(line) + } +} + +// serverFlag is --server, moved to https:// when it is http:// (see +// enroll.HTTPSAddress), with the move logged. +func serverFlag(raw string, out *sayer) string { + moved, ok := enroll.HTTPSAddress(raw) + if ok { + sayMoved(raw, moved, out) + } + return moved +} + // openState loads the state directory and settles the server URL and what // to trust for it, from flags or from what an earlier enroll remembered. func openState(f commonFlags) (*enroll.State, error) { @@ -186,7 +240,7 @@ func openState(f commonFlags) (*enroll.State, error) { return nil, err } if *f.server != "" { - st.Config.ServerURL = *f.server + st.Config.ServerURL = serverFlag(*f.server, &sayer{}) } if st.Config.ServerURL == "" { return nil, errors.New("--server is required the first time") @@ -1881,7 +1935,8 @@ func forgetStaleTrust(f commonFlags, out *sayer) (snap trustSnapshot, forgot boo } snap = trustSnapshot{system: st.Config.SystemTrust, ca: st.CA(), server: st.Config.ServerURL} if *f.server != "" { - st.Config.ServerURL = *f.server + // Not logged here: openState moves the same flag and says so. + st.Config.ServerURL, _ = enroll.HTTPSAddress(*f.server) } if !st.HasTrust() || st.Config.ServerURL == "" { return snap, false, nil diff --git a/cmd/fog-agent/serverurl_test.go b/cmd/fog-agent/serverurl_test.go new file mode 100644 index 0000000..c3dbac8 --- /dev/null +++ b/cmd/fog-agent/serverurl_test.go @@ -0,0 +1,144 @@ +package main + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/FOGProject/fog-agent/internal/enroll" +) + +// setHTTPServer rewrites config.json's server_url to the http:// form of the +// enrolled server, as an install made before the move left it. +func setHTTPServer(t *testing.T, dir string) { + t.Helper() + path := filepath.Join(dir, "config.json") + b, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + var raw map[string]any + if err := json.Unmarshal(b, &raw); err != nil { + t.Fatal(err) + } + raw["server_url"] = "http://fog.example.org/fog" + if b, err = json.Marshal(raw); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(path, b, 0o600); err != nil { + t.Fatal(err) + } +} + +func captureLog(t *testing.T) *bytes.Buffer { + t.Helper() + var log bytes.Buffer + saved := logOut + logOut = &log + // movedSaid lives for the process; each test is a fresh start. + movedSaid = "" + t.Cleanup(func() { logOut = saved; movedSaid = "" }) + return &log +} + +// Forum topic 18320: an agent installed with an http:// address enrolled and +// then failed every poll. An install already in that state moves to +// https:// on its next start, keeps its enrollment, says so, and saves it. +func TestAnHTTPAddressInConfigMovesToHTTPSAndKeepsTheEnrollment(t *testing.T) { + dir := enrolledState(t, false) + setHTTPServer(t, dir) + stubCheckTrust(t, nil) + stubProbe(t, trustedProbe) + log := captureLog(t) + + st, err := settleTrust(flagsFor(t, "--dir", dir), &sayer{}) + if err != nil { + t.Fatalf("setup with an http:// address in config.json: %v", err) + } + if st.Config.ServerURL != "https://fog.example.org/fog" { + t.Errorf("server = %q; want https://fog.example.org/fog", st.Config.ServerURL) + } + if st.Config.HostID != 42 { + t.Errorf("host id = %d; moving the address must keep the enrollment", st.Config.HostID) + } + if !strings.Contains(log.String(), "http://fog.example.org/fog is not https://") { + t.Errorf("the move was not logged; log:\n%s", log.String()) + } + reloaded, err := enroll.Load(dir) + if err != nil { + t.Fatal(err) + } + if reloaded.ServerURLMoved != "" || reloaded.Config.ServerURL != "https://fog.example.org/fog" { + t.Errorf("config.json still holds %q after setup; the move was not saved", reloaded.ServerURLMoved) + } +} + +// The service start (run) on an install that saved http://, which is what +// the forum reporter's cloned machines do: moved, enrollment kept, saved. +func TestServiceStartMovesAnHTTPAddressInConfig(t *testing.T) { + dir := enrolledState(t, false) + setHTTPServer(t, dir) + log := captureLog(t) + + st, err := openState(flagsFor(t, "--dir", dir)) + if err != nil { + t.Fatalf("openState with an http:// address in config.json: %v", err) + } + if st.Config.ServerURL != "https://fog.example.org/fog" || st.Config.HostID != 42 { + t.Errorf("server = %q, host id = %d; want https://fog.example.org/fog and 42", st.Config.ServerURL, st.Config.HostID) + } + if n := strings.Count(log.String(), "is not https://"); n != 1 { + t.Errorf("the move was logged %d times; want once. log:\n%s", n, log.String()) + } + if again, err := openState(flagsFor(t, "--dir", dir)); err != nil || again.ServerURLMoved != "" { + t.Errorf("the second start moved the address again (err %v); it was not saved", err) + } +} + +// config.json and --server both http://, as a cloned image set up again +// gives: one move, one line. +func TestSetupLogsTheMoveOnceWhenConfigAndFlagAreBothHTTP(t *testing.T) { + dir := enrolledState(t, false) + setHTTPServer(t, dir) + stubCheckTrust(t, nil) + stubProbe(t, trustedProbe) + log := captureLog(t) + + if _, err := settleTrust(flagsFor(t, "--dir", dir, "--server", "http://fog.example.org/fog"), &sayer{}); err != nil { + t.Fatalf("setup: %v", err) + } + if n := strings.Count(log.String(), "is not https://"); n != 1 { + t.Errorf("the move was logged %d times; want once. log:\n%s", n, log.String()) + } +} + +// An address with user:password@ in it must not put the password in the log. +func TestTheMovedLineRedactsAPassword(t *testing.T) { + line := movedServerLine("http://u:s3cret@fog.lab/fog", "https://u:s3cret@fog.lab/fog") + if strings.Contains(line, "s3cret") { + t.Fatalf("the log line carries the password: %q", line) + } +} + +// The MSI and the installer wizard pass --server. An http:// value is moved +// before anything is enrolled against it, and the move is logged. +func TestAnHTTPServerFlagMovesToHTTPS(t *testing.T) { + dir := enrolledState(t, false) + stubCheckTrust(t, nil) + stubProbe(t, trustedProbe) + log := captureLog(t) + + st, err := settleTrust(flagsFor(t, "--dir", dir, "--server", "http://fog.example.org/fog"), &sayer{}) + if err != nil { + t.Fatalf("setup with --server http://: %v", err) + } + if st.Config.ServerURL != "https://fog.example.org/fog" { + t.Errorf("server = %q; want https://fog.example.org/fog", st.Config.ServerURL) + } + if !strings.Contains(log.String(), "is not https://; using https://fog.example.org/fog") { + t.Errorf("the move was not logged; log:\n%s", log.String()) + } +} diff --git a/docs/design/0002-trust-model.md b/docs/design/0002-trust-model.md index 426f412..dc3574d 100644 --- a/docs/design/0002-trust-model.md +++ b/docs/design/0002-trust-model.md @@ -68,8 +68,9 @@ the first-install rules: a `--ca` file, a fingerprint, or system trust. If that fails, setup restores the old trust. With `--ca` or a fingerprint, setup then fails with the error. The stored server URL stays as it was. Without them, setup continues with the old trust, as before this check. It -then saves the `--server` value, also as before. An MSI upgrade passes -neither, so it does not start failing. Setup drops the issued certificate +then saves the `--server` value, also as before. Setup saves an `http://` +value with no port or port 80 as its `https://` form (protocol-v1.md). An +MSI upgrade passes neither, so it does not start failing. Setup drops the issued certificate only when it settles a different trust. The accepted cost is an attacker on the path at setup time. A bad diff --git a/docs/design/protocol-v1.md b/docs/design/protocol-v1.md index b2f2222..337d207 100644 --- a/docs/design/protocol-v1.md +++ b/docs/design/protocol-v1.md @@ -4,6 +4,14 @@ Status: DRAFT 2026-09-03. The agent owns this contract (design doc 5.1); the server implements it under `/agent/v1`. JSON both ways, UTF-8, `Content-Type: application/json`. +Every route below is HTTPS. The server identifies an agent only by its TLS +client certificate. So no call after enrollment can succeed over plain HTTP. +The agent changes an `http://` server address to `https://` on the same +host. It does this only when the address has no port or port 80. The +address can come from `--server` or from an older `config.json`. The agent +logs each change. It keeps an `http://` address on another port, and the +log then gives the reason for each failure. + ## POST /agent/v1/enroll Server-authenticated TLS only (the agent has no certificate yet). The agent @@ -907,9 +915,10 @@ The FOG AD Join/Rejoin enhancement requires the server to support both `directory-task-operations-v1`. `GET /agent/v1/enroll` advertises these tokens in `X-FOG-Agent-Features`, separated by commas or whitespace. Additional tokens are allowed; substrings are not capabilities. Trusted HTTPS and the existing -certificate principal remain required: with a server address that is not -`https://`, the agent sends no check, logs the address as the cause, and -leaves the feature off until it restarts. Unreachable/temporarily +certificate principal remain required. A server address can still be +`http://` on a port other than 80. For that address, the agent sends no +check. It logs the address as the cause. It leaves the feature off until it +restarts. Unreachable/temporarily unavailable and reachable-but-unsupported servers are distinct errors. An unavailable server is asked again on later polls; an unsupported one leaves the feature off until the agent restarts. While the feature is off, the diff --git a/docs/install-windows.md b/docs/install-windows.md index b35536a..9a902c0 100644 --- a/docs/install-windows.md +++ b/docs/install-windows.md @@ -20,6 +20,12 @@ same thing you type to reach the management pages. If you reach the UI at `https://fog.example.org/fog/management/`, the address to give the agent is `https://fog.example.org/fog`. +The agent talks to the server only over HTTPS. The server knows each +machine by the agent's certificate, and that certificate travels only over +HTTPS. You can give an `http://` address with no port or with port 80. The +agent then uses the `https://` address on the same host, and its log says +so. A FOG server serves HTTPS even when it does not redirect HTTP. + ### What the agent trusts The agent does not trust whatever a browser would accept. The wizard's diff --git a/internal/enroll/caprobe.go b/internal/enroll/caprobe.go index a669c79..a279cf4 100644 --- a/internal/enroll/caprobe.go +++ b/internal/enroll/caprobe.go @@ -68,6 +68,10 @@ type CAProbe struct { // The order matters. A FOG CA in the machine's store -- the legacy client // put it there -- must still go through the fingerprint, not skip it. func ProbeCA(ctx context.Context, serverURL string) (*CAProbe, error) { + // The wizard probes the address a person typed, before setup moves it. + // Probe the https:// address the agent will use, so the CA is checked + // against the server's own certificate and the address shown is that one. + serverURL, _ = HTTPSAddress(serverURL) base := strings.TrimRight(serverURL, "/") u, err := url.Parse(base) if err != nil || u.Host == "" { diff --git a/internal/enroll/serverurl.go b/internal/enroll/serverurl.go new file mode 100644 index 0000000..6e2efaf --- /dev/null +++ b/internal/enroll/serverurl.go @@ -0,0 +1,37 @@ +package enroll + +import ( + "net/url" + "strings" +) + +// HTTPSAddress turns an http:// server address into the https:// address on +// the same host, and reports whether it did. +// +// The server knows an agent only by its TLS client certificate, so nothing +// after enrollment can work over plain HTTP: the server answers every call +// with no_client_certificate. Before this, an http:// address enrolled, got +// a certificate it could never present, and failed every poll (forum topic +// 18320). A FOG install serves HTTPS whether or not it redirects HTTP to it +// (installfog.sh --no-force-https: "serve both HTTP and HTTPS"), so the same +// host on https:// is the address that works. +// +// Only port 80 or no port is moved. Another explicit port is left alone: it +// says where a person put the server, and port 443 is a guess this cannot +// make for them. Such an address still fails, and the log says why. +func HTTPSAddress(raw string) (string, bool) { + u, err := url.Parse(strings.TrimSpace(raw)) + if err != nil || !strings.EqualFold(u.Scheme, "http") || u.Hostname() == "" { + return raw, false + } + if p := u.Port(); p != "" && p != "80" { + return raw, false + } + u.Scheme = "https" + u.Host = u.Hostname() + if strings.Contains(u.Host, ":") { + // An IPv6 literal needs its brackets back once the port is gone. + u.Host = "[" + u.Host + "]" + } + return u.String(), true +} diff --git a/internal/enroll/serverurl_test.go b/internal/enroll/serverurl_test.go new file mode 100644 index 0000000..8f7a3b2 --- /dev/null +++ b/internal/enroll/serverurl_test.go @@ -0,0 +1,27 @@ +package enroll + +import "testing" + +func TestHTTPSAddressMovesOnlyPlainHTTPOnTheDefaultPort(t *testing.T) { + for _, tc := range []struct { + in, want string + moved bool + }{ + {"http://fog.lab/fog", "https://fog.lab/fog", true}, + {"HTTP://fog.lab/fog/", "https://fog.lab/fog/", true}, + {"http://fog.lab:80/fog", "https://fog.lab/fog", true}, + {"http://10.0.0.5/fog", "https://10.0.0.5/fog", true}, + {"http://[fd00::5]/fog", "https://[fd00::5]/fog", true}, + {"http://[fd00::5]:80/fog", "https://[fd00::5]/fog", true}, + {"https://fog.lab/fog", "https://fog.lab/fog", false}, + {"http://fog.lab:8080/fog", "http://fog.lab:8080/fog", false}, + {"ftp://fog.lab/fog", "ftp://fog.lab/fog", false}, + {"fog.lab/fog", "fog.lab/fog", false}, + {"", "", false}, + } { + got, moved := HTTPSAddress(tc.in) + if got != tc.want || moved != tc.moved { + t.Errorf("HTTPSAddress(%q) = %q, %t; want %q, %t", tc.in, got, moved, tc.want, tc.moved) + } + } +} diff --git a/internal/enroll/state.go b/internal/enroll/state.go index 49398e0..bffd464 100644 --- a/internal/enroll/state.go +++ b/internal/enroll/state.go @@ -182,6 +182,9 @@ type State struct { // started from config.json.prev instead. It is the line to log. // RepairConfig writes the recovered config back. ConfigRecovered string + // ServerURLMoved is the http:// address config.json held when Load moved + // it to https:// (see HTTPSAddress), or "". The caller logs it and saves. + ServerURLMoved string } // DirectoryCheck is read-only policy metadata, never a join credential. @@ -231,6 +234,13 @@ func Load(dir string) (*State, error) { configFile, dir, err, configFile, configFile) } } + // An install that enrolled against http:// before HTTPSAddress existed + // fails every poll. Moving it here reaches every command, the service + // included, without each one repeating the check. + if moved, ok := HTTPSAddress(st.Config.ServerURL); ok { + st.ServerURLMoved = st.Config.ServerURL + st.Config.ServerURL = moved + } b, err := os.ReadFile(filepath.Join(dir, keyFile)) if errors.Is(err, os.ErrNotExist) { return st, nil