Files
nmon/internal/auth/selfsigned_test.go
Vibe Coder f31fb3712a
Some checks failed
ci / lint (push) Successful in 4m19s
ci / openapi (push) Successful in 29s
ci / postgres (push) Successful in 4m55s
ci / test (push) Failing after 19m48s
Add settings forms, first-install wizard, webhook presets, help and poll tools
Make nmon easier to install and run without editing YAML by hand:

- Settings: point-and-click forms over config.yaml (GET/PUT
  /api/v1/settings/form); the YAML editor moves to /settings/yaml.
- First install: with no sign-in configured nmon starts in install
  mode, serves only the setup wizard (sign-in, basics, devices,
  alerting) and closes it for good once sign-in is saved. A
  self-signed certificate is generated when no TLS source is given.
  poller.interval becomes a config setting.
- Reverse proxies: opt-in --http-listen plain-HTTP listener beside
  HTTPS; sign-in stays required.
- Webhooks: Generic, Teams Adaptive Card, Slack and Discord presets,
  template preview and test send.
- Help: the operations, coverage and profile docs served at /help.
- Polling: per-device "Poll now", a dry-run test poll returning an
  SNMP trace, and time-limited in-memory SNMP debug recording, in the
  UI and as `nmon device poll`.
- Ansible: deploys into install mode when no sign-in is set, and no
  longer overwrites the config.yaml the wizard wrote.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017TuKgyKve2XLFtrrXWMi2w
2026-10-25 06:31:12 +00:00

64 lines
1.9 KiB
Go

package auth
import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"net"
"os"
"testing"
"time"
"github.com/v10networks/nmon/internal/clock"
)
func TestSelfSignedIsGeneratedOnceReusedAndCoversTheHost(t *testing.T) {
dir := t.TempDir()
clk := clock.NewFake(time.Date(2026, 10, 24, 12, 0, 0, 0, time.UTC))
names := LocalNames("nmon-test", []net.Addr{
&net.IPNet{IP: net.ParseIP("2001:db8::5"), Mask: net.CIDRMask(64, 128)},
&net.IPNet{IP: net.ParseIP("192.0.2.9"), Mask: net.CIDRMask(24, 32)},
&net.IPNet{IP: net.ParseIP("::1"), Mask: net.CIDRMask(128, 128)},
})
certFile, keyFile, gen, err := EnsureSelfSigned(dir, names, clk)
if err != nil || !gen {
t.Fatalf("first call: gen=%v err=%v", gen, err)
}
st, err := os.Stat(keyFile)
if err != nil || st.Mode().Perm() != 0o600 {
t.Fatalf("key perms: %v %v", st, err)
}
first, _ := os.ReadFile(certFile)
if _, _, gen, err = EnsureSelfSigned(dir, names, clk); err != nil || gen {
t.Fatalf("second call must reuse: gen=%v err=%v", gen, err)
}
if again, _ := os.ReadFile(certFile); string(again) != string(first) {
t.Fatal("certificate changed on reuse")
}
blk, _ := pem.Decode(first)
c, err := x509.ParseCertificate(blk.Bytes)
if err != nil {
t.Fatal(err)
}
if _, ok := c.PublicKey.(*ecdsa.PublicKey); !ok {
t.Fatalf("want ECDSA, got %T", c.PublicKey)
}
for _, want := range []string{"2001:db8::5", "192.0.2.9", "::1", "127.0.0.1"} {
if err := c.VerifyHostname(want); err != nil {
t.Errorf("SAN %s: %v", want, err)
}
}
for _, want := range []string{"nmon-test", "localhost"} {
if err := c.VerifyHostname(want); err != nil {
t.Errorf("SAN %s: %v", want, err)
}
}
if d := c.NotAfter.Sub(clk.Now()); d < 700*24*time.Hour {
t.Errorf("validity too short: %v", d)
}
clk.Advance(750 * 24 * time.Hour) // inside the renewal window
if _, _, gen, _ = EnsureSelfSigned(dir, names, clk); !gen {
t.Error("an expiring certificate must be replaced")
}
}