Files
nmon/internal/app/security.go

93 lines
3.4 KiB
Go

package app
import (
"crypto/tls"
"errors"
"log/slog"
"net/http"
"github.com/v10networks/nmon/internal/auth"
"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
}
// 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) {
provided := f.TLSCert != "" || f.TLSKey != ""
acme := len(f.ACMEHosts) > 0
switch {
case provided && (f.TLSCert == "" || f.TLSKey == ""):
return nil, errors.New("--tls-cert and --tls-key go together")
case provided && acme:
return nil, errors.New("use either --tls-cert/--tls-key or --acme-host, not both")
case f.InsecureHTTP && (provided || acme):
return nil, errors.New("--insecure-http serves plain HTTP; it cannot be combined with TLS flags")
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
}
return nil, errors.New("nmon serves HTTPS by default and has no certificate: pass --tls-cert and --tls-key, " +
"or --acme-host (repeatable) and --acme-email for Let's Encrypt; " +
"for development only, --insecure-http serves plain HTTP")
}
// authMode says how callers are authenticated.
type authMode struct {
// devOpen: no authentication; only allowed together with --insecure-http.
devOpen bool
}
// planAuth refuses to serve over TLS with no way to sign in, and turns authentication off only for
// --insecure-http with no OIDC and no break-glass file (development mode, DEVIATIONS 31).
func planAuth(tr *transport, a config.Auth) (authMode, error) {
configured := a.OIDC != nil || a.BreakGlassFile != ""
switch {
case configured:
return authMode{}, nil
case tr.insecure:
return authMode{devOpen: 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 with authentication off)")
}
// 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.acme != nil:
return "HTTPS with ACME certificates"
}
return "HTTPS with the provided certificate"
}