Skip to content
Merged
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
4 changes: 3 additions & 1 deletion lib/srv/db/postgres/sql/redshift-activate-user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ BEGIN
-- it, otherwise provision a new one.
IF EXISTS (SELECT user_id FROM svv_user_grants WHERE user_name = username AND admin_option = false AND role_name = 'teleport-auto-user') THEN
-- If the user has active connections, make sure the provided roles
-- match what the user currently has.
-- match what the user currently has. Update to pg_stat_activity is
-- delayed for a few hundred ms. Use stv_sessions instead:
-- https://docs.aws.amazon.com/redshift/latest/dg/r_STV_SESSIONS.html
IF EXISTS (SELECT user_name FROM stv_sessions WHERE user_name = CONCAT('IAM:', username)) THEN
SELECT INTO cur_roles_length COUNT(role_name) FROM svv_user_grants WHERE user_name = username AND admin_option=false AND role_name != 'teleport-auto-user';
IF roles_length != cur_roles_length THEN
Expand Down
3 changes: 2 additions & 1 deletion lib/srv/db/postgres/sql/redshift-deactivate-user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ DECLARE
BEGIN
-- Only deactivate if the user doesn't have other active sessions.
-- Update to pg_stat_activity is delayed for a few hundred ms. Use
-- stv_sessions instead.
-- stv_sessions instead:
-- https://docs.aws.amazon.com/redshift/latest/dg/r_STV_SESSIONS.html
IF EXISTS (SELECT user_name FROM stv_sessions WHERE user_name = CONCAT('IAM:', username)) THEN
RAISE EXCEPTION 'TP000: User has active connections';
ELSE
Expand Down
7 changes: 5 additions & 2 deletions lib/srv/db/postgres/sql/redshift-delete-user.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ LANGUAGE plpgsql
AS $$
BEGIN
-- Only drop if the user doesn't have other active sessions.
IF EXISTS (SELECT usename FROM pg_stat_activity WHERE usename = username) THEN
RAISE NOTICE 'User has active connections';
-- Update to pg_stat_activity is delayed for a few hundred ms. Use
-- stv_sessions instead:
-- https://docs.aws.amazon.com/redshift/latest/dg/r_STV_SESSIONS.html
IF EXISTS (SELECT user_name FROM stv_sessions WHERE user_name = CONCAT('IAM:', username)) THEN
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Maybe include a link to https://docs.aws.amazon.com/redshift/latest/dg/r_STV_SESSIONS.html that explains what this table is? I had to look it up.

RAISE EXCEPTION 'TP000: User has active connections';
Comment on lines +9 to +10
Copy link
Copy Markdown
Contributor Author

@greedy52 greedy52 Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied from:

-- Only deactivate if the user doesn't have other active sessions.
-- Update to pg_stat_activity is delayed for a few hundred ms. Use
-- stv_sessions instead.
IF EXISTS (SELECT user_name FROM stv_sessions WHERE user_name = CONCAT('IAM:', username)) THEN
RAISE EXCEPTION 'TP000: User has active connections';

Note the bug happens because pg_stat_activity is sometimes delayed so that the procedure will RAISE NOTICE 'User has active connections';. However, RAISE NOTICE does not trigger an error in our database engine with Redshift.

ELSE
BEGIN
EXECUTE 'DROP USER ' || QUOTE_IDENT(username);
Expand Down