2021-01-16 20:41:39 +00:00
|
|
|
package ak
|
|
|
|
|
|
|
|
import (
|
2021-05-16 19:07:01 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-01-16 20:41:39 +00:00
|
|
|
"math/rand"
|
2021-05-16 19:07:01 +00:00
|
|
|
"net/http"
|
2021-01-16 20:41:39 +00:00
|
|
|
"net/url"
|
2021-11-24 21:41:55 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2021-01-16 20:41:39 +00:00
|
|
|
"time"
|
|
|
|
|
2021-12-12 12:46:31 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-05-11 19:46:30 +00:00
|
|
|
"github.com/google/uuid"
|
2021-12-11 21:48:33 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2021-09-08 18:04:56 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2022-03-03 09:40:07 +00:00
|
|
|
"goauthentik.io/api/v3"
|
2021-07-17 10:49:13 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2022-06-20 09:54:10 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-10-08 23:06:52 +00:00
|
|
|
type WSHandler func(ctx context.Context, args map[string]interface{})
|
|
|
|
|
2021-01-16 20:41:39 +00:00
|
|
|
const ConfigLogLevel = "log_level"
|
|
|
|
|
|
|
|
// APIController main controller which connects to the authentik api via http and ws
|
|
|
|
type APIController struct {
|
2021-08-21 12:13:46 +00:00
|
|
|
Client *api.APIClient
|
|
|
|
Outpost api.Outpost
|
2022-05-26 13:15:30 +00:00
|
|
|
GlobalConfig *api.Config
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
Server Outpost
|
|
|
|
|
2021-08-21 12:13:46 +00:00
|
|
|
token string
|
|
|
|
|
2021-04-23 08:08:19 +00:00
|
|
|
logger *log.Entry
|
2021-01-16 20:41:39 +00:00
|
|
|
|
2021-12-11 21:48:33 +00:00
|
|
|
reloadOffset time.Duration
|
|
|
|
|
|
|
|
wsConn *websocket.Conn
|
|
|
|
lastWsReconnect time.Time
|
|
|
|
wsIsReconnecting bool
|
|
|
|
wsBackoffMultiplier int
|
2023-10-08 23:06:52 +00:00
|
|
|
wsHandlers []WSHandler
|
2021-12-22 23:38:49 +00:00
|
|
|
refreshHandlers []func()
|
2021-01-16 20:41:39 +00:00
|
|
|
|
2021-05-11 19:46:30 +00:00
|
|
|
instanceUUID uuid.UUID
|
2021-01-16 20:41:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPIController initialise new API Controller instance from URL and API token
|
2021-04-19 16:38:16 +00:00
|
|
|
func NewAPIController(akURL url.URL, token string) *APIController {
|
2021-12-13 15:18:26 +00:00
|
|
|
rsp := sentry.StartSpan(context.Background(), "authentik.outposts.init")
|
2021-12-12 12:46:31 +00:00
|
|
|
|
2021-05-16 19:07:01 +00:00
|
|
|
config := api.NewConfiguration()
|
|
|
|
config.Host = akURL.Host
|
|
|
|
config.Scheme = akURL.Scheme
|
|
|
|
config.HTTPClient = &http.Client{
|
2023-02-08 13:13:12 +00:00
|
|
|
Transport: web.NewUserAgentTransport(
|
|
|
|
constants.OutpostUserAgent(),
|
|
|
|
web.NewTracingTransport(
|
|
|
|
rsp.Context(),
|
|
|
|
GetTLSTransport(),
|
|
|
|
),
|
|
|
|
),
|
2021-05-16 19:07:01 +00:00
|
|
|
}
|
|
|
|
config.AddDefaultHeader("Authorization", fmt.Sprintf("Bearer %s", token))
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
// create the API client, with the transport
|
2021-05-16 19:07:01 +00:00
|
|
|
apiClient := api.NewAPIClient(config)
|
2021-01-16 20:41:39 +00:00
|
|
|
|
2021-02-11 22:48:54 +00:00
|
|
|
log := log.WithField("logger", "authentik.outpost.ak-api-controller")
|
|
|
|
|
2021-01-16 20:41:39 +00:00
|
|
|
// Because we don't know the outpost UUID, we simply do a list and pick the first
|
|
|
|
// The service account this token belongs to should only have access to a single outpost
|
2021-05-16 19:35:23 +00:00
|
|
|
outposts, _, err := apiClient.OutpostsApi.OutpostsInstancesList(context.Background()).Execute()
|
2021-01-16 20:41:39 +00:00
|
|
|
if err != nil {
|
2022-06-02 22:06:09 +00:00
|
|
|
log.WithError(err).Error("Failed to fetch outpost configuration, retrying in 3 seconds")
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
return NewAPIController(akURL, token)
|
2021-01-16 20:41:39 +00:00
|
|
|
}
|
2023-02-08 13:13:12 +00:00
|
|
|
if len(outposts.Results) < 1 {
|
|
|
|
panic("No outposts found with given token, ensure the given token corresponds to an authenitk Outpost")
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
outpost := outposts.Results[0]
|
2021-01-16 20:41:39 +00:00
|
|
|
|
2021-08-21 14:17:30 +00:00
|
|
|
log.WithField("name", outpost.Name).Debug("Fetched outpost configuration")
|
|
|
|
|
|
|
|
akConfig, _, err := apiClient.RootApi.RootConfigRetrieve(context.Background()).Execute()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to fetch global configuration")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
log.Debug("Fetched global configuration")
|
|
|
|
|
2021-12-21 12:10:57 +00:00
|
|
|
// doGlobalSetup is called by the OnRefresh handler, which ticks on start
|
|
|
|
// doGlobalSetup(outpost, akConfig)
|
2021-11-29 13:42:19 +00:00
|
|
|
|
2021-01-16 20:41:39 +00:00
|
|
|
ac := &APIController{
|
2021-08-21 12:13:46 +00:00
|
|
|
Client: apiClient,
|
|
|
|
GlobalConfig: akConfig,
|
2021-01-16 20:41:39 +00:00
|
|
|
|
2021-08-21 12:13:46 +00:00
|
|
|
token: token,
|
2021-02-11 22:48:54 +00:00
|
|
|
logger: log,
|
2021-01-16 20:41:39 +00:00
|
|
|
|
2021-12-11 21:48:33 +00:00
|
|
|
reloadOffset: time.Duration(rand.Intn(10)) * time.Second,
|
|
|
|
instanceUUID: uuid.New(),
|
|
|
|
Outpost: outpost,
|
2023-10-08 23:06:52 +00:00
|
|
|
wsHandlers: []WSHandler{},
|
2021-12-11 21:48:33 +00:00
|
|
|
wsBackoffMultiplier: 1,
|
2021-12-22 23:38:49 +00:00
|
|
|
refreshHandlers: make([]func(), 0),
|
2021-01-16 20:41:39 +00:00
|
|
|
}
|
2021-11-24 21:41:55 +00:00
|
|
|
ac.logger.WithField("offset", ac.reloadOffset.String()).Debug("HA Reload offset")
|
2021-12-11 21:48:33 +00:00
|
|
|
err = ac.initWS(akURL, outpost.Pk)
|
|
|
|
if err != nil {
|
|
|
|
go ac.reconnectWS()
|
|
|
|
}
|
2021-11-24 21:41:55 +00:00
|
|
|
ac.configureRefreshSignal()
|
2021-01-16 20:41:39 +00:00
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start Starts all handlers, non-blocking
|
|
|
|
func (a *APIController) Start() error {
|
2021-12-20 20:47:32 +00:00
|
|
|
err := a.Server.Refresh()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-26 13:15:30 +00:00
|
|
|
err = a.StartBackgroundTasks()
|
2021-07-29 09:30:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
err := a.Server.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-24 21:41:55 +00:00
|
|
|
func (a *APIController) configureRefreshSignal() {
|
|
|
|
s := make(chan os.Signal, 1)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
<-s
|
|
|
|
err := a.OnRefresh()
|
|
|
|
if err != nil {
|
|
|
|
a.logger.WithError(err).Warning("failed to refresh")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
signal.Notify(s, syscall.SIGUSR1)
|
|
|
|
a.logger.Debug("Enabled USR1 hook to reload")
|
|
|
|
}
|
|
|
|
|
2021-12-22 23:38:49 +00:00
|
|
|
func (a *APIController) AddRefreshHandler(handler func()) {
|
|
|
|
a.refreshHandlers = append(a.refreshHandlers, handler)
|
|
|
|
}
|
|
|
|
|
2023-10-08 23:06:52 +00:00
|
|
|
func (a *APIController) AddWSHandler(handler WSHandler) {
|
|
|
|
a.wsHandlers = append(a.wsHandlers, handler)
|
|
|
|
}
|
|
|
|
|
2021-09-10 10:18:19 +00:00
|
|
|
func (a *APIController) OnRefresh() error {
|
|
|
|
// Because we don't know the outpost UUID, we simply do a list and pick the first
|
|
|
|
// The service account this token belongs to should only have access to a single outpost
|
|
|
|
outposts, _, err := a.Client.OutpostsApi.OutpostsInstancesList(context.Background()).Execute()
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Failed to fetch outpost configuration")
|
|
|
|
return err
|
|
|
|
}
|
2021-09-26 09:40:18 +00:00
|
|
|
a.Outpost = outposts.Results[0]
|
2021-09-10 10:18:19 +00:00
|
|
|
|
2021-11-11 22:18:32 +00:00
|
|
|
a.logger.WithField("name", a.Outpost.Name).Debug("Fetched outpost configuration")
|
2021-12-20 20:08:03 +00:00
|
|
|
doGlobalSetup(a.Outpost, a.GlobalConfig)
|
2021-12-22 23:38:49 +00:00
|
|
|
err = a.Server.Refresh()
|
|
|
|
for _, handler := range a.refreshHandlers {
|
|
|
|
handler()
|
|
|
|
}
|
|
|
|
return err
|
2021-09-10 10:18:19 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 15:02:16 +00:00
|
|
|
func (a *APIController) getWebsocketArgs() map[string]interface{} {
|
|
|
|
args := map[string]interface{}{
|
|
|
|
"version": constants.VERSION,
|
|
|
|
"buildHash": constants.BUILD("tagged"),
|
|
|
|
"uuid": a.instanceUUID.String(),
|
|
|
|
}
|
|
|
|
hostname, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
args["hostname"] = hostname
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2022-05-26 13:15:30 +00:00
|
|
|
func (a *APIController) StartBackgroundTasks() error {
|
2021-09-16 10:09:12 +00:00
|
|
|
OutpostInfo.With(prometheus.Labels{
|
|
|
|
"outpost_name": a.Outpost.Name,
|
|
|
|
"outpost_type": a.Server.Type(),
|
|
|
|
"uuid": a.instanceUUID.String(),
|
|
|
|
"version": constants.VERSION,
|
2022-01-14 09:45:37 +00:00
|
|
|
"build": constants.BUILD("tagged"),
|
2021-09-16 10:09:12 +00:00
|
|
|
}).Set(1)
|
2021-01-16 20:41:39 +00:00
|
|
|
go func() {
|
|
|
|
a.logger.Debug("Starting WS Handler...")
|
|
|
|
a.startWSHandler()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
a.logger.Debug("Starting WS Health notifier...")
|
|
|
|
a.startWSHealth()
|
|
|
|
}()
|
2021-05-10 23:07:26 +00:00
|
|
|
go func() {
|
|
|
|
a.logger.Debug("Starting Interval updater...")
|
|
|
|
a.startIntervalUpdater()
|
|
|
|
}()
|
2021-08-21 14:17:30 +00:00
|
|
|
go func() {
|
|
|
|
a.logger.Debug("Starting periodical timer...")
|
|
|
|
a.startPeriodicalTasks()
|
|
|
|
}()
|
2021-01-16 20:41:39 +00:00
|
|
|
return nil
|
|
|
|
}
|