2021-05-02 22:49:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-05-04 12:28:48 +00:00
|
|
|
"fmt"
|
2021-12-20 18:42:45 +00:00
|
|
|
"net/http"
|
2021-07-29 09:30:30 +00:00
|
|
|
"net/url"
|
|
|
|
"time"
|
2021-05-02 22:49:16 +00:00
|
|
|
|
2021-05-04 12:28:48 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-05-02 22:49:16 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-06-16 15:29:01 +00:00
|
|
|
"goauthentik.io/internal/common"
|
2021-05-02 22:49:16 +00:00
|
|
|
"goauthentik.io/internal/config"
|
2021-05-04 12:28:48 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-05-02 22:49:16 +00:00
|
|
|
"goauthentik.io/internal/gounicorn"
|
2021-07-29 09:30:30 +00:00
|
|
|
"goauthentik.io/internal/outpost/ak"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/outpost/proxyv2"
|
2021-05-02 22:49:16 +00:00
|
|
|
"goauthentik.io/internal/web"
|
2021-12-22 10:43:45 +00:00
|
|
|
"goauthentik.io/internal/web/tenant_tls"
|
2021-05-02 22:49:16 +00:00
|
|
|
)
|
|
|
|
|
2021-07-17 16:04:09 +00:00
|
|
|
var running = true
|
|
|
|
|
2021-05-02 22:49:16 +00:00
|
|
|
func main() {
|
2021-05-04 12:07:12 +00:00
|
|
|
log.SetLevel(log.DebugLevel)
|
2021-12-20 20:23:19 +00:00
|
|
|
log.SetFormatter(&log.JSONFormatter{
|
|
|
|
FieldMap: log.FieldMap{
|
|
|
|
log.FieldKeyMsg: "event",
|
|
|
|
log.FieldKeyTime: "timestamp",
|
|
|
|
},
|
|
|
|
})
|
2021-11-06 12:54:59 +00:00
|
|
|
l := log.WithField("logger", "authentik.root")
|
2021-05-02 22:49:16 +00:00
|
|
|
config.DefaultConfig()
|
2021-07-18 14:12:57 +00:00
|
|
|
err := config.LoadConfig("./authentik/lib/default.yml")
|
|
|
|
if err != nil {
|
2021-11-06 12:54:59 +00:00
|
|
|
l.WithError(err).Warning("failed to load default config")
|
2021-07-18 14:12:57 +00:00
|
|
|
}
|
|
|
|
err = config.LoadConfig("./local.env.yml")
|
|
|
|
if err != nil {
|
2021-11-06 12:54:59 +00:00
|
|
|
l.WithError(err).Debug("no local config to load")
|
2021-07-18 14:12:57 +00:00
|
|
|
}
|
2021-07-29 09:30:30 +00:00
|
|
|
err = config.FromEnv()
|
|
|
|
if err != nil {
|
2021-11-06 12:54:59 +00:00
|
|
|
l.WithError(err).Debug("failed to environment variables")
|
2021-07-29 09:30:30 +00:00
|
|
|
}
|
2021-05-02 22:49:16 +00:00
|
|
|
config.ConfigureLogger()
|
|
|
|
|
2021-05-04 12:28:48 +00:00
|
|
|
if config.G.ErrorReporting.Enabled {
|
2021-07-18 14:12:57 +00:00
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
2021-11-02 13:44:37 +00:00
|
|
|
Dsn: config.G.ErrorReporting.DSN,
|
2021-05-04 12:28:48 +00:00
|
|
|
AttachStacktrace: true,
|
2021-12-20 18:42:26 +00:00
|
|
|
TracesSampleRate: config.G.ErrorReporting.SampleRate,
|
2021-05-04 12:28:48 +00:00
|
|
|
Release: fmt.Sprintf("authentik@%s", constants.VERSION),
|
|
|
|
Environment: config.G.ErrorReporting.Environment,
|
2021-12-20 18:42:45 +00:00
|
|
|
IgnoreErrors: []string{
|
|
|
|
http.ErrAbortHandler.Error(),
|
|
|
|
},
|
2021-05-04 12:28:48 +00:00
|
|
|
})
|
2021-07-18 14:12:57 +00:00
|
|
|
if err != nil {
|
2021-11-06 12:54:59 +00:00
|
|
|
l.WithError(err).Warning("failed to init sentry")
|
2021-07-18 14:12:57 +00:00
|
|
|
}
|
2021-05-04 12:28:48 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 18:40:51 +00:00
|
|
|
ex := common.Init()
|
2021-06-16 15:29:01 +00:00
|
|
|
defer common.Defer()
|
|
|
|
|
2021-07-29 09:30:30 +00:00
|
|
|
u, _ := url.Parse("http://localhost:8000")
|
2021-06-23 18:40:51 +00:00
|
|
|
|
2021-06-29 14:21:00 +00:00
|
|
|
g := gounicorn.NewGoUnicorn()
|
2021-10-05 20:19:33 +00:00
|
|
|
ws := web.NewWebServer(g)
|
2021-11-04 12:16:53 +00:00
|
|
|
g.HealthyCallback = func() {
|
|
|
|
if !config.G.Web.DisableEmbeddedOutpost {
|
|
|
|
go attemptProxyStart(ws, u)
|
|
|
|
}
|
|
|
|
}
|
2021-09-09 13:52:24 +00:00
|
|
|
go web.RunMetricsServer()
|
2021-06-23 18:40:51 +00:00
|
|
|
for {
|
2021-07-17 16:04:09 +00:00
|
|
|
go attemptStartBackend(g)
|
2021-06-29 14:21:00 +00:00
|
|
|
ws.Start()
|
2021-06-23 18:40:51 +00:00
|
|
|
|
2021-06-29 14:21:00 +00:00
|
|
|
<-ex
|
2021-07-17 16:04:09 +00:00
|
|
|
running = false
|
2021-12-20 20:23:19 +00:00
|
|
|
l.Info("shutting down gunicorn")
|
2021-11-06 12:54:59 +00:00
|
|
|
go g.Kill()
|
2021-12-20 20:23:19 +00:00
|
|
|
l.Info("shutting down webserver")
|
2021-07-17 15:02:24 +00:00
|
|
|
go ws.Shutdown()
|
2021-06-23 18:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-18 14:12:57 +00:00
|
|
|
func attemptStartBackend(g *gounicorn.GoUnicorn) {
|
2021-06-23 18:40:51 +00:00
|
|
|
for {
|
2021-07-17 16:04:09 +00:00
|
|
|
if !running {
|
2021-07-18 14:12:57 +00:00
|
|
|
return
|
2021-06-29 14:21:00 +00:00
|
|
|
}
|
2021-11-06 12:54:59 +00:00
|
|
|
err := g.Start()
|
2021-10-11 15:51:49 +00:00
|
|
|
log.WithField("logger", "authentik.router").WithError(err).Warning("gunicorn process died, restarting")
|
2021-06-23 18:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 09:30:30 +00:00
|
|
|
func attemptProxyStart(ws *web.WebServer, u *url.URL) {
|
|
|
|
maxTries := 100
|
|
|
|
attempt := 0
|
2021-12-20 20:23:19 +00:00
|
|
|
l := log.WithField("logger", "authentik.server")
|
2021-07-29 09:30:30 +00:00
|
|
|
for {
|
2021-12-20 20:23:19 +00:00
|
|
|
l.Debug("attempting to init outpost")
|
2021-07-29 09:30:30 +00:00
|
|
|
ac := ak.NewAPIController(*u, config.G.SecretKey)
|
|
|
|
if ac == nil {
|
|
|
|
attempt += 1
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
if attempt > maxTries {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2021-12-22 10:43:45 +00:00
|
|
|
// Init tenant_tls here too since it requires an API Client,
|
|
|
|
// so we just re-use the same one as the outpost uses
|
|
|
|
tw := tenant_tls.NewWatcher(ac.Client)
|
|
|
|
go tw.Start()
|
|
|
|
ws.TenantTLS = tw
|
|
|
|
|
2021-09-09 08:23:46 +00:00
|
|
|
srv := proxyv2.NewProxyServer(ac, 0)
|
2021-07-29 09:30:30 +00:00
|
|
|
ws.ProxyServer = srv
|
|
|
|
ac.Server = srv
|
2021-12-20 20:23:19 +00:00
|
|
|
l.Debug("attempting to start outpost")
|
2021-07-29 09:30:30 +00:00
|
|
|
err := ac.StartBackgorundTasks()
|
|
|
|
if err != nil {
|
2021-12-20 20:23:19 +00:00
|
|
|
l.WithError(err).Warning("outpost failed to start")
|
2021-07-29 09:30:30 +00:00
|
|
|
attempt += 1
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
if attempt > maxTries {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
select {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|