@@ -207,7 +207,7 @@ REVOKE ALL PRIVILEGES
207207map [string ]string {
208208"username" : postgresqlUser ,
209209"namespace" : postgresqlSchema ,
210- "verifier" : string (clusterSecret .Data [credentialSecretKey ]),
210+ "verifier" : string (clusterSecret .Data [verifierSecretKey ]),
211211
212212"ON_ERROR_STOP" : "on" , // Abort when any one statement fails.
213213"QUIET" : "on" , // Do not print successful statements to stdout.
@@ -218,21 +218,29 @@ REVOKE ALL PRIVILEGES
218218return err
219219}
220220
221- func generateVerifier () ([]byte , error ) {
222- v , err := util .GeneratePassword (32 )
221+ func generatePassword () (plaintext , verifier string , err error ) {
222+ // PgBouncer can login to PostgreSQL using either MD5 or SCRAM-SHA-256.
223+ // When using MD5, the (hashed) verifier can be stored in PgBouncer's
224+ // authentication file. When using SCRAM, the plaintext password must be
225+ // stored.
226+ // - https://www.pgbouncer.org/config.html#authentication-file-format
227+ // - https://github.com/pgbouncer/pgbouncer/issues/508#issuecomment-713339834
228+
229+ plaintext , err = util .GeneratePassword (32 )
223230if err == nil {
224- // NOTE(cbandy): It is not possible to use a SCRAM password for the
225- // "auth_user" account.
226- // - https://github.com/pgbouncer/pgbouncer/issues/508#issuecomment-713339834
227- v , err = password .NewMD5Password (postgresqlUser , v ).Build ()
231+ verifier , err = password .NewSCRAMPassword (plaintext ).Build ()
228232}
229- return [] byte ( v ), err
233+ return
230234}
231235
232- func postgresqlHBA () postgres.HostBasedAuthentication {
233- // PgBouncer connects over TLS using an MD5 password.
234- // NOTE(cbandy): It is not possible to use a SCRAM password for the
235- // "auth_user" account.
236- // - https://github.com/pgbouncer/pgbouncer/issues/508#issuecomment-713339834
237- return * postgres .NewHBA ().User (postgresqlUser ).TLS ().Method ("md5" )
236+ func postgresqlHBAs () []postgres.HostBasedAuthentication {
237+ // PgBouncer must connect over TLS using a SCRAM password. Other network
238+ // connections are forbidden.
239+ // - https://www.postgresql.org/docs/current/auth-pg-hba-conf.html
240+ // - https://www.postgresql.org/docs/current/auth-password.html
241+
242+ return []postgres.HostBasedAuthentication {
243+ * postgres .NewHBA ().User (postgresqlUser ).TLS ().Method ("scram-sha-256" ),
244+ * postgres .NewHBA ().User (postgresqlUser ).TCP ().Method ("reject" ),
245+ }
238246}
0 commit comments