MCPcopy Create free account
hub / github.com/CrunchyData/postgres-operator / sqlAuthenticationQuery

Function sqlAuthenticationQuery

internal/pgbouncer/postgres.go:29–55  ·  view source on GitHub ↗

sqlAuthenticationQuery returns the SECURITY DEFINER function that allows PgBouncer to access non-privileged and non-system user credentials.

(sqlFunctionName string)

Source from the content-addressed store, hash-verified

27// sqlAuthenticationQuery returns the SECURITY DEFINER function that allows
28// PgBouncer to access non-privileged and non-system user credentials.
29func 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(`
48CREATE OR REPLACE FUNCTION ` + sqlFunctionName + `(username TEXT)
49RETURNS 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) + `
54LANGUAGE SQL STABLE SECURITY DEFINER;`)
55}
56
57// DisableInPostgreSQL removes any objects created by EnableInPostgreSQL.
58func DisableInPostgreSQL(ctx context.Context, exec postgres.Executor) error {

Callers 2

EnableInPostgreSQLFunction · 0.85

Calls 1

QuoteLiteralFunction · 0.92

Tested by 1