2 -- Tests for password types
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 PASSWORD 'role_pwd1';
16 CREATE ROLE regress_passwd2 PASSWORD 'role_pwd2';
17 SET password_encryption = 'scram-sha-256';
18 CREATE ROLE regress_passwd3 PASSWORD 'role_pwd3';
19 CREATE ROLE regress_passwd4 PASSWORD NULL;
20 -- check list of created entries
22 -- The scram secret will look something like:
23 -- SCRAM-SHA-256$4096:E4HxLGtnRzsYwg==$6YtlR4t69SguDiwFvbVgVZtuz6gpJQQqUMZ7IQJK5yI=:ps75jrHeYU4lXCcXI4O8oIdJ3eO8o2jirjruw9phBTo=
25 -- Since the salt is random, the exact value stored will be different on every test
26 -- run. Use a regular expression to mask the changing parts.
27 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
29 WHERE rolname LIKE 'regress_passwd%'
30 ORDER BY rolname, rolpassword;
31 rolname | rolpassword_masked
32 -----------------+---------------------------------------------------
33 regress_passwd1 | md5783277baca28003b33453252be4dbb34
34 regress_passwd2 | md54044304ba511dd062133eb5b4b84a2a3
35 regress_passwd3 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey>
40 ALTER ROLE regress_passwd2 RENAME TO regress_passwd2_new;
41 NOTICE: MD5 password cleared because of role rename
42 -- md5 entry should have been removed
43 SELECT rolname, rolpassword
45 WHERE rolname LIKE 'regress_passwd2_new'
46 ORDER BY rolname, rolpassword;
48 ---------------------+-------------
52 ALTER ROLE regress_passwd2_new RENAME TO regress_passwd2;
53 -- Change passwords with ALTER USER. With plaintext or already-encrypted
55 SET password_encryption = 'md5';
57 ALTER ROLE regress_passwd2 PASSWORD 'foo';
58 -- already encrypted, use as they are
59 ALTER ROLE regress_passwd1 PASSWORD 'md5cd3578025fe2c3d7ed1b9a9b26238b70';
60 ALTER ROLE regress_passwd3 PASSWORD 'SCRAM-SHA-256$4096:VLK4RMaQLCvNtQ==$6YtlR4t69SguDiwFvbVgVZtuz6gpJQQqUMZ7IQJK5yI=:ps75jrHeYU4lXCcXI4O8oIdJ3eO8o2jirjruw9phBTo=';
61 SET password_encryption = 'scram-sha-256';
62 -- create SCRAM secret
63 ALTER ROLE regress_passwd4 PASSWORD 'foo';
64 -- already encrypted with MD5, use as it is
65 CREATE ROLE regress_passwd5 PASSWORD 'md5e73a4b11df52a6068f8b39f90be36023';
66 -- This looks like a valid SCRAM-SHA-256 secret, but it is not
67 -- so it should be hashed with SCRAM-SHA-256.
68 CREATE ROLE regress_passwd6 PASSWORD 'SCRAM-SHA-256$1234';
69 -- These may look like valid MD5 secrets, but they are not, so they
70 -- should be hashed with SCRAM-SHA-256.
71 -- trailing garbage at the end
72 CREATE ROLE regress_passwd7 PASSWORD 'md5012345678901234567890123456789zz';
74 CREATE ROLE regress_passwd8 PASSWORD 'md501234567890123456789012345678901zz';
75 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
77 WHERE rolname LIKE 'regress_passwd%'
78 ORDER BY rolname, rolpassword;
79 rolname | rolpassword_masked
80 -----------------+---------------------------------------------------
81 regress_passwd1 | md5cd3578025fe2c3d7ed1b9a9b26238b70
82 regress_passwd2 | md5dfa155cadd5f4ad57860162f3fab9cdb
83 regress_passwd3 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey>
84 regress_passwd4 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey>
85 regress_passwd5 | md5e73a4b11df52a6068f8b39f90be36023
86 regress_passwd6 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey>
87 regress_passwd7 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey>
88 regress_passwd8 | SCRAM-SHA-256$4096:<salt>$<storedkey>:<serverkey>
91 -- An empty password is not allowed, in any form
92 CREATE ROLE regress_passwd_empty PASSWORD '';
93 NOTICE: empty string is not a valid password, clearing password
94 ALTER ROLE regress_passwd_empty PASSWORD 'md585939a5ce845f1a1b620742e3c659e0a';
95 NOTICE: empty string is not a valid password, clearing password
96 ALTER ROLE regress_passwd_empty PASSWORD 'SCRAM-SHA-256$4096:hpFyHTUsSWcR7O9P$LgZFIt6Oqdo27ZFKbZ2nV+vtnYM995pDh9ca6WSi120=:qVV5NeluNfUPkwm7Vqat25RjSPLkGeoZBQs6wVv+um4=';
97 NOTICE: empty string is not a valid password, clearing password
98 SELECT rolpassword FROM pg_authid WHERE rolname='regress_passwd_empty';
104 -- Test with invalid stored and server keys.
106 -- The first is valid, to act as a control. The others have too long
107 -- stored/server keys. They will be re-hashed.
108 CREATE ROLE regress_passwd_sha_len0 PASSWORD 'SCRAM-SHA-256$4096:A6xHKoH/494E941doaPOYg==$Ky+A30sewHIH3VHQLRN9vYsuzlgNyGNKCh37dy96Rqw=:COPdlNiIkrsacU5QoxydEuOH6e/KfiipeETb/bPw8ZI=';
109 CREATE ROLE regress_passwd_sha_len1 PASSWORD 'SCRAM-SHA-256$4096:A6xHKoH/494E941doaPOYg==$Ky+A30sewHIH3VHQLRN9vYsuzlgNyGNKCh37dy96RqwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=:COPdlNiIkrsacU5QoxydEuOH6e/KfiipeETb/bPw8ZI=';
110 CREATE ROLE regress_passwd_sha_len2 PASSWORD 'SCRAM-SHA-256$4096:A6xHKoH/494E941doaPOYg==$Ky+A30sewHIH3VHQLRN9vYsuzlgNyGNKCh37dy96Rqw=:COPdlNiIkrsacU5QoxydEuOH6e/KfiipeETb/bPw8ZIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=';
111 -- Check that the invalid secrets were re-hashed. A re-hashed secret
112 -- should not contain the original salt.
113 SELECT rolname, rolpassword not like '%A6xHKoH/494E941doaPOYg==%' as is_rolpassword_rehashed
115 WHERE rolname LIKE 'regress_passwd_sha_len%'
117 rolname | is_rolpassword_rehashed
118 -------------------------+-------------------------
119 regress_passwd_sha_len0 | f
120 regress_passwd_sha_len1 | t
121 regress_passwd_sha_len2 | t
124 DROP ROLE regress_passwd1;
125 DROP ROLE regress_passwd2;
126 DROP ROLE regress_passwd3;
127 DROP ROLE regress_passwd4;
128 DROP ROLE regress_passwd5;
129 DROP ROLE regress_passwd6;
130 DROP ROLE regress_passwd7;
131 DROP ROLE regress_passwd8;
132 DROP ROLE regress_passwd_empty;
133 DROP ROLE regress_passwd_sha_len0;
134 DROP ROLE regress_passwd_sha_len1;
135 DROP ROLE regress_passwd_sha_len2;
136 -- all entries should have been removed
137 SELECT rolname, rolpassword
139 WHERE rolname LIKE 'regress_passwd%'
140 ORDER BY rolname, rolpassword;
141 rolname | rolpassword
142 ---------+-------------