From b4bc027b06ab122c48bd9a296027c664328b64f6 Mon Sep 17 00:00:00 2001
From: Garet Halliday <me@garet.holiday>
Date: Wed, 30 Aug 2023 16:29:18 -0500
Subject: [PATCH] fix

---
 lib/gat/modes/pgbouncer/authfile.go | 33 +++++++++++++++++++++++++++++
 lib/gat/modes/pgbouncer/config.go   |  2 +-
 lib/gat/modes/pgbouncer/pools.go    | 18 +---------------
 lib/gat/modes/zalando/config.go     |  6 +++++-
 4 files changed, 40 insertions(+), 19 deletions(-)
 create mode 100644 lib/gat/modes/pgbouncer/authfile.go

diff --git a/lib/gat/modes/pgbouncer/authfile.go b/lib/gat/modes/pgbouncer/authfile.go
new file mode 100644
index 00000000..18649a9c
--- /dev/null
+++ b/lib/gat/modes/pgbouncer/authfile.go
@@ -0,0 +1,33 @@
+package pgbouncer
+
+import (
+	"os"
+
+	"pggat2/lib/util/encoding/ini"
+	"pggat2/lib/util/encoding/userlist"
+)
+
+type AuthFile struct {
+	Users map[string]string
+}
+
+func (T *AuthFile) UnmarshalINI(bytes []byte) error {
+	path := string(bytes)
+	if path == "" {
+		return nil
+	}
+
+	file, err := os.ReadFile(path)
+	if err != nil {
+		return err
+	}
+
+	T.Users, err = userlist.Unmarshal(file)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+var _ ini.Unmarshaller = (*AuthFile)(nil)
diff --git a/lib/gat/modes/pgbouncer/config.go b/lib/gat/modes/pgbouncer/config.go
index 3d74dd08..4de99906 100644
--- a/lib/gat/modes/pgbouncer/config.go
+++ b/lib/gat/modes/pgbouncer/config.go
@@ -82,7 +82,7 @@ type PgBouncer struct {
 	StatsPeriod             int                `ini:"stats_period"`
 	AuthType                string             `ini:"auth_type"`
 	AuthHbaFile             string             `ini:"auth_hba_file"`
-	AuthFile                string             `ini:"auth_file"`
+	AuthFile                AuthFile           `ini:"auth_file"`
 	AuthUser                string             `ini:"auth_user"`
 	AuthQuery               string             `ini:"auth_query"`
 	AuthDbname              string             `ini:"auth_dbname"`
diff --git a/lib/gat/modes/pgbouncer/pools.go b/lib/gat/modes/pgbouncer/pools.go
index faa0ebdf..7ec25396 100644
--- a/lib/gat/modes/pgbouncer/pools.go
+++ b/lib/gat/modes/pgbouncer/pools.go
@@ -2,7 +2,6 @@ package pgbouncer
 
 import (
 	"net"
-	"os"
 	"strconv"
 	"time"
 
@@ -15,7 +14,6 @@ import (
 	"pggat2/lib/gat/pool/pools/session"
 	"pggat2/lib/gat/pool/pools/transaction"
 	"pggat2/lib/psql"
-	"pggat2/lib/util/encoding/userlist"
 	"pggat2/lib/util/strutil"
 	"pggat2/lib/zap"
 )
@@ -33,8 +31,6 @@ type poolKey struct {
 type Pools struct {
 	Config *Config
 
-	AuthFile map[string]string
-
 	pools map[poolKey]*pool.Pool
 	keys  map[[8]byte]*pool.Pool
 }
@@ -44,18 +40,6 @@ func NewPools(config *Config) (*Pools, error) {
 		Config: config,
 	}
 
-	if config.PgBouncer.AuthFile != "" {
-		file, err := os.ReadFile(config.PgBouncer.AuthFile)
-		if err != nil {
-			return nil, err
-		}
-
-		pools.AuthFile, err = userlist.Unmarshal(file)
-		if err != nil {
-			return nil, err
-		}
-	}
-
 	return pools, nil
 }
 
@@ -79,7 +63,7 @@ func (T *Pools) Lookup(user, database string) *pool.Pool {
 		}
 	}
 
-	password, ok := T.AuthFile[user]
+	password, ok := T.Config.PgBouncer.AuthFile.Users[user]
 	if !ok {
 		// try auth query
 		authUser := db.AuthUser
diff --git a/lib/gat/modes/zalando/config.go b/lib/gat/modes/zalando/config.go
index 55670ff0..f0705c49 100644
--- a/lib/gat/modes/zalando/config.go
+++ b/lib/gat/modes/zalando/config.go
@@ -50,7 +50,11 @@ func (T *Config) ListenAndServe() error {
 	pgb.PgBouncer.ListenPort = T.PoolerPort
 	pgb.PgBouncer.ListenAddr = "*"
 	pgb.PgBouncer.AuthType = "md5"
-	pgb.PgBouncer.AuthFile = "/etc/pgbouncer/auth_file.txt"
+	pgb.PgBouncer.AuthFile = pgbouncer.AuthFile{
+		Users: map[string]string{
+			T.PGUser: T.PGPassword,
+		},
+	}
 	pgb.PgBouncer.AdminUsers = []string{T.PGUser}
 	pgb.PgBouncer.AuthQuery = fmt.Sprintf("SELECT * FROM %s.user_lookup($1)", T.PGSchema)
 	pgb.PgBouncer.LogFile = "/var/olg/pgbouncer/pgbouncer.log"
-- 
GitLab