mirror of
https://github.com/lukaszraczylo/traefikoidc.git
synced 2026-10-13 04:07:37 +00:00
Add redis support for distributed caching (#83)
* Add redis support for distributed caching * Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi. * fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi. * fixup! fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi. * fixup! fixup! fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi. * fixup! fixup! fixup! fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi. * fixup! fixup! fixup! fixup! fixup! Move towards the self-provided Redis connection pool and RESP protocol implementation. Official redis client library won't work with yaegi. * ... and another all nighter. * fixup! ... and another all nighter. * fixup! fixup! ... and another all nighter. * fixup! fixup! fixup! ... and another all nighter. * Resolve issue #85 by adding ability to set custom claims in JWT tokens * Remove redundant validation in auth middleware ( issue #89 ) * Add ability to set cookie prefix for session cookies ( #87 ) * fixup! Add ability to set cookie prefix for session cookies ( #87 ) * Add ability to set cookie max age - issue #91 * Potential fix for code scanning alert no. 10: Size computation for allocation may overflow Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> * fixup! Merge main into 0.8.0-redis: resolve conflicts --------- Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This commit is contained in:
co-authored by
Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
parent
5fcbd54955
commit
e64fc7f730
@@ -0,0 +1,91 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/lukaszraczylo/traefikoidc/internal/cleanup"
|
||||
"github.com/lukaszraczylo/traefikoidc/internal/recovery"
|
||||
)
|
||||
|
||||
// LoggerInterface defines the common logger interface used across the package
|
||||
type LoggerInterface interface {
|
||||
Infof(format string, args ...interface{})
|
||||
Debugf(format string, args ...interface{})
|
||||
Errorf(format string, args ...interface{})
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// RECOVERY LOGGER WRAPPER
|
||||
// ============================================================================
|
||||
|
||||
// recoveryLoggerWrapper wraps a logger to match recovery.Logger interface
|
||||
type recoveryLoggerWrapper struct {
|
||||
logger LoggerInterface
|
||||
}
|
||||
|
||||
// WrapLoggerForRecovery wraps a logger for use with recovery modules
|
||||
func WrapLoggerForRecovery(logger LoggerInterface) recovery.Logger {
|
||||
return &recoveryLoggerWrapper{logger: logger}
|
||||
}
|
||||
|
||||
// Logf logs an informational message
|
||||
func (lw *recoveryLoggerWrapper) Logf(format string, args ...interface{}) {
|
||||
if lw.logger != nil {
|
||||
lw.logger.Infof(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// ErrorLogf logs an error message
|
||||
func (lw *recoveryLoggerWrapper) ErrorLogf(format string, args ...interface{}) {
|
||||
if lw.logger != nil {
|
||||
lw.logger.Errorf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// DebugLogf logs a debug message
|
||||
func (lw *recoveryLoggerWrapper) DebugLogf(format string, args ...interface{}) {
|
||||
if lw.logger != nil {
|
||||
lw.logger.Debugf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// CLEANUP LOGGER WRAPPER
|
||||
// ============================================================================
|
||||
|
||||
// cleanupLoggerWrapper wraps a logger to match cleanup.Logger interface
|
||||
type cleanupLoggerWrapper struct {
|
||||
logger LoggerInterface
|
||||
}
|
||||
|
||||
// WrapLoggerForCleanup wraps a logger for use with cleanup modules
|
||||
func WrapLoggerForCleanup(logger LoggerInterface) cleanup.Logger {
|
||||
return &cleanupLoggerWrapper{logger: logger}
|
||||
}
|
||||
|
||||
// Logf logs an informational message
|
||||
func (lw *cleanupLoggerWrapper) Logf(format string, args ...interface{}) {
|
||||
if lw.logger != nil {
|
||||
lw.logger.Infof(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// ErrorLogf logs an error message
|
||||
func (lw *cleanupLoggerWrapper) ErrorLogf(format string, args ...interface{}) {
|
||||
if lw.logger != nil {
|
||||
lw.logger.Errorf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// DebugLogf logs a debug message
|
||||
func (lw *cleanupLoggerWrapper) DebugLogf(format string, args ...interface{}) {
|
||||
if lw.logger != nil {
|
||||
lw.logger.Debugf(format, args...)
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// SESSION LOGGER WRAPPER
|
||||
// ============================================================================
|
||||
|
||||
// Note: Session logger wrapper is not included here because session.Logger
|
||||
// has a different interface (Debug/Info/Warn/Error instead of Logf/ErrorLogf/DebugLogf).
|
||||
// Each package should implement its own session logger adapter as needed.
|
||||
@@ -553,3 +553,164 @@ func TestIsTestModeYaegiCompiler(t *testing.T) {
|
||||
t.Error("Expected true when GO_TEST=1 is set")
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// LOGGER WRAPPER TESTS
|
||||
// ============================================================================
|
||||
|
||||
// mockLogger is a simple mock implementation for testing
|
||||
type mockLogger struct {
|
||||
infoCalls int
|
||||
debugCalls int
|
||||
errorCalls int
|
||||
lastFormat string
|
||||
lastArgs []interface{}
|
||||
}
|
||||
|
||||
func (m *mockLogger) Infof(format string, args ...interface{}) {
|
||||
m.infoCalls++
|
||||
m.lastFormat = format
|
||||
m.lastArgs = args
|
||||
}
|
||||
|
||||
func (m *mockLogger) Debugf(format string, args ...interface{}) {
|
||||
m.debugCalls++
|
||||
m.lastFormat = format
|
||||
m.lastArgs = args
|
||||
}
|
||||
|
||||
func (m *mockLogger) Errorf(format string, args ...interface{}) {
|
||||
m.errorCalls++
|
||||
m.lastFormat = format
|
||||
m.lastArgs = args
|
||||
}
|
||||
|
||||
// TestWrapLoggerForRecovery tests the recovery logger wrapper
|
||||
func TestWrapLoggerForRecovery(t *testing.T) {
|
||||
mock := &mockLogger{}
|
||||
wrapper := WrapLoggerForRecovery(mock)
|
||||
|
||||
if wrapper == nil {
|
||||
t.Fatal("WrapLoggerForRecovery should not return nil")
|
||||
}
|
||||
|
||||
// Test Logf
|
||||
wrapper.Logf("test info: %s", "value")
|
||||
if mock.infoCalls != 1 {
|
||||
t.Errorf("Expected 1 info call, got %d", mock.infoCalls)
|
||||
}
|
||||
if mock.lastFormat != "test info: %s" {
|
||||
t.Errorf("Expected format 'test info: %%s', got '%s'", mock.lastFormat)
|
||||
}
|
||||
|
||||
// Test ErrorLogf
|
||||
wrapper.ErrorLogf("test error: %d", 123)
|
||||
if mock.errorCalls != 1 {
|
||||
t.Errorf("Expected 1 error call, got %d", mock.errorCalls)
|
||||
}
|
||||
if mock.lastFormat != "test error: %d" {
|
||||
t.Errorf("Expected format 'test error: %%d', got '%s'", mock.lastFormat)
|
||||
}
|
||||
|
||||
// Test DebugLogf
|
||||
wrapper.DebugLogf("test debug: %v", true)
|
||||
if mock.debugCalls != 1 {
|
||||
t.Errorf("Expected 1 debug call, got %d", mock.debugCalls)
|
||||
}
|
||||
if mock.lastFormat != "test debug: %v" {
|
||||
t.Errorf("Expected format 'test debug: %%v', got '%s'", mock.lastFormat)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWrapLoggerForRecovery_NilLogger tests recovery wrapper with nil logger
|
||||
func TestWrapLoggerForRecovery_NilLogger(t *testing.T) {
|
||||
wrapper := WrapLoggerForRecovery(nil)
|
||||
|
||||
if wrapper == nil {
|
||||
t.Fatal("WrapLoggerForRecovery should not return nil even with nil logger")
|
||||
}
|
||||
|
||||
// These should not panic
|
||||
wrapper.Logf("test")
|
||||
wrapper.ErrorLogf("test")
|
||||
wrapper.DebugLogf("test")
|
||||
}
|
||||
|
||||
// TestWrapLoggerForCleanup tests the cleanup logger wrapper
|
||||
func TestWrapLoggerForCleanup(t *testing.T) {
|
||||
mock := &mockLogger{}
|
||||
wrapper := WrapLoggerForCleanup(mock)
|
||||
|
||||
if wrapper == nil {
|
||||
t.Fatal("WrapLoggerForCleanup should not return nil")
|
||||
}
|
||||
|
||||
// Test Logf
|
||||
wrapper.Logf("cleanup info: %s", "value")
|
||||
if mock.infoCalls != 1 {
|
||||
t.Errorf("Expected 1 info call, got %d", mock.infoCalls)
|
||||
}
|
||||
if mock.lastFormat != "cleanup info: %s" {
|
||||
t.Errorf("Expected format 'cleanup info: %%s', got '%s'", mock.lastFormat)
|
||||
}
|
||||
|
||||
// Test ErrorLogf
|
||||
wrapper.ErrorLogf("cleanup error: %d", 456)
|
||||
if mock.errorCalls != 1 {
|
||||
t.Errorf("Expected 1 error call, got %d", mock.errorCalls)
|
||||
}
|
||||
if mock.lastFormat != "cleanup error: %d" {
|
||||
t.Errorf("Expected format 'cleanup error: %%d', got '%s'", mock.lastFormat)
|
||||
}
|
||||
|
||||
// Test DebugLogf
|
||||
wrapper.DebugLogf("cleanup debug: %v", false)
|
||||
if mock.debugCalls != 1 {
|
||||
t.Errorf("Expected 1 debug call, got %d", mock.debugCalls)
|
||||
}
|
||||
if mock.lastFormat != "cleanup debug: %v" {
|
||||
t.Errorf("Expected format 'cleanup debug: %%v', got '%s'", mock.lastFormat)
|
||||
}
|
||||
}
|
||||
|
||||
// TestWrapLoggerForCleanup_NilLogger tests cleanup wrapper with nil logger
|
||||
func TestWrapLoggerForCleanup_NilLogger(t *testing.T) {
|
||||
wrapper := WrapLoggerForCleanup(nil)
|
||||
|
||||
if wrapper == nil {
|
||||
t.Fatal("WrapLoggerForCleanup should not return nil even with nil logger")
|
||||
}
|
||||
|
||||
// These should not panic
|
||||
wrapper.Logf("test")
|
||||
wrapper.ErrorLogf("test")
|
||||
wrapper.DebugLogf("test")
|
||||
}
|
||||
|
||||
// TestLoggerWrappers_MultipleArgs tests wrappers with multiple arguments
|
||||
func TestLoggerWrappers_MultipleArgs(t *testing.T) {
|
||||
mock := &mockLogger{}
|
||||
|
||||
// Test recovery wrapper with multiple args
|
||||
recoveryWrapper := WrapLoggerForRecovery(mock)
|
||||
recoveryWrapper.Logf("format: %s %d %v", "str", 123, true)
|
||||
if mock.infoCalls != 1 {
|
||||
t.Errorf("Expected 1 info call, got %d", mock.infoCalls)
|
||||
}
|
||||
if len(mock.lastArgs) != 3 {
|
||||
t.Errorf("Expected 3 args, got %d", len(mock.lastArgs))
|
||||
}
|
||||
|
||||
// Reset mock
|
||||
mock = &mockLogger{}
|
||||
|
||||
// Test cleanup wrapper with multiple args
|
||||
cleanupWrapper := WrapLoggerForCleanup(mock)
|
||||
cleanupWrapper.ErrorLogf("error: %s %d %v", "err", 456, false)
|
||||
if mock.errorCalls != 1 {
|
||||
t.Errorf("Expected 1 error call, got %d", mock.errorCalls)
|
||||
}
|
||||
if len(mock.lastArgs) != 3 {
|
||||
t.Errorf("Expected 3 args, got %d", len(mock.lastArgs))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user