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"
|
|
|
|
"time"
|
|
|
|
|
2021-05-16 19:07:01 +00:00
|
|
|
"github.com/go-openapi/strfmt"
|
2021-05-11 19:46:30 +00:00
|
|
|
"github.com/google/uuid"
|
2021-01-16 20:41:39 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/recws-org/recws"
|
2021-06-16 10:35:17 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-05-16 19:07:01 +00:00
|
|
|
"goauthentik.io/outpost/api"
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ConfigLogLevel = "log_level"
|
|
|
|
const ConfigErrorReportingEnabled = "error_reporting_enabled"
|
|
|
|
const ConfigErrorReportingEnvironment = "error_reporting_environment"
|
|
|
|
|
|
|
|
// APIController main controller which connects to the authentik api via http and ws
|
|
|
|
type APIController struct {
|
2021-05-16 19:07:01 +00:00
|
|
|
Client *api.APIClient
|
2021-01-16 20:41:39 +00:00
|
|
|
token string
|
|
|
|
|
|
|
|
Server Outpost
|
|
|
|
|
2021-04-23 08:08:19 +00:00
|
|
|
logger *log.Entry
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
reloadOffset time.Duration
|
|
|
|
|
2021-05-11 19:46:30 +00:00
|
|
|
wsConn *recws.RecConn
|
|
|
|
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-05-16 19:07:01 +00:00
|
|
|
config := api.NewConfiguration()
|
|
|
|
config.Host = akURL.Host
|
|
|
|
config.Scheme = akURL.Scheme
|
|
|
|
config.HTTPClient = &http.Client{
|
2021-06-16 10:35:17 +00:00
|
|
|
Transport: SetUserAgent(GetTLSTransport(), constants.OutpostUserAgent()),
|
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 {
|
2021-04-19 16:56:36 +00:00
|
|
|
log.WithError(err).Error("Failed to fetch configuration")
|
2021-06-23 18:40:51 +00:00
|
|
|
return nil
|
2021-01-16 20:41:39 +00:00
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
outpost := outposts.Results[0]
|
|
|
|
doGlobalSetup(outpost.Config)
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
ac := &APIController{
|
|
|
|
Client: apiClient,
|
|
|
|
token: token,
|
|
|
|
|
2021-02-11 22:48:54 +00:00
|
|
|
logger: log,
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
reloadOffset: time.Duration(rand.Intn(10)) * time.Second,
|
2021-05-11 19:46:30 +00:00
|
|
|
instanceUUID: uuid.New(),
|
2021-01-16 20:41:39 +00:00
|
|
|
}
|
|
|
|
ac.logger.Debugf("HA Reload offset: %s", ac.reloadOffset)
|
2021-05-16 19:07:01 +00:00
|
|
|
ac.initWS(akURL, strfmt.UUID(outpost.Pk))
|
2021-01-16 20:41:39 +00:00
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start Starts all handlers, non-blocking
|
|
|
|
func (a *APIController) Start() error {
|
|
|
|
err := a.Server.Refresh()
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to run initial refresh")
|
|
|
|
}
|
|
|
|
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-01-16 20:41:39 +00:00
|
|
|
go func() {
|
2021-04-19 18:43:13 +00:00
|
|
|
err := a.Server.Start()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-01-16 20:41:39 +00:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|