2021-01-16 20:41:39 +00:00
|
|
|
package ak
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-openapi/runtime"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/recws-org/recws"
|
2021-01-16 20:45:24 +00:00
|
|
|
"goauthentik.io/outpost/pkg"
|
|
|
|
"goauthentik.io/outpost/pkg/client"
|
|
|
|
"goauthentik.io/outpost/pkg/client/outposts"
|
2021-01-16 20:41:39 +00:00
|
|
|
|
|
|
|
httptransport "github.com/go-openapi/runtime/client"
|
|
|
|
"github.com/go-openapi/strfmt"
|
|
|
|
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 {
|
|
|
|
Client *client.Authentik
|
|
|
|
Auth runtime.ClientAuthInfoWriter
|
|
|
|
token string
|
|
|
|
|
|
|
|
Server Outpost
|
|
|
|
|
|
|
|
lastBundleHash string
|
|
|
|
logger *log.Entry
|
|
|
|
|
|
|
|
reloadOffset time.Duration
|
|
|
|
|
|
|
|
wsConn *recws.RecConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAPIController initialise new API Controller instance from URL and API token
|
|
|
|
func NewAPIController(pbURL url.URL, token string) *APIController {
|
|
|
|
transport := httptransport.New(pbURL.Host, client.DefaultBasePath, []string{pbURL.Scheme})
|
|
|
|
transport.Transport = SetUserAgent(getTLSTransport(), fmt.Sprintf("authentik-proxy@%s", pkg.VERSION))
|
|
|
|
|
|
|
|
// create the transport
|
|
|
|
auth := httptransport.BasicAuth("", token)
|
|
|
|
|
|
|
|
// create the API client, with the transport
|
|
|
|
apiClient := client.New(transport, strfmt.Default)
|
|
|
|
|
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
|
|
|
|
outposts, err := apiClient.Outposts.OutpostsOutpostsList(outposts.NewOutpostsOutpostsListParams(), auth)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-02-11 22:48:54 +00:00
|
|
|
log.WithError(err).Panic("Failed to fetch configuration")
|
2021-01-16 20:41:39 +00:00
|
|
|
}
|
|
|
|
outpost := outposts.Payload.Results[0]
|
|
|
|
doGlobalSetup(outpost.Config.(map[string]interface{}))
|
|
|
|
|
|
|
|
ac := &APIController{
|
|
|
|
Client: apiClient,
|
|
|
|
Auth: auth,
|
|
|
|
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,
|
|
|
|
|
|
|
|
lastBundleHash: "",
|
|
|
|
}
|
|
|
|
ac.logger.Debugf("HA Reload offset: %s", ac.reloadOffset)
|
|
|
|
ac.initWS(pbURL, outpost.Pk)
|
|
|
|
return ac
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *APIController) GetLastBundleHash() string {
|
|
|
|
return a.lastBundleHash
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
a.Server.Start()
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|