mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-06-05 22:44:17 +00:00
1b49e133da
* Fix bug affecting Azure OIDC authentication ( and most likely others ) * Fixes issue #51 * Ensure that appended roles are unique. Update the documentation. * Improvements targetting possible memory usage spikes. * Additional fixes and cleanup * Refactoring code to fix the issues identified by the users. * Modernize run * Fieldalignment * Multiple changes to improve performance and reduce complexity. - Optimise the errors and recovery. - Deduplicate code in metadata cache. - Remove unused performance monitoring code. - Simplify session management and settings handling. * Fix claims issue. * Add ability to overwrite the default scopes in the settings file * Well.. that escalated quickly. Completely forgot that Traefik uses outdated Yaegi and requires compatibility with 1.20 ( pre-generic Go code ). * Bugfix #51: Ensures that user provided scopes overrides work. * fixup! Bugfix #51: Ensures that user provided scopes overrides work. * fixup! fixup! Bugfix #51: Ensures that user provided scopes overrides work. * Abstract the provider logic into a separate package. * Additional micro fixes and cleanups. * Simplify all the things. * fixup! Simplify all the things. * fixup! fixup! Simplify all the things. * fixup! fixup! fixup! Simplify all the things. * fixup! fixup! fixup! fixup! Simplify all the things. * ... * Cleanup tests. * fixup! Cleanup tests. * fixup! fixup! fixup! Cleanup tests. * fixup! fixup! fixup! fixup! Cleanup tests. * fixup! fixup! fixup! fixup! fixup! Cleanup tests. * Issue #53: Fix CSRF token handling in reverse proxy 1. ✅ HTTPS Detection Fixed (session.go:723) - Now uses X-Forwarded-Proto header instead of r.URL.Scheme - Properly detects HTTPS in reverse proxy environments 2. ✅ SameSite Cookie Attribute Fixed - Removed automatic SameSiteStrictMode for HTTPS (would break OAuth) - Keeps SameSiteLaxMode to allow OAuth callbacks from external domains - Only uses Strict for AJAX requests which don't involve OAuth redirects 3. ✅ Cookie Domain Handling Fixed - Now respects X-Forwarded-Host header for cookie domain - Ensures cookies are set for the public domain, not internal proxy domain 4. ✅ EnhanceSessionSecurity Properly Integrated - Function is now actually called during session save - Applies security enhancements without breaking OAuth flow Why Issue #53 Failed Before: 1. Cookies were not marked Secure in HTTPS environments (browser wouldn't send them back) 2. If they had been Secure with SameSite=Strict, Azure callbacks would still fail 3. Cookie domain might have been wrong (internal vs public domain) Why It Works Now: 1. Cookies are properly marked Secure for HTTPS 2. Uses SameSite=Lax to allow OAuth provider callbacks 3. Cookie domain uses public domain from X-Forwarded-Host 4. CSRF token persists through the entire OAuth flow * Next set of enhancements together with memory usage improvements. * Memory leak fixes and optimisations. * CSRF and Cookie Domain fixes * fixup! CSRF and Cookie Domain fixes * Metadata cache leak fix + profiling * fixup! Metadata cache leak fix + profiling * Memory leaks hunting, part 1337. * Further pursue of perfection. * fixup! Further pursue of perfection. * fixup! fixup! Further pursue of perfection. * fixup! fixup! fixup! Further pursue of perfection. * fixup! fixup! fixup! fixup! Further pursue of perfection. * fixup! fixup! fixup! fixup! fixup! Further pursue of perfection. * fixup! fixup! fixup! fixup! fixup! fixup! Further pursue of perfection. * fixup! fixup! fixup! fixup! fixup! fixup! fixup! Further pursue of perfection. * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Further pursue of perfection. * fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! fixup! Further pursue of perfection. * Clear race conditions * fixup! Clear race conditions * Weekend fun with memory leaks * Splitting code into multiple files with reasonable testing coverage. ``` ok github.com/lukaszraczylo/traefikoidc 117.017s coverage: 72.6% of statements ok github.com/lukaszraczylo/traefikoidc/auth 0.505s coverage: 87.1% of statements ok github.com/lukaszraczylo/traefikoidc/circuit_breaker 0.283s coverage: 99.0% of statements github.com/lukaszraczylo/traefikoidc/config coverage: 0.0% of statements ok github.com/lukaszraczylo/traefikoidc/handlers 0.349s coverage: 98.2% of statements ok github.com/lukaszraczylo/traefikoidc/internal/providers (cached) coverage: 94.3% of statements ok github.com/lukaszraczylo/traefikoidc/middleware 0.808s coverage: 78.0% of statements ok github.com/lukaszraczylo/traefikoidc/recovery 0.653s coverage: 100.0% of statements ok github.com/lukaszraczylo/traefikoidc/session/chunking (cached) coverage: 87.8% of statements ok github.com/lukaszraczylo/traefikoidc/session/core (cached) coverage: 85.6% of statements ok github.com/lukaszraczylo/traefikoidc/session/crypto (cached) coverage: 81.8% of statements ok github.com/lukaszraczylo/traefikoidc/session/storage (cached) coverage: 93.5% of statements ok github.com/lukaszraczylo/traefikoidc/session/validators (cached) coverage: 98.8% of statements ```` * fixup! Splitting code into multiple files with reasonable testing coverage. * fixup! fixup! Splitting code into multiple files with reasonable testing coverage. * Weekend fun with further optimisations. * fixup! Weekend fun with further optimisations. * fixup! fixup! Weekend fun with further optimisations. * fixup! fixup! fixup! Weekend fun with further optimisations. * fixup! fixup! fixup! fixup! Weekend fun with further optimisations. * fixup! fixup! fixup! fixup! fixup! Weekend fun with further optimisations. * Pre-release cleanup. * Enhance test coverage. * fixup! Enhance test coverage. * fixup! fixup! Enhance test coverage. * fixup! fixup! fixup! Enhance test coverage.
236 lines
5.2 KiB
Go
236 lines
5.2 KiB
Go
package traefikoidc
|
|
|
|
import (
|
|
"bytes"
|
|
"compress/gzip"
|
|
"sync"
|
|
)
|
|
|
|
// MemoryOptimizations contains all memory optimization utilities
|
|
type MemoryOptimizations struct {
|
|
bufferPool *BufferPool
|
|
gzipWriterPool *GzipWriterPool
|
|
gzipReaderPool *GzipReaderPool
|
|
loggerSingleton *Logger
|
|
loggerOnce sync.Once
|
|
}
|
|
|
|
var (
|
|
globalMemoryOpts *MemoryOptimizations
|
|
globalMemoryOptsOnce sync.Once
|
|
)
|
|
|
|
// GetMemoryOptimizations returns the global memory optimizations instance
|
|
func GetMemoryOptimizations() *MemoryOptimizations {
|
|
globalMemoryOptsOnce.Do(func() {
|
|
globalMemoryOpts = &MemoryOptimizations{
|
|
bufferPool: NewBufferPool(4096),
|
|
gzipWriterPool: NewGzipWriterPool(),
|
|
gzipReaderPool: NewGzipReaderPool(),
|
|
}
|
|
})
|
|
return globalMemoryOpts
|
|
}
|
|
|
|
// BufferPool manages a pool of byte buffers
|
|
type BufferPool struct {
|
|
pool sync.Pool
|
|
maxSize int
|
|
}
|
|
|
|
// NewBufferPool creates a new buffer pool
|
|
func NewBufferPool(maxSize int) *BufferPool {
|
|
return &BufferPool{
|
|
maxSize: maxSize,
|
|
pool: sync.Pool{
|
|
New: func() interface{} {
|
|
return bytes.NewBuffer(make([]byte, 0, 1024))
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Get retrieves a buffer from the pool
|
|
func (p *BufferPool) Get() *bytes.Buffer {
|
|
buf := p.pool.Get().(*bytes.Buffer)
|
|
buf.Reset()
|
|
return buf
|
|
}
|
|
|
|
// Put returns a buffer to the pool
|
|
func (p *BufferPool) Put(buf *bytes.Buffer) {
|
|
if buf == nil {
|
|
return
|
|
}
|
|
// Only pool if not too large
|
|
if buf.Cap() <= p.maxSize {
|
|
buf.Reset()
|
|
p.pool.Put(buf)
|
|
}
|
|
}
|
|
|
|
// GzipWriterPool manages a pool of gzip writers
|
|
type GzipWriterPool struct {
|
|
pool sync.Pool
|
|
}
|
|
|
|
// NewGzipWriterPool creates a new gzip writer pool
|
|
func NewGzipWriterPool() *GzipWriterPool {
|
|
return &GzipWriterPool{
|
|
pool: sync.Pool{
|
|
New: func() interface{} {
|
|
w, _ := gzip.NewWriterLevel(nil, gzip.BestSpeed)
|
|
return w
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Get retrieves a gzip writer from the pool
|
|
func (p *GzipWriterPool) Get() *gzip.Writer {
|
|
return p.pool.Get().(*gzip.Writer)
|
|
}
|
|
|
|
// Put returns a gzip writer to the pool
|
|
func (p *GzipWriterPool) Put(w *gzip.Writer) {
|
|
if w != nil {
|
|
w.Reset(nil)
|
|
p.pool.Put(w)
|
|
}
|
|
}
|
|
|
|
// GzipReaderPool manages a pool of gzip readers
|
|
type GzipReaderPool struct {
|
|
pool sync.Pool
|
|
}
|
|
|
|
// NewGzipReaderPool creates a new gzip reader pool
|
|
func NewGzipReaderPool() *GzipReaderPool {
|
|
return &GzipReaderPool{
|
|
pool: sync.Pool{
|
|
New: func() interface{} {
|
|
// Return nil, readers will be created as needed
|
|
return (*gzip.Reader)(nil)
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// Get retrieves a gzip reader from the pool
|
|
func (p *GzipReaderPool) Get() *gzip.Reader {
|
|
r := p.pool.Get()
|
|
if r == nil {
|
|
return nil
|
|
}
|
|
return r.(*gzip.Reader)
|
|
}
|
|
|
|
// Put returns a gzip reader to the pool
|
|
func (p *GzipReaderPool) Put(r *gzip.Reader) {
|
|
if r != nil {
|
|
r.Reset(nil)
|
|
p.pool.Put(r)
|
|
}
|
|
}
|
|
|
|
// GetSingletonLogger returns a singleton logger instance
|
|
func (m *MemoryOptimizations) GetSingletonLogger(level string) *Logger {
|
|
m.loggerOnce.Do(func() {
|
|
m.loggerSingleton = NewLogger(level)
|
|
})
|
|
return m.loggerSingleton
|
|
}
|
|
|
|
// CompressTokenOptimized compresses a token using pooled resources
|
|
func CompressTokenOptimized(token string) (string, error) {
|
|
opts := GetMemoryOptimizations()
|
|
|
|
buf := opts.bufferPool.Get()
|
|
defer opts.bufferPool.Put(buf)
|
|
|
|
gzipWriter := opts.gzipWriterPool.Get()
|
|
defer opts.gzipWriterPool.Put(gzipWriter)
|
|
|
|
gzipWriter.Reset(buf)
|
|
|
|
if _, err := gzipWriter.Write([]byte(token)); err != nil {
|
|
return token, err
|
|
}
|
|
|
|
if err := gzipWriter.Close(); err != nil {
|
|
return token, err
|
|
}
|
|
|
|
compressed := buf.Bytes()
|
|
|
|
// Only use compression if it's beneficial
|
|
if len(compressed) < len(token) {
|
|
return string(compressed), nil
|
|
}
|
|
|
|
return token, nil
|
|
}
|
|
|
|
// DecompressTokenOptimized decompresses a token using pooled resources
|
|
func DecompressTokenOptimized(compressed string) (string, error) {
|
|
opts := GetMemoryOptimizations()
|
|
|
|
buf := bytes.NewReader([]byte(compressed))
|
|
|
|
gzipReader, err := gzip.NewReader(buf)
|
|
if err != nil {
|
|
return compressed, err
|
|
}
|
|
defer gzipReader.Close()
|
|
|
|
outputBuf := opts.bufferPool.Get()
|
|
defer opts.bufferPool.Put(outputBuf)
|
|
|
|
if _, err := outputBuf.ReadFrom(gzipReader); err != nil {
|
|
return compressed, err
|
|
}
|
|
|
|
return outputBuf.String(), nil
|
|
}
|
|
|
|
// SimplifiedSessionData represents a simplified session structure with fewer references
|
|
type SimplifiedSessionData struct {
|
|
mainData map[string]interface{}
|
|
tokens map[string]string
|
|
chunks map[string][]string
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
// NewSimplifiedSessionData creates a new simplified session data structure
|
|
func NewSimplifiedSessionData() *SimplifiedSessionData {
|
|
return &SimplifiedSessionData{
|
|
mainData: make(map[string]interface{}),
|
|
tokens: make(map[string]string),
|
|
chunks: make(map[string][]string),
|
|
}
|
|
}
|
|
|
|
// SetToken sets a token value
|
|
func (s *SimplifiedSessionData) SetToken(name, value string) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.tokens[name] = value
|
|
}
|
|
|
|
// GetToken gets a token value
|
|
func (s *SimplifiedSessionData) GetToken(name string) (string, bool) {
|
|
s.mu.RLock()
|
|
defer s.mu.RUnlock()
|
|
val, exists := s.tokens[name]
|
|
return val, exists
|
|
}
|
|
|
|
// Clear clears all session data
|
|
func (s *SimplifiedSessionData) Clear() {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.mainData = make(map[string]interface{})
|
|
s.tokens = make(map[string]string)
|
|
s.chunks = make(map[string][]string)
|
|
}
|