Skip to content

Fix an issue auto-provisioned PostgreSQL user may keep old roles indefinitely#33969

Merged
greedy52 merged 2 commits intomasterfrom
STeve/p998_user_active_connection_role_changed
Nov 1, 2023
Merged

Fix an issue auto-provisioned PostgreSQL user may keep old roles indefinitely#33969
greedy52 merged 2 commits intomasterfrom
STeve/p998_user_active_connection_role_changed

Conversation

@greedy52
Copy link
Copy Markdown
Contributor

@greedy52 greedy52 commented Oct 27, 2023

changelog: Fix an issue auto-provisioned PostgreSQL user may keep old roles indefinitely

Fixes https://github.com/gravitational/teleport-private/issues/998

change:

        -- If the user has active connections, make sure the provided roles
        -- match what the user currently has.
        IF EXISTS (SELECT usename FROM pg_stat_activity WHERE usename = username) THEN
            SELECT CAST(array_agg(rolname) as varchar[]) INTO cur_roles FROM pg_auth_members JOIN pg_roles ON roleid = oid WHERE member=(SELECT oid FROM pg_roles WHERE rolname = username) AND rolname != 'teleport-auto-user';
            -- "a <@ b" checks if all unique elements in "a" are contained by
            -- "b". Using length check plus "contains" check to avoid sorting.
            IF ARRAY_LENGTH(roles, 1) = ARRAY_LENGTH(cur_roles, 1) AND roles <@ cur_roles THEN
                RETURN;
            END IF;
            RAISE EXCEPTION SQLSTATE 'TP002' USING MESSAGE = 'TP002: User has active connections and roles have changed';
        END IF;

This PR also UPPERCASE the existing SQL scripts to be consistent, see discussion #33307 (comment)

Note that all new implementations of auto-user provisioning (MySQL/MariaDB/Redshift) already include similar fixes for this issue in their initial implementation.

@greedy52 greedy52 force-pushed the STeve/p998_user_active_connection_role_changed branch from f594013 to dd8173a Compare October 27, 2023 15:35
@greedy52 greedy52 marked this pull request as ready for review October 27, 2023 15:41
@github-actions
Copy link
Copy Markdown
Contributor

The PR changelog entry failed validation: Changelog entry not found in the PR body. Please add a "no-changelog" label to the PR, or changelog lines starting with changelog: followed by the changelog entries for the PR.

@github-actions github-actions Bot added database-access Database access related issues and PRs size/sm labels Oct 27, 2023
Copy link
Copy Markdown
Contributor

@gabrielcorado gabrielcorado left a comment

Choose a reason for hiding this comment

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

LGTM.

@greedy52 greedy52 added this pull request to the merge queue Nov 1, 2023
Merged via the queue into master with commit 9da4362 Nov 1, 2023
@greedy52 greedy52 deleted the STeve/p998_user_active_connection_role_changed branch November 1, 2023 17:00
@public-teleport-github-review-bot
Copy link
Copy Markdown

@greedy52 See the table below for backport results.

Branch Result
branch/v13 Failed
branch/v14 Create PR

greedy52 added a commit that referenced this pull request Nov 1, 2023
…finitely (#33969)

* Fix an issue auto-provisioned PostgreSQL user may keep old roles indefinitely

* upper case the sql scripts
github-merge-queue Bot pushed a commit that referenced this pull request Nov 7, 2023
…finitely (#33969) (#34120)

* Fix an issue auto-provisioned PostgreSQL user may keep old roles indefinitely

* upper case the sql scripts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

database-access Database access related issues and PRs security Security Issues size/sm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants