Read the config with Viper and environment variables, on Go 1.27.4
Security / Vulnerability check (push) Successful in 41s
Test / Lint and test (push) Successful in 1m54s

The config loader uses Viper instead of BurntSushi/toml. Every setting
can also come from UBERBRINGER_<SECTION>_<KEY>, which wins over the
file, and UBERBRINGER_CONFIG sets the default of --config; set empty, no
file is read, so a container can run from the environment alone. Each
setting is bound to its own variable, so a variable named after a
section changes nothing.

The file is parsed with go-toml and checked as written: unknown keys,
empty tables and keys that differ only in case are errors, a UTF-8 BOM
is skipped and a UTF-16 file is refused. Values are decoded strictly:
durations are strings, numbers decimal, flags true or false, and a
fraction for a whole number is refused. Errors name the file, the line
of a syntax error and the variables that were set, never their values.

`uberbringer config get <key>` prints one setting as the daemon would
use it. install.sh reads body_limit, bind_ip and port through it, so the
config is checked before the service restarts and Caddy proxies to the
configured address. The script then waits for the daemon to answer,
downloads Caddy's key and source list before replacing them, requires
Caddy's own repository, keeps the Caddyfile during dpkg upgrades,
detects an existing import loosely, logs Caddy's other errors, drops
query strings from the access log and validates the domain and printer
address more tightly.

go.mod names Go 1.27.4 with no toolchain line, and the workflows check
against the go line. The changelog folds the unreleased entries into
0.2.0, which is not tagged yet.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
57_WolveandClaude Opus 5.5 committed 2026-11-30 08:12:57 -06:00
1 parent 92dcacb3b9
commit eacd0bfd27
18 files changed
+1244 -248

No files matched your search

+299
View File
@@ -164,6 +164,106 @@ func TestLoadErrors(t *testing.T) {
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[log]\nlevel = \"loud\"\n",
wantErr: "log.level",
},
{
name: "unknown empty table",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[printr]\n",
wantErr: "unknown keys: printr",
},
{
name: "unknown empty subtable",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[printer.extra]\n",
wantErr: "unknown keys: printer.extra",
},
{
name: "unknown empty inline table",
body: "extra = {}\n[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n",
wantErr: "unknown keys: extra",
},
{
name: "quoted dotted key",
body: "\"log.level\" = \"debug\"\n[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n",
wantErr: `unknown keys: "log.level"`,
},
{
name: "table for a setting",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[printer.port]\nnumber = 9100\n",
wantErr: "unknown keys: printer.port",
},
{
name: "value for a section",
body: "log = \"debug\"\n[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n",
wantErr: "unknown keys: log",
},
{
name: "fraction for an integer",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nretries = 2.9\n",
wantErr: "printer.retries",
},
{
name: "fraction for a duration",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\ndial_timeout = 2.5\n",
wantErr: "printer.dial_timeout",
},
{
name: "number for a string",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nmodel = 88\n",
wantErr: "printer.model",
},
{
name: "empty string for an integer",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nretries = \"\"\n",
wantErr: "printer.retries",
},
{
name: "number for a flag",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[images]\nallow_private_networks = 1\n",
wantErr: "images.allow_private_networks",
},
{
name: "syntax error names its line",
body: "[printer]\naddress = \"p\n\"\n",
wantErr: "line 2",
},
{
name: "tables that differ only in case",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[Printer]\nport = 9101\n",
wantErr: "keys that differ only in case: Printer and printer",
},
{
name: "settings that differ only in case",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nport = 1000\nPORT = 2000\n",
wantErr: "keys that differ only in case: printer.PORT and printer.port",
},
{
name: "bare number for a duration",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\ndial_timeout = 5\n",
wantErr: `'printer.dial_timeout' expected a duration as a string`,
},
{
name: "number string in another base",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\nretries = \"0x10\"\n",
wantErr: "'printer.retries' expected a whole number in decimal digits",
},
{
name: "flag string other than true or false",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n[sentry]\ndebug = \"1\"\n",
wantErr: "'sentry.debug' expected true or false",
},
{
name: "key with a space is quoted",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \"p\"\n\"retry delay\" = \"1s\"\n",
wantErr: `unknown keys: printer."retry delay"`,
},
{
name: "blank printer address",
body: "[api]\nsecret = \"" + validSecret + "\"\n[printer]\naddress = \" \"\n",
wantErr: "printer.address is required",
},
{
name: "UTF-16 file",
body: "\xff\xfe[\x00a\x00p\x00i\x00]\x00",
wantErr: "the file is UTF-16; save it as UTF-8",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -194,9 +294,208 @@ func TestExampleConfigLoads(t *testing.T) {
}
}
func TestLoadEnvironmentOverridesFile(t *testing.T) {
path := writeConfig(t, `
[api]
secret = "`+validSecret+`"
[printer]
address = "file.lan"
retries = 2
`)
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "env.lan")
t.Setenv("UBERBRINGER_PRINTER_DIAL_TIMEOUT", "2s")
t.Setenv("UBERBRINGER_WEBSERVER_PORT", "9090")
t.Setenv("UBERBRINGER_IMAGES_ALLOW_PRIVATE_NETWORKS", "true")
t.Setenv("UBERBRINGER_WEBSERVER_TRUSTED_PROXIES", "10.0.0.1, 172.18.0.0/16")
t.Setenv("UBERBRINGER_NOT_A_KEY", "ignored")
t.Setenv("UBERBRINGER_PRINTER_RETRIES", "")
cfg, err := config.Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Printer.Address != "env.lan" {
t.Errorf("Address = %q, want the environment's value", cfg.Printer.Address)
}
if cfg.Printer.Retries != 2 {
t.Errorf("Retries = %d, want the file's value, since an empty variable counts as unset", cfg.Printer.Retries)
}
if cfg.Printer.DialTimeout != 2*time.Second || cfg.WebServer.Port != 9090 || !cfg.Images.AllowPrivateNetworks {
t.Errorf("typed environment values not applied: %+v %+v %+v", cfg.Printer, cfg.WebServer, cfg.Images)
}
if got := strings.Join(cfg.WebServer.TrustedProxies, "|"); got != "10.0.0.1|172.18.0.0/16" {
t.Errorf("TrustedProxies = %q, want two trimmed items", got)
}
}
// A variable named after a section rather than a setting changes nothing.
func TestLoadIgnoresSectionNamedVariables(t *testing.T) {
path := writeConfig(t, `
[api]
secret = "`+validSecret+`"
[printer]
address = "file.lan"
[sentry]
environment = "staging"
`)
t.Setenv("UBERBRINGER_PRINTER", "kitchen")
t.Setenv("UBERBRINGER_SENTRY", "x")
cfg, err := config.Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Printer.Address != "file.lan" || cfg.Printer.Port != 9100 {
t.Errorf("printer = %+v, want the file's address and the default port", cfg.Printer)
}
if cfg.Sentry.Environment != "staging" {
t.Errorf("sentry.environment = %q, want the file's value", cfg.Sentry.Environment)
}
}
// Some Windows editors start a UTF-8 file with a byte order mark.
func TestLoadFileWithByteOrderMark(t *testing.T) {
path := writeConfig(t, "\xef\xbb\xbf[api]\nsecret = \""+validSecret+"\"\n[printer]\naddress = \"bom.lan\"\n")
cfg, err := config.Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Printer.Address != "bom.lan" {
t.Errorf("Address = %q", cfg.Printer.Address)
}
}
// An error from a value that may have come from the environment names the
// variables that were set, and never their values.
func TestLoadErrorNamesVariables(t *testing.T) {
path := writeConfig(t, "[api]\nsecret = \""+validSecret+"\"\n[printer]\naddress = \"p\"\n")
t.Setenv("UBERBRINGER_PRINTER_RETRY_DELAY", "soon")
_, err := config.Load(path)
if err == nil {
t.Fatal("Load accepted a bad retry_delay")
}
if msg := err.Error(); !strings.Contains(msg, path) || !strings.Contains(msg, "UBERBRINGER_PRINTER_RETRY_DELAY") {
t.Errorf("error = %q, want it to name the file and the variable", msg)
}
t.Setenv("UBERBRINGER_API_SECRET", "short-env-secret")
_, err = config.Load(path)
if err == nil || strings.Contains(err.Error(), "short-env-secret") {
t.Errorf("error = %v, want a failure that does not contain the secret", err)
}
}
func TestLoadEnvironmentOnly(t *testing.T) {
t.Setenv("UBERBRINGER_API_SECRET", validSecret)
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "192.168.1.245")
t.Setenv("UBERBRINGER_WEBSERVER_BIND_IP", "0.0.0.0")
cfg, err := config.Load("")
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Printer.Address != "192.168.1.245" || cfg.WebServer.BindIP != "0.0.0.0" || cfg.API.Secret != validSecret {
t.Errorf("environment not applied: %+v %+v", cfg.Printer, cfg.WebServer)
}
if def := config.Default(); cfg.Queue.Size != def.Queue.Size || cfg.Printer.Model != def.Printer.Model {
t.Error("defaults not applied without a file")
}
}
// A failed load names the UBERBRINGER_* variables that set nothing, which
// are likely misspelt, but not their values or UBERBRINGER_CONFIG.
func TestLoadErrorNamesMisspeltVariables(t *testing.T) {
t.Setenv("UBERBRINGER_API_SECRETS", validSecret)
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "p")
t.Setenv(config.PathEnvVar, "unused.toml")
_, err := config.Load("")
if err == nil {
t.Fatal("Load accepted a config without api.secret")
}
msg := err.Error()
if !strings.Contains(msg, "variables that set nothing: UBERBRINGER_API_SECRETS") {
t.Errorf("error = %q, want it to name UBERBRINGER_API_SECRETS", msg)
}
if strings.Contains(msg, validSecret) || strings.Contains(msg, config.PathEnvVar) {
t.Errorf("error = %q, want no values and no %s", msg, config.PathEnvVar)
}
}
// Strings for numbers are read as decimal, with spaces around them ignored.
func TestLoadEnvironmentNumbersAreDecimal(t *testing.T) {
t.Setenv("UBERBRINGER_API_SECRET", validSecret)
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "p")
t.Setenv("UBERBRINGER_QUEUE_SIZE", "010")
t.Setenv("UBERBRINGER_WEBSERVER_PORT", " 9090 ")
cfg, err := config.Load("")
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Queue.Size != 10 || cfg.WebServer.Port != 9090 {
t.Errorf("queue.size = %d, webserver.port = %d; want 10 and 9090", cfg.Queue.Size, cfg.WebServer.Port)
}
}
func TestLoadEnvironmentErrors(t *testing.T) {
tests := []struct {
name, key, value, wantErr string
}{
{"not a number", "UBERBRINGER_WEBSERVER_PORT", "eighty", "port"},
{"bad duration", "UBERBRINGER_PRINTER_RETRY_DELAY", "soon", "retry_delay"},
{"bare number for a duration", "UBERBRINGER_PRINTER_RETRY_DELAY", "5", "retry_delay"},
{"fails validation", "UBERBRINGER_LOG_LEVEL", "loud", "log.level"},
{"only spaces", "UBERBRINGER_IMAGES_ALLOWED_HOSTS", " ", "UBERBRINGER_IMAGES_ALLOWED_HOSTS holds only spaces"},
{"trailing comma", "UBERBRINGER_WEBSERVER_TRUSTED_PROXIES", "10.0.0.1,", "a list item is empty"},
{"number for a flag", "UBERBRINGER_SENTRY_DEBUG", "1", "expected true or false"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("UBERBRINGER_API_SECRET", validSecret)
t.Setenv("UBERBRINGER_PRINTER_ADDRESS", "p")
t.Setenv(tt.key, tt.value)
_, err := config.Load("")
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("Load error = %v, want it to contain %q", err, tt.wantErr)
}
})
}
}
func TestLoadMissingFile(t *testing.T) {
_, err := config.Load(filepath.Join(t.TempDir(), "nope.toml"))
if err == nil {
t.Fatal("expected error for missing file")
}
}
func TestValue(t *testing.T) {
cfg := config.Default()
cfg.Images.AllowedHosts = []string{"a.lan", "b.lan"}
tests := []struct {
key, want, wantErr string
}{
{key: "webserver.port", want: "8080"},
{key: "WebServer.Body_Limit", want: "4194304"},
{key: "printer.dial_timeout", want: "5s"},
{key: "webserver.trusted_proxies", want: "127.0.0.1,::1"},
{key: "images.allowed_hosts", want: "a.lan,b.lan"},
{key: "images.allow_private_networks", want: "false"},
{key: "printer", wantErr: `"printer" is a section`},
{key: "printer.adress", wantErr: `unknown key "printer.adress"`},
{key: "printer.port.x", wantErr: `unknown key "printer.port.x"`},
}
for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
got, err := cfg.Value(tt.key)
if tt.wantErr != "" {
if err == nil || !strings.Contains(err.Error(), tt.wantErr) {
t.Fatalf("Value error = %v, want it to contain %q", err, tt.wantErr)
}
return
}
if err != nil || got != tt.want {
t.Errorf("Value = %q, %v; want %q", got, err, tt.want)
}
})
}
}