postgresql_passchanger_func.../passchanger_rds.sql

157 lines
5.5 KiB
MySQL
Raw Permalink Normal View History

2022-05-06 09:18:52 +00:00
-- Schema creation
create schema dba ;
-- role creation
create role dba with NOLOGIN NOINHERIT ;
-- grants for dba
2022-05-09 16:12:54 +00:00
-- GRANT rds_superuser TO dba ;
2022-05-06 09:18:52 +00:00
2022-05-06 15:52:04 +00:00
-- grant select on pg_catalog.pg_authid to dba ;
2022-05-09 16:12:54 +00:00
-- grant pg_read_all_stats to dba ;
2022-05-06 09:18:52 +00:00
-- password history table
CREATE TABLE IF NOT EXISTS dba.pwdhistory
(
usename character varying COLLATE pg_catalog."default",
usename_addres character varying COLLATE pg_catalog."default",
application_name character varying COLLATE pg_catalog."default",
password character varying COLLATE pg_catalog."default",
changed_on timestamp without time zone
)
TABLESPACE pg_default;
-- alter if you come from a previous version of the table:
-- alter table dba.pwdhistory add column usename_addres character varying ;
-- alter table dba.pwdhistory add column application_name character varying ;
ALTER TABLE IF EXISTS dba.pwdhistory
OWNER to dba;
-- ######################################
-- ######################################
-- real functions
-- ######################################
-- ######################################
2022-05-10 09:04:24 +00:00
drop function if exists dba.change_valid_until ;
2022-05-06 15:52:04 +00:00
2022-05-06 09:18:52 +00:00
CREATE OR REPLACE FUNCTION dba.change_valid_until(_usename text, _thepassword text)
RETURNS integer
SECURITY DEFINER
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
declare
2022-05-09 15:56:08 +00:00
_invokingfunction text := '';
_matches text;
2022-05-10 09:08:27 +00:00
_password_lifetime integer := 120 ; -- specify password lifetime in days
2022-05-10 09:20:19 +00:00
_retval integer;
_expiration_epoch numeric ;
_expiration_date timestamp ;
2022-05-06 09:18:52 +00:00
begin
2022-05-10 09:20:19 +00:00
select extract(epoch from localtimestamp) into _expiration_epoch;
select _expiration_epoch+(_password_lifetime*24*60*60) into _expiration_epoch;
select to_timestamp(_expiration_epoch) into _expiration_date ;
2022-05-10 09:08:27 +00:00
2022-05-06 09:18:52 +00:00
select query into _invokingfunction from pg_stat_activity where pid = pg_backend_pid() ;
2022-05-09 15:56:08 +00:00
-- first, checking the invoking function
2022-05-06 15:52:04 +00:00
_matches := regexp_matches(_invokingfunction, E'select dba\.change_my_password\\(.*\\)[[:space:]]{0,};' , 'i');
2022-05-06 09:18:52 +00:00
if _matches IS NOT NULL then
2022-05-09 15:56:08 +00:00
-- then checking the regex for the password
_matches := regexp_matches(_invokingfunction, E'select dba\.change_my_password\\([[:space:]]{0,}''([[:alnum:]]|@|\\$|#|%|\\^|&|\\*|\\(|\\)|\\_|\\+|\\{|\\}|\\||<|>|\\?|=|!){11,100}''[[:space:]]{0,}\\)[[:space:]]{0,};' , 'i');
2022-05-06 15:52:04 +00:00
if _matches IS NOT NULL then
2022-05-10 09:20:19 +00:00
-- EXECUTE format('ALTER ROLE %I WITH PASSWORD %L VALID UNTIL to_timestamp(%L) ;', _usename, _thepassword, _expiration_date);
EXECUTE format('ALTER ROLE %I WITH PASSWORD %L VALID UNTIL %L ;', _usename, _thepassword, _expiration_date);
2022-05-06 15:52:04 +00:00
-- INTO _retval;
RETURN 0;
else
raise exception 'Regular expresion for password check failed'
using errcode = '22023' -- 22023 = "invalid_parameter_value'
, detail = 'Check your generated password (' || _thepassword || ') an try again'
, hint = 'Read the official documentation' ;
2022-05-10 09:08:27 +00:00
RETURN 1;
2022-05-06 15:52:04 +00:00
end if;
2022-05-09 15:56:08 +00:00
else
-- also catches NULL
2022-05-06 09:18:52 +00:00
-- raise custom error
raise exception 'You''re not allowed to run this function directly'
using errcode = '22023' -- 22023 = "invalid_parameter_value'
, detail = 'Please call dba.change_my_password function.'
, hint = 'Invoked function: ' || _invokingfunction ;
2022-05-10 09:08:27 +00:00
RETURN 1;
2022-05-06 09:18:52 +00:00
end if;
end
$BODY$;
2022-05-09 16:02:43 +00:00
-- ALTER FUNCTION dba.change_valid_until(text, text) OWNER TO dba;
2022-05-06 15:52:04 +00:00
REVOKE EXECUTE ON FUNCTION dba.change_valid_until(text, text) From PUBLIC;
2022-05-06 09:18:52 +00:00
2022-05-10 09:20:19 +00:00
drop function if exists dba.change_my_password ;
2022-05-06 09:18:52 +00:00
CREATE OR REPLACE FUNCTION dba.change_my_password(_password text)
RETURNS integer
SECURITY INVOKER
LANGUAGE 'plpgsql'
COST 100
VOLATILE PARALLEL UNSAFE
AS $BODY$
declare
2022-05-09 15:56:08 +00:00
_min_password_length int := 12; -- specify min length here
_usename text := '';
_useraddress text := '';
_userapp text := '';
2022-05-10 09:08:27 +00:00
_retval integer ;
2022-05-06 09:18:52 +00:00
begin
2022-05-09 15:56:08 +00:00
select user into _usename;
if user = 'postgres' then
raise exception 'This function should not be run by user postgres'
using errcode = '22024' -- 22023 = "invalid_parameter_value'
2022-05-06 09:18:52 +00:00
, detail = 'Use a named user only.' ;
2022-05-10 09:20:19 +00:00
return 1;
2022-05-09 15:56:08 +00:00
end if;
2022-05-06 09:18:52 +00:00
2022-05-09 15:56:08 +00:00
if length(_password) < _min_password_length then
2022-05-06 09:18:52 +00:00
-- also catches NULL
-- raise custom error
raise exception 'Password too short!'
using errcode = '22023' -- 22023 = "invalid_parameter_value'
, detail = 'Please check your password.'
, hint = 'Password must be at least ' || _min_password_length || ' characters.';
2022-05-10 09:20:19 +00:00
return 1;
2022-05-09 15:56:08 +00:00
end if;
2022-05-06 09:18:52 +00:00
2022-05-09 15:56:08 +00:00
select client_addr into _useraddress from pg_stat_activity where pid = pg_backend_pid() ;
select application_name into _userapp from pg_stat_activity where pid = pg_backend_pid() ;
2022-05-06 09:18:52 +00:00
2022-05-10 09:20:19 +00:00
--PERFORM dba.change_valid_until(_usename, _password) ;
SELECT dba.change_valid_until(_usename, _password)
INTO _retval;
if _retval = 0 then
insert into dba.pwdhistory
(usename, usename_addres, application_name, password, changed_on)
values (_usename, _useraddress, _userapp, md5(_password),now());
raise notice 'Password changed' ;
else
raise exception 'Could not change expiration date, please check'
using errcode = '22023' -- 22023 = "invalid_parameter_value'
, detail = 'contact the dba' ;
return 1;
end if;
2022-05-06 09:18:52 +00:00
2022-05-06 15:52:04 +00:00
return 0;
2022-05-06 09:18:52 +00:00
end
$BODY$;
2022-05-06 15:52:04 +00:00
ALTER FUNCTION dba.change_my_password(text) OWNER TO dba;
2022-05-06 09:18:52 +00:00
REVOKE EXECUTE ON FUNCTION dba.change_my_password(text) From PUBLIC;