Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 58 additions & 3 deletions cmd/fog-agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"flag"
"fmt"
"io"
"net/url"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -165,16 +166,69 @@ 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
}
out.say(st.ConfigRecovered)
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) {
Expand All @@ -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")
Expand Down Expand Up @@ -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
Expand Down
144 changes: 144 additions & 0 deletions cmd/fog-agent/serverurl_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}
5 changes: 3 additions & 2 deletions docs/design/0002-trust-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 12 additions & 3 deletions docs/design/protocol-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions docs/install-windows.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions internal/enroll/caprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down
37 changes: 37 additions & 0 deletions internal/enroll/serverurl.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 additions & 0 deletions internal/enroll/serverurl_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
10 changes: 10 additions & 0 deletions internal/enroll/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
Loading