|
| 1 | +-- |
| 2 | +-- Tests for password types |
| 3 | +-- |
| 4 | +-- Tests for GUC password_encryption |
| 5 | +SET password_encryption = 'novalue'; -- error |
| 6 | +ERROR: invalid value for parameter "password_encryption": "novalue" |
| 7 | +HINT: Available values: md5, scram-sha-256. |
| 8 | +SET password_encryption = true; -- error |
| 9 | +ERROR: invalid value for parameter "password_encryption": "true" |
| 10 | +HINT: Available values: md5, scram-sha-256. |
| 11 | +SET password_encryption = 'md5'; -- ok |
| 12 | +SET password_encryption = 'scram-sha-256'; -- ok |
| 13 | +-- consistency of password entries |
| 14 | +SET password_encryption = 'md5'; |
| 15 | +CREATE ROLE regress_passwd1; |
| 16 | +ALTER ROLE regress_passwd1 PASSWORD 'role_pwd1'; |
| 17 | +ERROR: password encryption failed: unsupported |
| 18 | +CREATE ROLE regress_passwd2; |
| 19 | +ALTER ROLE regress_passwd2 PASSWORD 'role_pwd2'; |
| 20 | +ERROR: password encryption failed: unsupported |
| 21 | +SET password_encryption = 'scram-sha-256'; |
| 22 | +CREATE ROLE regress_passwd3 PASSWORD 'role_pwd3'; |
| 23 | +CREATE ROLE regress_passwd4 PASSWORD NULL; |
| 24 | +-- check list of created entries |
| 25 | +-- |
| 26 | +-- The scram secret will look something like: |
| 27 | +-- SCRAM-SHA-256$4096:E4HxLGtnRzsYwg==$6YtlR4t69SguDiwFvbVgVZtuz6gpJQQqUMZ7IQJK5yI=:ps75jrHeYU4lXCcXI4O8oIdJ3eO8o2jirjruw9phBTo= |
| 28 | +-- |
| 29 | +-- Since the salt is random, the exact value stored will be different on every test |
| 30 | +-- run. Use a regular expression to mask the changing parts. |
| 31 | +SELECT rolname, regexp_replace(rolpassword, '(SCRAM-SHA-256)\$(\d+):([a-zA-Z0-9+/=]+)\$([a-zA-Z0-9+=/]+):([a-zA-Z0-9+/=]+)', '\1$\2:<salt>$<storedkey>:<serverkey>') as rolpassword_masked |
| 32 | + FROM pg_authid |
| 33 | + WHERE rolname LIKE 'regress_passwd%' |
| 34 | + ORDER BY rolname, rolpassword; |
| 35 | + rolname | rolpassword_masked |
| 36 | +-----------------+--------------------------------------------------- |
| 37 | + regress_passwd1 | |
| 38 | + regress_passwd2 | |
| 39 | + regress_passwd3 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey> |
| 40 | + regress_passwd4 | |
| 41 | +(4 rows) |
| 42 | + |
| 43 | +-- Rename a role |
| 44 | +ALTER ROLE regress_passwd2 RENAME TO regress_passwd2_new; |
| 45 | +-- md5 entry should have been removed |
| 46 | +SELECT rolname, rolpassword |
| 47 | + FROM pg_authid |
| 48 | + WHERE rolname LIKE 'regress_passwd2_new' |
| 49 | + ORDER BY rolname, rolpassword; |
| 50 | + rolname | rolpassword |
| 51 | +---------------------+------------- |
| 52 | + regress_passwd2_new | |
| 53 | +(1 row) |
| 54 | + |
| 55 | +ALTER ROLE regress_passwd2_new RENAME TO regress_passwd2; |
| 56 | +-- Change passwords with ALTER USER. With plaintext or already-encrypted |
| 57 | +-- passwords. |
| 58 | +SET password_encryption = 'md5'; |
| 59 | +-- encrypt with MD5 |
| 60 | +ALTER ROLE regress_passwd2 PASSWORD 'foo'; |
| 61 | +ERROR: password encryption failed: unsupported |
| 62 | +-- already encrypted, use as they are |
| 63 | +ALTER ROLE regress_passwd1 PASSWORD 'md5cd3578025fe2c3d7ed1b9a9b26238b70'; |
| 64 | +ALTER ROLE regress_passwd3 PASSWORD 'SCRAM-SHA-256$4096:VLK4RMaQLCvNtQ==$6YtlR4t69SguDiwFvbVgVZtuz6gpJQQqUMZ7IQJK5yI=:ps75jrHeYU4lXCcXI4O8oIdJ3eO8o2jirjruw9phBTo='; |
| 65 | +SET password_encryption = 'scram-sha-256'; |
| 66 | +-- create SCRAM secret |
| 67 | +ALTER ROLE regress_passwd4 PASSWORD 'foo'; |
| 68 | +-- already encrypted with MD5, use as it is |
| 69 | +CREATE ROLE regress_passwd5 PASSWORD 'md5e73a4b11df52a6068f8b39f90be36023'; |
| 70 | +-- This looks like a valid SCRAM-SHA-256 secret, but it is not |
| 71 | +-- so it should be hashed with SCRAM-SHA-256. |
| 72 | +CREATE ROLE regress_passwd6 PASSWORD 'SCRAM-SHA-256$1234'; |
| 73 | +-- These may look like valid MD5 secrets, but they are not, so they |
| 74 | +-- should be hashed with SCRAM-SHA-256. |
| 75 | +-- trailing garbage at the end |
| 76 | +CREATE ROLE regress_passwd7 PASSWORD 'md5012345678901234567890123456789zz'; |
| 77 | +-- invalid length |
| 78 | +CREATE ROLE regress_passwd8 PASSWORD 'md501234567890123456789012345678901zz'; |
| 79 | +-- Changing the SCRAM iteration count |
| 80 | +SET scram_iterations = 1024; |
| 81 | +CREATE ROLE regress_passwd9 PASSWORD 'alterediterationcount'; |
| 82 | +SELECT rolname, regexp_replace(rolpassword, '(SCRAM-SHA-256)\$(\d+):([a-zA-Z0-9+/=]+)\$([a-zA-Z0-9+=/]+):([a-zA-Z0-9+/=]+)', '\1$\2:<salt>$<storedkey>:<serverkey>') as rolpassword_masked |
| 83 | + FROM pg_authid |
| 84 | + WHERE rolname LIKE 'regress_passwd%' |
| 85 | + ORDER BY rolname, rolpassword; |
| 86 | + rolname | rolpassword_masked |
| 87 | +-----------------+--------------------------------------------------- |
| 88 | + regress_passwd1 | md5cd3578025fe2c3d7ed1b9a9b26238b70 |
| 89 | + regress_passwd2 | |
| 90 | + regress_passwd3 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey> |
| 91 | + regress_passwd4 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey> |
| 92 | + regress_passwd5 | md5e73a4b11df52a6068f8b39f90be36023 |
| 93 | + regress_passwd6 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey> |
| 94 | + regress_passwd7 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey> |
| 95 | + regress_passwd8 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey> |
| 96 | + regress_passwd9 | SCRAM-SHA-256$1024:<salt>$<storedkey>:<serverkey> |
| 97 | +(9 rows) |
| 98 | + |
| 99 | +-- An empty password is not allowed, in any form |
| 100 | +CREATE ROLE regress_passwd_empty PASSWORD ''; |
| 101 | +NOTICE: empty string is not a valid password, clearing password |
| 102 | +ALTER ROLE regress_passwd_empty PASSWORD 'md585939a5ce845f1a1b620742e3c659e0a'; |
| 103 | +ALTER ROLE regress_passwd_empty PASSWORD 'SCRAM-SHA-256$4096:hpFyHTUsSWcR7O9P$LgZFIt6Oqdo27ZFKbZ2nV+vtnYM995pDh9ca6WSi120=:qVV5NeluNfUPkwm7Vqat25RjSPLkGeoZBQs6wVv+um4='; |
| 104 | +NOTICE: empty string is not a valid password, clearing password |
| 105 | +SELECT rolpassword FROM pg_authid WHERE rolname='regress_passwd_empty'; |
| 106 | + rolpassword |
| 107 | +------------- |
| 108 | + |
| 109 | +(1 row) |
| 110 | + |
| 111 | +-- Test with invalid stored and server keys. |
| 112 | +-- |
| 113 | +-- The first is valid, to act as a control. The others have too long |
| 114 | +-- stored/server keys. They will be re-hashed. |
| 115 | +CREATE ROLE regress_passwd_sha_len0 PASSWORD 'SCRAM-SHA-256$4096:A6xHKoH/494E941doaPOYg==$Ky+A30sewHIH3VHQLRN9vYsuzlgNyGNKCh37dy96Rqw=:COPdlNiIkrsacU5QoxydEuOH6e/KfiipeETb/bPw8ZI='; |
| 116 | +CREATE ROLE regress_passwd_sha_len1 PASSWORD 'SCRAM-SHA-256$4096:A6xHKoH/494E941doaPOYg==$Ky+A30sewHIH3VHQLRN9vYsuzlgNyGNKCh37dy96RqwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=:COPdlNiIkrsacU5QoxydEuOH6e/KfiipeETb/bPw8ZI='; |
| 117 | +CREATE ROLE regress_passwd_sha_len2 PASSWORD 'SCRAM-SHA-256$4096:A6xHKoH/494E941doaPOYg==$Ky+A30sewHIH3VHQLRN9vYsuzlgNyGNKCh37dy96Rqw=:COPdlNiIkrsacU5QoxydEuOH6e/KfiipeETb/bPw8ZIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA='; |
| 118 | +-- Check that the invalid secrets were re-hashed. A re-hashed secret |
| 119 | +-- should not contain the original salt. |
| 120 | +SELECT rolname, rolpassword not like '%A6xHKoH/494E941doaPOYg==%' as is_rolpassword_rehashed |
| 121 | + FROM pg_authid |
| 122 | + WHERE rolname LIKE 'regress_passwd_sha_len%' |
| 123 | + ORDER BY rolname; |
| 124 | + rolname | is_rolpassword_rehashed |
| 125 | +-------------------------+------------------------- |
| 126 | + regress_passwd_sha_len0 | f |
| 127 | + regress_passwd_sha_len1 | t |
| 128 | + regress_passwd_sha_len2 | t |
| 129 | +(3 rows) |
| 130 | + |
| 131 | +DROP ROLE regress_passwd1; |
| 132 | +DROP ROLE regress_passwd2; |
| 133 | +DROP ROLE regress_passwd3; |
| 134 | +DROP ROLE regress_passwd4; |
| 135 | +DROP ROLE regress_passwd5; |
| 136 | +DROP ROLE regress_passwd6; |
| 137 | +DROP ROLE regress_passwd7; |
| 138 | +DROP ROLE regress_passwd8; |
| 139 | +DROP ROLE regress_passwd9; |
| 140 | +DROP ROLE regress_passwd_empty; |
| 141 | +DROP ROLE regress_passwd_sha_len0; |
| 142 | +DROP ROLE regress_passwd_sha_len1; |
| 143 | +DROP ROLE regress_passwd_sha_len2; |
| 144 | +-- all entries should have been removed |
| 145 | +SELECT rolname, rolpassword |
| 146 | + FROM pg_authid |
| 147 | + WHERE rolname LIKE 'regress_passwd%' |
| 148 | + ORDER BY rolname, rolpassword; |
| 149 | + rolname | rolpassword |
| 150 | +---------+------------- |
| 151 | +(0 rows) |
| 152 | + |
0 commit comments