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
202 lines
7.5 KiB
Go
202 lines
7.5 KiB
Go
package app
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/v10networks/nmon/internal/api"
|
|
"github.com/v10networks/nmon/internal/auth"
|
|
"github.com/v10networks/nmon/internal/clock"
|
|
"github.com/v10networks/nmon/internal/config"
|
|
)
|
|
|
|
// Install mode is the first-install wizard's window. It is open only while no sign-in method is configured and
|
|
// setup was never completed; the stored flag (store.SetupState) makes the end permanent, so editing config.yaml or
|
|
// deleting the break-glass file later cannot reopen it. There is no setup token and no localhost restriction by
|
|
// owner decision: nmon is deployed inside a DMZ or an internal network, and whoever reaches it in install mode can
|
|
// run setup (DEVIATIONS 51). The auth service enforces the allow-list (auth.Policy.AllowInstall).
|
|
|
|
// installWarnEvery is how often the "setup is open" warning repeats while install mode lasts.
|
|
const installWarnEvery = 10 * time.Minute
|
|
|
|
// breakGlassFileName is the break-glass file the wizard writes inside the data dir.
|
|
const breakGlassFileName = "breakglass"
|
|
|
|
// decideAuth runs once the inventory is open. It reads the stored setup flag, applies planAuth, and marks an
|
|
// existing install (one that already has a sign-in method) completed, so it can never enter install mode later.
|
|
func (a *App) decideAuth(ctx context.Context, s *settings) error {
|
|
done, _, err := a.st.SetupCompleted(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("read setup state: %w", err)
|
|
}
|
|
mode, err := planAuth(s.tr, s.cfg.Auth, done)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !done && signInConfigured(s.cfg.Auth) {
|
|
if err := a.st.MarkSetupCompleted(ctx, "startup: a sign-in method was already configured"); err != nil {
|
|
return fmt.Errorf("mark setup completed: %w", err)
|
|
}
|
|
a.log.Info("sign-in is already configured: setup marked completed, install mode can never open")
|
|
}
|
|
s.mode = mode
|
|
a.install.Store(mode.install)
|
|
return nil
|
|
}
|
|
|
|
// authMiddleware authenticates with whichever auth service is live, so the swap on setup completion applies to the
|
|
// very next request.
|
|
func (a *App) authMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { a.curAuth().Middleware(next).ServeHTTP(w, r) })
|
|
}
|
|
|
|
// startInstallWarnings logs the "setup is open" warning now and every installWarnEvery until install mode ends.
|
|
func (a *App) startInstallWarnings() {
|
|
warn := func() {
|
|
a.log.Warn("INSTALL MODE: setup is open to anyone who can reach this address, and they are treated as an admin; "+
|
|
"open the web UI now and finish setup (sign-in comes first)", "mode", "install", "listen", a.listenAddr())
|
|
}
|
|
warn()
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
done := make(chan struct{})
|
|
go func() {
|
|
defer close(done)
|
|
t := clock.Real().NewTicker(installWarnEvery)
|
|
defer t.Stop()
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-t.C():
|
|
if !a.install.Load() {
|
|
return
|
|
}
|
|
warn()
|
|
}
|
|
}
|
|
}()
|
|
a.onStop("install warnings", func(context.Context) error { cancel(); <-done; return nil })
|
|
}
|
|
|
|
// listenAddr names where nmon is reachable, for the install warning.
|
|
func (a *App) listenAddr() string {
|
|
if a.tr.httpOnly {
|
|
return a.tr.httpAddr
|
|
}
|
|
return a.flags.Listen
|
|
}
|
|
|
|
// InstallMode implements api.SetupAdmin.
|
|
func (a *App) InstallMode() bool { return a.install.Load() }
|
|
|
|
// SetupCompleted implements api.SetupAdmin.
|
|
func (a *App) SetupCompleted(ctx context.Context) (bool, error) {
|
|
done, _, err := a.st.SetupCompleted(ctx)
|
|
return done, err
|
|
}
|
|
|
|
// SignIn implements api.SetupAdmin: break-glass file first (it validates the user and password), then the optional
|
|
// OIDC secret, then auth.* in config.yaml through the ordinary settings save path.
|
|
func (a *App) SignIn(ctx context.Context, actor string, in api.SetupSignInBody) (api.SetupSignIn, error) {
|
|
a.setupMu.Lock()
|
|
defer a.setupMu.Unlock()
|
|
if !a.install.Load() {
|
|
return api.SetupSignIn{}, api.ErrSetupClosed
|
|
}
|
|
user := strings.TrimSpace(in.Username)
|
|
if err := checkSetupOIDC(in.OIDC); err != nil {
|
|
return api.SetupSignIn{}, fmt.Errorf("%w: %w", api.ErrInvalidSettings, err)
|
|
}
|
|
file := filepath.Join(a.dataDir, breakGlassFileName)
|
|
if err := auth.WriteBreakGlassFile(file, user, in.Password); err != nil {
|
|
return api.SetupSignIn{}, fmt.Errorf("%w: password: %w", api.ErrInvalidSettings, err)
|
|
}
|
|
if o := in.OIDC; o != nil {
|
|
if err := a.st.PutSecret(ctx, api.OIDCSecretName, o.ClientSecret); err != nil {
|
|
return api.SetupSignIn{}, fmt.Errorf("store the OIDC client secret: %w", err)
|
|
}
|
|
}
|
|
_, err := a.UpdateConfig(ctx, actor, func(c *config.Config) error {
|
|
c.Auth.BreakGlassFile, c.Auth.BreakGlassUser = file, ""
|
|
c.Auth.OIDC = oidcConfigFor(in.OIDC)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return api.SetupSignIn{}, err
|
|
}
|
|
if err := a.st.AppendAudit(ctx, actor, "setup.signin", "auth", nil, map[string]any{"user": user, "oidc": in.OIDC != nil}); err != nil {
|
|
a.log.Warn("audit setup.signin", "err", err)
|
|
}
|
|
return api.SetupSignIn{BreakGlassUser: user, OIDC: in.OIDC != nil}, nil
|
|
}
|
|
|
|
// oidcConfigFor is the config.yaml block for the wizard's OIDC fields, or nil without them.
|
|
func oidcConfigFor(o *api.SetupOIDC) *config.OIDC {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
return &config.OIDC{Issuer: strings.TrimSpace(o.Issuer), ClientID: strings.TrimSpace(o.ClientID),
|
|
ClientSecretName: api.OIDCSecretName, RedirectURL: strings.TrimSpace(o.RedirectURL),
|
|
GroupsClaim: strings.TrimSpace(o.GroupsClaim), AdminGroup: strings.TrimSpace(o.AdminGroup)}
|
|
}
|
|
|
|
// checkSetupOIDC validates the OIDC fields before anything is written. Discovery is not contacted here: the
|
|
// provider is only reached at the first sign-in, as it is for an OIDC block written by hand.
|
|
func checkSetupOIDC(o *api.SetupOIDC) error {
|
|
if o == nil {
|
|
return nil
|
|
}
|
|
return config.Auth{OIDC: oidcConfigFor(o)}.Validate()
|
|
}
|
|
|
|
// Complete implements api.SetupAdmin. Order matters: the new auth service is built first (a failure leaves install
|
|
// mode on), then the flag is stored (the point of no return), then the live service is swapped.
|
|
func (a *App) Complete(ctx context.Context, actor string) (api.SetupComplete, error) {
|
|
a.setupMu.Lock()
|
|
defer a.setupMu.Unlock()
|
|
if !a.install.Load() {
|
|
return api.SetupComplete{}, api.ErrSetupClosed
|
|
}
|
|
cfg := a.live.get()
|
|
if !signInConfigured(cfg.Auth) {
|
|
return api.SetupComplete{}, api.ErrSetupNoSignIn
|
|
}
|
|
svc, err := a.newAuth(ctx, cfg.Auth, authMode{})
|
|
if err != nil {
|
|
return api.SetupComplete{}, fmt.Errorf("build authentication: %w", err)
|
|
}
|
|
if err := a.st.MarkSetupCompleted(ctx, actor); err != nil {
|
|
return api.SetupComplete{}, fmt.Errorf("mark setup completed: %w", err)
|
|
}
|
|
a.swapAuth(svc, cfg.Auth)
|
|
if err := a.st.AppendAudit(ctx, actor, "setup.complete", "setup", nil, nil); err != nil {
|
|
a.log.Warn("audit setup.complete", "err", err)
|
|
}
|
|
a.log.Info("setup completed: authentication is on, install mode is closed for good", "by", actor, "oidc", svc.OIDCEnabled())
|
|
url := "/auth/breakglass"
|
|
if svc.OIDCEnabled() {
|
|
url = "/auth/login"
|
|
}
|
|
return api.SetupComplete{SignInURL: url}, nil
|
|
}
|
|
|
|
// swapAuth makes svc the live authentication without a restart. Auth is already behind an atomic pointer that
|
|
// the middleware, the /auth/ handler and the trusted-proxy reload read per request, so the swap is one store; the
|
|
// old service's housekeeping is replaced by the new one's. The auth section is now what is running, so it no
|
|
// longer counts as "restart required".
|
|
func (a *App) swapAuth(svc *auth.Auth, ac config.Auth) {
|
|
a.stopAuthRun()
|
|
a.authp.Store(svc)
|
|
a.runAuth(svc)
|
|
a.install.Store(false)
|
|
a.live.mu.Lock()
|
|
a.live.running.Auth = ac
|
|
a.live.mu.Unlock()
|
|
}
|
|
|
|
var _ api.SetupAdmin = (*App)(nil)
|