Files
felhom-agent/internal/config/config_test.go
T
admin a042316d6d feat(agent): scaffold + proxmox interaction layer (slice 1)
Stand up the felhom-agent project (module gitea.dooplex.hu/admin/felhom-agent,
binary felhom-agent) and the internal/proxmox package: the typed library every
other agent module calls to talk to Proxmox.

- API-first Client (hand-rolled REST over net/http, PVEAPIToken auth) with typed
  read ops (version/nodes/status/lxc/config/storage) and async mutating ops
  (restore/vzdump/snapshot/rollback/delete-snapshot/setconfig/start/stop), each
  returning a UPID. WaitTask polls task status until stopped and asserts
  exitstatus OK (authz can surface at task exec, not the POST — phase1-2 §1.3).
- Fenced Privileged (root-CLI) backend for the THREE proven exceptions only
  (keyctl pct create, USB mount/fstab, SMART/sensors); each cites why it can't be
  the API. Fence is structural (Client never shells out, Privileged never HTTPs)
  and asserted in routing_test.go.
- TLS: SHA-256 leaf-cert pinning or CA file; insecure mode explicit + off by
  default. No blanket verification disable.
- 403 -> privilege-named APIError; failed task -> privilege-named TaskError.
- JSON config + env overrides (token never logged); slog logging.
- cmd/felhom-agent --selftest (read-only health report) + gated --selftest=task
  (reversible snapshot/rollback/delete exercise of WaitTask). No daemon loop yet.
- Types grounded in the spike findings and exact JSON shapes captured live from
  demo-felhom (PVE 9.2.2). Unit tests use a mock transport + runner.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-08 14:34:32 +02:00

60 lines
1.6 KiB
Go

package config
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestRedactedMasksSecret(t *testing.T) {
c := Default()
c.Proxmox.Token = "felhom-agent@pve!agent=b6547d9d-08ec-4f22-beb8-a551dc2cd69d"
got := c.Redacted().Proxmox.Token
if strings.Contains(got, "b6547d9d") {
t.Fatalf("secret leaked in redacted token: %q", got)
}
if !strings.HasPrefix(got, "felhom-agent@pve!agent=") {
t.Errorf("redacted token lost its public prefix: %q", got)
}
// The original must be untouched (Redacted returns a copy).
if !strings.Contains(c.Proxmox.Token, "b6547d9d") {
t.Errorf("Redacted mutated the original config")
}
}
func TestValidate(t *testing.T) {
c := Default()
c.Proxmox.Node = "demo-felhom"
c.Proxmox.Token = "felhom-agent@pve!agent=secret"
if err := c.Validate(); err != nil {
t.Fatalf("valid config rejected: %v", err)
}
c.Proxmox.Token = "no-bang-no-eq"
if err := c.Validate(); err == nil {
t.Errorf("malformed token accepted")
}
}
func TestLoadFileThenEnvOverride(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "agent.json")
if err := os.WriteFile(path, []byte(`{"proxmox":{"node":"file-node","token":"u@pve!t=filesecret"}}`), 0o600); err != nil {
t.Fatal(err)
}
t.Setenv("FELHOM_AGENT_PROXMOX_NODE", "env-node")
cfg, err := Load(path)
if err != nil {
t.Fatalf("Load: %v", err)
}
if cfg.Proxmox.Node != "env-node" {
t.Errorf("env did not override node: %q", cfg.Proxmox.Node)
}
if cfg.Proxmox.Token != "u@pve!t=filesecret" {
t.Errorf("token from file lost: %q", cfg.Proxmox.Token)
}
if cfg.Proxmox.Endpoint != "https://127.0.0.1:8006" {
t.Errorf("default endpoint lost: %q", cfg.Proxmox.Endpoint)
}
}