diff --git a/internal/outpost/proxyv2/application/session.go b/internal/outpost/proxyv2/application/session.go index 42ab6707d3..db253e6e6e 100644 --- a/internal/outpost/proxyv2/application/session.go +++ b/internal/outpost/proxyv2/application/session.go @@ -31,7 +31,7 @@ func (a *Application) getStore(p api.ProxyOutpostConfig, externalHost *url.URL) } if a.isEmbedded { // New PostgreSQL store - ps, err := postgresstore.NewPostgresStore() + ps, err := postgresstore.NewPostgresStore(a.log) if err != nil { return nil, err } diff --git a/internal/outpost/proxyv2/postgresstore/logger.go b/internal/outpost/proxyv2/postgresstore/logger.go new file mode 100644 index 0000000000..64bba15005 --- /dev/null +++ b/internal/outpost/proxyv2/postgresstore/logger.go @@ -0,0 +1,48 @@ +package postgresstore + +import ( + "context" + "time" + + log "github.com/sirupsen/logrus" + gormlogger "gorm.io/gorm/logger" +) + +type logrusLogger struct { + logger *log.Entry +} + +func NewLogger(parent *log.Entry) *logrusLogger { + return &logrusLogger{ + logger: parent, + } +} + +func (l *logrusLogger) LogMode(gormlogger.LogLevel) gormlogger.Interface { + return l +} + +func (l *logrusLogger) Info(ctx context.Context, s string, args ...interface{}) { + l.logger.WithContext(ctx).Infof(s, args...) +} + +func (l *logrusLogger) Warn(ctx context.Context, s string, args ...interface{}) { + l.logger.WithContext(ctx).Warnf(s, args...) +} + +func (l *logrusLogger) Error(ctx context.Context, s string, args ...interface{}) { + l.logger.WithContext(ctx).Errorf(s, args...) +} + +func (l *logrusLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { + elapsed := time.Since(begin) + sql, _ := fc() + fields := log.Fields{ + "elapsed": elapsed, + } + if err != nil { + l.logger.WithContext(ctx).WithFields(fields).WithError(err).Error(sql) + return + } + l.logger.WithContext(ctx).WithFields(fields).Trace(sql) +} diff --git a/internal/outpost/proxyv2/postgresstore/postgresstore.go b/internal/outpost/proxyv2/postgresstore/postgresstore.go index 9958542360..7328bf518a 100644 --- a/internal/outpost/proxyv2/postgresstore/postgresstore.go +++ b/internal/outpost/proxyv2/postgresstore/postgresstore.go @@ -7,7 +7,6 @@ import ( "fmt" "net/http" "strings" - "sync" "time" "github.com/google/uuid" @@ -18,7 +17,6 @@ import ( _ "gorm.io/driver/postgres" "gorm.io/gorm" "gorm.io/gorm/clause" - "gorm.io/gorm/logger" "goauthentik.io/internal/config" "goauthentik.io/internal/outpost/proxyv2/constants" @@ -159,12 +157,12 @@ func SetupGORMWithRefreshablePool(cfg config.PostgreSQLConfig, gormConfig *gorm. } // NewPostgresStore returns a new PostgresStore -func NewPostgresStore() (*PostgresStore, error) { +func NewPostgresStore(log *log.Entry) (*PostgresStore, error) { cfg := config.Get().PostgreSQL // Configure GORM gormConfig := &gorm.Config{ - Logger: logger.Default.LogMode(logger.Silent), + Logger: NewLogger(log), NowFunc: func() time.Time { return time.Now().UTC() }, @@ -437,39 +435,6 @@ func generateSessionID() string { return uuid.New().String() } -var ( - globalStore *PostgresStore - mu sync.Mutex -) - -// GetPersistentStore creates a new postgres store if it is the first time the function has been called. -// If the function is called multiple times, the store from the variable is returned to ensure that only one instance is running. -func GetPersistentStore() (*PostgresStore, error) { - mu.Lock() - defer mu.Unlock() - - if globalStore == nil { - store, err := NewPostgresStore() - if err != nil { - return nil, fmt.Errorf("failed to create persistent store: %w", err) - } - globalStore = store - } - - return globalStore, nil -} - -// StopPersistentStore stops the cleanup background job and clears the globalStore variable. -func StopPersistentStore() { - mu.Lock() - defer mu.Unlock() - - if globalStore != nil { - _ = globalStore.Close() - globalStore = nil - } -} - // NewTestStore creates a PostgresStore for testing with the given database and pool. // The pool reference is required to properly close connections in test cleanup. func NewTestStore(db *gorm.DB, pool *RefreshableConnPool) *PostgresStore {