Compare commits
9 Commits
v2024.11.8
...
v2025.3.10
Author | SHA1 | Date | |
---|---|---|---|
6a4a144c97 | |||
ac7b7e8189 | |||
2df74bad4f | |||
076a3d784e | |||
826edef358 | |||
ce7415af20 | |||
7cc9de180c | |||
74c2a2c055 | |||
239d4573c3 |
@ -17,9 +17,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func RunCommand() *cli.Command {
|
func RunCommand() *cli.Command {
|
||||||
flags := append(
|
flags := common.Flags()
|
||||||
common.Flags(),
|
|
||||||
)
|
|
||||||
|
|
||||||
return &cli.Command{
|
return &cli.Command{
|
||||||
Name: "run",
|
Name: "run",
|
||||||
|
@ -20,6 +20,10 @@ func NewDefaultLayersConfig() LayersConfig {
|
|||||||
TransportConfig: NewDefaultTransportConfig(),
|
TransportConfig: NewDefaultTransportConfig(),
|
||||||
Timeout: NewInterpolatedDuration(10 * time.Second),
|
Timeout: NewInterpolatedDuration(10 * time.Second),
|
||||||
},
|
},
|
||||||
|
ProviderCacheTimeout: NewInterpolatedDuration(time.Hour),
|
||||||
|
},
|
||||||
|
Sessions: AuthnLayerSessionConfig{
|
||||||
|
TTL: NewInterpolatedDuration(time.Hour),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -34,10 +38,16 @@ type AuthnLayerConfig struct {
|
|||||||
Debug InterpolatedBool `yaml:"debug"`
|
Debug InterpolatedBool `yaml:"debug"`
|
||||||
TemplateDir InterpolatedString `yaml:"templateDir"`
|
TemplateDir InterpolatedString `yaml:"templateDir"`
|
||||||
OIDC AuthnOIDCLayerConfig `yaml:"oidc"`
|
OIDC AuthnOIDCLayerConfig `yaml:"oidc"`
|
||||||
|
Sessions AuthnLayerSessionConfig `yaml:"sessions"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type AuthnLayerSessionConfig struct {
|
||||||
|
TTL *InterpolatedDuration `yaml:"ttl"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthnOIDCLayerConfig struct {
|
type AuthnOIDCLayerConfig struct {
|
||||||
HTTPClient AuthnOIDCHTTPClientConfig `yaml:"httpClient"`
|
HTTPClient AuthnOIDCHTTPClientConfig `yaml:"httpClient"`
|
||||||
|
ProviderCacheTimeout *InterpolatedDuration `yaml:"providerCacheTimeout"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthnOIDCHTTPClientConfig struct {
|
type AuthnOIDCHTTPClientConfig struct {
|
||||||
|
@ -32,7 +32,7 @@ func NewDefaultSentryConfig() SentryConfig {
|
|||||||
EnableTracing: false,
|
EnableTracing: false,
|
||||||
TracesSampleRate: 0.1,
|
TracesSampleRate: 0.1,
|
||||||
ProfilesSampleRate: 0.1,
|
ProfilesSampleRate: 0.1,
|
||||||
IgnoreErrors: []string{"context canceled"},
|
IgnoreErrors: []string{"context canceled", "net/http: abort"},
|
||||||
SendDefaultPII: false,
|
SendDefaultPII: false,
|
||||||
ServerName: "",
|
ServerName: "",
|
||||||
Environment: "",
|
Environment: "",
|
||||||
|
@ -13,6 +13,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
"forge.cadoles.com/Cadoles/go-proxy/wildcard"
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/cache"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/authn"
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/authn"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||||
@ -27,6 +28,7 @@ type Authenticator struct {
|
|||||||
store sessions.Store
|
store sessions.Store
|
||||||
httpTransport *http.Transport
|
httpTransport *http.Transport
|
||||||
httpClientTimeout time.Duration
|
httpClientTimeout time.Duration
|
||||||
|
oidcProviderCache cache.Cache[string, *oidc.Provider]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Authenticator) PreAuthentication(w http.ResponseWriter, r *http.Request, layer *store.Layer) error {
|
func (a *Authenticator) PreAuthentication(w http.ResponseWriter, r *http.Request, layer *store.Layer) error {
|
||||||
@ -378,17 +380,25 @@ func (a *Authenticator) getClient(options *LayerOptions, redirectURL string) (*C
|
|||||||
Transport: transport,
|
Transport: transport,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
provider, exists := a.oidcProviderCache.Get(options.OIDC.IssuerURL)
|
||||||
|
if !exists {
|
||||||
|
var err error
|
||||||
ctx = oidc.ClientContext(ctx, httpClient)
|
ctx = oidc.ClientContext(ctx, httpClient)
|
||||||
|
|
||||||
if options.OIDC.SkipIssuerVerification {
|
if options.OIDC.SkipIssuerVerification {
|
||||||
ctx = oidc.InsecureIssuerURLContext(ctx, options.OIDC.IssuerURL)
|
ctx = oidc.InsecureIssuerURLContext(ctx, options.OIDC.IssuerURL)
|
||||||
}
|
}
|
||||||
|
|
||||||
provider, err := oidc.NewProvider(ctx, options.OIDC.IssuerURL)
|
logger.Debug(ctx, "refreshing oidc provider", logger.F("issuerURL", options.OIDC.IssuerURL))
|
||||||
|
|
||||||
|
provider, err = oidc.NewProvider(ctx, options.OIDC.IssuerURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "could not create oidc provider")
|
return nil, errors.Wrap(err, "could not create oidc provider")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
a.oidcProviderCache.Set(options.OIDC.IssuerURL, provider)
|
||||||
|
}
|
||||||
|
|
||||||
client := NewClient(
|
client := NewClient(
|
||||||
WithCredentials(options.OIDC.ClientID, options.OIDC.ClientSecret),
|
WithCredentials(options.OIDC.ClientID, options.OIDC.ClientSecret),
|
||||||
WithProvider(provider),
|
WithProvider(provider),
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package oidc
|
package oidc
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/cache/memory"
|
||||||
|
"forge.cadoles.com/cadoles/bouncer/internal/cache/ttl"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/authn"
|
"forge.cadoles.com/cadoles/bouncer/internal/proxy/director/layer/authn"
|
||||||
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
"forge.cadoles.com/cadoles/bouncer/internal/store"
|
||||||
|
"github.com/coreos/go-oidc/v3/oidc"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -14,5 +19,10 @@ func NewLayer(store sessions.Store, funcs ...OptionFunc) *authn.Layer {
|
|||||||
httpTransport: opts.HTTPTransport,
|
httpTransport: opts.HTTPTransport,
|
||||||
httpClientTimeout: opts.HTTPClientTimeout,
|
httpClientTimeout: opts.HTTPClientTimeout,
|
||||||
store: store,
|
store: store,
|
||||||
|
oidcProviderCache: ttl.NewCache(
|
||||||
|
memory.NewCache[string, *oidc.Provider](),
|
||||||
|
memory.NewCache[string, time.Time](),
|
||||||
|
opts.OIDCProviderCacheTimeout,
|
||||||
|
),
|
||||||
}, opts.AuthnOptions...)
|
}, opts.AuthnOptions...)
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ type Options struct {
|
|||||||
HTTPTransport *http.Transport
|
HTTPTransport *http.Transport
|
||||||
HTTPClientTimeout time.Duration
|
HTTPClientTimeout time.Duration
|
||||||
AuthnOptions []authn.OptionFunc
|
AuthnOptions []authn.OptionFunc
|
||||||
|
OIDCProviderCacheTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type OptionFunc func(opts *Options)
|
type OptionFunc func(opts *Options)
|
||||||
@ -33,11 +34,18 @@ func WithAuthnOptions(funcs ...authn.OptionFunc) OptionFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithOIDCProviderCacheTimeout(timeout time.Duration) OptionFunc {
|
||||||
|
return func(opts *Options) {
|
||||||
|
opts.OIDCProviderCacheTimeout = timeout
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func NewOptions(funcs ...OptionFunc) *Options {
|
func NewOptions(funcs ...OptionFunc) *Options {
|
||||||
opts := &Options{
|
opts := &Options{
|
||||||
HTTPTransport: http.DefaultTransport.(*http.Transport),
|
HTTPTransport: http.DefaultTransport.(*http.Transport),
|
||||||
HTTPClientTimeout: 30 * time.Second,
|
HTTPClientTimeout: 30 * time.Second,
|
||||||
AuthnOptions: make([]authn.OptionFunc, 0),
|
AuthnOptions: make([]authn.OptionFunc, 0),
|
||||||
|
OIDCProviderCacheTimeout: time.Hour,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, fn := range funcs {
|
for _, fn := range funcs {
|
||||||
|
@ -233,9 +233,7 @@ func (s *Server) handleDefault(w http.ResponseWriter, r *http.Request) {
|
|||||||
func (s *Server) handleError(w http.ResponseWriter, r *http.Request, status int, err error) {
|
func (s *Server) handleError(w http.ResponseWriter, r *http.Request, status int, err error) {
|
||||||
err = errors.WithStack(err)
|
err = errors.WithStack(err)
|
||||||
|
|
||||||
if errors.Is(err, context.Canceled) {
|
if !errors.Is(err, context.Canceled) {
|
||||||
logger.Warn(r.Context(), err.Error(), logger.E(err))
|
|
||||||
} else {
|
|
||||||
logger.Error(r.Context(), err.Error(), logger.CapturedE(err))
|
logger.Error(r.Context(), err.Error(), logger.CapturedE(err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
type Options struct {
|
type Options struct {
|
||||||
Session sessions.Options
|
Session sessions.Options
|
||||||
KeyPrefix string
|
KeyPrefix string
|
||||||
|
TTL time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type OptionFunc func(opts *Options)
|
type OptionFunc func(opts *Options)
|
||||||
@ -25,6 +26,7 @@ func NewOptions(funcs ...OptionFunc) *Options {
|
|||||||
SameSite: http.SameSiteDefaultMode,
|
SameSite: http.SameSiteDefaultMode,
|
||||||
},
|
},
|
||||||
KeyPrefix: "session:",
|
KeyPrefix: "session:",
|
||||||
|
TTL: time.Hour,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, fn := range funcs {
|
for _, fn := range funcs {
|
||||||
@ -45,3 +47,9 @@ func WithKeyPrefix(prefix string) OptionFunc {
|
|||||||
opts.KeyPrefix = prefix
|
opts.KeyPrefix = prefix
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WithTTL(ttl time.Duration) OptionFunc {
|
||||||
|
return func(opts *Options) {
|
||||||
|
opts.TTL = ttl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -31,6 +31,7 @@ type Store struct {
|
|||||||
keyPrefix string
|
keyPrefix string
|
||||||
keyGen KeyGenFunc
|
keyGen KeyGenFunc
|
||||||
serializer SessionSerializer
|
serializer SessionSerializer
|
||||||
|
ttl time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
type KeyGenFunc func() (string, error)
|
type KeyGenFunc func() (string, error)
|
||||||
@ -43,6 +44,7 @@ func NewStore(adapter StoreAdapter, funcs ...OptionFunc) *Store {
|
|||||||
keyPrefix: opts.KeyPrefix,
|
keyPrefix: opts.KeyPrefix,
|
||||||
keyGen: generateRandomKey,
|
keyGen: generateRandomKey,
|
||||||
serializer: GobSerializer{},
|
serializer: GobSerializer{},
|
||||||
|
ttl: opts.TTL,
|
||||||
}
|
}
|
||||||
|
|
||||||
return rs
|
return rs
|
||||||
@ -62,20 +64,21 @@ func (s *Store) New(r *http.Request, name string) (*sessions.Session, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
session.ID = c.Value
|
session.ID = c.Value
|
||||||
|
|
||||||
err = s.load(r.Context(), session)
|
err = s.load(r.Context(), session)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
session.IsNew = false
|
session.IsNew = false
|
||||||
} else if !errors.Is(err, ErrNotFound) {
|
} else if !errors.Is(err, ErrNotFound) {
|
||||||
return nil, errors.WithStack(err)
|
return session, errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Store) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
|
func (s *Store) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
|
||||||
if session.Options.MaxAge <= 0 {
|
if session.Options.MaxAge < 0 {
|
||||||
if err := s.delete(r.Context(), session); err != nil {
|
if err := s.delete(r.Context(), session); err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
@ -120,7 +123,12 @@ func (s *Store) save(ctx context.Context, session *sessions.Session) error {
|
|||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := s.adapter.Set(ctx, s.keyPrefix+session.ID, b, time.Duration(session.Options.MaxAge)*time.Second); err != nil {
|
ttl := time.Duration(session.Options.MaxAge) * time.Second
|
||||||
|
if s.ttl < ttl || ttl == 0 {
|
||||||
|
ttl = s.ttl
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := s.adapter.Set(ctx, s.keyPrefix+session.ID, b, ttl); err != nil {
|
||||||
return errors.WithStack(err)
|
return errors.WithStack(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,5 +37,6 @@ func setupAuthnOIDCLayer(conf *config.Config) (director.Layer, error) {
|
|||||||
authn.WithTemplateDir(string(conf.Layers.Authn.TemplateDir)),
|
authn.WithTemplateDir(string(conf.Layers.Authn.TemplateDir)),
|
||||||
authn.WithDebug(bool(conf.Layers.Authn.Debug)),
|
authn.WithDebug(bool(conf.Layers.Authn.Debug)),
|
||||||
),
|
),
|
||||||
|
oidc.WithOIDCProviderCacheTimeout(time.Duration(*conf.Layers.Authn.OIDC.ProviderCacheTimeout)),
|
||||||
), nil
|
), nil
|
||||||
}
|
}
|
||||||
|
@ -218,6 +218,11 @@ layers:
|
|||||||
authn:
|
authn:
|
||||||
# Répertoire contenant les templates
|
# Répertoire contenant les templates
|
||||||
templateDir: "/etc/bouncer/layers/authn/templates"
|
templateDir: "/etc/bouncer/layers/authn/templates"
|
||||||
|
# Configuration des sessions
|
||||||
|
sessions:
|
||||||
|
# Temps de persistence sans actualisation des sessions dans le store
|
||||||
|
# (prévalent sur le MaxAge de la session)
|
||||||
|
ttl: "1h"
|
||||||
|
|
||||||
# Configuration d'une série de proxy/layers
|
# Configuration d'une série de proxy/layers
|
||||||
# à créer par défaut par le serveur d'administration
|
# à créer par défaut par le serveur d'administration
|
||||||
|
Reference in New Issue
Block a user