2021-09-08 18:04:56 +00:00
|
|
|
package proxyv2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
2021-12-12 12:46:31 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-09-08 18:04:56 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
|
|
|
"goauthentik.io/internal/outpost/ak"
|
|
|
|
"goauthentik.io/internal/outpost/proxyv2/application"
|
2022-06-20 09:54:10 +00:00
|
|
|
"goauthentik.io/internal/utils/web"
|
2023-02-12 15:34:57 +00:00
|
|
|
"golang.org/x/exp/maps"
|
2021-09-08 18:04:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (ps *ProxyServer) Refresh() error {
|
|
|
|
providers, _, err := ps.akAPI.Client.OutpostsApi.OutpostsProxyList(context.Background()).Execute()
|
|
|
|
if err != nil {
|
|
|
|
ps.log.WithError(err).Error("Failed to fetch providers")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
apps := make(map[string]*application.Application)
|
|
|
|
for _, provider := range providers.Results {
|
2021-12-12 12:46:31 +00:00
|
|
|
rsp := sentry.StartSpan(context.Background(), "authentik.outposts.proxy.application_ss")
|
2021-09-08 18:04:56 +00:00
|
|
|
ua := fmt.Sprintf(" (provider=%s)", provider.Name)
|
|
|
|
hc := &http.Client{
|
2023-01-14 21:12:48 +00:00
|
|
|
Transport: web.NewUserAgentTransport(
|
|
|
|
constants.OutpostUserAgent()+ua,
|
|
|
|
web.NewTracingTransport(
|
|
|
|
rsp.Context(),
|
|
|
|
ak.GetTLSTransport(),
|
|
|
|
),
|
|
|
|
),
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
2023-02-12 15:34:57 +00:00
|
|
|
a, err := application.NewApplication(provider, hc, ps)
|
2023-01-14 21:12:48 +00:00
|
|
|
existing, ok := apps[a.Host]
|
|
|
|
if ok {
|
|
|
|
existing.Stop()
|
|
|
|
}
|
2021-10-13 19:48:11 +00:00
|
|
|
if err != nil {
|
|
|
|
ps.log.WithError(err).Warning("failed to setup application")
|
|
|
|
} else {
|
|
|
|
apps[a.Host] = a
|
|
|
|
}
|
2021-09-08 18:04:56 +00:00
|
|
|
}
|
|
|
|
ps.apps = apps
|
|
|
|
ps.log.Debug("Swapped maps")
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-12 15:34:57 +00:00
|
|
|
|
|
|
|
func (ps *ProxyServer) API() *ak.APIController {
|
|
|
|
return ps.akAPI
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *ProxyServer) CryptoStore() *ak.CryptoStore {
|
|
|
|
return ps.cryptoStore
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ps *ProxyServer) Apps() []*application.Application {
|
|
|
|
return maps.Values(ps.apps)
|
|
|
|
}
|