sqlAuthenticationQuery returns the SECURITY DEFINER function that allows PgBouncer to access non-privileged and non-system user credentials.
(sqlFunctionName string)
| 27 | // sqlAuthenticationQuery returns the SECURITY DEFINER function that allows |
| 28 | // PgBouncer to access non-privileged and non-system user credentials. |
| 29 | func sqlAuthenticationQuery(sqlFunctionName string) string { |
| 30 | // Only a subset of authorization identifiers should be accessible to |
| 31 | // PgBouncer. |
| 32 | // - https://www.postgresql.org/docs/current/catalog-pg-authid.html |
| 33 | sqlAuthorizationConditions := strings.Join([]string{ |
| 34 | // Only those with permission to login. |
| 35 | `pg_authid.rolcanlogin`, |
| 36 | // No superusers. This is important: allowing superusers would make the |
| 37 | // PgBouncer user a de facto superuser. |
| 38 | `NOT pg_authid.rolsuper`, |
| 39 | // No replicators. |
| 40 | `NOT pg_authid.rolreplication`, |
| 41 | // Not the PgBouncer role itself. |
| 42 | `pg_authid.rolname <> ` + postgres.QuoteLiteral(PostgresqlUser), |
| 43 | // Those without a password expiration or an expiration in the future. |
| 44 | `(pg_authid.rolvaliduntil IS NULL OR pg_authid.rolvaliduntil >= CURRENT_TIMESTAMP)`, |
| 45 | }, "\n AND ") |
| 46 | |
| 47 | return strings.TrimSpace(` |
| 48 | CREATE OR REPLACE FUNCTION ` + sqlFunctionName + `(username TEXT) |
| 49 | RETURNS TABLE(username TEXT, password TEXT) AS ` + postgres.QuoteLiteral(` |
| 50 | SELECT rolname::TEXT, rolpassword::TEXT |
| 51 | FROM pg_catalog.pg_authid |
| 52 | WHERE pg_authid.rolname = $1 |
| 53 | AND `+sqlAuthorizationConditions) + ` |
| 54 | LANGUAGE SQL STABLE SECURITY DEFINER;`) |
| 55 | } |
| 56 | |
| 57 | // DisableInPostgreSQL removes any objects created by EnableInPostgreSQL. |
| 58 | func DisableInPostgreSQL(ctx context.Context, exec postgres.Executor) error { |