root: fix linting errors

Signed-off-by: Jens Langhammer <jens.langhammer@beryju.org>
This commit is contained in:
Jens Langhammer 2021-07-18 16:12:57 +02:00
parent 36de302250
commit 6ddd6bfa72
8 changed files with 71 additions and 47 deletions

View File

@ -2,8 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"net/url"
"time"
"github.com/getsentry/sentry-go" "github.com/getsentry/sentry-go"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -11,8 +9,6 @@ import (
"goauthentik.io/internal/config" "goauthentik.io/internal/config"
"goauthentik.io/internal/constants" "goauthentik.io/internal/constants"
"goauthentik.io/internal/gounicorn" "goauthentik.io/internal/gounicorn"
"goauthentik.io/internal/outpost/ak"
"goauthentik.io/internal/outpost/proxy"
"goauthentik.io/internal/web" "goauthentik.io/internal/web"
) )
@ -21,18 +17,27 @@ var running = true
func main() { func main() {
log.SetLevel(log.DebugLevel) log.SetLevel(log.DebugLevel)
config.DefaultConfig() config.DefaultConfig()
config.LoadConfig("./authentik/lib/default.yml") err := config.LoadConfig("./authentik/lib/default.yml")
config.LoadConfig("./local.env.yml") if err != nil {
log.WithError(err).Warning("failed to load default config")
}
err = config.LoadConfig("./local.env.yml")
if err != nil {
log.WithError(err).Debug("failed to local config")
}
config.ConfigureLogger() config.ConfigureLogger()
if config.G.ErrorReporting.Enabled { if config.G.ErrorReporting.Enabled {
sentry.Init(sentry.ClientOptions{ err := sentry.Init(sentry.ClientOptions{
Dsn: "https://a579bb09306d4f8b8d8847c052d3a1d3@sentry.beryju.org/8", Dsn: "https://a579bb09306d4f8b8d8847c052d3a1d3@sentry.beryju.org/8",
AttachStacktrace: true, AttachStacktrace: true,
TracesSampleRate: 0.6, TracesSampleRate: 0.6,
Release: fmt.Sprintf("authentik@%s", constants.VERSION), Release: fmt.Sprintf("authentik@%s", constants.VERSION),
Environment: config.G.ErrorReporting.Environment, Environment: config.G.ErrorReporting.Environment,
}) })
if err != nil {
log.WithError(err).Warning("failed to init sentry")
}
} }
ex := common.Init() ex := common.Init()
@ -58,45 +63,45 @@ func main() {
} }
} }
func attemptStartBackend(g *gounicorn.GoUnicorn) error { func attemptStartBackend(g *gounicorn.GoUnicorn) {
for { for {
err := g.Start() err := g.Start()
if !running { if !running {
return nil return
} }
log.WithField("logger", "authentik.g").WithError(err).Warning("gunicorn process died, restarting") log.WithField("logger", "authentik.g").WithError(err).Warning("gunicorn process died, restarting")
} }
} }
func attemptProxyStart(u *url.URL) error { // func attemptProxyStart(u *url.URL) error {
maxTries := 100 // maxTries := 100
attempt := 0 // attempt := 0
for { // for {
log.WithField("logger", "authentik").Debug("attempting to init outpost") // log.WithField("logger", "authentik").Debug("attempting to init outpost")
ac := ak.NewAPIController(*u, config.G.SecretKey) // ac := ak.NewAPIController(*u, config.G.SecretKey)
if ac == nil { // if ac == nil {
attempt += 1 // attempt += 1
time.Sleep(1 * time.Second) // time.Sleep(1 * time.Second)
if attempt > maxTries { // if attempt > maxTries {
break // break
} // }
continue // continue
} // }
ac.Server = proxy.NewServer(ac) // ac.Server = proxy.NewServer(ac)
err := ac.Start() // err := ac.Start()
log.WithField("logger", "authentik").Debug("attempting to start outpost") // log.WithField("logger", "authentik").Debug("attempting to start outpost")
if err != nil { // if err != nil {
attempt += 1 // attempt += 1
time.Sleep(5 * time.Second) // time.Sleep(5 * time.Second)
if attempt > maxTries { // if attempt > maxTries {
break // break
} // }
continue // continue
} // }
if !running { // if !running {
ac.Shutdown() // ac.Shutdown()
return nil // return nil
} // }
} // }
return nil // return nil
} // }

View File

@ -58,7 +58,10 @@ func (g *GoUnicorn) Start() error {
return g.p.Run() return g.p.Run()
} }
func (g *GoUnicorn) Kill() error { func (g *GoUnicorn) Kill() {
g.killed = true g.killed = true
return g.p.Process.Kill() err := g.p.Process.Kill()
if err != nil {
g.log.WithError(err).Warning("failed to kill gunicorn")
}
} }

View File

@ -5,10 +5,12 @@ import (
"net/http" "net/http"
sentryhttp "github.com/getsentry/sentry-go/http" sentryhttp "github.com/getsentry/sentry-go/http"
log "github.com/sirupsen/logrus"
) )
func recoveryMiddleware() func(next http.Handler) http.Handler { func recoveryMiddleware() func(next http.Handler) http.Handler {
sentryHandler := sentryhttp.New(sentryhttp.Options{}) sentryHandler := sentryhttp.New(sentryhttp.Options{})
l := log.WithField("logger", "authentik.sentry")
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
sentryHandler.Handle(next) sentryHandler.Handle(next)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -20,6 +22,7 @@ func recoveryMiddleware() func(next http.Handler) http.Handler {
} }
err := re.(error) err := re.(error)
if err != nil { if err != nil {
l.WithError(err).Warning("global panic handler")
jsonBody, _ := json.Marshal(struct { jsonBody, _ := json.Marshal(struct {
Successful bool Successful bool
Error string Error string
@ -30,7 +33,10 @@ func recoveryMiddleware() func(next http.Handler) http.Handler {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
w.Write(jsonBody) _, err := w.Write(jsonBody)
if err != nil {
l.WithError(err).Warning("Failed to write sentry error body")
}
} }
}() }()
}) })

View File

@ -66,7 +66,10 @@ func (ws *WebServer) listenPlain() {
ws.serve(ln) ws.serve(ln)
ws.log.WithField("addr", config.G.Web.Listen).Info("Running") ws.log.WithField("addr", config.G.Web.Listen).Info("Running")
http.ListenAndServe(config.G.Web.Listen, ws.m) err = http.ListenAndServe(config.G.Web.Listen, ws.m)
if err != nil && !errors.Is(err, http.ErrServerClosed) {
ws.log.Errorf("ERROR: http.Serve() - %s", err)
}
} }
func (ws *WebServer) serve(listener net.Listener) { func (ws *WebServer) serve(listener net.Listener) {

View File

@ -23,6 +23,7 @@ func (ws *WebServer) listenTLS() {
ln, err := net.Listen("tcp", config.G.Web.ListenTLS) ln, err := net.Listen("tcp", config.G.Web.ListenTLS)
if err != nil { if err != nil {
ws.log.WithError(err).Fatalf("failed to listen") ws.log.WithError(err).Fatalf("failed to listen")
return
} }
ws.log.WithField("addr", config.G.Web.ListenTLS).Info("Running") ws.log.WithField("addr", config.G.Web.ListenTLS).Info("Running")

View File

@ -29,12 +29,18 @@ func (ws *WebServer) configureStatic() {
ws.lh.Path("/robots.txt").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { ws.lh.Path("/robots.txt").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header()["Content-Type"] = []string{"text/plain"} rw.Header()["Content-Type"] = []string{"text/plain"}
rw.WriteHeader(200) rw.WriteHeader(200)
rw.Write(staticWeb.RobotsTxt) _, err := rw.Write(staticWeb.RobotsTxt)
if err != nil {
ws.log.WithError(err).Warning("failed to write response")
}
}) })
ws.lh.Path("/.well-known/security.txt").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { ws.lh.Path("/.well-known/security.txt").HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
rw.Header()["Content-Type"] = []string{"text/plain"} rw.Header()["Content-Type"] = []string{"text/plain"}
rw.WriteHeader(200) rw.WriteHeader(200)
rw.Write(staticWeb.SecurityTxt) _, err := rw.Write(staticWeb.SecurityTxt)
if err != nil {
ws.log.WithError(err).Warning("failed to write response")
}
}) })
} }

0
web/dist/.gitkeep vendored Normal file
View File

0
website/help/.gitkeep Normal file
View File