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
184 lines
7.1 KiB
Go
184 lines
7.1 KiB
Go
package app
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
|
|
"github.com/v10networks/nmon/internal/auth"
|
|
"github.com/v10networks/nmon/internal/clock"
|
|
"github.com/v10networks/nmon/internal/config"
|
|
)
|
|
|
|
// DefaultACMEListen is where the http-01 challenge is served; the CA connects to port 80.
|
|
const DefaultACMEListen = ":80"
|
|
|
|
// transport is how the API is served: plain HTTP for development, or HTTPS.
|
|
type transport struct {
|
|
insecure bool
|
|
files *auth.CertFiles // provided certificate, reloaded on SIGHUP; nil with ACME or plain HTTP
|
|
cfg *tls.Config // nil for plain HTTP
|
|
acme http.Handler // the http-01 challenge handler when ACME is on
|
|
|
|
// httpAddr is the --http-listen address for a reverse proxy; empty when the listener is off. Unlike
|
|
// insecure, it never turns authentication off.
|
|
httpAddr string
|
|
// selfSigned: no certificate was given, so nmon generated (or reused) one under <data-dir>/tls.
|
|
selfSigned bool
|
|
// httpOnly: no TLS is configured, so httpAddr is the only listener and --listen is not bound.
|
|
httpOnly bool
|
|
}
|
|
|
|
// planTransport decides how to serve and refuses the combinations that would expose nmon unprotected:
|
|
// no TLS without --insecure-http, both at once, or half a certificate pair.
|
|
func planTransport(f *config.Flags, dataDir string) (*transport, error) {
|
|
if f.HTTPListen != "" && f.InsecureHTTP {
|
|
return nil, errors.New("--http-listen (plain HTTP behind a reverse proxy, sign-in required) and --insecure-http " +
|
|
"(development mode, no sign-in) are mutually exclusive; use one")
|
|
}
|
|
tr, err := planTLS(f, dataDir)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
tr.httpAddr = f.HTTPListen
|
|
return tr, nil
|
|
}
|
|
|
|
// checkTLSFlags refuses half a certificate pair and flag combinations that contradict each other.
|
|
func checkTLSFlags(f *config.Flags) error {
|
|
provided := f.TLSCert != "" || f.TLSKey != ""
|
|
acme := len(f.ACMEHosts) > 0
|
|
switch {
|
|
case provided && (f.TLSCert == "" || f.TLSKey == ""):
|
|
return errors.New("--tls-cert and --tls-key go together")
|
|
case provided && acme:
|
|
return errors.New("use either --tls-cert/--tls-key or --acme-host, not both")
|
|
case f.InsecureHTTP && (provided || acme):
|
|
return errors.New("--insecure-http serves plain HTTP; it cannot be combined with TLS flags")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// planTLS picks the primary listener's transport from the TLS flags; --http-listen alone yields a plain-HTTP-only
|
|
// transport instead of the "no certificate" refusal.
|
|
func planTLS(f *config.Flags, dataDir string) (*transport, error) {
|
|
if err := checkTLSFlags(f); err != nil {
|
|
return nil, err
|
|
}
|
|
provided := f.TLSCert != "" || f.TLSKey != ""
|
|
acme := len(f.ACMEHosts) > 0
|
|
switch {
|
|
case f.InsecureHTTP:
|
|
return &transport{insecure: true}, nil
|
|
case provided:
|
|
files, err := auth.LoadCertFiles(f.TLSCert, f.TLSKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &transport{files: files, cfg: files.Config()}, nil
|
|
case acme:
|
|
cfg, challenge, err := auth.ACME(auth.ACMEOptions{DataDir: dataDir, Hosts: f.ACMEHosts, Email: f.ACMEEmail})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &transport{cfg: cfg, acme: challenge}, nil
|
|
case f.HTTPListen != "":
|
|
return &transport{httpOnly: true}, nil
|
|
}
|
|
return selfSignedTransport(dataDir)
|
|
}
|
|
|
|
// selfSignedTransport is the fresh-install default: HTTPS with a generated certificate, so nmon never starts on
|
|
// plain HTTP by accident. The pair is created once and reused; replace it with --tls-cert/--tls-key or --acme-host.
|
|
func selfSignedTransport(dataDir string) (*transport, error) {
|
|
host, _ := os.Hostname()
|
|
addrs, _ := net.InterfaceAddrs() // a failure leaves only the loopback names, which is still a working certificate
|
|
certFile, keyFile, _, err := auth.EnsureSelfSigned(dataDir, auth.LocalNames(host, addrs), clock.Real())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("self-signed certificate: %w", err)
|
|
}
|
|
files, err := auth.LoadCertFiles(certFile, keyFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &transport{files: files, cfg: files.Config(), selfSigned: true}, nil
|
|
}
|
|
|
|
// warnSelfSigned tells the operator which certificate browsers will complain about and how to replace it.
|
|
func warnSelfSigned(log *slog.Logger, tr *transport, dataDir string) {
|
|
if !tr.selfSigned {
|
|
return
|
|
}
|
|
log.Warn("a SELF-SIGNED certificate is in use, so browsers will warn; replace it with --tls-cert and --tls-key "+
|
|
"(reloaded on SIGHUP) or --acme-host for Let's Encrypt", "dir", auth.SelfSignedDir(dataDir))
|
|
}
|
|
|
|
// authMode says how callers are authenticated.
|
|
type authMode struct {
|
|
// devOpen: no authentication; only allowed together with --insecure-http.
|
|
devOpen bool
|
|
// install: first-install setup is open to whoever can reach nmon (see install.go).
|
|
install bool
|
|
}
|
|
|
|
// planAuth decides how callers are authenticated. A configured sign-in method means normal authentication;
|
|
// --insecure-http with none is development mode (DEVIATIONS 31); otherwise nmon is in install mode until setup has
|
|
// been completed, and refuses to serve once it has (an install that lost its sign-in must not reopen setup).
|
|
func planAuth(tr *transport, a config.Auth, setupCompleted bool) (authMode, error) {
|
|
switch {
|
|
case signInConfigured(a):
|
|
return authMode{}, nil
|
|
case tr.insecure:
|
|
return authMode{devOpen: true}, nil
|
|
case !setupCompleted:
|
|
return authMode{install: true}, nil
|
|
}
|
|
return authMode{}, errors.New("no way to sign in is configured: set auth.oidc and/or auth.break_glass_file in config.yaml " +
|
|
"(nmon will not serve over TLS or --http-listen with authentication off, and setup was already completed)")
|
|
}
|
|
|
|
// signInConfigured reports whether config.yaml names OIDC or a break-glass file.
|
|
func signInConfigured(a config.Auth) bool { return a.OIDC != nil || a.BreakGlassFile != "" }
|
|
|
|
// warnDevOpen logs, loudly, that every request is an admin.
|
|
func warnDevOpen(log *slog.Logger, addr string) {
|
|
log.Warn("AUTHENTICATION IS DISABLED (development mode): every request is the admin \"dev\"; "+
|
|
"plain HTTP is served on "+addr+"; configure auth.oidc or auth.break_glass_file and use TLS before exposing this port",
|
|
"mode", "dev-open")
|
|
}
|
|
|
|
// describe is the transport in one startup log line.
|
|
func (t *transport) describe() string {
|
|
switch {
|
|
case t.insecure:
|
|
return "plain HTTP (--insecure-http)"
|
|
case t.httpOnly:
|
|
return "plain HTTP only (--http-listen, for a reverse proxy)"
|
|
}
|
|
d := "HTTPS with the provided certificate"
|
|
if t.selfSigned {
|
|
d = "HTTPS with a self-signed certificate"
|
|
}
|
|
if t.acme != nil {
|
|
d = "HTTPS with ACME certificates"
|
|
}
|
|
if t.httpAddr != "" {
|
|
d += " and plain HTTP on " + t.httpAddr + " (--http-listen, for a reverse proxy)"
|
|
}
|
|
return d
|
|
}
|
|
|
|
// warnNoTrustedProxies tells the operator that without auth.trusted_proxies the proxy hides every client.
|
|
func warnNoTrustedProxies(log *slog.Logger, tr *transport, a config.Auth) {
|
|
if tr.httpAddr == "" || len(a.TrustedProxies) > 0 {
|
|
return
|
|
}
|
|
log.Warn("--http-listen is on but auth.trusted_proxies is empty: X-Forwarded-For and X-Forwarded-Proto are ignored, so "+
|
|
"every client is seen as the proxy's address (break-glass lockout becomes per-proxy, audit shows the proxy) and "+
|
|
"session cookies are not marked Secure; list the proxy's address in auth.trusted_proxies", "http_listen", tr.httpAddr)
|
|
}
|