internal: cleanup logging, remove duplicate code

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-12-22 10:33:21 +01:00
parent 22a8603892
commit b3ba083ff0
8 changed files with 26 additions and 54 deletions

View file

@ -35,6 +35,7 @@ lint-fix:
lint: lint:
bandit -r authentik tests lifecycle -x node_modules bandit -r authentik tests lifecycle -x node_modules
pylint authentik tests lifecycle pylint authentik tests lifecycle
golangci-lint run -v
i18n-extract: i18n-extract-core web-extract i18n-extract: i18n-extract-core web-extract

View file

@ -62,7 +62,7 @@ func (ls *LDAPServer) StartLDAPServer() error {
if err != nil { if err != nil {
return err return err
} }
ls.log.Printf("closing %s", ln.Addr()) ls.log.WithField("listen", listen).Info("Stopping LDAP server")
return ls.s.ListenAndServe(listen) return ls.s.ListenAndServe(listen)
} }

View file

@ -50,6 +50,6 @@ func (ls *LDAPServer) StartLDAPTLSServer() error {
if err != nil { if err != nil {
return err return err
} }
ls.log.Printf("closing %s", ln.Addr()) ls.log.WithField("listen", listen).Info("Stopping LDAP SSL Server")
return ls.s.ListenAndServe(listen) return nil
} }

View file

@ -8,7 +8,6 @@ import (
"net" "net"
"net/http" "net/http"
"sync" "sync"
"time"
sentryhttp "github.com/getsentry/sentry-go/http" sentryhttp "github.com/getsentry/sentry-go/http"
"github.com/gorilla/mux" "github.com/gorilla/mux"
@ -119,9 +118,9 @@ func (ps *ProxyServer) ServeHTTP() {
proxyListener := &proxyproto.Listener{Listener: listener} proxyListener := &proxyproto.Listener{Listener: listener}
defer proxyListener.Close() defer proxyListener.Close()
ps.log.Printf("listening on %s", listener.Addr()) ps.log.WithField("listen", listenAddress).Info("Starting HTTP server")
ps.serve(proxyListener) ps.serve(proxyListener)
ps.log.Printf("closing %s", listener.Addr()) ps.log.WithField("listen", listenAddress).Info("Stopping HTTP server")
} }
// ServeHTTPS constructs a net.Listener and starts handling HTTPS requests // ServeHTTPS constructs a net.Listener and starts handling HTTPS requests
@ -135,16 +134,15 @@ func (ps *ProxyServer) ServeHTTPS() {
ln, err := net.Listen("tcp", listenAddress) ln, err := net.Listen("tcp", listenAddress)
if err != nil { if err != nil {
ps.log.Fatalf("listen (%s) failed - %s", listenAddress, err) ps.log.WithError(err).Warning("Failed to listen for HTTPS")
} }
ps.log.Printf("listening on %s", ln.Addr()) proxyListener := &proxyproto.Listener{Listener: web.TCPKeepAliveListener{TCPListener: ln.(*net.TCPListener)}}
proxyListener := &proxyproto.Listener{Listener: tcpKeepAliveListener{ln.(*net.TCPListener)}}
defer proxyListener.Close() defer proxyListener.Close()
tlsListener := tls.NewListener(proxyListener, config) tlsListener := tls.NewListener(proxyListener, config)
ps.log.WithField("listen", listenAddress).Info("Starting HTTPS server")
ps.serve(tlsListener) ps.serve(tlsListener)
ps.log.Printf("closing %s", tlsListener.Addr()) ps.log.WithField("listen", listenAddress).Info("Stopping HTTPS server")
} }
func (ps *ProxyServer) Start() error { func (ps *ProxyServer) Start() error {
@ -179,7 +177,7 @@ func (ps *ProxyServer) serve(listener net.Listener) {
// We received an interrupt signal, shut down. // We received an interrupt signal, shut down.
if err := srv.Shutdown(context.Background()); err != nil { if err := srv.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout: // Error from closing listeners, or context timeout:
ps.log.Printf("HTTP server Shutdown: %v", err) ps.log.WithError(err).Info("HTTP server Shutdown")
} }
close(idleConnsClosed) close(idleConnsClosed)
}() }()
@ -190,27 +188,3 @@ func (ps *ProxyServer) serve(listener net.Listener) {
} }
<-idleConnsClosed <-idleConnsClosed
} }
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually
// go away.
type tcpKeepAliveListener struct {
*net.TCPListener
}
func (ln tcpKeepAliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP()
if err != nil {
return nil, err
}
err = tc.SetKeepAlive(true)
if err != nil {
log.Printf("Error setting Keep-Alive: %v", err)
}
err = tc.SetKeepAlivePeriod(3 * time.Minute)
if err != nil {
log.Printf("Error setting Keep-Alive period: %v", err)
}
return tc, nil
}

View file

@ -1,31 +1,32 @@
package web package web
import ( import (
"log"
"net" "net"
"time" "time"
log "github.com/sirupsen/logrus"
) )
// tcpKeepAliveListener sets TCP keep-alive timeouts on accepted // tcpKeepAliveListener sets TCP keep-alive timeouts on accepted
// connections. It's used by ListenAndServe and ListenAndServeTLS so // connections. It's used by ListenAndServe and ListenAndServeTLS so
// dead TCP connections (e.g. closing laptop mid-download) eventually // dead TCP connections (e.g. closing laptop mid-download) eventually
// go away. // go away.
type tcpKeepAliveListener struct { type TCPKeepAliveListener struct {
*net.TCPListener *net.TCPListener
} }
func (ln tcpKeepAliveListener) Accept() (net.Conn, error) { func (ln TCPKeepAliveListener) Accept() (net.Conn, error) {
tc, err := ln.AcceptTCP() tc, err := ln.AcceptTCP()
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = tc.SetKeepAlive(true) err = tc.SetKeepAlive(true)
if err != nil { if err != nil {
log.Printf("Error setting Keep-Alive: %v", err) log.WithError(err).Warning("Error setting Keep-Alive")
} }
err = tc.SetKeepAlivePeriod(3 * time.Minute) err = tc.SetKeepAlivePeriod(3 * time.Minute)
if err != nil { if err != nil {
log.Printf("Error setting Keep-Alive period: %v", err) log.WithError(err).Warning("Error setting Keep-Alive period")
} }
return tc, nil return tc, nil
} }

View file

@ -52,9 +52,10 @@ func RunMetricsServer() {
return return
} }
}) })
l.WithField("listen", config.G.Web.ListenMetrics).Info("Listening (metrics)") l.WithField("listen", config.G.Web.ListenMetrics).Info("Starting Metrics server")
err := http.ListenAndServe(config.G.Web.ListenMetrics, m) err := http.ListenAndServe(config.G.Web.ListenMetrics, m)
if err != nil { if err != nil {
panic(err) l.WithError(err).Warning("Failed to start metrics server")
} }
l.WithField("listen", config.G.Web.ListenMetrics).Info("Stopping Metrics server")
} }

View file

@ -7,6 +7,7 @@ import (
"github.com/pires/go-proxyproto" "github.com/pires/go-proxyproto"
"goauthentik.io/internal/config" "goauthentik.io/internal/config"
"goauthentik.io/internal/crypto" "goauthentik.io/internal/crypto"
"goauthentik.io/internal/utils/web"
) )
func (ws *WebServer) GetCertificate() func(ch *tls.ClientHelloInfo) (*tls.Certificate, error) { func (ws *WebServer) GetCertificate() func(ch *tls.ClientHelloInfo) (*tls.Certificate, error) {
@ -39,12 +40,11 @@ func (ws *WebServer) listenTLS() {
ws.log.WithError(err).Fatalf("failed to listen") ws.log.WithError(err).Fatalf("failed to listen")
return return
} }
ws.log.WithField("listen", config.G.Web.ListenTLS).Info("Listening (TLS)") proxyListener := &proxyproto.Listener{Listener: web.TCPKeepAliveListener{TCPListener: ln.(*net.TCPListener)}}
proxyListener := &proxyproto.Listener{Listener: tcpKeepAliveListener{ln.(*net.TCPListener)}}
defer proxyListener.Close() defer proxyListener.Close()
tlsListener := tls.NewListener(proxyListener, tlsConfig) tlsListener := tls.NewListener(proxyListener, tlsConfig)
ws.log.WithField("listen", config.G.Web.ListenTLS).Info("Starting HTTPS server")
ws.serve(tlsListener) ws.serve(tlsListener)
ws.log.Printf("closing %s", tlsListener.Addr()) ws.log.WithField("listen", config.G.Web.ListenTLS).Info("Stopping HTTPS server")
} }

View file

@ -74,17 +74,12 @@ func (ws *WebServer) listenPlain() {
if err != nil { if err != nil {
ws.log.WithError(err).Fatal("failed to listen") ws.log.WithError(err).Fatal("failed to listen")
} }
ws.log.WithField("listen", config.G.Web.Listen).Info("Listening")
proxyListener := &proxyproto.Listener{Listener: ln} proxyListener := &proxyproto.Listener{Listener: ln}
defer proxyListener.Close() defer proxyListener.Close()
ws.log.WithField("listen", config.G.Web.Listen).Info("Starting HTTP server")
ws.serve(proxyListener) ws.serve(proxyListener)
ws.log.WithField("listen", config.G.Web.Listen).Info("Stopping HTTP server")
err = http.ListenAndServe(config.G.Web.Listen, ws.m)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
ws.log.WithError(err).Error("failed to listen")
}
} }
func (ws *WebServer) serve(listener net.Listener) { func (ws *WebServer) serve(listener net.Listener) {