Repository navigation
Commit 89d65c2
authored
## Summary
### Problem
When CREATE ROLE or ALTER ROLE stores a password without `VALID UNTIL`,
it sets `rolvaliduntil` to now() plus the `yb_password_validity` value,
and each role can have its own value. Say role app_user has its own 30d
(`ALTER ROLE app_user SET yb_password_validity = '30d'`) and role admin
resets app_user's password: app_user should get now() + 30 days, but the
value comes from admin instead:
| admin's own value | app_user's expiry after admin's reset (before the
fix) | After the fix |
| --- | --- | --- |
| 0 (default) | unchanged: app_user's old `rolvaliduntil` is written
back | now() + 30 days |
| 20d | now() + 20 days | now() + 30 days |
With admin at 0, an expired app_user stays locked out after the reset,
and if admin is a password vault that rotates app_user's password, the
rotations never push app_user's expiry out. `CREATE ROLE ... PASSWORD`
has the same problem: a role that admin creates gets admin's value.
```sql
-- as admin, a superuser whose value is 0
CREATE ROLE app_user LOGIN PASSWORD 'p1' VALID UNTIL '2020-01-01';
ALTER ROLE app_user SET yb_password_validity = '30d';
ALTER ROLE app_user PASSWORD 'p2';
SELECT rolvaliduntil FROM pg_authid WHERE rolname = 'app_user';
-- without the fix: 2020-01-01 00:00:00 (app_user still cannot log in)
-- with the fix: now() + 30 days
```
### RCA
`YbComputePasswordValidUntil()` uses the session's
`yb_password_validity`, which a login loads for the role that logs in,
so in the example admin's value is used for app_user. 482a44e
([#33480]) added the setting. 2026.2, 2026.1 and 2025.2 have the same
code.
PostgreSQL is not affected: it has no duration-based expiry.
### Fix
When the statement gives `VALID UNTIL`, that date is used. Otherwise use
the target role's own value: check these in order and use the first one
that is set:
1. the role's own value (`ALTER ROLE <role> SET`)
- A role being created (CREATE ROLE) has no value of its own yet, so
this step is skipped.
2. the value for all roles (`ALTER ROLE ALL SET`)
3. the value in ysql_pg.conf (ysql_pg_conf_csv)
4. the default value
This is the same order the role's own login applies them in
([`process_settings()`](https://github.com/yugabyte/yugabyte-db/blob/53ca95749813802e0ff54df29a7444300f9fd0f1/src/postgres/src/backend/utils/init/postinit.c#L2076-L2080)).
When app_user changes its own password, the value its login loaded is
used, as with any `ALTER ROLE ... SET`: a change to app_user's setting
made after app_user logged in applies from its next login.
For step 3, each process saves the password settings from ysql_pg.conf
whenever it loads the file (at startup and on each reload), because a
session whose login role has its own value keeps no copy of the file's
value.
## Test plan
| Case | Without the fix | With the fix | Test |
| --- | --- | --- | --- |
| admin (no value) resets app_user (own 30d, expired in 2020), the
issue's repro | Still expired | 30 days | yb.orig.password_valid_until |
| admin (own 20d) resets app_user (own 30d) | 20 days | 30 days |
yb.orig.password_valid_until |
| admin (own 20d) changes app_user's (own 30d) password after `SET
SESSION AUTHORIZATION app_user`, and from a `SECURITY DEFINER` function
owned by app_user | 20 days | 30 days | yb.orig.password_valid_until |
| admin (own 20d) resets app_user (own 0) | 20 days | No expiry |
yb.orig.password_valid_until |
| admin (own 20d) resets app_user (no value) and creates a role, with
`ALTER ROLE ALL SET` 40d | 20 days | 40 days |
yb.orig.password_valid_until |
| The same, with no `ALTER ROLE ALL SET` | 20 days | No expiry (the
default, 0) | yb.orig.password_valid_until |
| 50d in ysql_pg_conf_csv: a superuser (no value) resets app_user (no
value) | 50 days | 50 days | TestPgPasswordValidity |
| 50d in ysql_pg_conf_csv and `ALTER ROLE ALL SET` 30d: a superuser (no
value) resets app_user (no value) | 50 days | 30 days |
TestPgPasswordValidity |
| 50d in ysql_pg_conf_csv: the superuser resets a role with its own 0 |
50 days | No expiry | TestPgPasswordValidity |
| 50d in ysql_pg_conf_csv: admin (own 0) resets app_user (no value) and
creates new_user | app_user unchanged, new_user no expiry | 50 days for
both | TestPgPasswordValidity |
| ysql_pg.conf changed to 20d, then to an invalid value, then with no
line, each followed by a reload: admin (own 0, one session throughout)
resets app_user (no value) after each | Expiry unchanged each time | 20
days; 20 days (the invalid value is ignored); no expiry (the default) |
TestPgPasswordValidity |
| A superuser logs in with its own 10d, sets its own value to 25d, then
changes its own password in that session | 10 days | 10 days, the value
its login loaded | yb.orig.password_valid_until |
| A superuser (own reuse limit 0) resets the password of a role with no
limit of its own; the limit in ysql_pg_conf_csv is 3 | Keeps all 4
passwords | Keeps the 3 newest | yb.orig.password_history |
With the fix, the four tests below pass on a fastdebug build (clang21),
directly and through YSQL Connection Manager
(`YB_ENABLE_YSQL_CONN_MGR_IN_JAVA_TESTS=true`):
```bash
./yb_build.sh fastdebug --java-test org.yb.pgsql.TestPgRegressAuth
./yb_build.sh fastdebug --java-test org.yb.pgsql.TestPgPasswordValidity
./yb_build.sh fastdebug --java-test org.yb.pgsql.TestPgRegressPasswordHistory
./yb_build.sh fastdebug --java-test org.yb.pgsql.TestPgPasswordHistory
```
Manual: with 50d in ysql_pg_conf_csv, admin (own value 0) kept a session
open while ysql_pg_conf_csv changed to 60d at runtime; admin's next
reset in that session gave app_user 60 days.
1 parent ab8ab2b commit 89d65c2
11 files changed
Lines changed: 685 additions & 172 deletions
File tree
- java/yb-pgsql/src/test/java/org/yb/pgsql
- src/postgres/src
- backend
- catalog
- commands
- utils/misc
- include
- catalog
- utils
- test/regress
- expected
- sql
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
0 commit comments