Skip to content

Improve Password Generation Security #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions misc/generate_password.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- WARNING: random() that is used here is not cryptographically strong –
-- if an attacker knows one value, it's easy to guess the "next" value
-- TODO: rework to use pgcrypto instead
-- This script uses pgcrypto extension to generate cryptographically secure random passwords
-- You need to enable the pgcrypto extension first with: CREATE EXTENSION IF NOT EXISTS pgcrypto;

with init(len, arr) as (
-- edit password length and possible characters here
Expand All @@ -9,7 +8,8 @@ with init(len, arr) as (
select count(*)
from (select unnest(arr) from init) _
), indexes(i) as (
select 1 + int4(random() * (l - 1))
-- Using gen_random_bytes from pgcrypto for cryptographically secure randomness
select 1 + (get_byte(gen_random_bytes(1), 0)::int % (l - 1))
from arrlen, (select generate_series(1, len) from init) _
), res as (
select array_to_string(array_agg(arr[i]), '') as password
Expand Down
9 changes: 6 additions & 3 deletions roles/alter_user_with_random_password.psql
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
-- This interactive script solves this problem.

-- Usage (run in psql):
-- 1) Set messages level to DEBUG (and keep logging level higher, to avoid having password in logs):
-- 1) Make sure the pgcrypto extension is installed:
-- CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- 2) Set messages level to DEBUG (and keep logging level higher, to avoid having password in logs):
-- set client_min_messages to DEBUG;
-- 2) Run interactive script in psql:
-- 3) Run interactive script in psql:
-- \i /path/to/PostgresDBA/roles/alter_user_with_random_password.psql

\prompt "Username?" postgres_dba_username
Expand Down Expand Up @@ -40,7 +42,8 @@ begin
allowed_len := length(allowed);
pwd := '';
while length(pwd) < 16 loop
j := int4(random() * allowed_len);
-- Using gen_random_bytes from pgcrypto for cryptographically secure randomness
j := get_byte(gen_random_bytes(1), 0) % allowed_len;
pwd := pwd || substr(allowed, j+1, 1);
end loop;
sql := 'alter role ' || current_setting('postgres_dba.username')::text || ' password ''' || pwd || ''';';
Expand Down
9 changes: 6 additions & 3 deletions roles/create_user_with_random_password.psql
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
-- This interactive script solves this problem.

-- Usage (run in psql):
-- 1) Set messages level to DEBUG (and keep logging level higher, to avoid having password in logs):
-- 1) Make sure the pgcrypto extension is installed:
-- CREATE EXTENSION IF NOT EXISTS pgcrypto;
-- 2) Set messages level to DEBUG (and keep logging level higher, to avoid having password in logs):
-- set client_min_messages to DEBUG;
-- 2) Run interactive script in psql:
-- 3) Run interactive script in psql:
-- \i /path/to/PostgresDBA/roles/create_user_with_random_password.psql

\prompt "Username?" postgres_dba_username
Expand Down Expand Up @@ -40,7 +42,8 @@ begin
allowed_len := length(allowed);
pwd := '';
while length(pwd) < 16 loop
j := int4(random() * allowed_len);
-- Using gen_random_bytes from pgcrypto for cryptographically secure randomness
j := get_byte(gen_random_bytes(1), 0) % allowed_len;
pwd := pwd || substr(allowed, j+1, 1);
end loop;
sql := 'create role ' || current_setting('postgres_dba.username')::text
Expand Down