This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
authentik/internal/outpost/proxyv2/refresh.go
Jens Langhammer 0ddcefce80
outposts/proxy: cache basic and bearer credentials for one minute
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
2023-01-14 22:12:48 +01:00

51 lines
1.2 KiB
Go

package proxyv2
import (
"context"
"fmt"
"net/http"
"github.com/getsentry/sentry-go"
"goauthentik.io/internal/constants"
"goauthentik.io/internal/outpost/ak"
"goauthentik.io/internal/outpost/proxyv2/application"
"goauthentik.io/internal/utils/web"
)
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 {
rsp := sentry.StartSpan(context.Background(), "authentik.outposts.proxy.application_ss")
ua := fmt.Sprintf(" (provider=%s)", provider.Name)
hc := &http.Client{
Transport: web.NewUserAgentTransport(
constants.OutpostUserAgent()+ua,
web.NewTracingTransport(
rsp.Context(),
ak.GetTLSTransport(),
),
),
}
a, err := application.NewApplication(provider, hc, ps.cryptoStore, ps.akAPI)
existing, ok := apps[a.Host]
if ok {
existing.Stop()
}
if err != nil {
ps.log.WithError(err).Warning("failed to setup application")
} else {
apps[a.Host] = a
}
}
ps.apps = apps
ps.log.Debug("Swapped maps")
return nil
}